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"
43 using support::prefixIs;
46 using support::suffixIs;
48 using boost::shared_ptr;
49 using boost::dynamic_pointer_cast;
58 using std::ostringstream;
62 #ifndef CXX_GLOBAL_CSTD
69 int const WIDTH_OF_LINE = 5;
70 int const default_line_space = 10;
74 string const write_attribute(string const & name, T const & t)
76 string const s = tostr(t);
77 return s.empty() ? s : " " + name + "=\"" + s + "\"";
81 string const write_attribute(string const & name, string const & t)
83 return t.empty() ? t : " " + name + "=\"" + t + "\"";
88 string const write_attribute(string const & name, bool const & b)
90 // we write only true attribute values so we remove a bit of the
91 // file format bloat for tabulars.
92 return b ? write_attribute(name, convert<string>(b)) : string();
97 string const write_attribute(string const & name, int const & i)
99 // we write only true attribute values so we remove a bit of the
100 // file format bloat for tabulars.
101 return i ? write_attribute(name, convert<string>(i)) : string();
106 string const write_attribute(string const & name, LyXTabular::idx_type const & i)
108 // we write only true attribute values so we remove a bit of the
109 // file format bloat for tabulars.
110 return i ? write_attribute(name, convert<string>(i)) : string();
115 string const write_attribute(string const & name, LyXLength const & value)
117 // we write only the value if we really have one same reson as above.
118 return value.zero() ? string() : write_attribute(name, value.asString());
122 string const tostr(LyXAlignment const & num)
127 case LYX_ALIGN_BLOCK:
131 case LYX_ALIGN_CENTER:
133 case LYX_ALIGN_RIGHT:
135 case LYX_ALIGN_LAYOUT:
137 case LYX_ALIGN_SPECIAL:
144 string const tostr(LyXTabular::VAlignment const & num)
147 case LyXTabular::LYX_VALIGN_TOP:
149 case LyXTabular::LYX_VALIGN_MIDDLE:
151 case LyXTabular::LYX_VALIGN_BOTTOM:
158 string const tostr(LyXTabular::BoxType const & num)
161 case LyXTabular::BOX_NONE:
163 case LyXTabular::BOX_PARBOX:
165 case LyXTabular::BOX_MINIPAGE:
172 // I would have liked a fromstr template a lot better. (Lgb)
173 bool string2type(string const str, LyXAlignment & num)
176 num = LYX_ALIGN_NONE;
177 else if (str == "block")
178 num = LYX_ALIGN_BLOCK;
179 else if (str == "left")
180 num = LYX_ALIGN_LEFT;
181 else if (str == "center")
182 num = LYX_ALIGN_CENTER;
183 else if (str == "right")
184 num = LYX_ALIGN_RIGHT;
191 bool string2type(string const str, LyXTabular::VAlignment & num)
194 num = LyXTabular::LYX_VALIGN_TOP;
195 else if (str == "middle" )
196 num = LyXTabular::LYX_VALIGN_MIDDLE;
197 else if (str == "bottom")
198 num = LyXTabular::LYX_VALIGN_BOTTOM;
205 bool string2type(string const str, LyXTabular::BoxType & num)
208 num = LyXTabular::BOX_NONE;
209 else if (str == "parbox")
210 num = LyXTabular::BOX_PARBOX;
211 else if (str == "minipage")
212 num = LyXTabular::BOX_MINIPAGE;
219 bool string2type(string const str, bool & num)
223 else if (str == "false")
231 bool getTokenValue(string const & str, char const * token, string & ret)
234 size_t token_length = strlen(token);
235 string::size_type pos = str.find(token);
237 if (pos == string::npos || pos + token_length + 1 >= str.length()
238 || str[pos + token_length] != '=')
240 pos += token_length + 1;
242 if (ch != '"' && ch != '\'') { // only read till next space
246 while (pos < str.length() - 1 && str[++pos] != ch)
253 bool getTokenValue(string const & str, char const * token, int & num)
257 if (!getTokenValue(str, token, tmp))
259 num = convert<int>(tmp);
264 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
267 return getTokenValue(str, token, tmp) && string2type(tmp, num);
271 bool getTokenValue(string const & str, char const * token,
272 LyXTabular::VAlignment & num)
275 return getTokenValue(str, token, tmp) && string2type(tmp, num);
279 bool getTokenValue(string const & str, char const * token,
280 LyXTabular::BoxType & num)
283 return getTokenValue(str, token, tmp) && string2type(tmp, num);
287 bool getTokenValue(string const & str, char const * token, bool & flag)
289 // set the flag always to false as this should be the default for bools
290 // not in the file-format.
293 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
297 bool getTokenValue(string const & str, char const * token, LyXLength & len)
299 // set the lenght to be zero() as default as this it should be if not
300 // in the file format.
303 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
307 bool getTokenValue(string const & str, char const * token, LyXLength & len, bool & flag)
312 if (!getTokenValue(str, token, tmp))
314 if (tmp == "default") {
318 return isValidLength(tmp, &len);
322 void l_getline(istream & is, string & str)
325 while (str.empty()) {
327 if (!str.empty() && str[str.length() - 1] == '\r')
328 str.erase(str.length() - 1);
334 /// Define a few methods for the inner structs
336 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
339 multicolumn(LyXTabular::CELL_NORMAL),
340 alignment(LYX_ALIGN_CENTER),
341 valignment(LYX_VALIGN_TOP),
348 inset(new InsetText(bp))
352 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
354 width_of_cell(cs.width_of_cell),
355 multicolumn(cs.multicolumn),
356 alignment(cs.alignment),
357 valignment(cs.valignment),
358 top_line(cs.top_line),
359 bottom_line(cs.bottom_line),
360 left_line(cs.left_line),
361 right_line(cs.right_line),
364 align_special(cs.align_special),
366 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
370 LyXTabular::cellstruct &
371 LyXTabular::cellstruct::operator=(cellstruct cs)
379 LyXTabular::cellstruct::swap(cellstruct & rhs)
381 std::swap(cellno, rhs.cellno);
382 std::swap(width_of_cell, rhs.width_of_cell);
383 std::swap(multicolumn, rhs.multicolumn);
384 std::swap(alignment, rhs.alignment);
385 std::swap(valignment, rhs.valignment);
386 std::swap(top_line, rhs.top_line);
387 std::swap(bottom_line, rhs.bottom_line);
388 std::swap(left_line, rhs.left_line);
389 std::swap(right_line, rhs.right_line);
390 std::swap(usebox, rhs.usebox);
391 std::swap(rotate, rhs.rotate);
392 std::swap(align_special, rhs.align_special);
393 p_width.swap(rhs.p_width);
394 inset.swap(rhs.inset);
398 LyXTabular::rowstruct::rowstruct()
403 top_space_default(false),
404 bottom_space_default(false),
405 interline_space_default(false),
414 LyXTabular::columnstruct::columnstruct()
415 : alignment(LYX_ALIGN_CENTER),
416 valignment(LYX_VALIGN_TOP),
424 LyXTabular::ltType::ltType()
431 LyXTabular::LyXTabular(BufferParams const & bp, row_type rows_arg,
432 col_type columns_arg)
434 init(bp, rows_arg, columns_arg);
438 // activates all lines and sets all widths to 0
439 void LyXTabular::init(BufferParams const & bp, row_type rows_arg,
440 col_type columns_arg)
443 columns_ = columns_arg;
444 row_info = row_vector(rows_);
445 column_info = column_vector(columns_);
446 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
447 row_info.reserve(10);
448 column_info.reserve(10);
449 cell_info.reserve(100);
451 for (row_type i = 0; i < rows_; ++i)
452 cell_info[i].back().right_line = true;
453 row_info.back().bottom_line = true;
454 row_info.front().bottom_line = true;
455 column_info.back().right_line = true;
456 is_long_tabular = false;
458 use_booktabs = false;
462 void LyXTabular::fixCellNums()
465 for (row_type i = 0; i < rows_; ++i) {
466 for (col_type j = 0; j < columns_; ++j) {
467 // When debugging it can be nice to set
469 cell_info[i][j].inset->setDrawFrame(false);
470 cell_info[i][j].cellno = cellno++;
472 cell_info[i].back().right_line = true;
475 set_row_column_number_info();
479 void LyXTabular::appendRow(BufferParams const & bp, idx_type const cell)
483 row_type const row = row_of_cell(cell);
485 row_vector::iterator rit = row_info.begin() + row;
486 row_info.insert(rit, rowstruct());
487 // now set the values of the row before
488 row_info[row] = row_info[row + 1];
490 cell_vvector old(rows_ - 1);
491 for (row_type i = 0; i < rows_ - 1; ++i)
492 swap(cell_info[i], old[i]);
494 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
496 for (row_type i = 0; i <= row; ++i)
497 swap(cell_info[i], old[i]);
498 for (row_type i = row + 2; i < rows_; ++i)
499 swap(cell_info[i], old[i - 1]);
502 // FIXME: Change Tracking (MG)
503 for (col_type j = 0; j < columns_; ++j)
504 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
506 set_row_column_number_info();
510 void LyXTabular::deleteRow(row_type const row)
512 // Not allowed to delete last row
516 row_info.erase(row_info.begin() + row);
517 cell_info.erase(cell_info.begin() + row);
523 void LyXTabular::copyRow(BufferParams const & bp, row_type const row)
527 row_info.insert(row_info.begin() + row, row_info[row]);
528 cell_info.insert(cell_info.begin() + row, cell_info[row]);
531 // FIXME: Change Tracking (MG)
532 for (col_type j = 0; j < columns_; ++j)
533 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
535 set_row_column_number_info();
539 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
543 col_type const column = column_of_cell(cell);
544 column_vector::iterator cit = column_info.begin() + column + 1;
545 column_info.insert(cit, columnstruct());
546 // set the column values of the column before
547 column_info[column + 1] = column_info[column];
549 for (row_type i = 0; i < rows_; ++i) {
550 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
552 // care about multicolumns
553 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
554 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
556 if (column + 2 >= columns_
557 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
558 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
561 for (row_type i = 0; i < rows_; ++i) {
562 cell_info[i][column + 1].inset->clear();
563 // FIXME: Change Tracking (MG)
565 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
571 void LyXTabular::deleteColumn(col_type const column)
573 // Not allowed to delete last column
577 column_info.erase(column_info.begin() + column);
578 for (row_type i = 0; i < rows_; ++i)
579 cell_info[i].erase(cell_info[i].begin() + column);
585 void LyXTabular::copyColumn(BufferParams const & bp, col_type const column)
589 column_info.insert(column_info.begin() + column, column_info[column]);
591 for (row_type i = 0; i < rows_; ++i)
592 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
595 // FIXME: Change Tracking (MG)
596 for (row_type i = 0; i < rows_; ++i)
597 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
602 void LyXTabular::set_row_column_number_info()
605 for (row_type row = 0; row < rows_; ++row) {
606 for (col_type column = 0; column < columns_; ++column) {
607 if (cell_info[row][column].multicolumn
608 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
610 if (numberofcells == 0)
611 // FIXME: Is this intended?
612 cell_info[row][column].cellno = npos;
614 cell_info[row][column].cellno =
619 rowofcell.resize(numberofcells);
620 columnofcell.resize(numberofcells);
625 c < numberofcells && row < rows_ && column < columns_;) {
627 columnofcell[c] = column;
631 } while (column < columns_ &&
632 cell_info[row][column].multicolumn
633 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
635 if (column == columns_) {
641 for (row_type row = 0; row < rows_; ++row) {
642 for (col_type column = 0; column < columns_; ++column) {
643 if (isPartOfMultiColumn(row,column))
645 cell_info[row][column].inset->setAutoBreakRows(
646 !getPWidth(getCellNumber(row, column)).zero());
652 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
654 return numberofcells;
658 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
660 row_type const row = row_of_cell(cell);
662 for (col_type i = 0; i < columns_; ++i)
663 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
669 bool LyXTabular::topLine(idx_type const cell, bool const onlycolumn) const
671 if (!onlycolumn && isMultiColumn(cell) &&
672 !(use_booktabs && row_of_cell(cell) == 0))
673 return cellinfo_of_cell(cell).top_line;
674 return row_info[row_of_cell(cell)].top_line;
678 bool LyXTabular::bottomLine(idx_type const cell, bool onlycolumn) const
680 if (!onlycolumn && isMultiColumn(cell) &&
681 !(use_booktabs && isLastRow(cell)))
682 return cellinfo_of_cell(cell).bottom_line;
683 return row_info[row_of_cell(cell)].bottom_line;
687 bool LyXTabular::leftLine(idx_type cell, bool onlycolumn) const
691 if (!onlycolumn && isMultiColumn(cell) &&
692 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
694 if (cellinfo_of_cell(cell).align_special.empty())
695 return cellinfo_of_cell(cell).left_line;
696 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
698 if (column_info[column_of_cell(cell)].align_special.empty())
699 return column_info[column_of_cell(cell)].left_line;
700 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
704 bool LyXTabular::rightLine(idx_type cell, bool onlycolumn) const
708 if (!onlycolumn && isMultiColumn(cell) &&
709 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
711 if (cellinfo_of_cell(cell).align_special.empty())
712 return cellinfo_of_cell(cell).right_line;
713 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
715 if (column_info[column_of_cell(cell)].align_special.empty())
716 return column_info[right_column_of_cell(cell)].right_line;
717 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
721 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
723 row_type row = row_of_cell(cell);
724 if (row > 0 && !getAdditionalHeight(row)) {
725 col_type column = column_of_cell(cell);
728 && cell_info[row][column].multicolumn
729 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
731 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
732 return row_info[row].bottom_line;
734 return cell_info[row][column].bottom_line;
740 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
742 col_type column = column_of_cell(cell);
744 row_type row = row_of_cell(cell);
746 (cell_info[row][column].multicolumn ==
747 LyXTabular::CELL_PART_OF_MULTICOLUMN));
748 if (getAdditionalWidth(cell_info[row][column].cellno))
750 return rightLine(cell_info[row][column].cellno);
756 bool LyXTabular::isLastRow(idx_type cell) const
758 return row_of_cell(cell) == rows_ - 1;
762 int LyXTabular::getAdditionalHeight(row_type row) const
764 if (!row || row >= rows_)
770 for (col_type column = 0; column < columns_ && bottom; ++column) {
771 switch (cell_info[row - 1][column].multicolumn) {
772 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
773 bottom = cell_info[row - 1][column].bottom_line;
775 case LyXTabular::CELL_NORMAL:
776 bottom = row_info[row - 1].bottom_line;
779 for (col_type column = 0; column < columns_ && top; ++column) {
780 switch (cell_info[row][column].multicolumn) {
781 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
782 top = cell_info[row][column].top_line;
784 case LyXTabular::CELL_NORMAL:
785 top = row_info[row].top_line;
788 int const interline_space = row_info[row - 1].interline_space_default ?
790 row_info[row - 1].interline_space.inPixels(width_of_tabular);
792 return interline_space + WIDTH_OF_LINE;
793 return interline_space;
797 int LyXTabular::getAdditionalWidth(idx_type cell) const
799 // internally already set in setWidthOfCell
800 // used to get it back in text.C
801 col_type const col = right_column_of_cell(cell);
802 row_type const row = row_of_cell(cell);
803 if (col < columns_ - 1 && rightLine(cell) &&
804 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
806 return WIDTH_OF_LINE;
812 // returns the maximum over all rows
813 int LyXTabular::getWidthOfColumn(idx_type cell) const
815 col_type const column1 = column_of_cell(cell);
816 col_type const column2 = right_column_of_cell(cell);
818 for (col_type i = column1; i <= column2; ++i)
819 result += column_info[i].width_of_column;
824 int LyXTabular::getWidthOfTabular() const
826 return width_of_tabular;
830 // returns true if a complete update is necessary, otherwise false
831 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
833 if (!isMultiColumn(cell))
836 row_type const row = row_of_cell(cell);
837 col_type const column1 = column_of_cell(cell);
838 col_type const column2 = right_column_of_cell(cell);
839 int const old_val = cell_info[row][column2].width_of_cell;
841 // first set columns to 0 so we can calculate the right width
842 for (col_type i = column1; i <= column2; ++i) {
843 cell_info[row][i].width_of_cell = 0;
845 // set the width to MAX_WIDTH until width > 0
846 int width = new_width + 2 * WIDTH_OF_LINE;
847 col_type i = column1;
848 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
849 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
850 width -= column_info[i].width_of_column;
853 cell_info[row][i].width_of_cell = width;
855 if (old_val != cell_info[row][column2].width_of_cell) {
856 // in this case we have to recalculate all multicolumn cells which
857 // have this column as one of theirs but not as last one
858 calculate_width_of_column_NMC(i);
859 recalculateMulticolumnsOfColumn(i);
860 calculate_width_of_column(i);
866 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
868 // the last column does not have to be recalculated because all
869 // multicolumns will have here there last multicolumn cell which
870 // always will have the whole rest of the width of the cell.
871 if (columns_ < 2 || column > (columns_ - 2))
873 for (row_type row = 0; row < rows_; ++row) {
874 int mc = cell_info[row][column].multicolumn;
875 int nmc = cell_info[row][column+1].multicolumn;
876 // we only have to update multicolumns which do not have this
877 // column as their last column!
878 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
879 (mc == CELL_PART_OF_MULTICOLUMN &&
880 nmc == CELL_PART_OF_MULTICOLUMN))
882 idx_type const cellno = cell_info[row][column].cellno;
883 setWidthOfMulticolCell(cellno,
884 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
890 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
892 row_type const row = row_of_cell(cell);
893 col_type const column1 = column_of_cell(cell);
898 if (rightLine(cell_info[row][column1].cellno, true) &&
899 column1 < columns_ - 1 &&
900 leftLine(cell_info[row][column1+1].cellno, true))
902 add_width = WIDTH_OF_LINE;
905 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
908 if (isMultiColumnReal(cell)) {
909 tmp = setWidthOfMulticolCell(cell, new_width);
911 width = new_width + 2 * WIDTH_OF_LINE + add_width;
912 cell_info[row][column1].width_of_cell = width;
913 tmp = calculate_width_of_column_NMC(column1);
915 recalculateMulticolumnsOfColumn(column1);
918 for (col_type i = 0; i < columns_; ++i)
919 calculate_width_of_column(i);
920 calculate_width_of_tabular();
925 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
928 if (!isMultiColumn(cell) || onlycolumn)
929 column_info[column_of_cell(cell)].alignment = align;
931 cellinfo_of_cell(cell).alignment = align;
935 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
938 if (!isMultiColumn(cell) || onlycolumn)
939 column_info[column_of_cell(cell)].valignment = align;
941 cellinfo_of_cell(cell).valignment = align;
948 * Allow line and paragraph breaks for fixed width cells or disallow them,
949 * merge cell paragraphs and reset layout to standard for variable width
952 void toggleFixedWidth(LCursor & cur, InsetText * inset, bool fixedWidth)
954 inset->setAutoBreakRows(fixedWidth);
958 // merge all paragraphs to one
959 BufferParams const & bp = cur.bv().buffer()->params();
960 while (inset->paragraphs().size() > 1)
961 mergeParagraph(bp, inset->paragraphs(), 0);
965 // undo information has already been recorded
966 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
967 bp.getLyXTextClass().defaultLayoutName());
974 void LyXTabular::setColumnPWidth(LCursor & cur, idx_type cell,
975 LyXLength const & width)
977 col_type const j = column_of_cell(cell);
979 column_info[j].p_width = width;
980 for (row_type i = 0; i < rows_; ++i) {
981 idx_type const cell = getCellNumber(i, j);
982 // because of multicolumns
983 toggleFixedWidth(cur, getCellInset(cell).get(),
984 !getPWidth(cell).zero());
986 // cur paragraph can become invalid after paragraphs were merged
987 if (cur.pit() > cur.lastpit())
988 cur.pit() = cur.lastpit();
989 // cur position can become invalid after newlines were removed
990 if (cur.pos() > cur.lastpos())
991 cur.pos() = cur.lastpos();
995 bool LyXTabular::setMColumnPWidth(LCursor & cur, idx_type cell,
996 LyXLength const & width)
998 if (!isMultiColumn(cell))
1001 cellinfo_of_cell(cell).p_width = width;
1002 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1003 // cur paragraph can become invalid after paragraphs were merged
1004 if (cur.pit() > cur.lastpit())
1005 cur.pit() = cur.lastpit();
1006 // cur position can become invalid after newlines were removed
1007 if (cur.pos() > cur.lastpos())
1008 cur.pos() = cur.lastpos();
1013 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
1014 LyXTabular::Feature what)
1016 if (what == SET_SPECIAL_MULTI)
1017 cellinfo_of_cell(cell).align_special = special;
1019 column_info[column_of_cell(cell)].align_special = special;
1023 void LyXTabular::setAllLines(idx_type cell, bool line)
1025 setTopLine(cell, line);
1026 setBottomLine(cell, line);
1027 setRightLine(cell, line);
1028 setLeftLine(cell, line);
1032 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
1034 row_type const row = row_of_cell(cell);
1035 if (onlycolumn || !isMultiColumn(cell))
1036 row_info[row].top_line = line;
1038 cellinfo_of_cell(cell).top_line = line;
1042 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
1044 if (onlycolumn || !isMultiColumn(cell))
1045 row_info[row_of_cell(cell)].bottom_line = line;
1047 cellinfo_of_cell(cell).bottom_line = line;
1051 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
1053 if (onlycolumn || !isMultiColumn(cell))
1054 column_info[column_of_cell(cell)].left_line = line;
1056 cellinfo_of_cell(cell).left_line = line;
1060 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
1062 if (onlycolumn || !isMultiColumn(cell))
1063 column_info[right_column_of_cell(cell)].right_line = line;
1065 cellinfo_of_cell(cell).right_line = line;
1069 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
1071 if (!onlycolumn && isMultiColumn(cell))
1072 return cellinfo_of_cell(cell).alignment;
1073 return column_info[column_of_cell(cell)].alignment;
1077 LyXTabular::VAlignment
1078 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
1080 if (!onlycolumn && isMultiColumn(cell))
1081 return cellinfo_of_cell(cell).valignment;
1082 return column_info[column_of_cell(cell)].valignment;
1086 LyXLength const LyXTabular::getPWidth(idx_type cell) const
1088 if (isMultiColumn(cell))
1089 return cellinfo_of_cell(cell).p_width;
1090 return column_info[column_of_cell(cell)].p_width;
1094 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
1096 return column_info[column_of_cell(cell)].p_width;
1100 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
1102 if (isMultiColumn(cell))
1103 return cellinfo_of_cell(cell).p_width;
1108 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
1110 if (what == SET_SPECIAL_MULTI)
1111 return cellinfo_of_cell(cell).align_special;
1112 return column_info[column_of_cell(cell)].align_special;
1116 int LyXTabular::getWidthOfCell(idx_type cell) const
1118 row_type const row = row_of_cell(cell);
1119 col_type const column1 = column_of_cell(cell);
1120 col_type const column2 = right_column_of_cell(cell);
1122 for (col_type i = column1; i <= column2; ++i)
1123 result += cell_info[row][i].width_of_cell;
1128 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1132 switch (getAlignment(cell)) {
1133 case LYX_ALIGN_CENTER:
1134 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1136 case LYX_ALIGN_RIGHT:
1137 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1138 // + getAdditionalWidth(cell);
1141 // LYX_ALIGN_LEFT: nothing :-)
1145 // the LaTeX Way :-(
1151 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1153 return column_of_cell(cell) == 0;
1157 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1159 if (row > rows_ - 1)
1161 return cell_info[row][0].cellno;
1165 bool LyXTabular::isLastCellInRow(idx_type cell) const
1167 return right_column_of_cell(cell) == columns_ - 1;
1171 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1173 if (row > rows_ - 1)
1175 return cell_info[row][columns_-1].cellno;
1179 void LyXTabular::calculate_width_of_column(col_type column)
1182 for (row_type i = 0; i < rows_; ++i)
1183 maximum = max(cell_info[i][column].width_of_cell, maximum);
1184 column_info[column].width_of_column = maximum;
1189 // Calculate the columns regarding ONLY the normal cells and if this
1190 // column is inside a multicolumn cell then use it only if its the last
1191 // column of this multicolumn cell as this gives an added width to the
1192 // column, all the rest should be adapted!
1194 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1196 int const old_column_width = column_info[column].width_of_column;
1198 for (row_type i = 0; i < rows_; ++i) {
1199 idx_type cell = getCellNumber(i, column);
1200 bool ismulti = isMultiColumnReal(cell);
1201 if ((!ismulti || column == right_column_of_cell(cell)) &&
1202 cell_info[i][column].width_of_cell > max)
1204 max = cell_info[i][column].width_of_cell;
1207 column_info[column].width_of_column = max;
1208 return column_info[column].width_of_column != old_column_width;
1212 void LyXTabular::calculate_width_of_tabular()
1214 width_of_tabular = 0;
1215 for (col_type i = 0; i < columns_; ++i)
1216 width_of_tabular += column_info[i].width_of_column;
1220 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1222 if (cell >= numberofcells)
1226 return rowofcell[cell];
1230 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1232 if (cell >= numberofcells)
1233 return columns_ - 1;
1236 return columnofcell[cell];
1240 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1242 row_type const row = row_of_cell(cell);
1243 col_type column = column_of_cell(cell);
1244 while (column < columns_ - 1 &&
1245 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1251 void LyXTabular::write(Buffer const & buf, ostream & os) const
1255 << write_attribute("version", 3)
1256 << write_attribute("rows", rows_)
1257 << write_attribute("columns", columns_)
1259 // global longtable options
1261 << write_attribute("rotate", rotate)
1262 << write_attribute("booktabs", use_booktabs)
1263 << write_attribute("islongtable", is_long_tabular)
1264 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1265 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1266 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1267 << write_attribute("headTopDL", endhead.topDL)
1268 << write_attribute("headBottomDL", endhead.bottomDL)
1269 << write_attribute("footTopDL", endfoot.topDL)
1270 << write_attribute("footBottomDL", endfoot.bottomDL)
1271 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1272 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1273 << write_attribute("lastFootEmpty", endlastfoot.empty)
1275 for (col_type j = 0; j < columns_; ++j) {
1277 << write_attribute("alignment", column_info[j].alignment)
1278 << write_attribute("valignment", column_info[j].valignment)
1279 << write_attribute("leftline", column_info[j].left_line)
1280 << write_attribute("rightline", column_info[j].right_line)
1281 << write_attribute("width", column_info[j].p_width.asString())
1282 << write_attribute("special", column_info[j].align_special)
1285 for (row_type i = 0; i < rows_; ++i) {
1287 << write_attribute("topline", row_info[i].top_line)
1288 << write_attribute("bottomline", row_info[i].bottom_line);
1289 static const string def("default");
1290 if (row_info[i].top_space_default)
1291 os << write_attribute("topspace", def);
1293 os << write_attribute("topspace", row_info[i].top_space);
1294 if (row_info[i].bottom_space_default)
1295 os << write_attribute("bottomspace", def);
1297 os << write_attribute("bottomspace", row_info[i].bottom_space);
1298 if (row_info[i].interline_space_default)
1299 os << write_attribute("interlinespace", def);
1301 os << write_attribute("interlinespace", row_info[i].interline_space);
1302 os << write_attribute("endhead", row_info[i].endhead)
1303 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1304 << write_attribute("endfoot", row_info[i].endfoot)
1305 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1306 << write_attribute("newpage", row_info[i].newpage)
1308 for (col_type j = 0; j < columns_; ++j) {
1310 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1311 << write_attribute("alignment", cell_info[i][j].alignment)
1312 << write_attribute("valignment", cell_info[i][j].valignment)
1313 << write_attribute("topline", cell_info[i][j].top_line)
1314 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1315 << write_attribute("leftline", cell_info[i][j].left_line)
1316 << write_attribute("rightline", cell_info[i][j].right_line)
1317 << write_attribute("rotate", cell_info[i][j].rotate)
1318 << write_attribute("usebox", cell_info[i][j].usebox)
1319 << write_attribute("width", cell_info[i][j].p_width)
1320 << write_attribute("special", cell_info[i][j].align_special)
1322 os << "\\begin_inset ";
1323 cell_info[i][j].inset->write(buf, os);
1324 os << "\n\\end_inset\n"
1329 os << "</lyxtabular>\n";
1333 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1336 istream & is = lex.getStream();
1338 l_getline(is, line);
1339 if (!prefixIs(line, "<lyxtabular ")
1340 && !prefixIs(line, "<LyXTabular ")) {
1341 BOOST_ASSERT(false);
1346 if (!getTokenValue(line, "version", version))
1348 BOOST_ASSERT(version >= 2);
1351 if (!getTokenValue(line, "rows", rows_arg))
1354 if (!getTokenValue(line, "columns", columns_arg))
1356 init(buf.params(), rows_arg, columns_arg);
1357 l_getline(is, line);
1358 if (!prefixIs(line, "<features")) {
1359 lyxerr << "Wrong tabular format (expected <features ...> got"
1360 << line << ')' << endl;
1363 getTokenValue(line, "rotate", rotate);
1364 getTokenValue(line, "booktabs", use_booktabs);
1365 getTokenValue(line, "islongtable", is_long_tabular);
1366 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1367 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1368 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1369 getTokenValue(line, "headTopDL", endhead.topDL);
1370 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1371 getTokenValue(line, "footTopDL", endfoot.topDL);
1372 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1373 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1374 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1375 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1377 for (col_type j = 0; j < columns_; ++j) {
1379 if (!prefixIs(line,"<column")) {
1380 lyxerr << "Wrong tabular format (expected <column ...> got"
1381 << line << ')' << endl;
1384 getTokenValue(line, "alignment", column_info[j].alignment);
1385 getTokenValue(line, "valignment", column_info[j].valignment);
1386 getTokenValue(line, "leftline", column_info[j].left_line);
1387 getTokenValue(line, "rightline", column_info[j].right_line);
1388 getTokenValue(line, "width", column_info[j].p_width);
1389 getTokenValue(line, "special", column_info[j].align_special);
1392 for (row_type i = 0; i < rows_; ++i) {
1393 l_getline(is, line);
1394 if (!prefixIs(line, "<row")) {
1395 lyxerr << "Wrong tabular format (expected <row ...> got"
1396 << line << ')' << endl;
1399 getTokenValue(line, "topline", row_info[i].top_line);
1400 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1401 getTokenValue(line, "topspace", row_info[i].top_space,
1402 row_info[i].top_space_default);
1403 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1404 row_info[i].bottom_space_default);
1405 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1406 row_info[i].interline_space_default);
1407 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1408 getTokenValue(line, "endhead", row_info[i].endhead);
1409 getTokenValue(line, "endfoot", row_info[i].endfoot);
1410 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1411 getTokenValue(line, "newpage", row_info[i].newpage);
1412 for (col_type j = 0; j < columns_; ++j) {
1413 l_getline(is, line);
1414 if (!prefixIs(line, "<cell")) {
1415 lyxerr << "Wrong tabular format (expected <cell ...> got"
1416 << line << ')' << endl;
1419 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1420 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1421 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1422 getTokenValue(line, "topline", cell_info[i][j].top_line);
1423 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1424 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1425 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1426 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1427 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1428 getTokenValue(line, "width", cell_info[i][j].p_width);
1429 getTokenValue(line, "special", cell_info[i][j].align_special);
1430 l_getline(is, line);
1431 if (prefixIs(line, "\\begin_inset")) {
1432 cell_info[i][j].inset->read(buf, lex);
1433 l_getline(is, line);
1435 if (!prefixIs(line, "</cell>")) {
1436 lyxerr << "Wrong tabular format (expected </cell> got"
1437 << line << ')' << endl;
1441 l_getline(is, line);
1442 if (!prefixIs(line, "</row>")) {
1443 lyxerr << "Wrong tabular format (expected </row> got"
1444 << line << ')' << endl;
1448 while (!prefixIs(line, "</lyxtabular>")) {
1449 l_getline(is, line);
1451 set_row_column_number_info();
1455 bool LyXTabular::isMultiColumn(idx_type cell) const
1457 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1461 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1463 return column_of_cell(cell) != right_column_of_cell(cell) &&
1464 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1468 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1470 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1474 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1477 cellstruct & cs = cellinfo_of_cell(cell);
1478 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1479 cs.alignment = column_info[column_of_cell(cell)].alignment;
1480 cs.top_line = row_info[row_of_cell(cell)].top_line;
1481 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1482 cs.left_line = column_info[column_of_cell(cell)].left_line;
1483 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1484 for (idx_type i = 1; i < number; ++i) {
1485 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1486 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1487 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1490 set_row_column_number_info();
1494 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1496 row_type const row = row_of_cell(cell);
1497 col_type column = column_of_cell(cell);
1498 idx_type result = 1;
1500 while (column < columns_ &&
1501 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1510 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1512 row_type const row = row_of_cell(cell);
1513 col_type column = column_of_cell(cell);
1515 idx_type result = 0;
1517 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1518 cell_info[row][column].multicolumn = CELL_NORMAL;
1520 while (column < columns_ &&
1521 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1523 cell_info[row][column].multicolumn = CELL_NORMAL;
1528 set_row_column_number_info();
1533 void LyXTabular::setBookTabs(bool what)
1535 use_booktabs = what;
1539 bool LyXTabular::useBookTabs() const
1541 return use_booktabs;
1545 void LyXTabular::setLongTabular(bool what)
1547 is_long_tabular = what;
1551 bool LyXTabular::isLongTabular() const
1553 return is_long_tabular;
1557 void LyXTabular::setRotateTabular(bool flag)
1563 bool LyXTabular::getRotateTabular() const
1569 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1571 cellinfo_of_cell(cell).rotate = flag;
1575 bool LyXTabular::getRotateCell(idx_type cell) const
1577 return cellinfo_of_cell(cell).rotate;
1581 bool LyXTabular::needRotating() const
1585 for (row_type i = 0; i < rows_; ++i)
1586 for (col_type j = 0; j < columns_; ++j)
1587 if (cell_info[i][j].rotate)
1593 bool LyXTabular::isLastCell(idx_type cell) const
1595 if (cell + 1 < numberofcells)
1601 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1603 if (row_of_cell(cell) > 0)
1604 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1609 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1611 if (row_of_cell(cell) + 1 < rows_)
1612 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1617 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1619 if (row_of_cell(cell) == 0)
1621 if (!isMultiColumn(cell))
1622 return getCellAbove(cell);
1623 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1627 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1629 if (row_of_cell(cell) + 1 >= rows_)
1631 if (!isMultiColumn(cell))
1632 return getCellBelow(cell);
1633 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1637 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1638 col_type column) const
1640 BOOST_ASSERT(column != npos && column < columns_ &&
1641 row != npos && row < rows_);
1642 return cell_info[row][column].cellno;
1646 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1648 cellinfo_of_cell(cell).usebox = type;
1652 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1654 if (column_info[column_of_cell(cell)].p_width.zero() &&
1655 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1657 if (cellinfo_of_cell(cell).usebox > 1)
1658 return cellinfo_of_cell(cell).usebox;
1659 return useParbox(cell);
1664 // This are functions used for the longtable support
1666 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1672 row_info[row].endfirsthead = flag;
1676 row_info[row].endhead = flag;
1681 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1684 hd.set = haveLTHead();
1685 return row_info[row].endhead;
1689 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1692 hd.set = haveLTFirstHead();
1693 return row_info[row].endfirsthead;
1697 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1703 row_info[row].endlastfoot = flag;
1707 row_info[row].endfoot = flag;
1712 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1715 fd.set = haveLTFoot();
1716 return row_info[row].endfoot;
1720 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1723 fd.set = haveLTLastFoot();
1724 return row_info[row].endlastfoot;
1728 void LyXTabular::setLTNewPage(row_type row, bool what)
1730 row_info[row].newpage = what;
1734 bool LyXTabular::getLTNewPage(row_type row) const
1736 return row_info[row].newpage;
1740 bool LyXTabular::haveLTHead() const
1742 for (row_type i = 0; i < rows_; ++i)
1743 if (row_info[i].endhead)
1749 bool LyXTabular::haveLTFirstHead() const
1751 if (endfirsthead.empty)
1753 for (row_type i = 0; i < rows_; ++i)
1754 if (row_info[i].endfirsthead)
1760 bool LyXTabular::haveLTFoot() const
1762 for (row_type i = 0; i < rows_; ++i)
1763 if (row_info[i].endfoot)
1769 bool LyXTabular::haveLTLastFoot() const
1771 if (endlastfoot.empty)
1773 for (row_type i = 0; i < rows_; ++i)
1774 if (row_info[i].endlastfoot)
1780 // end longtable support functions
1782 void LyXTabular::setAscentOfRow(row_type row, int height)
1784 if (row >= rows_ || row_info[row].ascent_of_row == height)
1786 row_info[row].ascent_of_row = height;
1790 void LyXTabular::setDescentOfRow(row_type row, int height)
1792 if (row >= rows_ || row_info[row].descent_of_row == height)
1794 row_info[row].descent_of_row = height;
1798 int LyXTabular::getAscentOfRow(row_type row) const
1802 return row_info[row].ascent_of_row;
1806 int LyXTabular::getDescentOfRow(row_type row) const
1808 BOOST_ASSERT(row < rows_);
1809 return row_info[row].descent_of_row;
1813 int LyXTabular::getHeightOfTabular() const
1816 for (row_type row = 0; row < rows_; ++row)
1817 height += getAscentOfRow(row) + getDescentOfRow(row) +
1818 getAdditionalHeight(row);
1823 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1825 BOOST_ASSERT(row < rows_);
1826 BOOST_ASSERT(column < columns_);
1827 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1831 int LyXTabular::TeXTopHLine(odocstream & os, row_type row) const
1833 // FIXME: assert or return 0 as in TeXBottomHLine()?
1834 BOOST_ASSERT(row != npos);
1835 BOOST_ASSERT(row < rows_);
1837 idx_type const fcell = getFirstCellInRow(row);
1838 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1841 for (idx_type i = fcell; i < n; ++i) {
1845 if (use_booktabs && row == 0) {
1847 } else if (tmp == n - fcell) {
1848 os << (use_booktabs ? "\\midrule " : "\\hline ");
1850 for (idx_type i = fcell; i < n; ++i) {
1852 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1853 << column_of_cell(i) + 1
1855 << right_column_of_cell(i) + 1
1867 int LyXTabular::TeXBottomHLine(odocstream & os, row_type row) const
1869 // FIXME: return 0 or assert as in TeXTopHLine()?
1870 if (row == npos || row >= rows_)
1873 idx_type const fcell = getFirstCellInRow(row);
1874 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1877 for (idx_type i = fcell; i < n; ++i) {
1881 if (use_booktabs && row == rows_ - 1) {
1882 os << "\\bottomrule";
1883 } else if (tmp == n - fcell) {
1884 os << (use_booktabs ? "\\midrule" : "\\hline");
1886 for (idx_type i = fcell; i < n; ++i) {
1887 if (bottomLine(i)) {
1888 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1889 << column_of_cell(i) + 1
1891 << right_column_of_cell(i) + 1
1903 int LyXTabular::TeXCellPreamble(odocstream & os, idx_type cell) const
1907 if (getRotateCell(cell)) {
1908 os << "\\begin{sideways}\n";
1911 if (isMultiColumn(cell)) {
1912 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1913 if (!cellinfo_of_cell(cell).align_special.empty()) {
1914 os << from_ascii(cellinfo_of_cell(cell).align_special)
1917 if (leftLine(cell) &&
1918 (isFirstCellInRow(cell) ||
1919 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1920 !rightLine(cell - 1, true))))
1924 if (!getPWidth(cell).zero()) {
1925 switch (getVAlignment(cell)) {
1926 case LYX_VALIGN_TOP:
1929 case LYX_VALIGN_MIDDLE:
1932 case LYX_VALIGN_BOTTOM:
1937 << from_ascii(getPWidth(cell).asLatexString())
1940 switch (getAlignment(cell)) {
1941 case LYX_ALIGN_LEFT:
1944 case LYX_ALIGN_RIGHT:
1952 if (rightLine(cell))
1954 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1960 if (getUsebox(cell) == BOX_PARBOX) {
1962 switch (getVAlignment(cell)) {
1963 case LYX_VALIGN_TOP:
1966 case LYX_VALIGN_MIDDLE:
1969 case LYX_VALIGN_BOTTOM:
1973 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1975 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1976 os << "\\begin{minipage}[";
1977 switch (getVAlignment(cell)) {
1978 case LYX_VALIGN_TOP:
1981 case LYX_VALIGN_MIDDLE:
1984 case LYX_VALIGN_BOTTOM:
1988 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1996 int LyXTabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2001 if (getUsebox(cell) == BOX_PARBOX)
2003 else if (getUsebox(cell) == BOX_MINIPAGE) {
2004 os << "%\n\\end{minipage}";
2007 if (isMultiColumn(cell)) {
2010 if (getRotateCell(cell)) {
2011 os << "%\n\\end{sideways}";
2018 int LyXTabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2019 OutputParams const & runparams) const
2021 if (!is_long_tabular)
2025 // output header info
2027 if (endhead.topDL) {
2031 for (row_type i = 0; i < rows_; ++i) {
2032 if (row_info[i].endhead) {
2033 ret += TeXRow(os, i, buf, runparams);
2036 if (endhead.bottomDL) {
2040 os << "\\endhead\n";
2042 if (endfirsthead.empty) {
2043 os << "\\endfirsthead\n";
2047 // output firstheader info
2048 if (haveLTFirstHead()) {
2049 if (endfirsthead.topDL) {
2053 for (row_type i = 0; i < rows_; ++i) {
2054 if (row_info[i].endfirsthead) {
2055 ret += TeXRow(os, i, buf, runparams);
2058 if (endfirsthead.bottomDL) {
2062 os << "\\endfirsthead\n";
2065 // output footer info
2067 if (endfoot.topDL) {
2071 for (row_type i = 0; i < rows_; ++i) {
2072 if (row_info[i].endfoot) {
2073 ret += TeXRow(os, i, buf, runparams);
2076 if (endfoot.bottomDL) {
2080 os << "\\endfoot\n";
2082 if (endlastfoot.empty) {
2083 os << "\\endlastfoot\n";
2087 // output lastfooter info
2088 if (haveLTLastFoot()) {
2089 if (endlastfoot.topDL) {
2093 for (row_type i = 0; i < rows_; ++i) {
2094 if (row_info[i].endlastfoot) {
2095 ret += TeXRow(os, i, buf, runparams);
2098 if (endlastfoot.bottomDL) {
2102 os << "\\endlastfoot\n";
2109 bool LyXTabular::isValidRow(row_type row) const
2111 if (!is_long_tabular)
2113 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2114 !row_info[row].endfoot && !row_info[row].endlastfoot;
2118 int LyXTabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2119 OutputParams const & runparams) const
2121 idx_type cell = getCellNumber(i, 0);
2122 int ret = TeXTopHLine(os, i);
2123 if (row_info[i].top_space_default) {
2125 os << "\\addlinespace\n";
2127 os << "\\noalign{\\vskip\\doublerulesep}\n";
2128 } else if(!row_info[i].top_space.zero()) {
2130 os << "\\addlinespace["
2131 << from_ascii(row_info[i].top_space.asLatexString())
2134 os << "\\noalign{\\vskip"
2135 << from_ascii(row_info[i].top_space.asLatexString())
2141 for (col_type j = 0; j < columns_; ++j) {
2142 if (isPartOfMultiColumn(i, j))
2144 ret += TeXCellPreamble(os, cell);
2145 shared_ptr<InsetText> inset = getCellInset(cell);
2147 Paragraph const & par = inset->paragraphs().front();
2148 bool rtl = par.isRightToLeftPar(buf.params())
2150 && getPWidth(cell).zero();
2154 ret += inset->latex(buf, os, runparams);
2158 ret += TeXCellPostamble(os, cell);
2159 if (!isLastCellInRow(cell)) { // not last cell in row
2164 os << "\\tabularnewline";
2165 if (row_info[i].bottom_space_default) {
2167 os << "\\addlinespace";
2169 os << "[\\doublerulesep]";
2170 } else if (!row_info[i].bottom_space.zero()) {
2172 os << "\\addlinespace";
2174 << from_ascii(row_info[i].bottom_space.asLatexString())
2179 ret += TeXBottomHLine(os, i);
2180 if (row_info[i].interline_space_default) {
2182 os << "\\addlinespace\n";
2184 os << "\\noalign{\\vskip\\doublerulesep}\n";
2185 } else if (!row_info[i].interline_space.zero()) {
2187 os << "\\addlinespace["
2188 << from_ascii(row_info[i].interline_space.asLatexString())
2191 os << "\\noalign{\\vskip"
2192 << from_ascii(row_info[i].interline_space.asLatexString())
2200 int LyXTabular::latex(Buffer const & buf, odocstream & os,
2201 OutputParams const & runparams) const
2205 //+---------------------------------------------------------------------
2206 //+ first the opening preamble +
2207 //+---------------------------------------------------------------------
2210 os << "\\begin{sideways}\n";
2213 if (is_long_tabular)
2214 os << "\\begin{longtable}{";
2216 os << "\\begin{tabular}{";
2217 for (col_type i = 0; i < columns_; ++i) {
2218 if (!column_info[i].align_special.empty()) {
2219 os << from_ascii(column_info[i].align_special);
2221 if (!use_booktabs && column_info[i].left_line)
2223 if (!column_info[i].p_width.zero()) {
2224 switch (column_info[i].alignment) {
2225 case LYX_ALIGN_LEFT:
2226 os << ">{\\raggedright}";
2228 case LYX_ALIGN_RIGHT:
2229 os << ">{\\raggedleft}";
2231 case LYX_ALIGN_CENTER:
2232 os << ">{\\centering}";
2234 case LYX_ALIGN_NONE:
2235 case LYX_ALIGN_BLOCK:
2236 case LYX_ALIGN_LAYOUT:
2237 case LYX_ALIGN_SPECIAL:
2241 switch (column_info[i].valignment) {
2242 case LYX_VALIGN_TOP:
2245 case LYX_VALIGN_MIDDLE:
2248 case LYX_VALIGN_BOTTOM:
2253 << from_ascii(column_info[i].p_width.asLatexString())
2256 switch (column_info[i].alignment) {
2257 case LYX_ALIGN_LEFT:
2260 case LYX_ALIGN_RIGHT:
2268 if (!use_booktabs && column_info[i].right_line)
2275 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2277 //+---------------------------------------------------------------------
2278 //+ the single row and columns (cells) +
2279 //+---------------------------------------------------------------------
2281 for (row_type i = 0; i < rows_; ++i) {
2282 if (isValidRow(i)) {
2283 ret += TeXRow(os, i, buf, runparams);
2284 if (is_long_tabular && row_info[i].newpage) {
2285 os << "\\newpage\n";
2291 //+---------------------------------------------------------------------
2292 //+ the closing of the tabular +
2293 //+---------------------------------------------------------------------
2295 if (is_long_tabular)
2296 os << "\\end{longtable}";
2298 os << "\\end{tabular}";
2300 os << "\n\\end{sideways}";
2308 int LyXTabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2309 OutputParams const & runparams) const
2312 idx_type cell = getFirstCellInRow(row);
2315 for (col_type j = 0; j < columns_; ++j) {
2316 if (isPartOfMultiColumn(row, j))
2319 os << "<entry align=\"";
2320 switch (getAlignment(cell)) {
2321 case LYX_ALIGN_LEFT:
2324 case LYX_ALIGN_RIGHT:
2332 os << "\" valign=\"";
2333 switch (getVAlignment(cell)) {
2334 case LYX_VALIGN_TOP:
2337 case LYX_VALIGN_BOTTOM:
2340 case LYX_VALIGN_MIDDLE:
2345 if (isMultiColumn(cell)) {
2346 os << " namest=\"col" << j << "\" ";
2347 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2351 ret += getCellInset(cell)->docbook(buf, os, runparams);
2360 int LyXTabular::docbook(Buffer const & buf, odocstream & os,
2361 OutputParams const & runparams) const
2365 //+---------------------------------------------------------------------
2366 //+ first the opening preamble +
2367 //+---------------------------------------------------------------------
2369 os << "<tgroup cols=\"" << columns_
2370 << "\" colsep=\"1\" rowsep=\"1\">\n";
2372 for (col_type i = 0; i < columns_; ++i) {
2373 os << "<colspec colname=\"col" << i << "\" align=\"";
2374 switch (column_info[i].alignment) {
2375 case LYX_ALIGN_LEFT:
2378 case LYX_ALIGN_RIGHT:
2386 if (runparams.flavor == OutputParams::XML)
2392 //+---------------------------------------------------------------------
2393 //+ Long Tabular case +
2394 //+---------------------------------------------------------------------
2396 // output header info
2397 if (haveLTHead() || haveLTFirstHead()) {
2400 for (row_type i = 0; i < rows_; ++i) {
2401 if (row_info[i].endhead || row_info[i].endfirsthead) {
2402 ret += docbookRow(buf, os, i, runparams);
2408 // output footer info
2409 if (haveLTFoot() || haveLTLastFoot()) {
2412 for (row_type i = 0; i < rows_; ++i) {
2413 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2414 ret += docbookRow(buf, os, i, runparams);
2421 //+---------------------------------------------------------------------
2422 //+ the single row and columns (cells) +
2423 //+---------------------------------------------------------------------
2427 for (row_type i = 0; i < rows_; ++i) {
2428 if (isValidRow(i)) {
2429 ret += docbookRow(buf, os, i, runparams);
2434 //+---------------------------------------------------------------------
2435 //+ the closing of the tabular +
2436 //+---------------------------------------------------------------------
2445 int LyXTabular::asciiTopHLine(odocstream & os, row_type row,
2446 vector<unsigned int> const & clen) const
2448 idx_type const fcell = getFirstCellInRow(row);
2449 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2452 for (idx_type i = fcell; i < n; ++i) {
2462 for (idx_type i = fcell; i < n; ++i) {
2473 col_type column = column_of_cell(i);
2474 int len = clen[column];
2475 while (column < columns_ - 1
2476 && isPartOfMultiColumn(row, ++column))
2477 len += clen[column] + 4;
2478 os << docstring(len, ch);
2493 int LyXTabular::asciiBottomHLine(odocstream & os, row_type row,
2494 vector<unsigned int> const & clen) const
2496 idx_type const fcell = getFirstCellInRow(row);
2497 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2500 for (idx_type i = fcell; i < n; ++i) {
2501 if (bottomLine(i)) {
2510 for (idx_type i = fcell; i < n; ++i) {
2511 if (bottomLine(i)) {
2521 col_type column = column_of_cell(i);
2522 int len = clen[column];
2523 while (column < columns_ -1
2524 && isPartOfMultiColumn(row, ++column))
2525 len += clen[column] + 4;
2526 os << docstring(len, ch);
2527 if (bottomLine(i)) {
2541 int LyXTabular::asciiPrintCell(Buffer const & buf, odocstream & os,
2542 OutputParams const & runparams,
2543 idx_type cell, row_type row, col_type column,
2544 vector<unsigned int> const & clen,
2545 bool onlydata) const
2547 odocstringstream sstr;
2548 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2560 unsigned int len1 = sstr.str().length();
2561 unsigned int len2 = clen[column];
2562 while (column < columns_ -1
2563 && isPartOfMultiColumn(row, ++column))
2564 len2 += clen[column] + 4;
2567 switch (getAlignment(cell)) {
2569 case LYX_ALIGN_LEFT:
2572 case LYX_ALIGN_RIGHT:
2576 case LYX_ALIGN_CENTER:
2582 os << docstring(len1, ' ') << sstr.str()
2583 << docstring(len2, ' ');
2585 if (rightLine(cell))
2594 int LyXTabular::plaintext(Buffer const & buf, odocstream & os,
2595 OutputParams const & runparams,
2597 bool onlydata, unsigned char delim) const
2601 // first calculate the width of the single columns
2602 vector<unsigned int> clen(columns_);
2605 // first all non (real) multicolumn cells!
2606 for (col_type j = 0; j < columns_; ++j) {
2608 for (row_type i = 0; i < rows_; ++i) {
2609 idx_type cell = getCellNumber(i, j);
2610 if (isMultiColumnReal(cell))
2612 odocstringstream sstr;
2613 getCellInset(cell)->plaintext(buf, sstr, runparams);
2614 if (clen[j] < sstr.str().length())
2615 clen[j] = sstr.str().length();
2618 // then all (real) multicolumn cells!
2619 for (col_type j = 0; j < columns_; ++j) {
2620 for (row_type i = 0; i < rows_; ++i) {
2621 idx_type cell = getCellNumber(i, j);
2622 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2624 odocstringstream sstr;
2625 getCellInset(cell)->plaintext(buf, sstr, runparams);
2626 int len = int(sstr.str().length());
2627 idx_type const n = cells_in_multicolumn(cell);
2628 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2630 if (len > int(clen[j + n - 1]))
2631 clen[j + n - 1] = len;
2636 for (row_type i = 0; i < rows_; ++i) {
2637 if (!onlydata && asciiTopHLine(os, i, clen))
2638 os << docstring(depth * 2, ' ');
2639 for (col_type j = 0; j < columns_; ++j) {
2640 if (isPartOfMultiColumn(i, j))
2642 if (onlydata && j > 0)
2644 ret += asciiPrintCell(buf, os, runparams,
2645 cell, i, j, clen, onlydata);
2650 os << docstring(depth * 2, ' ');
2651 if (asciiBottomHLine(os, i, clen))
2652 os << docstring(depth * 2, ' ');
2659 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2661 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2665 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2666 col_type column) const
2668 return cell_info[row][column].inset;
2672 void LyXTabular::setCellInset(row_type row, col_type column,
2673 shared_ptr<InsetText> ins) const
2675 cell_info[row][column].inset = ins;
2679 LyXTabular::idx_type
2680 LyXTabular::getCellFromInset(InsetBase const * inset) const
2682 // is this inset part of the tabular?
2684 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2685 BOOST_ASSERT(false);
2688 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2689 if (getCellInset(cell).get() == inset) {
2690 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2691 << "cell=" << cell << endl;
2695 // We should have found a cell at this point
2696 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2697 << inset << " not found!" << endl;
2698 BOOST_ASSERT(false);
2704 void LyXTabular::validate(LaTeXFeatures & features) const
2706 features.require("NeedTabularnewline");
2708 features.require("booktabs");
2709 if (isLongTabular())
2710 features.require("longtable");
2712 features.require("rotating");
2713 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2714 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2715 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2716 features.require("array");
2717 getCellInset(cell)->validate(features);
2722 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2724 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2725 ParagraphList::const_iterator cit = parlist.begin();
2726 ParagraphList::const_iterator end = parlist.end();
2728 for (; cit != end; ++cit)
2729 for (int i = 0; i < cit->size(); ++i)
2730 if (cit->isNewline(i))