3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
23 #include "bufferparams.h"
25 #include "LaTeXFeatures.h"
27 #include "outputparams.h"
28 #include "paragraph.h"
30 #include "insets/insettabular.h"
32 #include "support/lstrings.h"
33 #include "support/tostr.h"
35 #include "support/std_sstream.h"
37 using lyx::support::ltrim;
38 using lyx::support::prefixIs;
39 using lyx::support::rtrim;
40 using lyx::support::strToInt;
41 using lyx::support::suffixIs;
50 using std::ostringstream;
54 #ifndef CXX_GLOBAL_CSTD
61 int const WIDTH_OF_LINE = 5;
64 string const write_attribute(string const & name, T const & t)
66 string const s = tostr(t);
67 return s.empty() ? s : " " + name + "=\"" + s + "\"";
70 string const write_attribute(string const & name, string const & t)
72 return t.empty() ? t : " " + name + "=\"" + t + "\"";
76 string const write_attribute(string const & name, bool const & b)
78 // we write only true attribute values so we remove a bit of the
79 // file format bloat for tabulars.
80 return b ? write_attribute(name, tostr(b)) : string();
84 string const write_attribute(string const & name, int const & i)
86 // we write only true attribute values so we remove a bit of the
87 // file format bloat for tabulars.
88 return i ? write_attribute(name, tostr(i)) : string();
92 string const write_attribute(string const & name, LyXLength const & value)
94 // we write only the value if we really have one same reson as above.
95 return value.zero() ? string() : write_attribute(name, value.asString());
99 string const tostr(LyXAlignment const & num)
104 case LYX_ALIGN_BLOCK:
108 case LYX_ALIGN_CENTER:
110 case LYX_ALIGN_RIGHT:
112 case LYX_ALIGN_LAYOUT:
114 case LYX_ALIGN_SPECIAL:
121 string const tostr(LyXTabular::VAlignment const & num)
124 case LyXTabular::LYX_VALIGN_TOP:
126 case LyXTabular::LYX_VALIGN_MIDDLE:
128 case LyXTabular::LYX_VALIGN_BOTTOM:
135 string const tostr(LyXTabular::BoxType const & num)
138 case LyXTabular::BOX_NONE:
140 case LyXTabular::BOX_PARBOX:
142 case LyXTabular::BOX_MINIPAGE:
149 // I would have liked a fromstr template a lot better. (Lgb)
150 bool string2type(string const str, LyXAlignment & num)
153 num = LYX_ALIGN_NONE;
154 else if (str == "block")
155 num = LYX_ALIGN_BLOCK;
156 else if (str == "left")
157 num = LYX_ALIGN_LEFT;
158 else if (str == "center")
159 num = LYX_ALIGN_CENTER;
160 else if (str == "right")
161 num = LYX_ALIGN_RIGHT;
168 bool string2type(string const str, LyXTabular::VAlignment & num)
171 num = LyXTabular::LYX_VALIGN_TOP;
172 else if (str == "middle" )
173 num = LyXTabular::LYX_VALIGN_MIDDLE;
174 else if (str == "bottom")
175 num = LyXTabular::LYX_VALIGN_BOTTOM;
182 bool string2type(string const str, LyXTabular::BoxType & num)
185 num = LyXTabular::BOX_NONE;
186 else if (str == "parbox")
187 num = LyXTabular::BOX_PARBOX;
188 else if (str == "minipage")
189 num = LyXTabular::BOX_MINIPAGE;
196 bool string2type(string const str, bool & num)
200 else if (str == "false")
208 bool getTokenValue(string const & str, const char * token, string & ret)
211 size_t token_length = strlen(token);
212 string::size_type pos = str.find(token);
214 if (pos == string::npos || pos + token_length + 1 >= str.length()
215 || str[pos + token_length] != '=')
217 pos += token_length + 1;
219 if (ch != '"' && ch != '\'') { // only read till next space
223 while (pos < str.length() - 1 && str[++pos] != ch)
230 bool getTokenValue(string const & str, const char * token, int & num)
234 if (!getTokenValue(str, token, tmp))
241 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
244 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::setOwner(InsetTabular * inset)
392 for (int i = 0; i < rows_; ++i) {
393 for (int j = 0; j < columns_; ++j) {
394 cell_info[i][j].inset.setOwner(inset);
395 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
401 void LyXTabular::appendRow(BufferParams const & bp, int cell)
405 int row = row_of_cell(cell);
407 row_vector::iterator rit = row_info.begin() + row;
408 row_info.insert(rit, rowstruct());
409 // now set the values of the row before
410 row_info[row] = row_info[row + 1];
412 cell_vvector old(rows_ - 1);
413 for (int i = 0; i < rows_ - 1; ++i)
414 swap(cell_info[i], old[i]);
416 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
418 for (int i = 0; i <= row; ++i)
419 swap(cell_info[i], old[i]);
420 for (int i = row + 2; i < rows_; ++i)
421 swap(cell_info[i], old[i - 1]);
423 if (bp.tracking_changes)
424 for (int j = 0; j < columns_; ++j)
425 cell_info[row + 1][j].inset.markNew(true);
427 set_row_column_number_info();
431 void LyXTabular::deleteRow(int row)
433 // Not allowed to delete last row
437 row_info.erase(row_info.begin() + row);
438 cell_info.erase(cell_info.begin() + row);
444 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
448 int const column = column_of_cell(cell);
449 column_vector::iterator cit = column_info.begin() + column + 1;
450 column_info.insert(cit, columnstruct());
451 // set the column values of the column before
452 column_info[column + 1] = column_info[column];
454 for (int i = 0; i < rows_; ++i) {
455 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
457 // care about multicolumns
458 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
459 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
461 if (column + 2 >= columns_
462 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
463 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
466 for (int i = 0; i < rows_; ++i) {
467 cell_info[i][column + 1].inset.clear(false);
468 if (bp.tracking_changes)
469 cell_info[i][column + 1].inset.markNew(true);
475 void LyXTabular::deleteColumn(int column)
477 // Not allowed to delete last column
481 column_info.erase(column_info.begin() + column);
482 for (int i = 0; i < rows_; ++i)
483 cell_info[i].erase(cell_info[i].begin() + column);
489 void LyXTabular::set_row_column_number_info()
492 for (int row = 0; row < rows_; ++row) {
493 for (int column = 0; column < columns_; ++column) {
494 if (cell_info[row][column].multicolumn
495 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
497 cell_info[row][column].cellno = numberofcells;
500 ++numberofcells; // because this is one more than as we start from 0
502 rowofcell.resize(numberofcells);
503 columnofcell.resize(numberofcells);
505 for (int row = 0, column = 0, c = 0;
506 c < numberofcells && row < rows_ && column < columns_;) {
508 columnofcell[c] = column;
512 } while (column < columns_ &&
513 cell_info[row][column].multicolumn
514 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
516 if (column == columns_) {
522 for (int row = 0; row < rows_; ++row) {
523 for (int column = 0; column < columns_; ++column) {
524 if (isPartOfMultiColumn(row,column))
526 cell_info[row][column].inset.setAutoBreakRows(
527 !getPWidth(getCellNumber(row, column)).zero());
533 int LyXTabular::getNumberOfCells() const
535 return numberofcells;
539 int LyXTabular::numberOfCellsInRow(int cell) const
541 int const row = row_of_cell(cell);
543 for (int i = 0; i < columns_; ++i)
544 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
550 // returns 1 if there is a topline, returns 0 if not
551 bool LyXTabular::topLine(int cell, bool onlycolumn) const
553 if (!onlycolumn && isMultiColumn(cell))
554 return cellinfo_of_cell(cell).top_line;
555 return row_info[row_of_cell(cell)].top_line;
559 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
561 if (!onlycolumn && isMultiColumn(cell))
562 return cellinfo_of_cell(cell).bottom_line;
563 return row_info[row_of_cell(cell)].bottom_line;
567 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
569 if (!onlycolumn && isMultiColumn(cell) &&
570 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
572 if (cellinfo_of_cell(cell).align_special.empty())
573 return cellinfo_of_cell(cell).left_line;
574 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
576 if (column_info[column_of_cell(cell)].align_special.empty())
577 return column_info[column_of_cell(cell)].left_line;
578 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
582 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
584 if (!onlycolumn && isMultiColumn(cell) &&
585 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
587 if (cellinfo_of_cell(cell).align_special.empty())
588 return cellinfo_of_cell(cell).right_line;
589 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
591 if (column_info[column_of_cell(cell)].align_special.empty())
592 return column_info[right_column_of_cell(cell)].right_line;
593 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
597 bool LyXTabular::topAlreadyDrawn(int cell) const
599 int row = row_of_cell(cell);
600 if (row > 0 && !getAdditionalHeight(row)) {
601 int column = column_of_cell(cell);
604 && cell_info[row][column].multicolumn
605 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
607 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
608 return row_info[row].bottom_line;
610 return cell_info[row][column].bottom_line;
616 bool LyXTabular::leftAlreadyDrawn(int cell) const
618 int column = column_of_cell(cell);
620 int row = row_of_cell(cell);
622 (cell_info[row][column].multicolumn ==
623 LyXTabular::CELL_PART_OF_MULTICOLUMN));
624 if (getAdditionalWidth(cell_info[row][column].cellno))
626 return rightLine(cell_info[row][column].cellno);
632 bool LyXTabular::isLastRow(int cell) const
634 return row_of_cell(cell) == rows_ - 1;
638 int LyXTabular::getAdditionalHeight(int row) const
640 if (!row || row >= rows_)
646 for (int column = 0; column < columns_ && bottom; ++column) {
647 switch (cell_info[row - 1][column].multicolumn) {
648 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
649 bottom = cell_info[row - 1][column].bottom_line;
651 case LyXTabular::CELL_NORMAL:
652 bottom = row_info[row - 1].bottom_line;
655 for (int column = 0; column < columns_ && top; ++column) {
656 switch (cell_info[row][column].multicolumn) {
657 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
658 top = cell_info[row][column].top_line;
660 case LyXTabular::CELL_NORMAL:
661 top = row_info[row].top_line;
665 return WIDTH_OF_LINE;
670 int LyXTabular::getAdditionalWidth(int cell) const
672 // internally already set in setWidthOfCell
673 // used to get it back in text.C
674 int const col = right_column_of_cell(cell);
675 int const row = row_of_cell(cell);
676 if (col < columns_ - 1 && rightLine(cell) &&
677 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
679 return WIDTH_OF_LINE;
685 // returns the maximum over all rows
686 int LyXTabular::getWidthOfColumn(int cell) const
688 int const column1 = column_of_cell(cell);
689 int const column2 = right_column_of_cell(cell);
691 for (int i = column1; i <= column2; ++i)
692 result += column_info[i].width_of_column;
697 int LyXTabular::getWidthOfTabular() const
699 return width_of_tabular;
703 // returns true if a complete update is necessary, otherwise false
704 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
706 if (!isMultiColumn(cell))
709 int const row = row_of_cell(cell);
710 int const column1 = column_of_cell(cell);
711 int const column2 = right_column_of_cell(cell);
712 int const old_val = cell_info[row][column2].width_of_cell;
714 // first set columns to 0 so we can calculate the right width
715 for (int i = column1; i <= column2; ++i) {
716 cell_info[row][i].width_of_cell = 0;
718 // set the width to MAX_WIDTH until width > 0
719 int width = new_width + 2 * WIDTH_OF_LINE;
721 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
722 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
723 width -= column_info[i].width_of_column;
726 cell_info[row][i].width_of_cell = width;
728 if (old_val != cell_info[row][column2].width_of_cell) {
729 // in this case we have to recalculate all multicolumn cells which
730 // have this column as one of theirs but not as last one
731 calculate_width_of_column_NMC(i);
732 recalculateMulticolumnsOfColumn(i);
733 calculate_width_of_column(i);
739 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
741 // the last column does not have to be recalculated because all
742 // multicolumns will have here there last multicolumn cell which
743 // always will have the whole rest of the width of the cell.
744 if (column > (columns_ - 2))
746 for(int row = 0; row < rows_; ++row) {
747 int mc = cell_info[row][column].multicolumn;
748 int nmc = cell_info[row][column+1].multicolumn;
749 // we only have to update multicolumns which do not have this
750 // column as their last column!
751 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
752 (mc == CELL_PART_OF_MULTICOLUMN &&
753 nmc == CELL_PART_OF_MULTICOLUMN))
755 int const cellno = cell_info[row][column].cellno;
756 setWidthOfMulticolCell(cellno,
757 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
763 // returns 1 if a complete update is necessary, otherwise 0
764 void LyXTabular::setWidthOfCell(int cell, int new_width)
766 int const row = row_of_cell(cell);
767 int const column1 = column_of_cell(cell);
772 if (rightLine(cell_info[row][column1].cellno, true) &&
773 column1 < columns_ - 1 &&
774 leftLine(cell_info[row][column1+1].cellno, true))
776 add_width = WIDTH_OF_LINE;
779 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
782 if (isMultiColumnReal(cell)) {
783 tmp = setWidthOfMulticolCell(cell, new_width);
785 width = new_width + 2 * WIDTH_OF_LINE + add_width;
786 cell_info[row][column1].width_of_cell = width;
787 tmp = calculate_width_of_column_NMC(column1);
789 recalculateMulticolumnsOfColumn(column1);
792 for (int i = 0; i < columns_; ++i)
793 calculate_width_of_column(i);
794 calculate_width_of_tabular();
799 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
801 if (!isMultiColumn(cell) || onlycolumn)
802 column_info[column_of_cell(cell)].alignment = align;
804 cellinfo_of_cell(cell).alignment = align;
808 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
810 if (!isMultiColumn(cell) || onlycolumn)
811 column_info[column_of_cell(cell)].valignment = align;
813 cellinfo_of_cell(cell).valignment = align;
817 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
819 int const j = column_of_cell(cell);
821 column_info[j].p_width = width;
822 for (int i = 0; i < rows_; ++i) {
823 int const cell = getCellNumber(i, j);
824 // because of multicolumns
825 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
830 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
832 if (!isMultiColumn(cell))
835 cellinfo_of_cell(cell).p_width = width;
836 getCellInset(cell).setAutoBreakRows(!width.zero());
841 void LyXTabular::setAlignSpecial(int cell, string const & special,
842 LyXTabular::Feature what)
844 if (what == SET_SPECIAL_MULTI)
845 cellinfo_of_cell(cell).align_special = special;
847 column_info[column_of_cell(cell)].align_special = special;
851 void LyXTabular::setAllLines(int cell, bool line)
853 setTopLine(cell, line);
854 setBottomLine(cell, line);
855 setRightLine(cell, line);
856 setLeftLine(cell, line);
860 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
862 int const row = row_of_cell(cell);
863 if (onlycolumn || !isMultiColumn(cell))
864 row_info[row].top_line = line;
866 cellinfo_of_cell(cell).top_line = line;
870 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
872 if (onlycolumn || !isMultiColumn(cell))
873 row_info[row_of_cell(cell)].bottom_line = line;
875 cellinfo_of_cell(cell).bottom_line = line;
879 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
881 if (onlycolumn || !isMultiColumn(cell))
882 column_info[column_of_cell(cell)].left_line = line;
884 cellinfo_of_cell(cell).left_line = line;
888 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
890 if (onlycolumn || !isMultiColumn(cell))
891 column_info[right_column_of_cell(cell)].right_line = line;
893 cellinfo_of_cell(cell).right_line = line;
897 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
899 if (!onlycolumn && isMultiColumn(cell))
900 return cellinfo_of_cell(cell).alignment;
901 return column_info[column_of_cell(cell)].alignment;
905 LyXTabular::VAlignment
906 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
908 if (!onlycolumn && isMultiColumn(cell))
909 return cellinfo_of_cell(cell).valignment;
910 return column_info[column_of_cell(cell)].valignment;
914 LyXLength const LyXTabular::getPWidth(int cell) const
916 if (isMultiColumn(cell))
917 return cellinfo_of_cell(cell).p_width;
918 return column_info[column_of_cell(cell)].p_width;
922 LyXLength const LyXTabular::getColumnPWidth(int cell) const
924 return column_info[column_of_cell(cell)].p_width;
928 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
930 if (isMultiColumn(cell))
931 return cellinfo_of_cell(cell).p_width;
936 string const LyXTabular::getAlignSpecial(int cell, int what) const
938 if (what == SET_SPECIAL_MULTI)
939 return cellinfo_of_cell(cell).align_special;
940 return column_info[column_of_cell(cell)].align_special;
944 int LyXTabular::getWidthOfCell(int cell) const
946 int const row = row_of_cell(cell);
947 int const column1 = column_of_cell(cell);
948 int const column2 = right_column_of_cell(cell);
950 for (int i = column1; i <= column2; ++i)
951 result += cell_info[row][i].width_of_cell;
956 int LyXTabular::getBeginningOfTextInCell(int cell) const
960 switch (getAlignment(cell)) {
961 case LYX_ALIGN_CENTER:
962 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
964 case LYX_ALIGN_RIGHT:
965 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
966 // + getAdditionalWidth(cell);
969 // LYX_ALIGN_LEFT: nothing :-)
979 bool LyXTabular::isFirstCellInRow(int cell) const
981 return column_of_cell(cell) == 0;
985 int LyXTabular::getFirstCellInRow(int row) const
989 return cell_info[row][0].cellno;
993 bool LyXTabular::isLastCellInRow(int cell) const
995 return right_column_of_cell(cell) == columns_ - 1;
999 int LyXTabular::getLastCellInRow(int row) const
1001 if (row > rows_ - 1)
1003 return cell_info[row][columns_-1].cellno;
1007 void LyXTabular::calculate_width_of_column(int column)
1010 for (int i = 0; i < rows_; ++i)
1011 maximum = max(cell_info[i][column].width_of_cell, maximum);
1012 column_info[column].width_of_column = maximum;
1017 // Calculate the columns regarding ONLY the normal cells and if this
1018 // column is inside a multicolumn cell then use it only if its the last
1019 // column of this multicolumn cell as this gives an added width to the
1020 // column, all the rest should be adapted!
1022 bool LyXTabular::calculate_width_of_column_NMC(int column)
1024 int const old_column_width = column_info[column].width_of_column;
1026 for (int i = 0; i < rows_; ++i) {
1027 int cell = getCellNumber(i, column);
1028 bool ismulti = isMultiColumnReal(cell);
1029 if ((!ismulti || column == right_column_of_cell(cell)) &&
1030 cell_info[i][column].width_of_cell > max)
1032 max = cell_info[i][column].width_of_cell;
1035 column_info[column].width_of_column = max;
1036 return column_info[column].width_of_column != old_column_width;
1040 void LyXTabular::calculate_width_of_tabular()
1042 width_of_tabular = 0;
1043 for (int i = 0; i < columns_; ++i)
1044 width_of_tabular += column_info[i].width_of_column;
1048 int LyXTabular::row_of_cell(int cell) const
1050 if (cell >= numberofcells)
1054 return rowofcell[cell];
1058 int LyXTabular::column_of_cell(int cell) const
1060 if (cell >= numberofcells)
1061 return columns_ - 1;
1064 return columnofcell[cell];
1068 int LyXTabular::right_column_of_cell(int cell) const
1070 int const row = row_of_cell(cell);
1071 int column = column_of_cell(cell);
1072 while (column < columns_ - 1 &&
1073 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1079 void LyXTabular::write(Buffer const & buf, ostream & os) const
1083 << write_attribute("version", 3)
1084 << write_attribute("rows", rows_)
1085 << write_attribute("columns", columns_)
1087 // global longtable options
1089 << write_attribute("rotate", rotate)
1090 << write_attribute("islongtable", is_long_tabular)
1091 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1092 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1093 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1094 << write_attribute("headTopDL", endhead.topDL)
1095 << write_attribute("headBottomDL", endhead.bottomDL)
1096 << write_attribute("footTopDL", endfoot.topDL)
1097 << write_attribute("footBottomDL", endfoot.bottomDL)
1098 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1099 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1100 << write_attribute("lastFootEmpty", endlastfoot.empty)
1102 for (int j = 0; j < columns_; ++j) {
1104 << write_attribute("alignment", column_info[j].alignment)
1105 << write_attribute("valignment", column_info[j].valignment)
1106 << write_attribute("leftline", column_info[j].left_line)
1107 << write_attribute("rightline", column_info[j].right_line)
1108 << write_attribute("width", column_info[j].p_width.asString())
1109 << write_attribute("special", column_info[j].align_special)
1112 for (int i = 0; i < rows_; ++i) {
1114 << write_attribute("topline", row_info[i].top_line)
1115 << write_attribute("bottomline", row_info[i].bottom_line)
1116 << write_attribute("endhead", row_info[i].endhead)
1117 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1118 << write_attribute("endfoot", row_info[i].endfoot)
1119 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1120 << write_attribute("newpage", row_info[i].newpage)
1122 for (int j = 0; j < columns_; ++j) {
1124 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1125 << write_attribute("alignment", cell_info[i][j].alignment)
1126 << write_attribute("valignment", cell_info[i][j].valignment)
1127 << write_attribute("topline", cell_info[i][j].top_line)
1128 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1129 << write_attribute("leftline", cell_info[i][j].left_line)
1130 << write_attribute("rightline", cell_info[i][j].right_line)
1131 << write_attribute("rotate", cell_info[i][j].rotate)
1132 << write_attribute("usebox", cell_info[i][j].usebox)
1133 << write_attribute("width", cell_info[i][j].p_width)
1134 << write_attribute("special", cell_info[i][j].align_special)
1136 os << "\\begin_inset ";
1137 cell_info[i][j].inset.write(buf, os);
1138 os << "\n\\end_inset \n"
1143 os << "</lyxtabular>\n";
1147 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1151 row_info[--hr].endhead = true;
1153 // set firstheader info
1154 if (fhr && fhr < rows_) {
1155 if (row_info[fhr].endhead) {
1157 row_info[--fhr].endfirsthead = true;
1158 row_info[fhr].endhead = false;
1160 } else if (row_info[fhr - 1].endhead) {
1161 endfirsthead.empty = true;
1163 while (fhr > 0 && !row_info[--fhr].endhead) {
1164 row_info[fhr].endfirsthead = true;
1169 if (fr && fr < rows_) {
1170 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1171 while (fr > 0 && !row_info[--fr].endhead) {
1172 row_info[fr].endfoot = true;
1173 row_info[fr].endhead = false;
1175 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1176 while (fr > 0 && !row_info[--fr].endfirsthead) {
1177 row_info[fr].endfoot = true;
1178 row_info[fr].endfirsthead = false;
1180 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1181 while (fr > 0 && !row_info[--fr].endhead &&
1182 !row_info[fr].endfirsthead)
1184 row_info[fr].endfoot = true;
1188 // set lastfooter info
1189 if (lfr && lfr < rows_) {
1190 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1191 while (lfr > 0 && !row_info[--lfr].endhead) {
1192 row_info[lfr].endlastfoot = true;
1193 row_info[lfr].endhead = false;
1195 } else if (row_info[lfr].endfirsthead &&
1196 row_info[lfr - 1].endfirsthead)
1198 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1199 row_info[lfr].endlastfoot = true;
1200 row_info[lfr].endfirsthead = false;
1202 } else if (row_info[lfr].endfoot
1203 && row_info[lfr - 1].endfoot) {
1204 while (lfr > 0 && !row_info[--lfr].endfoot) {
1205 row_info[lfr].endlastfoot = true;
1206 row_info[lfr].endfoot = false;
1208 } else if (!row_info[fr - 1].endhead
1209 && !row_info[fr - 1].endfirsthead &&
1210 !row_info[fr - 1].endfoot)
1213 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1214 !row_info[lfr].endfoot)
1216 row_info[lfr].endlastfoot = true;
1218 } else if (haveLTFoot()) {
1219 endlastfoot.empty = true;
1225 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1228 istream & is = lex.getStream();
1230 l_getline(is, line);
1231 if (!prefixIs(line, "<lyxtabular ")
1232 && !prefixIs(line, "<LyXTabular ")) {
1233 BOOST_ASSERT(false);
1238 if (!getTokenValue(line, "version", version))
1240 BOOST_ASSERT(version >= 2);
1243 if (!getTokenValue(line, "rows", rows_arg))
1246 if (!getTokenValue(line, "columns", columns_arg))
1248 init(buf.params(), rows_arg, columns_arg);
1249 l_getline(is, line);
1250 if (!prefixIs(line, "<features")) {
1251 lyxerr << "Wrong tabular format (expected <features ...> got"
1252 << line << ')' << endl;
1255 getTokenValue(line, "rotate", rotate);
1256 getTokenValue(line, "islongtable", is_long_tabular);
1257 // compatibility read for old longtable options. Now we can make any
1258 // row part of the header/footer type we want before it was strict
1259 // sequential from the first row down (as LaTeX does it!). So now when
1260 // we find a header/footer line we have to go up the rows and set it
1261 // on all preceding rows till the first or one with already a h/f option
1262 // set. If we find a firstheader on the same line as a header or a
1263 // lastfooter on the same line as a footer then this should be set empty.
1271 getTokenValue(line, "endhead", hrow);
1272 getTokenValue(line, "endfirsthead", fhrow);
1273 getTokenValue(line, "endfoot", frow);
1274 getTokenValue(line, "endlastfoot", lfrow);
1275 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1277 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1278 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1279 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1280 getTokenValue(line, "headTopDL", endhead.topDL);
1281 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1282 getTokenValue(line, "footTopDL", endfoot.topDL);
1283 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1284 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1285 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1286 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1288 for (int j = 0; j < columns_; ++j) {
1290 if (!prefixIs(line,"<column")) {
1291 lyxerr << "Wrong tabular format (expected <column ...> got"
1292 << line << ')' << endl;
1295 getTokenValue(line, "alignment", column_info[j].alignment);
1296 getTokenValue(line, "valignment", column_info[j].valignment);
1297 getTokenValue(line, "leftline", column_info[j].left_line);
1298 getTokenValue(line, "rightline", column_info[j].right_line);
1299 getTokenValue(line, "width", column_info[j].p_width);
1300 getTokenValue(line, "special", column_info[j].align_special);
1303 for (int i = 0; i < rows_; ++i) {
1304 l_getline(is, line);
1305 if (!prefixIs(line, "<row")) {
1306 lyxerr << "Wrong tabular format (expected <row ...> got"
1307 << line << ')' << endl;
1310 getTokenValue(line, "topline", row_info[i].top_line);
1311 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1312 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1313 getTokenValue(line, "endhead", row_info[i].endhead);
1314 getTokenValue(line, "endfoot", row_info[i].endfoot);
1315 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1316 getTokenValue(line, "newpage", row_info[i].newpage);
1317 for (int j = 0; j < columns_; ++j) {
1318 l_getline(is, line);
1319 if (!prefixIs(line, "<cell")) {
1320 lyxerr << "Wrong tabular format (expected <cell ...> got"
1321 << line << ')' << endl;
1324 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1325 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1326 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1327 getTokenValue(line, "topline", cell_info[i][j].top_line);
1328 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1329 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1330 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1331 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1332 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1333 getTokenValue(line, "width", cell_info[i][j].p_width);
1334 getTokenValue(line, "special", cell_info[i][j].align_special);
1335 l_getline(is, line);
1336 if (prefixIs(line, "\\begin_inset")) {
1337 cell_info[i][j].inset.read(buf, lex);
1338 l_getline(is, line);
1340 if (!prefixIs(line, "</cell>")) {
1341 lyxerr << "Wrong tabular format (expected </cell> got"
1342 << line << ')' << endl;
1346 l_getline(is, line);
1347 if (!prefixIs(line, "</row>")) {
1348 lyxerr << "Wrong tabular format (expected </row> got"
1349 << line << ')' << endl;
1353 while (!prefixIs(line, "</lyxtabular>")) {
1354 l_getline(is, line);
1356 set_row_column_number_info();
1360 bool LyXTabular::isMultiColumn(int cell) const
1362 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1366 bool LyXTabular::isMultiColumnReal(int cell) const
1368 return column_of_cell(cell) != right_column_of_cell(cell) &&
1369 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1373 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1375 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1379 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1381 cellstruct & cs = cellinfo_of_cell(cell);
1382 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1383 cs.alignment = column_info[column_of_cell(cell)].alignment;
1384 cs.top_line = row_info[row_of_cell(cell)].top_line;
1385 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1386 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1387 for (int i = 1; i < number; ++i) {
1388 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1389 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1390 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs());
1391 cs1.inset.clear(false);
1393 set_row_column_number_info();
1397 int LyXTabular::cells_in_multicolumn(int cell) const
1399 int const row = row_of_cell(cell);
1400 int column = column_of_cell(cell);
1403 while (column < columns_ &&
1404 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1413 int LyXTabular::unsetMultiColumn(int cell)
1415 int const row = row_of_cell(cell);
1416 int column = column_of_cell(cell);
1420 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1421 cell_info[row][column].multicolumn = CELL_NORMAL;
1423 while (column < columns_ &&
1424 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1426 cell_info[row][column].multicolumn = CELL_NORMAL;
1431 set_row_column_number_info();
1436 void LyXTabular::setLongTabular(bool what)
1438 is_long_tabular = what;
1442 bool LyXTabular::isLongTabular() const
1444 return is_long_tabular;
1448 void LyXTabular::setRotateTabular(bool flag)
1454 bool LyXTabular::getRotateTabular() const
1460 void LyXTabular::setRotateCell(int cell, bool flag)
1462 cellinfo_of_cell(cell).rotate = flag;
1466 bool LyXTabular::getRotateCell(int cell) const
1468 return cellinfo_of_cell(cell).rotate;
1472 bool LyXTabular::needRotating() const
1476 for (int i = 0; i < rows_; ++i)
1477 for (int j = 0; j < columns_; ++j)
1478 if (cell_info[i][j].rotate)
1484 bool LyXTabular::isLastCell(int cell) const
1486 if (cell + 1 < numberofcells)
1492 int LyXTabular::getCellAbove(int cell) const
1494 if (row_of_cell(cell) > 0)
1495 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1500 int LyXTabular::getCellBelow(int cell) const
1502 if (row_of_cell(cell) + 1 < rows_)
1503 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1508 int LyXTabular::getLastCellAbove(int cell) const
1510 if (row_of_cell(cell) <= 0)
1512 if (!isMultiColumn(cell))
1513 return getCellAbove(cell);
1514 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1518 int LyXTabular::getLastCellBelow(int cell) const
1520 if (row_of_cell(cell) + 1 >= rows_)
1522 if (!isMultiColumn(cell))
1523 return getCellBelow(cell);
1524 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1528 int LyXTabular::getCellNumber(int row, int column) const
1530 BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1531 return cell_info[row][column].cellno;
1535 void LyXTabular::setUsebox(int cell, BoxType type)
1537 cellinfo_of_cell(cell).usebox = type;
1541 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1543 if (column_info[column_of_cell(cell)].p_width.zero() &&
1544 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1546 if (cellinfo_of_cell(cell).usebox > 1)
1547 return cellinfo_of_cell(cell).usebox;
1548 return useParbox(cell);
1553 // This are functions used for the longtable support
1555 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1560 row_info[row].endfirsthead = flag;
1564 row_info[row].endhead = flag;
1569 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1572 hd.set = haveLTHead();
1573 return row_info[row].endhead;
1577 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1580 hd.set = haveLTFirstHead();
1581 return row_info[row].endfirsthead;
1585 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1590 row_info[row].endlastfoot = flag;
1594 row_info[row].endfoot = flag;
1599 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1602 fd.set = haveLTFoot();
1603 return row_info[row].endfoot;
1607 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1610 fd.set = haveLTLastFoot();
1611 return row_info[row].endlastfoot;
1615 void LyXTabular::setLTNewPage(int row, bool what)
1617 row_info[row].newpage = what;
1621 bool LyXTabular::getLTNewPage(int row) const
1623 return row_info[row].newpage;
1627 bool LyXTabular::haveLTHead() const
1629 for (int i = 0; i < rows_; ++i)
1630 if (row_info[i].endhead)
1636 bool LyXTabular::haveLTFirstHead() const
1638 if (endfirsthead.empty)
1640 for (int i = 0; i < rows_; ++i)
1641 if (row_info[i].endfirsthead)
1647 bool LyXTabular::haveLTFoot() const
1649 for (int i = 0; i < rows_; ++i)
1650 if (row_info[i].endfoot)
1656 bool LyXTabular::haveLTLastFoot() const
1658 if (endlastfoot.empty)
1660 for (int i = 0; i < rows_; ++i)
1661 if (row_info[i].endlastfoot)
1667 // end longtable support functions
1669 void LyXTabular::setAscentOfRow(int row, int height)
1671 if (row >= rows_ || row_info[row].ascent_of_row == height)
1673 row_info[row].ascent_of_row = height;
1677 void LyXTabular::setDescentOfRow(int row, int height)
1679 if (row >= rows_ || row_info[row].descent_of_row == height)
1681 row_info[row].descent_of_row = height;
1685 int LyXTabular::getAscentOfRow(int row) const
1689 return row_info[row].ascent_of_row;
1693 int LyXTabular::getDescentOfRow(int row) const
1697 return row_info[row].descent_of_row;
1701 int LyXTabular::getHeightOfTabular() const
1704 for (int row = 0; row < rows_; ++row)
1705 height += getAscentOfRow(row) + getDescentOfRow(row) +
1706 getAdditionalHeight(row);
1711 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1713 if (row >= rows_ || column >= columns_)
1715 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1719 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1721 if (row < 0 || row >= rows_)
1724 int const fcell = getFirstCellInRow(row);
1725 int const n = numberOfCellsInRow(fcell) + fcell;
1728 for (int i = fcell; i < n; ++i) {
1732 if (tmp == n - fcell) {
1735 for (int i = fcell; i < n; ++i) {
1738 << column_of_cell(i) + 1
1740 << right_column_of_cell(i) + 1
1752 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1754 if (row < 0 || row >= rows_)
1757 int const fcell = getFirstCellInRow(row);
1758 int const n = numberOfCellsInRow(fcell) + fcell;
1761 for (int i = fcell; i < n; ++i) {
1765 if (tmp == n - fcell) {
1768 for (int i = fcell; i < n; ++i) {
1769 if (bottomLine(i)) {
1771 << column_of_cell(i) + 1
1773 << right_column_of_cell(i) + 1
1785 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1789 if (getRotateCell(cell)) {
1790 os << "\\begin{sideways}\n";
1793 if (isMultiColumn(cell)) {
1794 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1795 if (!cellinfo_of_cell(cell).align_special.empty()) {
1796 os << cellinfo_of_cell(cell).align_special << "}{";
1798 if (leftLine(cell) &&
1799 (isFirstCellInRow(cell) ||
1800 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1801 !rightLine(cell - 1, true))))
1805 if (!getPWidth(cell).zero()) {
1806 switch (getVAlignment(cell)) {
1807 case LYX_VALIGN_TOP:
1810 case LYX_VALIGN_MIDDLE:
1813 case LYX_VALIGN_BOTTOM:
1818 << getPWidth(cell).asLatexString()
1821 switch (getAlignment(cell)) {
1822 case LYX_ALIGN_LEFT:
1825 case LYX_ALIGN_RIGHT:
1833 if (rightLine(cell))
1835 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1841 if (getUsebox(cell) == BOX_PARBOX) {
1843 switch (getVAlignment(cell)) {
1844 case LYX_VALIGN_TOP:
1847 case LYX_VALIGN_MIDDLE:
1850 case LYX_VALIGN_BOTTOM:
1854 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1855 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1856 os << "\\begin{minipage}[";
1857 switch (getVAlignment(cell)) {
1858 case LYX_VALIGN_TOP:
1861 case LYX_VALIGN_MIDDLE:
1864 case LYX_VALIGN_BOTTOM:
1868 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1875 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1880 if (getUsebox(cell) == BOX_PARBOX)
1882 else if (getUsebox(cell) == BOX_MINIPAGE) {
1883 os << "%\n\\end{minipage}";
1886 if (isMultiColumn(cell)) {
1889 if (getRotateCell(cell)) {
1890 os << "%\n\\end{sideways}";
1897 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1898 OutputParams const & runparams) const
1900 if (!is_long_tabular)
1904 // output header info
1906 if (endhead.topDL) {
1910 for (int i = 0; i < rows_; ++i) {
1911 if (row_info[i].endhead) {
1912 ret += TeXRow(os, i, buf, runparams);
1915 if (endhead.bottomDL) {
1919 os << "\\endhead\n";
1921 if (endfirsthead.empty) {
1922 os << "\\endfirsthead\n";
1926 // output firstheader info
1927 if (haveLTFirstHead()) {
1928 if (endfirsthead.topDL) {
1932 for (int i = 0; i < rows_; ++i) {
1933 if (row_info[i].endfirsthead) {
1934 ret += TeXRow(os, i, buf, runparams);
1937 if (endfirsthead.bottomDL) {
1941 os << "\\endfirsthead\n";
1944 // output footer info
1946 if (endfoot.topDL) {
1950 for (int i = 0; i < rows_; ++i) {
1951 if (row_info[i].endfoot) {
1952 ret += TeXRow(os, i, buf, runparams);
1955 if (endfoot.bottomDL) {
1959 os << "\\endfoot\n";
1961 if (endlastfoot.empty) {
1962 os << "\\endlastfoot\n";
1966 // output lastfooter info
1967 if (haveLTLastFoot()) {
1968 if (endlastfoot.topDL) {
1972 for (int i = 0; i < rows_; ++i) {
1973 if (row_info[i].endlastfoot) {
1974 ret += TeXRow(os, i, buf, runparams);
1977 if (endlastfoot.bottomDL) {
1981 os << "\\endlastfoot\n";
1988 bool LyXTabular::isValidRow(int row) const
1990 if (!is_long_tabular)
1992 return !row_info[row].endhead && !row_info[row].endfirsthead &&
1993 !row_info[row].endfoot && !row_info[row].endlastfoot;
1997 int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
1998 OutputParams const & runparams) const
2001 int cell = getCellNumber(i, 0);
2003 ret += TeXTopHLine(os, i);
2004 for (int j = 0; j < columns_; ++j) {
2005 if (isPartOfMultiColumn(i, j))
2007 ret += TeXCellPreamble(os, cell);
2008 InsetText & inset = getCellInset(cell);
2010 bool rtl = inset.text_.isRTL(inset.paragraphs().front())
2011 && !inset.paragraphs().begin()->empty()
2012 && getPWidth(cell).zero();
2016 ret += inset.latex(buf, os, runparams);
2020 ret += TeXCellPostamble(os, cell);
2021 if (!isLastCellInRow(cell)) { // not last cell in row
2027 os << "\\tabularnewline\n";
2029 ret += TeXBottomHLine(os, i);
2034 int LyXTabular::latex(Buffer const & buf, ostream & os,
2035 OutputParams const & runparams) const
2039 //+---------------------------------------------------------------------
2040 //+ first the opening preamble +
2041 //+---------------------------------------------------------------------
2044 os << "\\begin{sideways}\n";
2047 if (is_long_tabular)
2048 os << "\\begin{longtable}{";
2050 os << "\\begin{tabular}{";
2051 for (int i = 0; i < columns_; ++i) {
2052 if (!column_info[i].align_special.empty()) {
2053 os << column_info[i].align_special;
2055 if (column_info[i].left_line)
2057 if (!column_info[i].p_width.zero()) {
2058 switch (column_info[i].alignment) {
2059 case LYX_ALIGN_LEFT:
2060 os << ">{\\raggedright}";
2062 case LYX_ALIGN_RIGHT:
2063 os << ">{\\raggedleft}";
2065 case LYX_ALIGN_CENTER:
2066 os << ">{\\centering}";
2068 case LYX_ALIGN_NONE:
2069 case LYX_ALIGN_BLOCK:
2070 case LYX_ALIGN_LAYOUT:
2071 case LYX_ALIGN_SPECIAL:
2075 switch (column_info[i].valignment) {
2076 case LYX_VALIGN_TOP:
2079 case LYX_VALIGN_MIDDLE:
2082 case LYX_VALIGN_BOTTOM:
2087 << column_info[i].p_width.asLatexString()
2090 switch (column_info[i].alignment) {
2091 case LYX_ALIGN_LEFT:
2094 case LYX_ALIGN_RIGHT:
2102 if (column_info[i].right_line)
2109 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2111 //+---------------------------------------------------------------------
2112 //+ the single row and columns (cells) +
2113 //+---------------------------------------------------------------------
2115 for (int i = 0; i < rows_; ++i) {
2116 if (isValidRow(i)) {
2117 ret += TeXRow(os, i, buf, runparams);
2118 if (is_long_tabular && row_info[i].newpage) {
2119 os << "\\newpage\n";
2125 //+---------------------------------------------------------------------
2126 //+ the closing of the tabular +
2127 //+---------------------------------------------------------------------
2129 if (is_long_tabular)
2130 os << "\\end{longtable}";
2132 os << "\\end{tabular}";
2134 os << "\n\\end{sideways}";
2142 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2143 const OutputParams & runparams) const
2145 os << "<tabular ca=\"";
2146 for (int i = 0; i < columns_; ++i) {
2147 switch (column_info[i].alignment) {
2148 case LYX_ALIGN_LEFT:
2151 case LYX_ALIGN_RIGHT:
2162 for (int i = 0; i < rows_; ++i) {
2163 for (int j = 0; j < columns_; ++j) {
2164 if (isPartOfMultiColumn(i, j))
2166 InsetText & inset = getCellInset(cell);
2168 ret += inset.linuxdoc(buf, os, runparams);
2170 if (isLastCellInRow(cell)) {
2179 os << "</tabular>\n";
2184 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row,
2185 OutputParams const & runparams) const
2188 int cell = getFirstCellInRow(row);
2191 for (int j = 0; j < columns_; ++j) {
2192 if (isPartOfMultiColumn(row, j))
2195 os << "<entry align=\"";
2196 switch (getAlignment(cell)) {
2197 case LYX_ALIGN_LEFT:
2200 case LYX_ALIGN_RIGHT:
2208 os << "\" valign=\"";
2209 switch (getVAlignment(cell)) {
2210 case LYX_VALIGN_TOP:
2213 case LYX_VALIGN_BOTTOM:
2216 case LYX_VALIGN_MIDDLE:
2221 if (isMultiColumn(cell)) {
2222 os << " namest=\"col" << j << "\" ";
2223 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2227 OutputParams runp = runparams;
2228 runp.mixed_content = true;
2229 ret += getCellInset(cell).docbook(buf, os, runp);
2238 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2239 OutputParams const & runparams) const
2243 //+---------------------------------------------------------------------
2244 //+ first the opening preamble +
2245 //+---------------------------------------------------------------------
2247 os << "<tgroup cols=\"" << columns_
2248 << "\" colsep=\"1\" rowsep=\"1\">\n";
2250 for (int i = 0; i < columns_; ++i) {
2251 os << "<colspec colname=\"col" << i << "\" align=\"";
2252 switch (column_info[i].alignment) {
2253 case LYX_ALIGN_LEFT:
2256 case LYX_ALIGN_RIGHT:
2267 //+---------------------------------------------------------------------
2268 //+ Long Tabular case +
2269 //+---------------------------------------------------------------------
2271 // output header info
2272 if (haveLTHead() || haveLTFirstHead()) {
2275 for (int i = 0; i < rows_; ++i) {
2276 if (row_info[i].endhead || row_info[i].endfirsthead) {
2277 ret += docbookRow(buf, os, i, runparams);
2283 // output footer info
2284 if (haveLTFoot() || haveLTLastFoot()) {
2287 for (int i = 0; i < rows_; ++i) {
2288 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2289 ret += docbookRow(buf, os, i, runparams);
2296 //+---------------------------------------------------------------------
2297 //+ the single row and columns (cells) +
2298 //+---------------------------------------------------------------------
2302 for (int i = 0; i < rows_; ++i) {
2303 if (isValidRow(i)) {
2304 ret += docbookRow(buf, os, i, runparams);
2309 //+---------------------------------------------------------------------
2310 //+ the closing of the tabular +
2311 //+---------------------------------------------------------------------
2320 int LyXTabular::asciiTopHLine(ostream & os, int row,
2321 vector<unsigned int> const & clen) const
2323 int const fcell = getFirstCellInRow(row);
2324 int const n = numberOfCellsInRow(fcell) + fcell;
2327 for (int i = fcell; i < n; ++i) {
2337 for (int i = fcell; i < n; ++i) {
2348 int column = column_of_cell(i);
2349 int len = clen[column];
2350 while (isPartOfMultiColumn(row, ++column))
2351 len += clen[column] + 4;
2352 os << string(len, ch);
2367 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2368 vector<unsigned int> const & clen) const
2370 int const fcell = getFirstCellInRow(row);
2371 int const n = numberOfCellsInRow(fcell) + fcell;
2374 for (int i = fcell; i < n; ++i) {
2375 if (bottomLine(i)) {
2384 for (int i = fcell; i < n; ++i) {
2385 if (bottomLine(i)) {
2395 int column = column_of_cell(i);
2396 int len = clen[column];
2397 while (isPartOfMultiColumn(row, ++column))
2398 len += clen[column] + 4;
2399 os << string(len, ch);
2400 if (bottomLine(i)) {
2414 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2415 OutputParams const & runparams,
2416 int cell, int row, int column,
2417 vector<unsigned int> const & clen,
2418 bool onlydata) const
2421 int ret = getCellInset(cell).plaintext(buf, sstr, runparams);
2433 unsigned int len1 = sstr.str().length();
2434 unsigned int len2 = clen[column];
2435 while (isPartOfMultiColumn(row, ++column))
2436 len2 += clen[column] + 4;
2439 switch (getAlignment(cell)) {
2441 case LYX_ALIGN_LEFT:
2444 case LYX_ALIGN_RIGHT:
2448 case LYX_ALIGN_CENTER:
2454 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2456 if (rightLine(cell))
2465 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2466 OutputParams const & runparams,
2468 bool onlydata, unsigned char delim) const
2472 // first calculate the width of the single columns
2473 vector<unsigned int> clen(columns_);
2476 // first all non (real) multicolumn cells!
2477 for (int j = 0; j < columns_; ++j) {
2479 for (int i = 0; i < rows_; ++i) {
2480 int cell = getCellNumber(i, j);
2481 if (isMultiColumnReal(cell))
2484 getCellInset(cell).plaintext(buf, sstr, runparams);
2485 if (clen[j] < sstr.str().length())
2486 clen[j] = sstr.str().length();
2489 // then all (real) multicolumn cells!
2490 for (int j = 0; j < columns_; ++j) {
2491 for (int i = 0; i < rows_; ++i) {
2492 int cell = getCellNumber(i, j);
2493 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2496 getCellInset(cell).plaintext(buf, sstr, runparams);
2497 int len = int(sstr.str().length());
2498 int const n = cells_in_multicolumn(cell);
2499 for (int k = j; len > 0 && k < j + n - 1; ++k)
2501 if (len > int(clen[j + n - 1]))
2502 clen[j + n - 1] = len;
2507 for (int i = 0; i < rows_; ++i) {
2508 if (!onlydata && asciiTopHLine(os, i, clen))
2509 for (int j = 0; j < depth; ++j)
2511 for (int j = 0; j < columns_; ++j) {
2512 if (isPartOfMultiColumn(i, j))
2514 if (onlydata && j > 0)
2516 ret += asciiPrintCell(buf, os, runparams,
2517 cell, i, j, clen, onlydata);
2522 for (int j = 0; j < depth; ++j)
2524 if (asciiBottomHLine(os, i, clen))
2525 for (int j = 0; j < depth; ++j)
2533 InsetText & LyXTabular::getCellInset(int cell) const
2535 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2539 InsetText & LyXTabular::getCellInset(int row, int column) const
2541 return cell_info[row][column].inset;
2545 int LyXTabular::getCellFromInset(InsetBase const * inset) const
2547 // is this inset part of the tabular?
2549 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2550 BOOST_ASSERT(false);
2553 for (int cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2554 if (&getCellInset(cell) == inset) {
2555 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2556 << "cell=" << cell << endl;
2560 // We should have found a cell at this point
2561 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2562 << inset << " not found!" << endl;
2563 BOOST_ASSERT(false);
2569 void LyXTabular::validate(LaTeXFeatures & features) const
2571 features.require("NeedTabularnewline");
2572 if (isLongTabular())
2573 features.require("longtable");
2575 features.require("rotating");
2576 for (int cell = 0; cell < numberofcells; ++cell) {
2577 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2578 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2579 features.require("array");
2580 getCellInset(cell).validate(features);
2585 void LyXTabular::getLabelList(Buffer const & buffer,
2586 std::vector<string> & list) const
2588 for (int i = 0; i < rows_; ++i)
2589 for (int j = 0; j < columns_; ++j)
2590 getCellInset(i, j).getLabelList(buffer, list);
2594 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2596 ParagraphList const & parlist = getCellInset(cell).paragraphs();
2597 ParagraphList::const_iterator cit = parlist.begin();
2598 ParagraphList::const_iterator end = parlist.end();
2600 for (; cit != end; ++cit)
2601 for (int i = 0; i < cit->size(); ++i)
2602 if (cit->isNewline(i))