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 for (col_type j = 0; j < columns_; ++j)
503 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
505 set_row_column_number_info();
509 void LyXTabular::deleteRow(row_type const row)
511 // Not allowed to delete last row
515 row_info.erase(row_info.begin() + row);
516 cell_info.erase(cell_info.begin() + row);
522 void LyXTabular::copyRow(BufferParams const & bp, row_type const row)
526 row_info.insert(row_info.begin() + row, row_info[row]);
527 cell_info.insert(cell_info.begin() + row, cell_info[row]);
530 for (col_type j = 0; j < columns_; ++j)
531 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
533 set_row_column_number_info();
537 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
541 col_type const column = column_of_cell(cell);
542 column_vector::iterator cit = column_info.begin() + column + 1;
543 column_info.insert(cit, columnstruct());
544 // set the column values of the column before
545 column_info[column + 1] = column_info[column];
547 for (row_type i = 0; i < rows_; ++i) {
548 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
550 // care about multicolumns
551 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
552 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
554 if (column + 2 >= columns_
555 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
556 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
559 for (row_type i = 0; i < rows_; ++i) {
560 cell_info[i][column + 1].inset->clear();
562 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
568 void LyXTabular::deleteColumn(col_type const column)
570 // Not allowed to delete last column
574 column_info.erase(column_info.begin() + column);
575 for (row_type i = 0; i < rows_; ++i)
576 cell_info[i].erase(cell_info[i].begin() + column);
582 void LyXTabular::copyColumn(BufferParams const & bp, col_type const column)
586 column_info.insert(column_info.begin() + column, column_info[column]);
588 for (row_type i = 0; i < rows_; ++i)
589 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
592 for (row_type i = 0; i < rows_; ++i)
593 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
598 void LyXTabular::set_row_column_number_info()
601 for (row_type row = 0; row < rows_; ++row) {
602 for (col_type column = 0; column < columns_; ++column) {
603 if (cell_info[row][column].multicolumn
604 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
606 if (numberofcells == 0)
607 // FIXME: Is this intended?
608 cell_info[row][column].cellno = npos;
610 cell_info[row][column].cellno =
615 rowofcell.resize(numberofcells);
616 columnofcell.resize(numberofcells);
621 c < numberofcells && row < rows_ && column < columns_;) {
623 columnofcell[c] = column;
627 } while (column < columns_ &&
628 cell_info[row][column].multicolumn
629 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
631 if (column == columns_) {
637 for (row_type row = 0; row < rows_; ++row) {
638 for (col_type column = 0; column < columns_; ++column) {
639 if (isPartOfMultiColumn(row,column))
641 cell_info[row][column].inset->setAutoBreakRows(
642 !getPWidth(getCellNumber(row, column)).zero());
648 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
650 return numberofcells;
654 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
656 row_type const row = row_of_cell(cell);
658 for (col_type i = 0; i < columns_; ++i)
659 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
665 bool LyXTabular::topLine(idx_type const cell, bool const wholerow) const
667 if (!wholerow && isMultiColumn(cell) &&
668 !(use_booktabs && row_of_cell(cell) == 0))
669 return cellinfo_of_cell(cell).top_line;
670 return row_info[row_of_cell(cell)].top_line;
674 bool LyXTabular::bottomLine(idx_type const cell, bool wholerow) const
676 if (!wholerow && isMultiColumn(cell) &&
677 !(use_booktabs && isLastRow(cell)))
678 return cellinfo_of_cell(cell).bottom_line;
679 return row_info[row_of_cell(cell)].bottom_line;
683 bool LyXTabular::leftLine(idx_type cell, bool wholecolumn) const
687 if (!wholecolumn && isMultiColumn(cell) &&
688 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
690 if (cellinfo_of_cell(cell).align_special.empty())
691 return cellinfo_of_cell(cell).left_line;
692 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
694 if (column_info[column_of_cell(cell)].align_special.empty())
695 return column_info[column_of_cell(cell)].left_line;
696 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
700 bool LyXTabular::rightLine(idx_type cell, bool wholecolumn) const
704 if (!wholecolumn && isMultiColumn(cell) &&
705 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
707 if (cellinfo_of_cell(cell).align_special.empty())
708 return cellinfo_of_cell(cell).right_line;
709 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
711 if (column_info[column_of_cell(cell)].align_special.empty())
712 return column_info[right_column_of_cell(cell)].right_line;
713 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
717 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
719 row_type row = row_of_cell(cell);
720 if (row > 0 && !getAdditionalHeight(row)) {
721 col_type column = column_of_cell(cell);
724 && cell_info[row][column].multicolumn
725 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
727 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
728 return row_info[row].bottom_line;
730 return cell_info[row][column].bottom_line;
736 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
738 col_type column = column_of_cell(cell);
740 row_type row = row_of_cell(cell);
742 (cell_info[row][column].multicolumn ==
743 LyXTabular::CELL_PART_OF_MULTICOLUMN));
744 if (getAdditionalWidth(cell_info[row][column].cellno))
746 return rightLine(cell_info[row][column].cellno);
752 bool LyXTabular::isLastRow(idx_type cell) const
754 return row_of_cell(cell) == rows_ - 1;
758 int LyXTabular::getAdditionalHeight(row_type row) const
760 if (!row || row >= rows_)
766 for (col_type column = 0; column < columns_ && bottom; ++column) {
767 switch (cell_info[row - 1][column].multicolumn) {
768 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
769 bottom = cell_info[row - 1][column].bottom_line;
771 case LyXTabular::CELL_NORMAL:
772 bottom = row_info[row - 1].bottom_line;
775 for (col_type column = 0; column < columns_ && top; ++column) {
776 switch (cell_info[row][column].multicolumn) {
777 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
778 top = cell_info[row][column].top_line;
780 case LyXTabular::CELL_NORMAL:
781 top = row_info[row].top_line;
784 int const interline_space = row_info[row - 1].interline_space_default ?
786 row_info[row - 1].interline_space.inPixels(width_of_tabular);
788 return interline_space + WIDTH_OF_LINE;
789 return interline_space;
793 int LyXTabular::getAdditionalWidth(idx_type cell) const
795 // internally already set in setWidthOfCell
796 // used to get it back in text.C
797 col_type const col = right_column_of_cell(cell);
798 row_type const row = row_of_cell(cell);
799 if (col < columns_ - 1 && rightLine(cell) &&
800 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
802 return WIDTH_OF_LINE;
808 // returns the maximum over all rows
809 int LyXTabular::getWidthOfColumn(idx_type cell) const
811 col_type const column1 = column_of_cell(cell);
812 col_type const column2 = right_column_of_cell(cell);
814 for (col_type i = column1; i <= column2; ++i)
815 result += column_info[i].width_of_column;
820 int LyXTabular::getWidthOfTabular() const
822 return width_of_tabular;
826 // returns true if a complete update is necessary, otherwise false
827 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
829 if (!isMultiColumn(cell))
832 row_type const row = row_of_cell(cell);
833 col_type const column1 = column_of_cell(cell);
834 col_type const column2 = right_column_of_cell(cell);
835 int const old_val = cell_info[row][column2].width_of_cell;
837 // first set columns to 0 so we can calculate the right width
838 for (col_type i = column1; i <= column2; ++i) {
839 cell_info[row][i].width_of_cell = 0;
841 // set the width to MAX_WIDTH until width > 0
842 int width = new_width + 2 * WIDTH_OF_LINE;
843 col_type i = column1;
844 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
845 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
846 width -= column_info[i].width_of_column;
849 cell_info[row][i].width_of_cell = width;
851 if (old_val != cell_info[row][column2].width_of_cell) {
852 // in this case we have to recalculate all multicolumn cells which
853 // have this column as one of theirs but not as last one
854 calculate_width_of_column_NMC(i);
855 recalculateMulticolumnsOfColumn(i);
856 calculate_width_of_column(i);
862 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
864 // the last column does not have to be recalculated because all
865 // multicolumns will have here there last multicolumn cell which
866 // always will have the whole rest of the width of the cell.
867 if (columns_ < 2 || column > (columns_ - 2))
869 for (row_type row = 0; row < rows_; ++row) {
870 int mc = cell_info[row][column].multicolumn;
871 int nmc = cell_info[row][column+1].multicolumn;
872 // we only have to update multicolumns which do not have this
873 // column as their last column!
874 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
875 (mc == CELL_PART_OF_MULTICOLUMN &&
876 nmc == CELL_PART_OF_MULTICOLUMN))
878 idx_type const cellno = cell_info[row][column].cellno;
879 setWidthOfMulticolCell(cellno,
880 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
886 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
888 row_type const row = row_of_cell(cell);
889 col_type const column1 = column_of_cell(cell);
894 if (rightLine(cell_info[row][column1].cellno, true) &&
895 column1 < columns_ - 1 &&
896 leftLine(cell_info[row][column1+1].cellno, true))
898 add_width = WIDTH_OF_LINE;
901 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
904 if (isMultiColumnReal(cell)) {
905 tmp = setWidthOfMulticolCell(cell, new_width);
907 width = new_width + 2 * WIDTH_OF_LINE + add_width;
908 cell_info[row][column1].width_of_cell = width;
909 tmp = calculate_width_of_column_NMC(column1);
911 recalculateMulticolumnsOfColumn(column1);
914 for (col_type i = 0; i < columns_; ++i)
915 calculate_width_of_column(i);
916 calculate_width_of_tabular();
921 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
924 if (!isMultiColumn(cell) || onlycolumn)
925 column_info[column_of_cell(cell)].alignment = align;
927 cellinfo_of_cell(cell).alignment = align;
931 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
934 if (!isMultiColumn(cell) || onlycolumn)
935 column_info[column_of_cell(cell)].valignment = align;
937 cellinfo_of_cell(cell).valignment = align;
944 * Allow line and paragraph breaks for fixed width cells or disallow them,
945 * merge cell paragraphs and reset layout to standard for variable width
948 void toggleFixedWidth(LCursor & cur, InsetText * inset, bool fixedWidth)
950 inset->setAutoBreakRows(fixedWidth);
954 // merge all paragraphs to one
955 BufferParams const & bp = cur.bv().buffer()->params();
956 while (inset->paragraphs().size() > 1)
957 mergeParagraph(bp, inset->paragraphs(), 0);
961 // undo information has already been recorded
962 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
963 bp.getLyXTextClass().defaultLayoutName());
970 void LyXTabular::setColumnPWidth(LCursor & cur, idx_type cell,
971 LyXLength const & width)
973 col_type const j = column_of_cell(cell);
975 column_info[j].p_width = width;
976 for (row_type i = 0; i < rows_; ++i) {
977 idx_type const cell = getCellNumber(i, j);
978 // because of multicolumns
979 toggleFixedWidth(cur, getCellInset(cell).get(),
980 !getPWidth(cell).zero());
982 // cur paragraph can become invalid after paragraphs were merged
983 if (cur.pit() > cur.lastpit())
984 cur.pit() = cur.lastpit();
985 // cur position can become invalid after newlines were removed
986 if (cur.pos() > cur.lastpos())
987 cur.pos() = cur.lastpos();
991 bool LyXTabular::setMColumnPWidth(LCursor & cur, idx_type cell,
992 LyXLength const & width)
994 if (!isMultiColumn(cell))
997 cellinfo_of_cell(cell).p_width = width;
998 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
999 // cur paragraph can become invalid after paragraphs were merged
1000 if (cur.pit() > cur.lastpit())
1001 cur.pit() = cur.lastpit();
1002 // cur position can become invalid after newlines were removed
1003 if (cur.pos() > cur.lastpos())
1004 cur.pos() = cur.lastpos();
1009 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
1010 LyXTabular::Feature what)
1012 if (what == SET_SPECIAL_MULTI)
1013 cellinfo_of_cell(cell).align_special = special;
1015 column_info[column_of_cell(cell)].align_special = special;
1019 void LyXTabular::setAllLines(idx_type cell, bool line)
1021 setTopLine(cell, line);
1022 setBottomLine(cell, line);
1023 setRightLine(cell, line);
1024 setLeftLine(cell, line);
1028 void LyXTabular::setTopLine(idx_type cell, bool line, bool wholerow)
1030 row_type const row = row_of_cell(cell);
1031 if (wholerow || !isMultiColumn(cell))
1032 row_info[row].top_line = line;
1034 cellinfo_of_cell(cell).top_line = line;
1038 void LyXTabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1040 if (wholerow || !isMultiColumn(cell))
1041 row_info[row_of_cell(cell)].bottom_line = line;
1043 cellinfo_of_cell(cell).bottom_line = line;
1047 void LyXTabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1049 if (wholecolumn || !isMultiColumn(cell))
1050 column_info[column_of_cell(cell)].left_line = line;
1052 cellinfo_of_cell(cell).left_line = line;
1056 void LyXTabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1058 if (wholecolumn || !isMultiColumn(cell))
1059 column_info[right_column_of_cell(cell)].right_line = line;
1061 cellinfo_of_cell(cell).right_line = line;
1065 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
1067 if (!onlycolumn && isMultiColumn(cell))
1068 return cellinfo_of_cell(cell).alignment;
1069 return column_info[column_of_cell(cell)].alignment;
1073 LyXTabular::VAlignment
1074 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
1076 if (!onlycolumn && isMultiColumn(cell))
1077 return cellinfo_of_cell(cell).valignment;
1078 return column_info[column_of_cell(cell)].valignment;
1082 LyXLength const LyXTabular::getPWidth(idx_type cell) const
1084 if (isMultiColumn(cell))
1085 return cellinfo_of_cell(cell).p_width;
1086 return column_info[column_of_cell(cell)].p_width;
1090 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
1092 return column_info[column_of_cell(cell)].p_width;
1096 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
1098 if (isMultiColumn(cell))
1099 return cellinfo_of_cell(cell).p_width;
1104 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
1106 if (what == SET_SPECIAL_MULTI)
1107 return cellinfo_of_cell(cell).align_special;
1108 return column_info[column_of_cell(cell)].align_special;
1112 int LyXTabular::getWidthOfCell(idx_type cell) const
1114 row_type const row = row_of_cell(cell);
1115 col_type const column1 = column_of_cell(cell);
1116 col_type const column2 = right_column_of_cell(cell);
1118 for (col_type i = column1; i <= column2; ++i)
1119 result += cell_info[row][i].width_of_cell;
1124 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1128 switch (getAlignment(cell)) {
1129 case LYX_ALIGN_CENTER:
1130 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1132 case LYX_ALIGN_RIGHT:
1133 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1134 // + getAdditionalWidth(cell);
1137 // LYX_ALIGN_LEFT: nothing :-)
1141 // the LaTeX Way :-(
1147 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1149 return column_of_cell(cell) == 0;
1153 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1155 if (row > rows_ - 1)
1157 return cell_info[row][0].cellno;
1161 bool LyXTabular::isLastCellInRow(idx_type cell) const
1163 return right_column_of_cell(cell) == columns_ - 1;
1167 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1169 if (row > rows_ - 1)
1171 return cell_info[row][columns_-1].cellno;
1175 void LyXTabular::calculate_width_of_column(col_type column)
1178 for (row_type i = 0; i < rows_; ++i)
1179 maximum = max(cell_info[i][column].width_of_cell, maximum);
1180 column_info[column].width_of_column = maximum;
1185 // Calculate the columns regarding ONLY the normal cells and if this
1186 // column is inside a multicolumn cell then use it only if its the last
1187 // column of this multicolumn cell as this gives an added width to the
1188 // column, all the rest should be adapted!
1190 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1192 int const old_column_width = column_info[column].width_of_column;
1194 for (row_type i = 0; i < rows_; ++i) {
1195 idx_type cell = getCellNumber(i, column);
1196 bool ismulti = isMultiColumnReal(cell);
1197 if ((!ismulti || column == right_column_of_cell(cell)) &&
1198 cell_info[i][column].width_of_cell > max)
1200 max = cell_info[i][column].width_of_cell;
1203 column_info[column].width_of_column = max;
1204 return column_info[column].width_of_column != old_column_width;
1208 void LyXTabular::calculate_width_of_tabular()
1210 width_of_tabular = 0;
1211 for (col_type i = 0; i < columns_; ++i)
1212 width_of_tabular += column_info[i].width_of_column;
1216 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1218 if (cell >= numberofcells)
1222 return rowofcell[cell];
1226 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1228 if (cell >= numberofcells)
1229 return columns_ - 1;
1232 return columnofcell[cell];
1236 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1238 row_type const row = row_of_cell(cell);
1239 col_type column = column_of_cell(cell);
1240 while (column < columns_ - 1 &&
1241 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1247 void LyXTabular::write(Buffer const & buf, ostream & os) const
1251 << write_attribute("version", 3)
1252 << write_attribute("rows", rows_)
1253 << write_attribute("columns", columns_)
1255 // global longtable options
1257 << write_attribute("rotate", rotate)
1258 << write_attribute("booktabs", use_booktabs)
1259 << write_attribute("islongtable", is_long_tabular)
1260 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1261 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1262 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1263 << write_attribute("headTopDL", endhead.topDL)
1264 << write_attribute("headBottomDL", endhead.bottomDL)
1265 << write_attribute("footTopDL", endfoot.topDL)
1266 << write_attribute("footBottomDL", endfoot.bottomDL)
1267 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1268 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1269 << write_attribute("lastFootEmpty", endlastfoot.empty)
1271 for (col_type j = 0; j < columns_; ++j) {
1273 << write_attribute("alignment", column_info[j].alignment)
1274 << write_attribute("valignment", column_info[j].valignment)
1275 << write_attribute("leftline", column_info[j].left_line)
1276 << write_attribute("rightline", column_info[j].right_line)
1277 << write_attribute("width", column_info[j].p_width.asString())
1278 << write_attribute("special", column_info[j].align_special)
1281 for (row_type i = 0; i < rows_; ++i) {
1283 << write_attribute("topline", row_info[i].top_line)
1284 << write_attribute("bottomline", row_info[i].bottom_line);
1285 static const string def("default");
1286 if (row_info[i].top_space_default)
1287 os << write_attribute("topspace", def);
1289 os << write_attribute("topspace", row_info[i].top_space);
1290 if (row_info[i].bottom_space_default)
1291 os << write_attribute("bottomspace", def);
1293 os << write_attribute("bottomspace", row_info[i].bottom_space);
1294 if (row_info[i].interline_space_default)
1295 os << write_attribute("interlinespace", def);
1297 os << write_attribute("interlinespace", row_info[i].interline_space);
1298 os << write_attribute("endhead", row_info[i].endhead)
1299 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1300 << write_attribute("endfoot", row_info[i].endfoot)
1301 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1302 << write_attribute("newpage", row_info[i].newpage)
1304 for (col_type j = 0; j < columns_; ++j) {
1306 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1307 << write_attribute("alignment", cell_info[i][j].alignment)
1308 << write_attribute("valignment", cell_info[i][j].valignment)
1309 << write_attribute("topline", cell_info[i][j].top_line)
1310 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1311 << write_attribute("leftline", cell_info[i][j].left_line)
1312 << write_attribute("rightline", cell_info[i][j].right_line)
1313 << write_attribute("rotate", cell_info[i][j].rotate)
1314 << write_attribute("usebox", cell_info[i][j].usebox)
1315 << write_attribute("width", cell_info[i][j].p_width)
1316 << write_attribute("special", cell_info[i][j].align_special)
1318 os << "\\begin_inset ";
1319 cell_info[i][j].inset->write(buf, os);
1320 os << "\n\\end_inset\n"
1325 os << "</lyxtabular>\n";
1329 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1332 istream & is = lex.getStream();
1334 l_getline(is, line);
1335 if (!prefixIs(line, "<lyxtabular ")
1336 && !prefixIs(line, "<LyXTabular ")) {
1337 BOOST_ASSERT(false);
1342 if (!getTokenValue(line, "version", version))
1344 BOOST_ASSERT(version >= 2);
1347 if (!getTokenValue(line, "rows", rows_arg))
1350 if (!getTokenValue(line, "columns", columns_arg))
1352 init(buf.params(), rows_arg, columns_arg);
1353 l_getline(is, line);
1354 if (!prefixIs(line, "<features")) {
1355 lyxerr << "Wrong tabular format (expected <features ...> got"
1356 << line << ')' << endl;
1359 getTokenValue(line, "rotate", rotate);
1360 getTokenValue(line, "booktabs", use_booktabs);
1361 getTokenValue(line, "islongtable", is_long_tabular);
1362 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1363 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1364 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1365 getTokenValue(line, "headTopDL", endhead.topDL);
1366 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1367 getTokenValue(line, "footTopDL", endfoot.topDL);
1368 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1369 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1370 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1371 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1373 for (col_type j = 0; j < columns_; ++j) {
1375 if (!prefixIs(line,"<column")) {
1376 lyxerr << "Wrong tabular format (expected <column ...> got"
1377 << line << ')' << endl;
1380 getTokenValue(line, "alignment", column_info[j].alignment);
1381 getTokenValue(line, "valignment", column_info[j].valignment);
1382 getTokenValue(line, "leftline", column_info[j].left_line);
1383 getTokenValue(line, "rightline", column_info[j].right_line);
1384 getTokenValue(line, "width", column_info[j].p_width);
1385 getTokenValue(line, "special", column_info[j].align_special);
1388 for (row_type i = 0; i < rows_; ++i) {
1389 l_getline(is, line);
1390 if (!prefixIs(line, "<row")) {
1391 lyxerr << "Wrong tabular format (expected <row ...> got"
1392 << line << ')' << endl;
1395 getTokenValue(line, "topline", row_info[i].top_line);
1396 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1397 getTokenValue(line, "topspace", row_info[i].top_space,
1398 row_info[i].top_space_default);
1399 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1400 row_info[i].bottom_space_default);
1401 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1402 row_info[i].interline_space_default);
1403 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1404 getTokenValue(line, "endhead", row_info[i].endhead);
1405 getTokenValue(line, "endfoot", row_info[i].endfoot);
1406 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1407 getTokenValue(line, "newpage", row_info[i].newpage);
1408 for (col_type j = 0; j < columns_; ++j) {
1409 l_getline(is, line);
1410 if (!prefixIs(line, "<cell")) {
1411 lyxerr << "Wrong tabular format (expected <cell ...> got"
1412 << line << ')' << endl;
1415 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1416 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1417 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1418 getTokenValue(line, "topline", cell_info[i][j].top_line);
1419 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1420 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1421 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1422 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1423 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1424 getTokenValue(line, "width", cell_info[i][j].p_width);
1425 getTokenValue(line, "special", cell_info[i][j].align_special);
1426 l_getline(is, line);
1427 if (prefixIs(line, "\\begin_inset")) {
1428 cell_info[i][j].inset->read(buf, lex);
1429 l_getline(is, line);
1431 if (!prefixIs(line, "</cell>")) {
1432 lyxerr << "Wrong tabular format (expected </cell> got"
1433 << line << ')' << endl;
1437 l_getline(is, line);
1438 if (!prefixIs(line, "</row>")) {
1439 lyxerr << "Wrong tabular format (expected </row> got"
1440 << line << ')' << endl;
1444 while (!prefixIs(line, "</lyxtabular>")) {
1445 l_getline(is, line);
1447 set_row_column_number_info();
1451 bool LyXTabular::isMultiColumn(idx_type cell) const
1453 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1457 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1459 return column_of_cell(cell) != right_column_of_cell(cell) &&
1460 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1464 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1466 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1470 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1473 cellstruct & cs = cellinfo_of_cell(cell);
1474 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1475 cs.alignment = column_info[column_of_cell(cell)].alignment;
1476 cs.top_line = row_info[row_of_cell(cell)].top_line;
1477 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1478 cs.left_line = column_info[column_of_cell(cell)].left_line;
1479 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1480 for (idx_type i = 1; i < number; ++i) {
1481 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1482 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1483 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1486 set_row_column_number_info();
1490 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1492 row_type const row = row_of_cell(cell);
1493 col_type column = column_of_cell(cell);
1494 idx_type result = 1;
1496 while (column < columns_ &&
1497 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1506 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1508 row_type const row = row_of_cell(cell);
1509 col_type column = column_of_cell(cell);
1511 idx_type result = 0;
1513 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1514 cell_info[row][column].multicolumn = CELL_NORMAL;
1516 while (column < columns_ &&
1517 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1519 cell_info[row][column].multicolumn = CELL_NORMAL;
1524 set_row_column_number_info();
1529 void LyXTabular::setBookTabs(bool what)
1531 use_booktabs = what;
1535 bool LyXTabular::useBookTabs() const
1537 return use_booktabs;
1541 void LyXTabular::setLongTabular(bool what)
1543 is_long_tabular = what;
1547 bool LyXTabular::isLongTabular() const
1549 return is_long_tabular;
1553 void LyXTabular::setRotateTabular(bool flag)
1559 bool LyXTabular::getRotateTabular() const
1565 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1567 cellinfo_of_cell(cell).rotate = flag;
1571 bool LyXTabular::getRotateCell(idx_type cell) const
1573 return cellinfo_of_cell(cell).rotate;
1577 bool LyXTabular::needRotating() const
1581 for (row_type i = 0; i < rows_; ++i)
1582 for (col_type j = 0; j < columns_; ++j)
1583 if (cell_info[i][j].rotate)
1589 bool LyXTabular::isLastCell(idx_type cell) const
1591 if (cell + 1 < numberofcells)
1597 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1599 if (row_of_cell(cell) > 0)
1600 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1605 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1607 if (row_of_cell(cell) + 1 < rows_)
1608 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1613 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1615 if (row_of_cell(cell) == 0)
1617 if (!isMultiColumn(cell))
1618 return getCellAbove(cell);
1619 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1623 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1625 if (row_of_cell(cell) + 1 >= rows_)
1627 if (!isMultiColumn(cell))
1628 return getCellBelow(cell);
1629 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1633 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1634 col_type column) const
1636 BOOST_ASSERT(column != npos && column < columns_ &&
1637 row != npos && row < rows_);
1638 return cell_info[row][column].cellno;
1642 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1644 cellinfo_of_cell(cell).usebox = type;
1648 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1650 if (column_info[column_of_cell(cell)].p_width.zero() &&
1651 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1653 if (cellinfo_of_cell(cell).usebox > 1)
1654 return cellinfo_of_cell(cell).usebox;
1655 return useParbox(cell);
1660 // This are functions used for the longtable support
1662 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1668 row_info[row].endfirsthead = flag;
1672 row_info[row].endhead = flag;
1677 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1680 hd.set = haveLTHead();
1681 return row_info[row].endhead;
1685 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1688 hd.set = haveLTFirstHead();
1689 return row_info[row].endfirsthead;
1693 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1699 row_info[row].endlastfoot = flag;
1703 row_info[row].endfoot = flag;
1708 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1711 fd.set = haveLTFoot();
1712 return row_info[row].endfoot;
1716 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1719 fd.set = haveLTLastFoot();
1720 return row_info[row].endlastfoot;
1724 void LyXTabular::setLTNewPage(row_type row, bool what)
1726 row_info[row].newpage = what;
1730 bool LyXTabular::getLTNewPage(row_type row) const
1732 return row_info[row].newpage;
1736 bool LyXTabular::haveLTHead() const
1738 for (row_type i = 0; i < rows_; ++i)
1739 if (row_info[i].endhead)
1745 bool LyXTabular::haveLTFirstHead() const
1747 if (endfirsthead.empty)
1749 for (row_type i = 0; i < rows_; ++i)
1750 if (row_info[i].endfirsthead)
1756 bool LyXTabular::haveLTFoot() const
1758 for (row_type i = 0; i < rows_; ++i)
1759 if (row_info[i].endfoot)
1765 bool LyXTabular::haveLTLastFoot() const
1767 if (endlastfoot.empty)
1769 for (row_type i = 0; i < rows_; ++i)
1770 if (row_info[i].endlastfoot)
1776 // end longtable support functions
1778 void LyXTabular::setAscentOfRow(row_type row, int height)
1780 if (row >= rows_ || row_info[row].ascent_of_row == height)
1782 row_info[row].ascent_of_row = height;
1786 void LyXTabular::setDescentOfRow(row_type row, int height)
1788 if (row >= rows_ || row_info[row].descent_of_row == height)
1790 row_info[row].descent_of_row = height;
1794 int LyXTabular::getAscentOfRow(row_type row) const
1798 return row_info[row].ascent_of_row;
1802 int LyXTabular::getDescentOfRow(row_type row) const
1804 BOOST_ASSERT(row < rows_);
1805 return row_info[row].descent_of_row;
1809 int LyXTabular::getHeightOfTabular() const
1812 for (row_type row = 0; row < rows_; ++row)
1813 height += getAscentOfRow(row) + getDescentOfRow(row) +
1814 getAdditionalHeight(row);
1819 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1821 BOOST_ASSERT(row < rows_);
1822 BOOST_ASSERT(column < columns_);
1823 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1827 int LyXTabular::TeXTopHLine(odocstream & os, row_type row) const
1829 // FIXME: assert or return 0 as in TeXBottomHLine()?
1830 BOOST_ASSERT(row != npos);
1831 BOOST_ASSERT(row < rows_);
1833 idx_type const fcell = getFirstCellInRow(row);
1834 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1837 for (idx_type i = fcell; i < n; ++i) {
1841 if (use_booktabs && row == 0) {
1843 } else if (tmp == n - fcell) {
1844 os << (use_booktabs ? "\\midrule " : "\\hline ");
1846 for (idx_type i = fcell; i < n; ++i) {
1848 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1849 << column_of_cell(i) + 1
1851 << right_column_of_cell(i) + 1
1863 int LyXTabular::TeXBottomHLine(odocstream & os, row_type row) const
1865 // FIXME: return 0 or assert as in TeXTopHLine()?
1866 if (row == npos || row >= rows_)
1869 idx_type const fcell = getFirstCellInRow(row);
1870 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1873 for (idx_type i = fcell; i < n; ++i) {
1877 if (use_booktabs && row == rows_ - 1) {
1878 os << "\\bottomrule";
1879 } else if (tmp == n - fcell) {
1880 os << (use_booktabs ? "\\midrule" : "\\hline");
1882 for (idx_type i = fcell; i < n; ++i) {
1883 if (bottomLine(i)) {
1884 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1885 << column_of_cell(i) + 1
1887 << right_column_of_cell(i) + 1
1899 int LyXTabular::TeXCellPreamble(odocstream & os, idx_type cell) const
1903 if (getRotateCell(cell)) {
1904 os << "\\begin{sideways}\n";
1907 if (isMultiColumn(cell)) {
1908 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1909 if (!cellinfo_of_cell(cell).align_special.empty()) {
1910 os << from_ascii(cellinfo_of_cell(cell).align_special)
1913 if (leftLine(cell) &&
1914 (isFirstCellInRow(cell) ||
1915 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1916 !rightLine(cell - 1, true))))
1920 if (!getPWidth(cell).zero()) {
1921 switch (getVAlignment(cell)) {
1922 case LYX_VALIGN_TOP:
1925 case LYX_VALIGN_MIDDLE:
1928 case LYX_VALIGN_BOTTOM:
1933 << from_ascii(getPWidth(cell).asLatexString())
1936 switch (getAlignment(cell)) {
1937 case LYX_ALIGN_LEFT:
1940 case LYX_ALIGN_RIGHT:
1948 if (rightLine(cell))
1950 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1956 if (getUsebox(cell) == BOX_PARBOX) {
1958 switch (getVAlignment(cell)) {
1959 case LYX_VALIGN_TOP:
1962 case LYX_VALIGN_MIDDLE:
1965 case LYX_VALIGN_BOTTOM:
1969 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1971 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1972 os << "\\begin{minipage}[";
1973 switch (getVAlignment(cell)) {
1974 case LYX_VALIGN_TOP:
1977 case LYX_VALIGN_MIDDLE:
1980 case LYX_VALIGN_BOTTOM:
1984 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1992 int LyXTabular::TeXCellPostamble(odocstream & os, idx_type cell) const
1997 if (getUsebox(cell) == BOX_PARBOX)
1999 else if (getUsebox(cell) == BOX_MINIPAGE) {
2000 os << "%\n\\end{minipage}";
2003 if (isMultiColumn(cell)) {
2006 if (getRotateCell(cell)) {
2007 os << "%\n\\end{sideways}";
2014 int LyXTabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2015 OutputParams const & runparams) const
2017 if (!is_long_tabular)
2021 // output header info
2023 if (endhead.topDL) {
2027 for (row_type i = 0; i < rows_; ++i) {
2028 if (row_info[i].endhead) {
2029 ret += TeXRow(os, i, buf, runparams);
2032 if (endhead.bottomDL) {
2036 os << "\\endhead\n";
2038 if (endfirsthead.empty) {
2039 os << "\\endfirsthead\n";
2043 // output firstheader info
2044 if (haveLTFirstHead()) {
2045 if (endfirsthead.topDL) {
2049 for (row_type i = 0; i < rows_; ++i) {
2050 if (row_info[i].endfirsthead) {
2051 ret += TeXRow(os, i, buf, runparams);
2054 if (endfirsthead.bottomDL) {
2058 os << "\\endfirsthead\n";
2061 // output footer info
2063 if (endfoot.topDL) {
2067 for (row_type i = 0; i < rows_; ++i) {
2068 if (row_info[i].endfoot) {
2069 ret += TeXRow(os, i, buf, runparams);
2072 if (endfoot.bottomDL) {
2076 os << "\\endfoot\n";
2078 if (endlastfoot.empty) {
2079 os << "\\endlastfoot\n";
2083 // output lastfooter info
2084 if (haveLTLastFoot()) {
2085 if (endlastfoot.topDL) {
2089 for (row_type i = 0; i < rows_; ++i) {
2090 if (row_info[i].endlastfoot) {
2091 ret += TeXRow(os, i, buf, runparams);
2094 if (endlastfoot.bottomDL) {
2098 os << "\\endlastfoot\n";
2105 bool LyXTabular::isValidRow(row_type row) const
2107 if (!is_long_tabular)
2109 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2110 !row_info[row].endfoot && !row_info[row].endlastfoot;
2114 int LyXTabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2115 OutputParams const & runparams) const
2117 idx_type cell = getCellNumber(i, 0);
2118 int ret = TeXTopHLine(os, i);
2119 if (row_info[i].top_space_default) {
2121 os << "\\addlinespace\n";
2123 os << "\\noalign{\\vskip\\doublerulesep}\n";
2124 } else if(!row_info[i].top_space.zero()) {
2126 os << "\\addlinespace["
2127 << from_ascii(row_info[i].top_space.asLatexString())
2130 os << "\\noalign{\\vskip"
2131 << from_ascii(row_info[i].top_space.asLatexString())
2137 for (col_type j = 0; j < columns_; ++j) {
2138 if (isPartOfMultiColumn(i, j))
2140 ret += TeXCellPreamble(os, cell);
2141 shared_ptr<InsetText> inset = getCellInset(cell);
2143 Paragraph const & par = inset->paragraphs().front();
2144 bool rtl = par.isRightToLeftPar(buf.params())
2146 && getPWidth(cell).zero();
2150 ret += inset->latex(buf, os, runparams);
2154 ret += TeXCellPostamble(os, cell);
2155 if (!isLastCellInRow(cell)) { // not last cell in row
2160 os << "\\tabularnewline";
2161 if (row_info[i].bottom_space_default) {
2163 os << "\\addlinespace";
2165 os << "[\\doublerulesep]";
2166 } else if (!row_info[i].bottom_space.zero()) {
2168 os << "\\addlinespace";
2170 << from_ascii(row_info[i].bottom_space.asLatexString())
2175 ret += TeXBottomHLine(os, i);
2176 if (row_info[i].interline_space_default) {
2178 os << "\\addlinespace\n";
2180 os << "\\noalign{\\vskip\\doublerulesep}\n";
2181 } else if (!row_info[i].interline_space.zero()) {
2183 os << "\\addlinespace["
2184 << from_ascii(row_info[i].interline_space.asLatexString())
2187 os << "\\noalign{\\vskip"
2188 << from_ascii(row_info[i].interline_space.asLatexString())
2196 int LyXTabular::latex(Buffer const & buf, odocstream & os,
2197 OutputParams const & runparams) const
2201 //+---------------------------------------------------------------------
2202 //+ first the opening preamble +
2203 //+---------------------------------------------------------------------
2206 os << "\\begin{sideways}\n";
2209 if (is_long_tabular)
2210 os << "\\begin{longtable}{";
2212 os << "\\begin{tabular}{";
2213 for (col_type i = 0; i < columns_; ++i) {
2214 if (!column_info[i].align_special.empty()) {
2215 os << from_ascii(column_info[i].align_special);
2217 if (!use_booktabs && column_info[i].left_line)
2219 if (!column_info[i].p_width.zero()) {
2220 switch (column_info[i].alignment) {
2221 case LYX_ALIGN_LEFT:
2222 os << ">{\\raggedright}";
2224 case LYX_ALIGN_RIGHT:
2225 os << ">{\\raggedleft}";
2227 case LYX_ALIGN_CENTER:
2228 os << ">{\\centering}";
2230 case LYX_ALIGN_NONE:
2231 case LYX_ALIGN_BLOCK:
2232 case LYX_ALIGN_LAYOUT:
2233 case LYX_ALIGN_SPECIAL:
2237 switch (column_info[i].valignment) {
2238 case LYX_VALIGN_TOP:
2241 case LYX_VALIGN_MIDDLE:
2244 case LYX_VALIGN_BOTTOM:
2249 << from_ascii(column_info[i].p_width.asLatexString())
2252 switch (column_info[i].alignment) {
2253 case LYX_ALIGN_LEFT:
2256 case LYX_ALIGN_RIGHT:
2264 if (!use_booktabs && column_info[i].right_line)
2271 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2273 //+---------------------------------------------------------------------
2274 //+ the single row and columns (cells) +
2275 //+---------------------------------------------------------------------
2277 for (row_type i = 0; i < rows_; ++i) {
2278 if (isValidRow(i)) {
2279 ret += TeXRow(os, i, buf, runparams);
2280 if (is_long_tabular && row_info[i].newpage) {
2281 os << "\\newpage\n";
2287 //+---------------------------------------------------------------------
2288 //+ the closing of the tabular +
2289 //+---------------------------------------------------------------------
2291 if (is_long_tabular)
2292 os << "\\end{longtable}";
2294 os << "\\end{tabular}";
2296 os << "\n\\end{sideways}";
2304 int LyXTabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2305 OutputParams const & runparams) const
2308 idx_type cell = getFirstCellInRow(row);
2311 for (col_type j = 0; j < columns_; ++j) {
2312 if (isPartOfMultiColumn(row, j))
2315 os << "<entry align=\"";
2316 switch (getAlignment(cell)) {
2317 case LYX_ALIGN_LEFT:
2320 case LYX_ALIGN_RIGHT:
2328 os << "\" valign=\"";
2329 switch (getVAlignment(cell)) {
2330 case LYX_VALIGN_TOP:
2333 case LYX_VALIGN_BOTTOM:
2336 case LYX_VALIGN_MIDDLE:
2341 if (isMultiColumn(cell)) {
2342 os << " namest=\"col" << j << "\" ";
2343 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2347 ret += getCellInset(cell)->docbook(buf, os, runparams);
2356 int LyXTabular::docbook(Buffer const & buf, odocstream & os,
2357 OutputParams const & runparams) const
2361 //+---------------------------------------------------------------------
2362 //+ first the opening preamble +
2363 //+---------------------------------------------------------------------
2365 os << "<tgroup cols=\"" << columns_
2366 << "\" colsep=\"1\" rowsep=\"1\">\n";
2368 for (col_type i = 0; i < columns_; ++i) {
2369 os << "<colspec colname=\"col" << i << "\" align=\"";
2370 switch (column_info[i].alignment) {
2371 case LYX_ALIGN_LEFT:
2374 case LYX_ALIGN_RIGHT:
2382 if (runparams.flavor == OutputParams::XML)
2388 //+---------------------------------------------------------------------
2389 //+ Long Tabular case +
2390 //+---------------------------------------------------------------------
2392 // output header info
2393 if (haveLTHead() || haveLTFirstHead()) {
2396 for (row_type i = 0; i < rows_; ++i) {
2397 if (row_info[i].endhead || row_info[i].endfirsthead) {
2398 ret += docbookRow(buf, os, i, runparams);
2404 // output footer info
2405 if (haveLTFoot() || haveLTLastFoot()) {
2408 for (row_type i = 0; i < rows_; ++i) {
2409 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2410 ret += docbookRow(buf, os, i, runparams);
2417 //+---------------------------------------------------------------------
2418 //+ the single row and columns (cells) +
2419 //+---------------------------------------------------------------------
2423 for (row_type i = 0; i < rows_; ++i) {
2424 if (isValidRow(i)) {
2425 ret += docbookRow(buf, os, i, runparams);
2430 //+---------------------------------------------------------------------
2431 //+ the closing of the tabular +
2432 //+---------------------------------------------------------------------
2441 int LyXTabular::asciiTopHLine(odocstream & os, row_type row,
2442 vector<unsigned int> const & clen) const
2444 idx_type const fcell = getFirstCellInRow(row);
2445 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2448 for (idx_type i = fcell; i < n; ++i) {
2458 for (idx_type i = fcell; i < n; ++i) {
2469 col_type column = column_of_cell(i);
2470 int len = clen[column];
2471 while (column < columns_ - 1
2472 && isPartOfMultiColumn(row, ++column))
2473 len += clen[column] + 4;
2474 os << docstring(len, ch);
2489 int LyXTabular::asciiBottomHLine(odocstream & os, row_type row,
2490 vector<unsigned int> const & clen) const
2492 idx_type const fcell = getFirstCellInRow(row);
2493 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2496 for (idx_type i = fcell; i < n; ++i) {
2497 if (bottomLine(i)) {
2506 for (idx_type i = fcell; i < n; ++i) {
2507 if (bottomLine(i)) {
2517 col_type column = column_of_cell(i);
2518 int len = clen[column];
2519 while (column < columns_ -1
2520 && isPartOfMultiColumn(row, ++column))
2521 len += clen[column] + 4;
2522 os << docstring(len, ch);
2523 if (bottomLine(i)) {
2537 int LyXTabular::asciiPrintCell(Buffer const & buf, odocstream & os,
2538 OutputParams const & runparams,
2539 idx_type cell, row_type row, col_type column,
2540 vector<unsigned int> const & clen,
2541 bool onlydata) const
2543 odocstringstream sstr;
2544 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2556 unsigned int len1 = sstr.str().length();
2557 unsigned int len2 = clen[column];
2558 while (column < columns_ -1
2559 && isPartOfMultiColumn(row, ++column))
2560 len2 += clen[column] + 4;
2563 switch (getAlignment(cell)) {
2565 case LYX_ALIGN_LEFT:
2568 case LYX_ALIGN_RIGHT:
2572 case LYX_ALIGN_CENTER:
2578 os << docstring(len1, ' ') << sstr.str()
2579 << docstring(len2, ' ');
2581 if (rightLine(cell))
2590 int LyXTabular::plaintext(Buffer const & buf, odocstream & os,
2591 OutputParams const & runparams,
2593 bool onlydata, unsigned char delim) const
2597 // first calculate the width of the single columns
2598 vector<unsigned int> clen(columns_);
2601 // first all non (real) multicolumn cells!
2602 for (col_type j = 0; j < columns_; ++j) {
2604 for (row_type i = 0; i < rows_; ++i) {
2605 idx_type cell = getCellNumber(i, j);
2606 if (isMultiColumnReal(cell))
2608 odocstringstream sstr;
2609 getCellInset(cell)->plaintext(buf, sstr, runparams);
2610 if (clen[j] < sstr.str().length())
2611 clen[j] = sstr.str().length();
2614 // then all (real) multicolumn cells!
2615 for (col_type j = 0; j < columns_; ++j) {
2616 for (row_type i = 0; i < rows_; ++i) {
2617 idx_type cell = getCellNumber(i, j);
2618 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2620 odocstringstream sstr;
2621 getCellInset(cell)->plaintext(buf, sstr, runparams);
2622 int len = int(sstr.str().length());
2623 idx_type const n = cells_in_multicolumn(cell);
2624 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2626 if (len > int(clen[j + n - 1]))
2627 clen[j + n - 1] = len;
2632 for (row_type i = 0; i < rows_; ++i) {
2633 if (!onlydata && asciiTopHLine(os, i, clen))
2634 os << docstring(depth * 2, ' ');
2635 for (col_type j = 0; j < columns_; ++j) {
2636 if (isPartOfMultiColumn(i, j))
2638 if (onlydata && j > 0)
2640 ret += asciiPrintCell(buf, os, runparams,
2641 cell, i, j, clen, onlydata);
2646 os << docstring(depth * 2, ' ');
2647 if (asciiBottomHLine(os, i, clen))
2648 os << docstring(depth * 2, ' ');
2655 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2657 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2661 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2662 col_type column) const
2664 return cell_info[row][column].inset;
2668 void LyXTabular::setCellInset(row_type row, col_type column,
2669 shared_ptr<InsetText> ins) const
2671 cell_info[row][column].inset = ins;
2675 LyXTabular::idx_type
2676 LyXTabular::getCellFromInset(InsetBase const * inset) const
2678 // is this inset part of the tabular?
2680 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2681 BOOST_ASSERT(false);
2684 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2685 if (getCellInset(cell).get() == inset) {
2686 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2687 << "cell=" << cell << endl;
2691 // We should have found a cell at this point
2692 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2693 << inset << " not found!" << endl;
2694 BOOST_ASSERT(false);
2700 void LyXTabular::validate(LaTeXFeatures & features) const
2702 features.require("NeedTabularnewline");
2704 features.require("booktabs");
2705 if (isLongTabular())
2706 features.require("longtable");
2708 features.require("rotating");
2709 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2710 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2711 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2712 features.require("array");
2713 getCellInset(cell)->validate(features);
2718 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2720 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2721 ParagraphList::const_iterator cit = parlist.begin();
2722 ParagraphList::const_iterator end = parlist.end();
2724 for (; cit != end; ++cit)
2725 for (int i = 0; i < cit->size(); ++i)
2726 if (cit->isNewline(i))