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 #warning for some strange reason, cellstruct does not seem to have copy-semantics
412 // work around using 'swap' only...
414 void LyXTabular::appendRow(BufferParams const & bp, int cell)
418 int row = row_of_cell(cell);
420 row_vector::iterator rit = row_info.begin() + row;
421 row_info.insert(rit, rowstruct());
422 // now set the values of the row before
423 row_info[row] = row_info[row + 1];
425 cell_vvector old(rows_ - 1);
426 for (int i = 0; i < rows_ - 1; ++i)
427 swap(cell_info[i], old[i]);
429 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
431 for (int i = 0; i <= row; ++i)
432 swap(cell_info[i], old[i]);
433 for (int i = row + 2; i < rows_; ++i)
434 swap(cell_info[i], old[i - 1]);
436 if (bp.tracking_changes)
437 for (int j = 0; j < columns_; ++j)
438 cell_info[row + 1][j].inset.markNew(true);
440 set_row_column_number_info();
444 void LyXTabular::deleteRow(int row)
446 // Not allowed to delete last row
450 row_info.erase(row_info.begin() + row);
451 cell_info.erase(cell_info.begin() + row);
457 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
461 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
463 int const column = column_of_cell(cell);
464 column_vector::iterator cit = column_info.begin() + column + 1;
465 column_info.insert(cit, columnstruct());
466 // set the column values of the column before
467 column_info[column + 1] = column_info[column];
469 for (int i = 0; i < rows_; ++i) {
470 for (int j = 0; j <= column; ++j)
471 c_info[i][j] = cell_info[i][j];
473 for (int j = column + 1; j < columns_; ++j)
474 c_info[i][j] = cell_info[i][j - 1];
476 // care about multicolumns
477 if (c_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
478 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
480 if (column + 2 >= columns_
481 || c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
482 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
486 for (int i = 0; i < rows_; ++i) {
487 cell_info[i][column + 1].inset.clear(false);
488 if (bp.tracking_changes)
489 cell_info[i][column + 1].inset.markNew(true);
495 void LyXTabular::deleteColumn(int column)
497 // Not allowed to delete last column
501 column_info.erase(column_info.begin() + column);
502 for (int i = 0; i < rows_; ++i)
503 cell_info[i].erase(cell_info[i].begin() + column);
509 void LyXTabular::set_row_column_number_info()
512 for (int row = 0; row < rows_; ++row) {
513 for (int column = 0; column < columns_; ++column) {
514 if (cell_info[row][column].multicolumn
515 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
517 cell_info[row][column].cellno = numberofcells;
520 ++numberofcells; // because this is one more than as we start from 0
522 rowofcell.resize(numberofcells);
523 columnofcell.resize(numberofcells);
525 for (int row = 0, column = 0, c = 0;
526 c < numberofcells && row < rows_ && column < columns_;) {
528 columnofcell[c] = column;
532 } while (column < columns_ &&
533 cell_info[row][column].multicolumn
534 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
536 if (column == columns_) {
542 for (int row = 0; row < rows_; ++row) {
543 for (int column = 0; column < columns_; ++column) {
544 if (isPartOfMultiColumn(row,column))
546 cell_info[row][column].inset.setAutoBreakRows(
547 !getPWidth(getCellNumber(row, column)).zero());
553 int LyXTabular::getNumberOfCells() const
555 return numberofcells;
559 int LyXTabular::numberOfCellsInRow(int cell) const
561 int const row = row_of_cell(cell);
563 for (int i = 0; i < columns_; ++i)
564 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
570 // returns 1 if there is a topline, returns 0 if not
571 bool LyXTabular::topLine(int cell, bool onlycolumn) const
573 if (!onlycolumn && isMultiColumn(cell))
574 return cellinfo_of_cell(cell).top_line;
575 return row_info[row_of_cell(cell)].top_line;
579 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
581 if (!onlycolumn && isMultiColumn(cell))
582 return cellinfo_of_cell(cell).bottom_line;
583 return row_info[row_of_cell(cell)].bottom_line;
587 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
589 if (!onlycolumn && isMultiColumn(cell) &&
590 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
592 if (cellinfo_of_cell(cell).align_special.empty())
593 return cellinfo_of_cell(cell).left_line;
594 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
596 if (column_info[column_of_cell(cell)].align_special.empty())
597 return column_info[column_of_cell(cell)].left_line;
598 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
602 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
604 if (!onlycolumn && isMultiColumn(cell) &&
605 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
607 if (cellinfo_of_cell(cell).align_special.empty())
608 return cellinfo_of_cell(cell).right_line;
609 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
611 if (column_info[column_of_cell(cell)].align_special.empty())
612 return column_info[right_column_of_cell(cell)].right_line;
613 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
617 bool LyXTabular::topAlreadyDrawn(int cell) const
619 int row = row_of_cell(cell);
620 if (row > 0 && !getAdditionalHeight(row)) {
621 int column = column_of_cell(cell);
624 && cell_info[row][column].multicolumn
625 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
627 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
628 return row_info[row].bottom_line;
630 return cell_info[row][column].bottom_line;
636 bool LyXTabular::leftAlreadyDrawn(int cell) const
638 int column = column_of_cell(cell);
640 int row = row_of_cell(cell);
642 (cell_info[row][column].multicolumn ==
643 LyXTabular::CELL_PART_OF_MULTICOLUMN));
644 if (getAdditionalWidth(cell_info[row][column].cellno))
646 return rightLine(cell_info[row][column].cellno);
652 bool LyXTabular::isLastRow(int cell) const
654 return row_of_cell(cell) == rows_ - 1;
658 int LyXTabular::getAdditionalHeight(int row) const
660 if (!row || row >= rows_)
666 for (int column = 0; column < columns_ && bottom; ++column) {
667 switch (cell_info[row - 1][column].multicolumn) {
668 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
669 bottom = cell_info[row - 1][column].bottom_line;
671 case LyXTabular::CELL_NORMAL:
672 bottom = row_info[row - 1].bottom_line;
675 for (int column = 0; column < columns_ && top; ++column) {
676 switch (cell_info[row][column].multicolumn) {
677 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
678 top = cell_info[row][column].top_line;
680 case LyXTabular::CELL_NORMAL:
681 top = row_info[row].top_line;
685 return WIDTH_OF_LINE;
690 int LyXTabular::getAdditionalWidth(int cell) const
692 // internally already set in setWidthOfCell
693 // used to get it back in text.C
694 int const col = right_column_of_cell(cell);
695 int const row = row_of_cell(cell);
696 if (col < columns_ - 1 && rightLine(cell) &&
697 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
699 return WIDTH_OF_LINE;
705 // returns the maximum over all rows
706 int LyXTabular::getWidthOfColumn(int cell) const
708 int const column1 = column_of_cell(cell);
709 int const column2 = right_column_of_cell(cell);
711 for (int i = column1; i <= column2; ++i)
712 result += column_info[i].width_of_column;
717 int LyXTabular::getWidthOfTabular() const
719 return width_of_tabular;
723 // returns true if a complete update is necessary, otherwise false
724 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
726 if (!isMultiColumn(cell))
729 int const row = row_of_cell(cell);
730 int const column1 = column_of_cell(cell);
731 int const column2 = right_column_of_cell(cell);
732 int const old_val = cell_info[row][column2].width_of_cell;
734 // first set columns to 0 so we can calculate the right width
735 for (int i = column1; i <= column2; ++i) {
736 cell_info[row][i].width_of_cell = 0;
738 // set the width to MAX_WIDTH until width > 0
739 int width = new_width + 2 * WIDTH_OF_LINE;
741 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
742 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
743 width -= column_info[i].width_of_column;
746 cell_info[row][i].width_of_cell = width;
748 if (old_val != cell_info[row][column2].width_of_cell) {
749 // in this case we have to recalculate all multicolumn cells which
750 // have this column as one of theirs but not as last one
751 calculate_width_of_column_NMC(i);
752 recalculateMulticolumnsOfColumn(i);
753 calculate_width_of_column(i);
759 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
761 // the last column does not have to be recalculated because all
762 // multicolumns will have here there last multicolumn cell which
763 // always will have the whole rest of the width of the cell.
764 if (column > (columns_ - 2))
766 for(int row = 0; row < rows_; ++row) {
767 int mc = cell_info[row][column].multicolumn;
768 int nmc = cell_info[row][column+1].multicolumn;
769 // we only have to update multicolumns which do not have this
770 // column as their last column!
771 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
772 (mc == CELL_PART_OF_MULTICOLUMN &&
773 nmc == CELL_PART_OF_MULTICOLUMN))
775 int const cellno = cell_info[row][column].cellno;
776 setWidthOfMulticolCell(cellno,
777 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
783 // returns 1 if a complete update is necessary, otherwise 0
784 void LyXTabular::setWidthOfCell(int cell, int new_width)
786 int const row = row_of_cell(cell);
787 int const column1 = column_of_cell(cell);
792 if (rightLine(cell_info[row][column1].cellno, true) &&
793 column1 < columns_ - 1 &&
794 leftLine(cell_info[row][column1+1].cellno, true))
796 add_width = WIDTH_OF_LINE;
799 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
802 if (isMultiColumnReal(cell)) {
803 tmp = setWidthOfMulticolCell(cell, new_width);
805 width = new_width + 2 * WIDTH_OF_LINE + add_width;
806 cell_info[row][column1].width_of_cell = width;
807 tmp = calculate_width_of_column_NMC(column1);
809 recalculateMulticolumnsOfColumn(column1);
812 for (int i = 0; i < columns_; ++i)
813 calculate_width_of_column(i);
814 calculate_width_of_tabular();
819 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
821 if (!isMultiColumn(cell) || onlycolumn)
822 column_info[column_of_cell(cell)].alignment = align;
824 cellinfo_of_cell(cell).alignment = align;
828 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
830 if (!isMultiColumn(cell) || onlycolumn)
831 column_info[column_of_cell(cell)].valignment = align;
833 cellinfo_of_cell(cell).valignment = align;
837 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
839 int const j = column_of_cell(cell);
841 column_info[j].p_width = width;
842 for (int i = 0; i < rows_; ++i) {
843 int const cell = getCellNumber(i, j);
844 // because of multicolumns
845 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
850 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
852 if (!isMultiColumn(cell))
855 cellinfo_of_cell(cell).p_width = width;
856 getCellInset(cell).setAutoBreakRows(!width.zero());
861 void LyXTabular::setAlignSpecial(int cell, string const & special,
862 LyXTabular::Feature what)
864 if (what == SET_SPECIAL_MULTI)
865 cellinfo_of_cell(cell).align_special = special;
867 column_info[column_of_cell(cell)].align_special = special;
871 void LyXTabular::setAllLines(int cell, bool line)
873 setTopLine(cell, line);
874 setBottomLine(cell, line);
875 setRightLine(cell, line);
876 setLeftLine(cell, line);
880 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
882 int const row = row_of_cell(cell);
883 if (onlycolumn || !isMultiColumn(cell))
884 row_info[row].top_line = line;
886 cellinfo_of_cell(cell).top_line = line;
890 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
892 if (onlycolumn || !isMultiColumn(cell))
893 row_info[row_of_cell(cell)].bottom_line = line;
895 cellinfo_of_cell(cell).bottom_line = line;
899 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
901 if (onlycolumn || !isMultiColumn(cell))
902 column_info[column_of_cell(cell)].left_line = line;
904 cellinfo_of_cell(cell).left_line = line;
908 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
910 if (onlycolumn || !isMultiColumn(cell))
911 column_info[right_column_of_cell(cell)].right_line = line;
913 cellinfo_of_cell(cell).right_line = line;
917 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
919 if (!onlycolumn && isMultiColumn(cell))
920 return cellinfo_of_cell(cell).alignment;
921 return column_info[column_of_cell(cell)].alignment;
925 LyXTabular::VAlignment
926 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
928 if (!onlycolumn && isMultiColumn(cell))
929 return cellinfo_of_cell(cell).valignment;
930 return column_info[column_of_cell(cell)].valignment;
934 LyXLength const LyXTabular::getPWidth(int cell) const
936 if (isMultiColumn(cell))
937 return cellinfo_of_cell(cell).p_width;
938 return column_info[column_of_cell(cell)].p_width;
942 LyXLength const LyXTabular::getColumnPWidth(int cell) const
944 return column_info[column_of_cell(cell)].p_width;
948 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
950 if (isMultiColumn(cell))
951 return cellinfo_of_cell(cell).p_width;
956 string const LyXTabular::getAlignSpecial(int cell, int what) const
958 if (what == SET_SPECIAL_MULTI)
959 return cellinfo_of_cell(cell).align_special;
960 return column_info[column_of_cell(cell)].align_special;
964 int LyXTabular::getWidthOfCell(int cell) const
966 int const row = row_of_cell(cell);
967 int const column1 = column_of_cell(cell);
968 int const column2 = right_column_of_cell(cell);
970 for (int i = column1; i <= column2; ++i)
971 result += cell_info[row][i].width_of_cell;
976 int LyXTabular::getBeginningOfTextInCell(int cell) const
980 switch (getAlignment(cell)) {
981 case LYX_ALIGN_CENTER:
982 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
984 case LYX_ALIGN_RIGHT:
985 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
986 // + getAdditionalWidth(cell);
989 // LYX_ALIGN_LEFT: nothing :-)
999 bool LyXTabular::isFirstCellInRow(int cell) const
1001 return column_of_cell(cell) == 0;
1005 int LyXTabular::getFirstCellInRow(int row) const
1007 if (row > rows_ - 1)
1009 return cell_info[row][0].cellno;
1013 bool LyXTabular::isLastCellInRow(int cell) const
1015 return right_column_of_cell(cell) == columns_ - 1;
1019 int LyXTabular::getLastCellInRow(int row) const
1021 if (row > rows_ - 1)
1023 return cell_info[row][columns_-1].cellno;
1027 void LyXTabular::calculate_width_of_column(int column)
1030 for (int i = 0; i < rows_; ++i)
1031 maximum = max(cell_info[i][column].width_of_cell, maximum);
1032 column_info[column].width_of_column = maximum;
1037 // Calculate the columns regarding ONLY the normal cells and if this
1038 // column is inside a multicolumn cell then use it only if its the last
1039 // column of this multicolumn cell as this gives an added width to the
1040 // column, all the rest should be adapted!
1042 bool LyXTabular::calculate_width_of_column_NMC(int column)
1044 int const old_column_width = column_info[column].width_of_column;
1046 for (int i = 0; i < rows_; ++i) {
1047 int cell = getCellNumber(i, column);
1048 bool ismulti = isMultiColumnReal(cell);
1049 if ((!ismulti || column == right_column_of_cell(cell)) &&
1050 cell_info[i][column].width_of_cell > max)
1052 max = cell_info[i][column].width_of_cell;
1055 column_info[column].width_of_column = max;
1056 return column_info[column].width_of_column != old_column_width;
1060 void LyXTabular::calculate_width_of_tabular()
1062 width_of_tabular = 0;
1063 for (int i = 0; i < columns_; ++i)
1064 width_of_tabular += column_info[i].width_of_column;
1068 int LyXTabular::row_of_cell(int cell) const
1070 if (cell >= numberofcells)
1074 return rowofcell[cell];
1078 int LyXTabular::column_of_cell(int cell) const
1080 if (cell >= numberofcells)
1081 return columns_ - 1;
1084 return columnofcell[cell];
1088 int LyXTabular::right_column_of_cell(int cell) const
1090 int const row = row_of_cell(cell);
1091 int column = column_of_cell(cell);
1092 while (column < columns_ - 1 &&
1093 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1099 void LyXTabular::write(Buffer const & buf, ostream & os) const
1103 << write_attribute("version", 3)
1104 << write_attribute("rows", rows_)
1105 << write_attribute("columns", columns_)
1107 // global longtable options
1109 << write_attribute("rotate", rotate)
1110 << write_attribute("islongtable", is_long_tabular)
1111 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1112 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1113 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1114 << write_attribute("headTopDL", endhead.topDL)
1115 << write_attribute("headBottomDL", endhead.bottomDL)
1116 << write_attribute("footTopDL", endfoot.topDL)
1117 << write_attribute("footBottomDL", endfoot.bottomDL)
1118 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1119 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1120 << write_attribute("lastFootEmpty", endlastfoot.empty)
1122 for (int j = 0; j < columns_; ++j) {
1124 << write_attribute("alignment", column_info[j].alignment)
1125 << write_attribute("valignment", column_info[j].valignment)
1126 << write_attribute("leftline", column_info[j].left_line)
1127 << write_attribute("rightline", column_info[j].right_line)
1128 << write_attribute("width", column_info[j].p_width.asString())
1129 << write_attribute("special", column_info[j].align_special)
1132 for (int i = 0; i < rows_; ++i) {
1134 << write_attribute("topline", row_info[i].top_line)
1135 << write_attribute("bottomline", row_info[i].bottom_line)
1136 << write_attribute("endhead", row_info[i].endhead)
1137 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1138 << write_attribute("endfoot", row_info[i].endfoot)
1139 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1140 << write_attribute("newpage", row_info[i].newpage)
1142 for (int j = 0; j < columns_; ++j) {
1144 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1145 << write_attribute("alignment", cell_info[i][j].alignment)
1146 << write_attribute("valignment", cell_info[i][j].valignment)
1147 << write_attribute("topline", cell_info[i][j].top_line)
1148 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1149 << write_attribute("leftline", cell_info[i][j].left_line)
1150 << write_attribute("rightline", cell_info[i][j].right_line)
1151 << write_attribute("rotate", cell_info[i][j].rotate)
1152 << write_attribute("usebox", cell_info[i][j].usebox)
1153 << write_attribute("width", cell_info[i][j].p_width)
1154 << write_attribute("special", cell_info[i][j].align_special)
1156 os << "\\begin_inset ";
1157 cell_info[i][j].inset.write(buf, os);
1158 os << "\n\\end_inset \n"
1163 os << "</lyxtabular>\n";
1167 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1171 row_info[--hr].endhead = true;
1173 // set firstheader info
1174 if (fhr && fhr < rows_) {
1175 if (row_info[fhr].endhead) {
1177 row_info[--fhr].endfirsthead = true;
1178 row_info[fhr].endhead = false;
1180 } else if (row_info[fhr - 1].endhead) {
1181 endfirsthead.empty = true;
1183 while (fhr > 0 && !row_info[--fhr].endhead) {
1184 row_info[fhr].endfirsthead = true;
1189 if (fr && fr < rows_) {
1190 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1191 while (fr > 0 && !row_info[--fr].endhead) {
1192 row_info[fr].endfoot = true;
1193 row_info[fr].endhead = false;
1195 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1196 while (fr > 0 && !row_info[--fr].endfirsthead) {
1197 row_info[fr].endfoot = true;
1198 row_info[fr].endfirsthead = false;
1200 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1201 while (fr > 0 && !row_info[--fr].endhead &&
1202 !row_info[fr].endfirsthead)
1204 row_info[fr].endfoot = true;
1208 // set lastfooter info
1209 if (lfr && lfr < rows_) {
1210 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1211 while (lfr > 0 && !row_info[--lfr].endhead) {
1212 row_info[lfr].endlastfoot = true;
1213 row_info[lfr].endhead = false;
1215 } else if (row_info[lfr].endfirsthead &&
1216 row_info[lfr - 1].endfirsthead)
1218 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1219 row_info[lfr].endlastfoot = true;
1220 row_info[lfr].endfirsthead = false;
1222 } else if (row_info[lfr].endfoot
1223 && row_info[lfr - 1].endfoot) {
1224 while (lfr > 0 && !row_info[--lfr].endfoot) {
1225 row_info[lfr].endlastfoot = true;
1226 row_info[lfr].endfoot = false;
1228 } else if (!row_info[fr - 1].endhead
1229 && !row_info[fr - 1].endfirsthead &&
1230 !row_info[fr - 1].endfoot)
1233 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1234 !row_info[lfr].endfoot)
1236 row_info[lfr].endlastfoot = true;
1238 } else if (haveLTFoot()) {
1239 endlastfoot.empty = true;
1245 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1248 istream & is = lex.getStream();
1250 l_getline(is, line);
1251 if (!prefixIs(line, "<lyxtabular ")
1252 && !prefixIs(line, "<LyXTabular ")) {
1253 BOOST_ASSERT(false);
1258 if (!getTokenValue(line, "version", version))
1260 BOOST_ASSERT(version >= 2);
1263 if (!getTokenValue(line, "rows", rows_arg))
1266 if (!getTokenValue(line, "columns", columns_arg))
1268 init(buf.params(), rows_arg, columns_arg);
1269 l_getline(is, line);
1270 if (!prefixIs(line, "<features")) {
1271 lyxerr << "Wrong tabular format (expected <features ...> got"
1272 << line << ')' << endl;
1275 getTokenValue(line, "rotate", rotate);
1276 getTokenValue(line, "islongtable", is_long_tabular);
1277 // compatibility read for old longtable options. Now we can make any
1278 // row part of the header/footer type we want before it was strict
1279 // sequential from the first row down (as LaTeX does it!). So now when
1280 // we find a header/footer line we have to go up the rows and set it
1281 // on all preceding rows till the first or one with already a h/f option
1282 // set. If we find a firstheader on the same line as a header or a
1283 // lastfooter on the same line as a footer then this should be set empty.
1291 getTokenValue(line, "endhead", hrow);
1292 getTokenValue(line, "endfirsthead", fhrow);
1293 getTokenValue(line, "endfoot", frow);
1294 getTokenValue(line, "endlastfoot", lfrow);
1295 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1297 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1298 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1299 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1300 getTokenValue(line, "headTopDL", endhead.topDL);
1301 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1302 getTokenValue(line, "footTopDL", endfoot.topDL);
1303 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1304 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1305 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1306 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1308 for (int j = 0; j < columns_; ++j) {
1310 if (!prefixIs(line,"<column")) {
1311 lyxerr << "Wrong tabular format (expected <column ...> got"
1312 << line << ')' << endl;
1315 getTokenValue(line, "alignment", column_info[j].alignment);
1316 getTokenValue(line, "valignment", column_info[j].valignment);
1317 getTokenValue(line, "leftline", column_info[j].left_line);
1318 getTokenValue(line, "rightline", column_info[j].right_line);
1319 getTokenValue(line, "width", column_info[j].p_width);
1320 getTokenValue(line, "special", column_info[j].align_special);
1323 for (int i = 0; i < rows_; ++i) {
1324 l_getline(is, line);
1325 if (!prefixIs(line, "<row")) {
1326 lyxerr << "Wrong tabular format (expected <row ...> got"
1327 << line << ')' << endl;
1330 getTokenValue(line, "topline", row_info[i].top_line);
1331 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1332 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1333 getTokenValue(line, "endhead", row_info[i].endhead);
1334 getTokenValue(line, "endfoot", row_info[i].endfoot);
1335 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1336 getTokenValue(line, "newpage", row_info[i].newpage);
1337 for (int j = 0; j < columns_; ++j) {
1338 l_getline(is, line);
1339 if (!prefixIs(line, "<cell")) {
1340 lyxerr << "Wrong tabular format (expected <cell ...> got"
1341 << line << ')' << endl;
1344 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1345 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1346 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1347 getTokenValue(line, "topline", cell_info[i][j].top_line);
1348 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1349 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1350 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1351 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1352 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1353 getTokenValue(line, "width", cell_info[i][j].p_width);
1354 getTokenValue(line, "special", cell_info[i][j].align_special);
1355 l_getline(is, line);
1356 if (prefixIs(line, "\\begin_inset")) {
1357 cell_info[i][j].inset.read(buf, lex);
1358 l_getline(is, line);
1360 if (!prefixIs(line, "</cell>")) {
1361 lyxerr << "Wrong tabular format (expected </cell> got"
1362 << line << ')' << endl;
1366 l_getline(is, line);
1367 if (!prefixIs(line, "</row>")) {
1368 lyxerr << "Wrong tabular format (expected </row> got"
1369 << line << ')' << endl;
1373 while (!prefixIs(line, "</lyxtabular>")) {
1374 l_getline(is, line);
1376 set_row_column_number_info();
1380 bool LyXTabular::isMultiColumn(int cell) const
1382 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1386 bool LyXTabular::isMultiColumnReal(int cell) const
1388 return column_of_cell(cell) != right_column_of_cell(cell) &&
1389 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1393 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1395 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1399 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1401 cellstruct & cs = cellinfo_of_cell(cell);
1402 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1403 cs.alignment = column_info[column_of_cell(cell)].alignment;
1404 cs.top_line = row_info[row_of_cell(cell)].top_line;
1405 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1406 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1407 for (int i = 1; i < number; ++i) {
1408 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1409 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1410 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1411 cs1.inset.clear(false);
1413 set_row_column_number_info();
1417 int LyXTabular::cells_in_multicolumn(int cell) const
1419 int const row = row_of_cell(cell);
1420 int column = column_of_cell(cell);
1423 while (column < columns_ &&
1424 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1433 int LyXTabular::unsetMultiColumn(int cell)
1435 int const row = row_of_cell(cell);
1436 int column = column_of_cell(cell);
1440 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1441 cell_info[row][column].multicolumn = CELL_NORMAL;
1443 while (column < columns_ &&
1444 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1446 cell_info[row][column].multicolumn = CELL_NORMAL;
1451 set_row_column_number_info();
1456 void LyXTabular::setLongTabular(bool what)
1458 is_long_tabular = what;
1462 bool LyXTabular::isLongTabular() const
1464 return is_long_tabular;
1468 void LyXTabular::setRotateTabular(bool flag)
1474 bool LyXTabular::getRotateTabular() const
1480 void LyXTabular::setRotateCell(int cell, bool flag)
1482 cellinfo_of_cell(cell).rotate = flag;
1486 bool LyXTabular::getRotateCell(int cell) const
1488 return cellinfo_of_cell(cell).rotate;
1492 bool LyXTabular::needRotating() const
1496 for (int i = 0; i < rows_; ++i)
1497 for (int j = 0; j < columns_; ++j)
1498 if (cell_info[i][j].rotate)
1504 bool LyXTabular::isLastCell(int cell) const
1506 if (cell + 1 < numberofcells)
1512 int LyXTabular::getCellAbove(int cell) const
1514 if (row_of_cell(cell) > 0)
1515 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1520 int LyXTabular::getCellBelow(int cell) const
1522 if (row_of_cell(cell) + 1 < rows_)
1523 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1528 int LyXTabular::getLastCellAbove(int cell) const
1530 if (row_of_cell(cell) <= 0)
1532 if (!isMultiColumn(cell))
1533 return getCellAbove(cell);
1534 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1538 int LyXTabular::getLastCellBelow(int cell) const
1540 if (row_of_cell(cell) + 1 >= rows_)
1542 if (!isMultiColumn(cell))
1543 return getCellBelow(cell);
1544 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1548 int LyXTabular::getCellNumber(int row, int column) const
1550 BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1551 return cell_info[row][column].cellno;
1555 void LyXTabular::setUsebox(int cell, BoxType type)
1557 cellinfo_of_cell(cell).usebox = type;
1561 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1563 if (column_info[column_of_cell(cell)].p_width.zero() &&
1564 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1566 if (cellinfo_of_cell(cell).usebox > 1)
1567 return cellinfo_of_cell(cell).usebox;
1568 return useParbox(cell);
1573 // This are functions used for the longtable support
1575 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1580 row_info[row].endfirsthead = flag;
1584 row_info[row].endhead = flag;
1589 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1592 hd.set = haveLTHead();
1593 return row_info[row].endhead;
1597 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1600 hd.set = haveLTFirstHead();
1601 return row_info[row].endfirsthead;
1605 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1610 row_info[row].endlastfoot = flag;
1614 row_info[row].endfoot = flag;
1619 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1622 fd.set = haveLTFoot();
1623 return row_info[row].endfoot;
1627 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1630 fd.set = haveLTLastFoot();
1631 return row_info[row].endlastfoot;
1635 void LyXTabular::setLTNewPage(int row, bool what)
1637 row_info[row].newpage = what;
1641 bool LyXTabular::getLTNewPage(int row) const
1643 return row_info[row].newpage;
1647 bool LyXTabular::haveLTHead() const
1649 for (int i = 0; i < rows_; ++i)
1650 if (row_info[i].endhead)
1656 bool LyXTabular::haveLTFirstHead() const
1658 if (endfirsthead.empty)
1660 for (int i = 0; i < rows_; ++i)
1661 if (row_info[i].endfirsthead)
1667 bool LyXTabular::haveLTFoot() const
1669 for (int i = 0; i < rows_; ++i)
1670 if (row_info[i].endfoot)
1676 bool LyXTabular::haveLTLastFoot() const
1678 if (endlastfoot.empty)
1680 for (int i = 0; i < rows_; ++i)
1681 if (row_info[i].endlastfoot)
1687 // end longtable support functions
1689 void LyXTabular::setAscentOfRow(int row, int height)
1691 if (row >= rows_ || row_info[row].ascent_of_row == height)
1693 row_info[row].ascent_of_row = height;
1697 void LyXTabular::setDescentOfRow(int row, int height)
1699 if (row >= rows_ || row_info[row].descent_of_row == height)
1701 row_info[row].descent_of_row = height;
1705 int LyXTabular::getAscentOfRow(int row) const
1709 return row_info[row].ascent_of_row;
1713 int LyXTabular::getDescentOfRow(int row) const
1717 return row_info[row].descent_of_row;
1721 int LyXTabular::getHeightOfTabular() const
1724 for (int row = 0; row < rows_; ++row)
1725 height += getAscentOfRow(row) + getDescentOfRow(row) +
1726 getAdditionalHeight(row);
1731 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1733 if (row >= rows_ || column >= columns_)
1735 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1739 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1741 if (row < 0 || row >= rows_)
1744 int const fcell = getFirstCellInRow(row);
1745 int const n = numberOfCellsInRow(fcell) + fcell;
1748 for (int i = fcell; i < n; ++i) {
1752 if (tmp == n - fcell) {
1755 for (int i = fcell; i < n; ++i) {
1758 << column_of_cell(i) + 1
1760 << right_column_of_cell(i) + 1
1772 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1774 if (row < 0 || row >= rows_)
1777 int const fcell = getFirstCellInRow(row);
1778 int const n = numberOfCellsInRow(fcell) + fcell;
1781 for (int i = fcell; i < n; ++i) {
1785 if (tmp == n - fcell) {
1788 for (int i = fcell; i < n; ++i) {
1789 if (bottomLine(i)) {
1791 << column_of_cell(i) + 1
1793 << right_column_of_cell(i) + 1
1805 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1809 if (getRotateCell(cell)) {
1810 os << "\\begin{sideways}\n";
1813 if (isMultiColumn(cell)) {
1814 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1815 if (!cellinfo_of_cell(cell).align_special.empty()) {
1816 os << cellinfo_of_cell(cell).align_special << "}{";
1818 if (leftLine(cell) &&
1819 (isFirstCellInRow(cell) ||
1820 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1821 !rightLine(cell - 1, true))))
1825 if (!getPWidth(cell).zero()) {
1826 switch (getVAlignment(cell)) {
1827 case LYX_VALIGN_TOP:
1830 case LYX_VALIGN_MIDDLE:
1833 case LYX_VALIGN_BOTTOM:
1838 << getPWidth(cell).asLatexString()
1841 switch (getAlignment(cell)) {
1842 case LYX_ALIGN_LEFT:
1845 case LYX_ALIGN_RIGHT:
1853 if (rightLine(cell))
1855 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1861 if (getUsebox(cell) == BOX_PARBOX) {
1863 switch (getVAlignment(cell)) {
1864 case LYX_VALIGN_TOP:
1867 case LYX_VALIGN_MIDDLE:
1870 case LYX_VALIGN_BOTTOM:
1874 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1875 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1876 os << "\\begin{minipage}[";
1877 switch (getVAlignment(cell)) {
1878 case LYX_VALIGN_TOP:
1881 case LYX_VALIGN_MIDDLE:
1884 case LYX_VALIGN_BOTTOM:
1888 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1895 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1900 if (getUsebox(cell) == BOX_PARBOX)
1902 else if (getUsebox(cell) == BOX_MINIPAGE) {
1903 os << "%\n\\end{minipage}";
1906 if (isMultiColumn(cell)) {
1909 if (getRotateCell(cell)) {
1910 os << "%\n\\end{sideways}";
1917 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1918 LatexRunParams const & runparams) const
1920 if (!is_long_tabular)
1924 // output header info
1926 if (endhead.topDL) {
1930 for (int i = 0; i < rows_; ++i) {
1931 if (row_info[i].endhead) {
1932 ret += TeXRow(os, i, buf, runparams);
1935 if (endhead.bottomDL) {
1939 os << "\\endhead\n";
1941 if (endfirsthead.empty) {
1942 os << "\\endfirsthead\n";
1946 // output firstheader info
1947 if (haveLTFirstHead()) {
1948 if (endfirsthead.topDL) {
1952 for (int i = 0; i < rows_; ++i) {
1953 if (row_info[i].endfirsthead) {
1954 ret += TeXRow(os, i, buf, runparams);
1957 if (endfirsthead.bottomDL) {
1961 os << "\\endfirsthead\n";
1964 // output footer info
1966 if (endfoot.topDL) {
1970 for (int i = 0; i < rows_; ++i) {
1971 if (row_info[i].endfoot) {
1972 ret += TeXRow(os, i, buf, runparams);
1975 if (endfoot.bottomDL) {
1979 os << "\\endfoot\n";
1981 if (endlastfoot.empty) {
1982 os << "\\endlastfoot\n";
1986 // output lastfooter info
1987 if (haveLTLastFoot()) {
1988 if (endlastfoot.topDL) {
1992 for (int i = 0; i < rows_; ++i) {
1993 if (row_info[i].endlastfoot) {
1994 ret += TeXRow(os, i, buf, runparams);
1997 if (endlastfoot.bottomDL) {
2001 os << "\\endlastfoot\n";
2008 bool LyXTabular::isValidRow(int row) const
2010 if (!is_long_tabular)
2012 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2013 !row_info[row].endfoot && !row_info[row].endlastfoot;
2017 int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
2018 LatexRunParams const & runparams) const
2021 int cell = getCellNumber(i, 0);
2022 BufferParams const & bufferparams = buf.params();
2024 ret += TeXTopHLine(os, i);
2025 for (int j = 0; j < columns_; ++j) {
2026 if (isPartOfMultiColumn(i, j))
2028 ret += TeXCellPreamble(os, cell);
2029 InsetText & inset = getCellInset(cell);
2031 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(bufferparams) &&
2032 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2036 ret += inset.latex(buf, os, runparams);
2040 ret += TeXCellPostamble(os, cell);
2041 if (!isLastCellInRow(cell)) { // not last cell in row
2047 os << "\\tabularnewline\n";
2049 ret += TeXBottomHLine(os, i);
2054 int LyXTabular::latex(Buffer const & buf, ostream & os,
2055 LatexRunParams const & runparams) const
2059 //+---------------------------------------------------------------------
2060 //+ first the opening preamble +
2061 //+---------------------------------------------------------------------
2064 os << "\\begin{sideways}\n";
2067 if (is_long_tabular)
2068 os << "\\begin{longtable}{";
2070 os << "\\begin{tabular}{";
2071 for (int i = 0; i < columns_; ++i) {
2072 if (!column_info[i].align_special.empty()) {
2073 os << column_info[i].align_special;
2075 if (column_info[i].left_line)
2077 if (!column_info[i].p_width.zero()) {
2078 switch (column_info[i].alignment) {
2079 case LYX_ALIGN_LEFT:
2080 os << ">{\\raggedright}";
2082 case LYX_ALIGN_RIGHT:
2083 os << ">{\\raggedleft}";
2085 case LYX_ALIGN_CENTER:
2086 os << ">{\\centering}";
2088 case LYX_ALIGN_NONE:
2089 case LYX_ALIGN_BLOCK:
2090 case LYX_ALIGN_LAYOUT:
2091 case LYX_ALIGN_SPECIAL:
2095 switch (column_info[i].valignment) {
2096 case LYX_VALIGN_TOP:
2099 case LYX_VALIGN_MIDDLE:
2102 case LYX_VALIGN_BOTTOM:
2107 << column_info[i].p_width.asLatexString()
2110 switch (column_info[i].alignment) {
2111 case LYX_ALIGN_LEFT:
2114 case LYX_ALIGN_RIGHT:
2122 if (column_info[i].right_line)
2129 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2131 //+---------------------------------------------------------------------
2132 //+ the single row and columns (cells) +
2133 //+---------------------------------------------------------------------
2135 for (int i = 0; i < rows_; ++i) {
2136 if (isValidRow(i)) {
2137 ret += TeXRow(os, i, buf, runparams);
2138 if (is_long_tabular && row_info[i].newpage) {
2139 os << "\\newpage\n";
2145 //+---------------------------------------------------------------------
2146 //+ the closing of the tabular +
2147 //+---------------------------------------------------------------------
2149 if (is_long_tabular)
2150 os << "\\end{longtable}";
2152 os << "\\end{tabular}";
2154 os << "\n\\end{sideways}";
2162 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os) const
2164 os << "<tabular ca=\"";
2165 for (int i = 0; i < columns_; ++i) {
2166 switch (column_info[i].alignment) {
2167 case LYX_ALIGN_LEFT:
2170 case LYX_ALIGN_RIGHT:
2181 for (int i = 0; i < rows_; ++i) {
2182 for (int j = 0; j < columns_; ++j) {
2183 if (isPartOfMultiColumn(i, j))
2185 InsetText & inset = getCellInset(cell);
2187 ret += inset.linuxdoc(buf, os);
2189 if (isLastCellInRow(cell)) {
2198 os << "</tabular>\n";
2203 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row) const
2206 int cell = getFirstCellInRow(row);
2209 for (int j = 0; j < columns_; ++j) {
2210 if (isPartOfMultiColumn(row, j))
2213 os << "<entry align=\"";
2214 switch (getAlignment(cell)) {
2215 case LYX_ALIGN_LEFT:
2218 case LYX_ALIGN_RIGHT:
2226 os << "\" valign=\"";
2227 switch (getVAlignment(cell)) {
2228 case LYX_VALIGN_TOP:
2231 case LYX_VALIGN_BOTTOM:
2234 case LYX_VALIGN_MIDDLE:
2239 if (isMultiColumn(cell)) {
2240 os << " namest=\"col" << j << "\" ";
2241 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2245 ret += getCellInset(cell).docbook(buf, os, true);
2254 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2255 bool /*mixcont*/) const
2259 //+---------------------------------------------------------------------
2260 //+ first the opening preamble +
2261 //+---------------------------------------------------------------------
2263 os << "<tgroup cols=\"" << columns_
2264 << "\" colsep=\"1\" rowsep=\"1\">\n";
2266 for (int i = 0; i < columns_; ++i) {
2267 os << "<colspec colname=\"col" << i << "\" align=\"";
2268 switch (column_info[i].alignment) {
2269 case LYX_ALIGN_LEFT:
2272 case LYX_ALIGN_RIGHT:
2283 //+---------------------------------------------------------------------
2284 //+ Long Tabular case +
2285 //+---------------------------------------------------------------------
2287 // output header info
2288 if (haveLTHead() || haveLTFirstHead()) {
2291 for (int i = 0; i < rows_; ++i) {
2292 if (row_info[i].endhead || row_info[i].endfirsthead) {
2293 ret += docbookRow(buf, os, i);
2299 // output footer info
2300 if (haveLTFoot() || haveLTLastFoot()) {
2303 for (int i = 0; i < rows_; ++i) {
2304 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2305 ret += docbookRow(buf, os, i);
2312 //+---------------------------------------------------------------------
2313 //+ the single row and columns (cells) +
2314 //+---------------------------------------------------------------------
2318 for (int i = 0; i < rows_; ++i) {
2319 if (isValidRow(i)) {
2320 ret += docbookRow(buf, os, i);
2325 //+---------------------------------------------------------------------
2326 //+ the closing of the tabular +
2327 //+---------------------------------------------------------------------
2336 int LyXTabular::asciiTopHLine(ostream & os, int row,
2337 vector<unsigned int> const & clen) const
2339 int const fcell = getFirstCellInRow(row);
2340 int const n = numberOfCellsInRow(fcell) + fcell;
2343 for (int i = fcell; i < n; ++i) {
2353 for (int i = fcell; i < n; ++i) {
2364 int column = column_of_cell(i);
2365 int len = clen[column];
2366 while (isPartOfMultiColumn(row, ++column))
2367 len += clen[column] + 4;
2368 os << string(len, ch);
2383 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2384 vector<unsigned int> const & clen) const
2386 int const fcell = getFirstCellInRow(row);
2387 int const n = numberOfCellsInRow(fcell) + fcell;
2390 for (int i = fcell; i < n; ++i) {
2391 if (bottomLine(i)) {
2400 for (int i = fcell; i < n; ++i) {
2401 if (bottomLine(i)) {
2411 int column = column_of_cell(i);
2412 int len = clen[column];
2413 while (isPartOfMultiColumn(row, ++column))
2414 len += clen[column] + 4;
2415 os << string(len, ch);
2416 if (bottomLine(i)) {
2430 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2431 int cell, int row, int column,
2432 vector<unsigned int> const & clen,
2433 bool onlydata) const
2436 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2448 unsigned int len1 = sstr.str().length();
2449 unsigned int len2 = clen[column];
2450 while (isPartOfMultiColumn(row, ++column))
2451 len2 += clen[column] + 4;
2454 switch (getAlignment(cell)) {
2456 case LYX_ALIGN_LEFT:
2459 case LYX_ALIGN_RIGHT:
2463 case LYX_ALIGN_CENTER:
2469 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2471 if (rightLine(cell))
2480 int LyXTabular::ascii(Buffer const & buf, ostream & os, int const depth,
2481 bool onlydata, unsigned char delim) const
2485 // first calculate the width of the single columns
2486 vector<unsigned int> clen(columns_);
2489 // first all non (real) multicolumn cells!
2490 for (int j = 0; j < columns_; ++j) {
2492 for (int i = 0; i < rows_; ++i) {
2493 int cell = getCellNumber(i, j);
2494 if (isMultiColumnReal(cell))
2497 getCellInset(cell).ascii(buf, sstr, 0);
2498 if (clen[j] < sstr.str().length())
2499 clen[j] = sstr.str().length();
2502 // then all (real) multicolumn cells!
2503 for (int j = 0; j < columns_; ++j) {
2504 for (int i = 0; i < rows_; ++i) {
2505 int cell = getCellNumber(i, j);
2506 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2509 getCellInset(cell).ascii(buf, sstr, 0);
2510 int len = int(sstr.str().length());
2511 int const n = cells_in_multicolumn(cell);
2512 for (int k = j; len > 0 && k < j + n - 1; ++k)
2514 if (len > int(clen[j + n - 1]))
2515 clen[j + n - 1] = len;
2520 for (int i = 0; i < rows_; ++i) {
2521 if (!onlydata && asciiTopHLine(os, i, clen))
2522 for (int j = 0; j < depth; ++j)
2524 for (int j = 0; j < columns_; ++j) {
2525 if (isPartOfMultiColumn(i, j))
2527 if (onlydata && j > 0)
2529 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2534 for (int j = 0; j < depth; ++j)
2536 if (asciiBottomHLine(os, i, clen))
2537 for (int j = 0; j < depth; ++j)
2545 InsetText & LyXTabular::getCellInset(int cell) const
2547 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2551 InsetText & LyXTabular::getCellInset(int row, int column) const
2553 return cell_info[row][column].inset;
2557 int LyXTabular::getCellFromInset(InsetOld const * inset) const
2559 // is this inset part of the tabular?
2561 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2565 for (int cell = getNumberOfCells(); cell >= 0; --cell)
2566 if (&getCellInset(cell) == inset) {
2567 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2568 << "cell=" << cell << endl;
2572 // We should have found a cell at this point
2573 lyxerr << "LyXTabular::getCellFromInset: Cell not found!" << endl;
2578 void LyXTabular::validate(LaTeXFeatures & features) const
2580 features.require("NeedTabularnewline");
2581 if (isLongTabular())
2582 features.require("longtable");
2584 features.require("rotating");
2585 for (int cell = 0; cell < numberofcells; ++cell) {
2586 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2587 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2588 features.require("array");
2589 getCellInset(cell).validate(features);
2594 void LyXTabular::getLabelList(Buffer const & buffer,
2595 std::vector<string> & list) const
2597 for (int i = 0; i < rows_; ++i)
2598 for (int j = 0; j < columns_; ++j)
2599 getCellInset(i, j).getLabelList(buffer, list);
2603 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2605 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2606 ParagraphList::const_iterator cit = parlist.begin();
2607 ParagraphList::const_iterator end = parlist.end();
2609 for (; cit != end; ++cit)
2610 for (int i = 0; i < cit->size(); ++i)
2611 if (cit->isNewline(i))