3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
23 #include "bufferparams.h"
25 #include "LaTeXFeatures.h"
27 #include "paragraph.h"
29 #include "insets/insettabular.h"
31 #include "support/lstrings.h"
32 #include "support/tostr.h"
34 #include "support/std_sstream.h"
36 using lyx::support::ltrim;
37 using lyx::support::prefixIs;
38 using lyx::support::rtrim;
39 using lyx::support::strToInt;
40 using lyx::support::suffixIs;
49 using std::ostringstream;
53 #ifndef CXX_GLOBAL_CSTD
60 int const WIDTH_OF_LINE = 5;
63 string const write_attribute(string const & name, T const & t)
65 string const s = tostr(t);
66 return s.empty() ? s : " " + name + "=\"" + s + "\"";
69 string const write_attribute(string const & name, string const & t)
71 return t.empty() ? t : " " + name + "=\"" + t + "\"";
75 string const write_attribute(string const & name, bool const & b)
77 // we write only true attribute values so we remove a bit of the
78 // file format bloat for tabulars.
79 return b ? write_attribute(name, tostr(b)) : string();
83 string const write_attribute(string const & name, int const & i)
85 // we write only true attribute values so we remove a bit of the
86 // file format bloat for tabulars.
87 return i ? write_attribute(name, tostr(i)) : string();
91 string const write_attribute(string const & name, LyXLength const & value)
93 // we write only the value if we really have one same reson as above.
94 return value.zero() ? string() : write_attribute(name, value.asString());
98 string const tostr(LyXAlignment const & num)
103 case LYX_ALIGN_BLOCK:
107 case LYX_ALIGN_CENTER:
109 case LYX_ALIGN_RIGHT:
111 case LYX_ALIGN_LAYOUT:
113 case LYX_ALIGN_SPECIAL:
120 string const tostr(LyXTabular::VAlignment const & num)
123 case LyXTabular::LYX_VALIGN_TOP:
125 case LyXTabular::LYX_VALIGN_MIDDLE:
127 case LyXTabular::LYX_VALIGN_BOTTOM:
134 string const tostr(LyXTabular::BoxType const & num)
137 case LyXTabular::BOX_NONE:
139 case LyXTabular::BOX_PARBOX:
141 case LyXTabular::BOX_MINIPAGE:
148 // I would have liked a fromstr template a lot better. (Lgb)
149 bool string2type(string const str, LyXAlignment & num)
152 num = LYX_ALIGN_NONE;
153 else if (str == "block")
154 num = LYX_ALIGN_BLOCK;
155 else if (str == "left")
156 num = LYX_ALIGN_LEFT;
157 else if (str == "center")
158 num = LYX_ALIGN_CENTER;
159 else if (str == "right")
160 num = LYX_ALIGN_RIGHT;
167 bool string2type(string const str, LyXTabular::VAlignment & num)
170 num = LyXTabular::LYX_VALIGN_TOP;
171 else if (str == "middle" )
172 num = LyXTabular::LYX_VALIGN_MIDDLE;
173 else if (str == "bottom")
174 num = LyXTabular::LYX_VALIGN_BOTTOM;
181 bool string2type(string const str, LyXTabular::BoxType & num)
184 num = LyXTabular::BOX_NONE;
185 else if (str == "parbox")
186 num = LyXTabular::BOX_PARBOX;
187 else if (str == "minipage")
188 num = LyXTabular::BOX_MINIPAGE;
195 bool string2type(string const str, bool & num)
199 else if (str == "false")
207 bool getTokenValue(string const & str, const char * token, string & ret)
210 size_t token_length = strlen(token);
211 string::size_type pos = str.find(token);
213 if (pos == string::npos || pos + token_length + 1 >= str.length()
214 || str[pos + token_length] != '=')
216 pos += token_length + 1;
218 if (ch != '"' && ch != '\'') { // only read till next space
222 while (pos < str.length() - 1 && str[++pos] != ch)
229 bool getTokenValue(string const & str, const char * token, int & num)
233 if (!getTokenValue(str, token, tmp))
240 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
243 if (!getTokenValue(str, token, tmp))
245 return string2type(tmp, num);
249 bool getTokenValue(string const & str, const char * token,
250 LyXTabular::VAlignment & num)
253 if (!getTokenValue(str, token, tmp))
255 return string2type(tmp, num);
259 bool getTokenValue(string const & str, const char * token,
260 LyXTabular::BoxType & num)
263 if (!getTokenValue(str, token, tmp))
265 return string2type(tmp, num);
269 bool getTokenValue(string const & str, const char * token, bool & flag)
271 // set the flag always to false as this should be the default for bools
272 // not in the file-format.
275 if (!getTokenValue(str, token, tmp))
277 return string2type(tmp, flag);
281 bool getTokenValue(string const & str, const char * token, LyXLength & len)
283 // set the lenght to be zero() as default as this it should be if not
284 // in the file format.
287 if (!getTokenValue(str, token, tmp))
289 return isValidLength(tmp, &len);
293 void l_getline(istream & is, string & str)
296 while (str.empty()) {
298 if (!str.empty() && str[str.length() - 1] == '\r')
299 str.erase(str.length() - 1);
305 /// Define a few methods for the inner structs
307 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
312 multicolumn = LyXTabular::CELL_NORMAL;
313 alignment = LYX_ALIGN_CENTER;
314 valignment = LYX_VALIGN_TOP;
324 LyXTabular::rowstruct::rowstruct()
331 endfirsthead = false;
338 LyXTabular::columnstruct::columnstruct()
342 alignment = LYX_ALIGN_CENTER;
343 valignment = LYX_VALIGN_TOP;
348 LyXTabular::ltType::ltType()
356 LyXTabular::LyXTabular(BufferParams const & bp, int rows_arg, int columns_arg)
358 init(bp, rows_arg, columns_arg);
362 // activates all lines and sets all widths to 0
363 void LyXTabular::init(BufferParams const & bp, int rows_arg, int columns_arg)
366 columns_ = columns_arg;
367 row_info = row_vector(rows_, rowstruct());
368 column_info = column_vector(columns_, columnstruct());
369 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
372 for (int i = 0; i < rows_; ++i) {
373 for (int j = 0; j < columns_; ++j) {
374 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
375 cell_info[i][j].cellno = cellno++;
377 cell_info[i].back().right_line = true;
379 row_info.back().bottom_line = true;
380 row_info.front().bottom_line = true;
381 column_info.back().right_line = true;
383 columnofcell.clear();
384 set_row_column_number_info();
385 is_long_tabular = false;
390 void LyXTabular::setOwner(InsetTabular * inset)
392 for (int i = 0; i < rows_; ++i)
393 for (int j = 0; j < columns_; ++j)
394 cell_info[i][j].inset.setOwner(inset);
398 void LyXTabular::appendRow(BufferParams const & bp, int cell)
402 int row = row_of_cell(cell);
404 row_vector::iterator rit = row_info.begin() + row;
405 row_info.insert(rit, rowstruct());
406 // now set the values of the row before
407 row_info[row] = row_info[row + 1];
410 cell_vvector::iterator cit = cell_info.begin() + row;
411 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
414 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
417 for (int i = 0; i <= row; ++i)
418 for (int j = 0; j < columns_; ++j)
419 c_info[i][j] = cell_info[i][j];
421 for (int i = row + 1; i < rows_; ++i)
422 for (int j = 0; j < columns_; ++j)
423 c_info[i][j] = cell_info[i-1][j];
427 for (int j = 0; j < columns_; ++j) {
428 cell_info[row][j].inset.clear(false);
429 if (bp.tracking_changes)
430 cell_info[row][j].inset.markNew(true);
436 void LyXTabular::deleteRow(int row)
438 // Not allowed to delete last row
442 row_info.erase(row_info.begin() + row);
443 cell_info.erase(cell_info.begin() + row);
448 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
452 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
454 int const column = column_of_cell(cell);
455 column_vector::iterator cit = column_info.begin() + column + 1;
456 column_info.insert(cit, columnstruct());
457 // set the column values of the column before
458 column_info[column + 1] = column_info[column];
460 for (int i = 0; i < rows_; ++i) {
461 for (int j = 0; j <= column; ++j)
462 c_info[i][j] = cell_info[i][j];
464 for (int j = column + 1; j < columns_; ++j)
465 c_info[i][j] = cell_info[i][j - 1];
467 // care about multicolumns
468 if (c_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
469 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
471 if (column + 2 >= columns_
472 || c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
473 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
477 for (int i = 0; i < rows_; ++i) {
478 cell_info[i][column + 1].inset.clear(false);
479 if (bp.tracking_changes)
480 cell_info[i][column + 1].inset.markNew(true);
485 void LyXTabular::deleteColumn(int column)
487 // Not allowed to delete last column
491 column_info.erase(column_info.begin() + column);
492 for (int i = 0; i < rows_; ++i)
493 cell_info[i].erase(cell_info[i].begin() + column);
498 void LyXTabular::set_row_column_number_info(bool oldformat)
501 for (int row = 0; row < rows_; ++row) {
502 for (int column = 0; column < columns_; ++column) {
503 if (cell_info[row][column].multicolumn
504 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
506 cell_info[row][column].cellno = numberofcells;
509 ++numberofcells; // because this is one more than as we start from 0
511 rowofcell.resize(numberofcells);
512 columnofcell.resize(numberofcells);
514 for (int row = 0, column = 0, c = 0;
515 c < numberofcells && row < rows_ && column < columns_;) {
517 columnofcell[c] = column;
521 } while (column < columns_ &&
522 cell_info[row][column].multicolumn
523 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
525 if (column == columns_) {
531 for (int row = 0; row < rows_; ++row) {
532 for (int column = 0; column < columns_; ++column) {
533 if (isPartOfMultiColumn(row,column))
535 // now set the right line of multicolumns right for oldformat read
537 cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
539 int cn = cells_in_multicolumn(cell_info[row][column].cellno);
540 cell_info[row][column].right_line =
541 cell_info[row][column+cn-1].right_line;
543 cell_info[row][column].inset.setAutoBreakRows(
544 !getPWidth(getCellNumber(row, column)).zero());
550 int LyXTabular::getNumberOfCells() const
552 return numberofcells;
556 int LyXTabular::numberOfCellsInRow(int cell) const
558 int const row = row_of_cell(cell);
560 for (int i = 0; i < columns_; ++i)
561 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
567 // returns 1 if there is a topline, returns 0 if not
568 bool LyXTabular::topLine(int cell, bool onlycolumn) const
570 if (!onlycolumn && isMultiColumn(cell))
571 return cellinfo_of_cell(cell).top_line;
572 return row_info[row_of_cell(cell)].top_line;
576 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
578 if (!onlycolumn && isMultiColumn(cell))
579 return cellinfo_of_cell(cell).bottom_line;
580 return row_info[row_of_cell(cell)].bottom_line;
584 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
586 if (!onlycolumn && isMultiColumn(cell) &&
587 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
589 if (cellinfo_of_cell(cell).align_special.empty())
590 return cellinfo_of_cell(cell).left_line;
591 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
593 if (column_info[column_of_cell(cell)].align_special.empty())
594 return column_info[column_of_cell(cell)].left_line;
595 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
599 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
601 if (!onlycolumn && isMultiColumn(cell) &&
602 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
604 if (cellinfo_of_cell(cell).align_special.empty())
605 return cellinfo_of_cell(cell).right_line;
606 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
608 if (column_info[column_of_cell(cell)].align_special.empty())
609 return column_info[right_column_of_cell(cell)].right_line;
610 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
614 bool LyXTabular::topAlreadyDrawn(int cell) const
616 int row = row_of_cell(cell);
617 if (row > 0 && !getAdditionalHeight(row)) {
618 int column = column_of_cell(cell);
621 && cell_info[row][column].multicolumn
622 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
624 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
625 return row_info[row].bottom_line;
627 return cell_info[row][column].bottom_line;
633 bool LyXTabular::leftAlreadyDrawn(int cell) const
635 int column = column_of_cell(cell);
637 int row = row_of_cell(cell);
639 (cell_info[row][column].multicolumn ==
640 LyXTabular::CELL_PART_OF_MULTICOLUMN));
641 if (getAdditionalWidth(cell_info[row][column].cellno))
643 return rightLine(cell_info[row][column].cellno);
649 bool LyXTabular::isLastRow(int cell) const
651 return (row_of_cell(cell) == rows_ - 1);
655 int LyXTabular::getAdditionalHeight(int row) const
657 if (!row || row >= rows_)
663 for (int column = 0; column < columns_ && bottom; ++column) {
664 switch (cell_info[row - 1][column].multicolumn) {
665 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
666 bottom = cell_info[row - 1][column].bottom_line;
668 case LyXTabular::CELL_NORMAL:
669 bottom = row_info[row - 1].bottom_line;
672 for (int column = 0; column < columns_ && top; ++column) {
673 switch (cell_info[row][column].multicolumn) {
674 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
675 top = cell_info[row][column].top_line;
677 case LyXTabular::CELL_NORMAL:
678 top = row_info[row].top_line;
682 return WIDTH_OF_LINE;
687 int LyXTabular::getAdditionalWidth(int cell) const
689 // internally already set in setWidthOfCell
690 // used to get it back in text.C
691 int const col = right_column_of_cell(cell);
692 int const row = row_of_cell(cell);
693 if (col < columns_ - 1 && rightLine(cell) &&
694 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
696 return WIDTH_OF_LINE;
702 // returns the maximum over all rows
703 int LyXTabular::getWidthOfColumn(int cell) const
705 int const column1 = column_of_cell(cell);
706 int const column2 = right_column_of_cell(cell);
708 for (int i = column1; i <= column2; ++i)
709 result += column_info[i].width_of_column;
714 int LyXTabular::getWidthOfTabular() const
716 return width_of_tabular;
720 // returns true if a complete update is necessary, otherwise false
721 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
723 if (!isMultiColumn(cell))
726 int const row = row_of_cell(cell);
727 int const column1 = column_of_cell(cell);
728 int const column2 = right_column_of_cell(cell);
729 int const old_val = cell_info[row][column2].width_of_cell;
731 // first set columns to 0 so we can calculate the right width
732 for (int i = column1; i <= column2; ++i) {
733 cell_info[row][i].width_of_cell = 0;
735 // set the width to MAX_WIDTH until width > 0
736 int width = new_width + 2 * WIDTH_OF_LINE;
738 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
739 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
740 width -= column_info[i].width_of_column;
743 cell_info[row][i].width_of_cell = width;
745 if (old_val != cell_info[row][column2].width_of_cell) {
746 // in this case we have to recalculate all multicolumn cells which
747 // have this column as one of theirs but not as last one
748 calculate_width_of_column_NMC(i);
749 recalculateMulticolumnsOfColumn(i);
750 calculate_width_of_column(i);
756 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
758 // the last column does not have to be recalculated because all
759 // multicolumns will have here there last multicolumn cell which
760 // always will have the whole rest of the width of the cell.
761 if (column > (columns_ - 2))
763 for(int row = 0; row < rows_; ++row) {
764 int mc = cell_info[row][column].multicolumn;
765 int nmc = cell_info[row][column+1].multicolumn;
766 // we only have to update multicolumns which do not have this
767 // column as their last column!
768 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
769 (mc == CELL_PART_OF_MULTICOLUMN &&
770 nmc == CELL_PART_OF_MULTICOLUMN))
772 int const cellno = cell_info[row][column].cellno;
773 setWidthOfMulticolCell(cellno,
774 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
780 // returns 1 if a complete update is necessary, otherwise 0
781 void LyXTabular::setWidthOfCell(int cell, int new_width)
783 int const row = row_of_cell(cell);
784 int const column1 = column_of_cell(cell);
789 if (rightLine(cell_info[row][column1].cellno, true) &&
790 column1 < columns_ - 1 &&
791 leftLine(cell_info[row][column1+1].cellno, true))
793 add_width = WIDTH_OF_LINE;
796 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
799 if (isMultiColumnReal(cell)) {
800 tmp = setWidthOfMulticolCell(cell, new_width);
802 width = new_width + 2 * WIDTH_OF_LINE + add_width;
803 cell_info[row][column1].width_of_cell = width;
804 tmp = calculate_width_of_column_NMC(column1);
806 recalculateMulticolumnsOfColumn(column1);
809 for (int i = 0; i < columns_; ++i)
810 calculate_width_of_column(i);
811 calculate_width_of_tabular();
816 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
818 if (!isMultiColumn(cell) || onlycolumn)
819 column_info[column_of_cell(cell)].alignment = align;
821 cellinfo_of_cell(cell).alignment = align;
825 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
827 if (!isMultiColumn(cell) || onlycolumn)
828 column_info[column_of_cell(cell)].valignment = align;
830 cellinfo_of_cell(cell).valignment = align;
834 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
836 int const j = column_of_cell(cell);
838 column_info[j].p_width = width;
839 for (int i = 0; i < rows_; ++i) {
840 int const cell = getCellNumber(i, j);
841 // because of multicolumns
842 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
847 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
849 if (!isMultiColumn(cell))
852 cellinfo_of_cell(cell).p_width = width;
853 getCellInset(cell).setAutoBreakRows(!width.zero());
858 void LyXTabular::setAlignSpecial(int cell, string const & special,
859 LyXTabular::Feature what)
861 if (what == SET_SPECIAL_MULTI)
862 cellinfo_of_cell(cell).align_special = special;
864 column_info[column_of_cell(cell)].align_special = special;
868 void LyXTabular::setAllLines(int cell, bool line)
870 setTopLine(cell, line);
871 setBottomLine(cell, line);
872 setRightLine(cell, line);
873 setLeftLine(cell, line);
877 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
879 int const row = row_of_cell(cell);
880 if (onlycolumn || !isMultiColumn(cell))
881 row_info[row].top_line = line;
883 cellinfo_of_cell(cell).top_line = line;
887 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
889 if (onlycolumn || !isMultiColumn(cell))
890 row_info[row_of_cell(cell)].bottom_line = line;
892 cellinfo_of_cell(cell).bottom_line = line;
896 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
898 if (onlycolumn || !isMultiColumn(cell))
899 column_info[column_of_cell(cell)].left_line = line;
901 cellinfo_of_cell(cell).left_line = line;
905 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
907 if (onlycolumn || !isMultiColumn(cell))
908 column_info[right_column_of_cell(cell)].right_line = line;
910 cellinfo_of_cell(cell).right_line = line;
914 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
916 if (!onlycolumn && isMultiColumn(cell))
917 return cellinfo_of_cell(cell).alignment;
918 return column_info[column_of_cell(cell)].alignment;
922 LyXTabular::VAlignment
923 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
925 if (!onlycolumn && isMultiColumn(cell))
926 return cellinfo_of_cell(cell).valignment;
927 return column_info[column_of_cell(cell)].valignment;
931 LyXLength const LyXTabular::getPWidth(int cell) const
933 if (isMultiColumn(cell))
934 return cellinfo_of_cell(cell).p_width;
935 return column_info[column_of_cell(cell)].p_width;
939 LyXLength const LyXTabular::getColumnPWidth(int cell) const
941 return column_info[column_of_cell(cell)].p_width;
945 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
947 if (isMultiColumn(cell))
948 return cellinfo_of_cell(cell).p_width;
953 string const LyXTabular::getAlignSpecial(int cell, int what) const
955 if (what == SET_SPECIAL_MULTI)
956 return cellinfo_of_cell(cell).align_special;
957 return column_info[column_of_cell(cell)].align_special;
961 int LyXTabular::getWidthOfCell(int cell) const
963 int const row = row_of_cell(cell);
964 int const column1 = column_of_cell(cell);
965 int const column2 = right_column_of_cell(cell);
967 for (int i = column1; i <= column2; ++i)
968 result += cell_info[row][i].width_of_cell;
973 int LyXTabular::getBeginningOfTextInCell(int cell) const
977 switch (getAlignment(cell)) {
978 case LYX_ALIGN_CENTER:
979 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
981 case LYX_ALIGN_RIGHT:
982 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
983 // + getAdditionalWidth(cell);
986 // LYX_ALIGN_LEFT: nothing :-)
996 bool LyXTabular::isFirstCellInRow(int cell) const
998 return column_of_cell(cell) == 0;
1002 int LyXTabular::getFirstCellInRow(int row) const
1004 if (row > rows_ - 1)
1006 return cell_info[row][0].cellno;
1010 bool LyXTabular::isLastCellInRow(int cell) const
1012 return right_column_of_cell(cell) == columns_ - 1;
1016 int LyXTabular::getLastCellInRow(int row) const
1018 if (row > rows_ - 1)
1020 return cell_info[row][columns_-1].cellno;
1024 void LyXTabular::calculate_width_of_column(int column)
1027 for (int i = 0; i < rows_; ++i)
1028 maximum = max(cell_info[i][column].width_of_cell, maximum);
1029 column_info[column].width_of_column = maximum;
1034 // Calculate the columns regarding ONLY the normal cells and if this
1035 // column is inside a multicolumn cell then use it only if its the last
1036 // column of this multicolumn cell as this gives an added width to the
1037 // column, all the rest should be adapted!
1039 bool LyXTabular::calculate_width_of_column_NMC(int column)
1041 int const old_column_width = column_info[column].width_of_column;
1043 for (int i = 0; i < rows_; ++i) {
1044 int cell = getCellNumber(i, column);
1045 bool ismulti = isMultiColumnReal(cell);
1046 if ((!ismulti || column == right_column_of_cell(cell)) &&
1047 cell_info[i][column].width_of_cell > max)
1049 max = cell_info[i][column].width_of_cell;
1052 column_info[column].width_of_column = max;
1053 return column_info[column].width_of_column != old_column_width;
1057 void LyXTabular::calculate_width_of_tabular()
1059 width_of_tabular = 0;
1060 for (int i = 0; i < columns_; ++i)
1061 width_of_tabular += column_info[i].width_of_column;
1065 int LyXTabular::row_of_cell(int cell) const
1067 if (cell >= numberofcells)
1071 return rowofcell[cell];
1075 int LyXTabular::column_of_cell(int cell) const
1077 if (cell >= numberofcells)
1078 return columns_ - 1;
1081 return columnofcell[cell];
1085 int LyXTabular::right_column_of_cell(int cell) const
1087 int const row = row_of_cell(cell);
1088 int column = column_of_cell(cell);
1089 while (column < columns_ - 1 &&
1090 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1096 void LyXTabular::write(Buffer const & buf, ostream & os) const
1100 << write_attribute("version", 3)
1101 << write_attribute("rows", rows_)
1102 << write_attribute("columns", columns_)
1104 // global longtable options
1106 << write_attribute("rotate", rotate)
1107 << write_attribute("islongtable", is_long_tabular)
1108 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1109 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1110 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1111 << write_attribute("headTopDL", endhead.topDL)
1112 << write_attribute("headBottomDL", endhead.bottomDL)
1113 << write_attribute("footTopDL", endfoot.topDL)
1114 << write_attribute("footBottomDL", endfoot.bottomDL)
1115 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1116 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1117 << write_attribute("lastFootEmpty", endlastfoot.empty)
1119 for (int j = 0; j < columns_; ++j) {
1121 << write_attribute("alignment", column_info[j].alignment)
1122 << write_attribute("valignment", column_info[j].valignment)
1123 << write_attribute("leftline", column_info[j].left_line)
1124 << write_attribute("rightline", column_info[j].right_line)
1125 << write_attribute("width", column_info[j].p_width.asString())
1126 << write_attribute("special", column_info[j].align_special)
1129 for (int i = 0; i < rows_; ++i) {
1131 << write_attribute("topline", row_info[i].top_line)
1132 << write_attribute("bottomline", row_info[i].bottom_line)
1133 << write_attribute("endhead", row_info[i].endhead)
1134 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1135 << write_attribute("endfoot", row_info[i].endfoot)
1136 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1137 << write_attribute("newpage", row_info[i].newpage)
1139 for (int j = 0; j < columns_; ++j) {
1141 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1142 << write_attribute("alignment", cell_info[i][j].alignment)
1143 << write_attribute("valignment", cell_info[i][j].valignment)
1144 << write_attribute("topline", cell_info[i][j].top_line)
1145 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1146 << write_attribute("leftline", cell_info[i][j].left_line)
1147 << write_attribute("rightline", cell_info[i][j].right_line)
1148 << write_attribute("rotate", cell_info[i][j].rotate)
1149 << write_attribute("usebox", cell_info[i][j].usebox)
1150 << write_attribute("width", cell_info[i][j].p_width)
1151 << write_attribute("special", cell_info[i][j].align_special)
1153 os << "\\begin_inset ";
1154 cell_info[i][j].inset.write(buf, os);
1155 os << "\n\\end_inset \n"
1160 os << "</lyxtabular>\n";
1164 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1168 row_info[--hr].endhead = true;
1170 // set firstheader info
1171 if (fhr && fhr < rows_) {
1172 if (row_info[fhr].endhead) {
1174 row_info[--fhr].endfirsthead = true;
1175 row_info[fhr].endhead = false;
1177 } else if (row_info[fhr - 1].endhead) {
1178 endfirsthead.empty = true;
1180 while (fhr > 0 && !row_info[--fhr].endhead) {
1181 row_info[fhr].endfirsthead = true;
1186 if (fr && fr < rows_) {
1187 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1188 while (fr > 0 && !row_info[--fr].endhead) {
1189 row_info[fr].endfoot = true;
1190 row_info[fr].endhead = false;
1192 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1193 while (fr > 0 && !row_info[--fr].endfirsthead) {
1194 row_info[fr].endfoot = true;
1195 row_info[fr].endfirsthead = false;
1197 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1198 while (fr > 0 && !row_info[--fr].endhead &&
1199 !row_info[fr].endfirsthead)
1201 row_info[fr].endfoot = true;
1205 // set lastfooter info
1206 if (lfr && lfr < rows_) {
1207 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1208 while (lfr > 0 && !row_info[--lfr].endhead) {
1209 row_info[lfr].endlastfoot = true;
1210 row_info[lfr].endhead = false;
1212 } else if (row_info[lfr].endfirsthead &&
1213 row_info[lfr - 1].endfirsthead)
1215 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1216 row_info[lfr].endlastfoot = true;
1217 row_info[lfr].endfirsthead = false;
1219 } else if (row_info[lfr].endfoot
1220 && row_info[lfr - 1].endfoot) {
1221 while (lfr > 0 && !row_info[--lfr].endfoot) {
1222 row_info[lfr].endlastfoot = true;
1223 row_info[lfr].endfoot = false;
1225 } else if (!row_info[fr - 1].endhead
1226 && !row_info[fr - 1].endfirsthead &&
1227 !row_info[fr - 1].endfoot)
1230 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1231 !row_info[lfr].endfoot)
1233 row_info[lfr].endlastfoot = true;
1235 } else if (haveLTFoot()) {
1236 endlastfoot.empty = true;
1242 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1245 istream & is = lex.getStream();
1247 l_getline(is, line);
1248 if (!prefixIs(line, "<lyxtabular ")
1249 && !prefixIs(line, "<LyXTabular ")) {
1250 BOOST_ASSERT(false);
1255 if (!getTokenValue(line, "version", version))
1257 BOOST_ASSERT(version >= 2);
1260 if (!getTokenValue(line, "rows", rows_arg))
1263 if (!getTokenValue(line, "columns", columns_arg))
1265 init(buf.params(), rows_arg, columns_arg);
1266 l_getline(is, line);
1267 if (!prefixIs(line, "<features")) {
1268 lyxerr << "Wrong tabular format (expected <features ...> got"
1269 << line << ')' << endl;
1272 getTokenValue(line, "rotate", rotate);
1273 getTokenValue(line, "islongtable", is_long_tabular);
1274 // compatibility read for old longtable options. Now we can make any
1275 // row part of the header/footer type we want before it was strict
1276 // sequential from the first row down (as LaTeX does it!). So now when
1277 // we find a header/footer line we have to go up the rows and set it
1278 // on all preceding rows till the first or one with already a h/f option
1279 // set. If we find a firstheader on the same line as a header or a
1280 // lastfooter on the same line as a footer then this should be set empty.
1288 getTokenValue(line, "endhead", hrow);
1289 getTokenValue(line, "endfirsthead", fhrow);
1290 getTokenValue(line, "endfoot", frow);
1291 getTokenValue(line, "endlastfoot", lfrow);
1292 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1294 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1295 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1296 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1297 getTokenValue(line, "headTopDL", endhead.topDL);
1298 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1299 getTokenValue(line, "footTopDL", endfoot.topDL);
1300 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1301 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1302 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1303 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1305 for (int j = 0; j < columns_; ++j) {
1307 if (!prefixIs(line,"<column")) {
1308 lyxerr << "Wrong tabular format (expected <column ...> got"
1309 << line << ')' << endl;
1312 getTokenValue(line, "alignment", column_info[j].alignment);
1313 getTokenValue(line, "valignment", column_info[j].valignment);
1314 getTokenValue(line, "leftline", column_info[j].left_line);
1315 getTokenValue(line, "rightline", column_info[j].right_line);
1316 getTokenValue(line, "width", column_info[j].p_width);
1317 getTokenValue(line, "special", column_info[j].align_special);
1320 for (int i = 0; i < rows_; ++i) {
1321 l_getline(is, line);
1322 if (!prefixIs(line, "<row")) {
1323 lyxerr << "Wrong tabular format (expected <row ...> got"
1324 << line << ')' << endl;
1327 getTokenValue(line, "topline", row_info[i].top_line);
1328 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1329 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1330 getTokenValue(line, "endhead", row_info[i].endhead);
1331 getTokenValue(line, "endfoot", row_info[i].endfoot);
1332 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1333 getTokenValue(line, "newpage", row_info[i].newpage);
1334 for (int j = 0; j < columns_; ++j) {
1335 l_getline(is, line);
1336 if (!prefixIs(line, "<cell")) {
1337 lyxerr << "Wrong tabular format (expected <cell ...> got"
1338 << line << ')' << endl;
1341 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1342 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1343 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1344 getTokenValue(line, "topline", cell_info[i][j].top_line);
1345 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1346 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1347 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1348 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1349 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1350 getTokenValue(line, "width", cell_info[i][j].p_width);
1351 getTokenValue(line, "special", cell_info[i][j].align_special);
1352 l_getline(is, line);
1353 if (prefixIs(line, "\\begin_inset")) {
1354 cell_info[i][j].inset.read(buf, lex);
1355 l_getline(is, line);
1357 if (!prefixIs(line, "</cell>")) {
1358 lyxerr << "Wrong tabular format (expected </cell> got"
1359 << line << ')' << endl;
1363 l_getline(is, line);
1364 if (!prefixIs(line, "</row>")) {
1365 lyxerr << "Wrong tabular format (expected </row> got"
1366 << line << ')' << endl;
1370 while (!prefixIs(line, "</lyxtabular>")) {
1371 l_getline(is, line);
1373 set_row_column_number_info();
1377 bool LyXTabular::isMultiColumn(int cell) const
1379 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1383 bool LyXTabular::isMultiColumnReal(int cell) const
1385 return column_of_cell(cell) != right_column_of_cell(cell) &&
1386 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1390 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1392 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1396 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1398 cellstruct & cs = cellinfo_of_cell(cell);
1399 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1400 cs.alignment = column_info[column_of_cell(cell)].alignment;
1401 cs.top_line = row_info[row_of_cell(cell)].top_line;
1402 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1403 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1404 for (int i = 1; i < number; ++i) {
1405 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1406 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1407 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1408 cs1.inset.clear(false);
1410 set_row_column_number_info();
1414 int LyXTabular::cells_in_multicolumn(int cell) const
1416 int const row = row_of_cell(cell);
1417 int column = column_of_cell(cell);
1420 while (column < columns_ &&
1421 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1430 int LyXTabular::unsetMultiColumn(int cell)
1432 int const row = row_of_cell(cell);
1433 int column = column_of_cell(cell);
1437 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1438 cell_info[row][column].multicolumn = CELL_NORMAL;
1440 while (column < columns_ &&
1441 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1443 cell_info[row][column].multicolumn = CELL_NORMAL;
1448 set_row_column_number_info();
1453 void LyXTabular::setLongTabular(bool what)
1455 is_long_tabular = what;
1459 bool LyXTabular::isLongTabular() const
1461 return is_long_tabular;
1465 void LyXTabular::setRotateTabular(bool flag)
1471 bool LyXTabular::getRotateTabular() const
1477 void LyXTabular::setRotateCell(int cell, bool flag)
1479 cellinfo_of_cell(cell).rotate = flag;
1483 bool LyXTabular::getRotateCell(int cell) const
1485 return cellinfo_of_cell(cell).rotate;
1489 bool LyXTabular::needRotating() const
1493 for (int i = 0; i < rows_; ++i)
1494 for (int j = 0; j < columns_; ++j)
1495 if (cell_info[i][j].rotate)
1501 bool LyXTabular::isLastCell(int cell) const
1503 if (cell + 1 < numberofcells)
1509 int LyXTabular::getCellAbove(int cell) const
1511 if (row_of_cell(cell) > 0)
1512 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1517 int LyXTabular::getCellBelow(int cell) const
1519 if (row_of_cell(cell) + 1 < rows_)
1520 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1525 int LyXTabular::getLastCellAbove(int cell) const
1527 if (row_of_cell(cell) <= 0)
1529 if (!isMultiColumn(cell))
1530 return getCellAbove(cell);
1531 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1535 int LyXTabular::getLastCellBelow(int cell) const
1537 if (row_of_cell(cell) + 1 >= rows_)
1539 if (!isMultiColumn(cell))
1540 return getCellBelow(cell);
1541 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1545 int LyXTabular::getCellNumber(int row, int column) const
1547 BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1548 return cell_info[row][column].cellno;
1552 void LyXTabular::setUsebox(int cell, BoxType type)
1554 cellinfo_of_cell(cell).usebox = type;
1558 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1560 if (column_info[column_of_cell(cell)].p_width.zero() &&
1561 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1563 if (cellinfo_of_cell(cell).usebox > 1)
1564 return cellinfo_of_cell(cell).usebox;
1565 return useParbox(cell);
1570 // This are functions used for the longtable support
1572 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1577 row_info[row].endfirsthead = flag;
1581 row_info[row].endhead = flag;
1586 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1589 hd.set = haveLTHead();
1590 return row_info[row].endhead;
1594 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1597 hd.set = haveLTFirstHead();
1598 return row_info[row].endfirsthead;
1602 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1607 row_info[row].endlastfoot = flag;
1611 row_info[row].endfoot = flag;
1616 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1619 fd.set = haveLTFoot();
1620 return row_info[row].endfoot;
1624 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1627 fd.set = haveLTLastFoot();
1628 return row_info[row].endlastfoot;
1632 void LyXTabular::setLTNewPage(int row, bool what)
1634 row_info[row].newpage = what;
1638 bool LyXTabular::getLTNewPage(int row) const
1640 return row_info[row].newpage;
1644 bool LyXTabular::haveLTHead() const
1646 for (int i = 0; i < rows_; ++i)
1647 if (row_info[i].endhead)
1653 bool LyXTabular::haveLTFirstHead() const
1655 if (endfirsthead.empty)
1657 for (int i = 0; i < rows_; ++i)
1658 if (row_info[i].endfirsthead)
1664 bool LyXTabular::haveLTFoot() const
1666 for (int i = 0; i < rows_; ++i)
1667 if (row_info[i].endfoot)
1673 bool LyXTabular::haveLTLastFoot() const
1675 if (endlastfoot.empty)
1677 for (int i = 0; i < rows_; ++i)
1678 if (row_info[i].endlastfoot)
1684 // end longtable support functions
1686 void LyXTabular::setAscentOfRow(int row, int height)
1688 if (row >= rows_ || row_info[row].ascent_of_row == height)
1690 row_info[row].ascent_of_row = height;
1694 void LyXTabular::setDescentOfRow(int row, int height)
1696 if (row >= rows_ || row_info[row].descent_of_row == height)
1698 row_info[row].descent_of_row = height;
1702 int LyXTabular::getAscentOfRow(int row) const
1706 return row_info[row].ascent_of_row;
1710 int LyXTabular::getDescentOfRow(int row) const
1714 return row_info[row].descent_of_row;
1718 int LyXTabular::getHeightOfTabular() const
1721 for (int row = 0; row < rows_; ++row)
1722 height += getAscentOfRow(row) + getDescentOfRow(row) +
1723 getAdditionalHeight(row);
1728 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1730 if (row >= rows_ || column >= columns_)
1732 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1736 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1738 if (row < 0 || row >= rows_)
1741 int const fcell = getFirstCellInRow(row);
1742 int const n = numberOfCellsInRow(fcell) + fcell;
1745 for (int i = fcell; i < n; ++i) {
1749 if (tmp == n - fcell) {
1752 for (int i = fcell; i < n; ++i) {
1755 << column_of_cell(i) + 1
1757 << right_column_of_cell(i) + 1
1769 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1771 if (row < 0 || row >= rows_)
1774 int const fcell = getFirstCellInRow(row);
1775 int const n = numberOfCellsInRow(fcell) + fcell;
1778 for (int i = fcell; i < n; ++i) {
1782 if (tmp == n - fcell) {
1785 for (int i = fcell; i < n; ++i) {
1786 if (bottomLine(i)) {
1788 << column_of_cell(i) + 1
1790 << right_column_of_cell(i) + 1
1802 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1806 if (getRotateCell(cell)) {
1807 os << "\\begin{sideways}\n";
1810 if (isMultiColumn(cell)) {
1811 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1812 if (!cellinfo_of_cell(cell).align_special.empty()) {
1813 os << cellinfo_of_cell(cell).align_special << "}{";
1815 if (leftLine(cell) &&
1816 (isFirstCellInRow(cell) ||
1817 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1818 !rightLine(cell - 1, true))))
1822 if (!getPWidth(cell).zero()) {
1823 switch (getVAlignment(cell)) {
1824 case LYX_VALIGN_TOP:
1827 case LYX_VALIGN_MIDDLE:
1830 case LYX_VALIGN_BOTTOM:
1835 << getPWidth(cell).asLatexString()
1838 switch (getAlignment(cell)) {
1839 case LYX_ALIGN_LEFT:
1842 case LYX_ALIGN_RIGHT:
1850 if (rightLine(cell))
1852 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1858 if (getUsebox(cell) == BOX_PARBOX) {
1860 switch (getVAlignment(cell)) {
1861 case LYX_VALIGN_TOP:
1864 case LYX_VALIGN_MIDDLE:
1867 case LYX_VALIGN_BOTTOM:
1871 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1872 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1873 os << "\\begin{minipage}[";
1874 switch (getVAlignment(cell)) {
1875 case LYX_VALIGN_TOP:
1878 case LYX_VALIGN_MIDDLE:
1881 case LYX_VALIGN_BOTTOM:
1885 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1892 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1897 if (getUsebox(cell) == BOX_PARBOX)
1899 else if (getUsebox(cell) == BOX_MINIPAGE) {
1900 os << "%\n\\end{minipage}";
1903 if (isMultiColumn(cell)) {
1906 if (getRotateCell(cell)) {
1907 os << "%\n\\end{sideways}";
1914 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1915 LatexRunParams const & runparams) const
1917 if (!is_long_tabular)
1921 // output header info
1923 if (endhead.topDL) {
1927 for (int i = 0; i < rows_; ++i) {
1928 if (row_info[i].endhead) {
1929 ret += TeXRow(os, i, buf, runparams);
1932 if (endhead.bottomDL) {
1936 os << "\\endhead\n";
1938 if (endfirsthead.empty) {
1939 os << "\\endfirsthead\n";
1943 // output firstheader info
1944 if (haveLTFirstHead()) {
1945 if (endfirsthead.topDL) {
1949 for (int i = 0; i < rows_; ++i) {
1950 if (row_info[i].endfirsthead) {
1951 ret += TeXRow(os, i, buf, runparams);
1954 if (endfirsthead.bottomDL) {
1958 os << "\\endfirsthead\n";
1961 // output footer info
1963 if (endfoot.topDL) {
1967 for (int i = 0; i < rows_; ++i) {
1968 if (row_info[i].endfoot) {
1969 ret += TeXRow(os, i, buf, runparams);
1972 if (endfoot.bottomDL) {
1976 os << "\\endfoot\n";
1978 if (endlastfoot.empty) {
1979 os << "\\endlastfoot\n";
1983 // output lastfooter info
1984 if (haveLTLastFoot()) {
1985 if (endlastfoot.topDL) {
1989 for (int i = 0; i < rows_; ++i) {
1990 if (row_info[i].endlastfoot) {
1991 ret += TeXRow(os, i, buf, runparams);
1994 if (endlastfoot.bottomDL) {
1998 os << "\\endlastfoot\n";
2005 bool LyXTabular::isValidRow(int row) const
2007 if (!is_long_tabular)
2009 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2010 !row_info[row].endfoot && !row_info[row].endlastfoot;
2014 int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
2015 LatexRunParams const & runparams) const
2018 int cell = getCellNumber(i, 0);
2019 BufferParams const & bufferparams = buf.params();
2021 ret += TeXTopHLine(os, i);
2022 for (int j = 0; j < columns_; ++j) {
2023 if (isPartOfMultiColumn(i, j))
2025 ret += TeXCellPreamble(os, cell);
2026 InsetText & inset = getCellInset(cell);
2028 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(bufferparams) &&
2029 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2033 ret += inset.latex(buf, os, runparams);
2037 ret += TeXCellPostamble(os, cell);
2038 if (!isLastCellInRow(cell)) { // not last cell in row
2044 os << "\\tabularnewline\n";
2046 ret += TeXBottomHLine(os, i);
2051 int LyXTabular::latex(Buffer const & buf, ostream & os,
2052 LatexRunParams const & runparams) const
2056 //+---------------------------------------------------------------------
2057 //+ first the opening preamble +
2058 //+---------------------------------------------------------------------
2061 os << "\\begin{sideways}\n";
2064 if (is_long_tabular)
2065 os << "\\begin{longtable}{";
2067 os << "\\begin{tabular}{";
2068 for (int i = 0; i < columns_; ++i) {
2069 if (!column_info[i].align_special.empty()) {
2070 os << column_info[i].align_special;
2072 if (column_info[i].left_line)
2074 if (!column_info[i].p_width.zero()) {
2075 switch (column_info[i].alignment) {
2076 case LYX_ALIGN_LEFT:
2077 os << ">{\\raggedright}";
2079 case LYX_ALIGN_RIGHT:
2080 os << ">{\\raggedleft}";
2082 case LYX_ALIGN_CENTER:
2083 os << ">{\\centering}";
2085 case LYX_ALIGN_NONE:
2086 case LYX_ALIGN_BLOCK:
2087 case LYX_ALIGN_LAYOUT:
2088 case LYX_ALIGN_SPECIAL:
2092 switch (column_info[i].valignment) {
2093 case LYX_VALIGN_TOP:
2096 case LYX_VALIGN_MIDDLE:
2099 case LYX_VALIGN_BOTTOM:
2104 << column_info[i].p_width.asLatexString()
2107 switch (column_info[i].alignment) {
2108 case LYX_ALIGN_LEFT:
2111 case LYX_ALIGN_RIGHT:
2119 if (column_info[i].right_line)
2126 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2128 //+---------------------------------------------------------------------
2129 //+ the single row and columns (cells) +
2130 //+---------------------------------------------------------------------
2132 for (int i = 0; i < rows_; ++i) {
2133 if (isValidRow(i)) {
2134 ret += TeXRow(os, i, buf, runparams);
2135 if (is_long_tabular && row_info[i].newpage) {
2136 os << "\\newpage\n";
2142 //+---------------------------------------------------------------------
2143 //+ the closing of the tabular +
2144 //+---------------------------------------------------------------------
2146 if (is_long_tabular)
2147 os << "\\end{longtable}";
2149 os << "\\end{tabular}";
2151 os << "\n\\end{sideways}";
2159 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os) const
2161 os << "<tabular ca=\"";
2162 for (int i = 0; i < columns_; ++i) {
2163 switch (column_info[i].alignment) {
2164 case LYX_ALIGN_LEFT:
2167 case LYX_ALIGN_RIGHT:
2178 for (int i = 0; i < rows_; ++i) {
2179 for (int j = 0; j < columns_; ++j) {
2180 if (isPartOfMultiColumn(i, j))
2182 InsetText & inset = getCellInset(cell);
2184 ret += inset.linuxdoc(buf, os);
2186 if (isLastCellInRow(cell)) {
2195 os << "</tabular>\n";
2200 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row) const
2203 int cell = getFirstCellInRow(row);
2206 for (int j = 0; j < columns_; ++j) {
2207 if (isPartOfMultiColumn(row, j))
2210 os << "<entry align=\"";
2211 switch (getAlignment(cell)) {
2212 case LYX_ALIGN_LEFT:
2215 case LYX_ALIGN_RIGHT:
2223 os << "\" valign=\"";
2224 switch (getVAlignment(cell)) {
2225 case LYX_VALIGN_TOP:
2228 case LYX_VALIGN_BOTTOM:
2231 case LYX_VALIGN_MIDDLE:
2236 if (isMultiColumn(cell)) {
2237 os << " namest=\"col" << j << "\" ";
2238 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2242 ret += getCellInset(cell).docbook(buf, os, true);
2251 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2252 bool /*mixcont*/) const
2256 //+---------------------------------------------------------------------
2257 //+ first the opening preamble +
2258 //+---------------------------------------------------------------------
2260 os << "<tgroup cols=\"" << columns_
2261 << "\" colsep=\"1\" rowsep=\"1\">\n";
2263 for (int i = 0; i < columns_; ++i) {
2264 os << "<colspec colname=\"col" << i << "\" align=\"";
2265 switch (column_info[i].alignment) {
2266 case LYX_ALIGN_LEFT:
2269 case LYX_ALIGN_RIGHT:
2280 //+---------------------------------------------------------------------
2281 //+ Long Tabular case +
2282 //+---------------------------------------------------------------------
2284 // output header info
2285 if (haveLTHead() || haveLTFirstHead()) {
2288 for (int i = 0; i < rows_; ++i) {
2289 if (row_info[i].endhead || row_info[i].endfirsthead) {
2290 ret += docbookRow(buf, os, i);
2296 // output footer info
2297 if (haveLTFoot() || haveLTLastFoot()) {
2300 for (int i = 0; i < rows_; ++i) {
2301 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2302 ret += docbookRow(buf, os, i);
2309 //+---------------------------------------------------------------------
2310 //+ the single row and columns (cells) +
2311 //+---------------------------------------------------------------------
2315 for (int i = 0; i < rows_; ++i) {
2316 if (isValidRow(i)) {
2317 ret += docbookRow(buf, os, i);
2322 //+---------------------------------------------------------------------
2323 //+ the closing of the tabular +
2324 //+---------------------------------------------------------------------
2333 int LyXTabular::asciiTopHLine(ostream & os, int row,
2334 vector<unsigned int> const & clen) const
2336 int const fcell = getFirstCellInRow(row);
2337 int const n = numberOfCellsInRow(fcell) + fcell;
2340 for (int i = fcell; i < n; ++i) {
2350 for (int i = fcell; i < n; ++i) {
2361 int column = column_of_cell(i);
2362 int len = clen[column];
2363 while (isPartOfMultiColumn(row, ++column))
2364 len += clen[column] + 4;
2365 os << string(len, ch);
2380 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2381 vector<unsigned int> const & clen) const
2383 int const fcell = getFirstCellInRow(row);
2384 int const n = numberOfCellsInRow(fcell) + fcell;
2387 for (int i = fcell; i < n; ++i) {
2388 if (bottomLine(i)) {
2397 for (int i = fcell; i < n; ++i) {
2398 if (bottomLine(i)) {
2408 int column = column_of_cell(i);
2409 int len = clen[column];
2410 while (isPartOfMultiColumn(row, ++column))
2411 len += clen[column] + 4;
2412 os << string(len, ch);
2413 if (bottomLine(i)) {
2427 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2428 int cell, int row, int column,
2429 vector<unsigned int> const & clen,
2430 bool onlydata) const
2433 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2445 unsigned int len1 = sstr.str().length();
2446 unsigned int len2 = clen[column];
2447 while (isPartOfMultiColumn(row, ++column))
2448 len2 += clen[column] + 4;
2451 switch (getAlignment(cell)) {
2453 case LYX_ALIGN_LEFT:
2456 case LYX_ALIGN_RIGHT:
2460 case LYX_ALIGN_CENTER:
2466 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2468 if (rightLine(cell))
2477 int LyXTabular::ascii(Buffer const & buf, ostream & os, int const depth,
2478 bool onlydata, unsigned char delim) const
2482 // first calculate the width of the single columns
2483 vector<unsigned int> clen(columns_);
2486 // first all non (real) multicolumn cells!
2487 for (int j = 0; j < columns_; ++j) {
2489 for (int i = 0; i < rows_; ++i) {
2490 int cell = getCellNumber(i, j);
2491 if (isMultiColumnReal(cell))
2494 getCellInset(cell).ascii(buf, sstr, 0);
2495 if (clen[j] < sstr.str().length())
2496 clen[j] = sstr.str().length();
2499 // then all (real) multicolumn cells!
2500 for (int j = 0; j < columns_; ++j) {
2501 for (int i = 0; i < rows_; ++i) {
2502 int cell = getCellNumber(i, j);
2503 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2506 getCellInset(cell).ascii(buf, sstr, 0);
2507 int len = int(sstr.str().length());
2508 int const n = cells_in_multicolumn(cell);
2509 for (int k = j; len > 0 && k < j + n - 1; ++k)
2511 if (len > int(clen[j + n - 1]))
2512 clen[j + n - 1] = len;
2517 for (int i = 0; i < rows_; ++i) {
2518 if (!onlydata && asciiTopHLine(os, i, clen))
2519 for (int j = 0; j < depth; ++j)
2521 for (int j = 0; j < columns_; ++j) {
2522 if (isPartOfMultiColumn(i, j))
2524 if (onlydata && j > 0)
2526 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2531 for (int j = 0; j < depth; ++j)
2533 if (asciiBottomHLine(os, i, clen))
2534 for (int j = 0; j < depth; ++j)
2542 InsetText & LyXTabular::getCellInset(int cell) const
2544 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2548 InsetText & LyXTabular::getCellInset(int row, int column) const
2550 return cell_info[row][column].inset;
2554 int LyXTabular::getCellFromInset(InsetOld const * inset) const
2556 // is this inset part of the tabular?
2558 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2562 for (int cell = getNumberOfCells(); cell >= 0; --cell)
2563 if (&getCellInset(cell) == inset) {
2564 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2565 << "cell=" << cell << endl;
2569 // We should have found a cell at this point
2570 lyxerr << "LyXTabular::getCellFromInset: Cell not found!" << endl;
2575 void LyXTabular::validate(LaTeXFeatures & features) const
2577 features.require("NeedTabularnewline");
2578 if (isLongTabular())
2579 features.require("longtable");
2581 features.require("rotating");
2582 for (int cell = 0; cell < numberofcells; ++cell) {
2583 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2584 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2585 features.require("array");
2586 getCellInset(cell).validate(features);
2591 void LyXTabular::getLabelList(Buffer const & buffer,
2592 std::vector<string> & list) const
2594 for (int i = 0; i < rows_; ++i)
2595 for (int j = 0; j < columns_; ++j)
2596 getCellInset(i, j).getLabelList(buffer, list);
2600 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2602 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2603 ParagraphList::const_iterator cit = parlist.begin();
2604 ParagraphList::const_iterator end = parlist.end();
2606 for (; cit != end; ++cit)
2607 for (int i = 0; i < cit->size(); ++i)
2608 if (cit->isNewline(i))