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::read(Buffer const & buf, LyXLex & lex)
1205 istream & is = lex.getStream();
1207 l_getline(is, line);
1208 if (!prefixIs(line, "<lyxtabular ")
1209 && !prefixIs(line, "<LyXTabular ")) {
1210 BOOST_ASSERT(false);
1215 if (!getTokenValue(line, "version", version))
1217 BOOST_ASSERT(version >= 2);
1220 if (!getTokenValue(line, "rows", rows_arg))
1223 if (!getTokenValue(line, "columns", columns_arg))
1225 init(buf.params(), rows_arg, columns_arg);
1226 l_getline(is, line);
1227 if (!prefixIs(line, "<features")) {
1228 lyxerr << "Wrong tabular format (expected <features ...> got"
1229 << line << ')' << endl;
1232 getTokenValue(line, "rotate", rotate);
1233 getTokenValue(line, "islongtable", is_long_tabular);
1234 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1235 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1236 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1237 getTokenValue(line, "headTopDL", endhead.topDL);
1238 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1239 getTokenValue(line, "footTopDL", endfoot.topDL);
1240 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1241 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1242 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1243 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1245 for (col_type j = 0; j < columns_; ++j) {
1247 if (!prefixIs(line,"<column")) {
1248 lyxerr << "Wrong tabular format (expected <column ...> got"
1249 << line << ')' << endl;
1252 getTokenValue(line, "alignment", column_info[j].alignment);
1253 getTokenValue(line, "valignment", column_info[j].valignment);
1254 getTokenValue(line, "leftline", column_info[j].left_line);
1255 getTokenValue(line, "rightline", column_info[j].right_line);
1256 getTokenValue(line, "width", column_info[j].p_width);
1257 getTokenValue(line, "special", column_info[j].align_special);
1260 for (row_type i = 0; i < rows_; ++i) {
1261 l_getline(is, line);
1262 if (!prefixIs(line, "<row")) {
1263 lyxerr << "Wrong tabular format (expected <row ...> got"
1264 << line << ')' << endl;
1267 getTokenValue(line, "topline", row_info[i].top_line);
1268 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1269 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1270 getTokenValue(line, "endhead", row_info[i].endhead);
1271 getTokenValue(line, "endfoot", row_info[i].endfoot);
1272 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1273 getTokenValue(line, "newpage", row_info[i].newpage);
1274 for (col_type j = 0; j < columns_; ++j) {
1275 l_getline(is, line);
1276 if (!prefixIs(line, "<cell")) {
1277 lyxerr << "Wrong tabular format (expected <cell ...> got"
1278 << line << ')' << endl;
1281 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1282 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1283 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1284 getTokenValue(line, "topline", cell_info[i][j].top_line);
1285 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1286 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1287 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1288 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1289 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1290 getTokenValue(line, "width", cell_info[i][j].p_width);
1291 getTokenValue(line, "special", cell_info[i][j].align_special);
1292 l_getline(is, line);
1293 if (prefixIs(line, "\\begin_inset")) {
1294 cell_info[i][j].inset->read(buf, lex);
1295 l_getline(is, line);
1297 if (!prefixIs(line, "</cell>")) {
1298 lyxerr << "Wrong tabular format (expected </cell> got"
1299 << line << ')' << endl;
1303 l_getline(is, line);
1304 if (!prefixIs(line, "</row>")) {
1305 lyxerr << "Wrong tabular format (expected </row> got"
1306 << line << ')' << endl;
1310 while (!prefixIs(line, "</lyxtabular>")) {
1311 l_getline(is, line);
1313 set_row_column_number_info();
1317 bool LyXTabular::isMultiColumn(idx_type cell) const
1319 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1323 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1325 return column_of_cell(cell) != right_column_of_cell(cell) &&
1326 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1330 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1332 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1336 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1339 cellstruct & cs = cellinfo_of_cell(cell);
1340 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1341 cs.alignment = column_info[column_of_cell(cell)].alignment;
1342 cs.top_line = row_info[row_of_cell(cell)].top_line;
1343 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1344 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1345 for (idx_type i = 1; i < number; ++i) {
1346 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1347 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1348 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1349 cs1.inset->clear(false);
1351 set_row_column_number_info();
1355 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1357 row_type const row = row_of_cell(cell);
1358 col_type column = column_of_cell(cell);
1359 idx_type result = 1;
1361 while (column < columns_ &&
1362 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1371 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1373 row_type const row = row_of_cell(cell);
1374 col_type column = column_of_cell(cell);
1376 idx_type result = 0;
1378 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1379 cell_info[row][column].multicolumn = CELL_NORMAL;
1381 while (column < columns_ &&
1382 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1384 cell_info[row][column].multicolumn = CELL_NORMAL;
1389 set_row_column_number_info();
1394 void LyXTabular::setLongTabular(bool what)
1396 is_long_tabular = what;
1400 bool LyXTabular::isLongTabular() const
1402 return is_long_tabular;
1406 void LyXTabular::setRotateTabular(bool flag)
1412 bool LyXTabular::getRotateTabular() const
1418 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1420 cellinfo_of_cell(cell).rotate = flag;
1424 bool LyXTabular::getRotateCell(idx_type cell) const
1426 return cellinfo_of_cell(cell).rotate;
1430 bool LyXTabular::needRotating() const
1434 for (row_type i = 0; i < rows_; ++i)
1435 for (col_type j = 0; j < columns_; ++j)
1436 if (cell_info[i][j].rotate)
1442 bool LyXTabular::isLastCell(idx_type cell) const
1444 if (cell + 1 < numberofcells)
1450 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1452 if (row_of_cell(cell) > 0)
1453 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1458 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1460 if (row_of_cell(cell) + 1 < rows_)
1461 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1466 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1468 if (row_of_cell(cell) == 0)
1470 if (!isMultiColumn(cell))
1471 return getCellAbove(cell);
1472 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1476 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1478 if (row_of_cell(cell) + 1 >= rows_)
1480 if (!isMultiColumn(cell))
1481 return getCellBelow(cell);
1482 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1486 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1487 col_type column) const
1489 BOOST_ASSERT(column != npos && column < columns_ &&
1490 row != npos && row < rows_);
1491 return cell_info[row][column].cellno;
1495 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1497 cellinfo_of_cell(cell).usebox = type;
1501 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1503 if (column_info[column_of_cell(cell)].p_width.zero() &&
1504 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1506 if (cellinfo_of_cell(cell).usebox > 1)
1507 return cellinfo_of_cell(cell).usebox;
1508 return useParbox(cell);
1513 // This are functions used for the longtable support
1515 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1521 row_info[row].endfirsthead = flag;
1525 row_info[row].endhead = flag;
1530 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1533 hd.set = haveLTHead();
1534 return row_info[row].endhead;
1538 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1541 hd.set = haveLTFirstHead();
1542 return row_info[row].endfirsthead;
1546 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1552 row_info[row].endlastfoot = flag;
1556 row_info[row].endfoot = flag;
1561 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1564 fd.set = haveLTFoot();
1565 return row_info[row].endfoot;
1569 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1572 fd.set = haveLTLastFoot();
1573 return row_info[row].endlastfoot;
1577 void LyXTabular::setLTNewPage(row_type row, bool what)
1579 row_info[row].newpage = what;
1583 bool LyXTabular::getLTNewPage(row_type row) const
1585 return row_info[row].newpage;
1589 bool LyXTabular::haveLTHead() const
1591 for (row_type i = 0; i < rows_; ++i)
1592 if (row_info[i].endhead)
1598 bool LyXTabular::haveLTFirstHead() const
1600 if (endfirsthead.empty)
1602 for (row_type i = 0; i < rows_; ++i)
1603 if (row_info[i].endfirsthead)
1609 bool LyXTabular::haveLTFoot() const
1611 for (row_type i = 0; i < rows_; ++i)
1612 if (row_info[i].endfoot)
1618 bool LyXTabular::haveLTLastFoot() const
1620 if (endlastfoot.empty)
1622 for (row_type i = 0; i < rows_; ++i)
1623 if (row_info[i].endlastfoot)
1629 // end longtable support functions
1631 void LyXTabular::setAscentOfRow(row_type row, int height)
1633 if (row >= rows_ || row_info[row].ascent_of_row == height)
1635 row_info[row].ascent_of_row = height;
1639 void LyXTabular::setDescentOfRow(row_type row, int height)
1641 if (row >= rows_ || row_info[row].descent_of_row == height)
1643 row_info[row].descent_of_row = height;
1647 int LyXTabular::getAscentOfRow(row_type row) const
1651 return row_info[row].ascent_of_row;
1655 int LyXTabular::getDescentOfRow(row_type row) const
1657 BOOST_ASSERT(row < rows_);
1658 return row_info[row].descent_of_row;
1662 int LyXTabular::getHeightOfTabular() const
1665 for (row_type row = 0; row < rows_; ++row)
1666 height += getAscentOfRow(row) + getDescentOfRow(row) +
1667 getAdditionalHeight(row);
1672 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1674 BOOST_ASSERT(row < rows_);
1675 BOOST_ASSERT(column < columns_);
1676 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1680 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1682 // FIXME: assert or return 0 as in TeXBottomHLine()?
1683 BOOST_ASSERT(row != npos);
1684 BOOST_ASSERT(row < rows_);
1686 idx_type const fcell = getFirstCellInRow(row);
1687 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1690 for (idx_type i = fcell; i < n; ++i) {
1694 if (tmp == n - fcell) {
1697 for (idx_type i = fcell; i < n; ++i) {
1700 << column_of_cell(i) + 1
1702 << right_column_of_cell(i) + 1
1714 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1716 // FIXME: return 0 or assert as in TeXTopHLine()?
1717 if (row == npos || row >= rows_)
1720 idx_type const fcell = getFirstCellInRow(row);
1721 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1724 for (idx_type i = fcell; i < n; ++i) {
1728 if (tmp == n - fcell) {
1731 for (idx_type i = fcell; i < n; ++i) {
1732 if (bottomLine(i)) {
1734 << column_of_cell(i) + 1
1736 << right_column_of_cell(i) + 1
1748 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1752 if (getRotateCell(cell)) {
1753 os << "\\begin{sideways}\n";
1756 if (isMultiColumn(cell)) {
1757 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1758 if (!cellinfo_of_cell(cell).align_special.empty()) {
1759 os << cellinfo_of_cell(cell).align_special << "}{";
1761 if (leftLine(cell) &&
1762 (isFirstCellInRow(cell) ||
1763 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1764 !rightLine(cell - 1, true))))
1768 if (!getPWidth(cell).zero()) {
1769 switch (getVAlignment(cell)) {
1770 case LYX_VALIGN_TOP:
1773 case LYX_VALIGN_MIDDLE:
1776 case LYX_VALIGN_BOTTOM:
1781 << getPWidth(cell).asLatexString()
1784 switch (getAlignment(cell)) {
1785 case LYX_ALIGN_LEFT:
1788 case LYX_ALIGN_RIGHT:
1796 if (rightLine(cell))
1798 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1804 if (getUsebox(cell) == BOX_PARBOX) {
1806 switch (getVAlignment(cell)) {
1807 case LYX_VALIGN_TOP:
1810 case LYX_VALIGN_MIDDLE:
1813 case LYX_VALIGN_BOTTOM:
1817 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1818 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1819 os << "\\begin{minipage}[";
1820 switch (getVAlignment(cell)) {
1821 case LYX_VALIGN_TOP:
1824 case LYX_VALIGN_MIDDLE:
1827 case LYX_VALIGN_BOTTOM:
1831 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1838 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1843 if (getUsebox(cell) == BOX_PARBOX)
1845 else if (getUsebox(cell) == BOX_MINIPAGE) {
1846 os << "%\n\\end{minipage}";
1849 if (isMultiColumn(cell)) {
1852 if (getRotateCell(cell)) {
1853 os << "%\n\\end{sideways}";
1860 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1861 OutputParams const & runparams) const
1863 if (!is_long_tabular)
1867 // output header info
1869 if (endhead.topDL) {
1873 for (row_type i = 0; i < rows_; ++i) {
1874 if (row_info[i].endhead) {
1875 ret += TeXRow(os, i, buf, runparams);
1878 if (endhead.bottomDL) {
1882 os << "\\endhead\n";
1884 if (endfirsthead.empty) {
1885 os << "\\endfirsthead\n";
1889 // output firstheader info
1890 if (haveLTFirstHead()) {
1891 if (endfirsthead.topDL) {
1895 for (row_type i = 0; i < rows_; ++i) {
1896 if (row_info[i].endfirsthead) {
1897 ret += TeXRow(os, i, buf, runparams);
1900 if (endfirsthead.bottomDL) {
1904 os << "\\endfirsthead\n";
1907 // output footer info
1909 if (endfoot.topDL) {
1913 for (row_type i = 0; i < rows_; ++i) {
1914 if (row_info[i].endfoot) {
1915 ret += TeXRow(os, i, buf, runparams);
1918 if (endfoot.bottomDL) {
1922 os << "\\endfoot\n";
1924 if (endlastfoot.empty) {
1925 os << "\\endlastfoot\n";
1929 // output lastfooter info
1930 if (haveLTLastFoot()) {
1931 if (endlastfoot.topDL) {
1935 for (row_type i = 0; i < rows_; ++i) {
1936 if (row_info[i].endlastfoot) {
1937 ret += TeXRow(os, i, buf, runparams);
1940 if (endlastfoot.bottomDL) {
1944 os << "\\endlastfoot\n";
1951 bool LyXTabular::isValidRow(row_type row) const
1953 if (!is_long_tabular)
1955 return !row_info[row].endhead && !row_info[row].endfirsthead &&
1956 !row_info[row].endfoot && !row_info[row].endlastfoot;
1960 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
1961 OutputParams const & runparams) const
1963 idx_type cell = getCellNumber(i, 0);
1965 int ret = TeXTopHLine(os, i);
1966 for (col_type j = 0; j < columns_; ++j) {
1967 if (isPartOfMultiColumn(i, j))
1969 ret += TeXCellPreamble(os, cell);
1970 shared_ptr<InsetText> inset = getCellInset(cell);
1972 Paragraph const & par = inset->paragraphs().front();
1973 bool rtl = par.isRightToLeftPar(buf.params())
1975 && getPWidth(cell).zero();
1979 ret += inset->latex(buf, os, runparams);
1983 ret += TeXCellPostamble(os, cell);
1984 if (!isLastCellInRow(cell)) { // not last cell in row
1990 os << "\\tabularnewline\n";
1992 ret += TeXBottomHLine(os, i);
1997 int LyXTabular::latex(Buffer const & buf, ostream & os,
1998 OutputParams const & runparams) const
2002 //+---------------------------------------------------------------------
2003 //+ first the opening preamble +
2004 //+---------------------------------------------------------------------
2007 os << "\\begin{sideways}\n";
2010 if (is_long_tabular)
2011 os << "\\begin{longtable}{";
2013 os << "\\begin{tabular}{";
2014 for (col_type i = 0; i < columns_; ++i) {
2015 if (!column_info[i].align_special.empty()) {
2016 os << column_info[i].align_special;
2018 if (column_info[i].left_line)
2020 if (!column_info[i].p_width.zero()) {
2021 switch (column_info[i].alignment) {
2022 case LYX_ALIGN_LEFT:
2023 os << ">{\\raggedright}";
2025 case LYX_ALIGN_RIGHT:
2026 os << ">{\\raggedleft}";
2028 case LYX_ALIGN_CENTER:
2029 os << ">{\\centering}";
2031 case LYX_ALIGN_NONE:
2032 case LYX_ALIGN_BLOCK:
2033 case LYX_ALIGN_LAYOUT:
2034 case LYX_ALIGN_SPECIAL:
2038 switch (column_info[i].valignment) {
2039 case LYX_VALIGN_TOP:
2042 case LYX_VALIGN_MIDDLE:
2045 case LYX_VALIGN_BOTTOM:
2050 << column_info[i].p_width.asLatexString()
2053 switch (column_info[i].alignment) {
2054 case LYX_ALIGN_LEFT:
2057 case LYX_ALIGN_RIGHT:
2065 if (column_info[i].right_line)
2072 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2074 //+---------------------------------------------------------------------
2075 //+ the single row and columns (cells) +
2076 //+---------------------------------------------------------------------
2078 for (row_type i = 0; i < rows_; ++i) {
2079 if (isValidRow(i)) {
2080 ret += TeXRow(os, i, buf, runparams);
2081 if (is_long_tabular && row_info[i].newpage) {
2082 os << "\\newpage\n";
2088 //+---------------------------------------------------------------------
2089 //+ the closing of the tabular +
2090 //+---------------------------------------------------------------------
2092 if (is_long_tabular)
2093 os << "\\end{longtable}";
2095 os << "\\end{tabular}";
2097 os << "\n\\end{sideways}";
2105 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2106 const OutputParams & runparams) const
2108 os << "<tabular ca=\"";
2109 for (col_type i = 0; i < columns_; ++i) {
2110 switch (column_info[i].alignment) {
2111 case LYX_ALIGN_LEFT:
2114 case LYX_ALIGN_RIGHT:
2125 for (row_type i = 0; i < rows_; ++i) {
2126 for (col_type j = 0; j < columns_; ++j) {
2127 if (isPartOfMultiColumn(i, j))
2129 shared_ptr<InsetText> inset = getCellInset(cell);
2131 ret += inset->linuxdoc(buf, os, runparams);
2133 if (isLastCellInRow(cell)) {
2142 os << "</tabular>\n";
2147 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2148 OutputParams const & runparams) const
2151 idx_type cell = getFirstCellInRow(row);
2154 for (col_type j = 0; j < columns_; ++j) {
2155 if (isPartOfMultiColumn(row, j))
2158 os << "<entry align=\"";
2159 switch (getAlignment(cell)) {
2160 case LYX_ALIGN_LEFT:
2163 case LYX_ALIGN_RIGHT:
2171 os << "\" valign=\"";
2172 switch (getVAlignment(cell)) {
2173 case LYX_VALIGN_TOP:
2176 case LYX_VALIGN_BOTTOM:
2179 case LYX_VALIGN_MIDDLE:
2184 if (isMultiColumn(cell)) {
2185 os << " namest=\"col" << j << "\" ";
2186 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2190 ret += getCellInset(cell)->docbook(buf, os, runparams);
2199 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2200 OutputParams const & runparams) const
2204 //+---------------------------------------------------------------------
2205 //+ first the opening preamble +
2206 //+---------------------------------------------------------------------
2208 os << "<tgroup cols=\"" << columns_
2209 << "\" colsep=\"1\" rowsep=\"1\">\n";
2211 for (col_type i = 0; i < columns_; ++i) {
2212 os << "<colspec colname=\"col" << i << "\" align=\"";
2213 switch (column_info[i].alignment) {
2214 case LYX_ALIGN_LEFT:
2217 case LYX_ALIGN_RIGHT:
2225 if (runparams.flavor == OutputParams::XML)
2231 //+---------------------------------------------------------------------
2232 //+ Long Tabular case +
2233 //+---------------------------------------------------------------------
2235 // output header info
2236 if (haveLTHead() || haveLTFirstHead()) {
2239 for (row_type i = 0; i < rows_; ++i) {
2240 if (row_info[i].endhead || row_info[i].endfirsthead) {
2241 ret += docbookRow(buf, os, i, runparams);
2247 // output footer info
2248 if (haveLTFoot() || haveLTLastFoot()) {
2251 for (row_type i = 0; i < rows_; ++i) {
2252 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2253 ret += docbookRow(buf, os, i, runparams);
2260 //+---------------------------------------------------------------------
2261 //+ the single row and columns (cells) +
2262 //+---------------------------------------------------------------------
2266 for (row_type i = 0; i < rows_; ++i) {
2267 if (isValidRow(i)) {
2268 ret += docbookRow(buf, os, i, runparams);
2273 //+---------------------------------------------------------------------
2274 //+ the closing of the tabular +
2275 //+---------------------------------------------------------------------
2284 int LyXTabular::asciiTopHLine(ostream & os, row_type row,
2285 vector<unsigned int> const & clen) const
2287 idx_type const fcell = getFirstCellInRow(row);
2288 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2291 for (idx_type i = fcell; i < n; ++i) {
2301 for (idx_type i = fcell; i < n; ++i) {
2312 col_type column = column_of_cell(i);
2313 int len = clen[column];
2314 while (column < columns_ - 1
2315 && isPartOfMultiColumn(row, ++column))
2316 len += clen[column] + 4;
2317 os << string(len, ch);
2332 int LyXTabular::asciiBottomHLine(ostream & os, row_type row,
2333 vector<unsigned int> const & clen) const
2335 idx_type const fcell = getFirstCellInRow(row);
2336 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2339 for (idx_type i = fcell; i < n; ++i) {
2340 if (bottomLine(i)) {
2349 for (idx_type i = fcell; i < n; ++i) {
2350 if (bottomLine(i)) {
2360 col_type column = column_of_cell(i);
2361 int len = clen[column];
2362 while (column < columns_ -1
2363 && isPartOfMultiColumn(row, ++column))
2364 len += clen[column] + 4;
2365 os << string(len, ch);
2366 if (bottomLine(i)) {
2380 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2381 OutputParams const & runparams,
2382 idx_type cell, row_type row, col_type column,
2383 vector<unsigned int> const & clen,
2384 bool onlydata) const
2387 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2399 unsigned int len1 = sstr.str().length();
2400 unsigned int len2 = clen[column];
2401 while (column < columns_ -1
2402 && isPartOfMultiColumn(row, ++column))
2403 len2 += clen[column] + 4;
2406 switch (getAlignment(cell)) {
2408 case LYX_ALIGN_LEFT:
2411 case LYX_ALIGN_RIGHT:
2415 case LYX_ALIGN_CENTER:
2421 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2423 if (rightLine(cell))
2432 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2433 OutputParams const & runparams,
2435 bool onlydata, unsigned char delim) const
2439 // first calculate the width of the single columns
2440 vector<unsigned int> clen(columns_);
2443 // first all non (real) multicolumn cells!
2444 for (col_type j = 0; j < columns_; ++j) {
2446 for (row_type i = 0; i < rows_; ++i) {
2447 idx_type cell = getCellNumber(i, j);
2448 if (isMultiColumnReal(cell))
2451 getCellInset(cell)->plaintext(buf, sstr, runparams);
2452 if (clen[j] < sstr.str().length())
2453 clen[j] = sstr.str().length();
2456 // then all (real) multicolumn cells!
2457 for (col_type j = 0; j < columns_; ++j) {
2458 for (row_type i = 0; i < rows_; ++i) {
2459 idx_type cell = getCellNumber(i, j);
2460 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2463 getCellInset(cell)->plaintext(buf, sstr, runparams);
2464 int len = int(sstr.str().length());
2465 idx_type const n = cells_in_multicolumn(cell);
2466 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2468 if (len > int(clen[j + n - 1]))
2469 clen[j + n - 1] = len;
2474 for (row_type i = 0; i < rows_; ++i) {
2475 if (!onlydata && asciiTopHLine(os, i, clen))
2476 os << string(depth * 2, ' ');
2477 for (col_type j = 0; j < columns_; ++j) {
2478 if (isPartOfMultiColumn(i, j))
2480 if (onlydata && j > 0)
2482 ret += asciiPrintCell(buf, os, runparams,
2483 cell, i, j, clen, onlydata);
2488 os << string(depth * 2, ' ');
2489 if (asciiBottomHLine(os, i, clen))
2490 os << string(depth * 2, ' ');
2497 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2499 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2503 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2504 col_type column) const
2506 return cell_info[row][column].inset;
2510 LyXTabular::idx_type
2511 LyXTabular::getCellFromInset(InsetBase const * inset) const
2513 // is this inset part of the tabular?
2515 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2516 BOOST_ASSERT(false);
2519 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2520 if (getCellInset(cell).get() == inset) {
2521 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2522 << "cell=" << cell << endl;
2526 // We should have found a cell at this point
2527 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2528 << inset << " not found!" << endl;
2529 BOOST_ASSERT(false);
2535 void LyXTabular::validate(LaTeXFeatures & features) const
2537 features.require("NeedTabularnewline");
2538 if (isLongTabular())
2539 features.require("longtable");
2541 features.require("rotating");
2542 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2543 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2544 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2545 features.require("array");
2546 getCellInset(cell)->validate(features);
2551 void LyXTabular::getLabelList(Buffer const & buffer,
2552 std::vector<string> & list) const
2554 for (row_type i = 0; i < rows_; ++i)
2555 for (col_type j = 0; j < columns_; ++j)
2556 getCellInset(i, j)->getLabelList(buffer, list);
2560 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2562 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2563 ParagraphList::const_iterator cit = parlist.begin();
2564 ParagraphList::const_iterator end = parlist.end();
2566 for (; cit != end; ++cit)
2567 for (int i = 0; i < cit->size(); ++i)
2568 if (cit->isNewline(i))