3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
23 #include "bufferparams.h"
24 #include "BufferView.h"
27 #include "LaTeXFeatures.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
33 #include "insets/insettabular.h"
35 #include "support/lstrings.h"
36 #include "support/convert.h"
40 using lyx::support::ltrim;
41 using lyx::support::prefixIs;
42 using lyx::support::rtrim;
43 using lyx::support::suffixIs;
45 using boost::shared_ptr;
46 using boost::dynamic_pointer_cast;
55 using std::ostringstream;
59 #ifndef CXX_GLOBAL_CSTD
66 int const WIDTH_OF_LINE = 5;
69 string const write_attribute(string const & name, T const & t)
71 string const s = tostr(t);
72 return s.empty() ? s : " " + name + "=\"" + s + "\"";
76 string const write_attribute(string const & name, string const & t)
78 return t.empty() ? t : " " + name + "=\"" + t + "\"";
83 string const write_attribute(string const & name, bool const & b)
85 // we write only true attribute values so we remove a bit of the
86 // file format bloat for tabulars.
87 return b ? write_attribute(name, convert<string>(b)) : string();
92 string const write_attribute(string const & name, int const & i)
94 // we write only true attribute values so we remove a bit of the
95 // file format bloat for tabulars.
96 return i ? write_attribute(name, convert<string>(i)) : string();
101 string const write_attribute(string const & name, LyXTabular::idx_type const & i)
103 // we write only true attribute values so we remove a bit of the
104 // file format bloat for tabulars.
105 return i ? write_attribute(name, convert<string>(i)) : string();
110 string const write_attribute(string const & name, LyXLength const & value)
112 // we write only the value if we really have one same reson as above.
113 return value.zero() ? string() : write_attribute(name, value.asString());
117 string const tostr(LyXAlignment const & num)
122 case LYX_ALIGN_BLOCK:
126 case LYX_ALIGN_CENTER:
128 case LYX_ALIGN_RIGHT:
130 case LYX_ALIGN_LAYOUT:
132 case LYX_ALIGN_SPECIAL:
139 string const tostr(LyXTabular::VAlignment const & num)
142 case LyXTabular::LYX_VALIGN_TOP:
144 case LyXTabular::LYX_VALIGN_MIDDLE:
146 case LyXTabular::LYX_VALIGN_BOTTOM:
153 string const tostr(LyXTabular::BoxType const & num)
156 case LyXTabular::BOX_NONE:
158 case LyXTabular::BOX_PARBOX:
160 case LyXTabular::BOX_MINIPAGE:
167 // I would have liked a fromstr template a lot better. (Lgb)
168 bool string2type(string const str, LyXAlignment & num)
171 num = LYX_ALIGN_NONE;
172 else if (str == "block")
173 num = LYX_ALIGN_BLOCK;
174 else if (str == "left")
175 num = LYX_ALIGN_LEFT;
176 else if (str == "center")
177 num = LYX_ALIGN_CENTER;
178 else if (str == "right")
179 num = LYX_ALIGN_RIGHT;
186 bool string2type(string const str, LyXTabular::VAlignment & num)
189 num = LyXTabular::LYX_VALIGN_TOP;
190 else if (str == "middle" )
191 num = LyXTabular::LYX_VALIGN_MIDDLE;
192 else if (str == "bottom")
193 num = LyXTabular::LYX_VALIGN_BOTTOM;
200 bool string2type(string const str, LyXTabular::BoxType & num)
203 num = LyXTabular::BOX_NONE;
204 else if (str == "parbox")
205 num = LyXTabular::BOX_PARBOX;
206 else if (str == "minipage")
207 num = LyXTabular::BOX_MINIPAGE;
214 bool string2type(string const str, bool & num)
218 else if (str == "false")
226 bool getTokenValue(string const & str, char const * token, string & ret)
229 size_t token_length = strlen(token);
230 string::size_type pos = str.find(token);
232 if (pos == string::npos || pos + token_length + 1 >= str.length()
233 || str[pos + token_length] != '=')
235 pos += token_length + 1;
237 if (ch != '"' && ch != '\'') { // only read till next space
241 while (pos < str.length() - 1 && str[++pos] != ch)
248 bool getTokenValue(string const & str, char const * token, int & num)
252 if (!getTokenValue(str, token, tmp))
254 num = convert<int>(tmp);
259 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
262 return getTokenValue(str, token, tmp) && string2type(tmp, num);
266 bool getTokenValue(string const & str, char const * token,
267 LyXTabular::VAlignment & num)
270 return getTokenValue(str, token, tmp) && string2type(tmp, num);
274 bool getTokenValue(string const & str, char const * token,
275 LyXTabular::BoxType & num)
278 return getTokenValue(str, token, tmp) && string2type(tmp, num);
282 bool getTokenValue(string const & str, char const * token, bool & flag)
284 // set the flag always to false as this should be the default for bools
285 // not in the file-format.
288 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
292 bool getTokenValue(string const & str, char const * token, LyXLength & len)
294 // set the lenght to be zero() as default as this it should be if not
295 // in the file format.
298 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
302 void l_getline(istream & is, string & str)
305 while (str.empty()) {
307 if (!str.empty() && str[str.length() - 1] == '\r')
308 str.erase(str.length() - 1);
314 /// Define a few methods for the inner structs
316 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
319 multicolumn(LyXTabular::CELL_NORMAL),
320 alignment(LYX_ALIGN_CENTER),
321 valignment(LYX_VALIGN_TOP),
328 inset(new InsetText(bp))
332 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
334 width_of_cell(cs.width_of_cell),
335 multicolumn(cs.multicolumn),
336 alignment(cs.alignment),
337 valignment(cs.valignment),
338 top_line(cs.top_line),
339 bottom_line(cs.bottom_line),
340 left_line(cs.left_line),
341 right_line(cs.right_line),
344 align_special(cs.align_special),
346 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
350 LyXTabular::cellstruct &
351 LyXTabular::cellstruct::operator=(cellstruct cs)
359 LyXTabular::cellstruct::swap(cellstruct & rhs)
361 std::swap(cellno, rhs.cellno);
362 std::swap(width_of_cell, rhs.width_of_cell);
363 std::swap(multicolumn, rhs.multicolumn);
364 std::swap(alignment, rhs.alignment);
365 std::swap(valignment, rhs.valignment);
366 std::swap(top_line, rhs.top_line);
367 std::swap(bottom_line, rhs.bottom_line);
368 std::swap(left_line, rhs.left_line);
369 std::swap(right_line, rhs.right_line);
370 std::swap(usebox, rhs.usebox);
371 std::swap(rotate, rhs.rotate);
372 std::swap(align_special, rhs.align_special);
373 p_width.swap(rhs.p_width);
374 inset.swap(rhs.inset);
378 LyXTabular::rowstruct::rowstruct()
391 LyXTabular::columnstruct::columnstruct()
392 : alignment(LYX_ALIGN_CENTER),
393 valignment(LYX_VALIGN_TOP),
401 LyXTabular::ltType::ltType()
408 LyXTabular::LyXTabular(BufferParams const & bp, row_type rows_arg,
409 col_type columns_arg)
411 init(bp, rows_arg, columns_arg);
415 // activates all lines and sets all widths to 0
416 void LyXTabular::init(BufferParams const & bp, row_type rows_arg,
417 col_type columns_arg)
420 columns_ = columns_arg;
421 row_info = row_vector(rows_);
422 column_info = column_vector(columns_);
423 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
424 row_info.reserve(10);
425 column_info.reserve(10);
426 cell_info.reserve(100);
428 for (row_type i = 0; i < rows_; ++i)
429 cell_info[i].back().right_line = true;
430 row_info.back().bottom_line = true;
431 row_info.front().bottom_line = true;
432 column_info.back().right_line = true;
433 is_long_tabular = false;
438 void LyXTabular::fixCellNums()
441 for (row_type i = 0; i < rows_; ++i) {
442 for (col_type j = 0; j < columns_; ++j) {
443 // When debugging it can be nice to set
445 cell_info[i][j].inset->setDrawFrame(false);
446 cell_info[i][j].cellno = cellno++;
448 cell_info[i].back().right_line = true;
451 set_row_column_number_info();
455 void LyXTabular::appendRow(BufferParams const & bp, idx_type const cell)
459 row_type const row = row_of_cell(cell);
461 row_vector::iterator rit = row_info.begin() + row;
462 row_info.insert(rit, rowstruct());
463 // now set the values of the row before
464 row_info[row] = row_info[row + 1];
466 cell_vvector old(rows_ - 1);
467 for (row_type i = 0; i < rows_ - 1; ++i)
468 swap(cell_info[i], old[i]);
470 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
472 for (row_type i = 0; i <= row; ++i)
473 swap(cell_info[i], old[i]);
474 for (row_type i = row + 2; i < rows_; ++i)
475 swap(cell_info[i], old[i - 1]);
477 if (bp.tracking_changes)
478 for (col_type j = 0; j < columns_; ++j)
479 cell_info[row + 1][j].inset->markNew(true);
481 set_row_column_number_info();
485 void LyXTabular::deleteRow(row_type const row)
487 // Not allowed to delete last row
491 row_info.erase(row_info.begin() + row);
492 cell_info.erase(cell_info.begin() + row);
498 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
502 col_type const column = column_of_cell(cell);
503 column_vector::iterator cit = column_info.begin() + column + 1;
504 column_info.insert(cit, columnstruct());
505 // set the column values of the column before
506 column_info[column + 1] = column_info[column];
508 for (row_type i = 0; i < rows_; ++i) {
509 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
511 // care about multicolumns
512 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
513 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
515 if (column + 2 >= columns_
516 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
517 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
520 for (row_type i = 0; i < rows_; ++i) {
521 cell_info[i][column + 1].inset->clear();
522 if (bp.tracking_changes)
523 cell_info[i][column + 1].inset->markNew(true);
529 void LyXTabular::deleteColumn(col_type const column)
531 // Not allowed to delete last column
535 column_info.erase(column_info.begin() + column);
536 for (row_type i = 0; i < rows_; ++i)
537 cell_info[i].erase(cell_info[i].begin() + column);
543 void LyXTabular::set_row_column_number_info()
546 for (row_type row = 0; row < rows_; ++row) {
547 for (col_type column = 0; column < columns_; ++column) {
548 if (cell_info[row][column].multicolumn
549 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
551 if (numberofcells == 0)
552 // FIXME: Is this intended?
553 cell_info[row][column].cellno = npos;
555 cell_info[row][column].cellno =
560 rowofcell.resize(numberofcells);
561 columnofcell.resize(numberofcells);
566 c < numberofcells && row < rows_ && column < columns_;) {
568 columnofcell[c] = column;
572 } while (column < columns_ &&
573 cell_info[row][column].multicolumn
574 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
576 if (column == columns_) {
582 for (row_type row = 0; row < rows_; ++row) {
583 for (col_type column = 0; column < columns_; ++column) {
584 if (isPartOfMultiColumn(row,column))
586 cell_info[row][column].inset->setAutoBreakRows(
587 !getPWidth(getCellNumber(row, column)).zero());
593 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
595 return numberofcells;
599 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
601 row_type const row = row_of_cell(cell);
603 for (col_type i = 0; i < columns_; ++i)
604 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
610 // returns 1 if there is a topline, returns 0 if not
611 bool LyXTabular::topLine(idx_type const cell, bool const onlycolumn) const
613 if (!onlycolumn && isMultiColumn(cell))
614 return cellinfo_of_cell(cell).top_line;
615 return row_info[row_of_cell(cell)].top_line;
619 bool LyXTabular::bottomLine(idx_type const cell, bool onlycolumn) const
621 if (!onlycolumn && isMultiColumn(cell))
622 return cellinfo_of_cell(cell).bottom_line;
623 return row_info[row_of_cell(cell)].bottom_line;
627 bool LyXTabular::leftLine(idx_type cell, bool onlycolumn) const
629 if (!onlycolumn && isMultiColumn(cell) &&
630 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
632 if (cellinfo_of_cell(cell).align_special.empty())
633 return cellinfo_of_cell(cell).left_line;
634 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
636 if (column_info[column_of_cell(cell)].align_special.empty())
637 return column_info[column_of_cell(cell)].left_line;
638 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
642 bool LyXTabular::rightLine(idx_type cell, bool onlycolumn) const
644 if (!onlycolumn && isMultiColumn(cell) &&
645 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
647 if (cellinfo_of_cell(cell).align_special.empty())
648 return cellinfo_of_cell(cell).right_line;
649 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
651 if (column_info[column_of_cell(cell)].align_special.empty())
652 return column_info[right_column_of_cell(cell)].right_line;
653 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
657 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
659 row_type row = row_of_cell(cell);
660 if (row > 0 && !getAdditionalHeight(row)) {
661 col_type column = column_of_cell(cell);
664 && cell_info[row][column].multicolumn
665 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
667 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
668 return row_info[row].bottom_line;
670 return cell_info[row][column].bottom_line;
676 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
678 col_type column = column_of_cell(cell);
680 row_type row = row_of_cell(cell);
682 (cell_info[row][column].multicolumn ==
683 LyXTabular::CELL_PART_OF_MULTICOLUMN));
684 if (getAdditionalWidth(cell_info[row][column].cellno))
686 return rightLine(cell_info[row][column].cellno);
692 bool LyXTabular::isLastRow(idx_type cell) const
694 return row_of_cell(cell) == rows_ - 1;
698 int LyXTabular::getAdditionalHeight(row_type row) const
700 if (!row || row >= rows_)
706 for (col_type column = 0; column < columns_ && bottom; ++column) {
707 switch (cell_info[row - 1][column].multicolumn) {
708 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
709 bottom = cell_info[row - 1][column].bottom_line;
711 case LyXTabular::CELL_NORMAL:
712 bottom = row_info[row - 1].bottom_line;
715 for (col_type column = 0; column < columns_ && top; ++column) {
716 switch (cell_info[row][column].multicolumn) {
717 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
718 top = cell_info[row][column].top_line;
720 case LyXTabular::CELL_NORMAL:
721 top = row_info[row].top_line;
725 return WIDTH_OF_LINE;
730 int LyXTabular::getAdditionalWidth(idx_type cell) const
732 // internally already set in setWidthOfCell
733 // used to get it back in text.C
734 col_type const col = right_column_of_cell(cell);
735 row_type const row = row_of_cell(cell);
736 if (col < columns_ - 1 && rightLine(cell) &&
737 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
739 return WIDTH_OF_LINE;
745 // returns the maximum over all rows
746 int LyXTabular::getWidthOfColumn(idx_type cell) const
748 col_type const column1 = column_of_cell(cell);
749 col_type const column2 = right_column_of_cell(cell);
751 for (col_type i = column1; i <= column2; ++i)
752 result += column_info[i].width_of_column;
757 int LyXTabular::getWidthOfTabular() const
759 return width_of_tabular;
763 // returns true if a complete update is necessary, otherwise false
764 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
766 if (!isMultiColumn(cell))
769 row_type const row = row_of_cell(cell);
770 col_type const column1 = column_of_cell(cell);
771 col_type const column2 = right_column_of_cell(cell);
772 int const old_val = cell_info[row][column2].width_of_cell;
774 // first set columns to 0 so we can calculate the right width
775 for (col_type i = column1; i <= column2; ++i) {
776 cell_info[row][i].width_of_cell = 0;
778 // set the width to MAX_WIDTH until width > 0
779 int width = new_width + 2 * WIDTH_OF_LINE;
780 col_type i = column1;
781 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
782 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
783 width -= column_info[i].width_of_column;
786 cell_info[row][i].width_of_cell = width;
788 if (old_val != cell_info[row][column2].width_of_cell) {
789 // in this case we have to recalculate all multicolumn cells which
790 // have this column as one of theirs but not as last one
791 calculate_width_of_column_NMC(i);
792 recalculateMulticolumnsOfColumn(i);
793 calculate_width_of_column(i);
799 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
801 // the last column does not have to be recalculated because all
802 // multicolumns will have here there last multicolumn cell which
803 // always will have the whole rest of the width of the cell.
804 if (columns_ < 2 || column > (columns_ - 2))
806 for (row_type row = 0; row < rows_; ++row) {
807 int mc = cell_info[row][column].multicolumn;
808 int nmc = cell_info[row][column+1].multicolumn;
809 // we only have to update multicolumns which do not have this
810 // column as their last column!
811 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
812 (mc == CELL_PART_OF_MULTICOLUMN &&
813 nmc == CELL_PART_OF_MULTICOLUMN))
815 idx_type const cellno = cell_info[row][column].cellno;
816 setWidthOfMulticolCell(cellno,
817 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
823 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
825 row_type const row = row_of_cell(cell);
826 col_type const column1 = column_of_cell(cell);
831 if (rightLine(cell_info[row][column1].cellno, true) &&
832 column1 < columns_ - 1 &&
833 leftLine(cell_info[row][column1+1].cellno, true))
835 add_width = WIDTH_OF_LINE;
838 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
841 if (isMultiColumnReal(cell)) {
842 tmp = setWidthOfMulticolCell(cell, new_width);
844 width = new_width + 2 * WIDTH_OF_LINE + add_width;
845 cell_info[row][column1].width_of_cell = width;
846 tmp = calculate_width_of_column_NMC(column1);
848 recalculateMulticolumnsOfColumn(column1);
851 for (col_type i = 0; i < columns_; ++i)
852 calculate_width_of_column(i);
853 calculate_width_of_tabular();
858 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
861 if (!isMultiColumn(cell) || onlycolumn)
862 column_info[column_of_cell(cell)].alignment = align;
864 cellinfo_of_cell(cell).alignment = align;
868 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
871 if (!isMultiColumn(cell) || onlycolumn)
872 column_info[column_of_cell(cell)].valignment = align;
874 cellinfo_of_cell(cell).valignment = align;
881 * Allow line and paragraph breaks for fixed width cells or disallow them,
882 * merge cell paragraphs and reset layout to standard for variable width
885 void toggleFixedWidth(LCursor & cur, InsetText * inset, bool fixedWidth)
887 inset->setAutoBreakRows(fixedWidth);
891 // merge all paragraphs to one
892 BufferParams const & bp =
893 inset->getText(0)->bv_owner->buffer()->params();
894 while (inset->paragraphs().size() > 1)
895 mergeParagraph(bp, inset->paragraphs(), 0);
899 inset->getText(0)->setLayout(0, cur.lastpit() + 1, "Standard");
906 void LyXTabular::setColumnPWidth(LCursor & cur, idx_type cell,
907 LyXLength const & width)
909 col_type const j = column_of_cell(cell);
911 column_info[j].p_width = width;
912 for (row_type i = 0; i < rows_; ++i) {
913 idx_type const cell = getCellNumber(i, j);
914 // because of multicolumns
915 toggleFixedWidth(cur, getCellInset(cell).get(),
916 !getPWidth(cell).zero());
918 // cur paragraph can become invalid after paragraphs were merged
919 if (cur.pit() > cur.lastpit())
920 cur.pit() = cur.lastpit();
921 // cur position can become invalid after newlines were removed
922 if (cur.pos() > cur.lastpos())
923 cur.pos() = cur.lastpos();
927 bool LyXTabular::setMColumnPWidth(LCursor & cur, idx_type cell,
928 LyXLength const & width)
930 if (!isMultiColumn(cell))
933 cellinfo_of_cell(cell).p_width = width;
934 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
935 // cur paragraph can become invalid after paragraphs were merged
936 if (cur.pit() > cur.lastpit())
937 cur.pit() = cur.lastpit();
938 // cur position can become invalid after newlines were removed
939 if (cur.pos() > cur.lastpos())
940 cur.pos() = cur.lastpos();
945 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
946 LyXTabular::Feature what)
948 if (what == SET_SPECIAL_MULTI)
949 cellinfo_of_cell(cell).align_special = special;
951 column_info[column_of_cell(cell)].align_special = special;
955 void LyXTabular::setAllLines(idx_type cell, bool line)
957 setTopLine(cell, line);
958 setBottomLine(cell, line);
959 setRightLine(cell, line);
960 setLeftLine(cell, line);
964 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
966 row_type const row = row_of_cell(cell);
967 if (onlycolumn || !isMultiColumn(cell))
968 row_info[row].top_line = line;
970 cellinfo_of_cell(cell).top_line = line;
974 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
976 if (onlycolumn || !isMultiColumn(cell))
977 row_info[row_of_cell(cell)].bottom_line = line;
979 cellinfo_of_cell(cell).bottom_line = line;
983 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
985 if (onlycolumn || !isMultiColumn(cell))
986 column_info[column_of_cell(cell)].left_line = line;
988 cellinfo_of_cell(cell).left_line = line;
992 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
994 if (onlycolumn || !isMultiColumn(cell))
995 column_info[right_column_of_cell(cell)].right_line = line;
997 cellinfo_of_cell(cell).right_line = line;
1001 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
1003 if (!onlycolumn && isMultiColumn(cell))
1004 return cellinfo_of_cell(cell).alignment;
1005 return column_info[column_of_cell(cell)].alignment;
1009 LyXTabular::VAlignment
1010 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
1012 if (!onlycolumn && isMultiColumn(cell))
1013 return cellinfo_of_cell(cell).valignment;
1014 return column_info[column_of_cell(cell)].valignment;
1018 LyXLength const LyXTabular::getPWidth(idx_type cell) const
1020 if (isMultiColumn(cell))
1021 return cellinfo_of_cell(cell).p_width;
1022 return column_info[column_of_cell(cell)].p_width;
1026 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
1028 return column_info[column_of_cell(cell)].p_width;
1032 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
1034 if (isMultiColumn(cell))
1035 return cellinfo_of_cell(cell).p_width;
1040 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
1042 if (what == SET_SPECIAL_MULTI)
1043 return cellinfo_of_cell(cell).align_special;
1044 return column_info[column_of_cell(cell)].align_special;
1048 int LyXTabular::getWidthOfCell(idx_type cell) const
1050 row_type const row = row_of_cell(cell);
1051 col_type const column1 = column_of_cell(cell);
1052 col_type const column2 = right_column_of_cell(cell);
1054 for (col_type i = column1; i <= column2; ++i)
1055 result += cell_info[row][i].width_of_cell;
1060 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1064 switch (getAlignment(cell)) {
1065 case LYX_ALIGN_CENTER:
1066 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1068 case LYX_ALIGN_RIGHT:
1069 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1070 // + getAdditionalWidth(cell);
1073 // LYX_ALIGN_LEFT: nothing :-)
1077 // the LaTeX Way :-(
1083 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1085 return column_of_cell(cell) == 0;
1089 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1091 if (row > rows_ - 1)
1093 return cell_info[row][0].cellno;
1097 bool LyXTabular::isLastCellInRow(idx_type cell) const
1099 return right_column_of_cell(cell) == columns_ - 1;
1103 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1105 if (row > rows_ - 1)
1107 return cell_info[row][columns_-1].cellno;
1111 void LyXTabular::calculate_width_of_column(col_type column)
1114 for (row_type i = 0; i < rows_; ++i)
1115 maximum = max(cell_info[i][column].width_of_cell, maximum);
1116 column_info[column].width_of_column = maximum;
1121 // Calculate the columns regarding ONLY the normal cells and if this
1122 // column is inside a multicolumn cell then use it only if its the last
1123 // column of this multicolumn cell as this gives an added width to the
1124 // column, all the rest should be adapted!
1126 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1128 int const old_column_width = column_info[column].width_of_column;
1130 for (row_type i = 0; i < rows_; ++i) {
1131 idx_type cell = getCellNumber(i, column);
1132 bool ismulti = isMultiColumnReal(cell);
1133 if ((!ismulti || column == right_column_of_cell(cell)) &&
1134 cell_info[i][column].width_of_cell > max)
1136 max = cell_info[i][column].width_of_cell;
1139 column_info[column].width_of_column = max;
1140 return column_info[column].width_of_column != old_column_width;
1144 void LyXTabular::calculate_width_of_tabular()
1146 width_of_tabular = 0;
1147 for (col_type i = 0; i < columns_; ++i)
1148 width_of_tabular += column_info[i].width_of_column;
1152 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1154 if (cell >= numberofcells)
1158 return rowofcell[cell];
1162 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1164 if (cell >= numberofcells)
1165 return columns_ - 1;
1168 return columnofcell[cell];
1172 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1174 row_type const row = row_of_cell(cell);
1175 col_type column = column_of_cell(cell);
1176 while (column < columns_ - 1 &&
1177 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1183 void LyXTabular::write(Buffer const & buf, ostream & os) const
1187 << write_attribute("version", 3)
1188 << write_attribute("rows", rows_)
1189 << write_attribute("columns", columns_)
1191 // global longtable options
1193 << write_attribute("rotate", rotate)
1194 << write_attribute("islongtable", is_long_tabular)
1195 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1196 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1197 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1198 << write_attribute("headTopDL", endhead.topDL)
1199 << write_attribute("headBottomDL", endhead.bottomDL)
1200 << write_attribute("footTopDL", endfoot.topDL)
1201 << write_attribute("footBottomDL", endfoot.bottomDL)
1202 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1203 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1204 << write_attribute("lastFootEmpty", endlastfoot.empty)
1206 for (col_type j = 0; j < columns_; ++j) {
1208 << write_attribute("alignment", column_info[j].alignment)
1209 << write_attribute("valignment", column_info[j].valignment)
1210 << write_attribute("leftline", column_info[j].left_line)
1211 << write_attribute("rightline", column_info[j].right_line)
1212 << write_attribute("width", column_info[j].p_width.asString())
1213 << write_attribute("special", column_info[j].align_special)
1216 for (row_type i = 0; i < rows_; ++i) {
1218 << write_attribute("topline", row_info[i].top_line)
1219 << write_attribute("bottomline", row_info[i].bottom_line)
1220 << write_attribute("endhead", row_info[i].endhead)
1221 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1222 << write_attribute("endfoot", row_info[i].endfoot)
1223 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1224 << write_attribute("newpage", row_info[i].newpage)
1226 for (col_type j = 0; j < columns_; ++j) {
1228 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1229 << write_attribute("alignment", cell_info[i][j].alignment)
1230 << write_attribute("valignment", cell_info[i][j].valignment)
1231 << write_attribute("topline", cell_info[i][j].top_line)
1232 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1233 << write_attribute("leftline", cell_info[i][j].left_line)
1234 << write_attribute("rightline", cell_info[i][j].right_line)
1235 << write_attribute("rotate", cell_info[i][j].rotate)
1236 << write_attribute("usebox", cell_info[i][j].usebox)
1237 << write_attribute("width", cell_info[i][j].p_width)
1238 << write_attribute("special", cell_info[i][j].align_special)
1240 os << "\\begin_inset ";
1241 cell_info[i][j].inset->write(buf, os);
1242 os << "\n\\end_inset\n"
1247 os << "</lyxtabular>\n";
1251 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1254 istream & is = lex.getStream();
1256 l_getline(is, line);
1257 if (!prefixIs(line, "<lyxtabular ")
1258 && !prefixIs(line, "<LyXTabular ")) {
1259 BOOST_ASSERT(false);
1264 if (!getTokenValue(line, "version", version))
1266 BOOST_ASSERT(version >= 2);
1269 if (!getTokenValue(line, "rows", rows_arg))
1272 if (!getTokenValue(line, "columns", columns_arg))
1274 init(buf.params(), rows_arg, columns_arg);
1275 l_getline(is, line);
1276 if (!prefixIs(line, "<features")) {
1277 lyxerr << "Wrong tabular format (expected <features ...> got"
1278 << line << ')' << endl;
1281 getTokenValue(line, "rotate", rotate);
1282 getTokenValue(line, "islongtable", is_long_tabular);
1283 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1284 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1285 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1286 getTokenValue(line, "headTopDL", endhead.topDL);
1287 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1288 getTokenValue(line, "footTopDL", endfoot.topDL);
1289 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1290 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1291 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1292 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1294 for (col_type j = 0; j < columns_; ++j) {
1296 if (!prefixIs(line,"<column")) {
1297 lyxerr << "Wrong tabular format (expected <column ...> got"
1298 << line << ')' << endl;
1301 getTokenValue(line, "alignment", column_info[j].alignment);
1302 getTokenValue(line, "valignment", column_info[j].valignment);
1303 getTokenValue(line, "leftline", column_info[j].left_line);
1304 getTokenValue(line, "rightline", column_info[j].right_line);
1305 getTokenValue(line, "width", column_info[j].p_width);
1306 getTokenValue(line, "special", column_info[j].align_special);
1309 for (row_type i = 0; i < rows_; ++i) {
1310 l_getline(is, line);
1311 if (!prefixIs(line, "<row")) {
1312 lyxerr << "Wrong tabular format (expected <row ...> got"
1313 << line << ')' << endl;
1316 getTokenValue(line, "topline", row_info[i].top_line);
1317 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1318 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1319 getTokenValue(line, "endhead", row_info[i].endhead);
1320 getTokenValue(line, "endfoot", row_info[i].endfoot);
1321 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1322 getTokenValue(line, "newpage", row_info[i].newpage);
1323 for (col_type j = 0; j < columns_; ++j) {
1324 l_getline(is, line);
1325 if (!prefixIs(line, "<cell")) {
1326 lyxerr << "Wrong tabular format (expected <cell ...> got"
1327 << line << ')' << endl;
1330 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1331 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1332 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1333 getTokenValue(line, "topline", cell_info[i][j].top_line);
1334 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1335 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1336 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1337 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1338 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1339 getTokenValue(line, "width", cell_info[i][j].p_width);
1340 getTokenValue(line, "special", cell_info[i][j].align_special);
1341 l_getline(is, line);
1342 if (prefixIs(line, "\\begin_inset")) {
1343 cell_info[i][j].inset->read(buf, lex);
1344 l_getline(is, line);
1346 if (!prefixIs(line, "</cell>")) {
1347 lyxerr << "Wrong tabular format (expected </cell> got"
1348 << line << ')' << endl;
1352 l_getline(is, line);
1353 if (!prefixIs(line, "</row>")) {
1354 lyxerr << "Wrong tabular format (expected </row> got"
1355 << line << ')' << endl;
1359 while (!prefixIs(line, "</lyxtabular>")) {
1360 l_getline(is, line);
1362 set_row_column_number_info();
1366 bool LyXTabular::isMultiColumn(idx_type cell) const
1368 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1372 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1374 return column_of_cell(cell) != right_column_of_cell(cell) &&
1375 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1379 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1381 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1385 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1388 cellstruct & cs = cellinfo_of_cell(cell);
1389 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1390 cs.alignment = column_info[column_of_cell(cell)].alignment;
1391 cs.top_line = row_info[row_of_cell(cell)].top_line;
1392 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1393 cs.left_line = column_info[column_of_cell(cell)].left_line;
1394 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1395 for (idx_type i = 1; i < number; ++i) {
1396 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1397 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1398 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1401 set_row_column_number_info();
1405 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1407 row_type const row = row_of_cell(cell);
1408 col_type column = column_of_cell(cell);
1409 idx_type result = 1;
1411 while (column < columns_ &&
1412 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1421 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1423 row_type const row = row_of_cell(cell);
1424 col_type column = column_of_cell(cell);
1426 idx_type result = 0;
1428 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1429 cell_info[row][column].multicolumn = CELL_NORMAL;
1431 while (column < columns_ &&
1432 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1434 cell_info[row][column].multicolumn = CELL_NORMAL;
1439 set_row_column_number_info();
1444 void LyXTabular::setLongTabular(bool what)
1446 is_long_tabular = what;
1450 bool LyXTabular::isLongTabular() const
1452 return is_long_tabular;
1456 void LyXTabular::setRotateTabular(bool flag)
1462 bool LyXTabular::getRotateTabular() const
1468 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1470 cellinfo_of_cell(cell).rotate = flag;
1474 bool LyXTabular::getRotateCell(idx_type cell) const
1476 return cellinfo_of_cell(cell).rotate;
1480 bool LyXTabular::needRotating() const
1484 for (row_type i = 0; i < rows_; ++i)
1485 for (col_type j = 0; j < columns_; ++j)
1486 if (cell_info[i][j].rotate)
1492 bool LyXTabular::isLastCell(idx_type cell) const
1494 if (cell + 1 < numberofcells)
1500 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1502 if (row_of_cell(cell) > 0)
1503 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1508 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1510 if (row_of_cell(cell) + 1 < rows_)
1511 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1516 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1518 if (row_of_cell(cell) == 0)
1520 if (!isMultiColumn(cell))
1521 return getCellAbove(cell);
1522 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1526 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1528 if (row_of_cell(cell) + 1 >= rows_)
1530 if (!isMultiColumn(cell))
1531 return getCellBelow(cell);
1532 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1536 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1537 col_type column) const
1539 BOOST_ASSERT(column != npos && column < columns_ &&
1540 row != npos && row < rows_);
1541 return cell_info[row][column].cellno;
1545 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1547 cellinfo_of_cell(cell).usebox = type;
1551 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1553 if (column_info[column_of_cell(cell)].p_width.zero() &&
1554 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1556 if (cellinfo_of_cell(cell).usebox > 1)
1557 return cellinfo_of_cell(cell).usebox;
1558 return useParbox(cell);
1563 // This are functions used for the longtable support
1565 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1571 row_info[row].endfirsthead = flag;
1575 row_info[row].endhead = flag;
1580 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1583 hd.set = haveLTHead();
1584 return row_info[row].endhead;
1588 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1591 hd.set = haveLTFirstHead();
1592 return row_info[row].endfirsthead;
1596 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1602 row_info[row].endlastfoot = flag;
1606 row_info[row].endfoot = flag;
1611 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1614 fd.set = haveLTFoot();
1615 return row_info[row].endfoot;
1619 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1622 fd.set = haveLTLastFoot();
1623 return row_info[row].endlastfoot;
1627 void LyXTabular::setLTNewPage(row_type row, bool what)
1629 row_info[row].newpage = what;
1633 bool LyXTabular::getLTNewPage(row_type row) const
1635 return row_info[row].newpage;
1639 bool LyXTabular::haveLTHead() const
1641 for (row_type i = 0; i < rows_; ++i)
1642 if (row_info[i].endhead)
1648 bool LyXTabular::haveLTFirstHead() const
1650 if (endfirsthead.empty)
1652 for (row_type i = 0; i < rows_; ++i)
1653 if (row_info[i].endfirsthead)
1659 bool LyXTabular::haveLTFoot() const
1661 for (row_type i = 0; i < rows_; ++i)
1662 if (row_info[i].endfoot)
1668 bool LyXTabular::haveLTLastFoot() const
1670 if (endlastfoot.empty)
1672 for (row_type i = 0; i < rows_; ++i)
1673 if (row_info[i].endlastfoot)
1679 // end longtable support functions
1681 void LyXTabular::setAscentOfRow(row_type row, int height)
1683 if (row >= rows_ || row_info[row].ascent_of_row == height)
1685 row_info[row].ascent_of_row = height;
1689 void LyXTabular::setDescentOfRow(row_type row, int height)
1691 if (row >= rows_ || row_info[row].descent_of_row == height)
1693 row_info[row].descent_of_row = height;
1697 int LyXTabular::getAscentOfRow(row_type row) const
1701 return row_info[row].ascent_of_row;
1705 int LyXTabular::getDescentOfRow(row_type row) const
1707 BOOST_ASSERT(row < rows_);
1708 return row_info[row].descent_of_row;
1712 int LyXTabular::getHeightOfTabular() const
1715 for (row_type row = 0; row < rows_; ++row)
1716 height += getAscentOfRow(row) + getDescentOfRow(row) +
1717 getAdditionalHeight(row);
1722 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1724 BOOST_ASSERT(row < rows_);
1725 BOOST_ASSERT(column < columns_);
1726 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1730 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1732 // FIXME: assert or return 0 as in TeXBottomHLine()?
1733 BOOST_ASSERT(row != npos);
1734 BOOST_ASSERT(row < rows_);
1736 idx_type const fcell = getFirstCellInRow(row);
1737 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1740 for (idx_type i = fcell; i < n; ++i) {
1744 if (tmp == n - fcell) {
1747 for (idx_type i = fcell; i < n; ++i) {
1750 << column_of_cell(i) + 1
1752 << right_column_of_cell(i) + 1
1764 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1766 // FIXME: return 0 or assert as in TeXTopHLine()?
1767 if (row == npos || row >= rows_)
1770 idx_type const fcell = getFirstCellInRow(row);
1771 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1774 for (idx_type i = fcell; i < n; ++i) {
1778 if (tmp == n - fcell) {
1781 for (idx_type i = fcell; i < n; ++i) {
1782 if (bottomLine(i)) {
1784 << column_of_cell(i) + 1
1786 << right_column_of_cell(i) + 1
1798 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1802 if (getRotateCell(cell)) {
1803 os << "\\begin{sideways}\n";
1806 if (isMultiColumn(cell)) {
1807 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1808 if (!cellinfo_of_cell(cell).align_special.empty()) {
1809 os << cellinfo_of_cell(cell).align_special << "}{";
1811 if (leftLine(cell) &&
1812 (isFirstCellInRow(cell) ||
1813 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1814 !rightLine(cell - 1, true))))
1818 if (!getPWidth(cell).zero()) {
1819 switch (getVAlignment(cell)) {
1820 case LYX_VALIGN_TOP:
1823 case LYX_VALIGN_MIDDLE:
1826 case LYX_VALIGN_BOTTOM:
1831 << getPWidth(cell).asLatexString()
1834 switch (getAlignment(cell)) {
1835 case LYX_ALIGN_LEFT:
1838 case LYX_ALIGN_RIGHT:
1846 if (rightLine(cell))
1848 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1854 if (getUsebox(cell) == BOX_PARBOX) {
1856 switch (getVAlignment(cell)) {
1857 case LYX_VALIGN_TOP:
1860 case LYX_VALIGN_MIDDLE:
1863 case LYX_VALIGN_BOTTOM:
1867 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1868 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1869 os << "\\begin{minipage}[";
1870 switch (getVAlignment(cell)) {
1871 case LYX_VALIGN_TOP:
1874 case LYX_VALIGN_MIDDLE:
1877 case LYX_VALIGN_BOTTOM:
1881 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1888 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1893 if (getUsebox(cell) == BOX_PARBOX)
1895 else if (getUsebox(cell) == BOX_MINIPAGE) {
1896 os << "%\n\\end{minipage}";
1899 if (isMultiColumn(cell)) {
1902 if (getRotateCell(cell)) {
1903 os << "%\n\\end{sideways}";
1910 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1911 OutputParams const & runparams) const
1913 if (!is_long_tabular)
1917 // output header info
1919 if (endhead.topDL) {
1923 for (row_type i = 0; i < rows_; ++i) {
1924 if (row_info[i].endhead) {
1925 ret += TeXRow(os, i, buf, runparams);
1928 if (endhead.bottomDL) {
1932 os << "\\endhead\n";
1934 if (endfirsthead.empty) {
1935 os << "\\endfirsthead\n";
1939 // output firstheader info
1940 if (haveLTFirstHead()) {
1941 if (endfirsthead.topDL) {
1945 for (row_type i = 0; i < rows_; ++i) {
1946 if (row_info[i].endfirsthead) {
1947 ret += TeXRow(os, i, buf, runparams);
1950 if (endfirsthead.bottomDL) {
1954 os << "\\endfirsthead\n";
1957 // output footer info
1959 if (endfoot.topDL) {
1963 for (row_type i = 0; i < rows_; ++i) {
1964 if (row_info[i].endfoot) {
1965 ret += TeXRow(os, i, buf, runparams);
1968 if (endfoot.bottomDL) {
1972 os << "\\endfoot\n";
1974 if (endlastfoot.empty) {
1975 os << "\\endlastfoot\n";
1979 // output lastfooter info
1980 if (haveLTLastFoot()) {
1981 if (endlastfoot.topDL) {
1985 for (row_type i = 0; i < rows_; ++i) {
1986 if (row_info[i].endlastfoot) {
1987 ret += TeXRow(os, i, buf, runparams);
1990 if (endlastfoot.bottomDL) {
1994 os << "\\endlastfoot\n";
2001 bool LyXTabular::isValidRow(row_type row) const
2003 if (!is_long_tabular)
2005 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2006 !row_info[row].endfoot && !row_info[row].endlastfoot;
2010 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
2011 OutputParams const & runparams) const
2013 idx_type cell = getCellNumber(i, 0);
2015 int ret = TeXTopHLine(os, i);
2016 for (col_type j = 0; j < columns_; ++j) {
2017 if (isPartOfMultiColumn(i, j))
2019 ret += TeXCellPreamble(os, cell);
2020 shared_ptr<InsetText> inset = getCellInset(cell);
2022 Paragraph const & par = inset->paragraphs().front();
2023 bool rtl = par.isRightToLeftPar(buf.params())
2025 && getPWidth(cell).zero();
2029 ret += inset->latex(buf, os, runparams);
2033 ret += TeXCellPostamble(os, cell);
2034 if (!isLastCellInRow(cell)) { // not last cell in row
2040 os << "\\tabularnewline\n";
2042 ret += TeXBottomHLine(os, i);
2047 int LyXTabular::latex(Buffer const & buf, ostream & os,
2048 OutputParams const & runparams) const
2052 //+---------------------------------------------------------------------
2053 //+ first the opening preamble +
2054 //+---------------------------------------------------------------------
2057 os << "\\begin{sideways}\n";
2060 if (is_long_tabular)
2061 os << "\\begin{longtable}{";
2063 os << "\\begin{tabular}{";
2064 for (col_type i = 0; i < columns_; ++i) {
2065 if (!column_info[i].align_special.empty()) {
2066 os << column_info[i].align_special;
2068 if (column_info[i].left_line)
2070 if (!column_info[i].p_width.zero()) {
2071 switch (column_info[i].alignment) {
2072 case LYX_ALIGN_LEFT:
2073 os << ">{\\raggedright}";
2075 case LYX_ALIGN_RIGHT:
2076 os << ">{\\raggedleft}";
2078 case LYX_ALIGN_CENTER:
2079 os << ">{\\centering}";
2081 case LYX_ALIGN_NONE:
2082 case LYX_ALIGN_BLOCK:
2083 case LYX_ALIGN_LAYOUT:
2084 case LYX_ALIGN_SPECIAL:
2088 switch (column_info[i].valignment) {
2089 case LYX_VALIGN_TOP:
2092 case LYX_VALIGN_MIDDLE:
2095 case LYX_VALIGN_BOTTOM:
2100 << column_info[i].p_width.asLatexString()
2103 switch (column_info[i].alignment) {
2104 case LYX_ALIGN_LEFT:
2107 case LYX_ALIGN_RIGHT:
2115 if (column_info[i].right_line)
2122 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2124 //+---------------------------------------------------------------------
2125 //+ the single row and columns (cells) +
2126 //+---------------------------------------------------------------------
2128 for (row_type i = 0; i < rows_; ++i) {
2129 if (isValidRow(i)) {
2130 ret += TeXRow(os, i, buf, runparams);
2131 if (is_long_tabular && row_info[i].newpage) {
2132 os << "\\newpage\n";
2138 //+---------------------------------------------------------------------
2139 //+ the closing of the tabular +
2140 //+---------------------------------------------------------------------
2142 if (is_long_tabular)
2143 os << "\\end{longtable}";
2145 os << "\\end{tabular}";
2147 os << "\n\\end{sideways}";
2155 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2156 const OutputParams & runparams) const
2158 os << "<tabular ca=\"";
2159 for (col_type i = 0; i < columns_; ++i) {
2160 switch (column_info[i].alignment) {
2161 case LYX_ALIGN_LEFT:
2164 case LYX_ALIGN_RIGHT:
2175 for (row_type i = 0; i < rows_; ++i) {
2176 for (col_type j = 0; j < columns_; ++j) {
2177 if (isPartOfMultiColumn(i, j))
2179 shared_ptr<InsetText> inset = getCellInset(cell);
2181 ret += inset->linuxdoc(buf, os, runparams);
2183 if (isLastCellInRow(cell)) {
2192 os << "</tabular>\n";
2197 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2198 OutputParams const & runparams) const
2201 idx_type cell = getFirstCellInRow(row);
2204 for (col_type j = 0; j < columns_; ++j) {
2205 if (isPartOfMultiColumn(row, j))
2208 os << "<entry align=\"";
2209 switch (getAlignment(cell)) {
2210 case LYX_ALIGN_LEFT:
2213 case LYX_ALIGN_RIGHT:
2221 os << "\" valign=\"";
2222 switch (getVAlignment(cell)) {
2223 case LYX_VALIGN_TOP:
2226 case LYX_VALIGN_BOTTOM:
2229 case LYX_VALIGN_MIDDLE:
2234 if (isMultiColumn(cell)) {
2235 os << " namest=\"col" << j << "\" ";
2236 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2240 ret += getCellInset(cell)->docbook(buf, os, runparams);
2249 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2250 OutputParams const & runparams) const
2254 //+---------------------------------------------------------------------
2255 //+ first the opening preamble +
2256 //+---------------------------------------------------------------------
2258 os << "<tgroup cols=\"" << columns_
2259 << "\" colsep=\"1\" rowsep=\"1\">\n";
2261 for (col_type i = 0; i < columns_; ++i) {
2262 os << "<colspec colname=\"col" << i << "\" align=\"";
2263 switch (column_info[i].alignment) {
2264 case LYX_ALIGN_LEFT:
2267 case LYX_ALIGN_RIGHT:
2275 if (runparams.flavor == OutputParams::XML)
2281 //+---------------------------------------------------------------------
2282 //+ Long Tabular case +
2283 //+---------------------------------------------------------------------
2285 // output header info
2286 if (haveLTHead() || haveLTFirstHead()) {
2289 for (row_type i = 0; i < rows_; ++i) {
2290 if (row_info[i].endhead || row_info[i].endfirsthead) {
2291 ret += docbookRow(buf, os, i, runparams);
2297 // output footer info
2298 if (haveLTFoot() || haveLTLastFoot()) {
2301 for (row_type i = 0; i < rows_; ++i) {
2302 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2303 ret += docbookRow(buf, os, i, runparams);
2310 //+---------------------------------------------------------------------
2311 //+ the single row and columns (cells) +
2312 //+---------------------------------------------------------------------
2316 for (row_type i = 0; i < rows_; ++i) {
2317 if (isValidRow(i)) {
2318 ret += docbookRow(buf, os, i, runparams);
2323 //+---------------------------------------------------------------------
2324 //+ the closing of the tabular +
2325 //+---------------------------------------------------------------------
2334 int LyXTabular::asciiTopHLine(ostream & os, row_type row,
2335 vector<unsigned int> const & clen) const
2337 idx_type const fcell = getFirstCellInRow(row);
2338 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2341 for (idx_type i = fcell; i < n; ++i) {
2351 for (idx_type i = fcell; i < n; ++i) {
2362 col_type column = column_of_cell(i);
2363 int len = clen[column];
2364 while (column < columns_ - 1
2365 && isPartOfMultiColumn(row, ++column))
2366 len += clen[column] + 4;
2367 os << string(len, ch);
2382 int LyXTabular::asciiBottomHLine(ostream & os, row_type row,
2383 vector<unsigned int> const & clen) const
2385 idx_type const fcell = getFirstCellInRow(row);
2386 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2389 for (idx_type i = fcell; i < n; ++i) {
2390 if (bottomLine(i)) {
2399 for (idx_type i = fcell; i < n; ++i) {
2400 if (bottomLine(i)) {
2410 col_type column = column_of_cell(i);
2411 int len = clen[column];
2412 while (column < columns_ -1
2413 && isPartOfMultiColumn(row, ++column))
2414 len += clen[column] + 4;
2415 os << string(len, ch);
2416 if (bottomLine(i)) {
2430 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2431 OutputParams const & runparams,
2432 idx_type cell, row_type row, col_type column,
2433 vector<unsigned int> const & clen,
2434 bool onlydata) const
2437 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2449 unsigned int len1 = sstr.str().length();
2450 unsigned int len2 = clen[column];
2451 while (column < columns_ -1
2452 && isPartOfMultiColumn(row, ++column))
2453 len2 += clen[column] + 4;
2456 switch (getAlignment(cell)) {
2458 case LYX_ALIGN_LEFT:
2461 case LYX_ALIGN_RIGHT:
2465 case LYX_ALIGN_CENTER:
2471 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2473 if (rightLine(cell))
2482 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2483 OutputParams const & runparams,
2485 bool onlydata, unsigned char delim) const
2489 // first calculate the width of the single columns
2490 vector<unsigned int> clen(columns_);
2493 // first all non (real) multicolumn cells!
2494 for (col_type j = 0; j < columns_; ++j) {
2496 for (row_type i = 0; i < rows_; ++i) {
2497 idx_type cell = getCellNumber(i, j);
2498 if (isMultiColumnReal(cell))
2501 getCellInset(cell)->plaintext(buf, sstr, runparams);
2502 if (clen[j] < sstr.str().length())
2503 clen[j] = sstr.str().length();
2506 // then all (real) multicolumn cells!
2507 for (col_type j = 0; j < columns_; ++j) {
2508 for (row_type i = 0; i < rows_; ++i) {
2509 idx_type cell = getCellNumber(i, j);
2510 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2513 getCellInset(cell)->plaintext(buf, sstr, runparams);
2514 int len = int(sstr.str().length());
2515 idx_type const n = cells_in_multicolumn(cell);
2516 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2518 if (len > int(clen[j + n - 1]))
2519 clen[j + n - 1] = len;
2524 for (row_type i = 0; i < rows_; ++i) {
2525 if (!onlydata && asciiTopHLine(os, i, clen))
2526 os << string(depth * 2, ' ');
2527 for (col_type j = 0; j < columns_; ++j) {
2528 if (isPartOfMultiColumn(i, j))
2530 if (onlydata && j > 0)
2532 ret += asciiPrintCell(buf, os, runparams,
2533 cell, i, j, clen, onlydata);
2538 os << string(depth * 2, ' ');
2539 if (asciiBottomHLine(os, i, clen))
2540 os << string(depth * 2, ' ');
2547 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2549 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2553 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2554 col_type column) const
2556 return cell_info[row][column].inset;
2560 void LyXTabular::setCellInset(row_type row, col_type column,
2561 shared_ptr<InsetText> ins) const
2563 cell_info[row][column].inset = ins;
2567 LyXTabular::idx_type
2568 LyXTabular::getCellFromInset(InsetBase const * inset) const
2570 // is this inset part of the tabular?
2572 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2573 BOOST_ASSERT(false);
2576 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2577 if (getCellInset(cell).get() == inset) {
2578 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2579 << "cell=" << cell << endl;
2583 // We should have found a cell at this point
2584 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2585 << inset << " not found!" << endl;
2586 BOOST_ASSERT(false);
2592 void LyXTabular::validate(LaTeXFeatures & features) const
2594 features.require("NeedTabularnewline");
2595 if (isLongTabular())
2596 features.require("longtable");
2598 features.require("rotating");
2599 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2600 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2601 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2602 features.require("array");
2603 getCellInset(cell)->validate(features);
2608 void LyXTabular::getLabelList(Buffer const & buffer,
2609 std::vector<string> & list) const
2611 for (row_type i = 0; i < rows_; ++i)
2612 for (col_type j = 0; j < columns_; ++j)
2613 getCellInset(i, j)->getLabelList(buffer, list);
2617 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2619 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2620 ParagraphList::const_iterator cit = parlist.begin();
2621 ParagraphList::const_iterator end = parlist.end();
2623 for (; cit != end; ++cit)
2624 for (int i = 0; i < cit->size(); ++i)
2625 if (cit->isNewline(i))