3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
23 #include "bufferparams.h"
25 #include "LaTeXFeatures.h"
27 #include "outputparams.h"
28 #include "paragraph.h"
30 #include "insets/insettabular.h"
32 #include "support/lstrings.h"
33 #include "support/convert.h"
37 using lyx::support::ltrim;
38 using lyx::support::prefixIs;
39 using lyx::support::rtrim;
40 using lyx::support::suffixIs;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
52 using std::ostringstream;
56 #ifndef CXX_GLOBAL_CSTD
63 int const WIDTH_OF_LINE = 5;
66 string const write_attribute(string const & name, T const & t)
68 string const s = tostr(t);
69 return s.empty() ? s : " " + name + "=\"" + s + "\"";
73 string const write_attribute(string const & name, string const & t)
75 return t.empty() ? t : " " + name + "=\"" + t + "\"";
80 string const write_attribute(string const & name, bool const & b)
82 // we write only true attribute values so we remove a bit of the
83 // file format bloat for tabulars.
84 return b ? write_attribute(name, convert<string>(b)) : string();
89 string const write_attribute(string const & name, int const & i)
91 // we write only true attribute values so we remove a bit of the
92 // file format bloat for tabulars.
93 return i ? write_attribute(name, convert<string>(i)) : string();
98 string const write_attribute(string const & name, LyXTabular::idx_type const & i)
100 // we write only true attribute values so we remove a bit of the
101 // file format bloat for tabulars.
102 return i ? write_attribute(name, convert<string>(i)) : string();
107 string const write_attribute(string const & name, LyXLength const & value)
109 // we write only the value if we really have one same reson as above.
110 return value.zero() ? string() : write_attribute(name, value.asString());
114 string const tostr(LyXAlignment const & num)
119 case LYX_ALIGN_BLOCK:
123 case LYX_ALIGN_CENTER:
125 case LYX_ALIGN_RIGHT:
127 case LYX_ALIGN_LAYOUT:
129 case LYX_ALIGN_SPECIAL:
136 string const tostr(LyXTabular::VAlignment const & num)
139 case LyXTabular::LYX_VALIGN_TOP:
141 case LyXTabular::LYX_VALIGN_MIDDLE:
143 case LyXTabular::LYX_VALIGN_BOTTOM:
150 string const tostr(LyXTabular::BoxType const & num)
153 case LyXTabular::BOX_NONE:
155 case LyXTabular::BOX_PARBOX:
157 case LyXTabular::BOX_MINIPAGE:
164 // I would have liked a fromstr template a lot better. (Lgb)
165 bool string2type(string const str, LyXAlignment & num)
168 num = LYX_ALIGN_NONE;
169 else if (str == "block")
170 num = LYX_ALIGN_BLOCK;
171 else if (str == "left")
172 num = LYX_ALIGN_LEFT;
173 else if (str == "center")
174 num = LYX_ALIGN_CENTER;
175 else if (str == "right")
176 num = LYX_ALIGN_RIGHT;
183 bool string2type(string const str, LyXTabular::VAlignment & num)
186 num = LyXTabular::LYX_VALIGN_TOP;
187 else if (str == "middle" )
188 num = LyXTabular::LYX_VALIGN_MIDDLE;
189 else if (str == "bottom")
190 num = LyXTabular::LYX_VALIGN_BOTTOM;
197 bool string2type(string const str, LyXTabular::BoxType & num)
200 num = LyXTabular::BOX_NONE;
201 else if (str == "parbox")
202 num = LyXTabular::BOX_PARBOX;
203 else if (str == "minipage")
204 num = LyXTabular::BOX_MINIPAGE;
211 bool string2type(string const str, bool & num)
215 else if (str == "false")
223 bool getTokenValue(string const & str, char const * token, string & ret)
226 size_t token_length = strlen(token);
227 string::size_type pos = str.find(token);
229 if (pos == string::npos || pos + token_length + 1 >= str.length()
230 || str[pos + token_length] != '=')
232 pos += token_length + 1;
234 if (ch != '"' && ch != '\'') { // only read till next space
238 while (pos < str.length() - 1 && str[++pos] != ch)
245 bool getTokenValue(string const & str, char const * token, int & num)
249 if (!getTokenValue(str, token, tmp))
251 num = convert<int>(tmp);
256 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
259 return getTokenValue(str, token, tmp) && string2type(tmp, num);
263 bool getTokenValue(string const & str, char const * token,
264 LyXTabular::VAlignment & num)
267 return getTokenValue(str, token, tmp) && string2type(tmp, num);
271 bool getTokenValue(string const & str, char const * token,
272 LyXTabular::BoxType & num)
275 return getTokenValue(str, token, tmp) && string2type(tmp, num);
279 bool getTokenValue(string const & str, char const * token, bool & flag)
281 // set the flag always to false as this should be the default for bools
282 // not in the file-format.
285 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
289 bool getTokenValue(string const & str, char const * token, LyXLength & len)
291 // set the lenght to be zero() as default as this it should be if not
292 // in the file format.
295 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
299 void l_getline(istream & is, string & str)
302 while (str.empty()) {
304 if (!str.empty() && str[str.length() - 1] == '\r')
305 str.erase(str.length() - 1);
311 /// Define a few methods for the inner structs
313 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
316 multicolumn(LyXTabular::CELL_NORMAL),
317 alignment(LYX_ALIGN_CENTER),
318 valignment(LYX_VALIGN_TOP),
325 inset(new InsetText(bp))
329 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
331 width_of_cell(cs.width_of_cell),
332 multicolumn(cs.multicolumn),
333 alignment(cs.alignment),
334 valignment(cs.valignment),
335 top_line(cs.top_line),
336 bottom_line(cs.bottom_line),
337 left_line(cs.left_line),
338 right_line(cs.right_line),
341 align_special(cs.align_special),
343 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
347 LyXTabular::cellstruct &
348 LyXTabular::cellstruct::operator=(cellstruct cs)
356 LyXTabular::cellstruct::swap(cellstruct & rhs)
358 std::swap(cellno, rhs.cellno);
359 std::swap(width_of_cell, rhs.width_of_cell);
360 std::swap(multicolumn, rhs.multicolumn);
361 std::swap(alignment, rhs.alignment);
362 std::swap(valignment, rhs.valignment);
363 std::swap(top_line, rhs.top_line);
364 std::swap(bottom_line, rhs.bottom_line);
365 std::swap(left_line, rhs.left_line);
366 std::swap(right_line, rhs.right_line);
367 std::swap(usebox, rhs.usebox);
368 std::swap(rotate, rhs.rotate);
369 std::swap(align_special, rhs.align_special);
370 p_width.swap(rhs.p_width);
371 inset.swap(rhs.inset);
375 LyXTabular::rowstruct::rowstruct()
388 LyXTabular::columnstruct::columnstruct()
389 : alignment(LYX_ALIGN_CENTER),
390 valignment(LYX_VALIGN_TOP),
398 LyXTabular::ltType::ltType()
405 LyXTabular::LyXTabular(BufferParams const & bp, row_type rows_arg,
406 col_type columns_arg)
408 init(bp, rows_arg, columns_arg);
412 // activates all lines and sets all widths to 0
413 void LyXTabular::init(BufferParams const & bp, row_type rows_arg,
414 col_type columns_arg)
417 columns_ = columns_arg;
418 row_info = row_vector(rows_);
419 column_info = column_vector(columns_);
420 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
421 row_info.reserve(10);
422 column_info.reserve(10);
423 cell_info.reserve(100);
425 for (row_type i = 0; i < rows_; ++i)
426 cell_info[i].back().right_line = true;
427 row_info.back().bottom_line = true;
428 row_info.front().bottom_line = true;
429 column_info.back().right_line = true;
430 is_long_tabular = false;
435 void LyXTabular::fixCellNums()
438 for (row_type i = 0; i < rows_; ++i) {
439 for (col_type j = 0; j < columns_; ++j) {
440 // When debugging it can be nice to set
442 cell_info[i][j].inset->setDrawFrame(false);
443 cell_info[i][j].cellno = cellno++;
445 cell_info[i].back().right_line = true;
448 set_row_column_number_info();
452 void LyXTabular::appendRow(BufferParams const & bp, idx_type const cell)
456 row_type const row = row_of_cell(cell);
458 row_vector::iterator rit = row_info.begin() + row;
459 row_info.insert(rit, rowstruct());
460 // now set the values of the row before
461 row_info[row] = row_info[row + 1];
463 cell_vvector old(rows_ - 1);
464 for (row_type i = 0; i < rows_ - 1; ++i)
465 swap(cell_info[i], old[i]);
467 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
469 for (row_type i = 0; i <= row; ++i)
470 swap(cell_info[i], old[i]);
471 for (row_type i = row + 2; i < rows_; ++i)
472 swap(cell_info[i], old[i - 1]);
474 if (bp.tracking_changes)
475 for (col_type j = 0; j < columns_; ++j)
476 cell_info[row + 1][j].inset->markNew(true);
478 set_row_column_number_info();
482 void LyXTabular::deleteRow(row_type const row)
484 // Not allowed to delete last row
488 row_info.erase(row_info.begin() + row);
489 cell_info.erase(cell_info.begin() + row);
495 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
499 col_type const column = column_of_cell(cell);
500 column_vector::iterator cit = column_info.begin() + column + 1;
501 column_info.insert(cit, columnstruct());
502 // set the column values of the column before
503 column_info[column + 1] = column_info[column];
505 for (row_type i = 0; i < rows_; ++i) {
506 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
508 // care about multicolumns
509 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
510 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
512 if (column + 2 >= columns_
513 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
514 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
517 for (row_type i = 0; i < rows_; ++i) {
518 cell_info[i][column + 1].inset->clear(false);
519 if (bp.tracking_changes)
520 cell_info[i][column + 1].inset->markNew(true);
526 void LyXTabular::deleteColumn(col_type const column)
528 // Not allowed to delete last column
532 column_info.erase(column_info.begin() + column);
533 for (row_type i = 0; i < rows_; ++i)
534 cell_info[i].erase(cell_info[i].begin() + column);
540 void LyXTabular::set_row_column_number_info()
543 for (row_type row = 0; row < rows_; ++row) {
544 for (col_type column = 0; column < columns_; ++column) {
545 if (cell_info[row][column].multicolumn
546 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
548 if (numberofcells == 0)
549 // FIXME: Is this intended?
550 cell_info[row][column].cellno = npos;
552 cell_info[row][column].cellno =
557 rowofcell.resize(numberofcells);
558 columnofcell.resize(numberofcells);
563 c < numberofcells && row < rows_ && column < columns_;) {
565 columnofcell[c] = column;
569 } while (column < columns_ &&
570 cell_info[row][column].multicolumn
571 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
573 if (column == columns_) {
579 for (row_type row = 0; row < rows_; ++row) {
580 for (col_type column = 0; column < columns_; ++column) {
581 if (isPartOfMultiColumn(row,column))
583 cell_info[row][column].inset->setAutoBreakRows(
584 !getPWidth(getCellNumber(row, column)).zero());
590 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
592 return numberofcells;
596 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
598 row_type const row = row_of_cell(cell);
600 for (col_type i = 0; i < columns_; ++i)
601 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
607 // returns 1 if there is a topline, returns 0 if not
608 bool LyXTabular::topLine(idx_type const cell, bool const onlycolumn) const
610 if (!onlycolumn && isMultiColumn(cell))
611 return cellinfo_of_cell(cell).top_line;
612 return row_info[row_of_cell(cell)].top_line;
616 bool LyXTabular::bottomLine(idx_type const cell, bool onlycolumn) const
618 if (!onlycolumn && isMultiColumn(cell))
619 return cellinfo_of_cell(cell).bottom_line;
620 return row_info[row_of_cell(cell)].bottom_line;
624 bool LyXTabular::leftLine(idx_type cell, bool onlycolumn) const
626 if (!onlycolumn && isMultiColumn(cell) &&
627 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
629 if (cellinfo_of_cell(cell).align_special.empty())
630 return cellinfo_of_cell(cell).left_line;
631 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
633 if (column_info[column_of_cell(cell)].align_special.empty())
634 return column_info[column_of_cell(cell)].left_line;
635 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
639 bool LyXTabular::rightLine(idx_type cell, bool onlycolumn) const
641 if (!onlycolumn && isMultiColumn(cell) &&
642 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
644 if (cellinfo_of_cell(cell).align_special.empty())
645 return cellinfo_of_cell(cell).right_line;
646 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
648 if (column_info[column_of_cell(cell)].align_special.empty())
649 return column_info[right_column_of_cell(cell)].right_line;
650 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
654 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
656 row_type row = row_of_cell(cell);
657 if (row > 0 && !getAdditionalHeight(row)) {
658 col_type column = column_of_cell(cell);
661 && cell_info[row][column].multicolumn
662 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
664 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
665 return row_info[row].bottom_line;
667 return cell_info[row][column].bottom_line;
673 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
675 col_type column = column_of_cell(cell);
677 row_type row = row_of_cell(cell);
679 (cell_info[row][column].multicolumn ==
680 LyXTabular::CELL_PART_OF_MULTICOLUMN));
681 if (getAdditionalWidth(cell_info[row][column].cellno))
683 return rightLine(cell_info[row][column].cellno);
689 bool LyXTabular::isLastRow(idx_type cell) const
691 return row_of_cell(cell) == rows_ - 1;
695 int LyXTabular::getAdditionalHeight(row_type row) const
697 if (!row || row >= rows_)
703 for (col_type column = 0; column < columns_ && bottom; ++column) {
704 switch (cell_info[row - 1][column].multicolumn) {
705 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
706 bottom = cell_info[row - 1][column].bottom_line;
708 case LyXTabular::CELL_NORMAL:
709 bottom = row_info[row - 1].bottom_line;
712 for (col_type column = 0; column < columns_ && top; ++column) {
713 switch (cell_info[row][column].multicolumn) {
714 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
715 top = cell_info[row][column].top_line;
717 case LyXTabular::CELL_NORMAL:
718 top = row_info[row].top_line;
722 return WIDTH_OF_LINE;
727 int LyXTabular::getAdditionalWidth(idx_type cell) const
729 // internally already set in setWidthOfCell
730 // used to get it back in text.C
731 col_type const col = right_column_of_cell(cell);
732 row_type const row = row_of_cell(cell);
733 if (col < columns_ - 1 && rightLine(cell) &&
734 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
736 return WIDTH_OF_LINE;
742 // returns the maximum over all rows
743 int LyXTabular::getWidthOfColumn(idx_type cell) const
745 col_type const column1 = column_of_cell(cell);
746 col_type const column2 = right_column_of_cell(cell);
748 for (col_type i = column1; i <= column2; ++i)
749 result += column_info[i].width_of_column;
754 int LyXTabular::getWidthOfTabular() const
756 return width_of_tabular;
760 // returns true if a complete update is necessary, otherwise false
761 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
763 if (!isMultiColumn(cell))
766 row_type const row = row_of_cell(cell);
767 col_type const column1 = column_of_cell(cell);
768 col_type const column2 = right_column_of_cell(cell);
769 int const old_val = cell_info[row][column2].width_of_cell;
771 // first set columns to 0 so we can calculate the right width
772 for (col_type i = column1; i <= column2; ++i) {
773 cell_info[row][i].width_of_cell = 0;
775 // set the width to MAX_WIDTH until width > 0
776 int width = new_width + 2 * WIDTH_OF_LINE;
777 col_type i = column1;
778 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
779 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
780 width -= column_info[i].width_of_column;
783 cell_info[row][i].width_of_cell = width;
785 if (old_val != cell_info[row][column2].width_of_cell) {
786 // in this case we have to recalculate all multicolumn cells which
787 // have this column as one of theirs but not as last one
788 calculate_width_of_column_NMC(i);
789 recalculateMulticolumnsOfColumn(i);
790 calculate_width_of_column(i);
796 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
798 // the last column does not have to be recalculated because all
799 // multicolumns will have here there last multicolumn cell which
800 // always will have the whole rest of the width of the cell.
801 if (column > (columns_ - 2))
803 for(row_type row = 0; row < rows_; ++row) {
804 int mc = cell_info[row][column].multicolumn;
805 int nmc = cell_info[row][column+1].multicolumn;
806 // we only have to update multicolumns which do not have this
807 // column as their last column!
808 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
809 (mc == CELL_PART_OF_MULTICOLUMN &&
810 nmc == CELL_PART_OF_MULTICOLUMN))
812 idx_type const cellno = cell_info[row][column].cellno;
813 setWidthOfMulticolCell(cellno,
814 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
820 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
822 row_type const row = row_of_cell(cell);
823 col_type const column1 = column_of_cell(cell);
828 if (rightLine(cell_info[row][column1].cellno, true) &&
829 column1 < columns_ - 1 &&
830 leftLine(cell_info[row][column1+1].cellno, true))
832 add_width = WIDTH_OF_LINE;
835 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
838 if (isMultiColumnReal(cell)) {
839 tmp = setWidthOfMulticolCell(cell, new_width);
841 width = new_width + 2 * WIDTH_OF_LINE + add_width;
842 cell_info[row][column1].width_of_cell = width;
843 tmp = calculate_width_of_column_NMC(column1);
845 recalculateMulticolumnsOfColumn(column1);
848 for (col_type i = 0; i < columns_; ++i)
849 calculate_width_of_column(i);
850 calculate_width_of_tabular();
855 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
858 if (!isMultiColumn(cell) || onlycolumn)
859 column_info[column_of_cell(cell)].alignment = align;
861 cellinfo_of_cell(cell).alignment = align;
865 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
868 if (!isMultiColumn(cell) || onlycolumn)
869 column_info[column_of_cell(cell)].valignment = align;
871 cellinfo_of_cell(cell).valignment = align;
875 void LyXTabular::setColumnPWidth(idx_type cell, LyXLength const & width)
877 col_type const j = column_of_cell(cell);
879 column_info[j].p_width = width;
880 for (row_type i = 0; i < rows_; ++i) {
881 idx_type const cell = getCellNumber(i, j);
882 // because of multicolumns
883 getCellInset(cell)->setAutoBreakRows(!getPWidth(cell).zero());
888 bool LyXTabular::setMColumnPWidth(idx_type cell, LyXLength const & width)
890 if (!isMultiColumn(cell))
893 cellinfo_of_cell(cell).p_width = width;
894 getCellInset(cell)->setAutoBreakRows(!width.zero());
899 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
900 LyXTabular::Feature what)
902 if (what == SET_SPECIAL_MULTI)
903 cellinfo_of_cell(cell).align_special = special;
905 column_info[column_of_cell(cell)].align_special = special;
909 void LyXTabular::setAllLines(idx_type cell, bool line)
911 setTopLine(cell, line);
912 setBottomLine(cell, line);
913 setRightLine(cell, line);
914 setLeftLine(cell, line);
918 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
920 row_type const row = row_of_cell(cell);
921 if (onlycolumn || !isMultiColumn(cell))
922 row_info[row].top_line = line;
924 cellinfo_of_cell(cell).top_line = line;
928 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
930 if (onlycolumn || !isMultiColumn(cell))
931 row_info[row_of_cell(cell)].bottom_line = line;
933 cellinfo_of_cell(cell).bottom_line = line;
937 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
939 if (onlycolumn || !isMultiColumn(cell))
940 column_info[column_of_cell(cell)].left_line = line;
942 cellinfo_of_cell(cell).left_line = line;
946 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
948 if (onlycolumn || !isMultiColumn(cell))
949 column_info[right_column_of_cell(cell)].right_line = line;
951 cellinfo_of_cell(cell).right_line = line;
955 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
957 if (!onlycolumn && isMultiColumn(cell))
958 return cellinfo_of_cell(cell).alignment;
959 return column_info[column_of_cell(cell)].alignment;
963 LyXTabular::VAlignment
964 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
966 if (!onlycolumn && isMultiColumn(cell))
967 return cellinfo_of_cell(cell).valignment;
968 return column_info[column_of_cell(cell)].valignment;
972 LyXLength const LyXTabular::getPWidth(idx_type cell) const
974 if (isMultiColumn(cell))
975 return cellinfo_of_cell(cell).p_width;
976 return column_info[column_of_cell(cell)].p_width;
980 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
982 return column_info[column_of_cell(cell)].p_width;
986 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
988 if (isMultiColumn(cell))
989 return cellinfo_of_cell(cell).p_width;
994 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
996 if (what == SET_SPECIAL_MULTI)
997 return cellinfo_of_cell(cell).align_special;
998 return column_info[column_of_cell(cell)].align_special;
1002 int LyXTabular::getWidthOfCell(idx_type cell) const
1004 row_type const row = row_of_cell(cell);
1005 col_type const column1 = column_of_cell(cell);
1006 col_type const column2 = right_column_of_cell(cell);
1008 for (col_type i = column1; i <= column2; ++i)
1009 result += cell_info[row][i].width_of_cell;
1014 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1018 switch (getAlignment(cell)) {
1019 case LYX_ALIGN_CENTER:
1020 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1022 case LYX_ALIGN_RIGHT:
1023 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1024 // + getAdditionalWidth(cell);
1027 // LYX_ALIGN_LEFT: nothing :-)
1031 // the LaTeX Way :-(
1037 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1039 return column_of_cell(cell) == 0;
1043 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1045 if (row > rows_ - 1)
1047 return cell_info[row][0].cellno;
1051 bool LyXTabular::isLastCellInRow(idx_type cell) const
1053 return right_column_of_cell(cell) == columns_ - 1;
1057 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1059 if (row > rows_ - 1)
1061 return cell_info[row][columns_-1].cellno;
1065 void LyXTabular::calculate_width_of_column(col_type column)
1068 for (row_type i = 0; i < rows_; ++i)
1069 maximum = max(cell_info[i][column].width_of_cell, maximum);
1070 column_info[column].width_of_column = maximum;
1075 // Calculate the columns regarding ONLY the normal cells and if this
1076 // column is inside a multicolumn cell then use it only if its the last
1077 // column of this multicolumn cell as this gives an added width to the
1078 // column, all the rest should be adapted!
1080 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1082 int const old_column_width = column_info[column].width_of_column;
1084 for (row_type i = 0; i < rows_; ++i) {
1085 idx_type cell = getCellNumber(i, column);
1086 bool ismulti = isMultiColumnReal(cell);
1087 if ((!ismulti || column == right_column_of_cell(cell)) &&
1088 cell_info[i][column].width_of_cell > max)
1090 max = cell_info[i][column].width_of_cell;
1093 column_info[column].width_of_column = max;
1094 return column_info[column].width_of_column != old_column_width;
1098 void LyXTabular::calculate_width_of_tabular()
1100 width_of_tabular = 0;
1101 for (col_type i = 0; i < columns_; ++i)
1102 width_of_tabular += column_info[i].width_of_column;
1106 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1108 if (cell >= numberofcells)
1112 return rowofcell[cell];
1116 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1118 if (cell >= numberofcells)
1119 return columns_ - 1;
1122 return columnofcell[cell];
1126 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1128 row_type const row = row_of_cell(cell);
1129 col_type column = column_of_cell(cell);
1130 while (column < columns_ - 1 &&
1131 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1137 void LyXTabular::write(Buffer const & buf, ostream & os) const
1141 << write_attribute("version", 3)
1142 << write_attribute("rows", rows_)
1143 << write_attribute("columns", columns_)
1145 // global longtable options
1147 << write_attribute("rotate", rotate)
1148 << write_attribute("islongtable", is_long_tabular)
1149 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1150 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1151 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1152 << write_attribute("headTopDL", endhead.topDL)
1153 << write_attribute("headBottomDL", endhead.bottomDL)
1154 << write_attribute("footTopDL", endfoot.topDL)
1155 << write_attribute("footBottomDL", endfoot.bottomDL)
1156 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1157 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1158 << write_attribute("lastFootEmpty", endlastfoot.empty)
1160 for (col_type j = 0; j < columns_; ++j) {
1162 << write_attribute("alignment", column_info[j].alignment)
1163 << write_attribute("valignment", column_info[j].valignment)
1164 << write_attribute("leftline", column_info[j].left_line)
1165 << write_attribute("rightline", column_info[j].right_line)
1166 << write_attribute("width", column_info[j].p_width.asString())
1167 << write_attribute("special", column_info[j].align_special)
1170 for (row_type i = 0; i < rows_; ++i) {
1172 << write_attribute("topline", row_info[i].top_line)
1173 << write_attribute("bottomline", row_info[i].bottom_line)
1174 << write_attribute("endhead", row_info[i].endhead)
1175 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1176 << write_attribute("endfoot", row_info[i].endfoot)
1177 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1178 << write_attribute("newpage", row_info[i].newpage)
1180 for (col_type j = 0; j < columns_; ++j) {
1182 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1183 << write_attribute("alignment", cell_info[i][j].alignment)
1184 << write_attribute("valignment", cell_info[i][j].valignment)
1185 << write_attribute("topline", cell_info[i][j].top_line)
1186 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1187 << write_attribute("leftline", cell_info[i][j].left_line)
1188 << write_attribute("rightline", cell_info[i][j].right_line)
1189 << write_attribute("rotate", cell_info[i][j].rotate)
1190 << write_attribute("usebox", cell_info[i][j].usebox)
1191 << write_attribute("width", cell_info[i][j].p_width)
1192 << write_attribute("special", cell_info[i][j].align_special)
1194 os << "\\begin_inset ";
1195 cell_info[i][j].inset->write(buf, os);
1196 os << "\n\\end_inset\n"
1201 os << "</lyxtabular>\n";
1205 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1208 istream & is = lex.getStream();
1210 l_getline(is, line);
1211 if (!prefixIs(line, "<lyxtabular ")
1212 && !prefixIs(line, "<LyXTabular ")) {
1213 BOOST_ASSERT(false);
1218 if (!getTokenValue(line, "version", version))
1220 BOOST_ASSERT(version >= 2);
1223 if (!getTokenValue(line, "rows", rows_arg))
1226 if (!getTokenValue(line, "columns", columns_arg))
1228 init(buf.params(), rows_arg, columns_arg);
1229 l_getline(is, line);
1230 if (!prefixIs(line, "<features")) {
1231 lyxerr << "Wrong tabular format (expected <features ...> got"
1232 << line << ')' << endl;
1235 getTokenValue(line, "rotate", rotate);
1236 getTokenValue(line, "islongtable", is_long_tabular);
1237 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1238 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1239 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1240 getTokenValue(line, "headTopDL", endhead.topDL);
1241 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1242 getTokenValue(line, "footTopDL", endfoot.topDL);
1243 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1244 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1245 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1246 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1248 for (col_type j = 0; j < columns_; ++j) {
1250 if (!prefixIs(line,"<column")) {
1251 lyxerr << "Wrong tabular format (expected <column ...> got"
1252 << line << ')' << endl;
1255 getTokenValue(line, "alignment", column_info[j].alignment);
1256 getTokenValue(line, "valignment", column_info[j].valignment);
1257 getTokenValue(line, "leftline", column_info[j].left_line);
1258 getTokenValue(line, "rightline", column_info[j].right_line);
1259 getTokenValue(line, "width", column_info[j].p_width);
1260 getTokenValue(line, "special", column_info[j].align_special);
1263 for (row_type i = 0; i < rows_; ++i) {
1264 l_getline(is, line);
1265 if (!prefixIs(line, "<row")) {
1266 lyxerr << "Wrong tabular format (expected <row ...> got"
1267 << line << ')' << endl;
1270 getTokenValue(line, "topline", row_info[i].top_line);
1271 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1272 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1273 getTokenValue(line, "endhead", row_info[i].endhead);
1274 getTokenValue(line, "endfoot", row_info[i].endfoot);
1275 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1276 getTokenValue(line, "newpage", row_info[i].newpage);
1277 for (col_type j = 0; j < columns_; ++j) {
1278 l_getline(is, line);
1279 if (!prefixIs(line, "<cell")) {
1280 lyxerr << "Wrong tabular format (expected <cell ...> got"
1281 << line << ')' << endl;
1284 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1285 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1286 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1287 getTokenValue(line, "topline", cell_info[i][j].top_line);
1288 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1289 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1290 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1291 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1292 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1293 getTokenValue(line, "width", cell_info[i][j].p_width);
1294 getTokenValue(line, "special", cell_info[i][j].align_special);
1295 l_getline(is, line);
1296 if (prefixIs(line, "\\begin_inset")) {
1297 cell_info[i][j].inset->read(buf, lex);
1298 l_getline(is, line);
1300 if (!prefixIs(line, "</cell>")) {
1301 lyxerr << "Wrong tabular format (expected </cell> got"
1302 << line << ')' << endl;
1306 l_getline(is, line);
1307 if (!prefixIs(line, "</row>")) {
1308 lyxerr << "Wrong tabular format (expected </row> got"
1309 << line << ')' << endl;
1313 while (!prefixIs(line, "</lyxtabular>")) {
1314 l_getline(is, line);
1316 set_row_column_number_info();
1320 bool LyXTabular::isMultiColumn(idx_type cell) const
1322 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1326 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1328 return column_of_cell(cell) != right_column_of_cell(cell) &&
1329 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1333 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1335 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1339 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1342 cellstruct & cs = cellinfo_of_cell(cell);
1343 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1344 cs.alignment = column_info[column_of_cell(cell)].alignment;
1345 cs.top_line = row_info[row_of_cell(cell)].top_line;
1346 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1347 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1348 for (idx_type i = 1; i < number; ++i) {
1349 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1350 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1351 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1352 cs1.inset->clear(false);
1354 set_row_column_number_info();
1358 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1360 row_type const row = row_of_cell(cell);
1361 col_type column = column_of_cell(cell);
1362 idx_type result = 1;
1364 while (column < columns_ &&
1365 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1374 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1376 row_type const row = row_of_cell(cell);
1377 col_type column = column_of_cell(cell);
1379 idx_type result = 0;
1381 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1382 cell_info[row][column].multicolumn = CELL_NORMAL;
1384 while (column < columns_ &&
1385 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1387 cell_info[row][column].multicolumn = CELL_NORMAL;
1392 set_row_column_number_info();
1397 void LyXTabular::setLongTabular(bool what)
1399 is_long_tabular = what;
1403 bool LyXTabular::isLongTabular() const
1405 return is_long_tabular;
1409 void LyXTabular::setRotateTabular(bool flag)
1415 bool LyXTabular::getRotateTabular() const
1421 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1423 cellinfo_of_cell(cell).rotate = flag;
1427 bool LyXTabular::getRotateCell(idx_type cell) const
1429 return cellinfo_of_cell(cell).rotate;
1433 bool LyXTabular::needRotating() const
1437 for (row_type i = 0; i < rows_; ++i)
1438 for (col_type j = 0; j < columns_; ++j)
1439 if (cell_info[i][j].rotate)
1445 bool LyXTabular::isLastCell(idx_type cell) const
1447 if (cell + 1 < numberofcells)
1453 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1455 if (row_of_cell(cell) > 0)
1456 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1461 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1463 if (row_of_cell(cell) + 1 < rows_)
1464 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1469 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1471 if (row_of_cell(cell) == 0)
1473 if (!isMultiColumn(cell))
1474 return getCellAbove(cell);
1475 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1479 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1481 if (row_of_cell(cell) + 1 >= rows_)
1483 if (!isMultiColumn(cell))
1484 return getCellBelow(cell);
1485 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1489 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1490 col_type column) const
1492 BOOST_ASSERT(column != npos && column < columns_ &&
1493 row != npos && row < rows_);
1494 return cell_info[row][column].cellno;
1498 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1500 cellinfo_of_cell(cell).usebox = type;
1504 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1506 if (column_info[column_of_cell(cell)].p_width.zero() &&
1507 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1509 if (cellinfo_of_cell(cell).usebox > 1)
1510 return cellinfo_of_cell(cell).usebox;
1511 return useParbox(cell);
1516 // This are functions used for the longtable support
1518 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1524 row_info[row].endfirsthead = flag;
1528 row_info[row].endhead = flag;
1533 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1536 hd.set = haveLTHead();
1537 return row_info[row].endhead;
1541 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1544 hd.set = haveLTFirstHead();
1545 return row_info[row].endfirsthead;
1549 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1555 row_info[row].endlastfoot = flag;
1559 row_info[row].endfoot = flag;
1564 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1567 fd.set = haveLTFoot();
1568 return row_info[row].endfoot;
1572 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1575 fd.set = haveLTLastFoot();
1576 return row_info[row].endlastfoot;
1580 void LyXTabular::setLTNewPage(row_type row, bool what)
1582 row_info[row].newpage = what;
1586 bool LyXTabular::getLTNewPage(row_type row) const
1588 return row_info[row].newpage;
1592 bool LyXTabular::haveLTHead() const
1594 for (row_type i = 0; i < rows_; ++i)
1595 if (row_info[i].endhead)
1601 bool LyXTabular::haveLTFirstHead() const
1603 if (endfirsthead.empty)
1605 for (row_type i = 0; i < rows_; ++i)
1606 if (row_info[i].endfirsthead)
1612 bool LyXTabular::haveLTFoot() const
1614 for (row_type i = 0; i < rows_; ++i)
1615 if (row_info[i].endfoot)
1621 bool LyXTabular::haveLTLastFoot() const
1623 if (endlastfoot.empty)
1625 for (row_type i = 0; i < rows_; ++i)
1626 if (row_info[i].endlastfoot)
1632 // end longtable support functions
1634 void LyXTabular::setAscentOfRow(row_type row, int height)
1636 if (row >= rows_ || row_info[row].ascent_of_row == height)
1638 row_info[row].ascent_of_row = height;
1642 void LyXTabular::setDescentOfRow(row_type row, int height)
1644 if (row >= rows_ || row_info[row].descent_of_row == height)
1646 row_info[row].descent_of_row = height;
1650 int LyXTabular::getAscentOfRow(row_type row) const
1654 return row_info[row].ascent_of_row;
1658 int LyXTabular::getDescentOfRow(row_type row) const
1660 BOOST_ASSERT(row < rows_);
1661 return row_info[row].descent_of_row;
1665 int LyXTabular::getHeightOfTabular() const
1668 for (row_type row = 0; row < rows_; ++row)
1669 height += getAscentOfRow(row) + getDescentOfRow(row) +
1670 getAdditionalHeight(row);
1675 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1677 BOOST_ASSERT(row < rows_);
1678 BOOST_ASSERT(column < columns_);
1679 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1683 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1685 // FIXME: assert or return 0 as in TeXBottomHLine()?
1686 BOOST_ASSERT(row != npos);
1687 BOOST_ASSERT(row < rows_);
1689 idx_type const fcell = getFirstCellInRow(row);
1690 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1693 for (idx_type i = fcell; i < n; ++i) {
1697 if (tmp == n - fcell) {
1700 for (idx_type i = fcell; i < n; ++i) {
1703 << column_of_cell(i) + 1
1705 << right_column_of_cell(i) + 1
1717 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1719 // FIXME: return 0 or assert as in TeXTopHLine()?
1720 if (row == npos || row >= rows_)
1723 idx_type const fcell = getFirstCellInRow(row);
1724 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1727 for (idx_type i = fcell; i < n; ++i) {
1731 if (tmp == n - fcell) {
1734 for (idx_type i = fcell; i < n; ++i) {
1735 if (bottomLine(i)) {
1737 << column_of_cell(i) + 1
1739 << right_column_of_cell(i) + 1
1751 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1755 if (getRotateCell(cell)) {
1756 os << "\\begin{sideways}\n";
1759 if (isMultiColumn(cell)) {
1760 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1761 if (!cellinfo_of_cell(cell).align_special.empty()) {
1762 os << cellinfo_of_cell(cell).align_special << "}{";
1764 if (leftLine(cell) &&
1765 (isFirstCellInRow(cell) ||
1766 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1767 !rightLine(cell - 1, true))))
1771 if (!getPWidth(cell).zero()) {
1772 switch (getVAlignment(cell)) {
1773 case LYX_VALIGN_TOP:
1776 case LYX_VALIGN_MIDDLE:
1779 case LYX_VALIGN_BOTTOM:
1784 << getPWidth(cell).asLatexString()
1787 switch (getAlignment(cell)) {
1788 case LYX_ALIGN_LEFT:
1791 case LYX_ALIGN_RIGHT:
1799 if (rightLine(cell))
1801 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1807 if (getUsebox(cell) == BOX_PARBOX) {
1809 switch (getVAlignment(cell)) {
1810 case LYX_VALIGN_TOP:
1813 case LYX_VALIGN_MIDDLE:
1816 case LYX_VALIGN_BOTTOM:
1820 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1821 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1822 os << "\\begin{minipage}[";
1823 switch (getVAlignment(cell)) {
1824 case LYX_VALIGN_TOP:
1827 case LYX_VALIGN_MIDDLE:
1830 case LYX_VALIGN_BOTTOM:
1834 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1841 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1846 if (getUsebox(cell) == BOX_PARBOX)
1848 else if (getUsebox(cell) == BOX_MINIPAGE) {
1849 os << "%\n\\end{minipage}";
1852 if (isMultiColumn(cell)) {
1855 if (getRotateCell(cell)) {
1856 os << "%\n\\end{sideways}";
1863 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1864 OutputParams const & runparams) const
1866 if (!is_long_tabular)
1870 // output header info
1872 if (endhead.topDL) {
1876 for (row_type i = 0; i < rows_; ++i) {
1877 if (row_info[i].endhead) {
1878 ret += TeXRow(os, i, buf, runparams);
1881 if (endhead.bottomDL) {
1885 os << "\\endhead\n";
1887 if (endfirsthead.empty) {
1888 os << "\\endfirsthead\n";
1892 // output firstheader info
1893 if (haveLTFirstHead()) {
1894 if (endfirsthead.topDL) {
1898 for (row_type i = 0; i < rows_; ++i) {
1899 if (row_info[i].endfirsthead) {
1900 ret += TeXRow(os, i, buf, runparams);
1903 if (endfirsthead.bottomDL) {
1907 os << "\\endfirsthead\n";
1910 // output footer info
1912 if (endfoot.topDL) {
1916 for (row_type i = 0; i < rows_; ++i) {
1917 if (row_info[i].endfoot) {
1918 ret += TeXRow(os, i, buf, runparams);
1921 if (endfoot.bottomDL) {
1925 os << "\\endfoot\n";
1927 if (endlastfoot.empty) {
1928 os << "\\endlastfoot\n";
1932 // output lastfooter info
1933 if (haveLTLastFoot()) {
1934 if (endlastfoot.topDL) {
1938 for (row_type i = 0; i < rows_; ++i) {
1939 if (row_info[i].endlastfoot) {
1940 ret += TeXRow(os, i, buf, runparams);
1943 if (endlastfoot.bottomDL) {
1947 os << "\\endlastfoot\n";
1954 bool LyXTabular::isValidRow(row_type row) const
1956 if (!is_long_tabular)
1958 return !row_info[row].endhead && !row_info[row].endfirsthead &&
1959 !row_info[row].endfoot && !row_info[row].endlastfoot;
1963 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
1964 OutputParams const & runparams) const
1966 idx_type cell = getCellNumber(i, 0);
1968 int ret = TeXTopHLine(os, i);
1969 for (col_type j = 0; j < columns_; ++j) {
1970 if (isPartOfMultiColumn(i, j))
1972 ret += TeXCellPreamble(os, cell);
1973 shared_ptr<InsetText> inset = getCellInset(cell);
1975 Paragraph const & par = inset->paragraphs().front();
1976 bool rtl = par.isRightToLeftPar(buf.params())
1978 && getPWidth(cell).zero();
1982 ret += inset->latex(buf, os, runparams);
1986 ret += TeXCellPostamble(os, cell);
1987 if (!isLastCellInRow(cell)) { // not last cell in row
1993 os << "\\tabularnewline\n";
1995 ret += TeXBottomHLine(os, i);
2000 int LyXTabular::latex(Buffer const & buf, ostream & os,
2001 OutputParams const & runparams) const
2005 //+---------------------------------------------------------------------
2006 //+ first the opening preamble +
2007 //+---------------------------------------------------------------------
2010 os << "\\begin{sideways}\n";
2013 if (is_long_tabular)
2014 os << "\\begin{longtable}{";
2016 os << "\\begin{tabular}{";
2017 for (col_type i = 0; i < columns_; ++i) {
2018 if (!column_info[i].align_special.empty()) {
2019 os << column_info[i].align_special;
2021 if (column_info[i].left_line)
2023 if (!column_info[i].p_width.zero()) {
2024 switch (column_info[i].alignment) {
2025 case LYX_ALIGN_LEFT:
2026 os << ">{\\raggedright}";
2028 case LYX_ALIGN_RIGHT:
2029 os << ">{\\raggedleft}";
2031 case LYX_ALIGN_CENTER:
2032 os << ">{\\centering}";
2034 case LYX_ALIGN_NONE:
2035 case LYX_ALIGN_BLOCK:
2036 case LYX_ALIGN_LAYOUT:
2037 case LYX_ALIGN_SPECIAL:
2041 switch (column_info[i].valignment) {
2042 case LYX_VALIGN_TOP:
2045 case LYX_VALIGN_MIDDLE:
2048 case LYX_VALIGN_BOTTOM:
2053 << column_info[i].p_width.asLatexString()
2056 switch (column_info[i].alignment) {
2057 case LYX_ALIGN_LEFT:
2060 case LYX_ALIGN_RIGHT:
2068 if (column_info[i].right_line)
2075 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2077 //+---------------------------------------------------------------------
2078 //+ the single row and columns (cells) +
2079 //+---------------------------------------------------------------------
2081 for (row_type i = 0; i < rows_; ++i) {
2082 if (isValidRow(i)) {
2083 ret += TeXRow(os, i, buf, runparams);
2084 if (is_long_tabular && row_info[i].newpage) {
2085 os << "\\newpage\n";
2091 //+---------------------------------------------------------------------
2092 //+ the closing of the tabular +
2093 //+---------------------------------------------------------------------
2095 if (is_long_tabular)
2096 os << "\\end{longtable}";
2098 os << "\\end{tabular}";
2100 os << "\n\\end{sideways}";
2108 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2109 const OutputParams & runparams) const
2111 os << "<tabular ca=\"";
2112 for (col_type i = 0; i < columns_; ++i) {
2113 switch (column_info[i].alignment) {
2114 case LYX_ALIGN_LEFT:
2117 case LYX_ALIGN_RIGHT:
2128 for (row_type i = 0; i < rows_; ++i) {
2129 for (col_type j = 0; j < columns_; ++j) {
2130 if (isPartOfMultiColumn(i, j))
2132 shared_ptr<InsetText> inset = getCellInset(cell);
2134 ret += inset->linuxdoc(buf, os, runparams);
2136 if (isLastCellInRow(cell)) {
2145 os << "</tabular>\n";
2150 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2151 OutputParams const & runparams) const
2154 idx_type cell = getFirstCellInRow(row);
2157 for (col_type j = 0; j < columns_; ++j) {
2158 if (isPartOfMultiColumn(row, j))
2161 os << "<entry align=\"";
2162 switch (getAlignment(cell)) {
2163 case LYX_ALIGN_LEFT:
2166 case LYX_ALIGN_RIGHT:
2174 os << "\" valign=\"";
2175 switch (getVAlignment(cell)) {
2176 case LYX_VALIGN_TOP:
2179 case LYX_VALIGN_BOTTOM:
2182 case LYX_VALIGN_MIDDLE:
2187 if (isMultiColumn(cell)) {
2188 os << " namest=\"col" << j << "\" ";
2189 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2193 ret += getCellInset(cell)->docbook(buf, os, runparams);
2202 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2203 OutputParams const & runparams) const
2207 //+---------------------------------------------------------------------
2208 //+ first the opening preamble +
2209 //+---------------------------------------------------------------------
2211 os << "<tgroup cols=\"" << columns_
2212 << "\" colsep=\"1\" rowsep=\"1\">\n";
2214 for (col_type i = 0; i < columns_; ++i) {
2215 os << "<colspec colname=\"col" << i << "\" align=\"";
2216 switch (column_info[i].alignment) {
2217 case LYX_ALIGN_LEFT:
2220 case LYX_ALIGN_RIGHT:
2228 if (runparams.flavor == OutputParams::XML)
2234 //+---------------------------------------------------------------------
2235 //+ Long Tabular case +
2236 //+---------------------------------------------------------------------
2238 // output header info
2239 if (haveLTHead() || haveLTFirstHead()) {
2242 for (row_type i = 0; i < rows_; ++i) {
2243 if (row_info[i].endhead || row_info[i].endfirsthead) {
2244 ret += docbookRow(buf, os, i, runparams);
2250 // output footer info
2251 if (haveLTFoot() || haveLTLastFoot()) {
2254 for (row_type i = 0; i < rows_; ++i) {
2255 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2256 ret += docbookRow(buf, os, i, runparams);
2263 //+---------------------------------------------------------------------
2264 //+ the single row and columns (cells) +
2265 //+---------------------------------------------------------------------
2269 for (row_type i = 0; i < rows_; ++i) {
2270 if (isValidRow(i)) {
2271 ret += docbookRow(buf, os, i, runparams);
2276 //+---------------------------------------------------------------------
2277 //+ the closing of the tabular +
2278 //+---------------------------------------------------------------------
2287 int LyXTabular::asciiTopHLine(ostream & os, row_type row,
2288 vector<unsigned int> const & clen) const
2290 idx_type const fcell = getFirstCellInRow(row);
2291 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2294 for (idx_type i = fcell; i < n; ++i) {
2304 for (idx_type i = fcell; i < n; ++i) {
2315 col_type column = column_of_cell(i);
2316 int len = clen[column];
2317 while (column < columns_ - 1
2318 && isPartOfMultiColumn(row, ++column))
2319 len += clen[column] + 4;
2320 os << string(len, ch);
2335 int LyXTabular::asciiBottomHLine(ostream & os, row_type row,
2336 vector<unsigned int> const & clen) const
2338 idx_type const fcell = getFirstCellInRow(row);
2339 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2342 for (idx_type i = fcell; i < n; ++i) {
2343 if (bottomLine(i)) {
2352 for (idx_type i = fcell; i < n; ++i) {
2353 if (bottomLine(i)) {
2363 col_type column = column_of_cell(i);
2364 int len = clen[column];
2365 while (column < columns_ -1
2366 && isPartOfMultiColumn(row, ++column))
2367 len += clen[column] + 4;
2368 os << string(len, ch);
2369 if (bottomLine(i)) {
2383 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2384 OutputParams const & runparams,
2385 idx_type cell, row_type row, col_type column,
2386 vector<unsigned int> const & clen,
2387 bool onlydata) const
2390 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2402 unsigned int len1 = sstr.str().length();
2403 unsigned int len2 = clen[column];
2404 while (column < columns_ -1
2405 && isPartOfMultiColumn(row, ++column))
2406 len2 += clen[column] + 4;
2409 switch (getAlignment(cell)) {
2411 case LYX_ALIGN_LEFT:
2414 case LYX_ALIGN_RIGHT:
2418 case LYX_ALIGN_CENTER:
2424 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2426 if (rightLine(cell))
2435 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2436 OutputParams const & runparams,
2438 bool onlydata, unsigned char delim) const
2442 // first calculate the width of the single columns
2443 vector<unsigned int> clen(columns_);
2446 // first all non (real) multicolumn cells!
2447 for (col_type j = 0; j < columns_; ++j) {
2449 for (row_type i = 0; i < rows_; ++i) {
2450 idx_type cell = getCellNumber(i, j);
2451 if (isMultiColumnReal(cell))
2454 getCellInset(cell)->plaintext(buf, sstr, runparams);
2455 if (clen[j] < sstr.str().length())
2456 clen[j] = sstr.str().length();
2459 // then all (real) multicolumn cells!
2460 for (col_type j = 0; j < columns_; ++j) {
2461 for (row_type i = 0; i < rows_; ++i) {
2462 idx_type cell = getCellNumber(i, j);
2463 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2466 getCellInset(cell)->plaintext(buf, sstr, runparams);
2467 int len = int(sstr.str().length());
2468 idx_type const n = cells_in_multicolumn(cell);
2469 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2471 if (len > int(clen[j + n - 1]))
2472 clen[j + n - 1] = len;
2477 for (row_type i = 0; i < rows_; ++i) {
2478 if (!onlydata && asciiTopHLine(os, i, clen))
2479 os << string(depth * 2, ' ');
2480 for (col_type j = 0; j < columns_; ++j) {
2481 if (isPartOfMultiColumn(i, j))
2483 if (onlydata && j > 0)
2485 ret += asciiPrintCell(buf, os, runparams,
2486 cell, i, j, clen, onlydata);
2491 os << string(depth * 2, ' ');
2492 if (asciiBottomHLine(os, i, clen))
2493 os << string(depth * 2, ' ');
2500 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2502 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2506 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2507 col_type column) const
2509 return cell_info[row][column].inset;
2513 void LyXTabular::setCellInset(row_type row, col_type column,
2514 shared_ptr<InsetText> ins) const
2516 cell_info[row][column].inset = ins;
2520 LyXTabular::idx_type
2521 LyXTabular::getCellFromInset(InsetBase const * inset) const
2523 // is this inset part of the tabular?
2525 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2526 BOOST_ASSERT(false);
2529 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2530 if (getCellInset(cell).get() == inset) {
2531 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2532 << "cell=" << cell << endl;
2536 // We should have found a cell at this point
2537 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2538 << inset << " not found!" << endl;
2539 BOOST_ASSERT(false);
2545 void LyXTabular::validate(LaTeXFeatures & features) const
2547 features.require("NeedTabularnewline");
2548 if (isLongTabular())
2549 features.require("longtable");
2551 features.require("rotating");
2552 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2553 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2554 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2555 features.require("array");
2556 getCellInset(cell)->validate(features);
2561 void LyXTabular::getLabelList(Buffer const & buffer,
2562 std::vector<string> & list) const
2564 for (row_type i = 0; i < rows_; ++i)
2565 for (col_type j = 0; j < columns_; ++j)
2566 getCellInset(i, j)->getLabelList(buffer, list);
2570 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2572 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2573 ParagraphList::const_iterator cit = parlist.begin();
2574 ParagraphList::const_iterator end = parlist.end();
2576 for (; cit != end; ++cit)
2577 for (int i = 0; i < cit->size(); ++i)
2578 if (cit->isNewline(i))