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"
24 #include "BufferView.h"
27 #include "LaTeXFeatures.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
33 #include "insets/insettabular.h"
35 #include "support/lstrings.h"
36 #include "support/convert.h"
40 using lyx::support::ltrim;
41 using lyx::support::prefixIs;
42 using lyx::support::rtrim;
43 using lyx::support::suffixIs;
45 using boost::shared_ptr;
46 using boost::dynamic_pointer_cast;
55 using std::ostringstream;
59 #ifndef CXX_GLOBAL_CSTD
66 int const WIDTH_OF_LINE = 5;
67 int const default_line_space = 10;
71 string const write_attribute(string const & name, T const & t)
73 string const s = tostr(t);
74 return s.empty() ? s : " " + name + "=\"" + s + "\"";
78 string const write_attribute(string const & name, string const & t)
80 return t.empty() ? t : " " + name + "=\"" + t + "\"";
85 string const write_attribute(string const & name, bool const & b)
87 // we write only true attribute values so we remove a bit of the
88 // file format bloat for tabulars.
89 return b ? write_attribute(name, convert<string>(b)) : string();
94 string const write_attribute(string const & name, int const & i)
96 // we write only true attribute values so we remove a bit of the
97 // file format bloat for tabulars.
98 return i ? write_attribute(name, convert<string>(i)) : string();
103 string const write_attribute(string const & name, LyXTabular::idx_type const & i)
105 // we write only true attribute values so we remove a bit of the
106 // file format bloat for tabulars.
107 return i ? write_attribute(name, convert<string>(i)) : string();
112 string const write_attribute(string const & name, LyXLength const & value)
114 // we write only the value if we really have one same reson as above.
115 return value.zero() ? string() : write_attribute(name, value.asString());
119 string const tostr(LyXAlignment const & num)
124 case LYX_ALIGN_BLOCK:
128 case LYX_ALIGN_CENTER:
130 case LYX_ALIGN_RIGHT:
132 case LYX_ALIGN_LAYOUT:
134 case LYX_ALIGN_SPECIAL:
141 string const tostr(LyXTabular::VAlignment const & num)
144 case LyXTabular::LYX_VALIGN_TOP:
146 case LyXTabular::LYX_VALIGN_MIDDLE:
148 case LyXTabular::LYX_VALIGN_BOTTOM:
155 string const tostr(LyXTabular::BoxType const & num)
158 case LyXTabular::BOX_NONE:
160 case LyXTabular::BOX_PARBOX:
162 case LyXTabular::BOX_MINIPAGE:
169 // I would have liked a fromstr template a lot better. (Lgb)
170 bool string2type(string const str, LyXAlignment & num)
173 num = LYX_ALIGN_NONE;
174 else if (str == "block")
175 num = LYX_ALIGN_BLOCK;
176 else if (str == "left")
177 num = LYX_ALIGN_LEFT;
178 else if (str == "center")
179 num = LYX_ALIGN_CENTER;
180 else if (str == "right")
181 num = LYX_ALIGN_RIGHT;
188 bool string2type(string const str, LyXTabular::VAlignment & num)
191 num = LyXTabular::LYX_VALIGN_TOP;
192 else if (str == "middle" )
193 num = LyXTabular::LYX_VALIGN_MIDDLE;
194 else if (str == "bottom")
195 num = LyXTabular::LYX_VALIGN_BOTTOM;
202 bool string2type(string const str, LyXTabular::BoxType & num)
205 num = LyXTabular::BOX_NONE;
206 else if (str == "parbox")
207 num = LyXTabular::BOX_PARBOX;
208 else if (str == "minipage")
209 num = LyXTabular::BOX_MINIPAGE;
216 bool string2type(string const str, bool & num)
220 else if (str == "false")
228 bool getTokenValue(string const & str, char const * token, string & ret)
231 size_t token_length = strlen(token);
232 string::size_type pos = str.find(token);
234 if (pos == string::npos || pos + token_length + 1 >= str.length()
235 || str[pos + token_length] != '=')
237 pos += token_length + 1;
239 if (ch != '"' && ch != '\'') { // only read till next space
243 while (pos < str.length() - 1 && str[++pos] != ch)
250 bool getTokenValue(string const & str, char const * token, int & num)
254 if (!getTokenValue(str, token, tmp))
256 num = convert<int>(tmp);
261 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
264 return getTokenValue(str, token, tmp) && string2type(tmp, num);
268 bool getTokenValue(string const & str, char const * token,
269 LyXTabular::VAlignment & num)
272 return getTokenValue(str, token, tmp) && string2type(tmp, num);
276 bool getTokenValue(string const & str, char const * token,
277 LyXTabular::BoxType & num)
280 return getTokenValue(str, token, tmp) && string2type(tmp, num);
284 bool getTokenValue(string const & str, char const * token, bool & flag)
286 // set the flag always to false as this should be the default for bools
287 // not in the file-format.
290 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
294 bool getTokenValue(string const & str, char const * token, LyXLength & len)
296 // set the lenght to be zero() as default as this it should be if not
297 // in the file format.
300 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
304 bool getTokenValue(string const & str, char const * token, LyXLength & len, bool & flag)
309 if (!getTokenValue(str, token, tmp))
311 if (tmp == "default") {
315 return isValidLength(tmp, &len);
319 void l_getline(istream & is, string & str)
322 while (str.empty()) {
324 if (!str.empty() && str[str.length() - 1] == '\r')
325 str.erase(str.length() - 1);
331 /// Define a few methods for the inner structs
333 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
336 multicolumn(LyXTabular::CELL_NORMAL),
337 alignment(LYX_ALIGN_CENTER),
338 valignment(LYX_VALIGN_TOP),
345 inset(new InsetText(bp))
349 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
351 width_of_cell(cs.width_of_cell),
352 multicolumn(cs.multicolumn),
353 alignment(cs.alignment),
354 valignment(cs.valignment),
355 top_line(cs.top_line),
356 bottom_line(cs.bottom_line),
357 left_line(cs.left_line),
358 right_line(cs.right_line),
361 align_special(cs.align_special),
363 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
367 LyXTabular::cellstruct &
368 LyXTabular::cellstruct::operator=(cellstruct cs)
376 LyXTabular::cellstruct::swap(cellstruct & rhs)
378 std::swap(cellno, rhs.cellno);
379 std::swap(width_of_cell, rhs.width_of_cell);
380 std::swap(multicolumn, rhs.multicolumn);
381 std::swap(alignment, rhs.alignment);
382 std::swap(valignment, rhs.valignment);
383 std::swap(top_line, rhs.top_line);
384 std::swap(bottom_line, rhs.bottom_line);
385 std::swap(left_line, rhs.left_line);
386 std::swap(right_line, rhs.right_line);
387 std::swap(usebox, rhs.usebox);
388 std::swap(rotate, rhs.rotate);
389 std::swap(align_special, rhs.align_special);
390 p_width.swap(rhs.p_width);
391 inset.swap(rhs.inset);
395 LyXTabular::rowstruct::rowstruct()
400 top_space_default(false),
401 bottom_space_default(false),
402 interline_space_default(false),
411 LyXTabular::columnstruct::columnstruct()
412 : alignment(LYX_ALIGN_CENTER),
413 valignment(LYX_VALIGN_TOP),
421 LyXTabular::ltType::ltType()
428 LyXTabular::LyXTabular(BufferParams const & bp, row_type rows_arg,
429 col_type columns_arg)
431 init(bp, rows_arg, columns_arg);
435 // activates all lines and sets all widths to 0
436 void LyXTabular::init(BufferParams const & bp, row_type rows_arg,
437 col_type columns_arg)
440 columns_ = columns_arg;
441 row_info = row_vector(rows_);
442 column_info = column_vector(columns_);
443 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
444 row_info.reserve(10);
445 column_info.reserve(10);
446 cell_info.reserve(100);
448 for (row_type i = 0; i < rows_; ++i)
449 cell_info[i].back().right_line = true;
450 row_info.back().bottom_line = true;
451 row_info.front().bottom_line = true;
452 column_info.back().right_line = true;
453 is_long_tabular = false;
455 use_booktabs = false;
459 void LyXTabular::fixCellNums()
462 for (row_type i = 0; i < rows_; ++i) {
463 for (col_type j = 0; j < columns_; ++j) {
464 // When debugging it can be nice to set
466 cell_info[i][j].inset->setDrawFrame(false);
467 cell_info[i][j].cellno = cellno++;
469 cell_info[i].back().right_line = true;
472 set_row_column_number_info();
476 void LyXTabular::appendRow(BufferParams const & bp, idx_type const cell)
480 row_type const row = row_of_cell(cell);
482 row_vector::iterator rit = row_info.begin() + row;
483 row_info.insert(rit, rowstruct());
484 // now set the values of the row before
485 row_info[row] = row_info[row + 1];
487 cell_vvector old(rows_ - 1);
488 for (row_type i = 0; i < rows_ - 1; ++i)
489 swap(cell_info[i], old[i]);
491 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
493 for (row_type i = 0; i <= row; ++i)
494 swap(cell_info[i], old[i]);
495 for (row_type i = row + 2; i < rows_; ++i)
496 swap(cell_info[i], old[i - 1]);
498 if (bp.tracking_changes)
499 for (col_type j = 0; j < columns_; ++j)
500 cell_info[row + 1][j].inset->markNew(true);
502 set_row_column_number_info();
506 void LyXTabular::deleteRow(row_type const row)
508 // Not allowed to delete last row
512 row_info.erase(row_info.begin() + row);
513 cell_info.erase(cell_info.begin() + row);
519 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
523 col_type const column = column_of_cell(cell);
524 column_vector::iterator cit = column_info.begin() + column + 1;
525 column_info.insert(cit, columnstruct());
526 // set the column values of the column before
527 column_info[column + 1] = column_info[column];
529 for (row_type i = 0; i < rows_; ++i) {
530 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
532 // care about multicolumns
533 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
534 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
536 if (column + 2 >= columns_
537 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
538 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
541 for (row_type i = 0; i < rows_; ++i) {
542 cell_info[i][column + 1].inset->clear();
543 if (bp.tracking_changes)
544 cell_info[i][column + 1].inset->markNew(true);
550 void LyXTabular::deleteColumn(col_type const column)
552 // Not allowed to delete last column
556 column_info.erase(column_info.begin() + column);
557 for (row_type i = 0; i < rows_; ++i)
558 cell_info[i].erase(cell_info[i].begin() + column);
564 void LyXTabular::set_row_column_number_info()
567 for (row_type row = 0; row < rows_; ++row) {
568 for (col_type column = 0; column < columns_; ++column) {
569 if (cell_info[row][column].multicolumn
570 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
572 if (numberofcells == 0)
573 // FIXME: Is this intended?
574 cell_info[row][column].cellno = npos;
576 cell_info[row][column].cellno =
581 rowofcell.resize(numberofcells);
582 columnofcell.resize(numberofcells);
587 c < numberofcells && row < rows_ && column < columns_;) {
589 columnofcell[c] = column;
593 } while (column < columns_ &&
594 cell_info[row][column].multicolumn
595 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
597 if (column == columns_) {
603 for (row_type row = 0; row < rows_; ++row) {
604 for (col_type column = 0; column < columns_; ++column) {
605 if (isPartOfMultiColumn(row,column))
607 cell_info[row][column].inset->setAutoBreakRows(
608 !getPWidth(getCellNumber(row, column)).zero());
614 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
616 return numberofcells;
620 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
622 row_type const row = row_of_cell(cell);
624 for (col_type i = 0; i < columns_; ++i)
625 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
631 bool LyXTabular::topLine(idx_type const cell, bool const onlycolumn) const
633 if (!onlycolumn && isMultiColumn(cell) &&
634 !(use_booktabs && row_of_cell(cell) == 0))
635 return cellinfo_of_cell(cell).top_line;
636 return row_info[row_of_cell(cell)].top_line;
640 bool LyXTabular::bottomLine(idx_type const cell, bool onlycolumn) const
642 if (!onlycolumn && isMultiColumn(cell) &&
643 !(use_booktabs && isLastRow(cell)))
644 return cellinfo_of_cell(cell).bottom_line;
645 return row_info[row_of_cell(cell)].bottom_line;
649 bool LyXTabular::leftLine(idx_type cell, bool onlycolumn) const
653 if (!onlycolumn && isMultiColumn(cell) &&
654 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
656 if (cellinfo_of_cell(cell).align_special.empty())
657 return cellinfo_of_cell(cell).left_line;
658 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
660 if (column_info[column_of_cell(cell)].align_special.empty())
661 return column_info[column_of_cell(cell)].left_line;
662 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
666 bool LyXTabular::rightLine(idx_type cell, bool onlycolumn) const
670 if (!onlycolumn && isMultiColumn(cell) &&
671 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
673 if (cellinfo_of_cell(cell).align_special.empty())
674 return cellinfo_of_cell(cell).right_line;
675 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
677 if (column_info[column_of_cell(cell)].align_special.empty())
678 return column_info[right_column_of_cell(cell)].right_line;
679 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
683 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
685 row_type row = row_of_cell(cell);
686 if (row > 0 && !getAdditionalHeight(row)) {
687 col_type column = column_of_cell(cell);
690 && cell_info[row][column].multicolumn
691 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
693 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
694 return row_info[row].bottom_line;
696 return cell_info[row][column].bottom_line;
702 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
704 col_type column = column_of_cell(cell);
706 row_type row = row_of_cell(cell);
708 (cell_info[row][column].multicolumn ==
709 LyXTabular::CELL_PART_OF_MULTICOLUMN));
710 if (getAdditionalWidth(cell_info[row][column].cellno))
712 return rightLine(cell_info[row][column].cellno);
718 bool LyXTabular::isLastRow(idx_type cell) const
720 return row_of_cell(cell) == rows_ - 1;
724 int LyXTabular::getAdditionalHeight(row_type row) const
726 if (!row || row >= rows_)
732 for (col_type column = 0; column < columns_ && bottom; ++column) {
733 switch (cell_info[row - 1][column].multicolumn) {
734 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
735 bottom = cell_info[row - 1][column].bottom_line;
737 case LyXTabular::CELL_NORMAL:
738 bottom = row_info[row - 1].bottom_line;
741 for (col_type column = 0; column < columns_ && top; ++column) {
742 switch (cell_info[row][column].multicolumn) {
743 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
744 top = cell_info[row][column].top_line;
746 case LyXTabular::CELL_NORMAL:
747 top = row_info[row].top_line;
750 int const interline_space = row_info[row - 1].interline_space_default ?
752 row_info[row - 1].interline_space.inPixels(width_of_tabular);
754 return interline_space + WIDTH_OF_LINE;
755 return interline_space;
759 int LyXTabular::getAdditionalWidth(idx_type cell) const
761 // internally already set in setWidthOfCell
762 // used to get it back in text.C
763 col_type const col = right_column_of_cell(cell);
764 row_type const row = row_of_cell(cell);
765 if (col < columns_ - 1 && rightLine(cell) &&
766 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
768 return WIDTH_OF_LINE;
774 // returns the maximum over all rows
775 int LyXTabular::getWidthOfColumn(idx_type cell) const
777 col_type const column1 = column_of_cell(cell);
778 col_type const column2 = right_column_of_cell(cell);
780 for (col_type i = column1; i <= column2; ++i)
781 result += column_info[i].width_of_column;
786 int LyXTabular::getWidthOfTabular() const
788 return width_of_tabular;
792 // returns true if a complete update is necessary, otherwise false
793 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
795 if (!isMultiColumn(cell))
798 row_type const row = row_of_cell(cell);
799 col_type const column1 = column_of_cell(cell);
800 col_type const column2 = right_column_of_cell(cell);
801 int const old_val = cell_info[row][column2].width_of_cell;
803 // first set columns to 0 so we can calculate the right width
804 for (col_type i = column1; i <= column2; ++i) {
805 cell_info[row][i].width_of_cell = 0;
807 // set the width to MAX_WIDTH until width > 0
808 int width = new_width + 2 * WIDTH_OF_LINE;
809 col_type i = column1;
810 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
811 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
812 width -= column_info[i].width_of_column;
815 cell_info[row][i].width_of_cell = width;
817 if (old_val != cell_info[row][column2].width_of_cell) {
818 // in this case we have to recalculate all multicolumn cells which
819 // have this column as one of theirs but not as last one
820 calculate_width_of_column_NMC(i);
821 recalculateMulticolumnsOfColumn(i);
822 calculate_width_of_column(i);
828 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
830 // the last column does not have to be recalculated because all
831 // multicolumns will have here there last multicolumn cell which
832 // always will have the whole rest of the width of the cell.
833 if (columns_ < 2 || column > (columns_ - 2))
835 for (row_type row = 0; row < rows_; ++row) {
836 int mc = cell_info[row][column].multicolumn;
837 int nmc = cell_info[row][column+1].multicolumn;
838 // we only have to update multicolumns which do not have this
839 // column as their last column!
840 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
841 (mc == CELL_PART_OF_MULTICOLUMN &&
842 nmc == CELL_PART_OF_MULTICOLUMN))
844 idx_type const cellno = cell_info[row][column].cellno;
845 setWidthOfMulticolCell(cellno,
846 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
852 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
854 row_type const row = row_of_cell(cell);
855 col_type const column1 = column_of_cell(cell);
860 if (rightLine(cell_info[row][column1].cellno, true) &&
861 column1 < columns_ - 1 &&
862 leftLine(cell_info[row][column1+1].cellno, true))
864 add_width = WIDTH_OF_LINE;
867 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
870 if (isMultiColumnReal(cell)) {
871 tmp = setWidthOfMulticolCell(cell, new_width);
873 width = new_width + 2 * WIDTH_OF_LINE + add_width;
874 cell_info[row][column1].width_of_cell = width;
875 tmp = calculate_width_of_column_NMC(column1);
877 recalculateMulticolumnsOfColumn(column1);
880 for (col_type i = 0; i < columns_; ++i)
881 calculate_width_of_column(i);
882 calculate_width_of_tabular();
887 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
890 if (!isMultiColumn(cell) || onlycolumn)
891 column_info[column_of_cell(cell)].alignment = align;
893 cellinfo_of_cell(cell).alignment = align;
897 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
900 if (!isMultiColumn(cell) || onlycolumn)
901 column_info[column_of_cell(cell)].valignment = align;
903 cellinfo_of_cell(cell).valignment = align;
910 * Allow line and paragraph breaks for fixed width cells or disallow them,
911 * merge cell paragraphs and reset layout to standard for variable width
914 void toggleFixedWidth(LCursor & cur, InsetText * inset, bool fixedWidth)
916 inset->setAutoBreakRows(fixedWidth);
920 // merge all paragraphs to one
921 BufferParams const & bp =
922 inset->getText(0)->bv_owner->buffer()->params();
923 while (inset->paragraphs().size() > 1)
924 mergeParagraph(bp, inset->paragraphs(), 0);
928 // undo information has already been recorded
929 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
930 bp.getLyXTextClass().defaultLayoutName());
937 void LyXTabular::setColumnPWidth(LCursor & cur, idx_type cell,
938 LyXLength const & width)
940 col_type const j = column_of_cell(cell);
942 column_info[j].p_width = width;
943 for (row_type i = 0; i < rows_; ++i) {
944 idx_type const cell = getCellNumber(i, j);
945 // because of multicolumns
946 toggleFixedWidth(cur, getCellInset(cell).get(),
947 !getPWidth(cell).zero());
949 // cur paragraph can become invalid after paragraphs were merged
950 if (cur.pit() > cur.lastpit())
951 cur.pit() = cur.lastpit();
952 // cur position can become invalid after newlines were removed
953 if (cur.pos() > cur.lastpos())
954 cur.pos() = cur.lastpos();
958 bool LyXTabular::setMColumnPWidth(LCursor & cur, idx_type cell,
959 LyXLength const & width)
961 if (!isMultiColumn(cell))
964 cellinfo_of_cell(cell).p_width = width;
965 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
966 // cur paragraph can become invalid after paragraphs were merged
967 if (cur.pit() > cur.lastpit())
968 cur.pit() = cur.lastpit();
969 // cur position can become invalid after newlines were removed
970 if (cur.pos() > cur.lastpos())
971 cur.pos() = cur.lastpos();
976 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
977 LyXTabular::Feature what)
979 if (what == SET_SPECIAL_MULTI)
980 cellinfo_of_cell(cell).align_special = special;
982 column_info[column_of_cell(cell)].align_special = special;
986 void LyXTabular::setAllLines(idx_type cell, bool line)
988 setTopLine(cell, line);
989 setBottomLine(cell, line);
990 setRightLine(cell, line);
991 setLeftLine(cell, line);
995 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
997 row_type const row = row_of_cell(cell);
998 if (onlycolumn || !isMultiColumn(cell))
999 row_info[row].top_line = line;
1001 cellinfo_of_cell(cell).top_line = line;
1005 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
1007 if (onlycolumn || !isMultiColumn(cell))
1008 row_info[row_of_cell(cell)].bottom_line = line;
1010 cellinfo_of_cell(cell).bottom_line = line;
1014 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
1016 if (onlycolumn || !isMultiColumn(cell))
1017 column_info[column_of_cell(cell)].left_line = line;
1019 cellinfo_of_cell(cell).left_line = line;
1023 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
1025 if (onlycolumn || !isMultiColumn(cell))
1026 column_info[right_column_of_cell(cell)].right_line = line;
1028 cellinfo_of_cell(cell).right_line = line;
1032 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
1034 if (!onlycolumn && isMultiColumn(cell))
1035 return cellinfo_of_cell(cell).alignment;
1036 return column_info[column_of_cell(cell)].alignment;
1040 LyXTabular::VAlignment
1041 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
1043 if (!onlycolumn && isMultiColumn(cell))
1044 return cellinfo_of_cell(cell).valignment;
1045 return column_info[column_of_cell(cell)].valignment;
1049 LyXLength const LyXTabular::getPWidth(idx_type cell) const
1051 if (isMultiColumn(cell))
1052 return cellinfo_of_cell(cell).p_width;
1053 return column_info[column_of_cell(cell)].p_width;
1057 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
1059 return column_info[column_of_cell(cell)].p_width;
1063 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
1065 if (isMultiColumn(cell))
1066 return cellinfo_of_cell(cell).p_width;
1071 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
1073 if (what == SET_SPECIAL_MULTI)
1074 return cellinfo_of_cell(cell).align_special;
1075 return column_info[column_of_cell(cell)].align_special;
1079 int LyXTabular::getWidthOfCell(idx_type cell) const
1081 row_type const row = row_of_cell(cell);
1082 col_type const column1 = column_of_cell(cell);
1083 col_type const column2 = right_column_of_cell(cell);
1085 for (col_type i = column1; i <= column2; ++i)
1086 result += cell_info[row][i].width_of_cell;
1091 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1095 switch (getAlignment(cell)) {
1096 case LYX_ALIGN_CENTER:
1097 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1099 case LYX_ALIGN_RIGHT:
1100 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1101 // + getAdditionalWidth(cell);
1104 // LYX_ALIGN_LEFT: nothing :-)
1108 // the LaTeX Way :-(
1114 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1116 return column_of_cell(cell) == 0;
1120 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1122 if (row > rows_ - 1)
1124 return cell_info[row][0].cellno;
1128 bool LyXTabular::isLastCellInRow(idx_type cell) const
1130 return right_column_of_cell(cell) == columns_ - 1;
1134 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1136 if (row > rows_ - 1)
1138 return cell_info[row][columns_-1].cellno;
1142 void LyXTabular::calculate_width_of_column(col_type column)
1145 for (row_type i = 0; i < rows_; ++i)
1146 maximum = max(cell_info[i][column].width_of_cell, maximum);
1147 column_info[column].width_of_column = maximum;
1152 // Calculate the columns regarding ONLY the normal cells and if this
1153 // column is inside a multicolumn cell then use it only if its the last
1154 // column of this multicolumn cell as this gives an added width to the
1155 // column, all the rest should be adapted!
1157 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1159 int const old_column_width = column_info[column].width_of_column;
1161 for (row_type i = 0; i < rows_; ++i) {
1162 idx_type cell = getCellNumber(i, column);
1163 bool ismulti = isMultiColumnReal(cell);
1164 if ((!ismulti || column == right_column_of_cell(cell)) &&
1165 cell_info[i][column].width_of_cell > max)
1167 max = cell_info[i][column].width_of_cell;
1170 column_info[column].width_of_column = max;
1171 return column_info[column].width_of_column != old_column_width;
1175 void LyXTabular::calculate_width_of_tabular()
1177 width_of_tabular = 0;
1178 for (col_type i = 0; i < columns_; ++i)
1179 width_of_tabular += column_info[i].width_of_column;
1183 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1185 if (cell >= numberofcells)
1189 return rowofcell[cell];
1193 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1195 if (cell >= numberofcells)
1196 return columns_ - 1;
1199 return columnofcell[cell];
1203 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1205 row_type const row = row_of_cell(cell);
1206 col_type column = column_of_cell(cell);
1207 while (column < columns_ - 1 &&
1208 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1214 void LyXTabular::write(Buffer const & buf, ostream & os) const
1218 << write_attribute("version", 3)
1219 << write_attribute("rows", rows_)
1220 << write_attribute("columns", columns_)
1222 // global longtable options
1224 << write_attribute("rotate", rotate)
1225 << write_attribute("booktabs", use_booktabs)
1226 << write_attribute("islongtable", is_long_tabular)
1227 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1228 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1229 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1230 << write_attribute("headTopDL", endhead.topDL)
1231 << write_attribute("headBottomDL", endhead.bottomDL)
1232 << write_attribute("footTopDL", endfoot.topDL)
1233 << write_attribute("footBottomDL", endfoot.bottomDL)
1234 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1235 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1236 << write_attribute("lastFootEmpty", endlastfoot.empty)
1238 for (col_type j = 0; j < columns_; ++j) {
1240 << write_attribute("alignment", column_info[j].alignment)
1241 << write_attribute("valignment", column_info[j].valignment)
1242 << write_attribute("leftline", column_info[j].left_line)
1243 << write_attribute("rightline", column_info[j].right_line)
1244 << write_attribute("width", column_info[j].p_width.asString())
1245 << write_attribute("special", column_info[j].align_special)
1248 for (row_type i = 0; i < rows_; ++i) {
1250 << write_attribute("topline", row_info[i].top_line)
1251 << write_attribute("bottomline", row_info[i].bottom_line);
1252 static const string def("default");
1253 if (row_info[i].top_space_default)
1254 os << write_attribute("topspace", def);
1256 os << write_attribute("topspace", row_info[i].top_space);
1257 if (row_info[i].bottom_space_default)
1258 os << write_attribute("bottomspace", def);
1260 os << write_attribute("bottomspace", row_info[i].bottom_space);
1261 if (row_info[i].interline_space_default)
1262 os << write_attribute("interlinespace", def);
1264 os << write_attribute("interlinespace", row_info[i].interline_space);
1265 os << write_attribute("endhead", row_info[i].endhead)
1266 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1267 << write_attribute("endfoot", row_info[i].endfoot)
1268 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1269 << write_attribute("newpage", row_info[i].newpage)
1271 for (col_type j = 0; j < columns_; ++j) {
1273 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1274 << write_attribute("alignment", cell_info[i][j].alignment)
1275 << write_attribute("valignment", cell_info[i][j].valignment)
1276 << write_attribute("topline", cell_info[i][j].top_line)
1277 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1278 << write_attribute("leftline", cell_info[i][j].left_line)
1279 << write_attribute("rightline", cell_info[i][j].right_line)
1280 << write_attribute("rotate", cell_info[i][j].rotate)
1281 << write_attribute("usebox", cell_info[i][j].usebox)
1282 << write_attribute("width", cell_info[i][j].p_width)
1283 << write_attribute("special", cell_info[i][j].align_special)
1285 os << "\\begin_inset ";
1286 cell_info[i][j].inset->write(buf, os);
1287 os << "\n\\end_inset\n"
1292 os << "</lyxtabular>\n";
1296 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1299 istream & is = lex.getStream();
1301 l_getline(is, line);
1302 if (!prefixIs(line, "<lyxtabular ")
1303 && !prefixIs(line, "<LyXTabular ")) {
1304 BOOST_ASSERT(false);
1309 if (!getTokenValue(line, "version", version))
1311 BOOST_ASSERT(version >= 2);
1314 if (!getTokenValue(line, "rows", rows_arg))
1317 if (!getTokenValue(line, "columns", columns_arg))
1319 init(buf.params(), rows_arg, columns_arg);
1320 l_getline(is, line);
1321 if (!prefixIs(line, "<features")) {
1322 lyxerr << "Wrong tabular format (expected <features ...> got"
1323 << line << ')' << endl;
1326 getTokenValue(line, "rotate", rotate);
1327 getTokenValue(line, "booktabs", use_booktabs);
1328 getTokenValue(line, "islongtable", is_long_tabular);
1329 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1330 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1331 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1332 getTokenValue(line, "headTopDL", endhead.topDL);
1333 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1334 getTokenValue(line, "footTopDL", endfoot.topDL);
1335 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1336 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1337 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1338 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1340 for (col_type j = 0; j < columns_; ++j) {
1342 if (!prefixIs(line,"<column")) {
1343 lyxerr << "Wrong tabular format (expected <column ...> got"
1344 << line << ')' << endl;
1347 getTokenValue(line, "alignment", column_info[j].alignment);
1348 getTokenValue(line, "valignment", column_info[j].valignment);
1349 getTokenValue(line, "leftline", column_info[j].left_line);
1350 getTokenValue(line, "rightline", column_info[j].right_line);
1351 getTokenValue(line, "width", column_info[j].p_width);
1352 getTokenValue(line, "special", column_info[j].align_special);
1355 for (row_type i = 0; i < rows_; ++i) {
1356 l_getline(is, line);
1357 if (!prefixIs(line, "<row")) {
1358 lyxerr << "Wrong tabular format (expected <row ...> got"
1359 << line << ')' << endl;
1362 getTokenValue(line, "topline", row_info[i].top_line);
1363 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1364 getTokenValue(line, "topspace", row_info[i].top_space,
1365 row_info[i].top_space_default);
1366 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1367 row_info[i].bottom_space_default);
1368 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1369 row_info[i].interline_space_default);
1370 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1371 getTokenValue(line, "endhead", row_info[i].endhead);
1372 getTokenValue(line, "endfoot", row_info[i].endfoot);
1373 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1374 getTokenValue(line, "newpage", row_info[i].newpage);
1375 for (col_type j = 0; j < columns_; ++j) {
1376 l_getline(is, line);
1377 if (!prefixIs(line, "<cell")) {
1378 lyxerr << "Wrong tabular format (expected <cell ...> got"
1379 << line << ')' << endl;
1382 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1383 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1384 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1385 getTokenValue(line, "topline", cell_info[i][j].top_line);
1386 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1387 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1388 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1389 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1390 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1391 getTokenValue(line, "width", cell_info[i][j].p_width);
1392 getTokenValue(line, "special", cell_info[i][j].align_special);
1393 l_getline(is, line);
1394 if (prefixIs(line, "\\begin_inset")) {
1395 cell_info[i][j].inset->read(buf, lex);
1396 l_getline(is, line);
1398 if (!prefixIs(line, "</cell>")) {
1399 lyxerr << "Wrong tabular format (expected </cell> got"
1400 << line << ')' << endl;
1404 l_getline(is, line);
1405 if (!prefixIs(line, "</row>")) {
1406 lyxerr << "Wrong tabular format (expected </row> got"
1407 << line << ')' << endl;
1411 while (!prefixIs(line, "</lyxtabular>")) {
1412 l_getline(is, line);
1414 set_row_column_number_info();
1418 bool LyXTabular::isMultiColumn(idx_type cell) const
1420 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1424 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1426 return column_of_cell(cell) != right_column_of_cell(cell) &&
1427 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1431 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1433 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1437 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1440 cellstruct & cs = cellinfo_of_cell(cell);
1441 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1442 cs.alignment = column_info[column_of_cell(cell)].alignment;
1443 cs.top_line = row_info[row_of_cell(cell)].top_line;
1444 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1445 cs.left_line = column_info[column_of_cell(cell)].left_line;
1446 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1447 for (idx_type i = 1; i < number; ++i) {
1448 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1449 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1450 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1453 set_row_column_number_info();
1457 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1459 row_type const row = row_of_cell(cell);
1460 col_type column = column_of_cell(cell);
1461 idx_type result = 1;
1463 while (column < columns_ &&
1464 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1473 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1475 row_type const row = row_of_cell(cell);
1476 col_type column = column_of_cell(cell);
1478 idx_type result = 0;
1480 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1481 cell_info[row][column].multicolumn = CELL_NORMAL;
1483 while (column < columns_ &&
1484 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1486 cell_info[row][column].multicolumn = CELL_NORMAL;
1491 set_row_column_number_info();
1496 void LyXTabular::setBookTabs(bool what)
1498 use_booktabs = what;
1502 bool LyXTabular::useBookTabs() const
1504 return use_booktabs;
1508 void LyXTabular::setLongTabular(bool what)
1510 is_long_tabular = what;
1514 bool LyXTabular::isLongTabular() const
1516 return is_long_tabular;
1520 void LyXTabular::setRotateTabular(bool flag)
1526 bool LyXTabular::getRotateTabular() const
1532 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1534 cellinfo_of_cell(cell).rotate = flag;
1538 bool LyXTabular::getRotateCell(idx_type cell) const
1540 return cellinfo_of_cell(cell).rotate;
1544 bool LyXTabular::needRotating() const
1548 for (row_type i = 0; i < rows_; ++i)
1549 for (col_type j = 0; j < columns_; ++j)
1550 if (cell_info[i][j].rotate)
1556 bool LyXTabular::isLastCell(idx_type cell) const
1558 if (cell + 1 < numberofcells)
1564 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1566 if (row_of_cell(cell) > 0)
1567 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1572 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1574 if (row_of_cell(cell) + 1 < rows_)
1575 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1580 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1582 if (row_of_cell(cell) == 0)
1584 if (!isMultiColumn(cell))
1585 return getCellAbove(cell);
1586 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1590 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1592 if (row_of_cell(cell) + 1 >= rows_)
1594 if (!isMultiColumn(cell))
1595 return getCellBelow(cell);
1596 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1600 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1601 col_type column) const
1603 BOOST_ASSERT(column != npos && column < columns_ &&
1604 row != npos && row < rows_);
1605 return cell_info[row][column].cellno;
1609 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1611 cellinfo_of_cell(cell).usebox = type;
1615 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1617 if (column_info[column_of_cell(cell)].p_width.zero() &&
1618 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1620 if (cellinfo_of_cell(cell).usebox > 1)
1621 return cellinfo_of_cell(cell).usebox;
1622 return useParbox(cell);
1627 // This are functions used for the longtable support
1629 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1635 row_info[row].endfirsthead = flag;
1639 row_info[row].endhead = flag;
1644 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1647 hd.set = haveLTHead();
1648 return row_info[row].endhead;
1652 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1655 hd.set = haveLTFirstHead();
1656 return row_info[row].endfirsthead;
1660 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1666 row_info[row].endlastfoot = flag;
1670 row_info[row].endfoot = flag;
1675 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1678 fd.set = haveLTFoot();
1679 return row_info[row].endfoot;
1683 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1686 fd.set = haveLTLastFoot();
1687 return row_info[row].endlastfoot;
1691 void LyXTabular::setLTNewPage(row_type row, bool what)
1693 row_info[row].newpage = what;
1697 bool LyXTabular::getLTNewPage(row_type row) const
1699 return row_info[row].newpage;
1703 bool LyXTabular::haveLTHead() const
1705 for (row_type i = 0; i < rows_; ++i)
1706 if (row_info[i].endhead)
1712 bool LyXTabular::haveLTFirstHead() const
1714 if (endfirsthead.empty)
1716 for (row_type i = 0; i < rows_; ++i)
1717 if (row_info[i].endfirsthead)
1723 bool LyXTabular::haveLTFoot() const
1725 for (row_type i = 0; i < rows_; ++i)
1726 if (row_info[i].endfoot)
1732 bool LyXTabular::haveLTLastFoot() const
1734 if (endlastfoot.empty)
1736 for (row_type i = 0; i < rows_; ++i)
1737 if (row_info[i].endlastfoot)
1743 // end longtable support functions
1745 void LyXTabular::setAscentOfRow(row_type row, int height)
1747 if (row >= rows_ || row_info[row].ascent_of_row == height)
1749 row_info[row].ascent_of_row = height;
1753 void LyXTabular::setDescentOfRow(row_type row, int height)
1755 if (row >= rows_ || row_info[row].descent_of_row == height)
1757 row_info[row].descent_of_row = height;
1761 int LyXTabular::getAscentOfRow(row_type row) const
1765 return row_info[row].ascent_of_row;
1769 int LyXTabular::getDescentOfRow(row_type row) const
1771 BOOST_ASSERT(row < rows_);
1772 return row_info[row].descent_of_row;
1776 int LyXTabular::getHeightOfTabular() const
1779 for (row_type row = 0; row < rows_; ++row)
1780 height += getAscentOfRow(row) + getDescentOfRow(row) +
1781 getAdditionalHeight(row);
1786 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1788 BOOST_ASSERT(row < rows_);
1789 BOOST_ASSERT(column < columns_);
1790 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1794 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1796 // FIXME: assert or return 0 as in TeXBottomHLine()?
1797 BOOST_ASSERT(row != npos);
1798 BOOST_ASSERT(row < rows_);
1800 idx_type const fcell = getFirstCellInRow(row);
1801 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1804 for (idx_type i = fcell; i < n; ++i) {
1808 if (use_booktabs && row == 0) {
1810 } else if (tmp == n - fcell) {
1811 os << (use_booktabs ? "\\midrule " : "\\hline ");
1813 for (idx_type i = fcell; i < n; ++i) {
1815 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1816 << column_of_cell(i) + 1
1818 << right_column_of_cell(i) + 1
1830 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1832 // FIXME: return 0 or assert as in TeXTopHLine()?
1833 if (row == npos || row >= rows_)
1836 idx_type const fcell = getFirstCellInRow(row);
1837 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1840 for (idx_type i = fcell; i < n; ++i) {
1844 if (use_booktabs && row == rows_ - 1) {
1845 os << "\\bottomrule";
1846 } else if (tmp == n - fcell) {
1847 os << (use_booktabs ? "\\midrule" : "\\hline");
1849 for (idx_type i = fcell; i < n; ++i) {
1850 if (bottomLine(i)) {
1851 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1852 << column_of_cell(i) + 1
1854 << right_column_of_cell(i) + 1
1866 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1870 if (getRotateCell(cell)) {
1871 os << "\\begin{sideways}\n";
1874 if (isMultiColumn(cell)) {
1875 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1876 if (!cellinfo_of_cell(cell).align_special.empty()) {
1877 os << cellinfo_of_cell(cell).align_special << "}{";
1879 if (leftLine(cell) &&
1880 (isFirstCellInRow(cell) ||
1881 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1882 !rightLine(cell - 1, true))))
1886 if (!getPWidth(cell).zero()) {
1887 switch (getVAlignment(cell)) {
1888 case LYX_VALIGN_TOP:
1891 case LYX_VALIGN_MIDDLE:
1894 case LYX_VALIGN_BOTTOM:
1899 << getPWidth(cell).asLatexString()
1902 switch (getAlignment(cell)) {
1903 case LYX_ALIGN_LEFT:
1906 case LYX_ALIGN_RIGHT:
1914 if (rightLine(cell))
1916 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1922 if (getUsebox(cell) == BOX_PARBOX) {
1924 switch (getVAlignment(cell)) {
1925 case LYX_VALIGN_TOP:
1928 case LYX_VALIGN_MIDDLE:
1931 case LYX_VALIGN_BOTTOM:
1935 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1936 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1937 os << "\\begin{minipage}[";
1938 switch (getVAlignment(cell)) {
1939 case LYX_VALIGN_TOP:
1942 case LYX_VALIGN_MIDDLE:
1945 case LYX_VALIGN_BOTTOM:
1949 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1956 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1961 if (getUsebox(cell) == BOX_PARBOX)
1963 else if (getUsebox(cell) == BOX_MINIPAGE) {
1964 os << "%\n\\end{minipage}";
1967 if (isMultiColumn(cell)) {
1970 if (getRotateCell(cell)) {
1971 os << "%\n\\end{sideways}";
1978 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1979 OutputParams const & runparams) const
1981 if (!is_long_tabular)
1985 // output header info
1987 if (endhead.topDL) {
1991 for (row_type i = 0; i < rows_; ++i) {
1992 if (row_info[i].endhead) {
1993 ret += TeXRow(os, i, buf, runparams);
1996 if (endhead.bottomDL) {
2000 os << "\\endhead\n";
2002 if (endfirsthead.empty) {
2003 os << "\\endfirsthead\n";
2007 // output firstheader info
2008 if (haveLTFirstHead()) {
2009 if (endfirsthead.topDL) {
2013 for (row_type i = 0; i < rows_; ++i) {
2014 if (row_info[i].endfirsthead) {
2015 ret += TeXRow(os, i, buf, runparams);
2018 if (endfirsthead.bottomDL) {
2022 os << "\\endfirsthead\n";
2025 // output footer info
2027 if (endfoot.topDL) {
2031 for (row_type i = 0; i < rows_; ++i) {
2032 if (row_info[i].endfoot) {
2033 ret += TeXRow(os, i, buf, runparams);
2036 if (endfoot.bottomDL) {
2040 os << "\\endfoot\n";
2042 if (endlastfoot.empty) {
2043 os << "\\endlastfoot\n";
2047 // output lastfooter info
2048 if (haveLTLastFoot()) {
2049 if (endlastfoot.topDL) {
2053 for (row_type i = 0; i < rows_; ++i) {
2054 if (row_info[i].endlastfoot) {
2055 ret += TeXRow(os, i, buf, runparams);
2058 if (endlastfoot.bottomDL) {
2062 os << "\\endlastfoot\n";
2069 bool LyXTabular::isValidRow(row_type row) const
2071 if (!is_long_tabular)
2073 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2074 !row_info[row].endfoot && !row_info[row].endlastfoot;
2078 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
2079 OutputParams const & runparams) const
2081 idx_type cell = getCellNumber(i, 0);
2082 int ret = TeXTopHLine(os, i);
2083 if (row_info[i].top_space_default) {
2085 os << "\\addlinespace\n";
2087 os << "\\noalign{\\vskip\\doublerulesep}\n";
2088 } else if(!row_info[i].top_space.zero()) {
2090 os << "\\addlinespace["
2091 << row_info[i].top_space.asLatexString() << "]\n";
2093 os << "\\noalign{\\vskip"
2094 << row_info[i].top_space.asLatexString() << "}\n";
2099 for (col_type j = 0; j < columns_; ++j) {
2100 if (isPartOfMultiColumn(i, j))
2102 ret += TeXCellPreamble(os, cell);
2103 shared_ptr<InsetText> inset = getCellInset(cell);
2105 Paragraph const & par = inset->paragraphs().front();
2106 bool rtl = par.isRightToLeftPar(buf.params())
2108 && getPWidth(cell).zero();
2112 ret += inset->latex(buf, os, runparams);
2116 ret += TeXCellPostamble(os, cell);
2117 if (!isLastCellInRow(cell)) { // not last cell in row
2123 os << "\\tabularnewline";
2124 if (row_info[i].bottom_space_default) {
2126 os << "\\addlinespace";
2128 os << "[\\doublerulesep]";
2129 } else if (!row_info[i].bottom_space.zero()) {
2131 os << "\\addlinespace";
2132 os << '[' << row_info[i].bottom_space.asLatexString() << ']';
2136 ret += TeXBottomHLine(os, i);
2137 if (row_info[i].interline_space_default) {
2139 os << "\\addlinespace\n";
2141 os << "\\noalign{\\vskip\\doublerulesep}\n";
2142 } else if (!row_info[i].interline_space.zero()) {
2144 os << "\\addlinespace["
2145 << row_info[i].interline_space.asLatexString()
2148 os << "\\noalign{\\vskip"
2149 << row_info[i].interline_space.asLatexString()
2157 int LyXTabular::latex(Buffer const & buf, ostream & os,
2158 OutputParams const & runparams) const
2162 //+---------------------------------------------------------------------
2163 //+ first the opening preamble +
2164 //+---------------------------------------------------------------------
2167 os << "\\begin{sideways}\n";
2170 if (is_long_tabular)
2171 os << "\\begin{longtable}{";
2173 os << "\\begin{tabular}{";
2174 for (col_type i = 0; i < columns_; ++i) {
2175 if (!column_info[i].align_special.empty()) {
2176 os << column_info[i].align_special;
2178 if (!use_booktabs && column_info[i].left_line)
2180 if (!column_info[i].p_width.zero()) {
2181 switch (column_info[i].alignment) {
2182 case LYX_ALIGN_LEFT:
2183 os << ">{\\raggedright}";
2185 case LYX_ALIGN_RIGHT:
2186 os << ">{\\raggedleft}";
2188 case LYX_ALIGN_CENTER:
2189 os << ">{\\centering}";
2191 case LYX_ALIGN_NONE:
2192 case LYX_ALIGN_BLOCK:
2193 case LYX_ALIGN_LAYOUT:
2194 case LYX_ALIGN_SPECIAL:
2198 switch (column_info[i].valignment) {
2199 case LYX_VALIGN_TOP:
2202 case LYX_VALIGN_MIDDLE:
2205 case LYX_VALIGN_BOTTOM:
2210 << column_info[i].p_width.asLatexString()
2213 switch (column_info[i].alignment) {
2214 case LYX_ALIGN_LEFT:
2217 case LYX_ALIGN_RIGHT:
2225 if (!use_booktabs && column_info[i].right_line)
2232 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2234 //+---------------------------------------------------------------------
2235 //+ the single row and columns (cells) +
2236 //+---------------------------------------------------------------------
2238 for (row_type i = 0; i < rows_; ++i) {
2239 if (isValidRow(i)) {
2240 ret += TeXRow(os, i, buf, runparams);
2241 if (is_long_tabular && row_info[i].newpage) {
2242 os << "\\newpage\n";
2248 //+---------------------------------------------------------------------
2249 //+ the closing of the tabular +
2250 //+---------------------------------------------------------------------
2252 if (is_long_tabular)
2253 os << "\\end{longtable}";
2255 os << "\\end{tabular}";
2257 os << "\n\\end{sideways}";
2265 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2266 const OutputParams & runparams) const
2268 os << "<tabular ca=\"";
2269 for (col_type i = 0; i < columns_; ++i) {
2270 switch (column_info[i].alignment) {
2271 case LYX_ALIGN_LEFT:
2274 case LYX_ALIGN_RIGHT:
2285 for (row_type i = 0; i < rows_; ++i) {
2286 for (col_type j = 0; j < columns_; ++j) {
2287 if (isPartOfMultiColumn(i, j))
2289 shared_ptr<InsetText> inset = getCellInset(cell);
2291 ret += inset->linuxdoc(buf, os, runparams);
2293 if (isLastCellInRow(cell)) {
2302 os << "</tabular>\n";
2307 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2308 OutputParams const & runparams) const
2311 idx_type cell = getFirstCellInRow(row);
2314 for (col_type j = 0; j < columns_; ++j) {
2315 if (isPartOfMultiColumn(row, j))
2318 os << "<entry align=\"";
2319 switch (getAlignment(cell)) {
2320 case LYX_ALIGN_LEFT:
2323 case LYX_ALIGN_RIGHT:
2331 os << "\" valign=\"";
2332 switch (getVAlignment(cell)) {
2333 case LYX_VALIGN_TOP:
2336 case LYX_VALIGN_BOTTOM:
2339 case LYX_VALIGN_MIDDLE:
2344 if (isMultiColumn(cell)) {
2345 os << " namest=\"col" << j << "\" ";
2346 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2350 ret += getCellInset(cell)->docbook(buf, os, runparams);
2359 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2360 OutputParams const & runparams) const
2364 //+---------------------------------------------------------------------
2365 //+ first the opening preamble +
2366 //+---------------------------------------------------------------------
2368 os << "<tgroup cols=\"" << columns_
2369 << "\" colsep=\"1\" rowsep=\"1\">\n";
2371 for (col_type i = 0; i < columns_; ++i) {
2372 os << "<colspec colname=\"col" << i << "\" align=\"";
2373 switch (column_info[i].alignment) {
2374 case LYX_ALIGN_LEFT:
2377 case LYX_ALIGN_RIGHT:
2385 if (runparams.flavor == OutputParams::XML)
2391 //+---------------------------------------------------------------------
2392 //+ Long Tabular case +
2393 //+---------------------------------------------------------------------
2395 // output header info
2396 if (haveLTHead() || haveLTFirstHead()) {
2399 for (row_type i = 0; i < rows_; ++i) {
2400 if (row_info[i].endhead || row_info[i].endfirsthead) {
2401 ret += docbookRow(buf, os, i, runparams);
2407 // output footer info
2408 if (haveLTFoot() || haveLTLastFoot()) {
2411 for (row_type i = 0; i < rows_; ++i) {
2412 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2413 ret += docbookRow(buf, os, i, runparams);
2420 //+---------------------------------------------------------------------
2421 //+ the single row and columns (cells) +
2422 //+---------------------------------------------------------------------
2426 for (row_type i = 0; i < rows_; ++i) {
2427 if (isValidRow(i)) {
2428 ret += docbookRow(buf, os, i, runparams);
2433 //+---------------------------------------------------------------------
2434 //+ the closing of the tabular +
2435 //+---------------------------------------------------------------------
2444 int LyXTabular::asciiTopHLine(ostream & os, row_type row,
2445 vector<unsigned int> const & clen) const
2447 idx_type const fcell = getFirstCellInRow(row);
2448 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2451 for (idx_type i = fcell; i < n; ++i) {
2461 for (idx_type i = fcell; i < n; ++i) {
2472 col_type column = column_of_cell(i);
2473 int len = clen[column];
2474 while (column < columns_ - 1
2475 && isPartOfMultiColumn(row, ++column))
2476 len += clen[column] + 4;
2477 os << string(len, ch);
2492 int LyXTabular::asciiBottomHLine(ostream & os, row_type row,
2493 vector<unsigned int> const & clen) const
2495 idx_type const fcell = getFirstCellInRow(row);
2496 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2499 for (idx_type i = fcell; i < n; ++i) {
2500 if (bottomLine(i)) {
2509 for (idx_type i = fcell; i < n; ++i) {
2510 if (bottomLine(i)) {
2520 col_type column = column_of_cell(i);
2521 int len = clen[column];
2522 while (column < columns_ -1
2523 && isPartOfMultiColumn(row, ++column))
2524 len += clen[column] + 4;
2525 os << string(len, ch);
2526 if (bottomLine(i)) {
2540 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2541 OutputParams const & runparams,
2542 idx_type cell, row_type row, col_type column,
2543 vector<unsigned int> const & clen,
2544 bool onlydata) const
2547 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2559 unsigned int len1 = sstr.str().length();
2560 unsigned int len2 = clen[column];
2561 while (column < columns_ -1
2562 && isPartOfMultiColumn(row, ++column))
2563 len2 += clen[column] + 4;
2566 switch (getAlignment(cell)) {
2568 case LYX_ALIGN_LEFT:
2571 case LYX_ALIGN_RIGHT:
2575 case LYX_ALIGN_CENTER:
2581 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2583 if (rightLine(cell))
2592 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2593 OutputParams const & runparams,
2595 bool onlydata, unsigned char delim) const
2599 // first calculate the width of the single columns
2600 vector<unsigned int> clen(columns_);
2603 // first all non (real) multicolumn cells!
2604 for (col_type j = 0; j < columns_; ++j) {
2606 for (row_type i = 0; i < rows_; ++i) {
2607 idx_type cell = getCellNumber(i, j);
2608 if (isMultiColumnReal(cell))
2611 getCellInset(cell)->plaintext(buf, sstr, runparams);
2612 if (clen[j] < sstr.str().length())
2613 clen[j] = sstr.str().length();
2616 // then all (real) multicolumn cells!
2617 for (col_type j = 0; j < columns_; ++j) {
2618 for (row_type i = 0; i < rows_; ++i) {
2619 idx_type cell = getCellNumber(i, j);
2620 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2623 getCellInset(cell)->plaintext(buf, sstr, runparams);
2624 int len = int(sstr.str().length());
2625 idx_type const n = cells_in_multicolumn(cell);
2626 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2628 if (len > int(clen[j + n - 1]))
2629 clen[j + n - 1] = len;
2634 for (row_type i = 0; i < rows_; ++i) {
2635 if (!onlydata && asciiTopHLine(os, i, clen))
2636 os << string(depth * 2, ' ');
2637 for (col_type j = 0; j < columns_; ++j) {
2638 if (isPartOfMultiColumn(i, j))
2640 if (onlydata && j > 0)
2642 ret += asciiPrintCell(buf, os, runparams,
2643 cell, i, j, clen, onlydata);
2648 os << string(depth * 2, ' ');
2649 if (asciiBottomHLine(os, i, clen))
2650 os << string(depth * 2, ' ');
2657 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2659 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2663 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2664 col_type column) const
2666 return cell_info[row][column].inset;
2670 void LyXTabular::setCellInset(row_type row, col_type column,
2671 shared_ptr<InsetText> ins) const
2673 cell_info[row][column].inset = ins;
2677 LyXTabular::idx_type
2678 LyXTabular::getCellFromInset(InsetBase const * inset) const
2680 // is this inset part of the tabular?
2682 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2683 BOOST_ASSERT(false);
2686 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2687 if (getCellInset(cell).get() == inset) {
2688 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2689 << "cell=" << cell << endl;
2693 // We should have found a cell at this point
2694 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2695 << inset << " not found!" << endl;
2696 BOOST_ASSERT(false);
2702 void LyXTabular::validate(LaTeXFeatures & features) const
2704 features.require("NeedTabularnewline");
2706 features.require("booktabs");
2707 if (isLongTabular())
2708 features.require("longtable");
2710 features.require("rotating");
2711 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2712 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2713 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2714 features.require("array");
2715 getCellInset(cell)->validate(features);
2720 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2722 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2723 ParagraphList::const_iterator cit = parlist.begin();
2724 ParagraphList::const_iterator end = parlist.end();
2726 for (; cit != end; ++cit)
2727 for (int i = 0; i < cit->size(); ++i)
2728 if (cit->isNewline(i))