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 // undo information has already been recorded
900 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
901 bp.getLyXTextClass().defaultLayoutName());
908 void LyXTabular::setColumnPWidth(LCursor & cur, idx_type cell,
909 LyXLength const & width)
911 col_type const j = column_of_cell(cell);
913 column_info[j].p_width = width;
914 for (row_type i = 0; i < rows_; ++i) {
915 idx_type const cell = getCellNumber(i, j);
916 // because of multicolumns
917 toggleFixedWidth(cur, getCellInset(cell).get(),
918 !getPWidth(cell).zero());
920 // cur paragraph can become invalid after paragraphs were merged
921 if (cur.pit() > cur.lastpit())
922 cur.pit() = cur.lastpit();
923 // cur position can become invalid after newlines were removed
924 if (cur.pos() > cur.lastpos())
925 cur.pos() = cur.lastpos();
929 bool LyXTabular::setMColumnPWidth(LCursor & cur, idx_type cell,
930 LyXLength const & width)
932 if (!isMultiColumn(cell))
935 cellinfo_of_cell(cell).p_width = width;
936 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
937 // cur paragraph can become invalid after paragraphs were merged
938 if (cur.pit() > cur.lastpit())
939 cur.pit() = cur.lastpit();
940 // cur position can become invalid after newlines were removed
941 if (cur.pos() > cur.lastpos())
942 cur.pos() = cur.lastpos();
947 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
948 LyXTabular::Feature what)
950 if (what == SET_SPECIAL_MULTI)
951 cellinfo_of_cell(cell).align_special = special;
953 column_info[column_of_cell(cell)].align_special = special;
957 void LyXTabular::setAllLines(idx_type cell, bool line)
959 setTopLine(cell, line);
960 setBottomLine(cell, line);
961 setRightLine(cell, line);
962 setLeftLine(cell, line);
966 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
968 row_type const row = row_of_cell(cell);
969 if (onlycolumn || !isMultiColumn(cell))
970 row_info[row].top_line = line;
972 cellinfo_of_cell(cell).top_line = line;
976 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
978 if (onlycolumn || !isMultiColumn(cell))
979 row_info[row_of_cell(cell)].bottom_line = line;
981 cellinfo_of_cell(cell).bottom_line = line;
985 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
987 if (onlycolumn || !isMultiColumn(cell))
988 column_info[column_of_cell(cell)].left_line = line;
990 cellinfo_of_cell(cell).left_line = line;
994 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
996 if (onlycolumn || !isMultiColumn(cell))
997 column_info[right_column_of_cell(cell)].right_line = line;
999 cellinfo_of_cell(cell).right_line = line;
1003 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
1005 if (!onlycolumn && isMultiColumn(cell))
1006 return cellinfo_of_cell(cell).alignment;
1007 return column_info[column_of_cell(cell)].alignment;
1011 LyXTabular::VAlignment
1012 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
1014 if (!onlycolumn && isMultiColumn(cell))
1015 return cellinfo_of_cell(cell).valignment;
1016 return column_info[column_of_cell(cell)].valignment;
1020 LyXLength const LyXTabular::getPWidth(idx_type cell) const
1022 if (isMultiColumn(cell))
1023 return cellinfo_of_cell(cell).p_width;
1024 return column_info[column_of_cell(cell)].p_width;
1028 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
1030 return column_info[column_of_cell(cell)].p_width;
1034 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
1036 if (isMultiColumn(cell))
1037 return cellinfo_of_cell(cell).p_width;
1042 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
1044 if (what == SET_SPECIAL_MULTI)
1045 return cellinfo_of_cell(cell).align_special;
1046 return column_info[column_of_cell(cell)].align_special;
1050 int LyXTabular::getWidthOfCell(idx_type cell) const
1052 row_type const row = row_of_cell(cell);
1053 col_type const column1 = column_of_cell(cell);
1054 col_type const column2 = right_column_of_cell(cell);
1056 for (col_type i = column1; i <= column2; ++i)
1057 result += cell_info[row][i].width_of_cell;
1062 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1066 switch (getAlignment(cell)) {
1067 case LYX_ALIGN_CENTER:
1068 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1070 case LYX_ALIGN_RIGHT:
1071 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1072 // + getAdditionalWidth(cell);
1075 // LYX_ALIGN_LEFT: nothing :-)
1079 // the LaTeX Way :-(
1085 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1087 return column_of_cell(cell) == 0;
1091 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1093 if (row > rows_ - 1)
1095 return cell_info[row][0].cellno;
1099 bool LyXTabular::isLastCellInRow(idx_type cell) const
1101 return right_column_of_cell(cell) == columns_ - 1;
1105 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1107 if (row > rows_ - 1)
1109 return cell_info[row][columns_-1].cellno;
1113 void LyXTabular::calculate_width_of_column(col_type column)
1116 for (row_type i = 0; i < rows_; ++i)
1117 maximum = max(cell_info[i][column].width_of_cell, maximum);
1118 column_info[column].width_of_column = maximum;
1123 // Calculate the columns regarding ONLY the normal cells and if this
1124 // column is inside a multicolumn cell then use it only if its the last
1125 // column of this multicolumn cell as this gives an added width to the
1126 // column, all the rest should be adapted!
1128 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1130 int const old_column_width = column_info[column].width_of_column;
1132 for (row_type i = 0; i < rows_; ++i) {
1133 idx_type cell = getCellNumber(i, column);
1134 bool ismulti = isMultiColumnReal(cell);
1135 if ((!ismulti || column == right_column_of_cell(cell)) &&
1136 cell_info[i][column].width_of_cell > max)
1138 max = cell_info[i][column].width_of_cell;
1141 column_info[column].width_of_column = max;
1142 return column_info[column].width_of_column != old_column_width;
1146 void LyXTabular::calculate_width_of_tabular()
1148 width_of_tabular = 0;
1149 for (col_type i = 0; i < columns_; ++i)
1150 width_of_tabular += column_info[i].width_of_column;
1154 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1156 if (cell >= numberofcells)
1160 return rowofcell[cell];
1164 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1166 if (cell >= numberofcells)
1167 return columns_ - 1;
1170 return columnofcell[cell];
1174 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1176 row_type const row = row_of_cell(cell);
1177 col_type column = column_of_cell(cell);
1178 while (column < columns_ - 1 &&
1179 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1185 void LyXTabular::write(Buffer const & buf, ostream & os) const
1189 << write_attribute("version", 3)
1190 << write_attribute("rows", rows_)
1191 << write_attribute("columns", columns_)
1193 // global longtable options
1195 << write_attribute("rotate", rotate)
1196 << write_attribute("islongtable", is_long_tabular)
1197 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1198 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1199 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1200 << write_attribute("headTopDL", endhead.topDL)
1201 << write_attribute("headBottomDL", endhead.bottomDL)
1202 << write_attribute("footTopDL", endfoot.topDL)
1203 << write_attribute("footBottomDL", endfoot.bottomDL)
1204 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1205 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1206 << write_attribute("lastFootEmpty", endlastfoot.empty)
1208 for (col_type j = 0; j < columns_; ++j) {
1210 << write_attribute("alignment", column_info[j].alignment)
1211 << write_attribute("valignment", column_info[j].valignment)
1212 << write_attribute("leftline", column_info[j].left_line)
1213 << write_attribute("rightline", column_info[j].right_line)
1214 << write_attribute("width", column_info[j].p_width.asString())
1215 << write_attribute("special", column_info[j].align_special)
1218 for (row_type i = 0; i < rows_; ++i) {
1220 << write_attribute("topline", row_info[i].top_line)
1221 << write_attribute("bottomline", row_info[i].bottom_line)
1222 << write_attribute("endhead", row_info[i].endhead)
1223 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1224 << write_attribute("endfoot", row_info[i].endfoot)
1225 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1226 << write_attribute("newpage", row_info[i].newpage)
1228 for (col_type j = 0; j < columns_; ++j) {
1230 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1231 << write_attribute("alignment", cell_info[i][j].alignment)
1232 << write_attribute("valignment", cell_info[i][j].valignment)
1233 << write_attribute("topline", cell_info[i][j].top_line)
1234 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1235 << write_attribute("leftline", cell_info[i][j].left_line)
1236 << write_attribute("rightline", cell_info[i][j].right_line)
1237 << write_attribute("rotate", cell_info[i][j].rotate)
1238 << write_attribute("usebox", cell_info[i][j].usebox)
1239 << write_attribute("width", cell_info[i][j].p_width)
1240 << write_attribute("special", cell_info[i][j].align_special)
1242 os << "\\begin_inset ";
1243 cell_info[i][j].inset->write(buf, os);
1244 os << "\n\\end_inset\n"
1249 os << "</lyxtabular>\n";
1253 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1256 istream & is = lex.getStream();
1258 l_getline(is, line);
1259 if (!prefixIs(line, "<lyxtabular ")
1260 && !prefixIs(line, "<LyXTabular ")) {
1261 BOOST_ASSERT(false);
1266 if (!getTokenValue(line, "version", version))
1268 BOOST_ASSERT(version >= 2);
1271 if (!getTokenValue(line, "rows", rows_arg))
1274 if (!getTokenValue(line, "columns", columns_arg))
1276 init(buf.params(), rows_arg, columns_arg);
1277 l_getline(is, line);
1278 if (!prefixIs(line, "<features")) {
1279 lyxerr << "Wrong tabular format (expected <features ...> got"
1280 << line << ')' << endl;
1283 getTokenValue(line, "rotate", rotate);
1284 getTokenValue(line, "islongtable", is_long_tabular);
1285 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1286 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1287 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1288 getTokenValue(line, "headTopDL", endhead.topDL);
1289 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1290 getTokenValue(line, "footTopDL", endfoot.topDL);
1291 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1292 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1293 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1294 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1296 for (col_type j = 0; j < columns_; ++j) {
1298 if (!prefixIs(line,"<column")) {
1299 lyxerr << "Wrong tabular format (expected <column ...> got"
1300 << line << ')' << endl;
1303 getTokenValue(line, "alignment", column_info[j].alignment);
1304 getTokenValue(line, "valignment", column_info[j].valignment);
1305 getTokenValue(line, "leftline", column_info[j].left_line);
1306 getTokenValue(line, "rightline", column_info[j].right_line);
1307 getTokenValue(line, "width", column_info[j].p_width);
1308 getTokenValue(line, "special", column_info[j].align_special);
1311 for (row_type i = 0; i < rows_; ++i) {
1312 l_getline(is, line);
1313 if (!prefixIs(line, "<row")) {
1314 lyxerr << "Wrong tabular format (expected <row ...> got"
1315 << line << ')' << endl;
1318 getTokenValue(line, "topline", row_info[i].top_line);
1319 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1320 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1321 getTokenValue(line, "endhead", row_info[i].endhead);
1322 getTokenValue(line, "endfoot", row_info[i].endfoot);
1323 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1324 getTokenValue(line, "newpage", row_info[i].newpage);
1325 for (col_type j = 0; j < columns_; ++j) {
1326 l_getline(is, line);
1327 if (!prefixIs(line, "<cell")) {
1328 lyxerr << "Wrong tabular format (expected <cell ...> got"
1329 << line << ')' << endl;
1332 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1333 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1334 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1335 getTokenValue(line, "topline", cell_info[i][j].top_line);
1336 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1337 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1338 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1339 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1340 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1341 getTokenValue(line, "width", cell_info[i][j].p_width);
1342 getTokenValue(line, "special", cell_info[i][j].align_special);
1343 l_getline(is, line);
1344 if (prefixIs(line, "\\begin_inset")) {
1345 cell_info[i][j].inset->read(buf, lex);
1346 l_getline(is, line);
1348 if (!prefixIs(line, "</cell>")) {
1349 lyxerr << "Wrong tabular format (expected </cell> got"
1350 << line << ')' << endl;
1354 l_getline(is, line);
1355 if (!prefixIs(line, "</row>")) {
1356 lyxerr << "Wrong tabular format (expected </row> got"
1357 << line << ')' << endl;
1361 while (!prefixIs(line, "</lyxtabular>")) {
1362 l_getline(is, line);
1364 set_row_column_number_info();
1368 bool LyXTabular::isMultiColumn(idx_type cell) const
1370 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1374 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1376 return column_of_cell(cell) != right_column_of_cell(cell) &&
1377 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1381 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1383 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1387 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1390 cellstruct & cs = cellinfo_of_cell(cell);
1391 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1392 cs.alignment = column_info[column_of_cell(cell)].alignment;
1393 cs.top_line = row_info[row_of_cell(cell)].top_line;
1394 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1395 cs.left_line = column_info[column_of_cell(cell)].left_line;
1396 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1397 for (idx_type i = 1; i < number; ++i) {
1398 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1399 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1400 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1403 set_row_column_number_info();
1407 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1409 row_type const row = row_of_cell(cell);
1410 col_type column = column_of_cell(cell);
1411 idx_type result = 1;
1413 while (column < columns_ &&
1414 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1423 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1425 row_type const row = row_of_cell(cell);
1426 col_type column = column_of_cell(cell);
1428 idx_type result = 0;
1430 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1431 cell_info[row][column].multicolumn = CELL_NORMAL;
1433 while (column < columns_ &&
1434 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1436 cell_info[row][column].multicolumn = CELL_NORMAL;
1441 set_row_column_number_info();
1446 void LyXTabular::setLongTabular(bool what)
1448 is_long_tabular = what;
1452 bool LyXTabular::isLongTabular() const
1454 return is_long_tabular;
1458 void LyXTabular::setRotateTabular(bool flag)
1464 bool LyXTabular::getRotateTabular() const
1470 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1472 cellinfo_of_cell(cell).rotate = flag;
1476 bool LyXTabular::getRotateCell(idx_type cell) const
1478 return cellinfo_of_cell(cell).rotate;
1482 bool LyXTabular::needRotating() const
1486 for (row_type i = 0; i < rows_; ++i)
1487 for (col_type j = 0; j < columns_; ++j)
1488 if (cell_info[i][j].rotate)
1494 bool LyXTabular::isLastCell(idx_type cell) const
1496 if (cell + 1 < numberofcells)
1502 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1504 if (row_of_cell(cell) > 0)
1505 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1510 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1512 if (row_of_cell(cell) + 1 < rows_)
1513 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1518 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1520 if (row_of_cell(cell) == 0)
1522 if (!isMultiColumn(cell))
1523 return getCellAbove(cell);
1524 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1528 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1530 if (row_of_cell(cell) + 1 >= rows_)
1532 if (!isMultiColumn(cell))
1533 return getCellBelow(cell);
1534 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1538 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1539 col_type column) const
1541 BOOST_ASSERT(column != npos && column < columns_ &&
1542 row != npos && row < rows_);
1543 return cell_info[row][column].cellno;
1547 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1549 cellinfo_of_cell(cell).usebox = type;
1553 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1555 if (column_info[column_of_cell(cell)].p_width.zero() &&
1556 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1558 if (cellinfo_of_cell(cell).usebox > 1)
1559 return cellinfo_of_cell(cell).usebox;
1560 return useParbox(cell);
1565 // This are functions used for the longtable support
1567 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1573 row_info[row].endfirsthead = flag;
1577 row_info[row].endhead = flag;
1582 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1585 hd.set = haveLTHead();
1586 return row_info[row].endhead;
1590 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1593 hd.set = haveLTFirstHead();
1594 return row_info[row].endfirsthead;
1598 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1604 row_info[row].endlastfoot = flag;
1608 row_info[row].endfoot = flag;
1613 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1616 fd.set = haveLTFoot();
1617 return row_info[row].endfoot;
1621 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1624 fd.set = haveLTLastFoot();
1625 return row_info[row].endlastfoot;
1629 void LyXTabular::setLTNewPage(row_type row, bool what)
1631 row_info[row].newpage = what;
1635 bool LyXTabular::getLTNewPage(row_type row) const
1637 return row_info[row].newpage;
1641 bool LyXTabular::haveLTHead() const
1643 for (row_type i = 0; i < rows_; ++i)
1644 if (row_info[i].endhead)
1650 bool LyXTabular::haveLTFirstHead() const
1652 if (endfirsthead.empty)
1654 for (row_type i = 0; i < rows_; ++i)
1655 if (row_info[i].endfirsthead)
1661 bool LyXTabular::haveLTFoot() const
1663 for (row_type i = 0; i < rows_; ++i)
1664 if (row_info[i].endfoot)
1670 bool LyXTabular::haveLTLastFoot() const
1672 if (endlastfoot.empty)
1674 for (row_type i = 0; i < rows_; ++i)
1675 if (row_info[i].endlastfoot)
1681 // end longtable support functions
1683 void LyXTabular::setAscentOfRow(row_type row, int height)
1685 if (row >= rows_ || row_info[row].ascent_of_row == height)
1687 row_info[row].ascent_of_row = height;
1691 void LyXTabular::setDescentOfRow(row_type row, int height)
1693 if (row >= rows_ || row_info[row].descent_of_row == height)
1695 row_info[row].descent_of_row = height;
1699 int LyXTabular::getAscentOfRow(row_type row) const
1703 return row_info[row].ascent_of_row;
1707 int LyXTabular::getDescentOfRow(row_type row) const
1709 BOOST_ASSERT(row < rows_);
1710 return row_info[row].descent_of_row;
1714 int LyXTabular::getHeightOfTabular() const
1717 for (row_type row = 0; row < rows_; ++row)
1718 height += getAscentOfRow(row) + getDescentOfRow(row) +
1719 getAdditionalHeight(row);
1724 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1726 BOOST_ASSERT(row < rows_);
1727 BOOST_ASSERT(column < columns_);
1728 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1732 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1734 // FIXME: assert or return 0 as in TeXBottomHLine()?
1735 BOOST_ASSERT(row != npos);
1736 BOOST_ASSERT(row < rows_);
1738 idx_type const fcell = getFirstCellInRow(row);
1739 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1742 for (idx_type i = fcell; i < n; ++i) {
1746 if (tmp == n - fcell) {
1749 for (idx_type i = fcell; i < n; ++i) {
1752 << column_of_cell(i) + 1
1754 << right_column_of_cell(i) + 1
1766 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1768 // FIXME: return 0 or assert as in TeXTopHLine()?
1769 if (row == npos || row >= rows_)
1772 idx_type const fcell = getFirstCellInRow(row);
1773 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1776 for (idx_type i = fcell; i < n; ++i) {
1780 if (tmp == n - fcell) {
1783 for (idx_type i = fcell; i < n; ++i) {
1784 if (bottomLine(i)) {
1786 << column_of_cell(i) + 1
1788 << right_column_of_cell(i) + 1
1800 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1804 if (getRotateCell(cell)) {
1805 os << "\\begin{sideways}\n";
1808 if (isMultiColumn(cell)) {
1809 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1810 if (!cellinfo_of_cell(cell).align_special.empty()) {
1811 os << cellinfo_of_cell(cell).align_special << "}{";
1813 if (leftLine(cell) &&
1814 (isFirstCellInRow(cell) ||
1815 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1816 !rightLine(cell - 1, true))))
1820 if (!getPWidth(cell).zero()) {
1821 switch (getVAlignment(cell)) {
1822 case LYX_VALIGN_TOP:
1825 case LYX_VALIGN_MIDDLE:
1828 case LYX_VALIGN_BOTTOM:
1833 << getPWidth(cell).asLatexString()
1836 switch (getAlignment(cell)) {
1837 case LYX_ALIGN_LEFT:
1840 case LYX_ALIGN_RIGHT:
1848 if (rightLine(cell))
1850 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1856 if (getUsebox(cell) == BOX_PARBOX) {
1858 switch (getVAlignment(cell)) {
1859 case LYX_VALIGN_TOP:
1862 case LYX_VALIGN_MIDDLE:
1865 case LYX_VALIGN_BOTTOM:
1869 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1870 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1871 os << "\\begin{minipage}[";
1872 switch (getVAlignment(cell)) {
1873 case LYX_VALIGN_TOP:
1876 case LYX_VALIGN_MIDDLE:
1879 case LYX_VALIGN_BOTTOM:
1883 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1890 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1895 if (getUsebox(cell) == BOX_PARBOX)
1897 else if (getUsebox(cell) == BOX_MINIPAGE) {
1898 os << "%\n\\end{minipage}";
1901 if (isMultiColumn(cell)) {
1904 if (getRotateCell(cell)) {
1905 os << "%\n\\end{sideways}";
1912 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1913 OutputParams const & runparams) const
1915 if (!is_long_tabular)
1919 // output header info
1921 if (endhead.topDL) {
1925 for (row_type i = 0; i < rows_; ++i) {
1926 if (row_info[i].endhead) {
1927 ret += TeXRow(os, i, buf, runparams);
1930 if (endhead.bottomDL) {
1934 os << "\\endhead\n";
1936 if (endfirsthead.empty) {
1937 os << "\\endfirsthead\n";
1941 // output firstheader info
1942 if (haveLTFirstHead()) {
1943 if (endfirsthead.topDL) {
1947 for (row_type i = 0; i < rows_; ++i) {
1948 if (row_info[i].endfirsthead) {
1949 ret += TeXRow(os, i, buf, runparams);
1952 if (endfirsthead.bottomDL) {
1956 os << "\\endfirsthead\n";
1959 // output footer info
1961 if (endfoot.topDL) {
1965 for (row_type i = 0; i < rows_; ++i) {
1966 if (row_info[i].endfoot) {
1967 ret += TeXRow(os, i, buf, runparams);
1970 if (endfoot.bottomDL) {
1974 os << "\\endfoot\n";
1976 if (endlastfoot.empty) {
1977 os << "\\endlastfoot\n";
1981 // output lastfooter info
1982 if (haveLTLastFoot()) {
1983 if (endlastfoot.topDL) {
1987 for (row_type i = 0; i < rows_; ++i) {
1988 if (row_info[i].endlastfoot) {
1989 ret += TeXRow(os, i, buf, runparams);
1992 if (endlastfoot.bottomDL) {
1996 os << "\\endlastfoot\n";
2003 bool LyXTabular::isValidRow(row_type row) const
2005 if (!is_long_tabular)
2007 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2008 !row_info[row].endfoot && !row_info[row].endlastfoot;
2012 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
2013 OutputParams const & runparams) const
2015 idx_type cell = getCellNumber(i, 0);
2017 int ret = TeXTopHLine(os, i);
2018 for (col_type j = 0; j < columns_; ++j) {
2019 if (isPartOfMultiColumn(i, j))
2021 ret += TeXCellPreamble(os, cell);
2022 shared_ptr<InsetText> inset = getCellInset(cell);
2024 Paragraph const & par = inset->paragraphs().front();
2025 bool rtl = par.isRightToLeftPar(buf.params())
2027 && getPWidth(cell).zero();
2031 ret += inset->latex(buf, os, runparams);
2035 ret += TeXCellPostamble(os, cell);
2036 if (!isLastCellInRow(cell)) { // not last cell in row
2042 os << "\\tabularnewline\n";
2044 ret += TeXBottomHLine(os, i);
2049 int LyXTabular::latex(Buffer const & buf, ostream & os,
2050 OutputParams const & runparams) const
2054 //+---------------------------------------------------------------------
2055 //+ first the opening preamble +
2056 //+---------------------------------------------------------------------
2059 os << "\\begin{sideways}\n";
2062 if (is_long_tabular)
2063 os << "\\begin{longtable}{";
2065 os << "\\begin{tabular}{";
2066 for (col_type i = 0; i < columns_; ++i) {
2067 if (!column_info[i].align_special.empty()) {
2068 os << column_info[i].align_special;
2070 if (column_info[i].left_line)
2072 if (!column_info[i].p_width.zero()) {
2073 switch (column_info[i].alignment) {
2074 case LYX_ALIGN_LEFT:
2075 os << ">{\\raggedright}";
2077 case LYX_ALIGN_RIGHT:
2078 os << ">{\\raggedleft}";
2080 case LYX_ALIGN_CENTER:
2081 os << ">{\\centering}";
2083 case LYX_ALIGN_NONE:
2084 case LYX_ALIGN_BLOCK:
2085 case LYX_ALIGN_LAYOUT:
2086 case LYX_ALIGN_SPECIAL:
2090 switch (column_info[i].valignment) {
2091 case LYX_VALIGN_TOP:
2094 case LYX_VALIGN_MIDDLE:
2097 case LYX_VALIGN_BOTTOM:
2102 << column_info[i].p_width.asLatexString()
2105 switch (column_info[i].alignment) {
2106 case LYX_ALIGN_LEFT:
2109 case LYX_ALIGN_RIGHT:
2117 if (column_info[i].right_line)
2124 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2126 //+---------------------------------------------------------------------
2127 //+ the single row and columns (cells) +
2128 //+---------------------------------------------------------------------
2130 for (row_type i = 0; i < rows_; ++i) {
2131 if (isValidRow(i)) {
2132 ret += TeXRow(os, i, buf, runparams);
2133 if (is_long_tabular && row_info[i].newpage) {
2134 os << "\\newpage\n";
2140 //+---------------------------------------------------------------------
2141 //+ the closing of the tabular +
2142 //+---------------------------------------------------------------------
2144 if (is_long_tabular)
2145 os << "\\end{longtable}";
2147 os << "\\end{tabular}";
2149 os << "\n\\end{sideways}";
2157 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2158 const OutputParams & runparams) const
2160 os << "<tabular ca=\"";
2161 for (col_type i = 0; i < columns_; ++i) {
2162 switch (column_info[i].alignment) {
2163 case LYX_ALIGN_LEFT:
2166 case LYX_ALIGN_RIGHT:
2177 for (row_type i = 0; i < rows_; ++i) {
2178 for (col_type j = 0; j < columns_; ++j) {
2179 if (isPartOfMultiColumn(i, j))
2181 shared_ptr<InsetText> inset = getCellInset(cell);
2183 ret += inset->linuxdoc(buf, os, runparams);
2185 if (isLastCellInRow(cell)) {
2194 os << "</tabular>\n";
2199 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2200 OutputParams const & runparams) const
2203 idx_type cell = getFirstCellInRow(row);
2206 for (col_type j = 0; j < columns_; ++j) {
2207 if (isPartOfMultiColumn(row, j))
2210 os << "<entry align=\"";
2211 switch (getAlignment(cell)) {
2212 case LYX_ALIGN_LEFT:
2215 case LYX_ALIGN_RIGHT:
2223 os << "\" valign=\"";
2224 switch (getVAlignment(cell)) {
2225 case LYX_VALIGN_TOP:
2228 case LYX_VALIGN_BOTTOM:
2231 case LYX_VALIGN_MIDDLE:
2236 if (isMultiColumn(cell)) {
2237 os << " namest=\"col" << j << "\" ";
2238 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2242 ret += getCellInset(cell)->docbook(buf, os, runparams);
2251 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2252 OutputParams const & runparams) const
2256 //+---------------------------------------------------------------------
2257 //+ first the opening preamble +
2258 //+---------------------------------------------------------------------
2260 os << "<tgroup cols=\"" << columns_
2261 << "\" colsep=\"1\" rowsep=\"1\">\n";
2263 for (col_type i = 0; i < columns_; ++i) {
2264 os << "<colspec colname=\"col" << i << "\" align=\"";
2265 switch (column_info[i].alignment) {
2266 case LYX_ALIGN_LEFT:
2269 case LYX_ALIGN_RIGHT:
2277 if (runparams.flavor == OutputParams::XML)
2283 //+---------------------------------------------------------------------
2284 //+ Long Tabular case +
2285 //+---------------------------------------------------------------------
2287 // output header info
2288 if (haveLTHead() || haveLTFirstHead()) {
2291 for (row_type i = 0; i < rows_; ++i) {
2292 if (row_info[i].endhead || row_info[i].endfirsthead) {
2293 ret += docbookRow(buf, os, i, runparams);
2299 // output footer info
2300 if (haveLTFoot() || haveLTLastFoot()) {
2303 for (row_type i = 0; i < rows_; ++i) {
2304 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2305 ret += docbookRow(buf, os, i, runparams);
2312 //+---------------------------------------------------------------------
2313 //+ the single row and columns (cells) +
2314 //+---------------------------------------------------------------------
2318 for (row_type i = 0; i < rows_; ++i) {
2319 if (isValidRow(i)) {
2320 ret += docbookRow(buf, os, i, runparams);
2325 //+---------------------------------------------------------------------
2326 //+ the closing of the tabular +
2327 //+---------------------------------------------------------------------
2336 int LyXTabular::asciiTopHLine(ostream & os, row_type row,
2337 vector<unsigned int> const & clen) const
2339 idx_type const fcell = getFirstCellInRow(row);
2340 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2343 for (idx_type i = fcell; i < n; ++i) {
2353 for (idx_type i = fcell; i < n; ++i) {
2364 col_type column = column_of_cell(i);
2365 int len = clen[column];
2366 while (column < columns_ - 1
2367 && isPartOfMultiColumn(row, ++column))
2368 len += clen[column] + 4;
2369 os << string(len, ch);
2384 int LyXTabular::asciiBottomHLine(ostream & os, row_type row,
2385 vector<unsigned int> const & clen) const
2387 idx_type const fcell = getFirstCellInRow(row);
2388 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2391 for (idx_type i = fcell; i < n; ++i) {
2392 if (bottomLine(i)) {
2401 for (idx_type i = fcell; i < n; ++i) {
2402 if (bottomLine(i)) {
2412 col_type column = column_of_cell(i);
2413 int len = clen[column];
2414 while (column < columns_ -1
2415 && isPartOfMultiColumn(row, ++column))
2416 len += clen[column] + 4;
2417 os << string(len, ch);
2418 if (bottomLine(i)) {
2432 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2433 OutputParams const & runparams,
2434 idx_type cell, row_type row, col_type column,
2435 vector<unsigned int> const & clen,
2436 bool onlydata) const
2439 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2451 unsigned int len1 = sstr.str().length();
2452 unsigned int len2 = clen[column];
2453 while (column < columns_ -1
2454 && isPartOfMultiColumn(row, ++column))
2455 len2 += clen[column] + 4;
2458 switch (getAlignment(cell)) {
2460 case LYX_ALIGN_LEFT:
2463 case LYX_ALIGN_RIGHT:
2467 case LYX_ALIGN_CENTER:
2473 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2475 if (rightLine(cell))
2484 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2485 OutputParams const & runparams,
2487 bool onlydata, unsigned char delim) const
2491 // first calculate the width of the single columns
2492 vector<unsigned int> clen(columns_);
2495 // first all non (real) multicolumn cells!
2496 for (col_type j = 0; j < columns_; ++j) {
2498 for (row_type i = 0; i < rows_; ++i) {
2499 idx_type cell = getCellNumber(i, j);
2500 if (isMultiColumnReal(cell))
2503 getCellInset(cell)->plaintext(buf, sstr, runparams);
2504 if (clen[j] < sstr.str().length())
2505 clen[j] = sstr.str().length();
2508 // then all (real) multicolumn cells!
2509 for (col_type j = 0; j < columns_; ++j) {
2510 for (row_type i = 0; i < rows_; ++i) {
2511 idx_type cell = getCellNumber(i, j);
2512 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2515 getCellInset(cell)->plaintext(buf, sstr, runparams);
2516 int len = int(sstr.str().length());
2517 idx_type const n = cells_in_multicolumn(cell);
2518 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2520 if (len > int(clen[j + n - 1]))
2521 clen[j + n - 1] = len;
2526 for (row_type i = 0; i < rows_; ++i) {
2527 if (!onlydata && asciiTopHLine(os, i, clen))
2528 os << string(depth * 2, ' ');
2529 for (col_type j = 0; j < columns_; ++j) {
2530 if (isPartOfMultiColumn(i, j))
2532 if (onlydata && j > 0)
2534 ret += asciiPrintCell(buf, os, runparams,
2535 cell, i, j, clen, onlydata);
2540 os << string(depth * 2, ' ');
2541 if (asciiBottomHLine(os, i, clen))
2542 os << string(depth * 2, ' ');
2549 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2551 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2555 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2556 col_type column) const
2558 return cell_info[row][column].inset;
2562 void LyXTabular::setCellInset(row_type row, col_type column,
2563 shared_ptr<InsetText> ins) const
2565 cell_info[row][column].inset = ins;
2569 LyXTabular::idx_type
2570 LyXTabular::getCellFromInset(InsetBase const * inset) const
2572 // is this inset part of the tabular?
2574 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2575 BOOST_ASSERT(false);
2578 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2579 if (getCellInset(cell).get() == inset) {
2580 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2581 << "cell=" << cell << endl;
2585 // We should have found a cell at this point
2586 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2587 << inset << " not found!" << endl;
2588 BOOST_ASSERT(false);
2594 void LyXTabular::validate(LaTeXFeatures & features) const
2596 features.require("NeedTabularnewline");
2597 if (isLongTabular())
2598 features.require("longtable");
2600 features.require("rotating");
2601 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2602 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2603 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2604 features.require("array");
2605 getCellInset(cell)->validate(features);
2610 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2612 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2613 ParagraphList::const_iterator cit = parlist.begin();
2614 ParagraphList::const_iterator end = parlist.end();
2616 for (; cit != end; ++cit)
2617 for (int i = 0; i < cit->size(); ++i)
2618 if (cit->isNewline(i))