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 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
381 cell_info[i][j].cellno = cellno++;
383 cell_info[i].back().right_line = true;
386 set_row_column_number_info();
390 void LyXTabular::appendRow(BufferParams const & bp, int cell)
394 int row = row_of_cell(cell);
396 row_vector::iterator rit = row_info.begin() + row;
397 row_info.insert(rit, rowstruct());
398 // now set the values of the row before
399 row_info[row] = row_info[row + 1];
401 cell_vvector old(rows_ - 1);
402 for (int i = 0; i < rows_ - 1; ++i)
403 swap(cell_info[i], old[i]);
405 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
407 for (int i = 0; i <= row; ++i)
408 swap(cell_info[i], old[i]);
409 for (int i = row + 2; i < rows_; ++i)
410 swap(cell_info[i], old[i - 1]);
412 if (bp.tracking_changes)
413 for (int j = 0; j < columns_; ++j)
414 cell_info[row + 1][j].inset.markNew(true);
416 set_row_column_number_info();
420 void LyXTabular::deleteRow(int row)
422 // Not allowed to delete last row
426 row_info.erase(row_info.begin() + row);
427 cell_info.erase(cell_info.begin() + row);
433 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
437 int const column = column_of_cell(cell);
438 column_vector::iterator cit = column_info.begin() + column + 1;
439 column_info.insert(cit, columnstruct());
440 // set the column values of the column before
441 column_info[column + 1] = column_info[column];
443 for (int i = 0; i < rows_; ++i) {
444 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
446 // care about multicolumns
447 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
448 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
450 if (column + 2 >= columns_
451 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
452 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
455 for (int i = 0; i < rows_; ++i) {
456 cell_info[i][column + 1].inset.clear(false);
457 if (bp.tracking_changes)
458 cell_info[i][column + 1].inset.markNew(true);
464 void LyXTabular::deleteColumn(int column)
466 // Not allowed to delete last column
470 column_info.erase(column_info.begin() + column);
471 for (int i = 0; i < rows_; ++i)
472 cell_info[i].erase(cell_info[i].begin() + column);
478 void LyXTabular::set_row_column_number_info()
481 for (int row = 0; row < rows_; ++row) {
482 for (int column = 0; column < columns_; ++column) {
483 if (cell_info[row][column].multicolumn
484 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
486 cell_info[row][column].cellno = numberofcells;
489 ++numberofcells; // because this is one more than as we start from 0
491 rowofcell.resize(numberofcells);
492 columnofcell.resize(numberofcells);
494 for (int row = 0, column = 0, c = 0;
495 c < numberofcells && row < rows_ && column < columns_;) {
497 columnofcell[c] = column;
501 } while (column < columns_ &&
502 cell_info[row][column].multicolumn
503 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
505 if (column == columns_) {
511 for (int row = 0; row < rows_; ++row) {
512 for (int column = 0; column < columns_; ++column) {
513 if (isPartOfMultiColumn(row,column))
515 cell_info[row][column].inset.setAutoBreakRows(
516 !getPWidth(getCellNumber(row, column)).zero());
522 int LyXTabular::getNumberOfCells() const
524 return numberofcells;
528 int LyXTabular::numberOfCellsInRow(int cell) const
530 int const row = row_of_cell(cell);
532 for (int i = 0; i < columns_; ++i)
533 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
539 // returns 1 if there is a topline, returns 0 if not
540 bool LyXTabular::topLine(int cell, bool onlycolumn) const
542 if (!onlycolumn && isMultiColumn(cell))
543 return cellinfo_of_cell(cell).top_line;
544 return row_info[row_of_cell(cell)].top_line;
548 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
550 if (!onlycolumn && isMultiColumn(cell))
551 return cellinfo_of_cell(cell).bottom_line;
552 return row_info[row_of_cell(cell)].bottom_line;
556 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
558 if (!onlycolumn && isMultiColumn(cell) &&
559 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
561 if (cellinfo_of_cell(cell).align_special.empty())
562 return cellinfo_of_cell(cell).left_line;
563 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
565 if (column_info[column_of_cell(cell)].align_special.empty())
566 return column_info[column_of_cell(cell)].left_line;
567 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
571 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
573 if (!onlycolumn && isMultiColumn(cell) &&
574 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
576 if (cellinfo_of_cell(cell).align_special.empty())
577 return cellinfo_of_cell(cell).right_line;
578 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
580 if (column_info[column_of_cell(cell)].align_special.empty())
581 return column_info[right_column_of_cell(cell)].right_line;
582 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
586 bool LyXTabular::topAlreadyDrawn(int cell) const
588 int row = row_of_cell(cell);
589 if (row > 0 && !getAdditionalHeight(row)) {
590 int column = column_of_cell(cell);
593 && cell_info[row][column].multicolumn
594 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
596 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
597 return row_info[row].bottom_line;
599 return cell_info[row][column].bottom_line;
605 bool LyXTabular::leftAlreadyDrawn(int cell) const
607 int column = column_of_cell(cell);
609 int row = row_of_cell(cell);
611 (cell_info[row][column].multicolumn ==
612 LyXTabular::CELL_PART_OF_MULTICOLUMN));
613 if (getAdditionalWidth(cell_info[row][column].cellno))
615 return rightLine(cell_info[row][column].cellno);
621 bool LyXTabular::isLastRow(int cell) const
623 return row_of_cell(cell) == rows_ - 1;
627 int LyXTabular::getAdditionalHeight(int row) const
629 if (!row || row >= rows_)
635 for (int column = 0; column < columns_ && bottom; ++column) {
636 switch (cell_info[row - 1][column].multicolumn) {
637 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
638 bottom = cell_info[row - 1][column].bottom_line;
640 case LyXTabular::CELL_NORMAL:
641 bottom = row_info[row - 1].bottom_line;
644 for (int column = 0; column < columns_ && top; ++column) {
645 switch (cell_info[row][column].multicolumn) {
646 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
647 top = cell_info[row][column].top_line;
649 case LyXTabular::CELL_NORMAL:
650 top = row_info[row].top_line;
654 return WIDTH_OF_LINE;
659 int LyXTabular::getAdditionalWidth(int cell) const
661 // internally already set in setWidthOfCell
662 // used to get it back in text.C
663 int const col = right_column_of_cell(cell);
664 int const row = row_of_cell(cell);
665 if (col < columns_ - 1 && rightLine(cell) &&
666 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
668 return WIDTH_OF_LINE;
674 // returns the maximum over all rows
675 int LyXTabular::getWidthOfColumn(int cell) const
677 int const column1 = column_of_cell(cell);
678 int const column2 = right_column_of_cell(cell);
680 for (int i = column1; i <= column2; ++i)
681 result += column_info[i].width_of_column;
686 int LyXTabular::getWidthOfTabular() const
688 return width_of_tabular;
692 // returns true if a complete update is necessary, otherwise false
693 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
695 if (!isMultiColumn(cell))
698 int const row = row_of_cell(cell);
699 int const column1 = column_of_cell(cell);
700 int const column2 = right_column_of_cell(cell);
701 int const old_val = cell_info[row][column2].width_of_cell;
703 // first set columns to 0 so we can calculate the right width
704 for (int i = column1; i <= column2; ++i) {
705 cell_info[row][i].width_of_cell = 0;
707 // set the width to MAX_WIDTH until width > 0
708 int width = new_width + 2 * WIDTH_OF_LINE;
710 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
711 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
712 width -= column_info[i].width_of_column;
715 cell_info[row][i].width_of_cell = width;
717 if (old_val != cell_info[row][column2].width_of_cell) {
718 // in this case we have to recalculate all multicolumn cells which
719 // have this column as one of theirs but not as last one
720 calculate_width_of_column_NMC(i);
721 recalculateMulticolumnsOfColumn(i);
722 calculate_width_of_column(i);
728 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
730 // the last column does not have to be recalculated because all
731 // multicolumns will have here there last multicolumn cell which
732 // always will have the whole rest of the width of the cell.
733 if (column > (columns_ - 2))
735 for(int row = 0; row < rows_; ++row) {
736 int mc = cell_info[row][column].multicolumn;
737 int nmc = cell_info[row][column+1].multicolumn;
738 // we only have to update multicolumns which do not have this
739 // column as their last column!
740 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
741 (mc == CELL_PART_OF_MULTICOLUMN &&
742 nmc == CELL_PART_OF_MULTICOLUMN))
744 int const cellno = cell_info[row][column].cellno;
745 setWidthOfMulticolCell(cellno,
746 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
752 // returns 1 if a complete update is necessary, otherwise 0
753 void LyXTabular::setWidthOfCell(int cell, int new_width)
755 int const row = row_of_cell(cell);
756 int const column1 = column_of_cell(cell);
761 if (rightLine(cell_info[row][column1].cellno, true) &&
762 column1 < columns_ - 1 &&
763 leftLine(cell_info[row][column1+1].cellno, true))
765 add_width = WIDTH_OF_LINE;
768 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
771 if (isMultiColumnReal(cell)) {
772 tmp = setWidthOfMulticolCell(cell, new_width);
774 width = new_width + 2 * WIDTH_OF_LINE + add_width;
775 cell_info[row][column1].width_of_cell = width;
776 tmp = calculate_width_of_column_NMC(column1);
778 recalculateMulticolumnsOfColumn(column1);
781 for (int i = 0; i < columns_; ++i)
782 calculate_width_of_column(i);
783 calculate_width_of_tabular();
788 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
790 if (!isMultiColumn(cell) || onlycolumn)
791 column_info[column_of_cell(cell)].alignment = align;
793 cellinfo_of_cell(cell).alignment = align;
797 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
799 if (!isMultiColumn(cell) || onlycolumn)
800 column_info[column_of_cell(cell)].valignment = align;
802 cellinfo_of_cell(cell).valignment = align;
806 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
808 int const j = column_of_cell(cell);
810 column_info[j].p_width = width;
811 for (int i = 0; i < rows_; ++i) {
812 int const cell = getCellNumber(i, j);
813 // because of multicolumns
814 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
819 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
821 if (!isMultiColumn(cell))
824 cellinfo_of_cell(cell).p_width = width;
825 getCellInset(cell).setAutoBreakRows(!width.zero());
830 void LyXTabular::setAlignSpecial(int cell, string const & special,
831 LyXTabular::Feature what)
833 if (what == SET_SPECIAL_MULTI)
834 cellinfo_of_cell(cell).align_special = special;
836 column_info[column_of_cell(cell)].align_special = special;
840 void LyXTabular::setAllLines(int cell, bool line)
842 setTopLine(cell, line);
843 setBottomLine(cell, line);
844 setRightLine(cell, line);
845 setLeftLine(cell, line);
849 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
851 int const row = row_of_cell(cell);
852 if (onlycolumn || !isMultiColumn(cell))
853 row_info[row].top_line = line;
855 cellinfo_of_cell(cell).top_line = line;
859 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
861 if (onlycolumn || !isMultiColumn(cell))
862 row_info[row_of_cell(cell)].bottom_line = line;
864 cellinfo_of_cell(cell).bottom_line = line;
868 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
870 if (onlycolumn || !isMultiColumn(cell))
871 column_info[column_of_cell(cell)].left_line = line;
873 cellinfo_of_cell(cell).left_line = line;
877 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
879 if (onlycolumn || !isMultiColumn(cell))
880 column_info[right_column_of_cell(cell)].right_line = line;
882 cellinfo_of_cell(cell).right_line = line;
886 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
888 if (!onlycolumn && isMultiColumn(cell))
889 return cellinfo_of_cell(cell).alignment;
890 return column_info[column_of_cell(cell)].alignment;
894 LyXTabular::VAlignment
895 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
897 if (!onlycolumn && isMultiColumn(cell))
898 return cellinfo_of_cell(cell).valignment;
899 return column_info[column_of_cell(cell)].valignment;
903 LyXLength const LyXTabular::getPWidth(int cell) const
905 if (isMultiColumn(cell))
906 return cellinfo_of_cell(cell).p_width;
907 return column_info[column_of_cell(cell)].p_width;
911 LyXLength const LyXTabular::getColumnPWidth(int cell) const
913 return column_info[column_of_cell(cell)].p_width;
917 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
919 if (isMultiColumn(cell))
920 return cellinfo_of_cell(cell).p_width;
925 string const LyXTabular::getAlignSpecial(int cell, int what) const
927 if (what == SET_SPECIAL_MULTI)
928 return cellinfo_of_cell(cell).align_special;
929 return column_info[column_of_cell(cell)].align_special;
933 int LyXTabular::getWidthOfCell(int cell) const
935 int const row = row_of_cell(cell);
936 int const column1 = column_of_cell(cell);
937 int const column2 = right_column_of_cell(cell);
939 for (int i = column1; i <= column2; ++i)
940 result += cell_info[row][i].width_of_cell;
945 int LyXTabular::getBeginningOfTextInCell(int cell) const
949 switch (getAlignment(cell)) {
950 case LYX_ALIGN_CENTER:
951 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
953 case LYX_ALIGN_RIGHT:
954 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
955 // + getAdditionalWidth(cell);
958 // LYX_ALIGN_LEFT: nothing :-)
968 bool LyXTabular::isFirstCellInRow(int cell) const
970 return column_of_cell(cell) == 0;
974 int LyXTabular::getFirstCellInRow(int row) const
978 return cell_info[row][0].cellno;
982 bool LyXTabular::isLastCellInRow(int cell) const
984 return right_column_of_cell(cell) == columns_ - 1;
988 int LyXTabular::getLastCellInRow(int row) const
992 return cell_info[row][columns_-1].cellno;
996 void LyXTabular::calculate_width_of_column(int column)
999 for (int i = 0; i < rows_; ++i)
1000 maximum = max(cell_info[i][column].width_of_cell, maximum);
1001 column_info[column].width_of_column = maximum;
1006 // Calculate the columns regarding ONLY the normal cells and if this
1007 // column is inside a multicolumn cell then use it only if its the last
1008 // column of this multicolumn cell as this gives an added width to the
1009 // column, all the rest should be adapted!
1011 bool LyXTabular::calculate_width_of_column_NMC(int column)
1013 int const old_column_width = column_info[column].width_of_column;
1015 for (int i = 0; i < rows_; ++i) {
1016 int cell = getCellNumber(i, column);
1017 bool ismulti = isMultiColumnReal(cell);
1018 if ((!ismulti || column == right_column_of_cell(cell)) &&
1019 cell_info[i][column].width_of_cell > max)
1021 max = cell_info[i][column].width_of_cell;
1024 column_info[column].width_of_column = max;
1025 return column_info[column].width_of_column != old_column_width;
1029 void LyXTabular::calculate_width_of_tabular()
1031 width_of_tabular = 0;
1032 for (int i = 0; i < columns_; ++i)
1033 width_of_tabular += column_info[i].width_of_column;
1037 int LyXTabular::row_of_cell(int cell) const
1039 if (cell >= numberofcells)
1043 return rowofcell[cell];
1047 int LyXTabular::column_of_cell(int cell) const
1049 if (cell >= numberofcells)
1050 return columns_ - 1;
1053 return columnofcell[cell];
1057 int LyXTabular::right_column_of_cell(int cell) const
1059 int const row = row_of_cell(cell);
1060 int column = column_of_cell(cell);
1061 while (column < columns_ - 1 &&
1062 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1068 void LyXTabular::write(Buffer const & buf, ostream & os) const
1072 << write_attribute("version", 3)
1073 << write_attribute("rows", rows_)
1074 << write_attribute("columns", columns_)
1076 // global longtable options
1078 << write_attribute("rotate", rotate)
1079 << write_attribute("islongtable", is_long_tabular)
1080 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1081 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1082 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1083 << write_attribute("headTopDL", endhead.topDL)
1084 << write_attribute("headBottomDL", endhead.bottomDL)
1085 << write_attribute("footTopDL", endfoot.topDL)
1086 << write_attribute("footBottomDL", endfoot.bottomDL)
1087 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1088 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1089 << write_attribute("lastFootEmpty", endlastfoot.empty)
1091 for (int j = 0; j < columns_; ++j) {
1093 << write_attribute("alignment", column_info[j].alignment)
1094 << write_attribute("valignment", column_info[j].valignment)
1095 << write_attribute("leftline", column_info[j].left_line)
1096 << write_attribute("rightline", column_info[j].right_line)
1097 << write_attribute("width", column_info[j].p_width.asString())
1098 << write_attribute("special", column_info[j].align_special)
1101 for (int i = 0; i < rows_; ++i) {
1103 << write_attribute("topline", row_info[i].top_line)
1104 << write_attribute("bottomline", row_info[i].bottom_line)
1105 << write_attribute("endhead", row_info[i].endhead)
1106 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1107 << write_attribute("endfoot", row_info[i].endfoot)
1108 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1109 << write_attribute("newpage", row_info[i].newpage)
1111 for (int j = 0; j < columns_; ++j) {
1113 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1114 << write_attribute("alignment", cell_info[i][j].alignment)
1115 << write_attribute("valignment", cell_info[i][j].valignment)
1116 << write_attribute("topline", cell_info[i][j].top_line)
1117 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1118 << write_attribute("leftline", cell_info[i][j].left_line)
1119 << write_attribute("rightline", cell_info[i][j].right_line)
1120 << write_attribute("rotate", cell_info[i][j].rotate)
1121 << write_attribute("usebox", cell_info[i][j].usebox)
1122 << write_attribute("width", cell_info[i][j].p_width)
1123 << write_attribute("special", cell_info[i][j].align_special)
1125 os << "\\begin_inset ";
1126 cell_info[i][j].inset.write(buf, os);
1127 os << "\n\\end_inset \n"
1132 os << "</lyxtabular>\n";
1136 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1140 row_info[--hr].endhead = true;
1142 // set firstheader info
1143 if (fhr && fhr < rows_) {
1144 if (row_info[fhr].endhead) {
1146 row_info[--fhr].endfirsthead = true;
1147 row_info[fhr].endhead = false;
1149 } else if (row_info[fhr - 1].endhead) {
1150 endfirsthead.empty = true;
1152 while (fhr > 0 && !row_info[--fhr].endhead) {
1153 row_info[fhr].endfirsthead = true;
1158 if (fr && fr < rows_) {
1159 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1160 while (fr > 0 && !row_info[--fr].endhead) {
1161 row_info[fr].endfoot = true;
1162 row_info[fr].endhead = false;
1164 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1165 while (fr > 0 && !row_info[--fr].endfirsthead) {
1166 row_info[fr].endfoot = true;
1167 row_info[fr].endfirsthead = false;
1169 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1170 while (fr > 0 && !row_info[--fr].endhead &&
1171 !row_info[fr].endfirsthead)
1173 row_info[fr].endfoot = true;
1177 // set lastfooter info
1178 if (lfr && lfr < rows_) {
1179 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1180 while (lfr > 0 && !row_info[--lfr].endhead) {
1181 row_info[lfr].endlastfoot = true;
1182 row_info[lfr].endhead = false;
1184 } else if (row_info[lfr].endfirsthead &&
1185 row_info[lfr - 1].endfirsthead)
1187 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1188 row_info[lfr].endlastfoot = true;
1189 row_info[lfr].endfirsthead = false;
1191 } else if (row_info[lfr].endfoot
1192 && row_info[lfr - 1].endfoot) {
1193 while (lfr > 0 && !row_info[--lfr].endfoot) {
1194 row_info[lfr].endlastfoot = true;
1195 row_info[lfr].endfoot = false;
1197 } else if (!row_info[fr - 1].endhead
1198 && !row_info[fr - 1].endfirsthead &&
1199 !row_info[fr - 1].endfoot)
1202 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1203 !row_info[lfr].endfoot)
1205 row_info[lfr].endlastfoot = true;
1207 } else if (haveLTFoot()) {
1208 endlastfoot.empty = true;
1214 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1217 istream & is = lex.getStream();
1219 l_getline(is, line);
1220 if (!prefixIs(line, "<lyxtabular ")
1221 && !prefixIs(line, "<LyXTabular ")) {
1222 BOOST_ASSERT(false);
1227 if (!getTokenValue(line, "version", version))
1229 BOOST_ASSERT(version >= 2);
1232 if (!getTokenValue(line, "rows", rows_arg))
1235 if (!getTokenValue(line, "columns", columns_arg))
1237 init(buf.params(), rows_arg, columns_arg);
1238 l_getline(is, line);
1239 if (!prefixIs(line, "<features")) {
1240 lyxerr << "Wrong tabular format (expected <features ...> got"
1241 << line << ')' << endl;
1244 getTokenValue(line, "rotate", rotate);
1245 getTokenValue(line, "islongtable", is_long_tabular);
1246 // compatibility read for old longtable options. Now we can make any
1247 // row part of the header/footer type we want before it was strict
1248 // sequential from the first row down (as LaTeX does it!). So now when
1249 // we find a header/footer line we have to go up the rows and set it
1250 // on all preceding rows till the first or one with already a h/f option
1251 // set. If we find a firstheader on the same line as a header or a
1252 // lastfooter on the same line as a footer then this should be set empty.
1260 getTokenValue(line, "endhead", hrow);
1261 getTokenValue(line, "endfirsthead", fhrow);
1262 getTokenValue(line, "endfoot", frow);
1263 getTokenValue(line, "endlastfoot", lfrow);
1264 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1266 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1267 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1268 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1269 getTokenValue(line, "headTopDL", endhead.topDL);
1270 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1271 getTokenValue(line, "footTopDL", endfoot.topDL);
1272 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1273 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1274 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1275 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1277 for (int j = 0; j < columns_; ++j) {
1279 if (!prefixIs(line,"<column")) {
1280 lyxerr << "Wrong tabular format (expected <column ...> got"
1281 << line << ')' << endl;
1284 getTokenValue(line, "alignment", column_info[j].alignment);
1285 getTokenValue(line, "valignment", column_info[j].valignment);
1286 getTokenValue(line, "leftline", column_info[j].left_line);
1287 getTokenValue(line, "rightline", column_info[j].right_line);
1288 getTokenValue(line, "width", column_info[j].p_width);
1289 getTokenValue(line, "special", column_info[j].align_special);
1292 for (int i = 0; i < rows_; ++i) {
1293 l_getline(is, line);
1294 if (!prefixIs(line, "<row")) {
1295 lyxerr << "Wrong tabular format (expected <row ...> got"
1296 << line << ')' << endl;
1299 getTokenValue(line, "topline", row_info[i].top_line);
1300 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1301 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1302 getTokenValue(line, "endhead", row_info[i].endhead);
1303 getTokenValue(line, "endfoot", row_info[i].endfoot);
1304 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1305 getTokenValue(line, "newpage", row_info[i].newpage);
1306 for (int j = 0; j < columns_; ++j) {
1307 l_getline(is, line);
1308 if (!prefixIs(line, "<cell")) {
1309 lyxerr << "Wrong tabular format (expected <cell ...> got"
1310 << line << ')' << endl;
1313 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1314 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1315 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1316 getTokenValue(line, "topline", cell_info[i][j].top_line);
1317 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1318 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1319 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1320 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1321 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1322 getTokenValue(line, "width", cell_info[i][j].p_width);
1323 getTokenValue(line, "special", cell_info[i][j].align_special);
1324 l_getline(is, line);
1325 if (prefixIs(line, "\\begin_inset")) {
1326 cell_info[i][j].inset.read(buf, lex);
1327 l_getline(is, line);
1329 if (!prefixIs(line, "</cell>")) {
1330 lyxerr << "Wrong tabular format (expected </cell> got"
1331 << line << ')' << endl;
1335 l_getline(is, line);
1336 if (!prefixIs(line, "</row>")) {
1337 lyxerr << "Wrong tabular format (expected </row> got"
1338 << line << ')' << endl;
1342 while (!prefixIs(line, "</lyxtabular>")) {
1343 l_getline(is, line);
1345 set_row_column_number_info();
1349 bool LyXTabular::isMultiColumn(int cell) const
1351 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1355 bool LyXTabular::isMultiColumnReal(int cell) const
1357 return column_of_cell(cell) != right_column_of_cell(cell) &&
1358 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1362 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1364 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1368 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1370 cellstruct & cs = cellinfo_of_cell(cell);
1371 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1372 cs.alignment = column_info[column_of_cell(cell)].alignment;
1373 cs.top_line = row_info[row_of_cell(cell)].top_line;
1374 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1375 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1376 for (int i = 1; i < number; ++i) {
1377 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1378 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1379 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs());
1380 cs1.inset.clear(false);
1382 set_row_column_number_info();
1386 int LyXTabular::cells_in_multicolumn(int cell) const
1388 int const row = row_of_cell(cell);
1389 int column = column_of_cell(cell);
1392 while (column < columns_ &&
1393 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1402 int LyXTabular::unsetMultiColumn(int cell)
1404 int const row = row_of_cell(cell);
1405 int column = column_of_cell(cell);
1409 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1410 cell_info[row][column].multicolumn = CELL_NORMAL;
1412 while (column < columns_ &&
1413 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1415 cell_info[row][column].multicolumn = CELL_NORMAL;
1420 set_row_column_number_info();
1425 void LyXTabular::setLongTabular(bool what)
1427 is_long_tabular = what;
1431 bool LyXTabular::isLongTabular() const
1433 return is_long_tabular;
1437 void LyXTabular::setRotateTabular(bool flag)
1443 bool LyXTabular::getRotateTabular() const
1449 void LyXTabular::setRotateCell(int cell, bool flag)
1451 cellinfo_of_cell(cell).rotate = flag;
1455 bool LyXTabular::getRotateCell(int cell) const
1457 return cellinfo_of_cell(cell).rotate;
1461 bool LyXTabular::needRotating() const
1465 for (int i = 0; i < rows_; ++i)
1466 for (int j = 0; j < columns_; ++j)
1467 if (cell_info[i][j].rotate)
1473 bool LyXTabular::isLastCell(int cell) const
1475 if (cell + 1 < numberofcells)
1481 int LyXTabular::getCellAbove(int cell) const
1483 if (row_of_cell(cell) > 0)
1484 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1489 int LyXTabular::getCellBelow(int cell) const
1491 if (row_of_cell(cell) + 1 < rows_)
1492 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1497 int LyXTabular::getLastCellAbove(int cell) const
1499 if (row_of_cell(cell) <= 0)
1501 if (!isMultiColumn(cell))
1502 return getCellAbove(cell);
1503 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1507 int LyXTabular::getLastCellBelow(int cell) const
1509 if (row_of_cell(cell) + 1 >= rows_)
1511 if (!isMultiColumn(cell))
1512 return getCellBelow(cell);
1513 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1517 int LyXTabular::getCellNumber(int row, int column) const
1519 BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1520 return cell_info[row][column].cellno;
1524 void LyXTabular::setUsebox(int cell, BoxType type)
1526 cellinfo_of_cell(cell).usebox = type;
1530 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1532 if (column_info[column_of_cell(cell)].p_width.zero() &&
1533 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1535 if (cellinfo_of_cell(cell).usebox > 1)
1536 return cellinfo_of_cell(cell).usebox;
1537 return useParbox(cell);
1542 // This are functions used for the longtable support
1544 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1549 row_info[row].endfirsthead = flag;
1553 row_info[row].endhead = flag;
1558 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1561 hd.set = haveLTHead();
1562 return row_info[row].endhead;
1566 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1569 hd.set = haveLTFirstHead();
1570 return row_info[row].endfirsthead;
1574 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1579 row_info[row].endlastfoot = flag;
1583 row_info[row].endfoot = flag;
1588 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1591 fd.set = haveLTFoot();
1592 return row_info[row].endfoot;
1596 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1599 fd.set = haveLTLastFoot();
1600 return row_info[row].endlastfoot;
1604 void LyXTabular::setLTNewPage(int row, bool what)
1606 row_info[row].newpage = what;
1610 bool LyXTabular::getLTNewPage(int row) const
1612 return row_info[row].newpage;
1616 bool LyXTabular::haveLTHead() const
1618 for (int i = 0; i < rows_; ++i)
1619 if (row_info[i].endhead)
1625 bool LyXTabular::haveLTFirstHead() const
1627 if (endfirsthead.empty)
1629 for (int i = 0; i < rows_; ++i)
1630 if (row_info[i].endfirsthead)
1636 bool LyXTabular::haveLTFoot() const
1638 for (int i = 0; i < rows_; ++i)
1639 if (row_info[i].endfoot)
1645 bool LyXTabular::haveLTLastFoot() const
1647 if (endlastfoot.empty)
1649 for (int i = 0; i < rows_; ++i)
1650 if (row_info[i].endlastfoot)
1656 // end longtable support functions
1658 void LyXTabular::setAscentOfRow(int row, int height)
1660 if (row >= rows_ || row_info[row].ascent_of_row == height)
1662 row_info[row].ascent_of_row = height;
1666 void LyXTabular::setDescentOfRow(int row, int height)
1668 if (row >= rows_ || row_info[row].descent_of_row == height)
1670 row_info[row].descent_of_row = height;
1674 int LyXTabular::getAscentOfRow(int row) const
1678 return row_info[row].ascent_of_row;
1682 int LyXTabular::getDescentOfRow(int row) const
1686 return row_info[row].descent_of_row;
1690 int LyXTabular::getHeightOfTabular() const
1693 for (int row = 0; row < rows_; ++row)
1694 height += getAscentOfRow(row) + getDescentOfRow(row) +
1695 getAdditionalHeight(row);
1700 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1702 if (row >= rows_ || column >= columns_)
1704 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1708 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1710 if (row < 0 || row >= rows_)
1713 int const fcell = getFirstCellInRow(row);
1714 int const n = numberOfCellsInRow(fcell) + fcell;
1717 for (int i = fcell; i < n; ++i) {
1721 if (tmp == n - fcell) {
1724 for (int i = fcell; i < n; ++i) {
1727 << column_of_cell(i) + 1
1729 << right_column_of_cell(i) + 1
1741 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1743 if (row < 0 || row >= rows_)
1746 int const fcell = getFirstCellInRow(row);
1747 int const n = numberOfCellsInRow(fcell) + fcell;
1750 for (int i = fcell; i < n; ++i) {
1754 if (tmp == n - fcell) {
1757 for (int i = fcell; i < n; ++i) {
1758 if (bottomLine(i)) {
1760 << column_of_cell(i) + 1
1762 << right_column_of_cell(i) + 1
1774 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1778 if (getRotateCell(cell)) {
1779 os << "\\begin{sideways}\n";
1782 if (isMultiColumn(cell)) {
1783 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1784 if (!cellinfo_of_cell(cell).align_special.empty()) {
1785 os << cellinfo_of_cell(cell).align_special << "}{";
1787 if (leftLine(cell) &&
1788 (isFirstCellInRow(cell) ||
1789 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1790 !rightLine(cell - 1, true))))
1794 if (!getPWidth(cell).zero()) {
1795 switch (getVAlignment(cell)) {
1796 case LYX_VALIGN_TOP:
1799 case LYX_VALIGN_MIDDLE:
1802 case LYX_VALIGN_BOTTOM:
1807 << getPWidth(cell).asLatexString()
1810 switch (getAlignment(cell)) {
1811 case LYX_ALIGN_LEFT:
1814 case LYX_ALIGN_RIGHT:
1822 if (rightLine(cell))
1824 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1830 if (getUsebox(cell) == BOX_PARBOX) {
1832 switch (getVAlignment(cell)) {
1833 case LYX_VALIGN_TOP:
1836 case LYX_VALIGN_MIDDLE:
1839 case LYX_VALIGN_BOTTOM:
1843 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1844 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1845 os << "\\begin{minipage}[";
1846 switch (getVAlignment(cell)) {
1847 case LYX_VALIGN_TOP:
1850 case LYX_VALIGN_MIDDLE:
1853 case LYX_VALIGN_BOTTOM:
1857 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1864 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1869 if (getUsebox(cell) == BOX_PARBOX)
1871 else if (getUsebox(cell) == BOX_MINIPAGE) {
1872 os << "%\n\\end{minipage}";
1875 if (isMultiColumn(cell)) {
1878 if (getRotateCell(cell)) {
1879 os << "%\n\\end{sideways}";
1886 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1887 OutputParams const & runparams) const
1889 if (!is_long_tabular)
1893 // output header info
1895 if (endhead.topDL) {
1899 for (int i = 0; i < rows_; ++i) {
1900 if (row_info[i].endhead) {
1901 ret += TeXRow(os, i, buf, runparams);
1904 if (endhead.bottomDL) {
1908 os << "\\endhead\n";
1910 if (endfirsthead.empty) {
1911 os << "\\endfirsthead\n";
1915 // output firstheader info
1916 if (haveLTFirstHead()) {
1917 if (endfirsthead.topDL) {
1921 for (int i = 0; i < rows_; ++i) {
1922 if (row_info[i].endfirsthead) {
1923 ret += TeXRow(os, i, buf, runparams);
1926 if (endfirsthead.bottomDL) {
1930 os << "\\endfirsthead\n";
1933 // output footer info
1935 if (endfoot.topDL) {
1939 for (int i = 0; i < rows_; ++i) {
1940 if (row_info[i].endfoot) {
1941 ret += TeXRow(os, i, buf, runparams);
1944 if (endfoot.bottomDL) {
1948 os << "\\endfoot\n";
1950 if (endlastfoot.empty) {
1951 os << "\\endlastfoot\n";
1955 // output lastfooter info
1956 if (haveLTLastFoot()) {
1957 if (endlastfoot.topDL) {
1961 for (int i = 0; i < rows_; ++i) {
1962 if (row_info[i].endlastfoot) {
1963 ret += TeXRow(os, i, buf, runparams);
1966 if (endlastfoot.bottomDL) {
1970 os << "\\endlastfoot\n";
1977 bool LyXTabular::isValidRow(int row) const
1979 if (!is_long_tabular)
1981 return !row_info[row].endhead && !row_info[row].endfirsthead &&
1982 !row_info[row].endfoot && !row_info[row].endlastfoot;
1986 int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
1987 OutputParams const & runparams) const
1990 int cell = getCellNumber(i, 0);
1992 ret += TeXTopHLine(os, i);
1993 for (int j = 0; j < columns_; ++j) {
1994 if (isPartOfMultiColumn(i, j))
1996 ret += TeXCellPreamble(os, cell);
1997 InsetText & inset = getCellInset(cell);
1999 Paragraph & par = inset.paragraphs().front();
2000 bool rtl = par.isRightToLeftPar(buf.params())
2002 && getPWidth(cell).zero();
2006 ret += inset.latex(buf, os, runparams);
2010 ret += TeXCellPostamble(os, cell);
2011 if (!isLastCellInRow(cell)) { // not last cell in row
2017 os << "\\tabularnewline\n";
2019 ret += TeXBottomHLine(os, i);
2024 int LyXTabular::latex(Buffer const & buf, ostream & os,
2025 OutputParams const & runparams) const
2029 //+---------------------------------------------------------------------
2030 //+ first the opening preamble +
2031 //+---------------------------------------------------------------------
2034 os << "\\begin{sideways}\n";
2037 if (is_long_tabular)
2038 os << "\\begin{longtable}{";
2040 os << "\\begin{tabular}{";
2041 for (int i = 0; i < columns_; ++i) {
2042 if (!column_info[i].align_special.empty()) {
2043 os << column_info[i].align_special;
2045 if (column_info[i].left_line)
2047 if (!column_info[i].p_width.zero()) {
2048 switch (column_info[i].alignment) {
2049 case LYX_ALIGN_LEFT:
2050 os << ">{\\raggedright}";
2052 case LYX_ALIGN_RIGHT:
2053 os << ">{\\raggedleft}";
2055 case LYX_ALIGN_CENTER:
2056 os << ">{\\centering}";
2058 case LYX_ALIGN_NONE:
2059 case LYX_ALIGN_BLOCK:
2060 case LYX_ALIGN_LAYOUT:
2061 case LYX_ALIGN_SPECIAL:
2065 switch (column_info[i].valignment) {
2066 case LYX_VALIGN_TOP:
2069 case LYX_VALIGN_MIDDLE:
2072 case LYX_VALIGN_BOTTOM:
2077 << column_info[i].p_width.asLatexString()
2080 switch (column_info[i].alignment) {
2081 case LYX_ALIGN_LEFT:
2084 case LYX_ALIGN_RIGHT:
2092 if (column_info[i].right_line)
2099 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2101 //+---------------------------------------------------------------------
2102 //+ the single row and columns (cells) +
2103 //+---------------------------------------------------------------------
2105 for (int i = 0; i < rows_; ++i) {
2106 if (isValidRow(i)) {
2107 ret += TeXRow(os, i, buf, runparams);
2108 if (is_long_tabular && row_info[i].newpage) {
2109 os << "\\newpage\n";
2115 //+---------------------------------------------------------------------
2116 //+ the closing of the tabular +
2117 //+---------------------------------------------------------------------
2119 if (is_long_tabular)
2120 os << "\\end{longtable}";
2122 os << "\\end{tabular}";
2124 os << "\n\\end{sideways}";
2132 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2133 const OutputParams & runparams) const
2135 os << "<tabular ca=\"";
2136 for (int i = 0; i < columns_; ++i) {
2137 switch (column_info[i].alignment) {
2138 case LYX_ALIGN_LEFT:
2141 case LYX_ALIGN_RIGHT:
2152 for (int i = 0; i < rows_; ++i) {
2153 for (int j = 0; j < columns_; ++j) {
2154 if (isPartOfMultiColumn(i, j))
2156 InsetText & inset = getCellInset(cell);
2158 ret += inset.linuxdoc(buf, os, runparams);
2160 if (isLastCellInRow(cell)) {
2169 os << "</tabular>\n";
2174 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row,
2175 OutputParams const & runparams) const
2178 int cell = getFirstCellInRow(row);
2181 for (int j = 0; j < columns_; ++j) {
2182 if (isPartOfMultiColumn(row, j))
2185 os << "<entry align=\"";
2186 switch (getAlignment(cell)) {
2187 case LYX_ALIGN_LEFT:
2190 case LYX_ALIGN_RIGHT:
2198 os << "\" valign=\"";
2199 switch (getVAlignment(cell)) {
2200 case LYX_VALIGN_TOP:
2203 case LYX_VALIGN_BOTTOM:
2206 case LYX_VALIGN_MIDDLE:
2211 if (isMultiColumn(cell)) {
2212 os << " namest=\"col" << j << "\" ";
2213 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2217 OutputParams runp = runparams;
2218 runp.mixed_content = true;
2219 ret += getCellInset(cell).docbook(buf, os, runp);
2228 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2229 OutputParams const & runparams) const
2233 //+---------------------------------------------------------------------
2234 //+ first the opening preamble +
2235 //+---------------------------------------------------------------------
2237 os << "<tgroup cols=\"" << columns_
2238 << "\" colsep=\"1\" rowsep=\"1\">\n";
2240 for (int i = 0; i < columns_; ++i) {
2241 os << "<colspec colname=\"col" << i << "\" align=\"";
2242 switch (column_info[i].alignment) {
2243 case LYX_ALIGN_LEFT:
2246 case LYX_ALIGN_RIGHT:
2257 //+---------------------------------------------------------------------
2258 //+ Long Tabular case +
2259 //+---------------------------------------------------------------------
2261 // output header info
2262 if (haveLTHead() || haveLTFirstHead()) {
2265 for (int i = 0; i < rows_; ++i) {
2266 if (row_info[i].endhead || row_info[i].endfirsthead) {
2267 ret += docbookRow(buf, os, i, runparams);
2273 // output footer info
2274 if (haveLTFoot() || haveLTLastFoot()) {
2277 for (int i = 0; i < rows_; ++i) {
2278 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2279 ret += docbookRow(buf, os, i, runparams);
2286 //+---------------------------------------------------------------------
2287 //+ the single row and columns (cells) +
2288 //+---------------------------------------------------------------------
2292 for (int i = 0; i < rows_; ++i) {
2293 if (isValidRow(i)) {
2294 ret += docbookRow(buf, os, i, runparams);
2299 //+---------------------------------------------------------------------
2300 //+ the closing of the tabular +
2301 //+---------------------------------------------------------------------
2310 int LyXTabular::asciiTopHLine(ostream & os, int row,
2311 vector<unsigned int> const & clen) const
2313 int const fcell = getFirstCellInRow(row);
2314 int const n = numberOfCellsInRow(fcell) + fcell;
2317 for (int i = fcell; i < n; ++i) {
2327 for (int i = fcell; i < n; ++i) {
2338 int column = column_of_cell(i);
2339 int len = clen[column];
2340 while (isPartOfMultiColumn(row, ++column))
2341 len += clen[column] + 4;
2342 os << string(len, ch);
2357 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2358 vector<unsigned int> const & clen) const
2360 int const fcell = getFirstCellInRow(row);
2361 int const n = numberOfCellsInRow(fcell) + fcell;
2364 for (int i = fcell; i < n; ++i) {
2365 if (bottomLine(i)) {
2374 for (int i = fcell; i < n; ++i) {
2375 if (bottomLine(i)) {
2385 int column = column_of_cell(i);
2386 int len = clen[column];
2387 while (isPartOfMultiColumn(row, ++column))
2388 len += clen[column] + 4;
2389 os << string(len, ch);
2390 if (bottomLine(i)) {
2404 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2405 OutputParams const & runparams,
2406 int cell, int row, int column,
2407 vector<unsigned int> const & clen,
2408 bool onlydata) const
2411 int ret = getCellInset(cell).plaintext(buf, sstr, runparams);
2423 unsigned int len1 = sstr.str().length();
2424 unsigned int len2 = clen[column];
2425 while (isPartOfMultiColumn(row, ++column))
2426 len2 += clen[column] + 4;
2429 switch (getAlignment(cell)) {
2431 case LYX_ALIGN_LEFT:
2434 case LYX_ALIGN_RIGHT:
2438 case LYX_ALIGN_CENTER:
2444 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2446 if (rightLine(cell))
2455 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2456 OutputParams const & runparams,
2458 bool onlydata, unsigned char delim) const
2462 // first calculate the width of the single columns
2463 vector<unsigned int> clen(columns_);
2466 // first all non (real) multicolumn cells!
2467 for (int j = 0; j < columns_; ++j) {
2469 for (int i = 0; i < rows_; ++i) {
2470 int cell = getCellNumber(i, j);
2471 if (isMultiColumnReal(cell))
2474 getCellInset(cell).plaintext(buf, sstr, runparams);
2475 if (clen[j] < sstr.str().length())
2476 clen[j] = sstr.str().length();
2479 // then all (real) multicolumn cells!
2480 for (int j = 0; j < columns_; ++j) {
2481 for (int i = 0; i < rows_; ++i) {
2482 int cell = getCellNumber(i, j);
2483 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2486 getCellInset(cell).plaintext(buf, sstr, runparams);
2487 int len = int(sstr.str().length());
2488 int const n = cells_in_multicolumn(cell);
2489 for (int k = j; len > 0 && k < j + n - 1; ++k)
2491 if (len > int(clen[j + n - 1]))
2492 clen[j + n - 1] = len;
2497 for (int i = 0; i < rows_; ++i) {
2498 if (!onlydata && asciiTopHLine(os, i, clen))
2499 for (int j = 0; j < depth; ++j)
2501 for (int j = 0; j < columns_; ++j) {
2502 if (isPartOfMultiColumn(i, j))
2504 if (onlydata && j > 0)
2506 ret += asciiPrintCell(buf, os, runparams,
2507 cell, i, j, clen, onlydata);
2512 for (int j = 0; j < depth; ++j)
2514 if (asciiBottomHLine(os, i, clen))
2515 for (int j = 0; j < depth; ++j)
2523 InsetText & LyXTabular::getCellInset(int cell) const
2525 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2529 InsetText & LyXTabular::getCellInset(int row, int column) const
2531 return cell_info[row][column].inset;
2535 int LyXTabular::getCellFromInset(InsetBase const * inset) const
2537 // is this inset part of the tabular?
2539 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2540 BOOST_ASSERT(false);
2543 for (int cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2544 if (&getCellInset(cell) == inset) {
2545 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2546 << "cell=" << cell << endl;
2550 // We should have found a cell at this point
2551 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2552 << inset << " not found!" << endl;
2553 BOOST_ASSERT(false);
2559 void LyXTabular::validate(LaTeXFeatures & features) const
2561 features.require("NeedTabularnewline");
2562 if (isLongTabular())
2563 features.require("longtable");
2565 features.require("rotating");
2566 for (int cell = 0; cell < numberofcells; ++cell) {
2567 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2568 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2569 features.require("array");
2570 getCellInset(cell).validate(features);
2575 void LyXTabular::getLabelList(Buffer const & buffer,
2576 std::vector<string> & list) const
2578 for (int i = 0; i < rows_; ++i)
2579 for (int j = 0; j < columns_; ++j)
2580 getCellInset(i, j).getLabelList(buffer, list);
2584 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2586 ParagraphList const & parlist = getCellInset(cell).paragraphs();
2587 ParagraphList::const_iterator cit = parlist.begin();
2588 ParagraphList::const_iterator end = parlist.end();
2590 for (; cit != end; ++cit)
2591 for (int i = 0; i < cit->size(); ++i)
2592 if (cit->isNewline(i))