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;
52 #ifndef CXX_GLOBAL_CSTD
59 int const WIDTH_OF_LINE = 5;
62 string const write_attribute(string const & name, T const & t)
64 string const s = tostr(t);
65 return s.empty() ? s : " " + name + "=\"" + s + "\"";
68 string const write_attribute(string const & name, string const & t)
70 return t.empty() ? t : " " + name + "=\"" + t + "\"";
74 string const write_attribute(string const & name, bool const & b)
76 // we write only true attribute values so we remove a bit of the
77 // file format bloat for tabulars.
78 return b ? write_attribute(name, tostr(b)) : string();
82 string const write_attribute(string const & name, int const & i)
84 // we write only true attribute values so we remove a bit of the
85 // file format bloat for tabulars.
86 return i ? write_attribute(name, tostr(i)) : string();
90 string const write_attribute(string const & name, LyXLength const & value)
92 // we write only the value if we really have one same reson as above.
93 return value.zero() ? string() : write_attribute(name, value.asString());
97 string const tostr(LyXAlignment const & num)
102 case LYX_ALIGN_BLOCK:
106 case LYX_ALIGN_CENTER:
108 case LYX_ALIGN_RIGHT:
110 case LYX_ALIGN_LAYOUT:
112 case LYX_ALIGN_SPECIAL:
119 string const tostr(LyXTabular::VAlignment const & num)
122 case LyXTabular::LYX_VALIGN_TOP:
124 case LyXTabular::LYX_VALIGN_MIDDLE:
126 case LyXTabular::LYX_VALIGN_BOTTOM:
133 string const tostr(LyXTabular::BoxType const & num)
136 case LyXTabular::BOX_NONE:
138 case LyXTabular::BOX_PARBOX:
140 case LyXTabular::BOX_MINIPAGE:
147 // I would have liked a fromstr template a lot better. (Lgb)
148 bool string2type(string const str, LyXAlignment & num)
151 num = LYX_ALIGN_NONE;
152 else if (str == "block")
153 num = LYX_ALIGN_BLOCK;
154 else if (str == "left")
155 num = LYX_ALIGN_LEFT;
156 else if (str == "center")
157 num = LYX_ALIGN_CENTER;
158 else if (str == "right")
159 num = LYX_ALIGN_RIGHT;
166 bool string2type(string const str, LyXTabular::VAlignment & num)
169 num = LyXTabular::LYX_VALIGN_TOP;
170 else if (str == "middle" )
171 num = LyXTabular::LYX_VALIGN_MIDDLE;
172 else if (str == "bottom")
173 num = LyXTabular::LYX_VALIGN_BOTTOM;
180 bool string2type(string const str, LyXTabular::BoxType & num)
183 num = LyXTabular::BOX_NONE;
184 else if (str == "parbox")
185 num = LyXTabular::BOX_PARBOX;
186 else if (str == "minipage")
187 num = LyXTabular::BOX_MINIPAGE;
194 bool string2type(string const str, bool & num)
198 else if (str == "false")
206 bool getTokenValue(string const & str, const char * token, string & ret)
209 size_t token_length = strlen(token);
210 string::size_type pos = str.find(token);
212 if (pos == string::npos || pos + token_length + 1 >= str.length()
213 || str[pos + token_length] != '=')
215 pos += token_length + 1;
217 if (ch != '"' && ch != '\'') { // only read till next space
221 while (pos < str.length() - 1 && str[++pos] != ch)
228 bool getTokenValue(string const & str, const char * token, int & num)
232 if (!getTokenValue(str, token, tmp))
239 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
242 if (!getTokenValue(str, token, tmp))
244 return string2type(tmp, num);
248 bool getTokenValue(string const & str, const char * token,
249 LyXTabular::VAlignment & num)
252 if (!getTokenValue(str, token, tmp))
254 return string2type(tmp, num);
258 bool getTokenValue(string const & str, const char * token,
259 LyXTabular::BoxType & num)
262 if (!getTokenValue(str, token, tmp))
264 return string2type(tmp, num);
268 bool getTokenValue(string const & str, const char * token, bool & flag)
270 // set the flag always to false as this should be the default for bools
271 // not in the file-format.
274 if (!getTokenValue(str, token, tmp))
276 return string2type(tmp, flag);
280 bool getTokenValue(string const & str, const char * token, LyXLength & len)
282 // set the lenght to be zero() as default as this it should be if not
283 // in the file format.
286 if (!getTokenValue(str, token, tmp))
288 return isValidLength(tmp, &len);
292 void l_getline(istream & is, string & str)
295 while (str.empty()) {
297 if (!str.empty() && str[str.length() - 1] == '\r')
298 str.erase(str.length() - 1);
304 /// Define a few methods for the inner structs
306 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
311 multicolumn = LyXTabular::CELL_NORMAL;
312 alignment = LYX_ALIGN_CENTER;
313 valignment = LYX_VALIGN_TOP;
323 LyXTabular::rowstruct::rowstruct()
330 endfirsthead = false;
337 LyXTabular::columnstruct::columnstruct()
341 alignment = LYX_ALIGN_CENTER;
342 valignment = LYX_VALIGN_TOP;
347 LyXTabular::ltType::ltType()
355 LyXTabular::LyXTabular(BufferParams const & bp, int rows_arg, int columns_arg)
357 init(bp, rows_arg, columns_arg);
361 // activates all lines and sets all widths to 0
362 void LyXTabular::init(BufferParams const & bp, int rows_arg, int columns_arg)
365 columns_ = columns_arg;
366 row_info = row_vector(rows_, rowstruct());
367 column_info = column_vector(columns_, columnstruct());
368 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
371 for (int i = 0; i < rows_; ++i) {
372 for (int j = 0; j < columns_; ++j) {
373 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
374 cell_info[i][j].cellno = cellno++;
376 cell_info[i].back().right_line = true;
378 row_info.back().bottom_line = true;
379 row_info.front().bottom_line = true;
380 column_info.back().right_line = true;
382 columnofcell.clear();
383 set_row_column_number_info();
384 is_long_tabular = false;
389 void LyXTabular::setOwner(InsetTabular * inset)
391 for (int i = 0; i < rows_; ++i)
392 for (int j = 0; j < columns_; ++j)
393 cell_info[i][j].inset.setOwner(inset);
397 void LyXTabular::appendRow(BufferParams const & bp, int cell)
401 int row = row_of_cell(cell);
403 row_vector::iterator rit = row_info.begin() + row;
404 row_info.insert(rit, rowstruct());
405 // now set the values of the row before
406 row_info[row] = row_info[row + 1];
409 cell_vvector::iterator cit = cell_info.begin() + row;
410 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
413 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
416 for (int i = 0; i <= row; ++i)
417 for (int j = 0; j < columns_; ++j)
418 c_info[i][j] = cell_info[i][j];
420 for (int i = row + 1; i < rows_; ++i)
421 for (int j = 0; j < columns_; ++j)
422 c_info[i][j] = cell_info[i-1][j];
426 for (int j = 0; j < columns_; ++j) {
427 cell_info[row][j].inset.clear(false);
428 if (bp.tracking_changes)
429 cell_info[row][j].inset.markNew(true);
435 void LyXTabular::deleteRow(int row)
437 // Not allowed to delete last row
441 row_info.erase(row_info.begin() + row);
442 cell_info.erase(cell_info.begin() + row);
447 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
451 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
453 int const column = column_of_cell(cell);
454 column_vector::iterator cit = column_info.begin() + column + 1;
455 column_info.insert(cit, columnstruct());
456 // set the column values of the column before
457 column_info[column + 1] = column_info[column];
459 for (int i = 0; i < rows_; ++i) {
460 for (int j = 0; j <= column; ++j)
461 c_info[i][j] = cell_info[i][j];
463 for (int j = column + 1; j < columns_; ++j)
464 c_info[i][j] = cell_info[i][j - 1];
466 // care about multicolumns
467 if (c_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
468 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
470 if (column + 2 >= columns_
471 || c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
472 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
476 for (int i = 0; i < rows_; ++i) {
477 cell_info[i][column + 1].inset.clear(false);
478 if (bp.tracking_changes)
479 cell_info[i][column + 1].inset.markNew(true);
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);
497 void LyXTabular::set_row_column_number_info(bool oldformat)
500 for (int row = 0; row < rows_; ++row) {
501 for (int column = 0; column < columns_; ++column) {
502 if (cell_info[row][column].multicolumn
503 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
505 cell_info[row][column].cellno = numberofcells;
508 ++numberofcells; // because this is one more than as we start from 0
510 rowofcell.resize(numberofcells);
511 columnofcell.resize(numberofcells);
513 for (int row = 0, column = 0, c = 0;
514 c < numberofcells && row < rows_ && column < columns_;) {
516 columnofcell[c] = column;
520 } while (column < columns_ &&
521 cell_info[row][column].multicolumn
522 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
524 if (column == columns_) {
530 for (int row = 0; row < rows_; ++row) {
531 for (int column = 0; column < columns_; ++column) {
532 if (isPartOfMultiColumn(row,column))
534 // now set the right line of multicolumns right for oldformat read
536 cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
538 int cn = cells_in_multicolumn(cell_info[row][column].cellno);
539 cell_info[row][column].right_line =
540 cell_info[row][column+cn-1].right_line;
542 cell_info[row][column].inset.setAutoBreakRows(
543 !getPWidth(getCellNumber(row, column)).zero());
549 int LyXTabular::getNumberOfCells() const
551 return numberofcells;
555 int LyXTabular::numberOfCellsInRow(int cell) const
557 int const row = row_of_cell(cell);
559 for (int i = 0; i < columns_; ++i)
560 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
566 // returns 1 if there is a topline, returns 0 if not
567 bool LyXTabular::topLine(int cell, bool onlycolumn) const
569 if (!onlycolumn && isMultiColumn(cell))
570 return cellinfo_of_cell(cell).top_line;
571 return row_info[row_of_cell(cell)].top_line;
575 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
577 if (!onlycolumn && isMultiColumn(cell))
578 return cellinfo_of_cell(cell).bottom_line;
579 return row_info[row_of_cell(cell)].bottom_line;
583 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
585 if (!onlycolumn && isMultiColumn(cell) &&
586 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
588 if (cellinfo_of_cell(cell).align_special.empty())
589 return cellinfo_of_cell(cell).left_line;
590 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
592 if (column_info[column_of_cell(cell)].align_special.empty())
593 return column_info[column_of_cell(cell)].left_line;
594 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
598 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
600 if (!onlycolumn && isMultiColumn(cell) &&
601 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
603 if (cellinfo_of_cell(cell).align_special.empty())
604 return cellinfo_of_cell(cell).right_line;
605 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
607 if (column_info[column_of_cell(cell)].align_special.empty())
608 return column_info[right_column_of_cell(cell)].right_line;
609 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
613 bool LyXTabular::topAlreadyDrawn(int cell) const
615 int row = row_of_cell(cell);
616 if (row > 0 && !getAdditionalHeight(row)) {
617 int column = column_of_cell(cell);
620 && cell_info[row][column].multicolumn
621 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
623 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
624 return row_info[row].bottom_line;
626 return cell_info[row][column].bottom_line;
632 bool LyXTabular::leftAlreadyDrawn(int cell) const
634 int column = column_of_cell(cell);
636 int row = row_of_cell(cell);
638 (cell_info[row][column].multicolumn ==
639 LyXTabular::CELL_PART_OF_MULTICOLUMN));
640 if (getAdditionalWidth(cell_info[row][column].cellno))
642 return rightLine(cell_info[row][column].cellno);
648 bool LyXTabular::isLastRow(int cell) const
650 return (row_of_cell(cell) == rows_ - 1);
654 int LyXTabular::getAdditionalHeight(int row) const
656 if (!row || row >= rows_)
662 for (int column = 0; column < columns_ && bottom; ++column) {
663 switch (cell_info[row - 1][column].multicolumn) {
664 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
665 bottom = cell_info[row - 1][column].bottom_line;
667 case LyXTabular::CELL_NORMAL:
668 bottom = row_info[row - 1].bottom_line;
671 for (int column = 0; column < columns_ && top; ++column) {
672 switch (cell_info[row][column].multicolumn) {
673 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
674 top = cell_info[row][column].top_line;
676 case LyXTabular::CELL_NORMAL:
677 top = row_info[row].top_line;
681 return WIDTH_OF_LINE;
686 int LyXTabular::getAdditionalWidth(int cell) const
688 // internally already set in setWidthOfCell
689 // used to get it back in text.C
690 int const col = right_column_of_cell(cell);
691 int const row = row_of_cell(cell);
692 if (col < columns_ - 1 && rightLine(cell) &&
693 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
695 return WIDTH_OF_LINE;
701 // returns the maximum over all rows
702 int LyXTabular::getWidthOfColumn(int cell) const
704 int const column1 = column_of_cell(cell);
705 int const column2 = right_column_of_cell(cell);
707 for (int i = column1; i <= column2; ++i)
708 result += column_info[i].width_of_column;
713 int LyXTabular::getWidthOfTabular() const
715 return width_of_tabular;
719 // returns true if a complete update is necessary, otherwise false
720 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
722 if (!isMultiColumn(cell))
725 int const row = row_of_cell(cell);
726 int const column1 = column_of_cell(cell);
727 int const column2 = right_column_of_cell(cell);
728 int const old_val = cell_info[row][column2].width_of_cell;
730 // first set columns to 0 so we can calculate the right width
731 for (int i = column1; i <= column2; ++i) {
732 cell_info[row][i].width_of_cell = 0;
734 // set the width to MAX_WIDTH until width > 0
735 int width = new_width + 2 * WIDTH_OF_LINE;
737 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
738 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
739 width -= column_info[i].width_of_column;
742 cell_info[row][i].width_of_cell = width;
744 if (old_val != cell_info[row][column2].width_of_cell) {
745 // in this case we have to recalculate all multicolumn cells which
746 // have this column as one of theirs but not as last one
747 calculate_width_of_column_NMC(i);
748 recalculateMulticolumnsOfColumn(i);
749 calculate_width_of_column(i);
755 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
757 // the last column does not have to be recalculated because all
758 // multicolumns will have here there last multicolumn cell which
759 // always will have the whole rest of the width of the cell.
760 if (column > (columns_ - 2))
762 for(int row = 0; row < rows_; ++row) {
763 int mc = cell_info[row][column].multicolumn;
764 int nmc = cell_info[row][column+1].multicolumn;
765 // we only have to update multicolumns which do not have this
766 // column as their last column!
767 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
768 (mc == CELL_PART_OF_MULTICOLUMN &&
769 nmc == CELL_PART_OF_MULTICOLUMN))
771 int const cellno = cell_info[row][column].cellno;
772 setWidthOfMulticolCell(cellno,
773 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
779 // returns 1 if a complete update is necessary, otherwise 0
780 void LyXTabular::setWidthOfCell(int cell, int new_width)
782 int const row = row_of_cell(cell);
783 int const column1 = column_of_cell(cell);
788 if (rightLine(cell_info[row][column1].cellno, true) &&
789 column1 < columns_ - 1 &&
790 leftLine(cell_info[row][column1+1].cellno, true))
792 add_width = WIDTH_OF_LINE;
795 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
798 if (isMultiColumnReal(cell)) {
799 tmp = setWidthOfMulticolCell(cell, new_width);
801 width = new_width + 2 * WIDTH_OF_LINE + add_width;
802 cell_info[row][column1].width_of_cell = width;
803 tmp = calculate_width_of_column_NMC(column1);
805 recalculateMulticolumnsOfColumn(column1);
808 for (int i = 0; i < columns_; ++i)
809 calculate_width_of_column(i);
810 calculate_width_of_tabular();
815 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
817 if (!isMultiColumn(cell) || onlycolumn)
818 column_info[column_of_cell(cell)].alignment = align;
820 cellinfo_of_cell(cell).alignment = align;
824 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
826 if (!isMultiColumn(cell) || onlycolumn)
827 column_info[column_of_cell(cell)].valignment = align;
829 cellinfo_of_cell(cell).valignment = align;
833 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
835 int const j = column_of_cell(cell);
837 column_info[j].p_width = width;
838 for (int i = 0; i < rows_; ++i) {
839 int const cell = getCellNumber(i, j);
840 // because of multicolumns
841 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
846 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
848 if (!isMultiColumn(cell))
851 cellinfo_of_cell(cell).p_width = width;
852 getCellInset(cell).setAutoBreakRows(!width.zero());
857 void LyXTabular::setAlignSpecial(int cell, string const & special,
858 LyXTabular::Feature what)
860 if (what == SET_SPECIAL_MULTI)
861 cellinfo_of_cell(cell).align_special = special;
863 column_info[column_of_cell(cell)].align_special = special;
867 void LyXTabular::setAllLines(int cell, bool line)
869 setTopLine(cell, line);
870 setBottomLine(cell, line);
871 setRightLine(cell, line);
872 setLeftLine(cell, line);
876 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
878 int const row = row_of_cell(cell);
879 if (onlycolumn || !isMultiColumn(cell))
880 row_info[row].top_line = line;
882 cellinfo_of_cell(cell).top_line = line;
886 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
888 if (onlycolumn || !isMultiColumn(cell))
889 row_info[row_of_cell(cell)].bottom_line = line;
891 cellinfo_of_cell(cell).bottom_line = line;
895 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
897 if (onlycolumn || !isMultiColumn(cell))
898 column_info[column_of_cell(cell)].left_line = line;
900 cellinfo_of_cell(cell).left_line = line;
904 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
906 if (onlycolumn || !isMultiColumn(cell))
907 column_info[right_column_of_cell(cell)].right_line = line;
909 cellinfo_of_cell(cell).right_line = line;
913 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
915 if (!onlycolumn && isMultiColumn(cell))
916 return cellinfo_of_cell(cell).alignment;
917 return column_info[column_of_cell(cell)].alignment;
921 LyXTabular::VAlignment
922 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
924 if (!onlycolumn && isMultiColumn(cell))
925 return cellinfo_of_cell(cell).valignment;
926 return column_info[column_of_cell(cell)].valignment;
930 LyXLength const LyXTabular::getPWidth(int cell) const
932 if (isMultiColumn(cell))
933 return cellinfo_of_cell(cell).p_width;
934 return column_info[column_of_cell(cell)].p_width;
938 LyXLength const LyXTabular::getColumnPWidth(int cell) const
940 return column_info[column_of_cell(cell)].p_width;
944 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
946 if (isMultiColumn(cell))
947 return cellinfo_of_cell(cell).p_width;
952 string const LyXTabular::getAlignSpecial(int cell, int what) const
954 if (what == SET_SPECIAL_MULTI)
955 return cellinfo_of_cell(cell).align_special;
956 return column_info[column_of_cell(cell)].align_special;
960 int LyXTabular::getWidthOfCell(int cell) const
962 int const row = row_of_cell(cell);
963 int const column1 = column_of_cell(cell);
964 int const column2 = right_column_of_cell(cell);
966 for (int i = column1; i <= column2; ++i)
967 result += cell_info[row][i].width_of_cell;
972 int LyXTabular::getBeginningOfTextInCell(int cell) const
976 switch (getAlignment(cell)) {
977 case LYX_ALIGN_CENTER:
978 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
980 case LYX_ALIGN_RIGHT:
981 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
982 // + getAdditionalWidth(cell);
985 // LYX_ALIGN_LEFT: nothing :-)
995 bool LyXTabular::isFirstCellInRow(int cell) const
997 return column_of_cell(cell) == 0;
1001 int LyXTabular::getFirstCellInRow(int row) const
1003 if (row > rows_ - 1)
1005 return cell_info[row][0].cellno;
1009 bool LyXTabular::isLastCellInRow(int cell) const
1011 return right_column_of_cell(cell) == columns_ - 1;
1015 int LyXTabular::getLastCellInRow(int row) const
1017 if (row > rows_ - 1)
1019 return cell_info[row][columns_-1].cellno;
1023 void LyXTabular::calculate_width_of_column(int column)
1026 for (int i = 0; i < rows_; ++i)
1027 maximum = max(cell_info[i][column].width_of_cell, maximum);
1028 column_info[column].width_of_column = maximum;
1033 // Calculate the columns regarding ONLY the normal cells and if this
1034 // column is inside a multicolumn cell then use it only if its the last
1035 // column of this multicolumn cell as this gives an added width to the
1036 // column, all the rest should be adapted!
1038 bool LyXTabular::calculate_width_of_column_NMC(int column)
1040 int const old_column_width = column_info[column].width_of_column;
1042 for (int i = 0; i < rows_; ++i) {
1043 int cell = getCellNumber(i, column);
1044 bool ismulti = isMultiColumnReal(cell);
1045 if ((!ismulti || column == right_column_of_cell(cell)) &&
1046 cell_info[i][column].width_of_cell > max)
1048 max = cell_info[i][column].width_of_cell;
1051 column_info[column].width_of_column = max;
1052 return column_info[column].width_of_column != old_column_width;
1056 void LyXTabular::calculate_width_of_tabular()
1058 width_of_tabular = 0;
1059 for (int i = 0; i < columns_; ++i)
1060 width_of_tabular += column_info[i].width_of_column;
1064 int LyXTabular::row_of_cell(int cell) const
1066 if (cell >= numberofcells)
1070 return rowofcell[cell];
1074 int LyXTabular::column_of_cell(int cell) const
1076 if (cell >= numberofcells)
1077 return columns_ - 1;
1080 return columnofcell[cell];
1084 int LyXTabular::right_column_of_cell(int cell) const
1086 int const row = row_of_cell(cell);
1087 int column = column_of_cell(cell);
1088 while (column < columns_ - 1 &&
1089 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1095 void LyXTabular::write(Buffer const & buf, ostream & os) const
1099 << write_attribute("version", 3)
1100 << write_attribute("rows", rows_)
1101 << write_attribute("columns", columns_)
1103 // global longtable options
1105 << write_attribute("rotate", rotate)
1106 << write_attribute("islongtable", is_long_tabular)
1107 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1108 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1109 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1110 << write_attribute("headTopDL", endhead.topDL)
1111 << write_attribute("headBottomDL", endhead.bottomDL)
1112 << write_attribute("footTopDL", endfoot.topDL)
1113 << write_attribute("footBottomDL", endfoot.bottomDL)
1114 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1115 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1116 << write_attribute("lastFootEmpty", endlastfoot.empty)
1118 for (int j = 0; j < columns_; ++j) {
1120 << write_attribute("alignment", column_info[j].alignment)
1121 << write_attribute("valignment", column_info[j].valignment)
1122 << write_attribute("leftline", column_info[j].left_line)
1123 << write_attribute("rightline", column_info[j].right_line)
1124 << write_attribute("width", column_info[j].p_width.asString())
1125 << write_attribute("special", column_info[j].align_special)
1128 for (int i = 0; i < rows_; ++i) {
1130 << write_attribute("topline", row_info[i].top_line)
1131 << write_attribute("bottomline", row_info[i].bottom_line)
1132 << write_attribute("endhead", row_info[i].endhead)
1133 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1134 << write_attribute("endfoot", row_info[i].endfoot)
1135 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1136 << write_attribute("newpage", row_info[i].newpage)
1138 for (int j = 0; j < columns_; ++j) {
1140 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1141 << write_attribute("alignment", cell_info[i][j].alignment)
1142 << write_attribute("valignment", cell_info[i][j].valignment)
1143 << write_attribute("topline", cell_info[i][j].top_line)
1144 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1145 << write_attribute("leftline", cell_info[i][j].left_line)
1146 << write_attribute("rightline", cell_info[i][j].right_line)
1147 << write_attribute("rotate", cell_info[i][j].rotate)
1148 << write_attribute("usebox", cell_info[i][j].usebox)
1149 << write_attribute("width", cell_info[i][j].p_width)
1150 << write_attribute("special", cell_info[i][j].align_special)
1152 os << "\\begin_inset ";
1153 cell_info[i][j].inset.write(buf, os);
1154 os << "\n\\end_inset \n"
1159 os << "</lyxtabular>\n";
1163 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1167 row_info[--hr].endhead = true;
1169 // set firstheader info
1170 if (fhr && fhr < rows_) {
1171 if (row_info[fhr].endhead) {
1173 row_info[--fhr].endfirsthead = true;
1174 row_info[fhr].endhead = false;
1176 } else if (row_info[fhr - 1].endhead) {
1177 endfirsthead.empty = true;
1179 while (fhr > 0 && !row_info[--fhr].endhead) {
1180 row_info[fhr].endfirsthead = true;
1185 if (fr && fr < rows_) {
1186 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1187 while (fr > 0 && !row_info[--fr].endhead) {
1188 row_info[fr].endfoot = true;
1189 row_info[fr].endhead = false;
1191 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1192 while (fr > 0 && !row_info[--fr].endfirsthead) {
1193 row_info[fr].endfoot = true;
1194 row_info[fr].endfirsthead = false;
1196 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1197 while (fr > 0 && !row_info[--fr].endhead &&
1198 !row_info[fr].endfirsthead)
1200 row_info[fr].endfoot = true;
1204 // set lastfooter info
1205 if (lfr && lfr < rows_) {
1206 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1207 while (lfr > 0 && !row_info[--lfr].endhead) {
1208 row_info[lfr].endlastfoot = true;
1209 row_info[lfr].endhead = false;
1211 } else if (row_info[lfr].endfirsthead &&
1212 row_info[lfr - 1].endfirsthead)
1214 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1215 row_info[lfr].endlastfoot = true;
1216 row_info[lfr].endfirsthead = false;
1218 } else if (row_info[lfr].endfoot
1219 && row_info[lfr - 1].endfoot) {
1220 while (lfr > 0 && !row_info[--lfr].endfoot) {
1221 row_info[lfr].endlastfoot = true;
1222 row_info[lfr].endfoot = false;
1224 } else if (!row_info[fr - 1].endhead
1225 && !row_info[fr - 1].endfirsthead &&
1226 !row_info[fr - 1].endfoot)
1229 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1230 !row_info[lfr].endfoot)
1232 row_info[lfr].endlastfoot = true;
1234 } else if (haveLTFoot()) {
1235 endlastfoot.empty = true;
1241 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1244 istream & is = lex.getStream();
1246 l_getline(is, line);
1247 if (!prefixIs(line, "<lyxtabular ")
1248 && !prefixIs(line, "<LyXTabular ")) {
1249 BOOST_ASSERT(false);
1254 if (!getTokenValue(line, "version", version))
1256 BOOST_ASSERT(version >= 2);
1259 if (!getTokenValue(line, "rows", rows_arg))
1262 if (!getTokenValue(line, "columns", columns_arg))
1264 init(buf.params(), rows_arg, columns_arg);
1265 l_getline(is, line);
1266 if (!prefixIs(line, "<features")) {
1267 lyxerr << "Wrong tabular format (expected <features ...> got"
1268 << line << ')' << endl;
1271 getTokenValue(line, "rotate", rotate);
1272 getTokenValue(line, "islongtable", is_long_tabular);
1273 // compatibility read for old longtable options. Now we can make any
1274 // row part of the header/footer type we want before it was strict
1275 // sequential from the first row down (as LaTeX does it!). So now when
1276 // we find a header/footer line we have to go up the rows and set it
1277 // on all preceding rows till the first or one with already a h/f option
1278 // set. If we find a firstheader on the same line as a header or a
1279 // lastfooter on the same line as a footer then this should be set empty.
1287 getTokenValue(line, "endhead", hrow);
1288 getTokenValue(line, "endfirsthead", fhrow);
1289 getTokenValue(line, "endfoot", frow);
1290 getTokenValue(line, "endlastfoot", lfrow);
1291 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1293 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1294 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1295 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1296 getTokenValue(line, "headTopDL", endhead.topDL);
1297 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1298 getTokenValue(line, "footTopDL", endfoot.topDL);
1299 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1300 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1301 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1302 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1304 for (int j = 0; j < columns_; ++j) {
1306 if (!prefixIs(line,"<column")) {
1307 lyxerr << "Wrong tabular format (expected <column ...> got"
1308 << line << ')' << endl;
1311 getTokenValue(line, "alignment", column_info[j].alignment);
1312 getTokenValue(line, "valignment", column_info[j].valignment);
1313 getTokenValue(line, "leftline", column_info[j].left_line);
1314 getTokenValue(line, "rightline", column_info[j].right_line);
1315 getTokenValue(line, "width", column_info[j].p_width);
1316 getTokenValue(line, "special", column_info[j].align_special);
1319 for (int i = 0; i < rows_; ++i) {
1320 l_getline(is, line);
1321 if (!prefixIs(line, "<row")) {
1322 lyxerr << "Wrong tabular format (expected <row ...> got"
1323 << line << ')' << endl;
1326 getTokenValue(line, "topline", row_info[i].top_line);
1327 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1328 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1329 getTokenValue(line, "endhead", row_info[i].endhead);
1330 getTokenValue(line, "endfoot", row_info[i].endfoot);
1331 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1332 getTokenValue(line, "newpage", row_info[i].newpage);
1333 for (int j = 0; j < columns_; ++j) {
1334 l_getline(is, line);
1335 if (!prefixIs(line, "<cell")) {
1336 lyxerr << "Wrong tabular format (expected <cell ...> got"
1337 << line << ')' << endl;
1340 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1341 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1342 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1343 getTokenValue(line, "topline", cell_info[i][j].top_line);
1344 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1345 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1346 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1347 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1348 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1349 getTokenValue(line, "width", cell_info[i][j].p_width);
1350 getTokenValue(line, "special", cell_info[i][j].align_special);
1351 l_getline(is, line);
1352 if (prefixIs(line, "\\begin_inset")) {
1353 cell_info[i][j].inset.read(buf, lex);
1354 l_getline(is, line);
1356 if (!prefixIs(line, "</cell>")) {
1357 lyxerr << "Wrong tabular format (expected </cell> got"
1358 << line << ')' << endl;
1362 l_getline(is, line);
1363 if (!prefixIs(line, "</row>")) {
1364 lyxerr << "Wrong tabular format (expected </row> got"
1365 << line << ')' << endl;
1369 while (!prefixIs(line, "</lyxtabular>")) {
1370 l_getline(is, line);
1372 set_row_column_number_info();
1376 bool LyXTabular::isMultiColumn(int cell) const
1378 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1382 bool LyXTabular::isMultiColumnReal(int cell) const
1384 return column_of_cell(cell) != right_column_of_cell(cell) &&
1385 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1389 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1391 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1395 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1397 cellstruct & cs = cellinfo_of_cell(cell);
1398 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1399 cs.alignment = column_info[column_of_cell(cell)].alignment;
1400 cs.top_line = row_info[row_of_cell(cell)].top_line;
1401 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1402 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1403 for (int i = 1; i < number; ++i) {
1404 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1405 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1406 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1407 cs1.inset.clear(false);
1409 set_row_column_number_info();
1413 int LyXTabular::cells_in_multicolumn(int cell) const
1415 int const row = row_of_cell(cell);
1416 int column = column_of_cell(cell);
1419 while (column < columns_ &&
1420 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1429 int LyXTabular::unsetMultiColumn(int cell)
1431 int const row = row_of_cell(cell);
1432 int column = column_of_cell(cell);
1436 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1437 cell_info[row][column].multicolumn = CELL_NORMAL;
1439 while (column < columns_ &&
1440 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1442 cell_info[row][column].multicolumn = CELL_NORMAL;
1447 set_row_column_number_info();
1452 void LyXTabular::setLongTabular(bool what)
1454 is_long_tabular = what;
1458 bool LyXTabular::isLongTabular() const
1460 return is_long_tabular;
1464 void LyXTabular::setRotateTabular(bool flag)
1470 bool LyXTabular::getRotateTabular() const
1476 void LyXTabular::setRotateCell(int cell, bool flag)
1478 cellinfo_of_cell(cell).rotate = flag;
1482 bool LyXTabular::getRotateCell(int cell) const
1484 return cellinfo_of_cell(cell).rotate;
1488 bool LyXTabular::needRotating() const
1492 for (int i = 0; i < rows_; ++i)
1493 for (int j = 0; j < columns_; ++j)
1494 if (cell_info[i][j].rotate)
1500 bool LyXTabular::isLastCell(int cell) const
1502 if (cell + 1 < numberofcells)
1508 int LyXTabular::getCellAbove(int cell) const
1510 if (row_of_cell(cell) > 0)
1511 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1516 int LyXTabular::getCellBelow(int cell) const
1518 if (row_of_cell(cell) + 1 < rows_)
1519 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1524 int LyXTabular::getLastCellAbove(int cell) const
1526 if (row_of_cell(cell) <= 0)
1528 if (!isMultiColumn(cell))
1529 return getCellAbove(cell);
1530 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1534 int LyXTabular::getLastCellBelow(int cell) const
1536 if (row_of_cell(cell) + 1 >= rows_)
1538 if (!isMultiColumn(cell))
1539 return getCellBelow(cell);
1540 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1544 int LyXTabular::getCellNumber(int row, int column) const
1546 BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1547 return cell_info[row][column].cellno;
1551 void LyXTabular::setUsebox(int cell, BoxType type)
1553 cellinfo_of_cell(cell).usebox = type;
1557 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1559 if (column_info[column_of_cell(cell)].p_width.zero() &&
1560 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1562 if (cellinfo_of_cell(cell).usebox > 1)
1563 return cellinfo_of_cell(cell).usebox;
1564 return useParbox(cell);
1569 // This are functions used for the longtable support
1571 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1576 row_info[row].endfirsthead = flag;
1580 row_info[row].endhead = flag;
1585 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1588 hd.set = haveLTHead();
1589 return row_info[row].endhead;
1593 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1596 hd.set = haveLTFirstHead();
1597 return row_info[row].endfirsthead;
1601 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1606 row_info[row].endlastfoot = flag;
1610 row_info[row].endfoot = flag;
1615 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1618 fd.set = haveLTFoot();
1619 return row_info[row].endfoot;
1623 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1626 fd.set = haveLTLastFoot();
1627 return row_info[row].endlastfoot;
1631 void LyXTabular::setLTNewPage(int row, bool what)
1633 row_info[row].newpage = what;
1637 bool LyXTabular::getLTNewPage(int row) const
1639 return row_info[row].newpage;
1643 bool LyXTabular::haveLTHead() const
1645 for (int i = 0; i < rows_; ++i)
1646 if (row_info[i].endhead)
1652 bool LyXTabular::haveLTFirstHead() const
1654 if (endfirsthead.empty)
1656 for (int i = 0; i < rows_; ++i)
1657 if (row_info[i].endfirsthead)
1663 bool LyXTabular::haveLTFoot() const
1665 for (int i = 0; i < rows_; ++i)
1666 if (row_info[i].endfoot)
1672 bool LyXTabular::haveLTLastFoot() const
1674 if (endlastfoot.empty)
1676 for (int i = 0; i < rows_; ++i)
1677 if (row_info[i].endlastfoot)
1683 // end longtable support functions
1685 void LyXTabular::setAscentOfRow(int row, int height)
1687 if (row >= rows_ || row_info[row].ascent_of_row == height)
1689 row_info[row].ascent_of_row = height;
1693 void LyXTabular::setDescentOfRow(int row, int height)
1695 if (row >= rows_ || row_info[row].descent_of_row == height)
1697 row_info[row].descent_of_row = height;
1701 int LyXTabular::getAscentOfRow(int row) const
1705 return row_info[row].ascent_of_row;
1709 int LyXTabular::getDescentOfRow(int row) const
1713 return row_info[row].descent_of_row;
1717 int LyXTabular::getHeightOfTabular() const
1720 for (int row = 0; row < rows_; ++row)
1721 height += getAscentOfRow(row) + getDescentOfRow(row) +
1722 getAdditionalHeight(row);
1727 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1729 if (row >= rows_ || column >= columns_)
1731 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1735 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1737 if (row < 0 || row >= rows_)
1740 int const fcell = getFirstCellInRow(row);
1741 int const n = numberOfCellsInRow(fcell) + fcell;
1744 for (int i = fcell; i < n; ++i) {
1748 if (tmp == n - fcell) {
1751 for (int i = fcell; i < n; ++i) {
1754 << column_of_cell(i) + 1
1756 << right_column_of_cell(i) + 1
1768 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1770 if (row < 0 || row >= rows_)
1773 int const fcell = getFirstCellInRow(row);
1774 int const n = numberOfCellsInRow(fcell) + fcell;
1777 for (int i = fcell; i < n; ++i) {
1781 if (tmp == n - fcell) {
1784 for (int i = fcell; i < n; ++i) {
1785 if (bottomLine(i)) {
1787 << column_of_cell(i) + 1
1789 << right_column_of_cell(i) + 1
1801 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1805 if (getRotateCell(cell)) {
1806 os << "\\begin{sideways}\n";
1809 if (isMultiColumn(cell)) {
1810 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1811 if (!cellinfo_of_cell(cell).align_special.empty()) {
1812 os << cellinfo_of_cell(cell).align_special << "}{";
1814 if (leftLine(cell) &&
1815 (isFirstCellInRow(cell) ||
1816 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1817 !rightLine(cell - 1, true))))
1821 if (!getPWidth(cell).zero()) {
1822 switch (getVAlignment(cell)) {
1823 case LYX_VALIGN_TOP:
1826 case LYX_VALIGN_MIDDLE:
1829 case LYX_VALIGN_BOTTOM:
1834 << getPWidth(cell).asLatexString()
1837 switch (getAlignment(cell)) {
1838 case LYX_ALIGN_LEFT:
1841 case LYX_ALIGN_RIGHT:
1849 if (rightLine(cell))
1851 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1857 if (getUsebox(cell) == BOX_PARBOX) {
1859 switch (getVAlignment(cell)) {
1860 case LYX_VALIGN_TOP:
1863 case LYX_VALIGN_MIDDLE:
1866 case LYX_VALIGN_BOTTOM:
1870 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1871 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1872 os << "\\begin{minipage}[";
1873 switch (getVAlignment(cell)) {
1874 case LYX_VALIGN_TOP:
1877 case LYX_VALIGN_MIDDLE:
1880 case LYX_VALIGN_BOTTOM:
1884 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1891 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1896 if (getUsebox(cell) == BOX_PARBOX)
1898 else if (getUsebox(cell) == BOX_MINIPAGE) {
1899 os << "%\n\\end{minipage}";
1902 if (isMultiColumn(cell)) {
1905 if (getRotateCell(cell)) {
1906 os << "%\n\\end{sideways}";
1913 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1914 LatexRunParams const & runparams) const
1916 if (!is_long_tabular)
1920 // output header info
1922 if (endhead.topDL) {
1926 for (int i = 0; i < rows_; ++i) {
1927 if (row_info[i].endhead) {
1928 ret += TeXRow(os, i, buf, runparams);
1931 if (endhead.bottomDL) {
1935 os << "\\endhead\n";
1937 if (endfirsthead.empty) {
1938 os << "\\endfirsthead\n";
1942 // output firstheader info
1943 if (haveLTFirstHead()) {
1944 if (endfirsthead.topDL) {
1948 for (int i = 0; i < rows_; ++i) {
1949 if (row_info[i].endfirsthead) {
1950 ret += TeXRow(os, i, buf, runparams);
1953 if (endfirsthead.bottomDL) {
1957 os << "\\endfirsthead\n";
1960 // output footer info
1962 if (endfoot.topDL) {
1966 for (int i = 0; i < rows_; ++i) {
1967 if (row_info[i].endfoot) {
1968 ret += TeXRow(os, i, buf, runparams);
1971 if (endfoot.bottomDL) {
1975 os << "\\endfoot\n";
1977 if (endlastfoot.empty) {
1978 os << "\\endlastfoot\n";
1982 // output lastfooter info
1983 if (haveLTLastFoot()) {
1984 if (endlastfoot.topDL) {
1988 for (int i = 0; i < rows_; ++i) {
1989 if (row_info[i].endlastfoot) {
1990 ret += TeXRow(os, i, buf, runparams);
1993 if (endlastfoot.bottomDL) {
1997 os << "\\endlastfoot\n";
2004 bool LyXTabular::isValidRow(int row) const
2006 if (!is_long_tabular)
2008 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2009 !row_info[row].endfoot && !row_info[row].endlastfoot;
2013 int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
2014 LatexRunParams const & runparams) const
2017 int cell = getCellNumber(i, 0);
2018 BufferParams const & bufferparams = buf.params();
2020 ret += TeXTopHLine(os, i);
2021 for (int j = 0; j < columns_; ++j) {
2022 if (isPartOfMultiColumn(i, j))
2024 ret += TeXCellPreamble(os, cell);
2025 InsetText & inset = getCellInset(cell);
2027 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(bufferparams) &&
2028 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2032 ret += inset.latex(buf, os, runparams);
2036 ret += TeXCellPostamble(os, cell);
2037 if (!isLastCellInRow(cell)) { // not last cell in row
2043 os << "\\tabularnewline\n";
2045 ret += TeXBottomHLine(os, i);
2050 int LyXTabular::latex(Buffer const & buf, ostream & os,
2051 LatexRunParams const & runparams) const
2055 //+---------------------------------------------------------------------
2056 //+ first the opening preamble +
2057 //+---------------------------------------------------------------------
2060 os << "\\begin{sideways}\n";
2063 if (is_long_tabular)
2064 os << "\\begin{longtable}{";
2066 os << "\\begin{tabular}{";
2067 for (int i = 0; i < columns_; ++i) {
2068 if (!column_info[i].align_special.empty()) {
2069 os << column_info[i].align_special;
2071 if (column_info[i].left_line)
2073 if (!column_info[i].p_width.zero()) {
2074 switch (column_info[i].alignment) {
2075 case LYX_ALIGN_LEFT:
2076 os << ">{\\raggedright}";
2078 case LYX_ALIGN_RIGHT:
2079 os << ">{\\raggedleft}";
2081 case LYX_ALIGN_CENTER:
2082 os << ">{\\centering}";
2084 case LYX_ALIGN_NONE:
2085 case LYX_ALIGN_BLOCK:
2086 case LYX_ALIGN_LAYOUT:
2087 case LYX_ALIGN_SPECIAL:
2091 switch (column_info[i].valignment) {
2092 case LYX_VALIGN_TOP:
2095 case LYX_VALIGN_MIDDLE:
2098 case LYX_VALIGN_BOTTOM:
2103 << column_info[i].p_width.asLatexString()
2106 switch (column_info[i].alignment) {
2107 case LYX_ALIGN_LEFT:
2110 case LYX_ALIGN_RIGHT:
2118 if (column_info[i].right_line)
2125 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2127 //+---------------------------------------------------------------------
2128 //+ the single row and columns (cells) +
2129 //+---------------------------------------------------------------------
2131 for (int i = 0; i < rows_; ++i) {
2132 if (isValidRow(i)) {
2133 ret += TeXRow(os, i, buf, runparams);
2134 if (is_long_tabular && row_info[i].newpage) {
2135 os << "\\newpage\n";
2141 //+---------------------------------------------------------------------
2142 //+ the closing of the tabular +
2143 //+---------------------------------------------------------------------
2145 if (is_long_tabular)
2146 os << "\\end{longtable}";
2148 os << "\\end{tabular}";
2150 os << "\n\\end{sideways}";
2158 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os) const
2160 os << "<tabular ca=\"";
2161 for (int i = 0; i < columns_; ++i) {
2162 switch (column_info[i].alignment) {
2163 case LYX_ALIGN_LEFT:
2166 case LYX_ALIGN_RIGHT:
2177 for (int i = 0; i < rows_; ++i) {
2178 for (int j = 0; j < columns_; ++j) {
2179 if (isPartOfMultiColumn(i, j))
2181 InsetText & inset = getCellInset(cell);
2183 ret += inset.linuxdoc(buf, os);
2185 if (isLastCellInRow(cell)) {
2194 os << "</tabular>\n";
2199 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row) const
2202 int cell = getFirstCellInRow(row);
2205 for (int j = 0; j < columns_; ++j) {
2206 if (isPartOfMultiColumn(row, j))
2209 os << "<entry align=\"";
2210 switch (getAlignment(cell)) {
2211 case LYX_ALIGN_LEFT:
2214 case LYX_ALIGN_RIGHT:
2222 os << "\" valign=\"";
2223 switch (getVAlignment(cell)) {
2224 case LYX_VALIGN_TOP:
2227 case LYX_VALIGN_BOTTOM:
2230 case LYX_VALIGN_MIDDLE:
2235 if (isMultiColumn(cell)) {
2236 os << " namest=\"col" << j << "\" ";
2237 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2241 ret += getCellInset(cell).docbook(buf, os, true);
2250 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2251 bool /*mixcont*/) const
2255 //+---------------------------------------------------------------------
2256 //+ first the opening preamble +
2257 //+---------------------------------------------------------------------
2259 os << "<tgroup cols=\"" << columns_
2260 << "\" colsep=\"1\" rowsep=\"1\">\n";
2262 for (int i = 0; i < columns_; ++i) {
2263 os << "<colspec colname=\"col" << i << "\" align=\"";
2264 switch (column_info[i].alignment) {
2265 case LYX_ALIGN_LEFT:
2268 case LYX_ALIGN_RIGHT:
2279 //+---------------------------------------------------------------------
2280 //+ Long Tabular case +
2281 //+---------------------------------------------------------------------
2283 // output header info
2284 if (haveLTHead() || haveLTFirstHead()) {
2287 for (int i = 0; i < rows_; ++i) {
2288 if (row_info[i].endhead || row_info[i].endfirsthead) {
2289 ret += docbookRow(buf, os, i);
2295 // output footer info
2296 if (haveLTFoot() || haveLTLastFoot()) {
2299 for (int i = 0; i < rows_; ++i) {
2300 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2301 ret += docbookRow(buf, os, i);
2308 //+---------------------------------------------------------------------
2309 //+ the single row and columns (cells) +
2310 //+---------------------------------------------------------------------
2314 for (int i = 0; i < rows_; ++i) {
2315 if (isValidRow(i)) {
2316 ret += docbookRow(buf, os, i);
2321 //+---------------------------------------------------------------------
2322 //+ the closing of the tabular +
2323 //+---------------------------------------------------------------------
2332 int LyXTabular::asciiTopHLine(ostream & os, int row,
2333 vector<unsigned int> const & clen) const
2335 int const fcell = getFirstCellInRow(row);
2336 int const n = numberOfCellsInRow(fcell) + fcell;
2339 for (int i = fcell; i < n; ++i) {
2349 for (int i = fcell; i < n; ++i) {
2360 int column = column_of_cell(i);
2361 int len = clen[column];
2362 while (isPartOfMultiColumn(row, ++column))
2363 len += clen[column] + 4;
2364 os << string(len, ch);
2379 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2380 vector<unsigned int> const & clen) const
2382 int const fcell = getFirstCellInRow(row);
2383 int const n = numberOfCellsInRow(fcell) + fcell;
2386 for (int i = fcell; i < n; ++i) {
2387 if (bottomLine(i)) {
2396 for (int i = fcell; i < n; ++i) {
2397 if (bottomLine(i)) {
2407 int column = column_of_cell(i);
2408 int len = clen[column];
2409 while (isPartOfMultiColumn(row, ++column))
2410 len += clen[column] + 4;
2411 os << string(len, ch);
2412 if (bottomLine(i)) {
2426 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2427 int cell, int row, int column,
2428 vector<unsigned int> const & clen,
2429 bool onlydata) const
2432 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2444 unsigned int len1 = sstr.str().length();
2445 unsigned int len2 = clen[column];
2446 while (isPartOfMultiColumn(row, ++column))
2447 len2 += clen[column] + 4;
2450 switch (getAlignment(cell)) {
2452 case LYX_ALIGN_LEFT:
2455 case LYX_ALIGN_RIGHT:
2459 case LYX_ALIGN_CENTER:
2465 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2467 if (rightLine(cell))
2476 int LyXTabular::ascii(Buffer const & buf, ostream & os, int const depth,
2477 bool onlydata, unsigned char delim) const
2481 // first calculate the width of the single columns
2482 vector<unsigned int> clen(columns_);
2485 // first all non (real) multicolumn cells!
2486 for (int j = 0; j < columns_; ++j) {
2488 for (int i = 0; i < rows_; ++i) {
2489 int cell = getCellNumber(i, j);
2490 if (isMultiColumnReal(cell))
2493 getCellInset(cell).ascii(buf, sstr, 0);
2494 if (clen[j] < sstr.str().length())
2495 clen[j] = sstr.str().length();
2498 // then all (real) multicolumn cells!
2499 for (int j = 0; j < columns_; ++j) {
2500 for (int i = 0; i < rows_; ++i) {
2501 int cell = getCellNumber(i, j);
2502 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2505 getCellInset(cell).ascii(buf, sstr, 0);
2506 int len = int(sstr.str().length());
2507 int const n = cells_in_multicolumn(cell);
2508 for (int k = j; len > 0 && k < j + n - 1; ++k)
2510 if (len > int(clen[j + n - 1]))
2511 clen[j + n - 1] = len;
2516 for (int i = 0; i < rows_; ++i) {
2517 if (!onlydata && asciiTopHLine(os, i, clen))
2518 for (int j = 0; j < depth; ++j)
2520 for (int j = 0; j < columns_; ++j) {
2521 if (isPartOfMultiColumn(i, j))
2523 if (onlydata && j > 0)
2525 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2530 for (int j = 0; j < depth; ++j)
2532 if (asciiBottomHLine(os, i, clen))
2533 for (int j = 0; j < depth; ++j)
2541 InsetText & LyXTabular::getCellInset(int cell) const
2543 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2547 InsetText & LyXTabular::getCellInset(int row, int column) const
2549 return cell_info[row][column].inset;
2553 int LyXTabular::getCellFromInset(InsetOld const * inset) const
2555 // is this inset part of the tabular?
2557 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2561 for (int cell = getNumberOfCells(); cell >= 0; --cell)
2562 if (&getCellInset(cell) == inset) {
2563 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2564 << "cell=" << cell << endl;
2568 // We should have found a cell at this point
2569 lyxerr << "LyXTabular::getCellFromInset: Cell not found!" << endl;
2574 void LyXTabular::validate(LaTeXFeatures & features) const
2576 features.require("NeedTabularnewline");
2577 if (isLongTabular())
2578 features.require("longtable");
2580 features.require("rotating");
2581 for (int cell = 0; cell < numberofcells; ++cell) {
2582 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2583 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2584 features.require("array");
2585 getCellInset(cell).validate(features);
2590 void LyXTabular::getLabelList(Buffer const & buffer,
2591 std::vector<string> & list) const
2593 for (int i = 0; i < rows_; ++i)
2594 for (int j = 0; j < columns_; ++j)
2595 getCellInset(i, j).getLabelList(buffer, list);
2599 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2601 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2602 ParagraphList::const_iterator cit = parlist.begin();
2603 ParagraphList::const_iterator end = parlist.end();
2605 for (; cit != end; ++cit)
2606 for (int i = 0; i < cit->size(); ++i)
2607 if (cit->isNewline(i))