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"
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 return getTokenValue(str, token, tmp) && string2type(tmp, num);
248 bool getTokenValue(string const & str, const char * token,
249 LyXTabular::VAlignment & num)
252 return getTokenValue(str, token, tmp) && string2type(tmp, num);
256 bool getTokenValue(string const & str, const char * token,
257 LyXTabular::BoxType & num)
260 return getTokenValue(str, token, tmp) && string2type(tmp, num);
264 bool getTokenValue(string const & str, const char * token, bool & flag)
266 // set the flag always to false as this should be the default for bools
267 // not in the file-format.
270 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
274 bool getTokenValue(string const & str, const char * token, LyXLength & len)
276 // set the lenght to be zero() as default as this it should be if not
277 // in the file format.
280 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
284 void l_getline(istream & is, string & str)
287 while (str.empty()) {
289 if (!str.empty() && str[str.length() - 1] == '\r')
290 str.erase(str.length() - 1);
296 /// Define a few methods for the inner structs
298 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
303 multicolumn = LyXTabular::CELL_NORMAL;
304 alignment = LYX_ALIGN_CENTER;
305 valignment = LYX_VALIGN_TOP;
315 LyXTabular::rowstruct::rowstruct()
322 endfirsthead = false;
329 LyXTabular::columnstruct::columnstruct()
333 alignment = LYX_ALIGN_CENTER;
334 valignment = LYX_VALIGN_TOP;
339 LyXTabular::ltType::ltType()
347 LyXTabular::LyXTabular(BufferParams const & bp, int rows_arg, int columns_arg)
349 init(bp, rows_arg, columns_arg);
353 // activates all lines and sets all widths to 0
354 void LyXTabular::init(BufferParams const & bp, int rows_arg, int columns_arg)
357 columns_ = columns_arg;
358 row_info = row_vector(rows_);
359 column_info = column_vector(columns_);
360 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
361 row_info.reserve(10);
362 column_info.reserve(10);
363 cell_info.reserve(100);
365 for (int i = 0; i < rows_; ++i)
366 cell_info[i].back().right_line = true;
367 row_info.back().bottom_line = true;
368 row_info.front().bottom_line = true;
369 column_info.back().right_line = true;
370 is_long_tabular = false;
375 void LyXTabular::fixCellNums()
378 for (int i = 0; i < rows_; ++i) {
379 for (int j = 0; j < columns_; ++j) {
380 // When debugging it can be nice to set
382 cell_info[i][j].inset.setDrawFrame(false);
383 cell_info[i][j].cellno = cellno++;
385 cell_info[i].back().right_line = true;
388 set_row_column_number_info();
392 void LyXTabular::appendRow(BufferParams const & bp, int cell)
396 int row = row_of_cell(cell);
398 row_vector::iterator rit = row_info.begin() + row;
399 row_info.insert(rit, rowstruct());
400 // now set the values of the row before
401 row_info[row] = row_info[row + 1];
403 cell_vvector old(rows_ - 1);
404 for (int i = 0; i < rows_ - 1; ++i)
405 swap(cell_info[i], old[i]);
407 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
409 for (int i = 0; i <= row; ++i)
410 swap(cell_info[i], old[i]);
411 for (int i = row + 2; i < rows_; ++i)
412 swap(cell_info[i], old[i - 1]);
414 if (bp.tracking_changes)
415 for (int j = 0; j < columns_; ++j)
416 cell_info[row + 1][j].inset.markNew(true);
418 set_row_column_number_info();
422 void LyXTabular::deleteRow(int row)
424 // Not allowed to delete last row
428 row_info.erase(row_info.begin() + row);
429 cell_info.erase(cell_info.begin() + row);
435 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
439 int const column = column_of_cell(cell);
440 column_vector::iterator cit = column_info.begin() + column + 1;
441 column_info.insert(cit, columnstruct());
442 // set the column values of the column before
443 column_info[column + 1] = column_info[column];
445 for (int i = 0; i < rows_; ++i) {
446 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
448 // care about multicolumns
449 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
450 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
452 if (column + 2 >= columns_
453 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
454 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
457 for (int i = 0; i < rows_; ++i) {
458 cell_info[i][column + 1].inset.clear(false);
459 if (bp.tracking_changes)
460 cell_info[i][column + 1].inset.markNew(true);
466 void LyXTabular::deleteColumn(int column)
468 // Not allowed to delete last column
472 column_info.erase(column_info.begin() + column);
473 for (int i = 0; i < rows_; ++i)
474 cell_info[i].erase(cell_info[i].begin() + column);
480 void LyXTabular::set_row_column_number_info()
483 for (int row = 0; row < rows_; ++row) {
484 for (int column = 0; column < columns_; ++column) {
485 if (cell_info[row][column].multicolumn
486 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
488 cell_info[row][column].cellno = numberofcells;
491 ++numberofcells; // because this is one more than as we start from 0
493 rowofcell.resize(numberofcells);
494 columnofcell.resize(numberofcells);
496 for (int row = 0, column = 0, c = 0;
497 c < numberofcells && row < rows_ && column < columns_;) {
499 columnofcell[c] = column;
503 } while (column < columns_ &&
504 cell_info[row][column].multicolumn
505 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
507 if (column == columns_) {
513 for (int row = 0; row < rows_; ++row) {
514 for (int column = 0; column < columns_; ++column) {
515 if (isPartOfMultiColumn(row,column))
517 cell_info[row][column].inset.setAutoBreakRows(
518 !getPWidth(getCellNumber(row, column)).zero());
524 int LyXTabular::getNumberOfCells() const
526 return numberofcells;
530 int LyXTabular::numberOfCellsInRow(int cell) const
532 int const row = row_of_cell(cell);
534 for (int i = 0; i < columns_; ++i)
535 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
541 // returns 1 if there is a topline, returns 0 if not
542 bool LyXTabular::topLine(int cell, bool onlycolumn) const
544 if (!onlycolumn && isMultiColumn(cell))
545 return cellinfo_of_cell(cell).top_line;
546 return row_info[row_of_cell(cell)].top_line;
550 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
552 if (!onlycolumn && isMultiColumn(cell))
553 return cellinfo_of_cell(cell).bottom_line;
554 return row_info[row_of_cell(cell)].bottom_line;
558 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
560 if (!onlycolumn && isMultiColumn(cell) &&
561 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
563 if (cellinfo_of_cell(cell).align_special.empty())
564 return cellinfo_of_cell(cell).left_line;
565 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
567 if (column_info[column_of_cell(cell)].align_special.empty())
568 return column_info[column_of_cell(cell)].left_line;
569 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
573 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
575 if (!onlycolumn && isMultiColumn(cell) &&
576 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
578 if (cellinfo_of_cell(cell).align_special.empty())
579 return cellinfo_of_cell(cell).right_line;
580 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
582 if (column_info[column_of_cell(cell)].align_special.empty())
583 return column_info[right_column_of_cell(cell)].right_line;
584 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
588 bool LyXTabular::topAlreadyDrawn(int cell) const
590 int row = row_of_cell(cell);
591 if (row > 0 && !getAdditionalHeight(row)) {
592 int column = column_of_cell(cell);
595 && cell_info[row][column].multicolumn
596 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
598 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
599 return row_info[row].bottom_line;
601 return cell_info[row][column].bottom_line;
607 bool LyXTabular::leftAlreadyDrawn(int cell) const
609 int column = column_of_cell(cell);
611 int row = row_of_cell(cell);
613 (cell_info[row][column].multicolumn ==
614 LyXTabular::CELL_PART_OF_MULTICOLUMN));
615 if (getAdditionalWidth(cell_info[row][column].cellno))
617 return rightLine(cell_info[row][column].cellno);
623 bool LyXTabular::isLastRow(int cell) const
625 return row_of_cell(cell) == rows_ - 1;
629 int LyXTabular::getAdditionalHeight(int row) const
631 if (!row || row >= rows_)
637 for (int column = 0; column < columns_ && bottom; ++column) {
638 switch (cell_info[row - 1][column].multicolumn) {
639 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
640 bottom = cell_info[row - 1][column].bottom_line;
642 case LyXTabular::CELL_NORMAL:
643 bottom = row_info[row - 1].bottom_line;
646 for (int column = 0; column < columns_ && top; ++column) {
647 switch (cell_info[row][column].multicolumn) {
648 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
649 top = cell_info[row][column].top_line;
651 case LyXTabular::CELL_NORMAL:
652 top = row_info[row].top_line;
656 return WIDTH_OF_LINE;
661 int LyXTabular::getAdditionalWidth(int cell) const
663 // internally already set in setWidthOfCell
664 // used to get it back in text.C
665 int const col = right_column_of_cell(cell);
666 int const row = row_of_cell(cell);
667 if (col < columns_ - 1 && rightLine(cell) &&
668 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
670 return WIDTH_OF_LINE;
676 // returns the maximum over all rows
677 int LyXTabular::getWidthOfColumn(int cell) const
679 int const column1 = column_of_cell(cell);
680 int const column2 = right_column_of_cell(cell);
682 for (int i = column1; i <= column2; ++i)
683 result += column_info[i].width_of_column;
688 int LyXTabular::getWidthOfTabular() const
690 return width_of_tabular;
694 // returns true if a complete update is necessary, otherwise false
695 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
697 if (!isMultiColumn(cell))
700 int const row = row_of_cell(cell);
701 int const column1 = column_of_cell(cell);
702 int const column2 = right_column_of_cell(cell);
703 int const old_val = cell_info[row][column2].width_of_cell;
705 // first set columns to 0 so we can calculate the right width
706 for (int i = column1; i <= column2; ++i) {
707 cell_info[row][i].width_of_cell = 0;
709 // set the width to MAX_WIDTH until width > 0
710 int width = new_width + 2 * WIDTH_OF_LINE;
712 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
713 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
714 width -= column_info[i].width_of_column;
717 cell_info[row][i].width_of_cell = width;
719 if (old_val != cell_info[row][column2].width_of_cell) {
720 // in this case we have to recalculate all multicolumn cells which
721 // have this column as one of theirs but not as last one
722 calculate_width_of_column_NMC(i);
723 recalculateMulticolumnsOfColumn(i);
724 calculate_width_of_column(i);
730 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
732 // the last column does not have to be recalculated because all
733 // multicolumns will have here there last multicolumn cell which
734 // always will have the whole rest of the width of the cell.
735 if (column > (columns_ - 2))
737 for(int row = 0; row < rows_; ++row) {
738 int mc = cell_info[row][column].multicolumn;
739 int nmc = cell_info[row][column+1].multicolumn;
740 // we only have to update multicolumns which do not have this
741 // column as their last column!
742 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
743 (mc == CELL_PART_OF_MULTICOLUMN &&
744 nmc == CELL_PART_OF_MULTICOLUMN))
746 int const cellno = cell_info[row][column].cellno;
747 setWidthOfMulticolCell(cellno,
748 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
754 // returns 1 if a complete update is necessary, otherwise 0
755 void LyXTabular::setWidthOfCell(int cell, int new_width)
757 int const row = row_of_cell(cell);
758 int const column1 = column_of_cell(cell);
763 if (rightLine(cell_info[row][column1].cellno, true) &&
764 column1 < columns_ - 1 &&
765 leftLine(cell_info[row][column1+1].cellno, true))
767 add_width = WIDTH_OF_LINE;
770 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
773 if (isMultiColumnReal(cell)) {
774 tmp = setWidthOfMulticolCell(cell, new_width);
776 width = new_width + 2 * WIDTH_OF_LINE + add_width;
777 cell_info[row][column1].width_of_cell = width;
778 tmp = calculate_width_of_column_NMC(column1);
780 recalculateMulticolumnsOfColumn(column1);
783 for (int i = 0; i < columns_; ++i)
784 calculate_width_of_column(i);
785 calculate_width_of_tabular();
790 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
792 if (!isMultiColumn(cell) || onlycolumn)
793 column_info[column_of_cell(cell)].alignment = align;
795 cellinfo_of_cell(cell).alignment = align;
799 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
801 if (!isMultiColumn(cell) || onlycolumn)
802 column_info[column_of_cell(cell)].valignment = align;
804 cellinfo_of_cell(cell).valignment = align;
808 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
810 int const j = column_of_cell(cell);
812 column_info[j].p_width = width;
813 for (int i = 0; i < rows_; ++i) {
814 int const cell = getCellNumber(i, j);
815 // because of multicolumns
816 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
821 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
823 if (!isMultiColumn(cell))
826 cellinfo_of_cell(cell).p_width = width;
827 getCellInset(cell).setAutoBreakRows(!width.zero());
832 void LyXTabular::setAlignSpecial(int cell, string const & special,
833 LyXTabular::Feature what)
835 if (what == SET_SPECIAL_MULTI)
836 cellinfo_of_cell(cell).align_special = special;
838 column_info[column_of_cell(cell)].align_special = special;
842 void LyXTabular::setAllLines(int cell, bool line)
844 setTopLine(cell, line);
845 setBottomLine(cell, line);
846 setRightLine(cell, line);
847 setLeftLine(cell, line);
851 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
853 int const row = row_of_cell(cell);
854 if (onlycolumn || !isMultiColumn(cell))
855 row_info[row].top_line = line;
857 cellinfo_of_cell(cell).top_line = line;
861 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
863 if (onlycolumn || !isMultiColumn(cell))
864 row_info[row_of_cell(cell)].bottom_line = line;
866 cellinfo_of_cell(cell).bottom_line = line;
870 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
872 if (onlycolumn || !isMultiColumn(cell))
873 column_info[column_of_cell(cell)].left_line = line;
875 cellinfo_of_cell(cell).left_line = line;
879 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
881 if (onlycolumn || !isMultiColumn(cell))
882 column_info[right_column_of_cell(cell)].right_line = line;
884 cellinfo_of_cell(cell).right_line = line;
888 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
890 if (!onlycolumn && isMultiColumn(cell))
891 return cellinfo_of_cell(cell).alignment;
892 return column_info[column_of_cell(cell)].alignment;
896 LyXTabular::VAlignment
897 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
899 if (!onlycolumn && isMultiColumn(cell))
900 return cellinfo_of_cell(cell).valignment;
901 return column_info[column_of_cell(cell)].valignment;
905 LyXLength const LyXTabular::getPWidth(int cell) const
907 if (isMultiColumn(cell))
908 return cellinfo_of_cell(cell).p_width;
909 return column_info[column_of_cell(cell)].p_width;
913 LyXLength const LyXTabular::getColumnPWidth(int cell) const
915 return column_info[column_of_cell(cell)].p_width;
919 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
921 if (isMultiColumn(cell))
922 return cellinfo_of_cell(cell).p_width;
927 string const LyXTabular::getAlignSpecial(int cell, int what) const
929 if (what == SET_SPECIAL_MULTI)
930 return cellinfo_of_cell(cell).align_special;
931 return column_info[column_of_cell(cell)].align_special;
935 int LyXTabular::getWidthOfCell(int cell) const
937 int const row = row_of_cell(cell);
938 int const column1 = column_of_cell(cell);
939 int const column2 = right_column_of_cell(cell);
941 for (int i = column1; i <= column2; ++i)
942 result += cell_info[row][i].width_of_cell;
947 int LyXTabular::getBeginningOfTextInCell(int cell) const
951 switch (getAlignment(cell)) {
952 case LYX_ALIGN_CENTER:
953 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
955 case LYX_ALIGN_RIGHT:
956 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
957 // + getAdditionalWidth(cell);
960 // LYX_ALIGN_LEFT: nothing :-)
970 bool LyXTabular::isFirstCellInRow(int cell) const
972 return column_of_cell(cell) == 0;
976 int LyXTabular::getFirstCellInRow(int row) const
980 return cell_info[row][0].cellno;
984 bool LyXTabular::isLastCellInRow(int cell) const
986 return right_column_of_cell(cell) == columns_ - 1;
990 int LyXTabular::getLastCellInRow(int row) const
994 return cell_info[row][columns_-1].cellno;
998 void LyXTabular::calculate_width_of_column(int column)
1001 for (int i = 0; i < rows_; ++i)
1002 maximum = max(cell_info[i][column].width_of_cell, maximum);
1003 column_info[column].width_of_column = maximum;
1008 // Calculate the columns regarding ONLY the normal cells and if this
1009 // column is inside a multicolumn cell then use it only if its the last
1010 // column of this multicolumn cell as this gives an added width to the
1011 // column, all the rest should be adapted!
1013 bool LyXTabular::calculate_width_of_column_NMC(int column)
1015 int const old_column_width = column_info[column].width_of_column;
1017 for (int i = 0; i < rows_; ++i) {
1018 int cell = getCellNumber(i, column);
1019 bool ismulti = isMultiColumnReal(cell);
1020 if ((!ismulti || column == right_column_of_cell(cell)) &&
1021 cell_info[i][column].width_of_cell > max)
1023 max = cell_info[i][column].width_of_cell;
1026 column_info[column].width_of_column = max;
1027 return column_info[column].width_of_column != old_column_width;
1031 void LyXTabular::calculate_width_of_tabular()
1033 width_of_tabular = 0;
1034 for (int i = 0; i < columns_; ++i)
1035 width_of_tabular += column_info[i].width_of_column;
1039 int LyXTabular::row_of_cell(int cell) const
1041 if (cell >= numberofcells)
1045 return rowofcell[cell];
1049 int LyXTabular::column_of_cell(int cell) const
1051 if (cell >= numberofcells)
1052 return columns_ - 1;
1055 return columnofcell[cell];
1059 int LyXTabular::right_column_of_cell(int cell) const
1061 int const row = row_of_cell(cell);
1062 int column = column_of_cell(cell);
1063 while (column < columns_ - 1 &&
1064 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1070 void LyXTabular::write(Buffer const & buf, ostream & os) const
1074 << write_attribute("version", 3)
1075 << write_attribute("rows", rows_)
1076 << write_attribute("columns", columns_)
1078 // global longtable options
1080 << write_attribute("rotate", rotate)
1081 << write_attribute("islongtable", is_long_tabular)
1082 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1083 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1084 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1085 << write_attribute("headTopDL", endhead.topDL)
1086 << write_attribute("headBottomDL", endhead.bottomDL)
1087 << write_attribute("footTopDL", endfoot.topDL)
1088 << write_attribute("footBottomDL", endfoot.bottomDL)
1089 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1090 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1091 << write_attribute("lastFootEmpty", endlastfoot.empty)
1093 for (int j = 0; j < columns_; ++j) {
1095 << write_attribute("alignment", column_info[j].alignment)
1096 << write_attribute("valignment", column_info[j].valignment)
1097 << write_attribute("leftline", column_info[j].left_line)
1098 << write_attribute("rightline", column_info[j].right_line)
1099 << write_attribute("width", column_info[j].p_width.asString())
1100 << write_attribute("special", column_info[j].align_special)
1103 for (int i = 0; i < rows_; ++i) {
1105 << write_attribute("topline", row_info[i].top_line)
1106 << write_attribute("bottomline", row_info[i].bottom_line)
1107 << write_attribute("endhead", row_info[i].endhead)
1108 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1109 << write_attribute("endfoot", row_info[i].endfoot)
1110 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1111 << write_attribute("newpage", row_info[i].newpage)
1113 for (int j = 0; j < columns_; ++j) {
1115 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1116 << write_attribute("alignment", cell_info[i][j].alignment)
1117 << write_attribute("valignment", cell_info[i][j].valignment)
1118 << write_attribute("topline", cell_info[i][j].top_line)
1119 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1120 << write_attribute("leftline", cell_info[i][j].left_line)
1121 << write_attribute("rightline", cell_info[i][j].right_line)
1122 << write_attribute("rotate", cell_info[i][j].rotate)
1123 << write_attribute("usebox", cell_info[i][j].usebox)
1124 << write_attribute("width", cell_info[i][j].p_width)
1125 << write_attribute("special", cell_info[i][j].align_special)
1127 os << "\\begin_inset ";
1128 cell_info[i][j].inset.write(buf, os);
1129 os << "\n\\end_inset \n"
1134 os << "</lyxtabular>\n";
1138 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1142 row_info[--hr].endhead = true;
1144 // set firstheader info
1145 if (fhr && fhr < rows_) {
1146 if (row_info[fhr].endhead) {
1148 row_info[--fhr].endfirsthead = true;
1149 row_info[fhr].endhead = false;
1151 } else if (row_info[fhr - 1].endhead) {
1152 endfirsthead.empty = true;
1154 while (fhr > 0 && !row_info[--fhr].endhead) {
1155 row_info[fhr].endfirsthead = true;
1160 if (fr && fr < rows_) {
1161 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1162 while (fr > 0 && !row_info[--fr].endhead) {
1163 row_info[fr].endfoot = true;
1164 row_info[fr].endhead = false;
1166 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1167 while (fr > 0 && !row_info[--fr].endfirsthead) {
1168 row_info[fr].endfoot = true;
1169 row_info[fr].endfirsthead = false;
1171 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1172 while (fr > 0 && !row_info[--fr].endhead &&
1173 !row_info[fr].endfirsthead)
1175 row_info[fr].endfoot = true;
1179 // set lastfooter info
1180 if (lfr && lfr < rows_) {
1181 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1182 while (lfr > 0 && !row_info[--lfr].endhead) {
1183 row_info[lfr].endlastfoot = true;
1184 row_info[lfr].endhead = false;
1186 } else if (row_info[lfr].endfirsthead &&
1187 row_info[lfr - 1].endfirsthead)
1189 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1190 row_info[lfr].endlastfoot = true;
1191 row_info[lfr].endfirsthead = false;
1193 } else if (row_info[lfr].endfoot
1194 && row_info[lfr - 1].endfoot) {
1195 while (lfr > 0 && !row_info[--lfr].endfoot) {
1196 row_info[lfr].endlastfoot = true;
1197 row_info[lfr].endfoot = false;
1199 } else if (!row_info[fr - 1].endhead
1200 && !row_info[fr - 1].endfirsthead &&
1201 !row_info[fr - 1].endfoot)
1204 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1205 !row_info[lfr].endfoot)
1207 row_info[lfr].endlastfoot = true;
1209 } else if (haveLTFoot()) {
1210 endlastfoot.empty = true;
1216 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1219 istream & is = lex.getStream();
1221 l_getline(is, line);
1222 if (!prefixIs(line, "<lyxtabular ")
1223 && !prefixIs(line, "<LyXTabular ")) {
1224 BOOST_ASSERT(false);
1229 if (!getTokenValue(line, "version", version))
1231 BOOST_ASSERT(version >= 2);
1234 if (!getTokenValue(line, "rows", rows_arg))
1237 if (!getTokenValue(line, "columns", columns_arg))
1239 init(buf.params(), rows_arg, columns_arg);
1240 l_getline(is, line);
1241 if (!prefixIs(line, "<features")) {
1242 lyxerr << "Wrong tabular format (expected <features ...> got"
1243 << line << ')' << endl;
1246 getTokenValue(line, "rotate", rotate);
1247 getTokenValue(line, "islongtable", is_long_tabular);
1248 // compatibility read for old longtable options. Now we can make any
1249 // row part of the header/footer type we want before it was strict
1250 // sequential from the first row down (as LaTeX does it!). So now when
1251 // we find a header/footer line we have to go up the rows and set it
1252 // on all preceding rows till the first or one with already a h/f option
1253 // set. If we find a firstheader on the same line as a header or a
1254 // lastfooter on the same line as a footer then this should be set empty.
1262 getTokenValue(line, "endhead", hrow);
1263 getTokenValue(line, "endfirsthead", fhrow);
1264 getTokenValue(line, "endfoot", frow);
1265 getTokenValue(line, "endlastfoot", lfrow);
1266 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1268 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1269 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1270 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1271 getTokenValue(line, "headTopDL", endhead.topDL);
1272 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1273 getTokenValue(line, "footTopDL", endfoot.topDL);
1274 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1275 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1276 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1277 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1279 for (int j = 0; j < columns_; ++j) {
1281 if (!prefixIs(line,"<column")) {
1282 lyxerr << "Wrong tabular format (expected <column ...> got"
1283 << line << ')' << endl;
1286 getTokenValue(line, "alignment", column_info[j].alignment);
1287 getTokenValue(line, "valignment", column_info[j].valignment);
1288 getTokenValue(line, "leftline", column_info[j].left_line);
1289 getTokenValue(line, "rightline", column_info[j].right_line);
1290 getTokenValue(line, "width", column_info[j].p_width);
1291 getTokenValue(line, "special", column_info[j].align_special);
1294 for (int i = 0; i < rows_; ++i) {
1295 l_getline(is, line);
1296 if (!prefixIs(line, "<row")) {
1297 lyxerr << "Wrong tabular format (expected <row ...> got"
1298 << line << ')' << endl;
1301 getTokenValue(line, "topline", row_info[i].top_line);
1302 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1303 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1304 getTokenValue(line, "endhead", row_info[i].endhead);
1305 getTokenValue(line, "endfoot", row_info[i].endfoot);
1306 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1307 getTokenValue(line, "newpage", row_info[i].newpage);
1308 for (int j = 0; j < columns_; ++j) {
1309 l_getline(is, line);
1310 if (!prefixIs(line, "<cell")) {
1311 lyxerr << "Wrong tabular format (expected <cell ...> got"
1312 << line << ')' << endl;
1315 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1316 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1317 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1318 getTokenValue(line, "topline", cell_info[i][j].top_line);
1319 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1320 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1321 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1322 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1323 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1324 getTokenValue(line, "width", cell_info[i][j].p_width);
1325 getTokenValue(line, "special", cell_info[i][j].align_special);
1326 l_getline(is, line);
1327 if (prefixIs(line, "\\begin_inset")) {
1328 cell_info[i][j].inset.read(buf, lex);
1329 l_getline(is, line);
1331 if (!prefixIs(line, "</cell>")) {
1332 lyxerr << "Wrong tabular format (expected </cell> got"
1333 << line << ')' << endl;
1337 l_getline(is, line);
1338 if (!prefixIs(line, "</row>")) {
1339 lyxerr << "Wrong tabular format (expected </row> got"
1340 << line << ')' << endl;
1344 while (!prefixIs(line, "</lyxtabular>")) {
1345 l_getline(is, line);
1347 set_row_column_number_info();
1351 bool LyXTabular::isMultiColumn(int cell) const
1353 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1357 bool LyXTabular::isMultiColumnReal(int cell) const
1359 return column_of_cell(cell) != right_column_of_cell(cell) &&
1360 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1364 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1366 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1370 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1372 cellstruct & cs = cellinfo_of_cell(cell);
1373 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1374 cs.alignment = column_info[column_of_cell(cell)].alignment;
1375 cs.top_line = row_info[row_of_cell(cell)].top_line;
1376 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1377 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1378 for (int i = 1; i < number; ++i) {
1379 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1380 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1381 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs());
1382 cs1.inset.clear(false);
1384 set_row_column_number_info();
1388 int LyXTabular::cells_in_multicolumn(int cell) const
1390 int const row = row_of_cell(cell);
1391 int column = column_of_cell(cell);
1394 while (column < columns_ &&
1395 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1404 int LyXTabular::unsetMultiColumn(int cell)
1406 int const row = row_of_cell(cell);
1407 int column = column_of_cell(cell);
1411 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1412 cell_info[row][column].multicolumn = CELL_NORMAL;
1414 while (column < columns_ &&
1415 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1417 cell_info[row][column].multicolumn = CELL_NORMAL;
1422 set_row_column_number_info();
1427 void LyXTabular::setLongTabular(bool what)
1429 is_long_tabular = what;
1433 bool LyXTabular::isLongTabular() const
1435 return is_long_tabular;
1439 void LyXTabular::setRotateTabular(bool flag)
1445 bool LyXTabular::getRotateTabular() const
1451 void LyXTabular::setRotateCell(int cell, bool flag)
1453 cellinfo_of_cell(cell).rotate = flag;
1457 bool LyXTabular::getRotateCell(int cell) const
1459 return cellinfo_of_cell(cell).rotate;
1463 bool LyXTabular::needRotating() const
1467 for (int i = 0; i < rows_; ++i)
1468 for (int j = 0; j < columns_; ++j)
1469 if (cell_info[i][j].rotate)
1475 bool LyXTabular::isLastCell(int cell) const
1477 if (cell + 1 < numberofcells)
1483 int LyXTabular::getCellAbove(int cell) const
1485 if (row_of_cell(cell) > 0)
1486 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1491 int LyXTabular::getCellBelow(int cell) const
1493 if (row_of_cell(cell) + 1 < rows_)
1494 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1499 int LyXTabular::getLastCellAbove(int cell) const
1501 if (row_of_cell(cell) <= 0)
1503 if (!isMultiColumn(cell))
1504 return getCellAbove(cell);
1505 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1509 int LyXTabular::getLastCellBelow(int cell) const
1511 if (row_of_cell(cell) + 1 >= rows_)
1513 if (!isMultiColumn(cell))
1514 return getCellBelow(cell);
1515 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1519 int LyXTabular::getCellNumber(int row, int column) const
1521 BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1522 return cell_info[row][column].cellno;
1526 void LyXTabular::setUsebox(int cell, BoxType type)
1528 cellinfo_of_cell(cell).usebox = type;
1532 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1534 if (column_info[column_of_cell(cell)].p_width.zero() &&
1535 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1537 if (cellinfo_of_cell(cell).usebox > 1)
1538 return cellinfo_of_cell(cell).usebox;
1539 return useParbox(cell);
1544 // This are functions used for the longtable support
1546 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1551 row_info[row].endfirsthead = flag;
1555 row_info[row].endhead = flag;
1560 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1563 hd.set = haveLTHead();
1564 return row_info[row].endhead;
1568 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1571 hd.set = haveLTFirstHead();
1572 return row_info[row].endfirsthead;
1576 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1581 row_info[row].endlastfoot = flag;
1585 row_info[row].endfoot = flag;
1590 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1593 fd.set = haveLTFoot();
1594 return row_info[row].endfoot;
1598 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1601 fd.set = haveLTLastFoot();
1602 return row_info[row].endlastfoot;
1606 void LyXTabular::setLTNewPage(int row, bool what)
1608 row_info[row].newpage = what;
1612 bool LyXTabular::getLTNewPage(int row) const
1614 return row_info[row].newpage;
1618 bool LyXTabular::haveLTHead() const
1620 for (int i = 0; i < rows_; ++i)
1621 if (row_info[i].endhead)
1627 bool LyXTabular::haveLTFirstHead() const
1629 if (endfirsthead.empty)
1631 for (int i = 0; i < rows_; ++i)
1632 if (row_info[i].endfirsthead)
1638 bool LyXTabular::haveLTFoot() const
1640 for (int i = 0; i < rows_; ++i)
1641 if (row_info[i].endfoot)
1647 bool LyXTabular::haveLTLastFoot() const
1649 if (endlastfoot.empty)
1651 for (int i = 0; i < rows_; ++i)
1652 if (row_info[i].endlastfoot)
1658 // end longtable support functions
1660 void LyXTabular::setAscentOfRow(int row, int height)
1662 if (row >= rows_ || row_info[row].ascent_of_row == height)
1664 row_info[row].ascent_of_row = height;
1668 void LyXTabular::setDescentOfRow(int row, int height)
1670 if (row >= rows_ || row_info[row].descent_of_row == height)
1672 row_info[row].descent_of_row = height;
1676 int LyXTabular::getAscentOfRow(int row) const
1680 return row_info[row].ascent_of_row;
1684 int LyXTabular::getDescentOfRow(int row) const
1686 BOOST_ASSERT(row < rows_);
1687 return row_info[row].descent_of_row;
1691 int LyXTabular::getHeightOfTabular() const
1694 for (int row = 0; row < rows_; ++row)
1695 height += getAscentOfRow(row) + getDescentOfRow(row) +
1696 getAdditionalHeight(row);
1701 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1703 BOOST_ASSERT(row < rows_);
1704 BOOST_ASSERT(column < columns_);
1705 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1709 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1711 BOOST_ASSERT(row >= 0);
1712 BOOST_ASSERT(row < rows_);
1714 int const fcell = getFirstCellInRow(row);
1715 int const n = numberOfCellsInRow(fcell) + fcell;
1718 for (int i = fcell; i < n; ++i) {
1722 if (tmp == n - fcell) {
1725 for (int i = fcell; i < n; ++i) {
1728 << column_of_cell(i) + 1
1730 << right_column_of_cell(i) + 1
1742 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1744 if (row < 0 || row >= rows_)
1747 int const fcell = getFirstCellInRow(row);
1748 int const n = numberOfCellsInRow(fcell) + fcell;
1751 for (int i = fcell; i < n; ++i) {
1755 if (tmp == n - fcell) {
1758 for (int i = fcell; i < n; ++i) {
1759 if (bottomLine(i)) {
1761 << column_of_cell(i) + 1
1763 << right_column_of_cell(i) + 1
1775 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1779 if (getRotateCell(cell)) {
1780 os << "\\begin{sideways}\n";
1783 if (isMultiColumn(cell)) {
1784 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1785 if (!cellinfo_of_cell(cell).align_special.empty()) {
1786 os << cellinfo_of_cell(cell).align_special << "}{";
1788 if (leftLine(cell) &&
1789 (isFirstCellInRow(cell) ||
1790 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1791 !rightLine(cell - 1, true))))
1795 if (!getPWidth(cell).zero()) {
1796 switch (getVAlignment(cell)) {
1797 case LYX_VALIGN_TOP:
1800 case LYX_VALIGN_MIDDLE:
1803 case LYX_VALIGN_BOTTOM:
1808 << getPWidth(cell).asLatexString()
1811 switch (getAlignment(cell)) {
1812 case LYX_ALIGN_LEFT:
1815 case LYX_ALIGN_RIGHT:
1823 if (rightLine(cell))
1825 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1831 if (getUsebox(cell) == BOX_PARBOX) {
1833 switch (getVAlignment(cell)) {
1834 case LYX_VALIGN_TOP:
1837 case LYX_VALIGN_MIDDLE:
1840 case LYX_VALIGN_BOTTOM:
1844 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1845 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1846 os << "\\begin{minipage}[";
1847 switch (getVAlignment(cell)) {
1848 case LYX_VALIGN_TOP:
1851 case LYX_VALIGN_MIDDLE:
1854 case LYX_VALIGN_BOTTOM:
1858 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1865 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1870 if (getUsebox(cell) == BOX_PARBOX)
1872 else if (getUsebox(cell) == BOX_MINIPAGE) {
1873 os << "%\n\\end{minipage}";
1876 if (isMultiColumn(cell)) {
1879 if (getRotateCell(cell)) {
1880 os << "%\n\\end{sideways}";
1887 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1888 OutputParams const & runparams) const
1890 if (!is_long_tabular)
1894 // output header info
1896 if (endhead.topDL) {
1900 for (int i = 0; i < rows_; ++i) {
1901 if (row_info[i].endhead) {
1902 ret += TeXRow(os, i, buf, runparams);
1905 if (endhead.bottomDL) {
1909 os << "\\endhead\n";
1911 if (endfirsthead.empty) {
1912 os << "\\endfirsthead\n";
1916 // output firstheader info
1917 if (haveLTFirstHead()) {
1918 if (endfirsthead.topDL) {
1922 for (int i = 0; i < rows_; ++i) {
1923 if (row_info[i].endfirsthead) {
1924 ret += TeXRow(os, i, buf, runparams);
1927 if (endfirsthead.bottomDL) {
1931 os << "\\endfirsthead\n";
1934 // output footer info
1936 if (endfoot.topDL) {
1940 for (int i = 0; i < rows_; ++i) {
1941 if (row_info[i].endfoot) {
1942 ret += TeXRow(os, i, buf, runparams);
1945 if (endfoot.bottomDL) {
1949 os << "\\endfoot\n";
1951 if (endlastfoot.empty) {
1952 os << "\\endlastfoot\n";
1956 // output lastfooter info
1957 if (haveLTLastFoot()) {
1958 if (endlastfoot.topDL) {
1962 for (int i = 0; i < rows_; ++i) {
1963 if (row_info[i].endlastfoot) {
1964 ret += TeXRow(os, i, buf, runparams);
1967 if (endlastfoot.bottomDL) {
1971 os << "\\endlastfoot\n";
1978 bool LyXTabular::isValidRow(int row) const
1980 if (!is_long_tabular)
1982 return !row_info[row].endhead && !row_info[row].endfirsthead &&
1983 !row_info[row].endfoot && !row_info[row].endlastfoot;
1987 int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
1988 OutputParams const & runparams) const
1991 int cell = getCellNumber(i, 0);
1993 ret += TeXTopHLine(os, i);
1994 for (int j = 0; j < columns_; ++j) {
1995 if (isPartOfMultiColumn(i, j))
1997 ret += TeXCellPreamble(os, cell);
1998 InsetText & inset = getCellInset(cell);
2000 Paragraph & par = inset.paragraphs().front();
2001 bool rtl = par.isRightToLeftPar(buf.params())
2003 && getPWidth(cell).zero();
2007 ret += inset.latex(buf, os, runparams);
2011 ret += TeXCellPostamble(os, cell);
2012 if (!isLastCellInRow(cell)) { // not last cell in row
2018 os << "\\tabularnewline\n";
2020 ret += TeXBottomHLine(os, i);
2025 int LyXTabular::latex(Buffer const & buf, ostream & os,
2026 OutputParams const & runparams) const
2030 //+---------------------------------------------------------------------
2031 //+ first the opening preamble +
2032 //+---------------------------------------------------------------------
2035 os << "\\begin{sideways}\n";
2038 if (is_long_tabular)
2039 os << "\\begin{longtable}{";
2041 os << "\\begin{tabular}{";
2042 for (int i = 0; i < columns_; ++i) {
2043 if (!column_info[i].align_special.empty()) {
2044 os << column_info[i].align_special;
2046 if (column_info[i].left_line)
2048 if (!column_info[i].p_width.zero()) {
2049 switch (column_info[i].alignment) {
2050 case LYX_ALIGN_LEFT:
2051 os << ">{\\raggedright}";
2053 case LYX_ALIGN_RIGHT:
2054 os << ">{\\raggedleft}";
2056 case LYX_ALIGN_CENTER:
2057 os << ">{\\centering}";
2059 case LYX_ALIGN_NONE:
2060 case LYX_ALIGN_BLOCK:
2061 case LYX_ALIGN_LAYOUT:
2062 case LYX_ALIGN_SPECIAL:
2066 switch (column_info[i].valignment) {
2067 case LYX_VALIGN_TOP:
2070 case LYX_VALIGN_MIDDLE:
2073 case LYX_VALIGN_BOTTOM:
2078 << column_info[i].p_width.asLatexString()
2081 switch (column_info[i].alignment) {
2082 case LYX_ALIGN_LEFT:
2085 case LYX_ALIGN_RIGHT:
2093 if (column_info[i].right_line)
2100 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2102 //+---------------------------------------------------------------------
2103 //+ the single row and columns (cells) +
2104 //+---------------------------------------------------------------------
2106 for (int i = 0; i < rows_; ++i) {
2107 if (isValidRow(i)) {
2108 ret += TeXRow(os, i, buf, runparams);
2109 if (is_long_tabular && row_info[i].newpage) {
2110 os << "\\newpage\n";
2116 //+---------------------------------------------------------------------
2117 //+ the closing of the tabular +
2118 //+---------------------------------------------------------------------
2120 if (is_long_tabular)
2121 os << "\\end{longtable}";
2123 os << "\\end{tabular}";
2125 os << "\n\\end{sideways}";
2133 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2134 const OutputParams & runparams) const
2136 os << "<tabular ca=\"";
2137 for (int i = 0; i < columns_; ++i) {
2138 switch (column_info[i].alignment) {
2139 case LYX_ALIGN_LEFT:
2142 case LYX_ALIGN_RIGHT:
2153 for (int i = 0; i < rows_; ++i) {
2154 for (int j = 0; j < columns_; ++j) {
2155 if (isPartOfMultiColumn(i, j))
2157 InsetText & inset = getCellInset(cell);
2159 ret += inset.linuxdoc(buf, os, runparams);
2161 if (isLastCellInRow(cell)) {
2170 os << "</tabular>\n";
2175 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row,
2176 OutputParams const & runparams) const
2179 int cell = getFirstCellInRow(row);
2182 for (int j = 0; j < columns_; ++j) {
2183 if (isPartOfMultiColumn(row, j))
2186 os << "<entry align=\"";
2187 switch (getAlignment(cell)) {
2188 case LYX_ALIGN_LEFT:
2191 case LYX_ALIGN_RIGHT:
2199 os << "\" valign=\"";
2200 switch (getVAlignment(cell)) {
2201 case LYX_VALIGN_TOP:
2204 case LYX_VALIGN_BOTTOM:
2207 case LYX_VALIGN_MIDDLE:
2212 if (isMultiColumn(cell)) {
2213 os << " namest=\"col" << j << "\" ";
2214 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2218 OutputParams runp = runparams;
2219 runp.mixed_content = true;
2220 ret += getCellInset(cell).docbook(buf, os, runp);
2229 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2230 OutputParams const & runparams) const
2234 //+---------------------------------------------------------------------
2235 //+ first the opening preamble +
2236 //+---------------------------------------------------------------------
2238 os << "<tgroup cols=\"" << columns_
2239 << "\" colsep=\"1\" rowsep=\"1\">\n";
2241 for (int i = 0; i < columns_; ++i) {
2242 os << "<colspec colname=\"col" << i << "\" align=\"";
2243 switch (column_info[i].alignment) {
2244 case LYX_ALIGN_LEFT:
2247 case LYX_ALIGN_RIGHT:
2255 if (runparams.flavor == OutputParams::XML)
2261 //+---------------------------------------------------------------------
2262 //+ Long Tabular case +
2263 //+---------------------------------------------------------------------
2265 // output header info
2266 if (haveLTHead() || haveLTFirstHead()) {
2269 for (int i = 0; i < rows_; ++i) {
2270 if (row_info[i].endhead || row_info[i].endfirsthead) {
2271 ret += docbookRow(buf, os, i, runparams);
2277 // output footer info
2278 if (haveLTFoot() || haveLTLastFoot()) {
2281 for (int i = 0; i < rows_; ++i) {
2282 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2283 ret += docbookRow(buf, os, i, runparams);
2290 //+---------------------------------------------------------------------
2291 //+ the single row and columns (cells) +
2292 //+---------------------------------------------------------------------
2296 for (int i = 0; i < rows_; ++i) {
2297 if (isValidRow(i)) {
2298 ret += docbookRow(buf, os, i, runparams);
2303 //+---------------------------------------------------------------------
2304 //+ the closing of the tabular +
2305 //+---------------------------------------------------------------------
2314 int LyXTabular::asciiTopHLine(ostream & os, int row,
2315 vector<unsigned int> const & clen) const
2317 int const fcell = getFirstCellInRow(row);
2318 int const n = numberOfCellsInRow(fcell) + fcell;
2321 for (int i = fcell; i < n; ++i) {
2331 for (int i = fcell; i < n; ++i) {
2342 int column = column_of_cell(i);
2343 int len = clen[column];
2344 while (column < columns_ - 1
2345 && isPartOfMultiColumn(row, ++column))
2346 len += clen[column] + 4;
2347 os << string(len, ch);
2362 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2363 vector<unsigned int> const & clen) const
2365 int const fcell = getFirstCellInRow(row);
2366 int const n = numberOfCellsInRow(fcell) + fcell;
2369 for (int i = fcell; i < n; ++i) {
2370 if (bottomLine(i)) {
2379 for (int i = fcell; i < n; ++i) {
2380 if (bottomLine(i)) {
2390 int column = column_of_cell(i);
2391 int len = clen[column];
2392 while (column < columns_ -1
2393 && isPartOfMultiColumn(row, ++column))
2394 len += clen[column] + 4;
2395 os << string(len, ch);
2396 if (bottomLine(i)) {
2410 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2411 OutputParams const & runparams,
2412 int cell, int row, int column,
2413 vector<unsigned int> const & clen,
2414 bool onlydata) const
2417 int ret = getCellInset(cell).plaintext(buf, sstr, runparams);
2429 unsigned int len1 = sstr.str().length();
2430 unsigned int len2 = clen[column];
2431 while (column < columns_ -1
2432 && isPartOfMultiColumn(row, ++column))
2433 len2 += clen[column] + 4;
2436 switch (getAlignment(cell)) {
2438 case LYX_ALIGN_LEFT:
2441 case LYX_ALIGN_RIGHT:
2445 case LYX_ALIGN_CENTER:
2451 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2453 if (rightLine(cell))
2462 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2463 OutputParams const & runparams,
2465 bool onlydata, unsigned char delim) const
2469 // first calculate the width of the single columns
2470 vector<unsigned int> clen(columns_);
2473 // first all non (real) multicolumn cells!
2474 for (int j = 0; j < columns_; ++j) {
2476 for (int i = 0; i < rows_; ++i) {
2477 int cell = getCellNumber(i, j);
2478 if (isMultiColumnReal(cell))
2481 getCellInset(cell).plaintext(buf, sstr, runparams);
2482 if (clen[j] < sstr.str().length())
2483 clen[j] = sstr.str().length();
2486 // then all (real) multicolumn cells!
2487 for (int j = 0; j < columns_; ++j) {
2488 for (int i = 0; i < rows_; ++i) {
2489 int cell = getCellNumber(i, j);
2490 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2493 getCellInset(cell).plaintext(buf, sstr, runparams);
2494 int len = int(sstr.str().length());
2495 int const n = cells_in_multicolumn(cell);
2496 for (int k = j; len > 0 && k < j + n - 1; ++k)
2498 if (len > int(clen[j + n - 1]))
2499 clen[j + n - 1] = len;
2504 for (int i = 0; i < rows_; ++i) {
2505 if (!onlydata && asciiTopHLine(os, i, clen))
2506 for (int j = 0; j < depth; ++j)
2508 for (int j = 0; j < columns_; ++j) {
2509 if (isPartOfMultiColumn(i, j))
2511 if (onlydata && j > 0)
2513 ret += asciiPrintCell(buf, os, runparams,
2514 cell, i, j, clen, onlydata);
2519 for (int j = 0; j < depth; ++j)
2521 if (asciiBottomHLine(os, i, clen))
2522 for (int j = 0; j < depth; ++j)
2530 InsetText & LyXTabular::getCellInset(int cell) const
2532 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2536 InsetText & LyXTabular::getCellInset(int row, int column) const
2538 return cell_info[row][column].inset;
2542 int LyXTabular::getCellFromInset(InsetBase const * inset) const
2544 // is this inset part of the tabular?
2546 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2547 BOOST_ASSERT(false);
2550 for (int cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2551 if (&getCellInset(cell) == inset) {
2552 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2553 << "cell=" << cell << endl;
2557 // We should have found a cell at this point
2558 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2559 << inset << " not found!" << endl;
2560 BOOST_ASSERT(false);
2566 void LyXTabular::validate(LaTeXFeatures & features) const
2568 features.require("NeedTabularnewline");
2569 if (isLongTabular())
2570 features.require("longtable");
2572 features.require("rotating");
2573 for (int cell = 0; cell < numberofcells; ++cell) {
2574 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2575 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2576 features.require("array");
2577 getCellInset(cell).validate(features);
2582 void LyXTabular::getLabelList(Buffer const & buffer,
2583 std::vector<string> & list) const
2585 for (int i = 0; i < rows_; ++i)
2586 for (int j = 0; j < columns_; ++j)
2587 getCellInset(i, j).getLabelList(buffer, list);
2591 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2593 ParagraphList const & parlist = getCellInset(cell).paragraphs();
2594 ParagraphList::const_iterator cit = parlist.begin();
2595 ParagraphList::const_iterator end = parlist.end();
2597 for (; cit != end; ++cit)
2598 for (int i = 0; i < cit->size(); ++i)
2599 if (cit->isNewline(i))