3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
23 #include "bufferparams.h"
25 #include "LaTeXFeatures.h"
27 #include "paragraph.h"
29 #include "insets/insettabular.h"
31 #include "support/lstrings.h"
32 #include "support/tostr.h"
34 #include "support/std_sstream.h"
36 using lyx::support::ltrim;
37 using lyx::support::prefixIs;
38 using lyx::support::rtrim;
39 using lyx::support::strToInt;
40 using lyx::support::suffixIs;
49 using std::ostringstream;
53 #ifndef CXX_GLOBAL_CSTD
60 int const WIDTH_OF_LINE = 5;
63 string const write_attribute(string const & name, T const & t)
65 string const s = tostr(t);
66 return s.empty() ? s : " " + name + "=\"" + s + "\"";
69 string const write_attribute(string const & name, string const & t)
71 return t.empty() ? t : " " + name + "=\"" + t + "\"";
75 string const write_attribute(string const & name, bool const & b)
77 // we write only true attribute values so we remove a bit of the
78 // file format bloat for tabulars.
79 return b ? write_attribute(name, tostr(b)) : string();
83 string const write_attribute(string const & name, int const & i)
85 // we write only true attribute values so we remove a bit of the
86 // file format bloat for tabulars.
87 return i ? write_attribute(name, tostr(i)) : string();
91 string const write_attribute(string const & name, LyXLength const & value)
93 // we write only the value if we really have one same reson as above.
94 return value.zero() ? string() : write_attribute(name, value.asString());
98 string const tostr(LyXAlignment const & num)
103 case LYX_ALIGN_BLOCK:
107 case LYX_ALIGN_CENTER:
109 case LYX_ALIGN_RIGHT:
111 case LYX_ALIGN_LAYOUT:
113 case LYX_ALIGN_SPECIAL:
120 string const tostr(LyXTabular::VAlignment const & num)
123 case LyXTabular::LYX_VALIGN_TOP:
125 case LyXTabular::LYX_VALIGN_MIDDLE:
127 case LyXTabular::LYX_VALIGN_BOTTOM:
134 string const tostr(LyXTabular::BoxType const & num)
137 case LyXTabular::BOX_NONE:
139 case LyXTabular::BOX_PARBOX:
141 case LyXTabular::BOX_MINIPAGE:
148 // I would have liked a fromstr template a lot better. (Lgb)
149 bool string2type(string const str, LyXAlignment & num)
152 num = LYX_ALIGN_NONE;
153 else if (str == "block")
154 num = LYX_ALIGN_BLOCK;
155 else if (str == "left")
156 num = LYX_ALIGN_LEFT;
157 else if (str == "center")
158 num = LYX_ALIGN_CENTER;
159 else if (str == "right")
160 num = LYX_ALIGN_RIGHT;
167 bool string2type(string const str, LyXTabular::VAlignment & num)
170 num = LyXTabular::LYX_VALIGN_TOP;
171 else if (str == "middle" )
172 num = LyXTabular::LYX_VALIGN_MIDDLE;
173 else if (str == "bottom")
174 num = LyXTabular::LYX_VALIGN_BOTTOM;
181 bool string2type(string const str, LyXTabular::BoxType & num)
184 num = LyXTabular::BOX_NONE;
185 else if (str == "parbox")
186 num = LyXTabular::BOX_PARBOX;
187 else if (str == "minipage")
188 num = LyXTabular::BOX_MINIPAGE;
195 bool string2type(string const str, bool & num)
199 else if (str == "false")
207 bool getTokenValue(string const & str, const char * token, string & ret)
210 size_t token_length = strlen(token);
211 string::size_type pos = str.find(token);
213 if (pos == string::npos || pos + token_length + 1 >= str.length()
214 || str[pos + token_length] != '=')
216 pos += token_length + 1;
218 if (ch != '"' && ch != '\'') { // only read till next space
222 while (pos < str.length() - 1 && str[++pos] != ch)
229 bool getTokenValue(string const & str, const char * token, int & num)
233 if (!getTokenValue(str, token, tmp))
240 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
243 if (!getTokenValue(str, token, tmp))
245 return string2type(tmp, num);
249 bool getTokenValue(string const & str, const char * token,
250 LyXTabular::VAlignment & num)
253 if (!getTokenValue(str, token, tmp))
255 return string2type(tmp, num);
259 bool getTokenValue(string const & str, const char * token,
260 LyXTabular::BoxType & num)
263 if (!getTokenValue(str, token, tmp))
265 return string2type(tmp, num);
269 bool getTokenValue(string const & str, const char * token, bool & flag)
271 // set the flag always to false as this should be the default for bools
272 // not in the file-format.
275 if (!getTokenValue(str, token, tmp))
277 return string2type(tmp, flag);
281 bool getTokenValue(string const & str, const char * token, LyXLength & len)
283 // set the lenght to be zero() as default as this it should be if not
284 // in the file format.
287 if (!getTokenValue(str, token, tmp))
289 return isValidLength(tmp, &len);
293 void l_getline(istream & is, string & str)
296 while (str.empty()) {
298 if (!str.empty() && str[str.length() - 1] == '\r')
299 str.erase(str.length() - 1);
305 /// Define a few methods for the inner structs
307 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
312 multicolumn = LyXTabular::CELL_NORMAL;
313 alignment = LYX_ALIGN_CENTER;
314 valignment = LYX_VALIGN_TOP;
324 LyXTabular::rowstruct::rowstruct()
331 endfirsthead = false;
338 LyXTabular::columnstruct::columnstruct()
342 alignment = LYX_ALIGN_CENTER;
343 valignment = LYX_VALIGN_TOP;
348 LyXTabular::ltType::ltType()
356 LyXTabular::LyXTabular(BufferParams const & bp, int rows_arg, int columns_arg)
358 init(bp, rows_arg, columns_arg);
362 // activates all lines and sets all widths to 0
363 void LyXTabular::init(BufferParams const & bp, int rows_arg, int columns_arg)
366 columns_ = columns_arg;
367 row_info = row_vector(rows_);
368 column_info = column_vector(columns_);
369 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
370 row_info.reserve(10);
371 column_info.reserve(10);
372 cell_info.reserve(100);
374 for (int i = 0; i < rows_; ++i)
375 cell_info[i].back().right_line = true;
376 row_info.back().bottom_line = true;
377 row_info.front().bottom_line = true;
378 column_info.back().right_line = true;
379 is_long_tabular = false;
384 void LyXTabular::fixCellNums()
387 for (int i = 0; i < rows_; ++i) {
388 for (int j = 0; j < columns_; ++j) {
389 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
390 cell_info[i][j].cellno = cellno++;
392 cell_info[i].back().right_line = true;
395 set_row_column_number_info();
399 void LyXTabular::setOwner(InsetTabular * inset)
401 for (int i = 0; i < rows_; ++i) {
402 for (int j = 0; j < columns_; ++j) {
403 cell_info[i][j].inset.setOwner(inset);
404 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
410 void LyXTabular::appendRow(BufferParams const & bp, int cell)
414 int row = row_of_cell(cell);
416 row_vector::iterator rit = row_info.begin() + row;
417 row_info.insert(rit, rowstruct());
418 // now set the values of the row before
419 row_info[row] = row_info[row + 1];
421 cell_vvector old(rows_ - 1);
422 for (int i = 0; i < rows_ - 1; ++i)
423 swap(cell_info[i], old[i]);
425 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
427 for (int i = 0; i <= row; ++i)
428 swap(cell_info[i], old[i]);
429 for (int i = row + 2; i < rows_; ++i)
430 swap(cell_info[i], old[i - 1]);
432 if (bp.tracking_changes)
433 for (int j = 0; j < columns_; ++j)
434 cell_info[row + 1][j].inset.markNew(true);
436 set_row_column_number_info();
440 void LyXTabular::deleteRow(int row)
442 // Not allowed to delete last row
446 row_info.erase(row_info.begin() + row);
447 cell_info.erase(cell_info.begin() + row);
453 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
457 int const column = column_of_cell(cell);
458 column_vector::iterator cit = column_info.begin() + column + 1;
459 column_info.insert(cit, columnstruct());
460 // set the column values of the column before
461 column_info[column + 1] = column_info[column];
463 for (int i = 0; i < rows_; ++i) {
464 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
466 // care about multicolumns
467 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
468 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
470 if (column + 2 >= columns_
471 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
472 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
475 for (int i = 0; i < rows_; ++i) {
476 cell_info[i][column + 1].inset.clear(false);
477 if (bp.tracking_changes)
478 cell_info[i][column + 1].inset.markNew(true);
484 void LyXTabular::deleteColumn(int column)
486 // Not allowed to delete last column
490 column_info.erase(column_info.begin() + column);
491 for (int i = 0; i < rows_; ++i)
492 cell_info[i].erase(cell_info[i].begin() + column);
498 void LyXTabular::set_row_column_number_info()
501 for (int row = 0; row < rows_; ++row) {
502 for (int column = 0; column < columns_; ++column) {
503 if (cell_info[row][column].multicolumn
504 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
506 cell_info[row][column].cellno = numberofcells;
509 ++numberofcells; // because this is one more than as we start from 0
511 rowofcell.resize(numberofcells);
512 columnofcell.resize(numberofcells);
514 for (int row = 0, column = 0, c = 0;
515 c < numberofcells && row < rows_ && column < columns_;) {
517 columnofcell[c] = column;
521 } while (column < columns_ &&
522 cell_info[row][column].multicolumn
523 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
525 if (column == columns_) {
531 for (int row = 0; row < rows_; ++row) {
532 for (int column = 0; column < columns_; ++column) {
533 if (isPartOfMultiColumn(row,column))
535 cell_info[row][column].inset.setAutoBreakRows(
536 !getPWidth(getCellNumber(row, column)).zero());
542 int LyXTabular::getNumberOfCells() const
544 return numberofcells;
548 int LyXTabular::numberOfCellsInRow(int cell) const
550 int const row = row_of_cell(cell);
552 for (int i = 0; i < columns_; ++i)
553 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
559 // returns 1 if there is a topline, returns 0 if not
560 bool LyXTabular::topLine(int cell, bool onlycolumn) const
562 if (!onlycolumn && isMultiColumn(cell))
563 return cellinfo_of_cell(cell).top_line;
564 return row_info[row_of_cell(cell)].top_line;
568 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
570 if (!onlycolumn && isMultiColumn(cell))
571 return cellinfo_of_cell(cell).bottom_line;
572 return row_info[row_of_cell(cell)].bottom_line;
576 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
578 if (!onlycolumn && isMultiColumn(cell) &&
579 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
581 if (cellinfo_of_cell(cell).align_special.empty())
582 return cellinfo_of_cell(cell).left_line;
583 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
585 if (column_info[column_of_cell(cell)].align_special.empty())
586 return column_info[column_of_cell(cell)].left_line;
587 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
591 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
593 if (!onlycolumn && isMultiColumn(cell) &&
594 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
596 if (cellinfo_of_cell(cell).align_special.empty())
597 return cellinfo_of_cell(cell).right_line;
598 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
600 if (column_info[column_of_cell(cell)].align_special.empty())
601 return column_info[right_column_of_cell(cell)].right_line;
602 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
606 bool LyXTabular::topAlreadyDrawn(int cell) const
608 int row = row_of_cell(cell);
609 if (row > 0 && !getAdditionalHeight(row)) {
610 int column = column_of_cell(cell);
613 && cell_info[row][column].multicolumn
614 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
616 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
617 return row_info[row].bottom_line;
619 return cell_info[row][column].bottom_line;
625 bool LyXTabular::leftAlreadyDrawn(int cell) const
627 int column = column_of_cell(cell);
629 int row = row_of_cell(cell);
631 (cell_info[row][column].multicolumn ==
632 LyXTabular::CELL_PART_OF_MULTICOLUMN));
633 if (getAdditionalWidth(cell_info[row][column].cellno))
635 return rightLine(cell_info[row][column].cellno);
641 bool LyXTabular::isLastRow(int cell) const
643 return row_of_cell(cell) == rows_ - 1;
647 int LyXTabular::getAdditionalHeight(int row) const
649 if (!row || row >= rows_)
655 for (int column = 0; column < columns_ && bottom; ++column) {
656 switch (cell_info[row - 1][column].multicolumn) {
657 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
658 bottom = cell_info[row - 1][column].bottom_line;
660 case LyXTabular::CELL_NORMAL:
661 bottom = row_info[row - 1].bottom_line;
664 for (int column = 0; column < columns_ && top; ++column) {
665 switch (cell_info[row][column].multicolumn) {
666 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
667 top = cell_info[row][column].top_line;
669 case LyXTabular::CELL_NORMAL:
670 top = row_info[row].top_line;
674 return WIDTH_OF_LINE;
679 int LyXTabular::getAdditionalWidth(int cell) const
681 // internally already set in setWidthOfCell
682 // used to get it back in text.C
683 int const col = right_column_of_cell(cell);
684 int const row = row_of_cell(cell);
685 if (col < columns_ - 1 && rightLine(cell) &&
686 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
688 return WIDTH_OF_LINE;
694 // returns the maximum over all rows
695 int LyXTabular::getWidthOfColumn(int cell) const
697 int const column1 = column_of_cell(cell);
698 int const column2 = right_column_of_cell(cell);
700 for (int i = column1; i <= column2; ++i)
701 result += column_info[i].width_of_column;
706 int LyXTabular::getWidthOfTabular() const
708 return width_of_tabular;
712 // returns true if a complete update is necessary, otherwise false
713 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
715 if (!isMultiColumn(cell))
718 int const row = row_of_cell(cell);
719 int const column1 = column_of_cell(cell);
720 int const column2 = right_column_of_cell(cell);
721 int const old_val = cell_info[row][column2].width_of_cell;
723 // first set columns to 0 so we can calculate the right width
724 for (int i = column1; i <= column2; ++i) {
725 cell_info[row][i].width_of_cell = 0;
727 // set the width to MAX_WIDTH until width > 0
728 int width = new_width + 2 * WIDTH_OF_LINE;
730 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
731 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
732 width -= column_info[i].width_of_column;
735 cell_info[row][i].width_of_cell = width;
737 if (old_val != cell_info[row][column2].width_of_cell) {
738 // in this case we have to recalculate all multicolumn cells which
739 // have this column as one of theirs but not as last one
740 calculate_width_of_column_NMC(i);
741 recalculateMulticolumnsOfColumn(i);
742 calculate_width_of_column(i);
748 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
750 // the last column does not have to be recalculated because all
751 // multicolumns will have here there last multicolumn cell which
752 // always will have the whole rest of the width of the cell.
753 if (column > (columns_ - 2))
755 for(int row = 0; row < rows_; ++row) {
756 int mc = cell_info[row][column].multicolumn;
757 int nmc = cell_info[row][column+1].multicolumn;
758 // we only have to update multicolumns which do not have this
759 // column as their last column!
760 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
761 (mc == CELL_PART_OF_MULTICOLUMN &&
762 nmc == CELL_PART_OF_MULTICOLUMN))
764 int const cellno = cell_info[row][column].cellno;
765 setWidthOfMulticolCell(cellno,
766 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
772 // returns 1 if a complete update is necessary, otherwise 0
773 void LyXTabular::setWidthOfCell(int cell, int new_width)
775 int const row = row_of_cell(cell);
776 int const column1 = column_of_cell(cell);
781 if (rightLine(cell_info[row][column1].cellno, true) &&
782 column1 < columns_ - 1 &&
783 leftLine(cell_info[row][column1+1].cellno, true))
785 add_width = WIDTH_OF_LINE;
788 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
791 if (isMultiColumnReal(cell)) {
792 tmp = setWidthOfMulticolCell(cell, new_width);
794 width = new_width + 2 * WIDTH_OF_LINE + add_width;
795 cell_info[row][column1].width_of_cell = width;
796 tmp = calculate_width_of_column_NMC(column1);
798 recalculateMulticolumnsOfColumn(column1);
801 for (int i = 0; i < columns_; ++i)
802 calculate_width_of_column(i);
803 calculate_width_of_tabular();
808 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
810 if (!isMultiColumn(cell) || onlycolumn)
811 column_info[column_of_cell(cell)].alignment = align;
813 cellinfo_of_cell(cell).alignment = align;
817 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
819 if (!isMultiColumn(cell) || onlycolumn)
820 column_info[column_of_cell(cell)].valignment = align;
822 cellinfo_of_cell(cell).valignment = align;
826 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
828 int const j = column_of_cell(cell);
830 column_info[j].p_width = width;
831 for (int i = 0; i < rows_; ++i) {
832 int const cell = getCellNumber(i, j);
833 // because of multicolumns
834 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
839 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
841 if (!isMultiColumn(cell))
844 cellinfo_of_cell(cell).p_width = width;
845 getCellInset(cell).setAutoBreakRows(!width.zero());
850 void LyXTabular::setAlignSpecial(int cell, string const & special,
851 LyXTabular::Feature what)
853 if (what == SET_SPECIAL_MULTI)
854 cellinfo_of_cell(cell).align_special = special;
856 column_info[column_of_cell(cell)].align_special = special;
860 void LyXTabular::setAllLines(int cell, bool line)
862 setTopLine(cell, line);
863 setBottomLine(cell, line);
864 setRightLine(cell, line);
865 setLeftLine(cell, line);
869 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
871 int const row = row_of_cell(cell);
872 if (onlycolumn || !isMultiColumn(cell))
873 row_info[row].top_line = line;
875 cellinfo_of_cell(cell).top_line = line;
879 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
881 if (onlycolumn || !isMultiColumn(cell))
882 row_info[row_of_cell(cell)].bottom_line = line;
884 cellinfo_of_cell(cell).bottom_line = line;
888 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
890 if (onlycolumn || !isMultiColumn(cell))
891 column_info[column_of_cell(cell)].left_line = line;
893 cellinfo_of_cell(cell).left_line = line;
897 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
899 if (onlycolumn || !isMultiColumn(cell))
900 column_info[right_column_of_cell(cell)].right_line = line;
902 cellinfo_of_cell(cell).right_line = line;
906 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
908 if (!onlycolumn && isMultiColumn(cell))
909 return cellinfo_of_cell(cell).alignment;
910 return column_info[column_of_cell(cell)].alignment;
914 LyXTabular::VAlignment
915 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
917 if (!onlycolumn && isMultiColumn(cell))
918 return cellinfo_of_cell(cell).valignment;
919 return column_info[column_of_cell(cell)].valignment;
923 LyXLength const LyXTabular::getPWidth(int cell) const
925 if (isMultiColumn(cell))
926 return cellinfo_of_cell(cell).p_width;
927 return column_info[column_of_cell(cell)].p_width;
931 LyXLength const LyXTabular::getColumnPWidth(int cell) const
933 return column_info[column_of_cell(cell)].p_width;
937 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
939 if (isMultiColumn(cell))
940 return cellinfo_of_cell(cell).p_width;
945 string const LyXTabular::getAlignSpecial(int cell, int what) const
947 if (what == SET_SPECIAL_MULTI)
948 return cellinfo_of_cell(cell).align_special;
949 return column_info[column_of_cell(cell)].align_special;
953 int LyXTabular::getWidthOfCell(int cell) const
955 int const row = row_of_cell(cell);
956 int const column1 = column_of_cell(cell);
957 int const column2 = right_column_of_cell(cell);
959 for (int i = column1; i <= column2; ++i)
960 result += cell_info[row][i].width_of_cell;
965 int LyXTabular::getBeginningOfTextInCell(int cell) const
969 switch (getAlignment(cell)) {
970 case LYX_ALIGN_CENTER:
971 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
973 case LYX_ALIGN_RIGHT:
974 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
975 // + getAdditionalWidth(cell);
978 // LYX_ALIGN_LEFT: nothing :-)
988 bool LyXTabular::isFirstCellInRow(int cell) const
990 return column_of_cell(cell) == 0;
994 int LyXTabular::getFirstCellInRow(int row) const
998 return cell_info[row][0].cellno;
1002 bool LyXTabular::isLastCellInRow(int cell) const
1004 return right_column_of_cell(cell) == columns_ - 1;
1008 int LyXTabular::getLastCellInRow(int row) const
1010 if (row > rows_ - 1)
1012 return cell_info[row][columns_-1].cellno;
1016 void LyXTabular::calculate_width_of_column(int column)
1019 for (int i = 0; i < rows_; ++i)
1020 maximum = max(cell_info[i][column].width_of_cell, maximum);
1021 column_info[column].width_of_column = maximum;
1026 // Calculate the columns regarding ONLY the normal cells and if this
1027 // column is inside a multicolumn cell then use it only if its the last
1028 // column of this multicolumn cell as this gives an added width to the
1029 // column, all the rest should be adapted!
1031 bool LyXTabular::calculate_width_of_column_NMC(int column)
1033 int const old_column_width = column_info[column].width_of_column;
1035 for (int i = 0; i < rows_; ++i) {
1036 int cell = getCellNumber(i, column);
1037 bool ismulti = isMultiColumnReal(cell);
1038 if ((!ismulti || column == right_column_of_cell(cell)) &&
1039 cell_info[i][column].width_of_cell > max)
1041 max = cell_info[i][column].width_of_cell;
1044 column_info[column].width_of_column = max;
1045 return column_info[column].width_of_column != old_column_width;
1049 void LyXTabular::calculate_width_of_tabular()
1051 width_of_tabular = 0;
1052 for (int i = 0; i < columns_; ++i)
1053 width_of_tabular += column_info[i].width_of_column;
1057 int LyXTabular::row_of_cell(int cell) const
1059 if (cell >= numberofcells)
1063 return rowofcell[cell];
1067 int LyXTabular::column_of_cell(int cell) const
1069 if (cell >= numberofcells)
1070 return columns_ - 1;
1073 return columnofcell[cell];
1077 int LyXTabular::right_column_of_cell(int cell) const
1079 int const row = row_of_cell(cell);
1080 int column = column_of_cell(cell);
1081 while (column < columns_ - 1 &&
1082 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1088 void LyXTabular::write(Buffer const & buf, ostream & os) const
1092 << write_attribute("version", 3)
1093 << write_attribute("rows", rows_)
1094 << write_attribute("columns", columns_)
1096 // global longtable options
1098 << write_attribute("rotate", rotate)
1099 << write_attribute("islongtable", is_long_tabular)
1100 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1101 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1102 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1103 << write_attribute("headTopDL", endhead.topDL)
1104 << write_attribute("headBottomDL", endhead.bottomDL)
1105 << write_attribute("footTopDL", endfoot.topDL)
1106 << write_attribute("footBottomDL", endfoot.bottomDL)
1107 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1108 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1109 << write_attribute("lastFootEmpty", endlastfoot.empty)
1111 for (int j = 0; j < columns_; ++j) {
1113 << write_attribute("alignment", column_info[j].alignment)
1114 << write_attribute("valignment", column_info[j].valignment)
1115 << write_attribute("leftline", column_info[j].left_line)
1116 << write_attribute("rightline", column_info[j].right_line)
1117 << write_attribute("width", column_info[j].p_width.asString())
1118 << write_attribute("special", column_info[j].align_special)
1121 for (int i = 0; i < rows_; ++i) {
1123 << write_attribute("topline", row_info[i].top_line)
1124 << write_attribute("bottomline", row_info[i].bottom_line)
1125 << write_attribute("endhead", row_info[i].endhead)
1126 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1127 << write_attribute("endfoot", row_info[i].endfoot)
1128 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1129 << write_attribute("newpage", row_info[i].newpage)
1131 for (int j = 0; j < columns_; ++j) {
1133 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1134 << write_attribute("alignment", cell_info[i][j].alignment)
1135 << write_attribute("valignment", cell_info[i][j].valignment)
1136 << write_attribute("topline", cell_info[i][j].top_line)
1137 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1138 << write_attribute("leftline", cell_info[i][j].left_line)
1139 << write_attribute("rightline", cell_info[i][j].right_line)
1140 << write_attribute("rotate", cell_info[i][j].rotate)
1141 << write_attribute("usebox", cell_info[i][j].usebox)
1142 << write_attribute("width", cell_info[i][j].p_width)
1143 << write_attribute("special", cell_info[i][j].align_special)
1145 os << "\\begin_inset ";
1146 cell_info[i][j].inset.write(buf, os);
1147 os << "\n\\end_inset \n"
1152 os << "</lyxtabular>\n";
1156 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1160 row_info[--hr].endhead = true;
1162 // set firstheader info
1163 if (fhr && fhr < rows_) {
1164 if (row_info[fhr].endhead) {
1166 row_info[--fhr].endfirsthead = true;
1167 row_info[fhr].endhead = false;
1169 } else if (row_info[fhr - 1].endhead) {
1170 endfirsthead.empty = true;
1172 while (fhr > 0 && !row_info[--fhr].endhead) {
1173 row_info[fhr].endfirsthead = true;
1178 if (fr && fr < rows_) {
1179 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1180 while (fr > 0 && !row_info[--fr].endhead) {
1181 row_info[fr].endfoot = true;
1182 row_info[fr].endhead = false;
1184 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1185 while (fr > 0 && !row_info[--fr].endfirsthead) {
1186 row_info[fr].endfoot = true;
1187 row_info[fr].endfirsthead = false;
1189 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1190 while (fr > 0 && !row_info[--fr].endhead &&
1191 !row_info[fr].endfirsthead)
1193 row_info[fr].endfoot = true;
1197 // set lastfooter info
1198 if (lfr && lfr < rows_) {
1199 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1200 while (lfr > 0 && !row_info[--lfr].endhead) {
1201 row_info[lfr].endlastfoot = true;
1202 row_info[lfr].endhead = false;
1204 } else if (row_info[lfr].endfirsthead &&
1205 row_info[lfr - 1].endfirsthead)
1207 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1208 row_info[lfr].endlastfoot = true;
1209 row_info[lfr].endfirsthead = false;
1211 } else if (row_info[lfr].endfoot
1212 && row_info[lfr - 1].endfoot) {
1213 while (lfr > 0 && !row_info[--lfr].endfoot) {
1214 row_info[lfr].endlastfoot = true;
1215 row_info[lfr].endfoot = false;
1217 } else if (!row_info[fr - 1].endhead
1218 && !row_info[fr - 1].endfirsthead &&
1219 !row_info[fr - 1].endfoot)
1222 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1223 !row_info[lfr].endfoot)
1225 row_info[lfr].endlastfoot = true;
1227 } else if (haveLTFoot()) {
1228 endlastfoot.empty = true;
1234 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1237 istream & is = lex.getStream();
1239 l_getline(is, line);
1240 if (!prefixIs(line, "<lyxtabular ")
1241 && !prefixIs(line, "<LyXTabular ")) {
1242 BOOST_ASSERT(false);
1247 if (!getTokenValue(line, "version", version))
1249 BOOST_ASSERT(version >= 2);
1252 if (!getTokenValue(line, "rows", rows_arg))
1255 if (!getTokenValue(line, "columns", columns_arg))
1257 init(buf.params(), rows_arg, columns_arg);
1258 l_getline(is, line);
1259 if (!prefixIs(line, "<features")) {
1260 lyxerr << "Wrong tabular format (expected <features ...> got"
1261 << line << ')' << endl;
1264 getTokenValue(line, "rotate", rotate);
1265 getTokenValue(line, "islongtable", is_long_tabular);
1266 // compatibility read for old longtable options. Now we can make any
1267 // row part of the header/footer type we want before it was strict
1268 // sequential from the first row down (as LaTeX does it!). So now when
1269 // we find a header/footer line we have to go up the rows and set it
1270 // on all preceding rows till the first or one with already a h/f option
1271 // set. If we find a firstheader on the same line as a header or a
1272 // lastfooter on the same line as a footer then this should be set empty.
1280 getTokenValue(line, "endhead", hrow);
1281 getTokenValue(line, "endfirsthead", fhrow);
1282 getTokenValue(line, "endfoot", frow);
1283 getTokenValue(line, "endlastfoot", lfrow);
1284 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1286 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1287 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1288 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1289 getTokenValue(line, "headTopDL", endhead.topDL);
1290 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1291 getTokenValue(line, "footTopDL", endfoot.topDL);
1292 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1293 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1294 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1295 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1297 for (int j = 0; j < columns_; ++j) {
1299 if (!prefixIs(line,"<column")) {
1300 lyxerr << "Wrong tabular format (expected <column ...> got"
1301 << line << ')' << endl;
1304 getTokenValue(line, "alignment", column_info[j].alignment);
1305 getTokenValue(line, "valignment", column_info[j].valignment);
1306 getTokenValue(line, "leftline", column_info[j].left_line);
1307 getTokenValue(line, "rightline", column_info[j].right_line);
1308 getTokenValue(line, "width", column_info[j].p_width);
1309 getTokenValue(line, "special", column_info[j].align_special);
1312 for (int i = 0; i < rows_; ++i) {
1313 l_getline(is, line);
1314 if (!prefixIs(line, "<row")) {
1315 lyxerr << "Wrong tabular format (expected <row ...> got"
1316 << line << ')' << endl;
1319 getTokenValue(line, "topline", row_info[i].top_line);
1320 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1321 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1322 getTokenValue(line, "endhead", row_info[i].endhead);
1323 getTokenValue(line, "endfoot", row_info[i].endfoot);
1324 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1325 getTokenValue(line, "newpage", row_info[i].newpage);
1326 for (int j = 0; j < columns_; ++j) {
1327 l_getline(is, line);
1328 if (!prefixIs(line, "<cell")) {
1329 lyxerr << "Wrong tabular format (expected <cell ...> got"
1330 << line << ')' << endl;
1333 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1334 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1335 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1336 getTokenValue(line, "topline", cell_info[i][j].top_line);
1337 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1338 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1339 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1340 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1341 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1342 getTokenValue(line, "width", cell_info[i][j].p_width);
1343 getTokenValue(line, "special", cell_info[i][j].align_special);
1344 l_getline(is, line);
1345 if (prefixIs(line, "\\begin_inset")) {
1346 cell_info[i][j].inset.read(buf, lex);
1347 l_getline(is, line);
1349 if (!prefixIs(line, "</cell>")) {
1350 lyxerr << "Wrong tabular format (expected </cell> got"
1351 << line << ')' << endl;
1355 l_getline(is, line);
1356 if (!prefixIs(line, "</row>")) {
1357 lyxerr << "Wrong tabular format (expected </row> got"
1358 << line << ')' << endl;
1362 while (!prefixIs(line, "</lyxtabular>")) {
1363 l_getline(is, line);
1365 set_row_column_number_info();
1369 bool LyXTabular::isMultiColumn(int cell) const
1371 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1375 bool LyXTabular::isMultiColumnReal(int cell) const
1377 return column_of_cell(cell) != right_column_of_cell(cell) &&
1378 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1382 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1384 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1388 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1390 cellstruct & cs = cellinfo_of_cell(cell);
1391 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1392 cs.alignment = column_info[column_of_cell(cell)].alignment;
1393 cs.top_line = row_info[row_of_cell(cell)].top_line;
1394 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1395 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1396 for (int i = 1; i < number; ++i) {
1397 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1398 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1399 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1400 cs1.inset.clear(false);
1402 set_row_column_number_info();
1406 int LyXTabular::cells_in_multicolumn(int cell) const
1408 int const row = row_of_cell(cell);
1409 int column = column_of_cell(cell);
1412 while (column < columns_ &&
1413 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1422 int LyXTabular::unsetMultiColumn(int cell)
1424 int const row = row_of_cell(cell);
1425 int column = column_of_cell(cell);
1429 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1430 cell_info[row][column].multicolumn = CELL_NORMAL;
1432 while (column < columns_ &&
1433 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1435 cell_info[row][column].multicolumn = CELL_NORMAL;
1440 set_row_column_number_info();
1445 void LyXTabular::setLongTabular(bool what)
1447 is_long_tabular = what;
1451 bool LyXTabular::isLongTabular() const
1453 return is_long_tabular;
1457 void LyXTabular::setRotateTabular(bool flag)
1463 bool LyXTabular::getRotateTabular() const
1469 void LyXTabular::setRotateCell(int cell, bool flag)
1471 cellinfo_of_cell(cell).rotate = flag;
1475 bool LyXTabular::getRotateCell(int cell) const
1477 return cellinfo_of_cell(cell).rotate;
1481 bool LyXTabular::needRotating() const
1485 for (int i = 0; i < rows_; ++i)
1486 for (int j = 0; j < columns_; ++j)
1487 if (cell_info[i][j].rotate)
1493 bool LyXTabular::isLastCell(int cell) const
1495 if (cell + 1 < numberofcells)
1501 int LyXTabular::getCellAbove(int cell) const
1503 if (row_of_cell(cell) > 0)
1504 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1509 int LyXTabular::getCellBelow(int cell) const
1511 if (row_of_cell(cell) + 1 < rows_)
1512 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1517 int LyXTabular::getLastCellAbove(int cell) const
1519 if (row_of_cell(cell) <= 0)
1521 if (!isMultiColumn(cell))
1522 return getCellAbove(cell);
1523 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1527 int LyXTabular::getLastCellBelow(int cell) const
1529 if (row_of_cell(cell) + 1 >= rows_)
1531 if (!isMultiColumn(cell))
1532 return getCellBelow(cell);
1533 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1537 int LyXTabular::getCellNumber(int row, int column) const
1539 BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1540 return cell_info[row][column].cellno;
1544 void LyXTabular::setUsebox(int cell, BoxType type)
1546 cellinfo_of_cell(cell).usebox = type;
1550 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1552 if (column_info[column_of_cell(cell)].p_width.zero() &&
1553 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1555 if (cellinfo_of_cell(cell).usebox > 1)
1556 return cellinfo_of_cell(cell).usebox;
1557 return useParbox(cell);
1562 // This are functions used for the longtable support
1564 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1569 row_info[row].endfirsthead = flag;
1573 row_info[row].endhead = flag;
1578 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1581 hd.set = haveLTHead();
1582 return row_info[row].endhead;
1586 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1589 hd.set = haveLTFirstHead();
1590 return row_info[row].endfirsthead;
1594 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1599 row_info[row].endlastfoot = flag;
1603 row_info[row].endfoot = flag;
1608 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1611 fd.set = haveLTFoot();
1612 return row_info[row].endfoot;
1616 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1619 fd.set = haveLTLastFoot();
1620 return row_info[row].endlastfoot;
1624 void LyXTabular::setLTNewPage(int row, bool what)
1626 row_info[row].newpage = what;
1630 bool LyXTabular::getLTNewPage(int row) const
1632 return row_info[row].newpage;
1636 bool LyXTabular::haveLTHead() const
1638 for (int i = 0; i < rows_; ++i)
1639 if (row_info[i].endhead)
1645 bool LyXTabular::haveLTFirstHead() const
1647 if (endfirsthead.empty)
1649 for (int i = 0; i < rows_; ++i)
1650 if (row_info[i].endfirsthead)
1656 bool LyXTabular::haveLTFoot() const
1658 for (int i = 0; i < rows_; ++i)
1659 if (row_info[i].endfoot)
1665 bool LyXTabular::haveLTLastFoot() const
1667 if (endlastfoot.empty)
1669 for (int i = 0; i < rows_; ++i)
1670 if (row_info[i].endlastfoot)
1676 // end longtable support functions
1678 void LyXTabular::setAscentOfRow(int row, int height)
1680 if (row >= rows_ || row_info[row].ascent_of_row == height)
1682 row_info[row].ascent_of_row = height;
1686 void LyXTabular::setDescentOfRow(int row, int height)
1688 if (row >= rows_ || row_info[row].descent_of_row == height)
1690 row_info[row].descent_of_row = height;
1694 int LyXTabular::getAscentOfRow(int row) const
1698 return row_info[row].ascent_of_row;
1702 int LyXTabular::getDescentOfRow(int row) const
1706 return row_info[row].descent_of_row;
1710 int LyXTabular::getHeightOfTabular() const
1713 for (int row = 0; row < rows_; ++row)
1714 height += getAscentOfRow(row) + getDescentOfRow(row) +
1715 getAdditionalHeight(row);
1720 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1722 if (row >= rows_ || column >= columns_)
1724 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1728 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1730 if (row < 0 || row >= rows_)
1733 int const fcell = getFirstCellInRow(row);
1734 int const n = numberOfCellsInRow(fcell) + fcell;
1737 for (int i = fcell; i < n; ++i) {
1741 if (tmp == n - fcell) {
1744 for (int i = fcell; i < n; ++i) {
1747 << column_of_cell(i) + 1
1749 << right_column_of_cell(i) + 1
1761 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1763 if (row < 0 || row >= rows_)
1766 int const fcell = getFirstCellInRow(row);
1767 int const n = numberOfCellsInRow(fcell) + fcell;
1770 for (int i = fcell; i < n; ++i) {
1774 if (tmp == n - fcell) {
1777 for (int i = fcell; i < n; ++i) {
1778 if (bottomLine(i)) {
1780 << column_of_cell(i) + 1
1782 << right_column_of_cell(i) + 1
1794 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1798 if (getRotateCell(cell)) {
1799 os << "\\begin{sideways}\n";
1802 if (isMultiColumn(cell)) {
1803 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1804 if (!cellinfo_of_cell(cell).align_special.empty()) {
1805 os << cellinfo_of_cell(cell).align_special << "}{";
1807 if (leftLine(cell) &&
1808 (isFirstCellInRow(cell) ||
1809 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1810 !rightLine(cell - 1, true))))
1814 if (!getPWidth(cell).zero()) {
1815 switch (getVAlignment(cell)) {
1816 case LYX_VALIGN_TOP:
1819 case LYX_VALIGN_MIDDLE:
1822 case LYX_VALIGN_BOTTOM:
1827 << getPWidth(cell).asLatexString()
1830 switch (getAlignment(cell)) {
1831 case LYX_ALIGN_LEFT:
1834 case LYX_ALIGN_RIGHT:
1842 if (rightLine(cell))
1844 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1850 if (getUsebox(cell) == BOX_PARBOX) {
1852 switch (getVAlignment(cell)) {
1853 case LYX_VALIGN_TOP:
1856 case LYX_VALIGN_MIDDLE:
1859 case LYX_VALIGN_BOTTOM:
1863 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1864 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1865 os << "\\begin{minipage}[";
1866 switch (getVAlignment(cell)) {
1867 case LYX_VALIGN_TOP:
1870 case LYX_VALIGN_MIDDLE:
1873 case LYX_VALIGN_BOTTOM:
1877 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1884 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1889 if (getUsebox(cell) == BOX_PARBOX)
1891 else if (getUsebox(cell) == BOX_MINIPAGE) {
1892 os << "%\n\\end{minipage}";
1895 if (isMultiColumn(cell)) {
1898 if (getRotateCell(cell)) {
1899 os << "%\n\\end{sideways}";
1906 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1907 LatexRunParams const & runparams) const
1909 if (!is_long_tabular)
1913 // output header info
1915 if (endhead.topDL) {
1919 for (int i = 0; i < rows_; ++i) {
1920 if (row_info[i].endhead) {
1921 ret += TeXRow(os, i, buf, runparams);
1924 if (endhead.bottomDL) {
1928 os << "\\endhead\n";
1930 if (endfirsthead.empty) {
1931 os << "\\endfirsthead\n";
1935 // output firstheader info
1936 if (haveLTFirstHead()) {
1937 if (endfirsthead.topDL) {
1941 for (int i = 0; i < rows_; ++i) {
1942 if (row_info[i].endfirsthead) {
1943 ret += TeXRow(os, i, buf, runparams);
1946 if (endfirsthead.bottomDL) {
1950 os << "\\endfirsthead\n";
1953 // output footer info
1955 if (endfoot.topDL) {
1959 for (int i = 0; i < rows_; ++i) {
1960 if (row_info[i].endfoot) {
1961 ret += TeXRow(os, i, buf, runparams);
1964 if (endfoot.bottomDL) {
1968 os << "\\endfoot\n";
1970 if (endlastfoot.empty) {
1971 os << "\\endlastfoot\n";
1975 // output lastfooter info
1976 if (haveLTLastFoot()) {
1977 if (endlastfoot.topDL) {
1981 for (int i = 0; i < rows_; ++i) {
1982 if (row_info[i].endlastfoot) {
1983 ret += TeXRow(os, i, buf, runparams);
1986 if (endlastfoot.bottomDL) {
1990 os << "\\endlastfoot\n";
1997 bool LyXTabular::isValidRow(int row) const
1999 if (!is_long_tabular)
2001 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2002 !row_info[row].endfoot && !row_info[row].endlastfoot;
2006 int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
2007 LatexRunParams const & runparams) const
2010 int cell = getCellNumber(i, 0);
2011 BufferParams const & bufferparams = buf.params();
2013 ret += TeXTopHLine(os, i);
2014 for (int j = 0; j < columns_; ++j) {
2015 if (isPartOfMultiColumn(i, j))
2017 ret += TeXCellPreamble(os, cell);
2018 InsetText & inset = getCellInset(cell);
2020 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(bufferparams) &&
2021 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2025 ret += inset.latex(buf, os, runparams);
2029 ret += TeXCellPostamble(os, cell);
2030 if (!isLastCellInRow(cell)) { // not last cell in row
2036 os << "\\tabularnewline\n";
2038 ret += TeXBottomHLine(os, i);
2043 int LyXTabular::latex(Buffer const & buf, ostream & os,
2044 LatexRunParams const & runparams) const
2048 //+---------------------------------------------------------------------
2049 //+ first the opening preamble +
2050 //+---------------------------------------------------------------------
2053 os << "\\begin{sideways}\n";
2056 if (is_long_tabular)
2057 os << "\\begin{longtable}{";
2059 os << "\\begin{tabular}{";
2060 for (int i = 0; i < columns_; ++i) {
2061 if (!column_info[i].align_special.empty()) {
2062 os << column_info[i].align_special;
2064 if (column_info[i].left_line)
2066 if (!column_info[i].p_width.zero()) {
2067 switch (column_info[i].alignment) {
2068 case LYX_ALIGN_LEFT:
2069 os << ">{\\raggedright}";
2071 case LYX_ALIGN_RIGHT:
2072 os << ">{\\raggedleft}";
2074 case LYX_ALIGN_CENTER:
2075 os << ">{\\centering}";
2077 case LYX_ALIGN_NONE:
2078 case LYX_ALIGN_BLOCK:
2079 case LYX_ALIGN_LAYOUT:
2080 case LYX_ALIGN_SPECIAL:
2084 switch (column_info[i].valignment) {
2085 case LYX_VALIGN_TOP:
2088 case LYX_VALIGN_MIDDLE:
2091 case LYX_VALIGN_BOTTOM:
2096 << column_info[i].p_width.asLatexString()
2099 switch (column_info[i].alignment) {
2100 case LYX_ALIGN_LEFT:
2103 case LYX_ALIGN_RIGHT:
2111 if (column_info[i].right_line)
2118 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2120 //+---------------------------------------------------------------------
2121 //+ the single row and columns (cells) +
2122 //+---------------------------------------------------------------------
2124 for (int i = 0; i < rows_; ++i) {
2125 if (isValidRow(i)) {
2126 ret += TeXRow(os, i, buf, runparams);
2127 if (is_long_tabular && row_info[i].newpage) {
2128 os << "\\newpage\n";
2134 //+---------------------------------------------------------------------
2135 //+ the closing of the tabular +
2136 //+---------------------------------------------------------------------
2138 if (is_long_tabular)
2139 os << "\\end{longtable}";
2141 os << "\\end{tabular}";
2143 os << "\n\\end{sideways}";
2151 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os) const
2153 os << "<tabular ca=\"";
2154 for (int i = 0; i < columns_; ++i) {
2155 switch (column_info[i].alignment) {
2156 case LYX_ALIGN_LEFT:
2159 case LYX_ALIGN_RIGHT:
2170 for (int i = 0; i < rows_; ++i) {
2171 for (int j = 0; j < columns_; ++j) {
2172 if (isPartOfMultiColumn(i, j))
2174 InsetText & inset = getCellInset(cell);
2176 ret += inset.linuxdoc(buf, os);
2178 if (isLastCellInRow(cell)) {
2187 os << "</tabular>\n";
2192 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row) const
2195 int cell = getFirstCellInRow(row);
2198 for (int j = 0; j < columns_; ++j) {
2199 if (isPartOfMultiColumn(row, j))
2202 os << "<entry align=\"";
2203 switch (getAlignment(cell)) {
2204 case LYX_ALIGN_LEFT:
2207 case LYX_ALIGN_RIGHT:
2215 os << "\" valign=\"";
2216 switch (getVAlignment(cell)) {
2217 case LYX_VALIGN_TOP:
2220 case LYX_VALIGN_BOTTOM:
2223 case LYX_VALIGN_MIDDLE:
2228 if (isMultiColumn(cell)) {
2229 os << " namest=\"col" << j << "\" ";
2230 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2234 ret += getCellInset(cell).docbook(buf, os, true);
2243 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2244 bool /*mixcont*/) const
2248 //+---------------------------------------------------------------------
2249 //+ first the opening preamble +
2250 //+---------------------------------------------------------------------
2252 os << "<tgroup cols=\"" << columns_
2253 << "\" colsep=\"1\" rowsep=\"1\">\n";
2255 for (int i = 0; i < columns_; ++i) {
2256 os << "<colspec colname=\"col" << i << "\" align=\"";
2257 switch (column_info[i].alignment) {
2258 case LYX_ALIGN_LEFT:
2261 case LYX_ALIGN_RIGHT:
2272 //+---------------------------------------------------------------------
2273 //+ Long Tabular case +
2274 //+---------------------------------------------------------------------
2276 // output header info
2277 if (haveLTHead() || haveLTFirstHead()) {
2280 for (int i = 0; i < rows_; ++i) {
2281 if (row_info[i].endhead || row_info[i].endfirsthead) {
2282 ret += docbookRow(buf, os, i);
2288 // output footer info
2289 if (haveLTFoot() || haveLTLastFoot()) {
2292 for (int i = 0; i < rows_; ++i) {
2293 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2294 ret += docbookRow(buf, os, i);
2301 //+---------------------------------------------------------------------
2302 //+ the single row and columns (cells) +
2303 //+---------------------------------------------------------------------
2307 for (int i = 0; i < rows_; ++i) {
2308 if (isValidRow(i)) {
2309 ret += docbookRow(buf, os, i);
2314 //+---------------------------------------------------------------------
2315 //+ the closing of the tabular +
2316 //+---------------------------------------------------------------------
2325 int LyXTabular::asciiTopHLine(ostream & os, int row,
2326 vector<unsigned int> const & clen) const
2328 int const fcell = getFirstCellInRow(row);
2329 int const n = numberOfCellsInRow(fcell) + fcell;
2332 for (int i = fcell; i < n; ++i) {
2342 for (int i = fcell; i < n; ++i) {
2353 int column = column_of_cell(i);
2354 int len = clen[column];
2355 while (isPartOfMultiColumn(row, ++column))
2356 len += clen[column] + 4;
2357 os << string(len, ch);
2372 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2373 vector<unsigned int> const & clen) const
2375 int const fcell = getFirstCellInRow(row);
2376 int const n = numberOfCellsInRow(fcell) + fcell;
2379 for (int i = fcell; i < n; ++i) {
2380 if (bottomLine(i)) {
2389 for (int i = fcell; i < n; ++i) {
2390 if (bottomLine(i)) {
2400 int column = column_of_cell(i);
2401 int len = clen[column];
2402 while (isPartOfMultiColumn(row, ++column))
2403 len += clen[column] + 4;
2404 os << string(len, ch);
2405 if (bottomLine(i)) {
2419 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2420 int cell, int row, int column,
2421 vector<unsigned int> const & clen,
2422 bool onlydata) const
2425 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2437 unsigned int len1 = sstr.str().length();
2438 unsigned int len2 = clen[column];
2439 while (isPartOfMultiColumn(row, ++column))
2440 len2 += clen[column] + 4;
2443 switch (getAlignment(cell)) {
2445 case LYX_ALIGN_LEFT:
2448 case LYX_ALIGN_RIGHT:
2452 case LYX_ALIGN_CENTER:
2458 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2460 if (rightLine(cell))
2469 int LyXTabular::ascii(Buffer const & buf, ostream & os, int const depth,
2470 bool onlydata, unsigned char delim) const
2474 // first calculate the width of the single columns
2475 vector<unsigned int> clen(columns_);
2478 // first all non (real) multicolumn cells!
2479 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))
2486 getCellInset(cell).ascii(buf, sstr, 0);
2487 if (clen[j] < sstr.str().length())
2488 clen[j] = sstr.str().length();
2491 // then all (real) multicolumn cells!
2492 for (int j = 0; j < columns_; ++j) {
2493 for (int i = 0; i < rows_; ++i) {
2494 int cell = getCellNumber(i, j);
2495 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2498 getCellInset(cell).ascii(buf, sstr, 0);
2499 int len = int(sstr.str().length());
2500 int const n = cells_in_multicolumn(cell);
2501 for (int k = j; len > 0 && k < j + n - 1; ++k)
2503 if (len > int(clen[j + n - 1]))
2504 clen[j + n - 1] = len;
2509 for (int i = 0; i < rows_; ++i) {
2510 if (!onlydata && asciiTopHLine(os, i, clen))
2511 for (int j = 0; j < depth; ++j)
2513 for (int j = 0; j < columns_; ++j) {
2514 if (isPartOfMultiColumn(i, j))
2516 if (onlydata && j > 0)
2518 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2523 for (int j = 0; j < depth; ++j)
2525 if (asciiBottomHLine(os, i, clen))
2526 for (int j = 0; j < depth; ++j)
2534 InsetText & LyXTabular::getCellInset(int cell) const
2536 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2540 InsetText & LyXTabular::getCellInset(int row, int column) const
2542 return cell_info[row][column].inset;
2546 int LyXTabular::getCellFromInset(InsetOld const * inset) const
2548 // is this inset part of the tabular?
2550 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2554 for (int cell = getNumberOfCells(); cell >= 0; --cell)
2555 if (&getCellInset(cell) == inset) {
2556 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2557 << "cell=" << cell << endl;
2561 // We should have found a cell at this point
2562 lyxerr << "LyXTabular::getCellFromInset: Cell not found!" << endl;
2567 void LyXTabular::validate(LaTeXFeatures & features) const
2569 features.require("NeedTabularnewline");
2570 if (isLongTabular())
2571 features.require("longtable");
2573 features.require("rotating");
2574 for (int cell = 0; cell < numberofcells; ++cell) {
2575 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2576 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2577 features.require("array");
2578 getCellInset(cell).validate(features);
2583 void LyXTabular::getLabelList(Buffer const & buffer,
2584 std::vector<string> & list) const
2586 for (int i = 0; i < rows_; ++i)
2587 for (int j = 0; j < columns_; ++j)
2588 getCellInset(i, j).getLabelList(buffer, list);
2592 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2594 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2595 ParagraphList::const_iterator cit = parlist.begin();
2596 ParagraphList::const_iterator end = parlist.end();
2598 for (; cit != end; ++cit)
2599 for (int i = 0; i < cit->size(); ++i)
2600 if (cit->isNewline(i))