1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2002 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
27 #include "BufferView.h"
28 #include "frontends/Painter.h"
29 #include "LaTeXFeatures.h"
30 #include "insets/insettabular.h"
31 #include "insets/insettext.h"
32 #include "support/lstrings.h"
33 #include "support/lyxmanip.h"
34 #include "support/LAssert.h"
35 #include "frontends/Alert.h"
37 #include "tabular_funcs.h"
51 #ifndef CXX_GLOBAL_CSTD
57 int const WIDTH_OF_LINE = 5;
61 /// Define a few methods for the inner structs
63 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
68 multicolumn = LyXTabular::CELL_NORMAL;
69 alignment = LYX_ALIGN_CENTER;
70 valignment = LYX_VALIGN_TOP;
80 LyXTabular::rowstruct::rowstruct()
94 LyXTabular::columnstruct::columnstruct()
98 alignment = LYX_ALIGN_CENTER;
99 valignment = LYX_VALIGN_TOP;
104 LyXTabular::lttype::lttype()
113 LyXTabular::LyXTabular(BufferParams const & bp,
114 InsetTabular * inset, int rows_arg, int columns_arg)
118 Init(bp, rows_arg, columns_arg);
122 LyXTabular::LyXTabular(BufferParams const & bp,
123 InsetTabular * inset, LyXTabular const & lt,
128 Init(bp, lt.rows_, lt.columns_, <);
129 // we really should change again to have InsetText as a pointer
130 // and allocate it then we would not have to do this stuff all
133 for (int i = 0; i < rows_; ++i) {
134 for (int j = 0; j < columns_; ++j) {
135 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
136 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
142 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
149 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
157 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
160 #warning This while method should look like this: (Lgb)
165 // If this and lt is not of the same size we have a serious bug
166 // So then it is ok to throw an exception, or for now
168 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
170 cell_info = lt.cell_info;
171 row_info = lt.row_info;
172 column_info = lt.column_info;
173 SetLongTabular(lt.is_long_tabular);
182 LyXTabular * LyXTabular::clone(BufferParams const & bp,
183 InsetTabular * inset, bool same_id)
185 LyXTabular * result = new LyXTabular(bp, inset, *this, same_id);
187 // don't know if this is good but I need to Clone also
188 // the text-insets here, this is for the Undo-facility!
189 for (int i = 0; i < rows_; ++i) {
190 for (int j = 0; j < columns_; ++j) {
191 result->cell_info[i][j].inset = cell_info[i][j].inset;
192 result->cell_info[i][j].inset.setOwner(inset);
200 /* activates all lines and sets all widths to 0 */
201 void LyXTabular::Init(BufferParams const & bp,
202 int rows_arg, int columns_arg, LyXTabular const * lt)
205 columns_ = columns_arg;
206 row_info = row_vector(rows_, rowstruct());
207 column_info = column_vector(columns_, columnstruct());
208 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
216 for (int i = 0; i < rows_; ++i) {
217 for (int j = 0; j < columns_; ++j) {
218 cell_info[i][j].inset.setOwner(owner_);
219 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
220 cell_info[i][j].cellno = cellno++;
222 cell_info[i].back().right_line = true;
224 row_info.back().bottom_line = true;
225 row_info.front().bottom_line = true;
227 for (int i = 0; i < columns_; ++i) {
228 calculate_width_of_column(i);
230 column_info.back().right_line = true;
232 calculate_width_of_tabular();
234 rowofcell = vector<int>();
235 columnofcell = vector<int>();
236 set_row_column_number_info();
237 is_long_tabular = false;
242 void LyXTabular::AppendRow(BufferParams const & bp, int cell)
246 int row = row_of_cell(cell);
248 row_vector::iterator rit = row_info.begin() + row;
249 row_info.insert(rit, rowstruct());
250 // now set the values of the row before
251 row_info[row] = row_info[row + 1];
254 cell_vvector::iterator cit = cell_info.begin() + row;
255 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
257 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
260 for (int i = 0; i <= row; ++i) {
261 for (int j = 0; j < columns_; ++j) {
262 c_info[i][j] = cell_info[i][j];
265 for (int i = row + 1; i < rows_; ++i) {
266 for (int j = 0; j < columns_; ++j) {
267 c_info[i][j] = cell_info[i-1][j];
272 for (int j = 0; j < columns_; ++j) {
273 cell_info[row][j].inset.clear(false);
274 if (bp.tracking_changes)
275 cell_info[row][j].inset.markNew(true);
282 void LyXTabular::DeleteRow(int row)
284 if (rows_ == 1) return; // Not allowed to delete last row
286 row_info.erase(row_info.begin() + row); //&row_info[row]);
287 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
293 void LyXTabular::AppendColumn(BufferParams const & bp, int cell)
297 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
299 int const column = column_of_cell(cell);
300 column_vector::iterator cit = column_info.begin() + column + 1;
301 column_info.insert(cit, columnstruct());
302 // set the column values of the column before
303 column_info[column + 1] = column_info[column];
305 for (int i = 0; i < rows_; ++i) {
306 for (int j = 0; j <= column; ++j) {
307 c_info[i][j] = cell_info[i][j];
309 for (int j = column + 1; j < columns_; ++j) {
310 c_info[i][j] = cell_info[i][j - 1];
312 // care about multicolumns
313 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
315 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
317 if ((column + 2) >= columns_ ||
318 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
320 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
325 for (int i = 0; i < rows_; ++i) {
326 cell_info[i][column + 1].inset.clear(false);
327 if (bp.tracking_changes)
328 cell_info[i][column + 1].inset.markNew(true);
334 void LyXTabular::DeleteColumn(int column)
336 // Similar to DeleteRow
337 //if (!(columns_ - 1))
339 if (columns_ == 1) return; // Not allowed to delete last column
341 column_info.erase(column_info.begin() + column);
342 for (int i = 0; i < rows_; ++i) {
343 cell_info[i].erase(cell_info[i].begin() + column);
350 void LyXTabular::reinit()
356 void LyXTabular::Reinit(bool reset_widths)
359 for (int i = 0; i < rows_; ++i) {
360 for (int j = 0; j < columns_; ++j) {
361 cell_info[i][j].width_of_cell = 0;
362 cell_info[i][j].inset.setOwner(owner_);
367 for (int i = 0; i < columns_; ++i) {
368 calculate_width_of_column(i);
370 calculate_width_of_tabular();
372 set_row_column_number_info();
376 void LyXTabular::set_row_column_number_info(bool oldformat)
379 for (int row = 0; row < rows_; ++row) {
380 for (int column = 0; column<columns_; ++column) {
381 if (cell_info[row][column].multicolumn
382 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
384 cell_info[row][column].cellno = numberofcells;
387 ++numberofcells; // because this is one more than as we start from 0
389 rowofcell.resize(numberofcells);
390 columnofcell.resize(numberofcells);
392 for (int row = 0, column = 0, c = 0;
393 c < numberofcells && row < rows_ && column < columns_;) {
395 columnofcell[c] = column;
399 } while (column < columns_ &&
400 cell_info[row][column].multicolumn
401 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
402 if (column == columns_) {
408 for (int row = 0; row < rows_; ++row) {
409 for (int column = 0; column < columns_; ++column) {
410 if (IsPartOfMultiColumn(row,column))
412 // now set the right line of multicolumns right for oldformat read
414 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
416 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
417 cell_info[row][column].right_line =
418 cell_info[row][column+cn-1].right_line;
420 cell_info[row][column].inset.setAutoBreakRows(
421 !GetPWidth(GetCellNumber(row, column)).zero());
427 int LyXTabular::GetNumberOfCells() const
429 return numberofcells;
433 int LyXTabular::NumberOfCellsInRow(int cell) const
435 int const row = row_of_cell(cell);
437 for (int i = 0; i < columns_; ++i) {
438 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
445 /* returns 1 if there is a topline, returns 0 if not */
446 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
448 int const row = row_of_cell(cell);
450 if (!onlycolumn && IsMultiColumn(cell))
451 return cellinfo_of_cell(cell)->top_line;
452 return row_info[row].top_line;
456 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
458 // no bottom line underneath non-existent cells if you please
459 // Isn't that a programming error? Is so this should
460 // be an Assert instead. (Lgb)
461 if (cell >= numberofcells)
464 if (!onlycolumn && IsMultiColumn(cell))
465 return cellinfo_of_cell(cell)->bottom_line;
466 return row_info[row_of_cell(cell)].bottom_line;
470 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
472 if (!onlycolumn && IsMultiColumn(cell) &&
473 (IsFirstCellInRow(cell) || IsMultiColumn(cell-1)))
475 #ifdef SPECIAL_COLUM_HANDLING
476 if (cellinfo_of_cell(cell)->align_special.empty())
477 return cellinfo_of_cell(cell)->left_line;
478 return prefixIs(ltrim(cellinfo_of_cell(cell)->align_special), "|");
480 return cellinfo_of_cell(cell)->left_line;
483 #ifdef SPECIAL_COLUM_HANDLING
484 if (column_info[column_of_cell(cell)].align_special.empty())
485 return column_info[column_of_cell(cell)].left_line;
486 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
488 return column_info[column_of_cell(cell)].left_line;
493 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
495 if (!onlycolumn && IsMultiColumn(cell) &&
496 (IsLastCellInRow(cell) || IsMultiColumn(cell+1)))
498 #ifdef SPECIAL_COLUM_HANDLING
499 if (cellinfo_of_cell(cell)->align_special.empty())
500 return cellinfo_of_cell(cell)->right_line;
501 return suffixIs(rtrim(cellinfo_of_cell(cell)->align_special), "|");
503 return cellinfo_of_cell(cell)->right_line;
506 #ifdef SPECIAL_COLUM_HANDLING
507 if (column_info[column_of_cell(cell)].align_special.empty())
508 return column_info[right_column_of_cell(cell)].right_line;
509 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
511 return column_info[right_column_of_cell(cell)].right_line;
516 bool LyXTabular::TopAlreadyDrawed(int cell) const
518 int row = row_of_cell(cell);
519 if ((row > 0) && !GetAdditionalHeight(row)) {
520 int column = column_of_cell(cell);
523 && cell_info[row][column].multicolumn
524 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
526 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
527 return row_info[row].bottom_line;
529 return cell_info[row][column].bottom_line;
535 bool LyXTabular::LeftAlreadyDrawed(int cell) const
537 int column = column_of_cell(cell);
539 int row = row_of_cell(cell);
541 (cell_info[row][column].multicolumn ==
542 LyXTabular::CELL_PART_OF_MULTICOLUMN));
543 if (GetAdditionalWidth(cell_info[row][column].cellno))
545 #ifdef SPECIAL_COLUM_HANDLING
546 return RightLine(cell_info[row][column].cellno);
548 return RightLine(cell_info[row][column].cellno, true);
555 bool LyXTabular::IsLastRow(int cell) const
557 return (row_of_cell(cell) == rows_ - 1);
561 int LyXTabular::GetAdditionalHeight(int row) const
563 if (!row || row >= rows_)
569 for (int column = 0; column < columns_ && bottom; ++column) {
570 switch (cell_info[row - 1][column].multicolumn) {
571 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
572 bottom = cell_info[row - 1][column].bottom_line;
574 case LyXTabular::CELL_NORMAL:
575 bottom = row_info[row - 1].bottom_line;
578 for (int column = 0; column < columns_ && top; ++column) {
579 switch (cell_info[row][column].multicolumn) {
580 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
581 top = cell_info[row][column].top_line;
583 case LyXTabular::CELL_NORMAL:
584 top = row_info[row].top_line;
588 return WIDTH_OF_LINE;
593 int LyXTabular::GetAdditionalWidth(int cell) const
595 // internally already set in SetWidthOfCell
596 // used to get it back in text.C
597 int const col = right_column_of_cell(cell);
598 int const row = row_of_cell(cell);
599 if (col < columns_ - 1 && RightLine(cell) &&
600 LeftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
602 return WIDTH_OF_LINE;
609 // returns the maximum over all rows
610 int LyXTabular::GetWidthOfColumn(int cell) const
612 int const column1 = column_of_cell(cell);
613 int const column2 = right_column_of_cell(cell);
615 for (int i = column1; i <= column2; ++i) {
616 result += column_info[i].width_of_column;
622 int LyXTabular::GetWidthOfTabular() const
624 return width_of_tabular;
628 /* returns 1 if a complete update is necessary, otherwise 0 */
629 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
631 if (!IsMultiColumn(cell))
634 int const row = row_of_cell(cell);
635 int const column1 = column_of_cell(cell);
636 int const column2 = right_column_of_cell(cell);
637 int const old_val = cell_info[row][column2].width_of_cell;
639 // first set columns to 0 so we can calculate the right width
640 for (int i = column1; i <= column2; ++i) {
641 cell_info[row][i].width_of_cell = 0;
643 // set the width to MAX_WIDTH until width > 0
644 int width = (new_width + 2 * WIDTH_OF_LINE);
646 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
647 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
648 width -= column_info[i].width_of_column;
651 cell_info[row][i].width_of_cell = width;
653 if (old_val != cell_info[row][column2].width_of_cell) {
654 // in this case we have to recalculate all multicolumn cells which
655 // have this column as one of theirs but not as last one
656 calculate_width_of_column_NMC(i);
657 recalculateMulticolumnsOfColumn(i);
658 calculate_width_of_column(i);
664 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
666 // the last column does not have to be recalculated because all
667 // multicolumns will have here there last multicolumn cell which
668 // always will have the whole rest of the width of the cell.
669 if (column > (columns_ - 2))
671 for(int row = 0; row < rows_; ++row) {
672 int mc = cell_info[row][column].multicolumn;
673 int nmc = cell_info[row][column+1].multicolumn;
674 // we only have to update multicolumns which do not have this
675 // column as their last column!
676 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
677 ((mc == CELL_PART_OF_MULTICOLUMN) &&
678 (nmc == CELL_PART_OF_MULTICOLUMN)))
680 int const cellno = cell_info[row][column].cellno;
681 SetWidthOfMulticolCell(cellno,
682 GetWidthOfCell(cellno)-(2 * WIDTH_OF_LINE));
688 /* returns 1 if a complete update is necessary, otherwise 0 */
689 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
691 int const row = row_of_cell(cell);
692 int const column1 = column_of_cell(cell);
697 #ifdef SPECIAL_COLUM_HANDLING
698 if (RightLine(cell_info[row][column1].cellno, true) &&
699 (column1 < columns_-1) &&
700 LeftLine(cell_info[row][column1+1].cellno, true))
702 if (column_info[column1].right_line && (column1 < columns_-1) &&
703 column_info[column1+1].left_line) // additional width
707 add_width = WIDTH_OF_LINE;
709 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
712 if (IsMultiColumn(cell, true)) {
713 tmp = SetWidthOfMulticolCell(cell, new_width);
715 width = (new_width + 2*WIDTH_OF_LINE + add_width);
716 cell_info[row][column1].width_of_cell = width;
717 tmp = calculate_width_of_column_NMC(column1);
719 recalculateMulticolumnsOfColumn(column1);
722 for (int i = 0; i < columns_; ++i)
723 calculate_width_of_column(i);
724 calculate_width_of_tabular();
731 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
733 if (!IsMultiColumn(cell) || onlycolumn)
734 column_info[column_of_cell(cell)].alignment = align;
736 cellinfo_of_cell(cell)->alignment = align;
741 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
743 if (!IsMultiColumn(cell) || onlycolumn)
744 column_info[column_of_cell(cell)].valignment = align;
746 cellinfo_of_cell(cell)->valignment = align;
751 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
753 bool flag = !width.zero();
754 int const j = column_of_cell(cell);
756 column_info[j].p_width = width;
757 // This should not ne necessary anymore
758 // if (flag) // do this only if there is a width
759 // SetAlignment(cell, LYX_ALIGN_LEFT);
760 for (int i = 0; i < rows_; ++i) {
761 int c = GetCellNumber(i, j);
762 flag = !GetPWidth(c).zero(); // because of multicolumns!
763 GetCellInset(c)->setAutoBreakRows(flag);
769 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
771 bool const flag = !width.zero();
773 cellinfo_of_cell(cell)->p_width = width;
774 if (IsMultiColumn(cell)) {
775 GetCellInset(cell)->setAutoBreakRows(flag);
782 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
783 LyXTabular::Feature what)
785 if (what == SET_SPECIAL_MULTI)
786 cellinfo_of_cell(cell)->align_special = special;
788 column_info[column_of_cell(cell)].align_special = special;
793 bool LyXTabular::SetAllLines(int cell, bool line)
795 SetTopLine(cell, line);
796 SetBottomLine(cell, line);
797 SetRightLine(cell, line);
798 SetLeftLine(cell, line);
803 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
805 int const row = row_of_cell(cell);
807 if (onlycolumn || !IsMultiColumn(cell))
808 row_info[row].top_line = line;
810 cellinfo_of_cell(cell)->top_line = line;
815 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
817 if (onlycolumn || !IsMultiColumn(cell))
818 row_info[row_of_cell(cell)].bottom_line = line;
820 cellinfo_of_cell(cell)->bottom_line = line;
825 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
827 if (onlycolumn || !IsMultiColumn(cell))
828 column_info[column_of_cell(cell)].left_line = line;
830 cellinfo_of_cell(cell)->left_line = line;
835 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
837 if (onlycolumn || !IsMultiColumn(cell))
838 column_info[right_column_of_cell(cell)].right_line = line;
840 cellinfo_of_cell(cell)->right_line = line;
845 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
847 if (!onlycolumn && IsMultiColumn(cell))
848 return cellinfo_of_cell(cell)->alignment;
850 return column_info[column_of_cell(cell)].alignment;
854 LyXTabular::VAlignment
855 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
857 if (!onlycolumn && IsMultiColumn(cell))
858 return cellinfo_of_cell(cell)->valignment;
860 return column_info[column_of_cell(cell)].valignment;
864 LyXLength const LyXTabular::GetPWidth(int cell) const
866 if (IsMultiColumn(cell))
867 return cellinfo_of_cell(cell)->p_width;
868 return column_info[column_of_cell(cell)].p_width;
872 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
874 return column_info[column_of_cell(cell)].p_width;
878 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
880 if (IsMultiColumn(cell))
881 return cellinfo_of_cell(cell)->p_width;
886 string const LyXTabular::GetAlignSpecial(int cell, int what) const
888 if (what == SET_SPECIAL_MULTI)
889 return cellinfo_of_cell(cell)->align_special;
890 return column_info[column_of_cell(cell)].align_special;
894 int LyXTabular::GetWidthOfCell(int cell) const
896 int const row = row_of_cell(cell);
897 int const column1 = column_of_cell(cell);
898 int const column2 = right_column_of_cell(cell);
900 for (int i = column1; i <= column2; ++i) {
901 result += cell_info[row][i].width_of_cell;
907 int LyXTabular::GetBeginningOfTextInCell(int cell) const
911 switch (GetAlignment(cell)) {
912 case LYX_ALIGN_CENTER:
913 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
915 case LYX_ALIGN_RIGHT:
916 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
917 // + GetAdditionalWidth(cell);
919 default: /* LYX_ALIGN_LEFT: nothing :-) */
929 bool LyXTabular::IsFirstCellInRow(int cell) const
931 return column_of_cell(cell) == 0;
935 int LyXTabular::GetFirstCellInRow(int row) const
939 return cell_info[row][0].cellno;
942 bool LyXTabular::IsLastCellInRow(int cell) const
944 return (right_column_of_cell(cell) == (columns_ - 1));
948 int LyXTabular::GetLastCellInRow(int row) const
952 return cell_info[row][columns_-1].cellno;
956 bool LyXTabular::calculate_width_of_column(int column)
958 int const old_column_width = column_info[column].width_of_column;
961 for (int i = 0; i < rows_; ++i) {
962 maximum = max(cell_info[i][column].width_of_cell, maximum);
964 column_info[column].width_of_column = maximum;
965 return (column_info[column].width_of_column != old_column_width);
970 // Calculate the columns regarding ONLY the normal cells and if this
971 // column is inside a multicolumn cell then use it only if its the last
972 // column of this multicolumn cell as this gives an added with to the
973 // column, all the rest should be adapted!
975 bool LyXTabular::calculate_width_of_column_NMC(int column)
977 int const old_column_width = column_info[column].width_of_column;
979 for (int i = 0; i < rows_; ++i) {
980 int cell = GetCellNumber(i, column);
981 bool ismulti = IsMultiColumn(cell, true);
982 if ((!ismulti || (column == right_column_of_cell(cell))) &&
983 (cell_info[i][column].width_of_cell > max))
985 max = cell_info[i][column].width_of_cell;
988 column_info[column].width_of_column = max;
989 return (column_info[column].width_of_column != old_column_width);
993 void LyXTabular::calculate_width_of_tabular()
995 width_of_tabular = 0;
996 for (int i = 0; i < columns_; ++i) {
997 width_of_tabular += column_info[i].width_of_column;
1002 int LyXTabular::row_of_cell(int cell) const
1004 if (cell >= numberofcells)
1008 return rowofcell[cell];
1012 int LyXTabular::column_of_cell(int cell) const
1014 if (cell >= numberofcells)
1015 return columns_ - 1;
1018 return columnofcell[cell];
1022 int LyXTabular::right_column_of_cell(int cell) const
1024 int const row = row_of_cell(cell);
1025 int column = column_of_cell(cell);
1026 while (column < (columns_ - 1) &&
1027 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1033 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1037 << write_attribute("version", 3)
1038 << write_attribute("rows", rows_)
1039 << write_attribute("columns", columns_)
1041 // global longtable options
1043 << write_attribute("rotate", rotate)
1044 << write_attribute("islongtable", is_long_tabular)
1045 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1046 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1047 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1048 << write_attribute("headTopDL", endhead.topDL)
1049 << write_attribute("headBottomDL", endhead.bottomDL)
1050 << write_attribute("footTopDL", endfoot.topDL)
1051 << write_attribute("footBottomDL", endfoot.bottomDL)
1052 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1053 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1054 << write_attribute("lastFootEmpty", endlastfoot.empty)
1056 for (int j = 0; j < columns_; ++j) {
1058 << write_attribute("alignment", column_info[j].alignment)
1059 << write_attribute("valignment", column_info[j].valignment)
1060 << write_attribute("leftline", column_info[j].left_line)
1061 << write_attribute("rightline", column_info[j].right_line)
1062 << write_attribute("width", column_info[j].p_width.asString())
1063 << write_attribute("special", column_info[j].align_special)
1066 for (int i = 0; i < rows_; ++i) {
1068 << write_attribute("topline", row_info[i].top_line)
1069 << write_attribute("bottomline", row_info[i].bottom_line)
1070 << write_attribute("endhead", row_info[i].endhead)
1071 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1072 << write_attribute("endfoot", row_info[i].endfoot)
1073 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1074 << write_attribute("newpage", row_info[i].newpage)
1076 for (int j = 0; j < columns_; ++j) {
1078 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1079 << write_attribute("alignment", cell_info[i][j].alignment)
1080 << write_attribute("valignment", cell_info[i][j].valignment)
1081 << write_attribute("topline", cell_info[i][j].top_line)
1082 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1083 << write_attribute("leftline", cell_info[i][j].left_line)
1084 << write_attribute("rightline", cell_info[i][j].right_line)
1085 << write_attribute("rotate", cell_info[i][j].rotate)
1086 << write_attribute("usebox", cell_info[i][j].usebox)
1087 << write_attribute("width", cell_info[i][j].p_width)
1088 << write_attribute("special", cell_info[i][j].align_special)
1090 os << "\\begin_inset ";
1091 cell_info[i][j].inset.write(buf, os);
1092 os << "\n\\end_inset \n"
1097 os << "</lyxtabular>\n";
1101 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1104 istream & is = lex.getStream();
1106 l_getline(is, line);
1107 if (!prefixIs(line, "<lyxtabular ")
1108 && !prefixIs(line, "<LyXTabular ")) {
1109 OldFormatRead(buf->params, lex, line);
1114 if (!getTokenValue(line, "version", version))
1117 ReadOld(buf, is, lex, line);
1118 else if (version >= 2)
1119 ReadNew(buf, is, lex, line, version);
1122 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1126 row_info[--hr].endhead = true;
1128 // set firstheader info
1129 if (fhr && (fhr < rows_)) {
1130 if (row_info[fhr].endhead) {
1132 row_info[--fhr].endfirsthead = true;
1133 row_info[fhr].endhead = false;
1135 } else if (row_info[fhr - 1].endhead) {
1136 endfirsthead.empty = true;
1138 while((fhr > 0) && !row_info[--fhr].endhead) {
1139 row_info[fhr].endfirsthead = true;
1144 if (fr && (fr < rows_)) {
1145 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1146 while((fr > 0) && !row_info[--fr].endhead) {
1147 row_info[fr].endfoot = true;
1148 row_info[fr].endhead = false;
1150 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1151 while((fr > 0) && !row_info[--fr].endfirsthead) {
1152 row_info[fr].endfoot = true;
1153 row_info[fr].endfirsthead = false;
1155 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1156 while((fr > 0) && !row_info[--fr].endhead &&
1157 !row_info[fr].endfirsthead)
1159 row_info[fr].endfoot = true;
1163 // set lastfooter info
1164 if (lfr && (lfr < rows_)) {
1165 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1166 while((lfr > 0) && !row_info[--lfr].endhead) {
1167 row_info[lfr].endlastfoot = true;
1168 row_info[lfr].endhead = false;
1170 } else if (row_info[lfr].endfirsthead &&
1171 row_info[lfr - 1].endfirsthead)
1173 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1174 row_info[lfr].endlastfoot = true;
1175 row_info[lfr].endfirsthead = false;
1177 } else if (row_info[lfr].endfoot
1178 && row_info[lfr - 1].endfoot) {
1179 while((lfr > 0) && !row_info[--lfr].endfoot) {
1180 row_info[lfr].endlastfoot = true;
1181 row_info[lfr].endfoot = false;
1183 } else if (!row_info[fr - 1].endhead
1184 && !row_info[fr - 1].endfirsthead &&
1185 !row_info[fr - 1].endfoot)
1188 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1189 !row_info[lfr].endfoot)
1191 row_info[lfr].endlastfoot = true;
1193 } else if (haveLTFoot()) {
1194 endlastfoot.empty = true;
1199 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1200 LyXLex & lex, string const & l, int const version)
1204 if (!getTokenValue(line, "rows", rows_arg))
1207 if (!getTokenValue(line, "columns", columns_arg))
1209 Init(buf->params, rows_arg, columns_arg);
1210 l_getline(is, line);
1211 if (!prefixIs(line, "<features")) {
1212 lyxerr << "Wrong tabular format (expected <features ...> got"
1213 << line << ')' << endl;
1216 getTokenValue(line, "rotate", rotate);
1217 getTokenValue(line, "islongtable", is_long_tabular);
1218 // compatibility read for old longtable options. Now we can make any
1219 // row part of the header/footer type we want before it was strict
1220 // sequential from the first row down (as LaTeX does it!). So now when
1221 // we find a header/footer line we have to go up the rows and set it
1222 // on all preceding rows till the first or one with already a h/f option
1223 // set. If we find a firstheader on the same line as a header or a
1224 // lastfooter on the same line as a footer then this should be set empty.
1232 getTokenValue(line, "endhead", hrow);
1233 getTokenValue(line, "endfirsthead", fhrow);
1234 getTokenValue(line, "endfoot", frow);
1235 getTokenValue(line, "endlastfoot", lfrow);
1236 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1238 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1239 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1240 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1241 getTokenValue(line, "headTopDL", endhead.topDL);
1242 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1243 getTokenValue(line, "footTopDL", endfoot.topDL);
1244 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1245 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1246 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1247 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1249 for (int j = 0; j < columns_; ++j) {
1251 if (!prefixIs(line,"<column")) {
1252 lyxerr << "Wrong tabular format (expected <column ...> got"
1253 << line << ')' << endl;
1256 getTokenValue(line, "alignment", column_info[j].alignment);
1257 getTokenValue(line, "valignment", column_info[j].valignment);
1258 getTokenValue(line, "leftline", column_info[j].left_line);
1259 getTokenValue(line, "rightline", column_info[j].right_line);
1260 getTokenValue(line, "width", column_info[j].p_width);
1261 getTokenValue(line, "special", column_info[j].align_special);
1264 for (int i = 0; i < rows_; ++i) {
1265 l_getline(is, line);
1266 if (!prefixIs(line, "<row")) {
1267 lyxerr << "Wrong tabular format (expected <row ...> got"
1268 << line << ')' << endl;
1271 getTokenValue(line, "topline", row_info[i].top_line);
1272 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1273 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1274 getTokenValue(line, "endhead", row_info[i].endhead);
1275 getTokenValue(line, "endfoot", row_info[i].endfoot);
1276 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1277 getTokenValue(line, "newpage", row_info[i].newpage);
1278 for (int j = 0; j < columns_; ++j) {
1279 l_getline(is, line);
1280 if (!prefixIs(line, "<cell")) {
1281 lyxerr << "Wrong tabular format (expected <cell ...> got"
1282 << line << ')' << endl;
1285 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1286 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1287 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1288 getTokenValue(line, "topline", cell_info[i][j].top_line);
1289 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1290 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1291 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1292 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1293 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1294 getTokenValue(line, "width", cell_info[i][j].p_width);
1295 getTokenValue(line, "special", cell_info[i][j].align_special);
1296 l_getline(is, line);
1297 if (prefixIs(line, "\\begin_inset")) {
1298 cell_info[i][j].inset.read(buf, lex);
1299 l_getline(is, line);
1301 if (!prefixIs(line, "</cell>")) {
1302 lyxerr << "Wrong tabular format (expected </cell> got"
1303 << line << ')' << endl;
1307 l_getline(is, line);
1308 if (!prefixIs(line, "</row>")) {
1309 lyxerr << "Wrong tabular format (expected </row> got"
1310 << line << ')' << endl;
1314 while (!prefixIs(line, "</lyxtabular>")) {
1315 l_getline(is, line);
1317 set_row_column_number_info();
1321 void LyXTabular::OldFormatRead(BufferParams const & bp,
1322 LyXLex & lex, string const & fl)
1328 int columns_arg = 0;
1329 int is_long_tabular_arg = false;
1330 int rotate_arg = false;
1339 istream & is = lex.getStream();
1342 version = lyx::atoi(s.substr(8, string::npos));
1346 vector<int> cont_row_info;
1349 lyxerr << "Tabular format < 5 is not supported anymore\n"
1350 "Get an older version of LyX (< 1.1.x) for conversion!"
1352 Alert::alert(_("Warning:"),
1353 _("Tabular format < 5 is not supported anymore\n"),
1354 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1356 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1357 >> rotate_arg >> a >> b >> c >> d;
1359 is >> rows_arg >> columns_arg;
1360 Init(bp, rows_arg, columns_arg);
1361 cont_row_info = vector<int>(rows_arg);
1362 SetLongTabular(is_long_tabular_arg);
1363 SetRotateTabular(rotate_arg);
1365 for (i = 0; i < rows_; ++i) {
1367 cont_row_info[i] = false;
1369 for (i = 0; i < columns_; ++i) {
1372 for (i = 0; i < rows_; ++i) {
1373 for (j = 0; j < columns_; ++j) {
1378 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1379 >> rotate_arg >> a >> b >> c >> d;
1380 Init(bp, rows_arg, columns_arg);
1381 cont_row_info = vector<int>(rows_arg);
1382 SetLongTabular(is_long_tabular_arg);
1383 SetRotateTabular(rotate_arg);
1384 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1385 for (i = 0; i < rows_; ++i) {
1386 a = b = c = d = e = f = g = 0;
1387 is >> a >> b >> c >> d;
1388 row_info[i].top_line = a;
1389 row_info[i].bottom_line = b;
1390 cont_row_info[i] = c;
1391 row_info[i].newpage = d;
1393 for (i = 0; i < columns_; ++i) {
1398 char ch; // skip '"'
1401 // ignore is buggy but we will use it later (Lgb)
1402 is.ignore(); // skip '"'
1404 getline(is, s1, '"');
1406 is >> ch; // skip '"'
1408 // ignore is buggy but we will use it later (Lgb)
1409 is.ignore(); // skip '"'
1411 getline(is, s2, '"');
1412 column_info[i].alignment = static_cast<LyXAlignment>(a);
1413 column_info[i].left_line = b;
1414 column_info[i].right_line = c;
1415 column_info[i].p_width = LyXLength(s1);
1416 column_info[i].align_special = s2;
1418 for (i = 0; i < rows_; ++i) {
1419 for (j = 0; j < columns_; ++j) {
1422 is >> a >> b >> c >> d >> e >> f >> g;
1425 is >> ch; // skip '"'
1427 // ignore is buggy but we will use it later (Lgb)
1428 is.ignore(); // skip '"'
1430 getline(is, s1, '"');
1432 is >> ch; // skip '"'
1434 // ignore is buggy but we will use it later (Lgb)
1435 is.ignore(); // skip '"'
1437 getline(is, s2, '"');
1438 cell_info[i][j].multicolumn = static_cast<char>(a);
1439 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1440 cell_info[i][j].top_line = static_cast<char>(c);
1441 cell_info[i][j].bottom_line = static_cast<char>(d);
1442 cell_info[i][j].left_line = column_info[j].left_line;
1443 cell_info[i][j].right_line = column_info[j].right_line;
1444 cell_info[i][j].rotate = static_cast<bool>(f);
1445 cell_info[i][j].usebox = static_cast<BoxType>(g);
1446 cell_info[i][j].align_special = s1;
1447 cell_info[i][j].p_width = LyXLength(s2);
1451 set_row_column_number_info(true);
1453 Paragraph * par = new Paragraph;
1454 Paragraph * return_par = 0;
1456 par->layout(bp.getLyXTextClass().defaultLayout());
1460 Paragraph::depth_type depth = 0;
1461 LyXFont font(LyXFont::ALL_INHERIT);
1462 font.setLanguage(owner_->bufferOwner()->getLanguage());
1464 while (lex.isOK()) {
1466 string const token = lex.getString();
1469 if (token == "\\layout"
1470 || token == "\\end_float" // this should not exist anymore
1471 || token == "\\end_inset" // as it is substituted by this
1472 || token == "\\end_deeper")
1474 lex.pushToken(token);
1477 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1482 lex.pushToken(token);
1486 lex.printError("New Paragraph allocated! This should not happen!");
1487 lex.pushToken(token);
1493 // now we have the par we should fill the insets with this!
1495 InsetText * inset = GetCellInset(cell);
1498 for (int i = 0; i < par->size(); ++i) {
1499 if (par->isNewline(i)) {
1501 if (cell > numberofcells) {
1502 lyxerr << "Some error in reading old table format occured!" <<
1503 endl << "Terminating when reading cell[" << cell << "]!" <<
1508 row = row_of_cell(cell);
1509 if (cont_row_info[row]) {
1511 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1512 while (!IsFirstCellInRow(--cell));
1514 inset = GetCellInset(cell);
1517 inset = GetCellInset(cell);
1518 row = row_of_cell(cell);
1519 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1521 // insert a space instead
1523 par->insertChar(i, ' ');
1526 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1527 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1534 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1536 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1537 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1541 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1543 int const row = row_of_cell(cell);
1544 int const column = column_of_cell(cell);
1545 return &cell_info[row][column];
1549 void LyXTabular::SetMultiColumn(Buffer const * buffer, int cell, int number)
1551 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1552 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1553 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1554 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1555 cellinfo_of_cell(cell)->right_line = column_info[column_of_cell(cell+number-1)].right_line;
1557 for (int i = 1; i < number; ++i) {
1558 cellinfo_of_cell(cell+i)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1559 cellinfo_of_cell(cell)->inset.appendParagraphs(buffer->params,
1560 cellinfo_of_cell(cell+i)->inset.paragraph());
1561 cellinfo_of_cell(cell+i)->inset.clear(false);
1564 for (number--; number > 0; --number) {
1565 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1568 set_row_column_number_info();
1572 int LyXTabular::cells_in_multicolumn(int cell) const
1574 int const row = row_of_cell(cell);
1575 int column = column_of_cell(cell);
1578 while ((column < columns_) &&
1579 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1588 int LyXTabular::UnsetMultiColumn(int cell)
1590 int const row = row_of_cell(cell);
1591 int column = column_of_cell(cell);
1595 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1596 cell_info[row][column].multicolumn = CELL_NORMAL;
1598 while ((column < columns_) &&
1599 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1601 cell_info[row][column].multicolumn = CELL_NORMAL;
1606 set_row_column_number_info();
1611 void LyXTabular::SetLongTabular(bool what)
1613 is_long_tabular = what;
1617 bool LyXTabular::IsLongTabular() const
1619 return is_long_tabular;
1623 void LyXTabular::SetRotateTabular(bool flag)
1629 bool LyXTabular::GetRotateTabular() const
1635 void LyXTabular::SetRotateCell(int cell, bool flag)
1637 cellinfo_of_cell(cell)->rotate = flag;
1641 bool LyXTabular::GetRotateCell(int cell) const
1643 return cellinfo_of_cell(cell)->rotate;
1647 bool LyXTabular::NeedRotating() const
1651 for (int i = 0; i < rows_; ++i) {
1652 for (int j = 0; j < columns_; ++j) {
1653 if (cell_info[i][j].rotate)
1661 bool LyXTabular::IsLastCell(int cell) const
1663 if ((cell + 1) < numberofcells)
1669 int LyXTabular::GetCellAbove(int cell) const
1671 if (row_of_cell(cell) > 0)
1672 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1677 int LyXTabular::GetCellBelow(int cell) const
1679 if (row_of_cell(cell) + 1 < rows_)
1680 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1685 int LyXTabular::GetLastCellAbove(int cell) const
1687 if (row_of_cell(cell) <= 0)
1689 if (!IsMultiColumn(cell))
1690 return GetCellAbove(cell);
1691 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1695 int LyXTabular::GetLastCellBelow(int cell) const
1697 if (row_of_cell(cell) + 1 >= rows_)
1699 if (!IsMultiColumn(cell))
1700 return GetCellBelow(cell);
1701 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1705 int LyXTabular::GetCellNumber(int row, int column) const
1708 if (column >= columns_)
1709 column = columns_ - 1;
1710 else if (column < 0)
1717 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1719 return cell_info[row][column].cellno;
1723 void LyXTabular::SetUsebox(int cell, BoxType type)
1725 cellinfo_of_cell(cell)->usebox = type;
1729 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1731 if (column_info[column_of_cell(cell)].p_width.zero() &&
1732 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1734 if (cellinfo_of_cell(cell)->usebox > 1)
1735 return cellinfo_of_cell(cell)->usebox;
1736 return UseParbox(cell);
1741 // This are functions used for the longtable support
1743 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1748 row_info[row].endfirsthead = flag;
1752 row_info[row].endhead = flag;
1757 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1760 hd.set = haveLTHead();
1761 return row_info[row].endhead;
1765 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1768 hd.set = haveLTFirstHead();
1769 return row_info[row].endfirsthead;
1773 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1778 row_info[row].endlastfoot = flag;
1782 row_info[row].endfoot = flag;
1787 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1790 fd.set = haveLTFoot();
1791 return row_info[row].endfoot;
1795 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1798 fd.set = haveLTLastFoot();
1799 return row_info[row].endlastfoot;
1803 void LyXTabular::SetLTNewPage(int row, bool what)
1805 row_info[row].newpage = what;
1809 bool LyXTabular::GetLTNewPage(int row) const
1811 return row_info[row].newpage;
1815 bool LyXTabular::haveLTHead() const
1817 for(int i=0; i < rows_; ++i) {
1818 if (row_info[i].endhead)
1825 bool LyXTabular::haveLTFirstHead() const
1827 if (endfirsthead.empty)
1829 for(int i=0; i < rows_; ++i) {
1830 if (row_info[i].endfirsthead)
1837 bool LyXTabular::haveLTFoot() const
1839 for(int i=0; i < rows_; ++i) {
1840 if (row_info[i].endfoot)
1847 bool LyXTabular::haveLTLastFoot() const
1849 if (endlastfoot.empty)
1851 for(int i=0; i < rows_; ++i) {
1852 if (row_info[i].endlastfoot)
1859 // end longtable support functions
1861 bool LyXTabular::SetAscentOfRow(int row, int height)
1863 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1865 row_info[row].ascent_of_row = height;
1870 bool LyXTabular::SetDescentOfRow(int row, int height)
1872 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1874 row_info[row].descent_of_row = height;
1879 int LyXTabular::GetAscentOfRow(int row) const
1883 return row_info[row].ascent_of_row;
1887 int LyXTabular::GetDescentOfRow(int row) const
1891 return row_info[row].descent_of_row;
1895 int LyXTabular::GetHeightOfTabular() const
1899 for (int row = 0; row < rows_; ++row)
1900 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1901 GetAdditionalHeight(row);
1906 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1908 if ((row >= rows_) || (column >= columns_))
1910 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1914 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1916 if ((row < 0) || (row >= rows_))
1919 int const fcell = GetFirstCellInRow(row);
1920 int const n = NumberOfCellsInRow(fcell) + fcell;
1923 for (int i = fcell; i < n; ++i) {
1927 if (tmp == (n - fcell)) {
1930 for (int i = fcell; i < n; ++i) {
1933 << column_of_cell(i) + 1
1935 << right_column_of_cell(i) + 1
1947 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1949 if ((row < 0) || (row >= rows_))
1952 int const fcell = GetFirstCellInRow(row);
1953 int const n = NumberOfCellsInRow(fcell) + fcell;
1956 for (int i = fcell; i < n; ++i) {
1960 if (tmp == (n - fcell)) {
1963 for (int i = fcell; i < n; ++i) {
1964 if (BottomLine(i)) {
1966 << column_of_cell(i) + 1
1968 << right_column_of_cell(i) + 1
1980 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1984 if (GetRotateCell(cell)) {
1985 os << "\\begin{sideways}\n";
1988 if (IsMultiColumn(cell)) {
1989 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1990 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1991 os << cellinfo_of_cell(cell)->align_special << "}{";
1993 if (LeftLine(cell) &&
1994 (IsFirstCellInRow(cell) ||
1995 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1996 !RightLine(cell-1, true))))
2000 if (!GetPWidth(cell).zero()) {
2001 switch (GetVAlignment(cell)) {
2002 case LYX_VALIGN_TOP:
2005 case LYX_VALIGN_CENTER:
2008 case LYX_VALIGN_BOTTOM:
2013 << GetPWidth(cell).asLatexString()
2016 switch (GetAlignment(cell)) {
2017 case LYX_ALIGN_LEFT:
2020 case LYX_ALIGN_RIGHT:
2028 if (RightLine(cell))
2030 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2036 if (GetUsebox(cell) == BOX_PARBOX) {
2038 switch (GetVAlignment(cell)) {
2039 case LYX_VALIGN_TOP:
2042 case LYX_VALIGN_CENTER:
2045 case LYX_VALIGN_BOTTOM:
2049 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2050 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2051 os << "\\begin{minipage}[";
2052 switch (GetVAlignment(cell)) {
2053 case LYX_VALIGN_TOP:
2056 case LYX_VALIGN_CENTER:
2059 case LYX_VALIGN_BOTTOM:
2063 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2070 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2075 if (GetUsebox(cell) == BOX_PARBOX)
2077 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2078 os << "%\n\\end{minipage}";
2081 if (IsMultiColumn(cell)) {
2084 if (GetRotateCell(cell)) {
2085 os << "%\n\\end{sideways}";
2092 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2093 bool fragile, bool fp) const
2095 if (!is_long_tabular)
2099 // output header info
2101 if (endhead.topDL) {
2105 for (int i = 0; i < rows_; ++i) {
2106 if (row_info[i].endhead) {
2107 ret += TeXRow(os, i, buf, fragile, fp);
2110 if (endhead.bottomDL) {
2114 os << "\\endhead\n";
2116 if (endfirsthead.empty) {
2117 os << "\\endfirsthead\n";
2121 // output firstheader info
2122 if (haveLTFirstHead()) {
2123 if (endfirsthead.topDL) {
2127 for (int i = 0; i < rows_; ++i) {
2128 if (row_info[i].endfirsthead) {
2129 ret += TeXRow(os, i, buf, fragile, fp);
2132 if (endfirsthead.bottomDL) {
2136 os << "\\endfirsthead\n";
2139 // output footer info
2141 if (endfoot.topDL) {
2145 for (int i = 0; i < rows_; ++i) {
2146 if (row_info[i].endfoot) {
2147 ret += TeXRow(os, i, buf, fragile, fp);
2150 if (endfoot.bottomDL) {
2154 os << "\\endfoot\n";
2156 if (endlastfoot.empty) {
2157 os << "\\endlastfoot\n";
2161 // output lastfooter info
2162 if (haveLTLastFoot()) {
2163 if (endlastfoot.topDL) {
2167 for (int i = 0; i < rows_; ++i) {
2168 if (row_info[i].endlastfoot) {
2169 ret += TeXRow(os, i, buf, fragile, fp);
2172 if (endlastfoot.bottomDL) {
2176 os << "\\endlastfoot\n";
2183 bool LyXTabular::isValidRow(int const row) const
2185 if (!is_long_tabular)
2187 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2188 !row_info[row].endfoot && !row_info[row].endlastfoot);
2192 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2193 bool fragile, bool fp) const
2196 int cell = GetCellNumber(i, 0);
2198 ret += TeXTopHLine(os, i);
2199 for (int j = 0; j < columns_; ++j) {
2200 if (IsPartOfMultiColumn(i,j))
2202 ret += TeXCellPreamble(os, cell);
2203 InsetText * inset = GetCellInset(cell);
2205 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2206 !inset->paragraph()->empty() && GetPWidth(cell).zero();
2210 ret += inset->latex(buf, os, fragile, fp);
2214 ret += TeXCellPostamble(os, cell);
2215 if (!IsLastCellInRow(cell)) { // not last cell in row
2221 os << "\\tabularnewline\n";
2223 ret += TeXBottomHLine(os, i);
2228 int LyXTabular::latex(Buffer const * buf,
2229 ostream & os, bool fragile, bool fp) const
2233 //+---------------------------------------------------------------------
2234 //+ first the opening preamble +
2235 //+---------------------------------------------------------------------
2238 os << "\\begin{sideways}\n";
2241 if (is_long_tabular)
2242 os << "\\begin{longtable}{";
2244 os << "\\begin{tabular}{";
2245 for (int i = 0; i < columns_; ++i) {
2246 if (!column_info[i].align_special.empty()) {
2247 os << column_info[i].align_special;
2249 if (column_info[i].left_line)
2251 if (!column_info[i].p_width.zero()) {
2252 switch (column_info[i].alignment) {
2253 case LYX_ALIGN_LEFT:
2254 os << ">{\\raggedright}";
2256 case LYX_ALIGN_RIGHT:
2257 os << ">{\\raggedleft}";
2259 case LYX_ALIGN_CENTER:
2260 os << ">{\\centering}";
2262 case LYX_ALIGN_NONE:
2263 case LYX_ALIGN_BLOCK:
2264 case LYX_ALIGN_LAYOUT:
2265 case LYX_ALIGN_SPECIAL:
2269 switch (column_info[i].valignment) {
2270 case LYX_VALIGN_TOP:
2273 case LYX_VALIGN_CENTER:
2276 case LYX_VALIGN_BOTTOM:
2281 << column_info[i].p_width.asLatexString()
2284 switch (column_info[i].alignment) {
2285 case LYX_ALIGN_LEFT:
2288 case LYX_ALIGN_RIGHT:
2296 if (column_info[i].right_line)
2303 ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2305 //+---------------------------------------------------------------------
2306 //+ the single row and columns (cells) +
2307 //+---------------------------------------------------------------------
2309 for (int i = 0; i < rows_; ++i) {
2310 if (isValidRow(i)) {
2311 ret += TeXRow(os, i, buf, fragile, fp);
2312 if (is_long_tabular && row_info[i].newpage) {
2313 os << "\\newpage\n";
2319 //+---------------------------------------------------------------------
2320 //+ the closing of the tabular +
2321 //+---------------------------------------------------------------------
2323 if (is_long_tabular)
2324 os << "\\end{longtable}";
2326 os << "\\end{tabular}";
2328 os << "\n\\end{sideways}";
2336 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2339 int cell = GetFirstCellInRow(row);
2342 for (int j = 0; j < columns_; ++j) {
2343 if (IsPartOfMultiColumn(row, j))
2346 os << "<entry align=\"";
2347 switch (GetAlignment(cell)) {
2348 case LYX_ALIGN_LEFT:
2351 case LYX_ALIGN_RIGHT:
2359 os << "\" valign=\"";
2360 switch (GetVAlignment(cell)) {
2361 case LYX_VALIGN_TOP:
2364 case LYX_VALIGN_BOTTOM:
2367 case LYX_VALIGN_CENTER:
2372 if (IsMultiColumn(cell)) {
2373 os << " namest=\"col" << j << "\" ";
2374 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2378 ret += GetCellInset(cell)->docbook(buf, os, true);
2387 int LyXTabular::docbook(Buffer const * buf, ostream & os,
2388 bool /*mixcont*/) const
2392 //+---------------------------------------------------------------------
2393 //+ first the opening preamble +
2394 //+---------------------------------------------------------------------
2396 os << "<tgroup cols=\"" << columns_
2397 << "\" colsep=\"1\" rowsep=\"1\">\n";
2399 for (int i = 0; i < columns_; ++i) {
2400 os << "<colspec colname=\"col" << i << "\" align=\"";
2401 switch (column_info[i].alignment) {
2402 case LYX_ALIGN_LEFT:
2405 case LYX_ALIGN_RIGHT:
2416 //+---------------------------------------------------------------------
2417 //+ Long Tabular case +
2418 //+---------------------------------------------------------------------
2420 // output header info
2421 if (haveLTHead() || haveLTFirstHead()) {
2424 for (int i = 0; i < rows_; ++i) {
2425 if (row_info[i].endhead || row_info[i].endfirsthead) {
2426 ret += docbookRow(buf, os, i);
2432 // output footer info
2433 if (haveLTFoot() || haveLTLastFoot()) {
2436 for (int i = 0; i < rows_; ++i) {
2437 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2438 ret += docbookRow(buf, os, i);
2445 //+---------------------------------------------------------------------
2446 //+ the single row and columns (cells) +
2447 //+---------------------------------------------------------------------
2451 for (int i = 0; i < rows_; ++i) {
2452 if (isValidRow(i)) {
2453 ret += docbookRow(buf, os, i);
2458 //+---------------------------------------------------------------------
2459 //+ the closing of the tabular +
2460 //+---------------------------------------------------------------------
2469 // ASCII export function and helpers
2471 int LyXTabular::asciiTopHLine(ostream & os, int row,
2472 vector<unsigned int> const & clen) const
2474 int const fcell = GetFirstCellInRow(row);
2475 int const n = NumberOfCellsInRow(fcell) + fcell;
2478 for (int i = fcell; i < n; ++i) {
2488 for (int i = fcell; i < n; ++i) {
2499 int column = column_of_cell(i);
2500 int len = clen[column];
2501 while (IsPartOfMultiColumn(row, ++column))
2502 len += clen[column] + 4;
2503 os << string(len, ch);
2518 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2519 vector<unsigned int> const & clen) const
2521 int const fcell = GetFirstCellInRow(row);
2522 int const n = NumberOfCellsInRow(fcell) + fcell;
2525 for (int i = fcell; i < n; ++i) {
2526 if (BottomLine(i)) {
2535 for (int i = fcell; i < n; ++i) {
2536 if (BottomLine(i)) {
2546 int column = column_of_cell(i);
2547 int len = clen[column];
2548 while (IsPartOfMultiColumn(row, ++column))
2549 len += clen[column] + 4;
2550 os << string(len, ch);
2551 if (BottomLine(i)) {
2565 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2566 int cell, int row, int column,
2567 vector<unsigned int> const & clen,
2568 bool onlydata) const
2571 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2583 unsigned int len1 = sstr.str().length();
2584 unsigned int len2 = clen[column];
2585 while (IsPartOfMultiColumn(row, ++column))
2586 len2 += clen[column] + 4;
2589 switch (GetAlignment(cell)) {
2591 case LYX_ALIGN_LEFT:
2594 case LYX_ALIGN_RIGHT:
2598 case LYX_ALIGN_CENTER:
2604 os << string(len1, ' ')
2606 << string(len2, ' ');
2607 if (RightLine(cell))
2616 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2617 bool onlydata, unsigned char delim) const
2621 //+---------------------------------------------------------------------
2622 //+ first calculate the width of the single columns +
2623 //+---------------------------------------------------------------------
2624 vector<unsigned int> clen(columns_);
2627 // first all non (real) multicolumn cells!
2628 for (int j = 0; j < columns_; ++j) {
2630 for (int i = 0; i < rows_; ++i) {
2631 int cell = GetCellNumber(i, j);
2632 if (IsMultiColumn(cell, true))
2635 GetCellInset(cell)->ascii(buf, sstr, 0);
2636 if (clen[j] < sstr.str().length())
2637 clen[j] = sstr.str().length();
2640 // then all (real) multicolumn cells!
2641 for (int j = 0; j < columns_; ++j) {
2642 for (int i = 0; i < rows_; ++i) {
2643 int cell = GetCellNumber(i, j);
2644 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2647 GetCellInset(cell)->ascii(buf, sstr, 0);
2648 int len = int(sstr.str().length());
2649 int const n = cells_in_multicolumn(cell);
2650 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2652 if (len > int(clen[j + n - 1]))
2653 clen[j + n - 1] = len;
2658 for (int i = 0; i < rows_; ++i) {
2660 if (asciiTopHLine(os, i, clen)) {
2661 for (int j = 0; j < depth; ++j)
2665 for (int j = 0; j < columns_; ++j) {
2666 if (IsPartOfMultiColumn(i,j))
2668 if (onlydata && j > 0)
2670 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2675 for (int j = 0; j < depth; ++j)
2677 if (asciiBottomHLine(os, i, clen)) {
2678 for (int j = 0; j < depth; ++j)
2690 InsetText * LyXTabular::GetCellInset(int cell) const
2693 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2697 InsetText * LyXTabular::GetCellInset(int row, int column) const
2699 cur_cell = GetCellNumber(row, column);
2700 return & cell_info[row][column].inset;
2704 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2706 // is this inset part of the tabular?
2707 if (!inset || inset->owner() != owner_) {
2708 lyxerr[Debug::INSETTEXT]
2709 << "this is not a cell of the tabular!" << endl;
2713 const int save_cur_cell = cur_cell;
2714 int cell = cur_cell;
2715 if (GetCellInset(cell) != inset) {
2717 if (cell == -1 || GetCellInset(cell) != inset) {
2723 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2724 if (GetCellInset(cell) == inset)
2727 lyxerr[Debug::INSETTEXT]
2728 << "LyXTabular::GetCellFromInset: "
2730 << ", cur_cell=" << save_cur_cell
2731 << ", maybe_cell=" << maybe_cell
2733 // We should have found a cell at this point
2735 lyxerr << "LyXTabular::GetCellFromInset: "
2736 << "Cell not found!" << endl;
2744 void LyXTabular::Validate(LaTeXFeatures & features) const
2746 features.require("NeedTabularnewline");
2747 if (IsLongTabular())
2748 features.require("longtable");
2750 features.require("rotating");
2751 for (int cell = 0; cell < numberofcells; ++cell) {
2752 if ( (GetVAlignment(cell) != LYX_VALIGN_TOP) ||
2753 ( !(GetPWidth(cell).zero())&&!(IsMultiColumn(cell)) )
2755 features.require("array");
2756 GetCellInset(cell)->validate(features);
2761 vector<string> const LyXTabular::getLabelList() const
2763 vector<string> label_list;
2764 for (int i = 0; i < rows_; ++i)
2765 for (int j = 0; j < columns_; ++j) {
2766 vector<string> const l =
2767 GetCellInset(i, j)->getLabelList();
2768 label_list.insert(label_list.end(),
2769 l.begin(), l.end());
2775 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2777 Paragraph * par = GetCellInset(cell)->paragraph();
2779 for (; par; par = par->next()) {
2780 for (int i = 0; i < par->size(); ++i) {
2781 if (par->getChar(i) == Paragraph::META_NEWLINE)