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/tostr.h"
37 using lyx::support::ltrim;
38 using lyx::support::prefixIs;
39 using lyx::support::rtrim;
40 using lyx::support::strToInt;
41 using lyx::support::suffixIs;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
53 using std::ostringstream;
57 #ifndef CXX_GLOBAL_CSTD
64 int const WIDTH_OF_LINE = 5;
67 string const write_attribute(string const & name, T const & t)
69 string const s = tostr(t);
70 return s.empty() ? s : " " + name + "=\"" + s + "\"";
73 string const write_attribute(string const & name, string const & t)
75 return t.empty() ? t : " " + name + "=\"" + t + "\"";
79 string const write_attribute(string const & name, bool const & b)
81 // we write only true attribute values so we remove a bit of the
82 // file format bloat for tabulars.
83 return b ? write_attribute(name, tostr(b)) : string();
87 string const write_attribute(string const & name, int const & i)
89 // we write only true attribute values so we remove a bit of the
90 // file format bloat for tabulars.
91 return i ? write_attribute(name, tostr(i)) : string();
95 string const write_attribute(string const & name, LyXTabular::idx_type const & i)
97 // we write only true attribute values so we remove a bit of the
98 // file format bloat for tabulars.
99 return i ? write_attribute(name, tostr(i)) : string();
103 string const write_attribute(string const & name, LyXLength const & value)
105 // we write only the value if we really have one same reson as above.
106 return value.zero() ? string() : write_attribute(name, value.asString());
110 string const tostr(LyXAlignment const & num)
115 case LYX_ALIGN_BLOCK:
119 case LYX_ALIGN_CENTER:
121 case LYX_ALIGN_RIGHT:
123 case LYX_ALIGN_LAYOUT:
125 case LYX_ALIGN_SPECIAL:
132 string const tostr(LyXTabular::VAlignment const & num)
135 case LyXTabular::LYX_VALIGN_TOP:
137 case LyXTabular::LYX_VALIGN_MIDDLE:
139 case LyXTabular::LYX_VALIGN_BOTTOM:
146 string const tostr(LyXTabular::BoxType const & num)
149 case LyXTabular::BOX_NONE:
151 case LyXTabular::BOX_PARBOX:
153 case LyXTabular::BOX_MINIPAGE:
160 // I would have liked a fromstr template a lot better. (Lgb)
161 bool string2type(string const str, LyXAlignment & num)
164 num = LYX_ALIGN_NONE;
165 else if (str == "block")
166 num = LYX_ALIGN_BLOCK;
167 else if (str == "left")
168 num = LYX_ALIGN_LEFT;
169 else if (str == "center")
170 num = LYX_ALIGN_CENTER;
171 else if (str == "right")
172 num = LYX_ALIGN_RIGHT;
179 bool string2type(string const str, LyXTabular::VAlignment & num)
182 num = LyXTabular::LYX_VALIGN_TOP;
183 else if (str == "middle" )
184 num = LyXTabular::LYX_VALIGN_MIDDLE;
185 else if (str == "bottom")
186 num = LyXTabular::LYX_VALIGN_BOTTOM;
193 bool string2type(string const str, LyXTabular::BoxType & num)
196 num = LyXTabular::BOX_NONE;
197 else if (str == "parbox")
198 num = LyXTabular::BOX_PARBOX;
199 else if (str == "minipage")
200 num = LyXTabular::BOX_MINIPAGE;
207 bool string2type(string const str, bool & num)
211 else if (str == "false")
219 bool getTokenValue(string const & str, char const * token, string & ret)
222 size_t token_length = strlen(token);
223 string::size_type pos = str.find(token);
225 if (pos == string::npos || pos + token_length + 1 >= str.length()
226 || str[pos + token_length] != '=')
228 pos += token_length + 1;
230 if (ch != '"' && ch != '\'') { // only read till next space
234 while (pos < str.length() - 1 && str[++pos] != ch)
241 bool getTokenValue(string const & str, char const * token, int & num)
245 if (!getTokenValue(str, token, tmp))
252 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
255 return getTokenValue(str, token, tmp) && string2type(tmp, num);
259 bool getTokenValue(string const & str, char const * token,
260 LyXTabular::VAlignment & num)
263 return getTokenValue(str, token, tmp) && string2type(tmp, num);
267 bool getTokenValue(string const & str, char const * token,
268 LyXTabular::BoxType & num)
271 return getTokenValue(str, token, tmp) && string2type(tmp, num);
275 bool getTokenValue(string const & str, char const * token, bool & flag)
277 // set the flag always to false as this should be the default for bools
278 // not in the file-format.
281 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
285 bool getTokenValue(string const & str, char const * token, LyXLength & len)
287 // set the lenght to be zero() as default as this it should be if not
288 // in the file format.
291 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
295 void l_getline(istream & is, string & str)
298 while (str.empty()) {
300 if (!str.empty() && str[str.length() - 1] == '\r')
301 str.erase(str.length() - 1);
307 /// Define a few methods for the inner structs
309 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
312 multicolumn(LyXTabular::CELL_NORMAL),
313 alignment(LYX_ALIGN_CENTER),
314 valignment(LYX_VALIGN_TOP),
321 inset(new InsetText(bp))
325 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
327 width_of_cell(cs.width_of_cell),
328 multicolumn(cs.multicolumn),
329 alignment(cs.alignment),
330 valignment(cs.valignment),
331 top_line(cs.top_line),
332 bottom_line(cs.bottom_line),
333 left_line(cs.left_line),
334 right_line(cs.right_line),
337 align_special(cs.align_special),
339 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
343 LyXTabular::cellstruct &
344 LyXTabular::cellstruct::operator=(cellstruct cs)
352 LyXTabular::cellstruct::swap(cellstruct & rhs)
354 std::swap(cellno, rhs.cellno);
355 std::swap(width_of_cell, rhs.width_of_cell);
356 std::swap(multicolumn, rhs.multicolumn);
357 std::swap(alignment, rhs.alignment);
358 std::swap(valignment, rhs.valignment);
359 std::swap(top_line, rhs.top_line);
360 std::swap(bottom_line, rhs.bottom_line);
361 std::swap(left_line, rhs.left_line);
362 std::swap(right_line, rhs.right_line);
363 std::swap(usebox, rhs.usebox);
364 std::swap(rotate, rhs.rotate);
365 std::swap(align_special, rhs.align_special);
366 p_width.swap(rhs.p_width);
367 inset.swap(rhs.inset);
371 LyXTabular::rowstruct::rowstruct()
384 LyXTabular::columnstruct::columnstruct()
385 : alignment(LYX_ALIGN_CENTER),
386 valignment(LYX_VALIGN_TOP),
394 LyXTabular::ltType::ltType()
401 LyXTabular::LyXTabular(BufferParams const & bp, row_type rows_arg,
402 col_type columns_arg)
404 init(bp, rows_arg, columns_arg);
408 // activates all lines and sets all widths to 0
409 void LyXTabular::init(BufferParams const & bp, row_type rows_arg,
410 col_type columns_arg)
413 columns_ = columns_arg;
414 row_info = row_vector(rows_);
415 column_info = column_vector(columns_);
416 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
417 row_info.reserve(10);
418 column_info.reserve(10);
419 cell_info.reserve(100);
421 for (row_type i = 0; i < rows_; ++i)
422 cell_info[i].back().right_line = true;
423 row_info.back().bottom_line = true;
424 row_info.front().bottom_line = true;
425 column_info.back().right_line = true;
426 is_long_tabular = false;
431 void LyXTabular::fixCellNums()
434 for (row_type i = 0; i < rows_; ++i) {
435 for (col_type j = 0; j < columns_; ++j) {
436 // When debugging it can be nice to set
438 cell_info[i][j].inset->setDrawFrame(false);
439 cell_info[i][j].cellno = cellno++;
441 cell_info[i].back().right_line = true;
444 set_row_column_number_info();
448 void LyXTabular::appendRow(BufferParams const & bp, idx_type const cell)
452 row_type const row = row_of_cell(cell);
454 row_vector::iterator rit = row_info.begin() + row;
455 row_info.insert(rit, rowstruct());
456 // now set the values of the row before
457 row_info[row] = row_info[row + 1];
459 cell_vvector old(rows_ - 1);
460 for (row_type i = 0; i < rows_ - 1; ++i)
461 swap(cell_info[i], old[i]);
463 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
465 for (row_type i = 0; i <= row; ++i)
466 swap(cell_info[i], old[i]);
467 for (row_type i = row + 2; i < rows_; ++i)
468 swap(cell_info[i], old[i - 1]);
470 if (bp.tracking_changes)
471 for (col_type j = 0; j < columns_; ++j)
472 cell_info[row + 1][j].inset->markNew(true);
474 set_row_column_number_info();
478 void LyXTabular::deleteRow(row_type const row)
480 // Not allowed to delete last row
484 row_info.erase(row_info.begin() + row);
485 cell_info.erase(cell_info.begin() + row);
491 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
495 col_type const column = column_of_cell(cell);
496 column_vector::iterator cit = column_info.begin() + column + 1;
497 column_info.insert(cit, columnstruct());
498 // set the column values of the column before
499 column_info[column + 1] = column_info[column];
501 for (row_type i = 0; i < rows_; ++i) {
502 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
504 // care about multicolumns
505 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
506 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
508 if (column + 2 >= columns_
509 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
510 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
513 for (row_type i = 0; i < rows_; ++i) {
514 cell_info[i][column + 1].inset->clear(false);
515 if (bp.tracking_changes)
516 cell_info[i][column + 1].inset->markNew(true);
522 void LyXTabular::deleteColumn(col_type const column)
524 // Not allowed to delete last column
528 column_info.erase(column_info.begin() + column);
529 for (row_type i = 0; i < rows_; ++i)
530 cell_info[i].erase(cell_info[i].begin() + column);
536 void LyXTabular::set_row_column_number_info()
539 for (row_type row = 0; row < rows_; ++row) {
540 for (col_type column = 0; column < columns_; ++column) {
541 if (cell_info[row][column].multicolumn
542 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
544 if (numberofcells == 0)
545 // FIXME: Is this intended?
546 cell_info[row][column].cellno = npos;
548 cell_info[row][column].cellno =
553 rowofcell.resize(numberofcells);
554 columnofcell.resize(numberofcells);
559 c < numberofcells && row < rows_ && column < columns_;) {
561 columnofcell[c] = column;
565 } while (column < columns_ &&
566 cell_info[row][column].multicolumn
567 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
569 if (column == columns_) {
575 for (row_type row = 0; row < rows_; ++row) {
576 for (col_type column = 0; column < columns_; ++column) {
577 if (isPartOfMultiColumn(row,column))
579 cell_info[row][column].inset->setAutoBreakRows(
580 !getPWidth(getCellNumber(row, column)).zero());
586 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
588 return numberofcells;
592 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
594 row_type const row = row_of_cell(cell);
596 for (col_type i = 0; i < columns_; ++i)
597 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
603 // returns 1 if there is a topline, returns 0 if not
604 bool LyXTabular::topLine(idx_type const cell, bool const onlycolumn) const
606 if (!onlycolumn && isMultiColumn(cell))
607 return cellinfo_of_cell(cell).top_line;
608 return row_info[row_of_cell(cell)].top_line;
612 bool LyXTabular::bottomLine(idx_type const cell, bool onlycolumn) const
614 if (!onlycolumn && isMultiColumn(cell))
615 return cellinfo_of_cell(cell).bottom_line;
616 return row_info[row_of_cell(cell)].bottom_line;
620 bool LyXTabular::leftLine(idx_type cell, bool onlycolumn) const
622 if (!onlycolumn && isMultiColumn(cell) &&
623 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
625 if (cellinfo_of_cell(cell).align_special.empty())
626 return cellinfo_of_cell(cell).left_line;
627 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
629 if (column_info[column_of_cell(cell)].align_special.empty())
630 return column_info[column_of_cell(cell)].left_line;
631 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
635 bool LyXTabular::rightLine(idx_type cell, bool onlycolumn) const
637 if (!onlycolumn && isMultiColumn(cell) &&
638 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
640 if (cellinfo_of_cell(cell).align_special.empty())
641 return cellinfo_of_cell(cell).right_line;
642 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
644 if (column_info[column_of_cell(cell)].align_special.empty())
645 return column_info[right_column_of_cell(cell)].right_line;
646 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
650 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
652 row_type row = row_of_cell(cell);
653 if (row > 0 && !getAdditionalHeight(row)) {
654 col_type column = column_of_cell(cell);
657 && cell_info[row][column].multicolumn
658 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
660 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
661 return row_info[row].bottom_line;
663 return cell_info[row][column].bottom_line;
669 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
671 col_type column = column_of_cell(cell);
673 row_type row = row_of_cell(cell);
675 (cell_info[row][column].multicolumn ==
676 LyXTabular::CELL_PART_OF_MULTICOLUMN));
677 if (getAdditionalWidth(cell_info[row][column].cellno))
679 return rightLine(cell_info[row][column].cellno);
685 bool LyXTabular::isLastRow(idx_type cell) const
687 return row_of_cell(cell) == rows_ - 1;
691 int LyXTabular::getAdditionalHeight(row_type row) const
693 if (!row || row >= rows_)
699 for (col_type column = 0; column < columns_ && bottom; ++column) {
700 switch (cell_info[row - 1][column].multicolumn) {
701 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
702 bottom = cell_info[row - 1][column].bottom_line;
704 case LyXTabular::CELL_NORMAL:
705 bottom = row_info[row - 1].bottom_line;
708 for (col_type column = 0; column < columns_ && top; ++column) {
709 switch (cell_info[row][column].multicolumn) {
710 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
711 top = cell_info[row][column].top_line;
713 case LyXTabular::CELL_NORMAL:
714 top = row_info[row].top_line;
718 return WIDTH_OF_LINE;
723 int LyXTabular::getAdditionalWidth(idx_type cell) const
725 // internally already set in setWidthOfCell
726 // used to get it back in text.C
727 col_type const col = right_column_of_cell(cell);
728 row_type const row = row_of_cell(cell);
729 if (col < columns_ - 1 && rightLine(cell) &&
730 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
732 return WIDTH_OF_LINE;
738 // returns the maximum over all rows
739 int LyXTabular::getWidthOfColumn(idx_type cell) const
741 col_type const column1 = column_of_cell(cell);
742 col_type const column2 = right_column_of_cell(cell);
744 for (col_type i = column1; i <= column2; ++i)
745 result += column_info[i].width_of_column;
750 int LyXTabular::getWidthOfTabular() const
752 return width_of_tabular;
756 // returns true if a complete update is necessary, otherwise false
757 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
759 if (!isMultiColumn(cell))
762 row_type const row = row_of_cell(cell);
763 col_type const column1 = column_of_cell(cell);
764 col_type const column2 = right_column_of_cell(cell);
765 int const old_val = cell_info[row][column2].width_of_cell;
767 // first set columns to 0 so we can calculate the right width
768 for (col_type i = column1; i <= column2; ++i) {
769 cell_info[row][i].width_of_cell = 0;
771 // set the width to MAX_WIDTH until width > 0
772 int width = new_width + 2 * WIDTH_OF_LINE;
773 col_type i = column1;
774 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
775 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
776 width -= column_info[i].width_of_column;
779 cell_info[row][i].width_of_cell = width;
781 if (old_val != cell_info[row][column2].width_of_cell) {
782 // in this case we have to recalculate all multicolumn cells which
783 // have this column as one of theirs but not as last one
784 calculate_width_of_column_NMC(i);
785 recalculateMulticolumnsOfColumn(i);
786 calculate_width_of_column(i);
792 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
794 // the last column does not have to be recalculated because all
795 // multicolumns will have here there last multicolumn cell which
796 // always will have the whole rest of the width of the cell.
797 if (column > (columns_ - 2))
799 for(row_type row = 0; row < rows_; ++row) {
800 int mc = cell_info[row][column].multicolumn;
801 int nmc = cell_info[row][column+1].multicolumn;
802 // we only have to update multicolumns which do not have this
803 // column as their last column!
804 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
805 (mc == CELL_PART_OF_MULTICOLUMN &&
806 nmc == CELL_PART_OF_MULTICOLUMN))
808 idx_type const cellno = cell_info[row][column].cellno;
809 setWidthOfMulticolCell(cellno,
810 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
816 // returns 1 if a complete update is necessary, otherwise 0
817 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
819 row_type const row = row_of_cell(cell);
820 col_type const column1 = column_of_cell(cell);
825 if (rightLine(cell_info[row][column1].cellno, true) &&
826 column1 < columns_ - 1 &&
827 leftLine(cell_info[row][column1+1].cellno, true))
829 add_width = WIDTH_OF_LINE;
832 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
835 if (isMultiColumnReal(cell)) {
836 tmp = setWidthOfMulticolCell(cell, new_width);
838 width = new_width + 2 * WIDTH_OF_LINE + add_width;
839 cell_info[row][column1].width_of_cell = width;
840 tmp = calculate_width_of_column_NMC(column1);
842 recalculateMulticolumnsOfColumn(column1);
845 for (col_type i = 0; i < columns_; ++i)
846 calculate_width_of_column(i);
847 calculate_width_of_tabular();
852 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
855 if (!isMultiColumn(cell) || onlycolumn)
856 column_info[column_of_cell(cell)].alignment = align;
858 cellinfo_of_cell(cell).alignment = align;
862 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
865 if (!isMultiColumn(cell) || onlycolumn)
866 column_info[column_of_cell(cell)].valignment = align;
868 cellinfo_of_cell(cell).valignment = align;
872 void LyXTabular::setColumnPWidth(idx_type cell, LyXLength const & width)
874 col_type const j = column_of_cell(cell);
876 column_info[j].p_width = width;
877 for (row_type i = 0; i < rows_; ++i) {
878 idx_type const cell = getCellNumber(i, j);
879 // because of multicolumns
880 getCellInset(cell)->setAutoBreakRows(!getPWidth(cell).zero());
885 bool LyXTabular::setMColumnPWidth(idx_type cell, LyXLength const & width)
887 if (!isMultiColumn(cell))
890 cellinfo_of_cell(cell).p_width = width;
891 getCellInset(cell)->setAutoBreakRows(!width.zero());
896 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
897 LyXTabular::Feature what)
899 if (what == SET_SPECIAL_MULTI)
900 cellinfo_of_cell(cell).align_special = special;
902 column_info[column_of_cell(cell)].align_special = special;
906 void LyXTabular::setAllLines(idx_type cell, bool line)
908 setTopLine(cell, line);
909 setBottomLine(cell, line);
910 setRightLine(cell, line);
911 setLeftLine(cell, line);
915 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
917 row_type const row = row_of_cell(cell);
918 if (onlycolumn || !isMultiColumn(cell))
919 row_info[row].top_line = line;
921 cellinfo_of_cell(cell).top_line = line;
925 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
927 if (onlycolumn || !isMultiColumn(cell))
928 row_info[row_of_cell(cell)].bottom_line = line;
930 cellinfo_of_cell(cell).bottom_line = line;
934 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
936 if (onlycolumn || !isMultiColumn(cell))
937 column_info[column_of_cell(cell)].left_line = line;
939 cellinfo_of_cell(cell).left_line = line;
943 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
945 if (onlycolumn || !isMultiColumn(cell))
946 column_info[right_column_of_cell(cell)].right_line = line;
948 cellinfo_of_cell(cell).right_line = line;
952 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
954 if (!onlycolumn && isMultiColumn(cell))
955 return cellinfo_of_cell(cell).alignment;
956 return column_info[column_of_cell(cell)].alignment;
960 LyXTabular::VAlignment
961 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
963 if (!onlycolumn && isMultiColumn(cell))
964 return cellinfo_of_cell(cell).valignment;
965 return column_info[column_of_cell(cell)].valignment;
969 LyXLength const LyXTabular::getPWidth(idx_type cell) const
971 if (isMultiColumn(cell))
972 return cellinfo_of_cell(cell).p_width;
973 return column_info[column_of_cell(cell)].p_width;
977 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
979 return column_info[column_of_cell(cell)].p_width;
983 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
985 if (isMultiColumn(cell))
986 return cellinfo_of_cell(cell).p_width;
991 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
993 if (what == SET_SPECIAL_MULTI)
994 return cellinfo_of_cell(cell).align_special;
995 return column_info[column_of_cell(cell)].align_special;
999 int LyXTabular::getWidthOfCell(idx_type cell) const
1001 row_type const row = row_of_cell(cell);
1002 col_type const column1 = column_of_cell(cell);
1003 col_type const column2 = right_column_of_cell(cell);
1005 for (col_type i = column1; i <= column2; ++i)
1006 result += cell_info[row][i].width_of_cell;
1011 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1015 switch (getAlignment(cell)) {
1016 case LYX_ALIGN_CENTER:
1017 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1019 case LYX_ALIGN_RIGHT:
1020 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1021 // + getAdditionalWidth(cell);
1024 // LYX_ALIGN_LEFT: nothing :-)
1028 // the LaTeX Way :-(
1034 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1036 return column_of_cell(cell) == 0;
1040 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1042 if (row > rows_ - 1)
1044 return cell_info[row][0].cellno;
1048 bool LyXTabular::isLastCellInRow(idx_type cell) const
1050 return right_column_of_cell(cell) == columns_ - 1;
1054 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1056 if (row > rows_ - 1)
1058 return cell_info[row][columns_-1].cellno;
1062 void LyXTabular::calculate_width_of_column(col_type column)
1065 for (row_type i = 0; i < rows_; ++i)
1066 maximum = max(cell_info[i][column].width_of_cell, maximum);
1067 column_info[column].width_of_column = maximum;
1072 // Calculate the columns regarding ONLY the normal cells and if this
1073 // column is inside a multicolumn cell then use it only if its the last
1074 // column of this multicolumn cell as this gives an added width to the
1075 // column, all the rest should be adapted!
1077 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1079 int const old_column_width = column_info[column].width_of_column;
1081 for (row_type i = 0; i < rows_; ++i) {
1082 idx_type cell = getCellNumber(i, column);
1083 bool ismulti = isMultiColumnReal(cell);
1084 if ((!ismulti || column == right_column_of_cell(cell)) &&
1085 cell_info[i][column].width_of_cell > max)
1087 max = cell_info[i][column].width_of_cell;
1090 column_info[column].width_of_column = max;
1091 return column_info[column].width_of_column != old_column_width;
1095 void LyXTabular::calculate_width_of_tabular()
1097 width_of_tabular = 0;
1098 for (col_type i = 0; i < columns_; ++i)
1099 width_of_tabular += column_info[i].width_of_column;
1103 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1105 if (cell >= numberofcells)
1109 return rowofcell[cell];
1113 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1115 if (cell >= numberofcells)
1116 return columns_ - 1;
1119 return columnofcell[cell];
1123 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1125 row_type const row = row_of_cell(cell);
1126 col_type column = column_of_cell(cell);
1127 while (column < columns_ - 1 &&
1128 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1134 void LyXTabular::write(Buffer const & buf, ostream & os) const
1138 << write_attribute("version", 3)
1139 << write_attribute("rows", rows_)
1140 << write_attribute("columns", columns_)
1142 // global longtable options
1144 << write_attribute("rotate", rotate)
1145 << write_attribute("islongtable", is_long_tabular)
1146 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1147 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1148 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1149 << write_attribute("headTopDL", endhead.topDL)
1150 << write_attribute("headBottomDL", endhead.bottomDL)
1151 << write_attribute("footTopDL", endfoot.topDL)
1152 << write_attribute("footBottomDL", endfoot.bottomDL)
1153 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1154 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1155 << write_attribute("lastFootEmpty", endlastfoot.empty)
1157 for (col_type j = 0; j < columns_; ++j) {
1159 << write_attribute("alignment", column_info[j].alignment)
1160 << write_attribute("valignment", column_info[j].valignment)
1161 << write_attribute("leftline", column_info[j].left_line)
1162 << write_attribute("rightline", column_info[j].right_line)
1163 << write_attribute("width", column_info[j].p_width.asString())
1164 << write_attribute("special", column_info[j].align_special)
1167 for (row_type i = 0; i < rows_; ++i) {
1169 << write_attribute("topline", row_info[i].top_line)
1170 << write_attribute("bottomline", row_info[i].bottom_line)
1171 << write_attribute("endhead", row_info[i].endhead)
1172 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1173 << write_attribute("endfoot", row_info[i].endfoot)
1174 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1175 << write_attribute("newpage", row_info[i].newpage)
1177 for (col_type j = 0; j < columns_; ++j) {
1179 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1180 << write_attribute("alignment", cell_info[i][j].alignment)
1181 << write_attribute("valignment", cell_info[i][j].valignment)
1182 << write_attribute("topline", cell_info[i][j].top_line)
1183 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1184 << write_attribute("leftline", cell_info[i][j].left_line)
1185 << write_attribute("rightline", cell_info[i][j].right_line)
1186 << write_attribute("rotate", cell_info[i][j].rotate)
1187 << write_attribute("usebox", cell_info[i][j].usebox)
1188 << write_attribute("width", cell_info[i][j].p_width)
1189 << write_attribute("special", cell_info[i][j].align_special)
1191 os << "\\begin_inset ";
1192 cell_info[i][j].inset->write(buf, os);
1193 os << "\n\\end_inset\n"
1198 os << "</lyxtabular>\n";
1202 void LyXTabular::setHeaderFooterRows(row_type hr, row_type fhr,
1203 row_type fr, row_type lfr)
1207 row_info[--hr].endhead = true;
1209 // set firstheader info
1210 if (fhr && fhr < rows_) {
1211 if (row_info[fhr].endhead) {
1213 row_info[--fhr].endfirsthead = true;
1214 row_info[fhr].endhead = false;
1216 } else if (row_info[fhr - 1].endhead) {
1217 endfirsthead.empty = true;
1219 while (fhr > 0 && !row_info[--fhr].endhead) {
1220 row_info[fhr].endfirsthead = true;
1225 if (fr && fr < rows_) {
1226 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1227 while (fr > 0 && !row_info[--fr].endhead) {
1228 row_info[fr].endfoot = true;
1229 row_info[fr].endhead = false;
1231 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1232 while (fr > 0 && !row_info[--fr].endfirsthead) {
1233 row_info[fr].endfoot = true;
1234 row_info[fr].endfirsthead = false;
1236 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1237 while (fr > 0 && !row_info[--fr].endhead &&
1238 !row_info[fr].endfirsthead)
1240 row_info[fr].endfoot = true;
1244 // set lastfooter info
1245 if (lfr && lfr < rows_) {
1246 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1247 while (lfr > 0 && !row_info[--lfr].endhead) {
1248 row_info[lfr].endlastfoot = true;
1249 row_info[lfr].endhead = false;
1251 } else if (row_info[lfr].endfirsthead &&
1252 row_info[lfr - 1].endfirsthead)
1254 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1255 row_info[lfr].endlastfoot = true;
1256 row_info[lfr].endfirsthead = false;
1258 } else if (row_info[lfr].endfoot
1259 && row_info[lfr - 1].endfoot) {
1260 while (lfr > 0 && !row_info[--lfr].endfoot) {
1261 row_info[lfr].endlastfoot = true;
1262 row_info[lfr].endfoot = false;
1264 } else if (!row_info[fr - 1].endhead
1265 && !row_info[fr - 1].endfirsthead &&
1266 !row_info[fr - 1].endfoot)
1269 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1270 !row_info[lfr].endfoot)
1272 row_info[lfr].endlastfoot = true;
1274 } else if (haveLTFoot()) {
1275 endlastfoot.empty = true;
1281 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1284 istream & is = lex.getStream();
1286 l_getline(is, line);
1287 if (!prefixIs(line, "<lyxtabular ")
1288 && !prefixIs(line, "<LyXTabular ")) {
1289 BOOST_ASSERT(false);
1294 if (!getTokenValue(line, "version", version))
1296 BOOST_ASSERT(version >= 2);
1299 if (!getTokenValue(line, "rows", rows_arg))
1302 if (!getTokenValue(line, "columns", columns_arg))
1304 init(buf.params(), rows_arg, columns_arg);
1305 l_getline(is, line);
1306 if (!prefixIs(line, "<features")) {
1307 lyxerr << "Wrong tabular format (expected <features ...> got"
1308 << line << ')' << endl;
1311 getTokenValue(line, "rotate", rotate);
1312 getTokenValue(line, "islongtable", is_long_tabular);
1313 // compatibility read for old longtable options. Now we can make any
1314 // row part of the header/footer type we want before it was strict
1315 // sequential from the first row down (as LaTeX does it!). So now when
1316 // we find a header/footer line we have to go up the rows and set it
1317 // on all preceding rows till the first or one with already a h/f option
1318 // set. If we find a firstheader on the same line as a header or a
1319 // lastfooter on the same line as a footer then this should be set empty.
1327 getTokenValue(line, "endhead", hrow);
1328 getTokenValue(line, "endfirsthead", fhrow);
1329 getTokenValue(line, "endfoot", frow);
1330 getTokenValue(line, "endlastfoot", lfrow);
1331 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1333 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1334 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1335 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1336 getTokenValue(line, "headTopDL", endhead.topDL);
1337 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1338 getTokenValue(line, "footTopDL", endfoot.topDL);
1339 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1340 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1341 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1342 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1344 for (col_type j = 0; j < columns_; ++j) {
1346 if (!prefixIs(line,"<column")) {
1347 lyxerr << "Wrong tabular format (expected <column ...> got"
1348 << line << ')' << endl;
1351 getTokenValue(line, "alignment", column_info[j].alignment);
1352 getTokenValue(line, "valignment", column_info[j].valignment);
1353 getTokenValue(line, "leftline", column_info[j].left_line);
1354 getTokenValue(line, "rightline", column_info[j].right_line);
1355 getTokenValue(line, "width", column_info[j].p_width);
1356 getTokenValue(line, "special", column_info[j].align_special);
1359 for (row_type i = 0; i < rows_; ++i) {
1360 l_getline(is, line);
1361 if (!prefixIs(line, "<row")) {
1362 lyxerr << "Wrong tabular format (expected <row ...> got"
1363 << line << ')' << endl;
1366 getTokenValue(line, "topline", row_info[i].top_line);
1367 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1368 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1369 getTokenValue(line, "endhead", row_info[i].endhead);
1370 getTokenValue(line, "endfoot", row_info[i].endfoot);
1371 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1372 getTokenValue(line, "newpage", row_info[i].newpage);
1373 for (col_type j = 0; j < columns_; ++j) {
1374 l_getline(is, line);
1375 if (!prefixIs(line, "<cell")) {
1376 lyxerr << "Wrong tabular format (expected <cell ...> got"
1377 << line << ')' << endl;
1380 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1381 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1382 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1383 getTokenValue(line, "topline", cell_info[i][j].top_line);
1384 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1385 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1386 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1387 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1388 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1389 getTokenValue(line, "width", cell_info[i][j].p_width);
1390 getTokenValue(line, "special", cell_info[i][j].align_special);
1391 l_getline(is, line);
1392 if (prefixIs(line, "\\begin_inset")) {
1393 cell_info[i][j].inset->read(buf, lex);
1394 l_getline(is, line);
1396 if (!prefixIs(line, "</cell>")) {
1397 lyxerr << "Wrong tabular format (expected </cell> got"
1398 << line << ')' << endl;
1402 l_getline(is, line);
1403 if (!prefixIs(line, "</row>")) {
1404 lyxerr << "Wrong tabular format (expected </row> got"
1405 << line << ')' << endl;
1409 while (!prefixIs(line, "</lyxtabular>")) {
1410 l_getline(is, line);
1412 set_row_column_number_info();
1416 bool LyXTabular::isMultiColumn(idx_type cell) const
1418 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1422 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1424 return column_of_cell(cell) != right_column_of_cell(cell) &&
1425 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1429 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1431 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1435 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1438 cellstruct & cs = cellinfo_of_cell(cell);
1439 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1440 cs.alignment = column_info[column_of_cell(cell)].alignment;
1441 cs.top_line = row_info[row_of_cell(cell)].top_line;
1442 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1443 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1444 for (idx_type i = 1; i < number; ++i) {
1445 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1446 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1447 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1448 cs1.inset->clear(false);
1450 set_row_column_number_info();
1454 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1456 row_type const row = row_of_cell(cell);
1457 col_type column = column_of_cell(cell);
1458 idx_type result = 1;
1460 while (column < columns_ &&
1461 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1470 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1472 row_type const row = row_of_cell(cell);
1473 col_type column = column_of_cell(cell);
1475 idx_type result = 0;
1477 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1478 cell_info[row][column].multicolumn = CELL_NORMAL;
1480 while (column < columns_ &&
1481 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1483 cell_info[row][column].multicolumn = CELL_NORMAL;
1488 set_row_column_number_info();
1493 void LyXTabular::setLongTabular(bool what)
1495 is_long_tabular = what;
1499 bool LyXTabular::isLongTabular() const
1501 return is_long_tabular;
1505 void LyXTabular::setRotateTabular(bool flag)
1511 bool LyXTabular::getRotateTabular() const
1517 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1519 cellinfo_of_cell(cell).rotate = flag;
1523 bool LyXTabular::getRotateCell(idx_type cell) const
1525 return cellinfo_of_cell(cell).rotate;
1529 bool LyXTabular::needRotating() const
1533 for (row_type i = 0; i < rows_; ++i)
1534 for (col_type j = 0; j < columns_; ++j)
1535 if (cell_info[i][j].rotate)
1541 bool LyXTabular::isLastCell(idx_type cell) const
1543 if (cell + 1 < numberofcells)
1549 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1551 if (row_of_cell(cell) > 0)
1552 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1557 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1559 if (row_of_cell(cell) + 1 < rows_)
1560 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1565 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1567 if (row_of_cell(cell) == 0)
1569 if (!isMultiColumn(cell))
1570 return getCellAbove(cell);
1571 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1575 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1577 if (row_of_cell(cell) + 1 >= rows_)
1579 if (!isMultiColumn(cell))
1580 return getCellBelow(cell);
1581 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1585 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1586 col_type column) const
1588 BOOST_ASSERT(column != npos && column < columns_ &&
1589 row != npos && row < rows_);
1590 return cell_info[row][column].cellno;
1594 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1596 cellinfo_of_cell(cell).usebox = type;
1600 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1602 if (column_info[column_of_cell(cell)].p_width.zero() &&
1603 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1605 if (cellinfo_of_cell(cell).usebox > 1)
1606 return cellinfo_of_cell(cell).usebox;
1607 return useParbox(cell);
1612 // This are functions used for the longtable support
1614 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1620 row_info[row].endfirsthead = flag;
1624 row_info[row].endhead = flag;
1629 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1632 hd.set = haveLTHead();
1633 return row_info[row].endhead;
1637 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1640 hd.set = haveLTFirstHead();
1641 return row_info[row].endfirsthead;
1645 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1651 row_info[row].endlastfoot = flag;
1655 row_info[row].endfoot = flag;
1660 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1663 fd.set = haveLTFoot();
1664 return row_info[row].endfoot;
1668 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1671 fd.set = haveLTLastFoot();
1672 return row_info[row].endlastfoot;
1676 void LyXTabular::setLTNewPage(row_type row, bool what)
1678 row_info[row].newpage = what;
1682 bool LyXTabular::getLTNewPage(row_type row) const
1684 return row_info[row].newpage;
1688 bool LyXTabular::haveLTHead() const
1690 for (row_type i = 0; i < rows_; ++i)
1691 if (row_info[i].endhead)
1697 bool LyXTabular::haveLTFirstHead() const
1699 if (endfirsthead.empty)
1701 for (row_type i = 0; i < rows_; ++i)
1702 if (row_info[i].endfirsthead)
1708 bool LyXTabular::haveLTFoot() const
1710 for (row_type i = 0; i < rows_; ++i)
1711 if (row_info[i].endfoot)
1717 bool LyXTabular::haveLTLastFoot() const
1719 if (endlastfoot.empty)
1721 for (row_type i = 0; i < rows_; ++i)
1722 if (row_info[i].endlastfoot)
1728 // end longtable support functions
1730 void LyXTabular::setAscentOfRow(row_type row, int height)
1732 if (row >= rows_ || row_info[row].ascent_of_row == height)
1734 row_info[row].ascent_of_row = height;
1738 void LyXTabular::setDescentOfRow(row_type row, int height)
1740 if (row >= rows_ || row_info[row].descent_of_row == height)
1742 row_info[row].descent_of_row = height;
1746 int LyXTabular::getAscentOfRow(row_type row) const
1750 return row_info[row].ascent_of_row;
1754 int LyXTabular::getDescentOfRow(row_type row) const
1756 BOOST_ASSERT(row < rows_);
1757 return row_info[row].descent_of_row;
1761 int LyXTabular::getHeightOfTabular() const
1764 for (row_type row = 0; row < rows_; ++row)
1765 height += getAscentOfRow(row) + getDescentOfRow(row) +
1766 getAdditionalHeight(row);
1771 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1773 BOOST_ASSERT(row < rows_);
1774 BOOST_ASSERT(column < columns_);
1775 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1779 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1781 // FIXME: assert or return 0 as in TeXBottomHLine()?
1782 BOOST_ASSERT(row != npos);
1783 BOOST_ASSERT(row < rows_);
1785 idx_type const fcell = getFirstCellInRow(row);
1786 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1789 for (idx_type i = fcell; i < n; ++i) {
1793 if (tmp == n - fcell) {
1796 for (idx_type i = fcell; i < n; ++i) {
1799 << column_of_cell(i) + 1
1801 << right_column_of_cell(i) + 1
1813 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1815 // FIXME: return 0 or assert as in TeXTopHLine()?
1816 if (row == npos || row >= rows_)
1819 idx_type const fcell = getFirstCellInRow(row);
1820 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1823 for (idx_type i = fcell; i < n; ++i) {
1827 if (tmp == n - fcell) {
1830 for (idx_type i = fcell; i < n; ++i) {
1831 if (bottomLine(i)) {
1833 << column_of_cell(i) + 1
1835 << right_column_of_cell(i) + 1
1847 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1851 if (getRotateCell(cell)) {
1852 os << "\\begin{sideways}\n";
1855 if (isMultiColumn(cell)) {
1856 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1857 if (!cellinfo_of_cell(cell).align_special.empty()) {
1858 os << cellinfo_of_cell(cell).align_special << "}{";
1860 if (leftLine(cell) &&
1861 (isFirstCellInRow(cell) ||
1862 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1863 !rightLine(cell - 1, true))))
1867 if (!getPWidth(cell).zero()) {
1868 switch (getVAlignment(cell)) {
1869 case LYX_VALIGN_TOP:
1872 case LYX_VALIGN_MIDDLE:
1875 case LYX_VALIGN_BOTTOM:
1880 << getPWidth(cell).asLatexString()
1883 switch (getAlignment(cell)) {
1884 case LYX_ALIGN_LEFT:
1887 case LYX_ALIGN_RIGHT:
1895 if (rightLine(cell))
1897 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1903 if (getUsebox(cell) == BOX_PARBOX) {
1905 switch (getVAlignment(cell)) {
1906 case LYX_VALIGN_TOP:
1909 case LYX_VALIGN_MIDDLE:
1912 case LYX_VALIGN_BOTTOM:
1916 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1917 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1918 os << "\\begin{minipage}[";
1919 switch (getVAlignment(cell)) {
1920 case LYX_VALIGN_TOP:
1923 case LYX_VALIGN_MIDDLE:
1926 case LYX_VALIGN_BOTTOM:
1930 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1937 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1942 if (getUsebox(cell) == BOX_PARBOX)
1944 else if (getUsebox(cell) == BOX_MINIPAGE) {
1945 os << "%\n\\end{minipage}";
1948 if (isMultiColumn(cell)) {
1951 if (getRotateCell(cell)) {
1952 os << "%\n\\end{sideways}";
1959 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1960 OutputParams const & runparams) const
1962 if (!is_long_tabular)
1966 // output header info
1968 if (endhead.topDL) {
1972 for (row_type i = 0; i < rows_; ++i) {
1973 if (row_info[i].endhead) {
1974 ret += TeXRow(os, i, buf, runparams);
1977 if (endhead.bottomDL) {
1981 os << "\\endhead\n";
1983 if (endfirsthead.empty) {
1984 os << "\\endfirsthead\n";
1988 // output firstheader info
1989 if (haveLTFirstHead()) {
1990 if (endfirsthead.topDL) {
1994 for (row_type i = 0; i < rows_; ++i) {
1995 if (row_info[i].endfirsthead) {
1996 ret += TeXRow(os, i, buf, runparams);
1999 if (endfirsthead.bottomDL) {
2003 os << "\\endfirsthead\n";
2006 // output footer info
2008 if (endfoot.topDL) {
2012 for (row_type i = 0; i < rows_; ++i) {
2013 if (row_info[i].endfoot) {
2014 ret += TeXRow(os, i, buf, runparams);
2017 if (endfoot.bottomDL) {
2021 os << "\\endfoot\n";
2023 if (endlastfoot.empty) {
2024 os << "\\endlastfoot\n";
2028 // output lastfooter info
2029 if (haveLTLastFoot()) {
2030 if (endlastfoot.topDL) {
2034 for (row_type i = 0; i < rows_; ++i) {
2035 if (row_info[i].endlastfoot) {
2036 ret += TeXRow(os, i, buf, runparams);
2039 if (endlastfoot.bottomDL) {
2043 os << "\\endlastfoot\n";
2050 bool LyXTabular::isValidRow(row_type row) const
2052 if (!is_long_tabular)
2054 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2055 !row_info[row].endfoot && !row_info[row].endlastfoot;
2059 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
2060 OutputParams const & runparams) const
2062 idx_type cell = getCellNumber(i, 0);
2064 int ret = TeXTopHLine(os, i);
2065 for (col_type j = 0; j < columns_; ++j) {
2066 if (isPartOfMultiColumn(i, j))
2068 ret += TeXCellPreamble(os, cell);
2069 shared_ptr<InsetText> inset = getCellInset(cell);
2071 Paragraph const & par = inset->paragraphs().front();
2072 bool rtl = par.isRightToLeftPar(buf.params())
2074 && getPWidth(cell).zero();
2078 ret += inset->latex(buf, os, runparams);
2082 ret += TeXCellPostamble(os, cell);
2083 if (!isLastCellInRow(cell)) { // not last cell in row
2089 os << "\\tabularnewline\n";
2091 ret += TeXBottomHLine(os, i);
2096 int LyXTabular::latex(Buffer const & buf, ostream & os,
2097 OutputParams const & runparams) const
2101 //+---------------------------------------------------------------------
2102 //+ first the opening preamble +
2103 //+---------------------------------------------------------------------
2106 os << "\\begin{sideways}\n";
2109 if (is_long_tabular)
2110 os << "\\begin{longtable}{";
2112 os << "\\begin{tabular}{";
2113 for (col_type i = 0; i < columns_; ++i) {
2114 if (!column_info[i].align_special.empty()) {
2115 os << column_info[i].align_special;
2117 if (column_info[i].left_line)
2119 if (!column_info[i].p_width.zero()) {
2120 switch (column_info[i].alignment) {
2121 case LYX_ALIGN_LEFT:
2122 os << ">{\\raggedright}";
2124 case LYX_ALIGN_RIGHT:
2125 os << ">{\\raggedleft}";
2127 case LYX_ALIGN_CENTER:
2128 os << ">{\\centering}";
2130 case LYX_ALIGN_NONE:
2131 case LYX_ALIGN_BLOCK:
2132 case LYX_ALIGN_LAYOUT:
2133 case LYX_ALIGN_SPECIAL:
2137 switch (column_info[i].valignment) {
2138 case LYX_VALIGN_TOP:
2141 case LYX_VALIGN_MIDDLE:
2144 case LYX_VALIGN_BOTTOM:
2149 << column_info[i].p_width.asLatexString()
2152 switch (column_info[i].alignment) {
2153 case LYX_ALIGN_LEFT:
2156 case LYX_ALIGN_RIGHT:
2164 if (column_info[i].right_line)
2171 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2173 //+---------------------------------------------------------------------
2174 //+ the single row and columns (cells) +
2175 //+---------------------------------------------------------------------
2177 for (row_type i = 0; i < rows_; ++i) {
2178 if (isValidRow(i)) {
2179 ret += TeXRow(os, i, buf, runparams);
2180 if (is_long_tabular && row_info[i].newpage) {
2181 os << "\\newpage\n";
2187 //+---------------------------------------------------------------------
2188 //+ the closing of the tabular +
2189 //+---------------------------------------------------------------------
2191 if (is_long_tabular)
2192 os << "\\end{longtable}";
2194 os << "\\end{tabular}";
2196 os << "\n\\end{sideways}";
2204 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2205 const OutputParams & runparams) const
2207 os << "<tabular ca=\"";
2208 for (col_type i = 0; i < columns_; ++i) {
2209 switch (column_info[i].alignment) {
2210 case LYX_ALIGN_LEFT:
2213 case LYX_ALIGN_RIGHT:
2224 for (row_type i = 0; i < rows_; ++i) {
2225 for (col_type j = 0; j < columns_; ++j) {
2226 if (isPartOfMultiColumn(i, j))
2228 shared_ptr<InsetText> inset = getCellInset(cell);
2230 ret += inset->linuxdoc(buf, os, runparams);
2232 if (isLastCellInRow(cell)) {
2241 os << "</tabular>\n";
2246 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2247 OutputParams const & runparams) const
2250 idx_type cell = getFirstCellInRow(row);
2253 for (col_type j = 0; j < columns_; ++j) {
2254 if (isPartOfMultiColumn(row, j))
2257 os << "<entry align=\"";
2258 switch (getAlignment(cell)) {
2259 case LYX_ALIGN_LEFT:
2262 case LYX_ALIGN_RIGHT:
2270 os << "\" valign=\"";
2271 switch (getVAlignment(cell)) {
2272 case LYX_VALIGN_TOP:
2275 case LYX_VALIGN_BOTTOM:
2278 case LYX_VALIGN_MIDDLE:
2283 if (isMultiColumn(cell)) {
2284 os << " namest=\"col" << j << "\" ";
2285 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2289 ret += getCellInset(cell)->docbook(buf, os, runparams);
2298 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2299 OutputParams const & runparams) const
2303 //+---------------------------------------------------------------------
2304 //+ first the opening preamble +
2305 //+---------------------------------------------------------------------
2307 os << "<tgroup cols=\"" << columns_
2308 << "\" colsep=\"1\" rowsep=\"1\">\n";
2310 for (col_type i = 0; i < columns_; ++i) {
2311 os << "<colspec colname=\"col" << i << "\" align=\"";
2312 switch (column_info[i].alignment) {
2313 case LYX_ALIGN_LEFT:
2316 case LYX_ALIGN_RIGHT:
2324 if (runparams.flavor == OutputParams::XML)
2330 //+---------------------------------------------------------------------
2331 //+ Long Tabular case +
2332 //+---------------------------------------------------------------------
2334 // output header info
2335 if (haveLTHead() || haveLTFirstHead()) {
2338 for (row_type i = 0; i < rows_; ++i) {
2339 if (row_info[i].endhead || row_info[i].endfirsthead) {
2340 ret += docbookRow(buf, os, i, runparams);
2346 // output footer info
2347 if (haveLTFoot() || haveLTLastFoot()) {
2350 for (row_type i = 0; i < rows_; ++i) {
2351 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2352 ret += docbookRow(buf, os, i, runparams);
2359 //+---------------------------------------------------------------------
2360 //+ the single row and columns (cells) +
2361 //+---------------------------------------------------------------------
2365 for (row_type i = 0; i < rows_; ++i) {
2366 if (isValidRow(i)) {
2367 ret += docbookRow(buf, os, i, runparams);
2372 //+---------------------------------------------------------------------
2373 //+ the closing of the tabular +
2374 //+---------------------------------------------------------------------
2383 int LyXTabular::asciiTopHLine(ostream & os, row_type row,
2384 vector<unsigned int> const & clen) const
2386 idx_type const fcell = getFirstCellInRow(row);
2387 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2390 for (idx_type i = fcell; i < n; ++i) {
2400 for (idx_type i = fcell; i < n; ++i) {
2411 col_type column = column_of_cell(i);
2412 int len = clen[column];
2413 while (column < columns_ - 1
2414 && isPartOfMultiColumn(row, ++column))
2415 len += clen[column] + 4;
2416 os << string(len, ch);
2431 int LyXTabular::asciiBottomHLine(ostream & os, row_type row,
2432 vector<unsigned int> const & clen) const
2434 idx_type const fcell = getFirstCellInRow(row);
2435 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2438 for (idx_type i = fcell; i < n; ++i) {
2439 if (bottomLine(i)) {
2448 for (idx_type i = fcell; i < n; ++i) {
2449 if (bottomLine(i)) {
2459 col_type column = column_of_cell(i);
2460 int len = clen[column];
2461 while (column < columns_ -1
2462 && isPartOfMultiColumn(row, ++column))
2463 len += clen[column] + 4;
2464 os << string(len, ch);
2465 if (bottomLine(i)) {
2479 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2480 OutputParams const & runparams,
2481 idx_type cell, row_type row, col_type column,
2482 vector<unsigned int> const & clen,
2483 bool onlydata) const
2486 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2498 unsigned int len1 = sstr.str().length();
2499 unsigned int len2 = clen[column];
2500 while (column < columns_ -1
2501 && isPartOfMultiColumn(row, ++column))
2502 len2 += clen[column] + 4;
2505 switch (getAlignment(cell)) {
2507 case LYX_ALIGN_LEFT:
2510 case LYX_ALIGN_RIGHT:
2514 case LYX_ALIGN_CENTER:
2520 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2522 if (rightLine(cell))
2531 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2532 OutputParams const & runparams,
2534 bool onlydata, unsigned char delim) const
2538 // first calculate the width of the single columns
2539 vector<unsigned int> clen(columns_);
2542 // first all non (real) multicolumn cells!
2543 for (col_type j = 0; j < columns_; ++j) {
2545 for (row_type i = 0; i < rows_; ++i) {
2546 idx_type cell = getCellNumber(i, j);
2547 if (isMultiColumnReal(cell))
2550 getCellInset(cell)->plaintext(buf, sstr, runparams);
2551 if (clen[j] < sstr.str().length())
2552 clen[j] = sstr.str().length();
2555 // then all (real) multicolumn cells!
2556 for (col_type j = 0; j < columns_; ++j) {
2557 for (row_type i = 0; i < rows_; ++i) {
2558 idx_type cell = getCellNumber(i, j);
2559 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2562 getCellInset(cell)->plaintext(buf, sstr, runparams);
2563 int len = int(sstr.str().length());
2564 idx_type const n = cells_in_multicolumn(cell);
2565 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2567 if (len > int(clen[j + n - 1]))
2568 clen[j + n - 1] = len;
2573 for (row_type i = 0; i < rows_; ++i) {
2574 if (!onlydata && asciiTopHLine(os, i, clen))
2575 os << string(depth * 2, ' ');
2576 for (col_type j = 0; j < columns_; ++j) {
2577 if (isPartOfMultiColumn(i, j))
2579 if (onlydata && j > 0)
2581 ret += asciiPrintCell(buf, os, runparams,
2582 cell, i, j, clen, onlydata);
2587 os << string(depth * 2, ' ');
2588 if (asciiBottomHLine(os, i, clen))
2589 os << string(depth * 2, ' ');
2596 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2598 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2602 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2603 col_type column) const
2605 return cell_info[row][column].inset;
2609 LyXTabular::idx_type
2610 LyXTabular::getCellFromInset(InsetBase const * inset) const
2612 // is this inset part of the tabular?
2614 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2615 BOOST_ASSERT(false);
2618 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2619 if (getCellInset(cell).get() == inset) {
2620 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2621 << "cell=" << cell << endl;
2625 // We should have found a cell at this point
2626 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2627 << inset << " not found!" << endl;
2628 BOOST_ASSERT(false);
2634 void LyXTabular::validate(LaTeXFeatures & features) const
2636 features.require("NeedTabularnewline");
2637 if (isLongTabular())
2638 features.require("longtable");
2640 features.require("rotating");
2641 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2642 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2643 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2644 features.require("array");
2645 getCellInset(cell)->validate(features);
2650 void LyXTabular::getLabelList(Buffer const & buffer,
2651 std::vector<string> & list) const
2653 for (row_type i = 0; i < rows_; ++i)
2654 for (col_type j = 0; j < columns_; ++j)
2655 getCellInset(i, j)->getLabelList(buffer, list);
2659 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2661 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2662 ParagraphList::const_iterator cit = parlist.begin();
2663 ParagraphList::const_iterator end = parlist.end();
2665 for (; cit != end; ++cit)
2666 for (int i = 0; i < cit->size(); ++i)
2667 if (cit->isNewline(i))