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 "outputparams.h"
28 #include "paragraph.h"
30 #include "insets/insettabular.h"
32 #include "support/lstrings.h"
33 #include "support/tostr.h"
35 #include "support/std_sstream.h"
37 using lyx::support::ltrim;
38 using lyx::support::prefixIs;
39 using lyx::support::rtrim;
40 using lyx::support::strToInt;
41 using lyx::support::suffixIs;
50 using std::ostringstream;
54 #ifndef CXX_GLOBAL_CSTD
61 int const WIDTH_OF_LINE = 5;
64 string const write_attribute(string const & name, T const & t)
66 string const s = tostr(t);
67 return s.empty() ? s : " " + name + "=\"" + s + "\"";
70 string const write_attribute(string const & name, string const & t)
72 return t.empty() ? t : " " + name + "=\"" + t + "\"";
76 string const write_attribute(string const & name, bool const & b)
78 // we write only true attribute values so we remove a bit of the
79 // file format bloat for tabulars.
80 return b ? write_attribute(name, tostr(b)) : string();
84 string const write_attribute(string const & name, int const & i)
86 // we write only true attribute values so we remove a bit of the
87 // file format bloat for tabulars.
88 return i ? write_attribute(name, tostr(i)) : string();
92 string const write_attribute(string const & name, LyXLength const & value)
94 // we write only the value if we really have one same reson as above.
95 return value.zero() ? string() : write_attribute(name, value.asString());
99 string const tostr(LyXAlignment const & num)
104 case LYX_ALIGN_BLOCK:
108 case LYX_ALIGN_CENTER:
110 case LYX_ALIGN_RIGHT:
112 case LYX_ALIGN_LAYOUT:
114 case LYX_ALIGN_SPECIAL:
121 string const tostr(LyXTabular::VAlignment const & num)
124 case LyXTabular::LYX_VALIGN_TOP:
126 case LyXTabular::LYX_VALIGN_MIDDLE:
128 case LyXTabular::LYX_VALIGN_BOTTOM:
135 string const tostr(LyXTabular::BoxType const & num)
138 case LyXTabular::BOX_NONE:
140 case LyXTabular::BOX_PARBOX:
142 case LyXTabular::BOX_MINIPAGE:
149 // I would have liked a fromstr template a lot better. (Lgb)
150 bool string2type(string const str, LyXAlignment & num)
153 num = LYX_ALIGN_NONE;
154 else if (str == "block")
155 num = LYX_ALIGN_BLOCK;
156 else if (str == "left")
157 num = LYX_ALIGN_LEFT;
158 else if (str == "center")
159 num = LYX_ALIGN_CENTER;
160 else if (str == "right")
161 num = LYX_ALIGN_RIGHT;
168 bool string2type(string const str, LyXTabular::VAlignment & num)
171 num = LyXTabular::LYX_VALIGN_TOP;
172 else if (str == "middle" )
173 num = LyXTabular::LYX_VALIGN_MIDDLE;
174 else if (str == "bottom")
175 num = LyXTabular::LYX_VALIGN_BOTTOM;
182 bool string2type(string const str, LyXTabular::BoxType & num)
185 num = LyXTabular::BOX_NONE;
186 else if (str == "parbox")
187 num = LyXTabular::BOX_PARBOX;
188 else if (str == "minipage")
189 num = LyXTabular::BOX_MINIPAGE;
196 bool string2type(string const str, bool & num)
200 else if (str == "false")
208 bool getTokenValue(string const & str, const char * token, string & ret)
211 size_t token_length = strlen(token);
212 string::size_type pos = str.find(token);
214 if (pos == string::npos || pos + token_length + 1 >= str.length()
215 || str[pos + token_length] != '=')
217 pos += token_length + 1;
219 if (ch != '"' && ch != '\'') { // only read till next space
223 while (pos < str.length() - 1 && str[++pos] != ch)
230 bool getTokenValue(string const & str, const char * token, int & num)
234 if (!getTokenValue(str, token, tmp))
241 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
244 if (!getTokenValue(str, token, tmp))
246 return string2type(tmp, num);
250 bool getTokenValue(string const & str, const char * token,
251 LyXTabular::VAlignment & num)
254 if (!getTokenValue(str, token, tmp))
256 return string2type(tmp, num);
260 bool getTokenValue(string const & str, const char * token,
261 LyXTabular::BoxType & num)
264 if (!getTokenValue(str, token, tmp))
266 return string2type(tmp, num);
270 bool getTokenValue(string const & str, const char * token, bool & flag)
272 // set the flag always to false as this should be the default for bools
273 // not in the file-format.
276 if (!getTokenValue(str, token, tmp))
278 return string2type(tmp, flag);
282 bool getTokenValue(string const & str, const char * token, LyXLength & len)
284 // set the lenght to be zero() as default as this it should be if not
285 // in the file format.
288 if (!getTokenValue(str, token, tmp))
290 return isValidLength(tmp, &len);
294 void l_getline(istream & is, string & str)
297 while (str.empty()) {
299 if (!str.empty() && str[str.length() - 1] == '\r')
300 str.erase(str.length() - 1);
306 /// Define a few methods for the inner structs
308 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
313 multicolumn = LyXTabular::CELL_NORMAL;
314 alignment = LYX_ALIGN_CENTER;
315 valignment = LYX_VALIGN_TOP;
325 LyXTabular::rowstruct::rowstruct()
332 endfirsthead = false;
339 LyXTabular::columnstruct::columnstruct()
343 alignment = LYX_ALIGN_CENTER;
344 valignment = LYX_VALIGN_TOP;
349 LyXTabular::ltType::ltType()
357 LyXTabular::LyXTabular(BufferParams const & bp, int rows_arg, int columns_arg)
359 init(bp, rows_arg, columns_arg);
363 // activates all lines and sets all widths to 0
364 void LyXTabular::init(BufferParams const & bp, int rows_arg, int columns_arg)
367 columns_ = columns_arg;
368 row_info = row_vector(rows_);
369 column_info = column_vector(columns_);
370 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
371 row_info.reserve(10);
372 column_info.reserve(10);
373 cell_info.reserve(100);
375 for (int i = 0; i < rows_; ++i)
376 cell_info[i].back().right_line = true;
377 row_info.back().bottom_line = true;
378 row_info.front().bottom_line = true;
379 column_info.back().right_line = true;
380 is_long_tabular = false;
385 void LyXTabular::fixCellNums()
388 for (int i = 0; i < rows_; ++i) {
389 for (int j = 0; j < columns_; ++j) {
390 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
391 cell_info[i][j].cellno = cellno++;
393 cell_info[i].back().right_line = true;
396 set_row_column_number_info();
400 void LyXTabular::setOwner(InsetTabular * inset)
402 for (int i = 0; i < rows_; ++i) {
403 for (int j = 0; j < columns_; ++j) {
404 cell_info[i][j].inset.setOwner(inset);
405 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
411 void LyXTabular::appendRow(BufferParams const & bp, int cell)
415 int row = row_of_cell(cell);
417 row_vector::iterator rit = row_info.begin() + row;
418 row_info.insert(rit, rowstruct());
419 // now set the values of the row before
420 row_info[row] = row_info[row + 1];
422 cell_vvector old(rows_ - 1);
423 for (int i = 0; i < rows_ - 1; ++i)
424 swap(cell_info[i], old[i]);
426 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
428 for (int i = 0; i <= row; ++i)
429 swap(cell_info[i], old[i]);
430 for (int i = row + 2; i < rows_; ++i)
431 swap(cell_info[i], old[i - 1]);
433 if (bp.tracking_changes)
434 for (int j = 0; j < columns_; ++j)
435 cell_info[row + 1][j].inset.markNew(true);
437 set_row_column_number_info();
441 void LyXTabular::deleteRow(int row)
443 // Not allowed to delete last row
447 row_info.erase(row_info.begin() + row);
448 cell_info.erase(cell_info.begin() + row);
454 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
458 int const column = column_of_cell(cell);
459 column_vector::iterator cit = column_info.begin() + column + 1;
460 column_info.insert(cit, columnstruct());
461 // set the column values of the column before
462 column_info[column + 1] = column_info[column];
464 for (int i = 0; i < rows_; ++i) {
465 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
467 // care about multicolumns
468 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
469 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
471 if (column + 2 >= columns_
472 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
473 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
476 for (int i = 0; i < rows_; ++i) {
477 cell_info[i][column + 1].inset.clear(false);
478 if (bp.tracking_changes)
479 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);
499 void LyXTabular::set_row_column_number_info()
502 for (int row = 0; row < rows_; ++row) {
503 for (int column = 0; column < columns_; ++column) {
504 if (cell_info[row][column].multicolumn
505 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
507 cell_info[row][column].cellno = numberofcells;
510 ++numberofcells; // because this is one more than as we start from 0
512 rowofcell.resize(numberofcells);
513 columnofcell.resize(numberofcells);
515 for (int row = 0, column = 0, c = 0;
516 c < numberofcells && row < rows_ && column < columns_;) {
518 columnofcell[c] = column;
522 } while (column < columns_ &&
523 cell_info[row][column].multicolumn
524 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
526 if (column == columns_) {
532 for (int row = 0; row < rows_; ++row) {
533 for (int column = 0; column < columns_; ++column) {
534 if (isPartOfMultiColumn(row,column))
536 cell_info[row][column].inset.setAutoBreakRows(
537 !getPWidth(getCellNumber(row, column)).zero());
543 int LyXTabular::getNumberOfCells() const
545 return numberofcells;
549 int LyXTabular::numberOfCellsInRow(int cell) const
551 int const row = row_of_cell(cell);
553 for (int i = 0; i < columns_; ++i)
554 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
560 // returns 1 if there is a topline, returns 0 if not
561 bool LyXTabular::topLine(int cell, bool onlycolumn) const
563 if (!onlycolumn && isMultiColumn(cell))
564 return cellinfo_of_cell(cell).top_line;
565 return row_info[row_of_cell(cell)].top_line;
569 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
571 if (!onlycolumn && isMultiColumn(cell))
572 return cellinfo_of_cell(cell).bottom_line;
573 return row_info[row_of_cell(cell)].bottom_line;
577 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
579 if (!onlycolumn && isMultiColumn(cell) &&
580 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
582 if (cellinfo_of_cell(cell).align_special.empty())
583 return cellinfo_of_cell(cell).left_line;
584 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
586 if (column_info[column_of_cell(cell)].align_special.empty())
587 return column_info[column_of_cell(cell)].left_line;
588 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
592 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
594 if (!onlycolumn && isMultiColumn(cell) &&
595 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
597 if (cellinfo_of_cell(cell).align_special.empty())
598 return cellinfo_of_cell(cell).right_line;
599 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
601 if (column_info[column_of_cell(cell)].align_special.empty())
602 return column_info[right_column_of_cell(cell)].right_line;
603 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
607 bool LyXTabular::topAlreadyDrawn(int cell) const
609 int row = row_of_cell(cell);
610 if (row > 0 && !getAdditionalHeight(row)) {
611 int column = column_of_cell(cell);
614 && cell_info[row][column].multicolumn
615 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
617 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
618 return row_info[row].bottom_line;
620 return cell_info[row][column].bottom_line;
626 bool LyXTabular::leftAlreadyDrawn(int cell) const
628 int column = column_of_cell(cell);
630 int row = row_of_cell(cell);
632 (cell_info[row][column].multicolumn ==
633 LyXTabular::CELL_PART_OF_MULTICOLUMN));
634 if (getAdditionalWidth(cell_info[row][column].cellno))
636 return rightLine(cell_info[row][column].cellno);
642 bool LyXTabular::isLastRow(int cell) const
644 return row_of_cell(cell) == rows_ - 1;
648 int LyXTabular::getAdditionalHeight(int row) const
650 if (!row || row >= rows_)
656 for (int column = 0; column < columns_ && bottom; ++column) {
657 switch (cell_info[row - 1][column].multicolumn) {
658 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
659 bottom = cell_info[row - 1][column].bottom_line;
661 case LyXTabular::CELL_NORMAL:
662 bottom = row_info[row - 1].bottom_line;
665 for (int column = 0; column < columns_ && top; ++column) {
666 switch (cell_info[row][column].multicolumn) {
667 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
668 top = cell_info[row][column].top_line;
670 case LyXTabular::CELL_NORMAL:
671 top = row_info[row].top_line;
675 return WIDTH_OF_LINE;
680 int LyXTabular::getAdditionalWidth(int cell) const
682 // internally already set in setWidthOfCell
683 // used to get it back in text.C
684 int const col = right_column_of_cell(cell);
685 int const row = row_of_cell(cell);
686 if (col < columns_ - 1 && rightLine(cell) &&
687 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
689 return WIDTH_OF_LINE;
695 // returns the maximum over all rows
696 int LyXTabular::getWidthOfColumn(int cell) const
698 int const column1 = column_of_cell(cell);
699 int const column2 = right_column_of_cell(cell);
701 for (int i = column1; i <= column2; ++i)
702 result += column_info[i].width_of_column;
707 int LyXTabular::getWidthOfTabular() const
709 return width_of_tabular;
713 // returns true if a complete update is necessary, otherwise false
714 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
716 if (!isMultiColumn(cell))
719 int const row = row_of_cell(cell);
720 int const column1 = column_of_cell(cell);
721 int const column2 = right_column_of_cell(cell);
722 int const old_val = cell_info[row][column2].width_of_cell;
724 // first set columns to 0 so we can calculate the right width
725 for (int i = column1; i <= column2; ++i) {
726 cell_info[row][i].width_of_cell = 0;
728 // set the width to MAX_WIDTH until width > 0
729 int width = new_width + 2 * WIDTH_OF_LINE;
731 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
732 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
733 width -= column_info[i].width_of_column;
736 cell_info[row][i].width_of_cell = width;
738 if (old_val != cell_info[row][column2].width_of_cell) {
739 // in this case we have to recalculate all multicolumn cells which
740 // have this column as one of theirs but not as last one
741 calculate_width_of_column_NMC(i);
742 recalculateMulticolumnsOfColumn(i);
743 calculate_width_of_column(i);
749 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
751 // the last column does not have to be recalculated because all
752 // multicolumns will have here there last multicolumn cell which
753 // always will have the whole rest of the width of the cell.
754 if (column > (columns_ - 2))
756 for(int row = 0; row < rows_; ++row) {
757 int mc = cell_info[row][column].multicolumn;
758 int nmc = cell_info[row][column+1].multicolumn;
759 // we only have to update multicolumns which do not have this
760 // column as their last column!
761 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
762 (mc == CELL_PART_OF_MULTICOLUMN &&
763 nmc == CELL_PART_OF_MULTICOLUMN))
765 int const cellno = cell_info[row][column].cellno;
766 setWidthOfMulticolCell(cellno,
767 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
773 // returns 1 if a complete update is necessary, otherwise 0
774 void LyXTabular::setWidthOfCell(int cell, int new_width)
776 int const row = row_of_cell(cell);
777 int const column1 = column_of_cell(cell);
782 if (rightLine(cell_info[row][column1].cellno, true) &&
783 column1 < columns_ - 1 &&
784 leftLine(cell_info[row][column1+1].cellno, true))
786 add_width = WIDTH_OF_LINE;
789 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
792 if (isMultiColumnReal(cell)) {
793 tmp = setWidthOfMulticolCell(cell, new_width);
795 width = new_width + 2 * WIDTH_OF_LINE + add_width;
796 cell_info[row][column1].width_of_cell = width;
797 tmp = calculate_width_of_column_NMC(column1);
799 recalculateMulticolumnsOfColumn(column1);
802 for (int i = 0; i < columns_; ++i)
803 calculate_width_of_column(i);
804 calculate_width_of_tabular();
809 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
811 if (!isMultiColumn(cell) || onlycolumn)
812 column_info[column_of_cell(cell)].alignment = align;
814 cellinfo_of_cell(cell).alignment = align;
818 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
820 if (!isMultiColumn(cell) || onlycolumn)
821 column_info[column_of_cell(cell)].valignment = align;
823 cellinfo_of_cell(cell).valignment = align;
827 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
829 int const j = column_of_cell(cell);
831 column_info[j].p_width = width;
832 for (int i = 0; i < rows_; ++i) {
833 int const cell = getCellNumber(i, j);
834 // because of multicolumns
835 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
840 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
842 if (!isMultiColumn(cell))
845 cellinfo_of_cell(cell).p_width = width;
846 getCellInset(cell).setAutoBreakRows(!width.zero());
851 void LyXTabular::setAlignSpecial(int cell, string const & special,
852 LyXTabular::Feature what)
854 if (what == SET_SPECIAL_MULTI)
855 cellinfo_of_cell(cell).align_special = special;
857 column_info[column_of_cell(cell)].align_special = special;
861 void LyXTabular::setAllLines(int cell, bool line)
863 setTopLine(cell, line);
864 setBottomLine(cell, line);
865 setRightLine(cell, line);
866 setLeftLine(cell, line);
870 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
872 int const row = row_of_cell(cell);
873 if (onlycolumn || !isMultiColumn(cell))
874 row_info[row].top_line = line;
876 cellinfo_of_cell(cell).top_line = line;
880 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
882 if (onlycolumn || !isMultiColumn(cell))
883 row_info[row_of_cell(cell)].bottom_line = line;
885 cellinfo_of_cell(cell).bottom_line = line;
889 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
891 if (onlycolumn || !isMultiColumn(cell))
892 column_info[column_of_cell(cell)].left_line = line;
894 cellinfo_of_cell(cell).left_line = line;
898 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
900 if (onlycolumn || !isMultiColumn(cell))
901 column_info[right_column_of_cell(cell)].right_line = line;
903 cellinfo_of_cell(cell).right_line = line;
907 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
909 if (!onlycolumn && isMultiColumn(cell))
910 return cellinfo_of_cell(cell).alignment;
911 return column_info[column_of_cell(cell)].alignment;
915 LyXTabular::VAlignment
916 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
918 if (!onlycolumn && isMultiColumn(cell))
919 return cellinfo_of_cell(cell).valignment;
920 return column_info[column_of_cell(cell)].valignment;
924 LyXLength const LyXTabular::getPWidth(int cell) const
926 if (isMultiColumn(cell))
927 return cellinfo_of_cell(cell).p_width;
928 return column_info[column_of_cell(cell)].p_width;
932 LyXLength const LyXTabular::getColumnPWidth(int cell) const
934 return column_info[column_of_cell(cell)].p_width;
938 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
940 if (isMultiColumn(cell))
941 return cellinfo_of_cell(cell).p_width;
946 string const LyXTabular::getAlignSpecial(int cell, int what) const
948 if (what == SET_SPECIAL_MULTI)
949 return cellinfo_of_cell(cell).align_special;
950 return column_info[column_of_cell(cell)].align_special;
954 int LyXTabular::getWidthOfCell(int cell) const
956 int const row = row_of_cell(cell);
957 int const column1 = column_of_cell(cell);
958 int const column2 = right_column_of_cell(cell);
960 for (int i = column1; i <= column2; ++i)
961 result += cell_info[row][i].width_of_cell;
966 int LyXTabular::getBeginningOfTextInCell(int cell) const
970 switch (getAlignment(cell)) {
971 case LYX_ALIGN_CENTER:
972 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
974 case LYX_ALIGN_RIGHT:
975 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
976 // + getAdditionalWidth(cell);
979 // LYX_ALIGN_LEFT: nothing :-)
989 bool LyXTabular::isFirstCellInRow(int cell) const
991 return column_of_cell(cell) == 0;
995 int LyXTabular::getFirstCellInRow(int row) const
999 return cell_info[row][0].cellno;
1003 bool LyXTabular::isLastCellInRow(int cell) const
1005 return right_column_of_cell(cell) == columns_ - 1;
1009 int LyXTabular::getLastCellInRow(int row) const
1011 if (row > rows_ - 1)
1013 return cell_info[row][columns_-1].cellno;
1017 void LyXTabular::calculate_width_of_column(int column)
1020 for (int i = 0; i < rows_; ++i)
1021 maximum = max(cell_info[i][column].width_of_cell, maximum);
1022 column_info[column].width_of_column = maximum;
1027 // Calculate the columns regarding ONLY the normal cells and if this
1028 // column is inside a multicolumn cell then use it only if its the last
1029 // column of this multicolumn cell as this gives an added width to the
1030 // column, all the rest should be adapted!
1032 bool LyXTabular::calculate_width_of_column_NMC(int column)
1034 int const old_column_width = column_info[column].width_of_column;
1036 for (int i = 0; i < rows_; ++i) {
1037 int cell = getCellNumber(i, column);
1038 bool ismulti = isMultiColumnReal(cell);
1039 if ((!ismulti || column == right_column_of_cell(cell)) &&
1040 cell_info[i][column].width_of_cell > max)
1042 max = cell_info[i][column].width_of_cell;
1045 column_info[column].width_of_column = max;
1046 return column_info[column].width_of_column != old_column_width;
1050 void LyXTabular::calculate_width_of_tabular()
1052 width_of_tabular = 0;
1053 for (int i = 0; i < columns_; ++i)
1054 width_of_tabular += column_info[i].width_of_column;
1058 int LyXTabular::row_of_cell(int cell) const
1060 if (cell >= numberofcells)
1064 return rowofcell[cell];
1068 int LyXTabular::column_of_cell(int cell) const
1070 if (cell >= numberofcells)
1071 return columns_ - 1;
1074 return columnofcell[cell];
1078 int LyXTabular::right_column_of_cell(int cell) const
1080 int const row = row_of_cell(cell);
1081 int column = column_of_cell(cell);
1082 while (column < columns_ - 1 &&
1083 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1089 void LyXTabular::write(Buffer const & buf, ostream & os) const
1093 << write_attribute("version", 3)
1094 << write_attribute("rows", rows_)
1095 << write_attribute("columns", columns_)
1097 // global longtable options
1099 << write_attribute("rotate", rotate)
1100 << write_attribute("islongtable", is_long_tabular)
1101 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1102 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1103 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1104 << write_attribute("headTopDL", endhead.topDL)
1105 << write_attribute("headBottomDL", endhead.bottomDL)
1106 << write_attribute("footTopDL", endfoot.topDL)
1107 << write_attribute("footBottomDL", endfoot.bottomDL)
1108 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1109 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1110 << write_attribute("lastFootEmpty", endlastfoot.empty)
1112 for (int j = 0; j < columns_; ++j) {
1114 << write_attribute("alignment", column_info[j].alignment)
1115 << write_attribute("valignment", column_info[j].valignment)
1116 << write_attribute("leftline", column_info[j].left_line)
1117 << write_attribute("rightline", column_info[j].right_line)
1118 << write_attribute("width", column_info[j].p_width.asString())
1119 << write_attribute("special", column_info[j].align_special)
1122 for (int i = 0; i < rows_; ++i) {
1124 << write_attribute("topline", row_info[i].top_line)
1125 << write_attribute("bottomline", row_info[i].bottom_line)
1126 << write_attribute("endhead", row_info[i].endhead)
1127 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1128 << write_attribute("endfoot", row_info[i].endfoot)
1129 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1130 << write_attribute("newpage", row_info[i].newpage)
1132 for (int j = 0; j < columns_; ++j) {
1134 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1135 << write_attribute("alignment", cell_info[i][j].alignment)
1136 << write_attribute("valignment", cell_info[i][j].valignment)
1137 << write_attribute("topline", cell_info[i][j].top_line)
1138 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1139 << write_attribute("leftline", cell_info[i][j].left_line)
1140 << write_attribute("rightline", cell_info[i][j].right_line)
1141 << write_attribute("rotate", cell_info[i][j].rotate)
1142 << write_attribute("usebox", cell_info[i][j].usebox)
1143 << write_attribute("width", cell_info[i][j].p_width)
1144 << write_attribute("special", cell_info[i][j].align_special)
1146 os << "\\begin_inset ";
1147 cell_info[i][j].inset.write(buf, os);
1148 os << "\n\\end_inset \n"
1153 os << "</lyxtabular>\n";
1157 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1161 row_info[--hr].endhead = true;
1163 // set firstheader info
1164 if (fhr && fhr < rows_) {
1165 if (row_info[fhr].endhead) {
1167 row_info[--fhr].endfirsthead = true;
1168 row_info[fhr].endhead = false;
1170 } else if (row_info[fhr - 1].endhead) {
1171 endfirsthead.empty = true;
1173 while (fhr > 0 && !row_info[--fhr].endhead) {
1174 row_info[fhr].endfirsthead = true;
1179 if (fr && fr < rows_) {
1180 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1181 while (fr > 0 && !row_info[--fr].endhead) {
1182 row_info[fr].endfoot = true;
1183 row_info[fr].endhead = false;
1185 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1186 while (fr > 0 && !row_info[--fr].endfirsthead) {
1187 row_info[fr].endfoot = true;
1188 row_info[fr].endfirsthead = false;
1190 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1191 while (fr > 0 && !row_info[--fr].endhead &&
1192 !row_info[fr].endfirsthead)
1194 row_info[fr].endfoot = true;
1198 // set lastfooter info
1199 if (lfr && lfr < rows_) {
1200 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1201 while (lfr > 0 && !row_info[--lfr].endhead) {
1202 row_info[lfr].endlastfoot = true;
1203 row_info[lfr].endhead = false;
1205 } else if (row_info[lfr].endfirsthead &&
1206 row_info[lfr - 1].endfirsthead)
1208 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1209 row_info[lfr].endlastfoot = true;
1210 row_info[lfr].endfirsthead = false;
1212 } else if (row_info[lfr].endfoot
1213 && row_info[lfr - 1].endfoot) {
1214 while (lfr > 0 && !row_info[--lfr].endfoot) {
1215 row_info[lfr].endlastfoot = true;
1216 row_info[lfr].endfoot = false;
1218 } else if (!row_info[fr - 1].endhead
1219 && !row_info[fr - 1].endfirsthead &&
1220 !row_info[fr - 1].endfoot)
1223 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1224 !row_info[lfr].endfoot)
1226 row_info[lfr].endlastfoot = true;
1228 } else if (haveLTFoot()) {
1229 endlastfoot.empty = true;
1235 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1238 istream & is = lex.getStream();
1240 l_getline(is, line);
1241 if (!prefixIs(line, "<lyxtabular ")
1242 && !prefixIs(line, "<LyXTabular ")) {
1243 BOOST_ASSERT(false);
1248 if (!getTokenValue(line, "version", version))
1250 BOOST_ASSERT(version >= 2);
1253 if (!getTokenValue(line, "rows", rows_arg))
1256 if (!getTokenValue(line, "columns", columns_arg))
1258 init(buf.params(), rows_arg, columns_arg);
1259 l_getline(is, line);
1260 if (!prefixIs(line, "<features")) {
1261 lyxerr << "Wrong tabular format (expected <features ...> got"
1262 << line << ')' << endl;
1265 getTokenValue(line, "rotate", rotate);
1266 getTokenValue(line, "islongtable", is_long_tabular);
1267 // compatibility read for old longtable options. Now we can make any
1268 // row part of the header/footer type we want before it was strict
1269 // sequential from the first row down (as LaTeX does it!). So now when
1270 // we find a header/footer line we have to go up the rows and set it
1271 // on all preceding rows till the first or one with already a h/f option
1272 // set. If we find a firstheader on the same line as a header or a
1273 // lastfooter on the same line as a footer then this should be set empty.
1281 getTokenValue(line, "endhead", hrow);
1282 getTokenValue(line, "endfirsthead", fhrow);
1283 getTokenValue(line, "endfoot", frow);
1284 getTokenValue(line, "endlastfoot", lfrow);
1285 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1287 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1288 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1289 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1290 getTokenValue(line, "headTopDL", endhead.topDL);
1291 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1292 getTokenValue(line, "footTopDL", endfoot.topDL);
1293 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1294 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1295 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1296 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1298 for (int j = 0; j < columns_; ++j) {
1300 if (!prefixIs(line,"<column")) {
1301 lyxerr << "Wrong tabular format (expected <column ...> got"
1302 << line << ')' << endl;
1305 getTokenValue(line, "alignment", column_info[j].alignment);
1306 getTokenValue(line, "valignment", column_info[j].valignment);
1307 getTokenValue(line, "leftline", column_info[j].left_line);
1308 getTokenValue(line, "rightline", column_info[j].right_line);
1309 getTokenValue(line, "width", column_info[j].p_width);
1310 getTokenValue(line, "special", column_info[j].align_special);
1313 for (int i = 0; i < rows_; ++i) {
1314 l_getline(is, line);
1315 if (!prefixIs(line, "<row")) {
1316 lyxerr << "Wrong tabular format (expected <row ...> got"
1317 << line << ')' << endl;
1320 getTokenValue(line, "topline", row_info[i].top_line);
1321 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1322 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1323 getTokenValue(line, "endhead", row_info[i].endhead);
1324 getTokenValue(line, "endfoot", row_info[i].endfoot);
1325 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1326 getTokenValue(line, "newpage", row_info[i].newpage);
1327 for (int j = 0; j < columns_; ++j) {
1328 l_getline(is, line);
1329 if (!prefixIs(line, "<cell")) {
1330 lyxerr << "Wrong tabular format (expected <cell ...> got"
1331 << line << ')' << endl;
1334 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1335 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1336 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1337 getTokenValue(line, "topline", cell_info[i][j].top_line);
1338 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1339 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1340 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1341 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1342 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1343 getTokenValue(line, "width", cell_info[i][j].p_width);
1344 getTokenValue(line, "special", cell_info[i][j].align_special);
1345 l_getline(is, line);
1346 if (prefixIs(line, "\\begin_inset")) {
1347 cell_info[i][j].inset.read(buf, lex);
1348 l_getline(is, line);
1350 if (!prefixIs(line, "</cell>")) {
1351 lyxerr << "Wrong tabular format (expected </cell> got"
1352 << line << ')' << endl;
1356 l_getline(is, line);
1357 if (!prefixIs(line, "</row>")) {
1358 lyxerr << "Wrong tabular format (expected </row> got"
1359 << line << ')' << endl;
1363 while (!prefixIs(line, "</lyxtabular>")) {
1364 l_getline(is, line);
1366 set_row_column_number_info();
1370 bool LyXTabular::isMultiColumn(int cell) const
1372 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1376 bool LyXTabular::isMultiColumnReal(int cell) const
1378 return column_of_cell(cell) != right_column_of_cell(cell) &&
1379 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1383 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1385 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1389 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1391 cellstruct & cs = cellinfo_of_cell(cell);
1392 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1393 cs.alignment = column_info[column_of_cell(cell)].alignment;
1394 cs.top_line = row_info[row_of_cell(cell)].top_line;
1395 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1396 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1397 for (int i = 1; i < number; ++i) {
1398 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1399 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1400 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1401 cs1.inset.clear(false);
1403 set_row_column_number_info();
1407 int LyXTabular::cells_in_multicolumn(int cell) const
1409 int const row = row_of_cell(cell);
1410 int column = column_of_cell(cell);
1413 while (column < columns_ &&
1414 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1423 int LyXTabular::unsetMultiColumn(int cell)
1425 int const row = row_of_cell(cell);
1426 int column = column_of_cell(cell);
1430 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1431 cell_info[row][column].multicolumn = CELL_NORMAL;
1433 while (column < columns_ &&
1434 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1436 cell_info[row][column].multicolumn = CELL_NORMAL;
1441 set_row_column_number_info();
1446 void LyXTabular::setLongTabular(bool what)
1448 is_long_tabular = what;
1452 bool LyXTabular::isLongTabular() const
1454 return is_long_tabular;
1458 void LyXTabular::setRotateTabular(bool flag)
1464 bool LyXTabular::getRotateTabular() const
1470 void LyXTabular::setRotateCell(int cell, bool flag)
1472 cellinfo_of_cell(cell).rotate = flag;
1476 bool LyXTabular::getRotateCell(int cell) const
1478 return cellinfo_of_cell(cell).rotate;
1482 bool LyXTabular::needRotating() const
1486 for (int i = 0; i < rows_; ++i)
1487 for (int j = 0; j < columns_; ++j)
1488 if (cell_info[i][j].rotate)
1494 bool LyXTabular::isLastCell(int cell) const
1496 if (cell + 1 < numberofcells)
1502 int LyXTabular::getCellAbove(int cell) const
1504 if (row_of_cell(cell) > 0)
1505 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1510 int LyXTabular::getCellBelow(int cell) const
1512 if (row_of_cell(cell) + 1 < rows_)
1513 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1518 int LyXTabular::getLastCellAbove(int cell) const
1520 if (row_of_cell(cell) <= 0)
1522 if (!isMultiColumn(cell))
1523 return getCellAbove(cell);
1524 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1528 int LyXTabular::getLastCellBelow(int cell) const
1530 if (row_of_cell(cell) + 1 >= rows_)
1532 if (!isMultiColumn(cell))
1533 return getCellBelow(cell);
1534 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1538 int LyXTabular::getCellNumber(int row, int column) const
1540 BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1541 return cell_info[row][column].cellno;
1545 void LyXTabular::setUsebox(int cell, BoxType type)
1547 cellinfo_of_cell(cell).usebox = type;
1551 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1553 if (column_info[column_of_cell(cell)].p_width.zero() &&
1554 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1556 if (cellinfo_of_cell(cell).usebox > 1)
1557 return cellinfo_of_cell(cell).usebox;
1558 return useParbox(cell);
1563 // This are functions used for the longtable support
1565 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1570 row_info[row].endfirsthead = flag;
1574 row_info[row].endhead = flag;
1579 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1582 hd.set = haveLTHead();
1583 return row_info[row].endhead;
1587 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1590 hd.set = haveLTFirstHead();
1591 return row_info[row].endfirsthead;
1595 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1600 row_info[row].endlastfoot = flag;
1604 row_info[row].endfoot = flag;
1609 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1612 fd.set = haveLTFoot();
1613 return row_info[row].endfoot;
1617 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1620 fd.set = haveLTLastFoot();
1621 return row_info[row].endlastfoot;
1625 void LyXTabular::setLTNewPage(int row, bool what)
1627 row_info[row].newpage = what;
1631 bool LyXTabular::getLTNewPage(int row) const
1633 return row_info[row].newpage;
1637 bool LyXTabular::haveLTHead() const
1639 for (int i = 0; i < rows_; ++i)
1640 if (row_info[i].endhead)
1646 bool LyXTabular::haveLTFirstHead() const
1648 if (endfirsthead.empty)
1650 for (int i = 0; i < rows_; ++i)
1651 if (row_info[i].endfirsthead)
1657 bool LyXTabular::haveLTFoot() const
1659 for (int i = 0; i < rows_; ++i)
1660 if (row_info[i].endfoot)
1666 bool LyXTabular::haveLTLastFoot() const
1668 if (endlastfoot.empty)
1670 for (int i = 0; i < rows_; ++i)
1671 if (row_info[i].endlastfoot)
1677 // end longtable support functions
1679 void LyXTabular::setAscentOfRow(int row, int height)
1681 if (row >= rows_ || row_info[row].ascent_of_row == height)
1683 row_info[row].ascent_of_row = height;
1687 void LyXTabular::setDescentOfRow(int row, int height)
1689 if (row >= rows_ || row_info[row].descent_of_row == height)
1691 row_info[row].descent_of_row = height;
1695 int LyXTabular::getAscentOfRow(int row) const
1699 return row_info[row].ascent_of_row;
1703 int LyXTabular::getDescentOfRow(int row) const
1707 return row_info[row].descent_of_row;
1711 int LyXTabular::getHeightOfTabular() const
1714 for (int row = 0; row < rows_; ++row)
1715 height += getAscentOfRow(row) + getDescentOfRow(row) +
1716 getAdditionalHeight(row);
1721 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1723 if (row >= rows_ || column >= columns_)
1725 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1729 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1731 if (row < 0 || row >= rows_)
1734 int const fcell = getFirstCellInRow(row);
1735 int const n = numberOfCellsInRow(fcell) + fcell;
1738 for (int i = fcell; i < n; ++i) {
1742 if (tmp == n - fcell) {
1745 for (int i = fcell; i < n; ++i) {
1748 << column_of_cell(i) + 1
1750 << right_column_of_cell(i) + 1
1762 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1764 if (row < 0 || row >= rows_)
1767 int const fcell = getFirstCellInRow(row);
1768 int const n = numberOfCellsInRow(fcell) + fcell;
1771 for (int i = fcell; i < n; ++i) {
1775 if (tmp == n - fcell) {
1778 for (int i = fcell; i < n; ++i) {
1779 if (bottomLine(i)) {
1781 << column_of_cell(i) + 1
1783 << right_column_of_cell(i) + 1
1795 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1799 if (getRotateCell(cell)) {
1800 os << "\\begin{sideways}\n";
1803 if (isMultiColumn(cell)) {
1804 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1805 if (!cellinfo_of_cell(cell).align_special.empty()) {
1806 os << cellinfo_of_cell(cell).align_special << "}{";
1808 if (leftLine(cell) &&
1809 (isFirstCellInRow(cell) ||
1810 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1811 !rightLine(cell - 1, true))))
1815 if (!getPWidth(cell).zero()) {
1816 switch (getVAlignment(cell)) {
1817 case LYX_VALIGN_TOP:
1820 case LYX_VALIGN_MIDDLE:
1823 case LYX_VALIGN_BOTTOM:
1828 << getPWidth(cell).asLatexString()
1831 switch (getAlignment(cell)) {
1832 case LYX_ALIGN_LEFT:
1835 case LYX_ALIGN_RIGHT:
1843 if (rightLine(cell))
1845 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1851 if (getUsebox(cell) == BOX_PARBOX) {
1853 switch (getVAlignment(cell)) {
1854 case LYX_VALIGN_TOP:
1857 case LYX_VALIGN_MIDDLE:
1860 case LYX_VALIGN_BOTTOM:
1864 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1865 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1866 os << "\\begin{minipage}[";
1867 switch (getVAlignment(cell)) {
1868 case LYX_VALIGN_TOP:
1871 case LYX_VALIGN_MIDDLE:
1874 case LYX_VALIGN_BOTTOM:
1878 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1885 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1890 if (getUsebox(cell) == BOX_PARBOX)
1892 else if (getUsebox(cell) == BOX_MINIPAGE) {
1893 os << "%\n\\end{minipage}";
1896 if (isMultiColumn(cell)) {
1899 if (getRotateCell(cell)) {
1900 os << "%\n\\end{sideways}";
1907 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1908 OutputParams const & runparams) const
1910 if (!is_long_tabular)
1914 // output header info
1916 if (endhead.topDL) {
1920 for (int i = 0; i < rows_; ++i) {
1921 if (row_info[i].endhead) {
1922 ret += TeXRow(os, i, buf, runparams);
1925 if (endhead.bottomDL) {
1929 os << "\\endhead\n";
1931 if (endfirsthead.empty) {
1932 os << "\\endfirsthead\n";
1936 // output firstheader info
1937 if (haveLTFirstHead()) {
1938 if (endfirsthead.topDL) {
1942 for (int i = 0; i < rows_; ++i) {
1943 if (row_info[i].endfirsthead) {
1944 ret += TeXRow(os, i, buf, runparams);
1947 if (endfirsthead.bottomDL) {
1951 os << "\\endfirsthead\n";
1954 // output footer info
1956 if (endfoot.topDL) {
1960 for (int i = 0; i < rows_; ++i) {
1961 if (row_info[i].endfoot) {
1962 ret += TeXRow(os, i, buf, runparams);
1965 if (endfoot.bottomDL) {
1969 os << "\\endfoot\n";
1971 if (endlastfoot.empty) {
1972 os << "\\endlastfoot\n";
1976 // output lastfooter info
1977 if (haveLTLastFoot()) {
1978 if (endlastfoot.topDL) {
1982 for (int i = 0; i < rows_; ++i) {
1983 if (row_info[i].endlastfoot) {
1984 ret += TeXRow(os, i, buf, runparams);
1987 if (endlastfoot.bottomDL) {
1991 os << "\\endlastfoot\n";
1998 bool LyXTabular::isValidRow(int row) const
2000 if (!is_long_tabular)
2002 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2003 !row_info[row].endfoot && !row_info[row].endlastfoot;
2007 int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
2008 OutputParams const & runparams) const
2011 int cell = getCellNumber(i, 0);
2012 BufferParams const & bufferparams = buf.params();
2014 ret += TeXTopHLine(os, i);
2015 for (int j = 0; j < columns_; ++j) {
2016 if (isPartOfMultiColumn(i, j))
2018 ret += TeXCellPreamble(os, cell);
2019 InsetText & inset = getCellInset(cell);
2021 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(bufferparams) &&
2022 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2026 ret += inset.latex(buf, os, runparams);
2030 ret += TeXCellPostamble(os, cell);
2031 if (!isLastCellInRow(cell)) { // not last cell in row
2037 os << "\\tabularnewline\n";
2039 ret += TeXBottomHLine(os, i);
2044 int LyXTabular::latex(Buffer const & buf, ostream & os,
2045 OutputParams const & runparams) const
2049 //+---------------------------------------------------------------------
2050 //+ first the opening preamble +
2051 //+---------------------------------------------------------------------
2054 os << "\\begin{sideways}\n";
2057 if (is_long_tabular)
2058 os << "\\begin{longtable}{";
2060 os << "\\begin{tabular}{";
2061 for (int i = 0; i < columns_; ++i) {
2062 if (!column_info[i].align_special.empty()) {
2063 os << column_info[i].align_special;
2065 if (column_info[i].left_line)
2067 if (!column_info[i].p_width.zero()) {
2068 switch (column_info[i].alignment) {
2069 case LYX_ALIGN_LEFT:
2070 os << ">{\\raggedright}";
2072 case LYX_ALIGN_RIGHT:
2073 os << ">{\\raggedleft}";
2075 case LYX_ALIGN_CENTER:
2076 os << ">{\\centering}";
2078 case LYX_ALIGN_NONE:
2079 case LYX_ALIGN_BLOCK:
2080 case LYX_ALIGN_LAYOUT:
2081 case LYX_ALIGN_SPECIAL:
2085 switch (column_info[i].valignment) {
2086 case LYX_VALIGN_TOP:
2089 case LYX_VALIGN_MIDDLE:
2092 case LYX_VALIGN_BOTTOM:
2097 << column_info[i].p_width.asLatexString()
2100 switch (column_info[i].alignment) {
2101 case LYX_ALIGN_LEFT:
2104 case LYX_ALIGN_RIGHT:
2112 if (column_info[i].right_line)
2119 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2121 //+---------------------------------------------------------------------
2122 //+ the single row and columns (cells) +
2123 //+---------------------------------------------------------------------
2125 for (int i = 0; i < rows_; ++i) {
2126 if (isValidRow(i)) {
2127 ret += TeXRow(os, i, buf, runparams);
2128 if (is_long_tabular && row_info[i].newpage) {
2129 os << "\\newpage\n";
2135 //+---------------------------------------------------------------------
2136 //+ the closing of the tabular +
2137 //+---------------------------------------------------------------------
2139 if (is_long_tabular)
2140 os << "\\end{longtable}";
2142 os << "\\end{tabular}";
2144 os << "\n\\end{sideways}";
2152 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2153 const OutputParams & runparams) const
2155 os << "<tabular ca=\"";
2156 for (int i = 0; i < columns_; ++i) {
2157 switch (column_info[i].alignment) {
2158 case LYX_ALIGN_LEFT:
2161 case LYX_ALIGN_RIGHT:
2172 for (int i = 0; i < rows_; ++i) {
2173 for (int j = 0; j < columns_; ++j) {
2174 if (isPartOfMultiColumn(i, j))
2176 InsetText & inset = getCellInset(cell);
2178 ret += inset.linuxdoc(buf, os, runparams);
2180 if (isLastCellInRow(cell)) {
2189 os << "</tabular>\n";
2194 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row,
2195 OutputParams const & runparams) const
2198 int cell = getFirstCellInRow(row);
2201 for (int j = 0; j < columns_; ++j) {
2202 if (isPartOfMultiColumn(row, j))
2205 os << "<entry align=\"";
2206 switch (getAlignment(cell)) {
2207 case LYX_ALIGN_LEFT:
2210 case LYX_ALIGN_RIGHT:
2218 os << "\" valign=\"";
2219 switch (getVAlignment(cell)) {
2220 case LYX_VALIGN_TOP:
2223 case LYX_VALIGN_BOTTOM:
2226 case LYX_VALIGN_MIDDLE:
2231 if (isMultiColumn(cell)) {
2232 os << " namest=\"col" << j << "\" ";
2233 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2237 OutputParams runp = runparams;
2238 runp.mixed_content = true;
2239 ret += getCellInset(cell).docbook(buf, os, runp);
2248 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2249 OutputParams const & runparams) const
2253 //+---------------------------------------------------------------------
2254 //+ first the opening preamble +
2255 //+---------------------------------------------------------------------
2257 os << "<tgroup cols=\"" << columns_
2258 << "\" colsep=\"1\" rowsep=\"1\">\n";
2260 for (int i = 0; i < columns_; ++i) {
2261 os << "<colspec colname=\"col" << i << "\" align=\"";
2262 switch (column_info[i].alignment) {
2263 case LYX_ALIGN_LEFT:
2266 case LYX_ALIGN_RIGHT:
2277 //+---------------------------------------------------------------------
2278 //+ Long Tabular case +
2279 //+---------------------------------------------------------------------
2281 // output header info
2282 if (haveLTHead() || haveLTFirstHead()) {
2285 for (int i = 0; i < rows_; ++i) {
2286 if (row_info[i].endhead || row_info[i].endfirsthead) {
2287 ret += docbookRow(buf, os, i, runparams);
2293 // output footer info
2294 if (haveLTFoot() || haveLTLastFoot()) {
2297 for (int i = 0; i < rows_; ++i) {
2298 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2299 ret += docbookRow(buf, os, i, runparams);
2306 //+---------------------------------------------------------------------
2307 //+ the single row and columns (cells) +
2308 //+---------------------------------------------------------------------
2312 for (int i = 0; i < rows_; ++i) {
2313 if (isValidRow(i)) {
2314 ret += docbookRow(buf, os, i, runparams);
2319 //+---------------------------------------------------------------------
2320 //+ the closing of the tabular +
2321 //+---------------------------------------------------------------------
2330 int LyXTabular::asciiTopHLine(ostream & os, int row,
2331 vector<unsigned int> const & clen) const
2333 int const fcell = getFirstCellInRow(row);
2334 int const n = numberOfCellsInRow(fcell) + fcell;
2337 for (int i = fcell; i < n; ++i) {
2347 for (int i = fcell; i < n; ++i) {
2358 int column = column_of_cell(i);
2359 int len = clen[column];
2360 while (isPartOfMultiColumn(row, ++column))
2361 len += clen[column] + 4;
2362 os << string(len, ch);
2377 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2378 vector<unsigned int> const & clen) const
2380 int const fcell = getFirstCellInRow(row);
2381 int const n = numberOfCellsInRow(fcell) + fcell;
2384 for (int i = fcell; i < n; ++i) {
2385 if (bottomLine(i)) {
2394 for (int i = fcell; i < n; ++i) {
2395 if (bottomLine(i)) {
2405 int column = column_of_cell(i);
2406 int len = clen[column];
2407 while (isPartOfMultiColumn(row, ++column))
2408 len += clen[column] + 4;
2409 os << string(len, ch);
2410 if (bottomLine(i)) {
2424 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2425 OutputParams const & runparams,
2426 int cell, int row, int column,
2427 vector<unsigned int> const & clen,
2428 bool onlydata) const
2431 int ret = getCellInset(cell).plaintext(buf, sstr, runparams);
2443 unsigned int len1 = sstr.str().length();
2444 unsigned int len2 = clen[column];
2445 while (isPartOfMultiColumn(row, ++column))
2446 len2 += clen[column] + 4;
2449 switch (getAlignment(cell)) {
2451 case LYX_ALIGN_LEFT:
2454 case LYX_ALIGN_RIGHT:
2458 case LYX_ALIGN_CENTER:
2464 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2466 if (rightLine(cell))
2475 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2476 OutputParams const & runparams,
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).plaintext(buf, sstr, runparams);
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).plaintext(buf, sstr, runparams);
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, runparams,
2527 cell, i, j, clen, onlydata);
2532 for (int j = 0; j < depth; ++j)
2534 if (asciiBottomHLine(os, i, clen))
2535 for (int j = 0; j < depth; ++j)
2543 InsetText & LyXTabular::getCellInset(int cell) const
2545 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2549 InsetText & LyXTabular::getCellInset(int row, int column) const
2551 return cell_info[row][column].inset;
2555 int LyXTabular::getCellFromInset(InsetOld const * inset) const
2557 // is this inset part of the tabular?
2559 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2563 for (int cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2564 if (&getCellInset(cell) == inset) {
2565 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2566 << "cell=" << cell << endl;
2570 // We should have found a cell at this point
2571 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2572 << inset << " not found!" << endl;
2577 void LyXTabular::validate(LaTeXFeatures & features) const
2579 features.require("NeedTabularnewline");
2580 if (isLongTabular())
2581 features.require("longtable");
2583 features.require("rotating");
2584 for (int cell = 0; cell < numberofcells; ++cell) {
2585 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2586 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2587 features.require("array");
2588 getCellInset(cell).validate(features);
2593 void LyXTabular::getLabelList(Buffer const & buffer,
2594 std::vector<string> & list) const
2596 for (int i = 0; i < rows_; ++i)
2597 for (int j = 0; j < columns_; ++j)
2598 getCellInset(i, j).getLabelList(buffer, list);
2602 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2604 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2605 ParagraphList::const_iterator cit = parlist.begin();
2606 ParagraphList::const_iterator end = parlist.end();
2608 for (; cit != end; ++cit)
2609 for (int i = 0; i < cit->size(); ++i)
2610 if (cit->isNewline(i))