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,
317 BufferView const * bv)
320 multicolumn(LyXTabular::CELL_NORMAL),
321 alignment(LYX_ALIGN_CENTER),
322 valignment(LYX_VALIGN_TOP),
329 inset(new InsetText(bp))
331 inset->setViewCache(bv);
335 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
337 width_of_cell(cs.width_of_cell),
338 multicolumn(cs.multicolumn),
339 alignment(cs.alignment),
340 valignment(cs.valignment),
341 top_line(cs.top_line),
342 bottom_line(cs.bottom_line),
343 left_line(cs.left_line),
344 right_line(cs.right_line),
347 align_special(cs.align_special),
349 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
353 LyXTabular::cellstruct &
354 LyXTabular::cellstruct::operator=(cellstruct cs)
362 LyXTabular::cellstruct::swap(cellstruct & rhs)
364 std::swap(cellno, rhs.cellno);
365 std::swap(width_of_cell, rhs.width_of_cell);
366 std::swap(multicolumn, rhs.multicolumn);
367 std::swap(alignment, rhs.alignment);
368 std::swap(valignment, rhs.valignment);
369 std::swap(top_line, rhs.top_line);
370 std::swap(bottom_line, rhs.bottom_line);
371 std::swap(left_line, rhs.left_line);
372 std::swap(right_line, rhs.right_line);
373 std::swap(usebox, rhs.usebox);
374 std::swap(rotate, rhs.rotate);
375 std::swap(align_special, rhs.align_special);
376 p_width.swap(rhs.p_width);
377 inset.swap(rhs.inset);
381 LyXTabular::rowstruct::rowstruct()
394 LyXTabular::columnstruct::columnstruct()
395 : alignment(LYX_ALIGN_CENTER),
396 valignment(LYX_VALIGN_TOP),
404 LyXTabular::ltType::ltType()
411 LyXTabular::LyXTabular(BufferParams const & bp, row_type rows_arg,
412 col_type columns_arg, BufferView const * bv)
414 init(bp, rows_arg, columns_arg, bv);
418 // activates all lines and sets all widths to 0
419 void LyXTabular::init(BufferParams const & bp, row_type rows_arg,
420 col_type columns_arg, BufferView const * bv)
423 columns_ = columns_arg;
424 row_info = row_vector(rows_);
425 column_info = column_vector(columns_);
426 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp, bv)));
427 row_info.reserve(10);
428 column_info.reserve(10);
429 cell_info.reserve(100);
431 for (row_type i = 0; i < rows_; ++i)
432 cell_info[i].back().right_line = true;
433 row_info.back().bottom_line = true;
434 row_info.front().bottom_line = true;
435 column_info.back().right_line = true;
436 is_long_tabular = false;
441 void LyXTabular::fixCellNums()
444 for (row_type i = 0; i < rows_; ++i) {
445 for (col_type j = 0; j < columns_; ++j) {
446 // When debugging it can be nice to set
448 cell_info[i][j].inset->setDrawFrame(false);
449 cell_info[i][j].cellno = cellno++;
451 cell_info[i].back().right_line = true;
454 set_row_column_number_info();
458 void LyXTabular::appendRow(BufferParams const & bp, idx_type const cell)
460 BufferView const * const bv = getCellInset(0)->getText(0)->bv();
463 row_type const row = row_of_cell(cell);
465 row_vector::iterator rit = row_info.begin() + row;
466 row_info.insert(rit, rowstruct());
467 // now set the values of the row before
468 row_info[row] = row_info[row + 1];
470 cell_vvector old(rows_ - 1);
471 for (row_type i = 0; i < rows_ - 1; ++i)
472 swap(cell_info[i], old[i]);
474 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp, bv)));
476 for (row_type i = 0; i <= row; ++i)
477 swap(cell_info[i], old[i]);
478 for (row_type i = row + 2; i < rows_; ++i)
479 swap(cell_info[i], old[i - 1]);
481 if (bp.tracking_changes)
482 for (col_type j = 0; j < columns_; ++j)
483 cell_info[row + 1][j].inset->markNew(true);
485 set_row_column_number_info();
489 void LyXTabular::deleteRow(row_type const row)
491 // Not allowed to delete last row
495 row_info.erase(row_info.begin() + row);
496 cell_info.erase(cell_info.begin() + row);
502 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
504 BufferView const * const bv = getCellInset(0)->getText(0)->bv();
507 col_type const column = column_of_cell(cell);
508 column_vector::iterator cit = column_info.begin() + column + 1;
509 column_info.insert(cit, columnstruct());
510 // set the column values of the column before
511 column_info[column + 1] = column_info[column];
513 for (row_type i = 0; i < rows_; ++i) {
514 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp, bv));
516 // care about multicolumns
517 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
518 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
520 if (column + 2 >= columns_
521 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
522 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
525 for (row_type i = 0; i < rows_; ++i) {
526 cell_info[i][column + 1].inset->clear();
527 if (bp.tracking_changes)
528 cell_info[i][column + 1].inset->markNew(true);
534 void LyXTabular::deleteColumn(col_type const column)
536 // Not allowed to delete last column
540 column_info.erase(column_info.begin() + column);
541 for (row_type i = 0; i < rows_; ++i)
542 cell_info[i].erase(cell_info[i].begin() + column);
548 void LyXTabular::set_row_column_number_info()
551 for (row_type row = 0; row < rows_; ++row) {
552 for (col_type column = 0; column < columns_; ++column) {
553 if (cell_info[row][column].multicolumn
554 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
556 if (numberofcells == 0)
557 // FIXME: Is this intended?
558 cell_info[row][column].cellno = npos;
560 cell_info[row][column].cellno =
565 rowofcell.resize(numberofcells);
566 columnofcell.resize(numberofcells);
571 c < numberofcells && row < rows_ && column < columns_;) {
573 columnofcell[c] = column;
577 } while (column < columns_ &&
578 cell_info[row][column].multicolumn
579 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
581 if (column == columns_) {
587 for (row_type row = 0; row < rows_; ++row) {
588 for (col_type column = 0; column < columns_; ++column) {
589 if (isPartOfMultiColumn(row,column))
591 cell_info[row][column].inset->setAutoBreakRows(
592 !getPWidth(getCellNumber(row, column)).zero());
598 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
600 return numberofcells;
604 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
606 row_type const row = row_of_cell(cell);
608 for (col_type i = 0; i < columns_; ++i)
609 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
615 // returns 1 if there is a topline, returns 0 if not
616 bool LyXTabular::topLine(idx_type const cell, bool const onlycolumn) const
618 if (!onlycolumn && isMultiColumn(cell))
619 return cellinfo_of_cell(cell).top_line;
620 return row_info[row_of_cell(cell)].top_line;
624 bool LyXTabular::bottomLine(idx_type const cell, bool onlycolumn) const
626 if (!onlycolumn && isMultiColumn(cell))
627 return cellinfo_of_cell(cell).bottom_line;
628 return row_info[row_of_cell(cell)].bottom_line;
632 bool LyXTabular::leftLine(idx_type cell, bool onlycolumn) const
634 if (!onlycolumn && isMultiColumn(cell) &&
635 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
637 if (cellinfo_of_cell(cell).align_special.empty())
638 return cellinfo_of_cell(cell).left_line;
639 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
641 if (column_info[column_of_cell(cell)].align_special.empty())
642 return column_info[column_of_cell(cell)].left_line;
643 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
647 bool LyXTabular::rightLine(idx_type cell, bool onlycolumn) const
649 if (!onlycolumn && isMultiColumn(cell) &&
650 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
652 if (cellinfo_of_cell(cell).align_special.empty())
653 return cellinfo_of_cell(cell).right_line;
654 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
656 if (column_info[column_of_cell(cell)].align_special.empty())
657 return column_info[right_column_of_cell(cell)].right_line;
658 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
662 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
664 row_type row = row_of_cell(cell);
665 if (row > 0 && !getAdditionalHeight(row)) {
666 col_type column = column_of_cell(cell);
669 && cell_info[row][column].multicolumn
670 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
672 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
673 return row_info[row].bottom_line;
675 return cell_info[row][column].bottom_line;
681 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
683 col_type column = column_of_cell(cell);
685 row_type row = row_of_cell(cell);
687 (cell_info[row][column].multicolumn ==
688 LyXTabular::CELL_PART_OF_MULTICOLUMN));
689 if (getAdditionalWidth(cell_info[row][column].cellno))
691 return rightLine(cell_info[row][column].cellno);
697 bool LyXTabular::isLastRow(idx_type cell) const
699 return row_of_cell(cell) == rows_ - 1;
703 int LyXTabular::getAdditionalHeight(row_type row) const
705 if (!row || row >= rows_)
711 for (col_type column = 0; column < columns_ && bottom; ++column) {
712 switch (cell_info[row - 1][column].multicolumn) {
713 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
714 bottom = cell_info[row - 1][column].bottom_line;
716 case LyXTabular::CELL_NORMAL:
717 bottom = row_info[row - 1].bottom_line;
720 for (col_type column = 0; column < columns_ && top; ++column) {
721 switch (cell_info[row][column].multicolumn) {
722 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
723 top = cell_info[row][column].top_line;
725 case LyXTabular::CELL_NORMAL:
726 top = row_info[row].top_line;
730 return WIDTH_OF_LINE;
735 int LyXTabular::getAdditionalWidth(idx_type cell) const
737 // internally already set in setWidthOfCell
738 // used to get it back in text.C
739 col_type const col = right_column_of_cell(cell);
740 row_type const row = row_of_cell(cell);
741 if (col < columns_ - 1 && rightLine(cell) &&
742 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
744 return WIDTH_OF_LINE;
750 // returns the maximum over all rows
751 int LyXTabular::getWidthOfColumn(idx_type cell) const
753 col_type const column1 = column_of_cell(cell);
754 col_type const column2 = right_column_of_cell(cell);
756 for (col_type i = column1; i <= column2; ++i)
757 result += column_info[i].width_of_column;
762 int LyXTabular::getWidthOfTabular() const
764 return width_of_tabular;
768 // returns true if a complete update is necessary, otherwise false
769 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
771 if (!isMultiColumn(cell))
774 row_type const row = row_of_cell(cell);
775 col_type const column1 = column_of_cell(cell);
776 col_type const column2 = right_column_of_cell(cell);
777 int const old_val = cell_info[row][column2].width_of_cell;
779 // first set columns to 0 so we can calculate the right width
780 for (col_type i = column1; i <= column2; ++i) {
781 cell_info[row][i].width_of_cell = 0;
783 // set the width to MAX_WIDTH until width > 0
784 int width = new_width + 2 * WIDTH_OF_LINE;
785 col_type i = column1;
786 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
787 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
788 width -= column_info[i].width_of_column;
791 cell_info[row][i].width_of_cell = width;
793 if (old_val != cell_info[row][column2].width_of_cell) {
794 // in this case we have to recalculate all multicolumn cells which
795 // have this column as one of theirs but not as last one
796 calculate_width_of_column_NMC(i);
797 recalculateMulticolumnsOfColumn(i);
798 calculate_width_of_column(i);
804 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
806 // the last column does not have to be recalculated because all
807 // multicolumns will have here there last multicolumn cell which
808 // always will have the whole rest of the width of the cell.
809 if (columns_ < 2 || column > (columns_ - 2))
811 for (row_type row = 0; row < rows_; ++row) {
812 int mc = cell_info[row][column].multicolumn;
813 int nmc = cell_info[row][column+1].multicolumn;
814 // we only have to update multicolumns which do not have this
815 // column as their last column!
816 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
817 (mc == CELL_PART_OF_MULTICOLUMN &&
818 nmc == CELL_PART_OF_MULTICOLUMN))
820 idx_type const cellno = cell_info[row][column].cellno;
821 setWidthOfMulticolCell(cellno,
822 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
828 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
830 row_type const row = row_of_cell(cell);
831 col_type const column1 = column_of_cell(cell);
836 if (rightLine(cell_info[row][column1].cellno, true) &&
837 column1 < columns_ - 1 &&
838 leftLine(cell_info[row][column1+1].cellno, true))
840 add_width = WIDTH_OF_LINE;
843 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
846 if (isMultiColumnReal(cell)) {
847 tmp = setWidthOfMulticolCell(cell, new_width);
849 width = new_width + 2 * WIDTH_OF_LINE + add_width;
850 cell_info[row][column1].width_of_cell = width;
851 tmp = calculate_width_of_column_NMC(column1);
853 recalculateMulticolumnsOfColumn(column1);
856 for (col_type i = 0; i < columns_; ++i)
857 calculate_width_of_column(i);
858 calculate_width_of_tabular();
863 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
866 if (!isMultiColumn(cell) || onlycolumn)
867 column_info[column_of_cell(cell)].alignment = align;
869 cellinfo_of_cell(cell).alignment = align;
873 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
876 if (!isMultiColumn(cell) || onlycolumn)
877 column_info[column_of_cell(cell)].valignment = align;
879 cellinfo_of_cell(cell).valignment = align;
886 * Allow line and paragraph breaks for fixed width cells or disallow them,
887 * merge cell paragraphs and reset layout to standard for variable width
890 void toggleFixedWidth(LCursor & cur, InsetText * inset, bool fixedWidth)
892 inset->setAutoBreakRows(fixedWidth);
896 // merge all paragraphs to one
897 BufferParams const & bp =
898 inset->getText(0)->bv_owner->buffer()->params();
899 while (inset->paragraphs().size() > 1)
900 mergeParagraph(bp, inset->paragraphs(), 0);
904 // undo information has already been recorded
905 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
906 bp.getLyXTextClass().defaultLayoutName());
913 void LyXTabular::setColumnPWidth(LCursor & cur, idx_type cell,
914 LyXLength const & width)
916 col_type const j = column_of_cell(cell);
918 column_info[j].p_width = width;
919 for (row_type i = 0; i < rows_; ++i) {
920 idx_type const cell = getCellNumber(i, j);
921 // because of multicolumns
922 toggleFixedWidth(cur, getCellInset(cell).get(),
923 !getPWidth(cell).zero());
925 // cur paragraph can become invalid after paragraphs were merged
926 if (cur.pit() > cur.lastpit())
927 cur.pit() = cur.lastpit();
928 // cur position can become invalid after newlines were removed
929 if (cur.pos() > cur.lastpos())
930 cur.pos() = cur.lastpos();
934 bool LyXTabular::setMColumnPWidth(LCursor & cur, idx_type cell,
935 LyXLength const & width)
937 if (!isMultiColumn(cell))
940 cellinfo_of_cell(cell).p_width = width;
941 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
942 // cur paragraph can become invalid after paragraphs were merged
943 if (cur.pit() > cur.lastpit())
944 cur.pit() = cur.lastpit();
945 // cur position can become invalid after newlines were removed
946 if (cur.pos() > cur.lastpos())
947 cur.pos() = cur.lastpos();
952 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
953 LyXTabular::Feature what)
955 if (what == SET_SPECIAL_MULTI)
956 cellinfo_of_cell(cell).align_special = special;
958 column_info[column_of_cell(cell)].align_special = special;
962 void LyXTabular::setAllLines(idx_type cell, bool line)
964 setTopLine(cell, line);
965 setBottomLine(cell, line);
966 setRightLine(cell, line);
967 setLeftLine(cell, line);
971 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
973 row_type const row = row_of_cell(cell);
974 if (onlycolumn || !isMultiColumn(cell))
975 row_info[row].top_line = line;
977 cellinfo_of_cell(cell).top_line = line;
981 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
983 if (onlycolumn || !isMultiColumn(cell))
984 row_info[row_of_cell(cell)].bottom_line = line;
986 cellinfo_of_cell(cell).bottom_line = line;
990 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
992 if (onlycolumn || !isMultiColumn(cell))
993 column_info[column_of_cell(cell)].left_line = line;
995 cellinfo_of_cell(cell).left_line = line;
999 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
1001 if (onlycolumn || !isMultiColumn(cell))
1002 column_info[right_column_of_cell(cell)].right_line = line;
1004 cellinfo_of_cell(cell).right_line = line;
1008 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
1010 if (!onlycolumn && isMultiColumn(cell))
1011 return cellinfo_of_cell(cell).alignment;
1012 return column_info[column_of_cell(cell)].alignment;
1016 LyXTabular::VAlignment
1017 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
1019 if (!onlycolumn && isMultiColumn(cell))
1020 return cellinfo_of_cell(cell).valignment;
1021 return column_info[column_of_cell(cell)].valignment;
1025 LyXLength const LyXTabular::getPWidth(idx_type cell) const
1027 if (isMultiColumn(cell))
1028 return cellinfo_of_cell(cell).p_width;
1029 return column_info[column_of_cell(cell)].p_width;
1033 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
1035 return column_info[column_of_cell(cell)].p_width;
1039 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
1041 if (isMultiColumn(cell))
1042 return cellinfo_of_cell(cell).p_width;
1047 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
1049 if (what == SET_SPECIAL_MULTI)
1050 return cellinfo_of_cell(cell).align_special;
1051 return column_info[column_of_cell(cell)].align_special;
1055 int LyXTabular::getWidthOfCell(idx_type cell) const
1057 row_type const row = row_of_cell(cell);
1058 col_type const column1 = column_of_cell(cell);
1059 col_type const column2 = right_column_of_cell(cell);
1061 for (col_type i = column1; i <= column2; ++i)
1062 result += cell_info[row][i].width_of_cell;
1067 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1071 switch (getAlignment(cell)) {
1072 case LYX_ALIGN_CENTER:
1073 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1075 case LYX_ALIGN_RIGHT:
1076 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1077 // + getAdditionalWidth(cell);
1080 // LYX_ALIGN_LEFT: nothing :-)
1084 // the LaTeX Way :-(
1090 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1092 return column_of_cell(cell) == 0;
1096 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1098 if (row > rows_ - 1)
1100 return cell_info[row][0].cellno;
1104 bool LyXTabular::isLastCellInRow(idx_type cell) const
1106 return right_column_of_cell(cell) == columns_ - 1;
1110 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1112 if (row > rows_ - 1)
1114 return cell_info[row][columns_-1].cellno;
1118 void LyXTabular::calculate_width_of_column(col_type column)
1121 for (row_type i = 0; i < rows_; ++i)
1122 maximum = max(cell_info[i][column].width_of_cell, maximum);
1123 column_info[column].width_of_column = maximum;
1128 // Calculate the columns regarding ONLY the normal cells and if this
1129 // column is inside a multicolumn cell then use it only if its the last
1130 // column of this multicolumn cell as this gives an added width to the
1131 // column, all the rest should be adapted!
1133 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1135 int const old_column_width = column_info[column].width_of_column;
1137 for (row_type i = 0; i < rows_; ++i) {
1138 idx_type cell = getCellNumber(i, column);
1139 bool ismulti = isMultiColumnReal(cell);
1140 if ((!ismulti || column == right_column_of_cell(cell)) &&
1141 cell_info[i][column].width_of_cell > max)
1143 max = cell_info[i][column].width_of_cell;
1146 column_info[column].width_of_column = max;
1147 return column_info[column].width_of_column != old_column_width;
1151 void LyXTabular::calculate_width_of_tabular()
1153 width_of_tabular = 0;
1154 for (col_type i = 0; i < columns_; ++i)
1155 width_of_tabular += column_info[i].width_of_column;
1159 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1161 if (cell >= numberofcells)
1165 return rowofcell[cell];
1169 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1171 if (cell >= numberofcells)
1172 return columns_ - 1;
1175 return columnofcell[cell];
1179 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1181 row_type const row = row_of_cell(cell);
1182 col_type column = column_of_cell(cell);
1183 while (column < columns_ - 1 &&
1184 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1190 void LyXTabular::write(Buffer const & buf, ostream & os) const
1194 << write_attribute("version", 3)
1195 << write_attribute("rows", rows_)
1196 << write_attribute("columns", columns_)
1198 // global longtable options
1200 << write_attribute("rotate", rotate)
1201 << write_attribute("islongtable", is_long_tabular)
1202 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1203 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1204 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1205 << write_attribute("headTopDL", endhead.topDL)
1206 << write_attribute("headBottomDL", endhead.bottomDL)
1207 << write_attribute("footTopDL", endfoot.topDL)
1208 << write_attribute("footBottomDL", endfoot.bottomDL)
1209 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1210 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1211 << write_attribute("lastFootEmpty", endlastfoot.empty)
1213 for (col_type j = 0; j < columns_; ++j) {
1215 << write_attribute("alignment", column_info[j].alignment)
1216 << write_attribute("valignment", column_info[j].valignment)
1217 << write_attribute("leftline", column_info[j].left_line)
1218 << write_attribute("rightline", column_info[j].right_line)
1219 << write_attribute("width", column_info[j].p_width.asString())
1220 << write_attribute("special", column_info[j].align_special)
1223 for (row_type i = 0; i < rows_; ++i) {
1225 << write_attribute("topline", row_info[i].top_line)
1226 << write_attribute("bottomline", row_info[i].bottom_line)
1227 << write_attribute("endhead", row_info[i].endhead)
1228 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1229 << write_attribute("endfoot", row_info[i].endfoot)
1230 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1231 << write_attribute("newpage", row_info[i].newpage)
1233 for (col_type j = 0; j < columns_; ++j) {
1235 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1236 << write_attribute("alignment", cell_info[i][j].alignment)
1237 << write_attribute("valignment", cell_info[i][j].valignment)
1238 << write_attribute("topline", cell_info[i][j].top_line)
1239 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1240 << write_attribute("leftline", cell_info[i][j].left_line)
1241 << write_attribute("rightline", cell_info[i][j].right_line)
1242 << write_attribute("rotate", cell_info[i][j].rotate)
1243 << write_attribute("usebox", cell_info[i][j].usebox)
1244 << write_attribute("width", cell_info[i][j].p_width)
1245 << write_attribute("special", cell_info[i][j].align_special)
1247 os << "\\begin_inset ";
1248 cell_info[i][j].inset->write(buf, os);
1249 os << "\n\\end_inset\n"
1254 os << "</lyxtabular>\n";
1258 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1261 istream & is = lex.getStream();
1263 l_getline(is, line);
1264 if (!prefixIs(line, "<lyxtabular ")
1265 && !prefixIs(line, "<LyXTabular ")) {
1266 BOOST_ASSERT(false);
1271 if (!getTokenValue(line, "version", version))
1273 BOOST_ASSERT(version >= 2);
1276 if (!getTokenValue(line, "rows", rows_arg))
1279 if (!getTokenValue(line, "columns", columns_arg))
1281 init(buf.params(), rows_arg, columns_arg, buf.text().bv());
1282 l_getline(is, line);
1283 if (!prefixIs(line, "<features")) {
1284 lyxerr << "Wrong tabular format (expected <features ...> got"
1285 << line << ')' << endl;
1288 getTokenValue(line, "rotate", rotate);
1289 getTokenValue(line, "islongtable", is_long_tabular);
1290 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1291 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1292 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1293 getTokenValue(line, "headTopDL", endhead.topDL);
1294 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1295 getTokenValue(line, "footTopDL", endfoot.topDL);
1296 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1297 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1298 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1299 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1301 for (col_type j = 0; j < columns_; ++j) {
1303 if (!prefixIs(line,"<column")) {
1304 lyxerr << "Wrong tabular format (expected <column ...> got"
1305 << line << ')' << endl;
1308 getTokenValue(line, "alignment", column_info[j].alignment);
1309 getTokenValue(line, "valignment", column_info[j].valignment);
1310 getTokenValue(line, "leftline", column_info[j].left_line);
1311 getTokenValue(line, "rightline", column_info[j].right_line);
1312 getTokenValue(line, "width", column_info[j].p_width);
1313 getTokenValue(line, "special", column_info[j].align_special);
1316 for (row_type i = 0; i < rows_; ++i) {
1317 l_getline(is, line);
1318 if (!prefixIs(line, "<row")) {
1319 lyxerr << "Wrong tabular format (expected <row ...> got"
1320 << line << ')' << endl;
1323 getTokenValue(line, "topline", row_info[i].top_line);
1324 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1325 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1326 getTokenValue(line, "endhead", row_info[i].endhead);
1327 getTokenValue(line, "endfoot", row_info[i].endfoot);
1328 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1329 getTokenValue(line, "newpage", row_info[i].newpage);
1330 for (col_type j = 0; j < columns_; ++j) {
1331 l_getline(is, line);
1332 if (!prefixIs(line, "<cell")) {
1333 lyxerr << "Wrong tabular format (expected <cell ...> got"
1334 << line << ')' << endl;
1337 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1338 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1339 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1340 getTokenValue(line, "topline", cell_info[i][j].top_line);
1341 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1342 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1343 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1344 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1345 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1346 getTokenValue(line, "width", cell_info[i][j].p_width);
1347 getTokenValue(line, "special", cell_info[i][j].align_special);
1348 l_getline(is, line);
1349 if (prefixIs(line, "\\begin_inset")) {
1350 cell_info[i][j].inset->read(buf, lex);
1351 l_getline(is, line);
1353 if (!prefixIs(line, "</cell>")) {
1354 lyxerr << "Wrong tabular format (expected </cell> got"
1355 << line << ')' << endl;
1359 l_getline(is, line);
1360 if (!prefixIs(line, "</row>")) {
1361 lyxerr << "Wrong tabular format (expected </row> got"
1362 << line << ')' << endl;
1366 while (!prefixIs(line, "</lyxtabular>")) {
1367 l_getline(is, line);
1369 set_row_column_number_info();
1373 bool LyXTabular::isMultiColumn(idx_type cell) const
1375 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1379 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1381 return column_of_cell(cell) != right_column_of_cell(cell) &&
1382 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1386 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1388 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1392 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1395 cellstruct & cs = cellinfo_of_cell(cell);
1396 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1397 cs.alignment = column_info[column_of_cell(cell)].alignment;
1398 cs.top_line = row_info[row_of_cell(cell)].top_line;
1399 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1400 cs.left_line = column_info[column_of_cell(cell)].left_line;
1401 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1402 for (idx_type i = 1; i < number; ++i) {
1403 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1404 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1405 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1408 set_row_column_number_info();
1412 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1414 row_type const row = row_of_cell(cell);
1415 col_type column = column_of_cell(cell);
1416 idx_type result = 1;
1418 while (column < columns_ &&
1419 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1428 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1430 row_type const row = row_of_cell(cell);
1431 col_type column = column_of_cell(cell);
1433 idx_type result = 0;
1435 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1436 cell_info[row][column].multicolumn = CELL_NORMAL;
1438 while (column < columns_ &&
1439 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1441 cell_info[row][column].multicolumn = CELL_NORMAL;
1446 set_row_column_number_info();
1451 void LyXTabular::setLongTabular(bool what)
1453 is_long_tabular = what;
1457 bool LyXTabular::isLongTabular() const
1459 return is_long_tabular;
1463 void LyXTabular::setRotateTabular(bool flag)
1469 bool LyXTabular::getRotateTabular() const
1475 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1477 cellinfo_of_cell(cell).rotate = flag;
1481 bool LyXTabular::getRotateCell(idx_type cell) const
1483 return cellinfo_of_cell(cell).rotate;
1487 bool LyXTabular::needRotating() const
1491 for (row_type i = 0; i < rows_; ++i)
1492 for (col_type j = 0; j < columns_; ++j)
1493 if (cell_info[i][j].rotate)
1499 bool LyXTabular::isLastCell(idx_type cell) const
1501 if (cell + 1 < numberofcells)
1507 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1509 if (row_of_cell(cell) > 0)
1510 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1515 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1517 if (row_of_cell(cell) + 1 < rows_)
1518 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1523 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1525 if (row_of_cell(cell) == 0)
1527 if (!isMultiColumn(cell))
1528 return getCellAbove(cell);
1529 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1533 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1535 if (row_of_cell(cell) + 1 >= rows_)
1537 if (!isMultiColumn(cell))
1538 return getCellBelow(cell);
1539 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1543 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1544 col_type column) const
1546 BOOST_ASSERT(column != npos && column < columns_ &&
1547 row != npos && row < rows_);
1548 return cell_info[row][column].cellno;
1552 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1554 cellinfo_of_cell(cell).usebox = type;
1558 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1560 if (column_info[column_of_cell(cell)].p_width.zero() &&
1561 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1563 if (cellinfo_of_cell(cell).usebox > 1)
1564 return cellinfo_of_cell(cell).usebox;
1565 return useParbox(cell);
1570 // This are functions used for the longtable support
1572 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1578 row_info[row].endfirsthead = flag;
1582 row_info[row].endhead = flag;
1587 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1590 hd.set = haveLTHead();
1591 return row_info[row].endhead;
1595 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1598 hd.set = haveLTFirstHead();
1599 return row_info[row].endfirsthead;
1603 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1609 row_info[row].endlastfoot = flag;
1613 row_info[row].endfoot = flag;
1618 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1621 fd.set = haveLTFoot();
1622 return row_info[row].endfoot;
1626 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1629 fd.set = haveLTLastFoot();
1630 return row_info[row].endlastfoot;
1634 void LyXTabular::setLTNewPage(row_type row, bool what)
1636 row_info[row].newpage = what;
1640 bool LyXTabular::getLTNewPage(row_type row) const
1642 return row_info[row].newpage;
1646 bool LyXTabular::haveLTHead() const
1648 for (row_type i = 0; i < rows_; ++i)
1649 if (row_info[i].endhead)
1655 bool LyXTabular::haveLTFirstHead() const
1657 if (endfirsthead.empty)
1659 for (row_type i = 0; i < rows_; ++i)
1660 if (row_info[i].endfirsthead)
1666 bool LyXTabular::haveLTFoot() const
1668 for (row_type i = 0; i < rows_; ++i)
1669 if (row_info[i].endfoot)
1675 bool LyXTabular::haveLTLastFoot() const
1677 if (endlastfoot.empty)
1679 for (row_type i = 0; i < rows_; ++i)
1680 if (row_info[i].endlastfoot)
1686 // end longtable support functions
1688 void LyXTabular::setAscentOfRow(row_type row, int height)
1690 if (row >= rows_ || row_info[row].ascent_of_row == height)
1692 row_info[row].ascent_of_row = height;
1696 void LyXTabular::setDescentOfRow(row_type row, int height)
1698 if (row >= rows_ || row_info[row].descent_of_row == height)
1700 row_info[row].descent_of_row = height;
1704 int LyXTabular::getAscentOfRow(row_type row) const
1708 return row_info[row].ascent_of_row;
1712 int LyXTabular::getDescentOfRow(row_type row) const
1714 BOOST_ASSERT(row < rows_);
1715 return row_info[row].descent_of_row;
1719 int LyXTabular::getHeightOfTabular() const
1722 for (row_type row = 0; row < rows_; ++row)
1723 height += getAscentOfRow(row) + getDescentOfRow(row) +
1724 getAdditionalHeight(row);
1729 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1731 BOOST_ASSERT(row < rows_);
1732 BOOST_ASSERT(column < columns_);
1733 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1737 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1739 // FIXME: assert or return 0 as in TeXBottomHLine()?
1740 BOOST_ASSERT(row != npos);
1741 BOOST_ASSERT(row < rows_);
1743 idx_type const fcell = getFirstCellInRow(row);
1744 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1747 for (idx_type i = fcell; i < n; ++i) {
1751 if (tmp == n - fcell) {
1754 for (idx_type i = fcell; i < n; ++i) {
1757 << column_of_cell(i) + 1
1759 << right_column_of_cell(i) + 1
1771 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1773 // FIXME: return 0 or assert as in TeXTopHLine()?
1774 if (row == npos || row >= rows_)
1777 idx_type const fcell = getFirstCellInRow(row);
1778 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1781 for (idx_type i = fcell; i < n; ++i) {
1785 if (tmp == n - fcell) {
1788 for (idx_type i = fcell; i < n; ++i) {
1789 if (bottomLine(i)) {
1791 << column_of_cell(i) + 1
1793 << right_column_of_cell(i) + 1
1805 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1809 if (getRotateCell(cell)) {
1810 os << "\\begin{sideways}\n";
1813 if (isMultiColumn(cell)) {
1814 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1815 if (!cellinfo_of_cell(cell).align_special.empty()) {
1816 os << cellinfo_of_cell(cell).align_special << "}{";
1818 if (leftLine(cell) &&
1819 (isFirstCellInRow(cell) ||
1820 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1821 !rightLine(cell - 1, true))))
1825 if (!getPWidth(cell).zero()) {
1826 switch (getVAlignment(cell)) {
1827 case LYX_VALIGN_TOP:
1830 case LYX_VALIGN_MIDDLE:
1833 case LYX_VALIGN_BOTTOM:
1838 << getPWidth(cell).asLatexString()
1841 switch (getAlignment(cell)) {
1842 case LYX_ALIGN_LEFT:
1845 case LYX_ALIGN_RIGHT:
1853 if (rightLine(cell))
1855 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1861 if (getUsebox(cell) == BOX_PARBOX) {
1863 switch (getVAlignment(cell)) {
1864 case LYX_VALIGN_TOP:
1867 case LYX_VALIGN_MIDDLE:
1870 case LYX_VALIGN_BOTTOM:
1874 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1875 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1876 os << "\\begin{minipage}[";
1877 switch (getVAlignment(cell)) {
1878 case LYX_VALIGN_TOP:
1881 case LYX_VALIGN_MIDDLE:
1884 case LYX_VALIGN_BOTTOM:
1888 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1895 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1900 if (getUsebox(cell) == BOX_PARBOX)
1902 else if (getUsebox(cell) == BOX_MINIPAGE) {
1903 os << "%\n\\end{minipage}";
1906 if (isMultiColumn(cell)) {
1909 if (getRotateCell(cell)) {
1910 os << "%\n\\end{sideways}";
1917 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1918 OutputParams const & runparams) const
1920 if (!is_long_tabular)
1924 // output header info
1926 if (endhead.topDL) {
1930 for (row_type i = 0; i < rows_; ++i) {
1931 if (row_info[i].endhead) {
1932 ret += TeXRow(os, i, buf, runparams);
1935 if (endhead.bottomDL) {
1939 os << "\\endhead\n";
1941 if (endfirsthead.empty) {
1942 os << "\\endfirsthead\n";
1946 // output firstheader info
1947 if (haveLTFirstHead()) {
1948 if (endfirsthead.topDL) {
1952 for (row_type i = 0; i < rows_; ++i) {
1953 if (row_info[i].endfirsthead) {
1954 ret += TeXRow(os, i, buf, runparams);
1957 if (endfirsthead.bottomDL) {
1961 os << "\\endfirsthead\n";
1964 // output footer info
1966 if (endfoot.topDL) {
1970 for (row_type i = 0; i < rows_; ++i) {
1971 if (row_info[i].endfoot) {
1972 ret += TeXRow(os, i, buf, runparams);
1975 if (endfoot.bottomDL) {
1979 os << "\\endfoot\n";
1981 if (endlastfoot.empty) {
1982 os << "\\endlastfoot\n";
1986 // output lastfooter info
1987 if (haveLTLastFoot()) {
1988 if (endlastfoot.topDL) {
1992 for (row_type i = 0; i < rows_; ++i) {
1993 if (row_info[i].endlastfoot) {
1994 ret += TeXRow(os, i, buf, runparams);
1997 if (endlastfoot.bottomDL) {
2001 os << "\\endlastfoot\n";
2008 bool LyXTabular::isValidRow(row_type row) const
2010 if (!is_long_tabular)
2012 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2013 !row_info[row].endfoot && !row_info[row].endlastfoot;
2017 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
2018 OutputParams const & runparams) const
2020 idx_type cell = getCellNumber(i, 0);
2022 int ret = TeXTopHLine(os, i);
2023 for (col_type j = 0; j < columns_; ++j) {
2024 if (isPartOfMultiColumn(i, j))
2026 ret += TeXCellPreamble(os, cell);
2027 shared_ptr<InsetText> inset = getCellInset(cell);
2029 Paragraph const & par = inset->paragraphs().front();
2030 bool rtl = par.isRightToLeftPar(buf.params())
2032 && getPWidth(cell).zero();
2036 ret += inset->latex(buf, os, runparams);
2040 ret += TeXCellPostamble(os, cell);
2041 if (!isLastCellInRow(cell)) { // not last cell in row
2047 os << "\\tabularnewline\n";
2049 ret += TeXBottomHLine(os, i);
2054 int LyXTabular::latex(Buffer const & buf, ostream & os,
2055 OutputParams const & runparams) const
2059 //+---------------------------------------------------------------------
2060 //+ first the opening preamble +
2061 //+---------------------------------------------------------------------
2064 os << "\\begin{sideways}\n";
2067 if (is_long_tabular)
2068 os << "\\begin{longtable}{";
2070 os << "\\begin{tabular}{";
2071 for (col_type i = 0; i < columns_; ++i) {
2072 if (!column_info[i].align_special.empty()) {
2073 os << column_info[i].align_special;
2075 if (column_info[i].left_line)
2077 if (!column_info[i].p_width.zero()) {
2078 switch (column_info[i].alignment) {
2079 case LYX_ALIGN_LEFT:
2080 os << ">{\\raggedright}";
2082 case LYX_ALIGN_RIGHT:
2083 os << ">{\\raggedleft}";
2085 case LYX_ALIGN_CENTER:
2086 os << ">{\\centering}";
2088 case LYX_ALIGN_NONE:
2089 case LYX_ALIGN_BLOCK:
2090 case LYX_ALIGN_LAYOUT:
2091 case LYX_ALIGN_SPECIAL:
2095 switch (column_info[i].valignment) {
2096 case LYX_VALIGN_TOP:
2099 case LYX_VALIGN_MIDDLE:
2102 case LYX_VALIGN_BOTTOM:
2107 << column_info[i].p_width.asLatexString()
2110 switch (column_info[i].alignment) {
2111 case LYX_ALIGN_LEFT:
2114 case LYX_ALIGN_RIGHT:
2122 if (column_info[i].right_line)
2129 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2131 //+---------------------------------------------------------------------
2132 //+ the single row and columns (cells) +
2133 //+---------------------------------------------------------------------
2135 for (row_type i = 0; i < rows_; ++i) {
2136 if (isValidRow(i)) {
2137 ret += TeXRow(os, i, buf, runparams);
2138 if (is_long_tabular && row_info[i].newpage) {
2139 os << "\\newpage\n";
2145 //+---------------------------------------------------------------------
2146 //+ the closing of the tabular +
2147 //+---------------------------------------------------------------------
2149 if (is_long_tabular)
2150 os << "\\end{longtable}";
2152 os << "\\end{tabular}";
2154 os << "\n\\end{sideways}";
2162 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2163 const OutputParams & runparams) const
2165 os << "<tabular ca=\"";
2166 for (col_type i = 0; i < columns_; ++i) {
2167 switch (column_info[i].alignment) {
2168 case LYX_ALIGN_LEFT:
2171 case LYX_ALIGN_RIGHT:
2182 for (row_type i = 0; i < rows_; ++i) {
2183 for (col_type j = 0; j < columns_; ++j) {
2184 if (isPartOfMultiColumn(i, j))
2186 shared_ptr<InsetText> inset = getCellInset(cell);
2188 ret += inset->linuxdoc(buf, os, runparams);
2190 if (isLastCellInRow(cell)) {
2199 os << "</tabular>\n";
2204 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2205 OutputParams const & runparams) const
2208 idx_type cell = getFirstCellInRow(row);
2211 for (col_type j = 0; j < columns_; ++j) {
2212 if (isPartOfMultiColumn(row, j))
2215 os << "<entry align=\"";
2216 switch (getAlignment(cell)) {
2217 case LYX_ALIGN_LEFT:
2220 case LYX_ALIGN_RIGHT:
2228 os << "\" valign=\"";
2229 switch (getVAlignment(cell)) {
2230 case LYX_VALIGN_TOP:
2233 case LYX_VALIGN_BOTTOM:
2236 case LYX_VALIGN_MIDDLE:
2241 if (isMultiColumn(cell)) {
2242 os << " namest=\"col" << j << "\" ";
2243 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2247 ret += getCellInset(cell)->docbook(buf, os, runparams);
2256 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2257 OutputParams const & runparams) const
2261 //+---------------------------------------------------------------------
2262 //+ first the opening preamble +
2263 //+---------------------------------------------------------------------
2265 os << "<tgroup cols=\"" << columns_
2266 << "\" colsep=\"1\" rowsep=\"1\">\n";
2268 for (col_type i = 0; i < columns_; ++i) {
2269 os << "<colspec colname=\"col" << i << "\" align=\"";
2270 switch (column_info[i].alignment) {
2271 case LYX_ALIGN_LEFT:
2274 case LYX_ALIGN_RIGHT:
2282 if (runparams.flavor == OutputParams::XML)
2288 //+---------------------------------------------------------------------
2289 //+ Long Tabular case +
2290 //+---------------------------------------------------------------------
2292 // output header info
2293 if (haveLTHead() || haveLTFirstHead()) {
2296 for (row_type i = 0; i < rows_; ++i) {
2297 if (row_info[i].endhead || row_info[i].endfirsthead) {
2298 ret += docbookRow(buf, os, i, runparams);
2304 // output footer info
2305 if (haveLTFoot() || haveLTLastFoot()) {
2308 for (row_type i = 0; i < rows_; ++i) {
2309 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2310 ret += docbookRow(buf, os, i, runparams);
2317 //+---------------------------------------------------------------------
2318 //+ the single row and columns (cells) +
2319 //+---------------------------------------------------------------------
2323 for (row_type i = 0; i < rows_; ++i) {
2324 if (isValidRow(i)) {
2325 ret += docbookRow(buf, os, i, runparams);
2330 //+---------------------------------------------------------------------
2331 //+ the closing of the tabular +
2332 //+---------------------------------------------------------------------
2341 int LyXTabular::asciiTopHLine(ostream & os, row_type row,
2342 vector<unsigned int> const & clen) const
2344 idx_type const fcell = getFirstCellInRow(row);
2345 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2348 for (idx_type i = fcell; i < n; ++i) {
2358 for (idx_type i = fcell; i < n; ++i) {
2369 col_type column = column_of_cell(i);
2370 int len = clen[column];
2371 while (column < columns_ - 1
2372 && isPartOfMultiColumn(row, ++column))
2373 len += clen[column] + 4;
2374 os << string(len, ch);
2389 int LyXTabular::asciiBottomHLine(ostream & os, row_type row,
2390 vector<unsigned int> const & clen) const
2392 idx_type const fcell = getFirstCellInRow(row);
2393 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2396 for (idx_type i = fcell; i < n; ++i) {
2397 if (bottomLine(i)) {
2406 for (idx_type i = fcell; i < n; ++i) {
2407 if (bottomLine(i)) {
2417 col_type column = column_of_cell(i);
2418 int len = clen[column];
2419 while (column < columns_ -1
2420 && isPartOfMultiColumn(row, ++column))
2421 len += clen[column] + 4;
2422 os << string(len, ch);
2423 if (bottomLine(i)) {
2437 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2438 OutputParams const & runparams,
2439 idx_type cell, row_type row, col_type column,
2440 vector<unsigned int> const & clen,
2441 bool onlydata) const
2444 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2456 unsigned int len1 = sstr.str().length();
2457 unsigned int len2 = clen[column];
2458 while (column < columns_ -1
2459 && isPartOfMultiColumn(row, ++column))
2460 len2 += clen[column] + 4;
2463 switch (getAlignment(cell)) {
2465 case LYX_ALIGN_LEFT:
2468 case LYX_ALIGN_RIGHT:
2472 case LYX_ALIGN_CENTER:
2478 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2480 if (rightLine(cell))
2489 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2490 OutputParams const & runparams,
2492 bool onlydata, unsigned char delim) const
2496 // first calculate the width of the single columns
2497 vector<unsigned int> clen(columns_);
2500 // first all non (real) multicolumn cells!
2501 for (col_type j = 0; j < columns_; ++j) {
2503 for (row_type i = 0; i < rows_; ++i) {
2504 idx_type cell = getCellNumber(i, j);
2505 if (isMultiColumnReal(cell))
2508 getCellInset(cell)->plaintext(buf, sstr, runparams);
2509 if (clen[j] < sstr.str().length())
2510 clen[j] = sstr.str().length();
2513 // then all (real) multicolumn cells!
2514 for (col_type j = 0; j < columns_; ++j) {
2515 for (row_type i = 0; i < rows_; ++i) {
2516 idx_type cell = getCellNumber(i, j);
2517 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2520 getCellInset(cell)->plaintext(buf, sstr, runparams);
2521 int len = int(sstr.str().length());
2522 idx_type const n = cells_in_multicolumn(cell);
2523 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2525 if (len > int(clen[j + n - 1]))
2526 clen[j + n - 1] = len;
2531 for (row_type i = 0; i < rows_; ++i) {
2532 if (!onlydata && asciiTopHLine(os, i, clen))
2533 os << string(depth * 2, ' ');
2534 for (col_type j = 0; j < columns_; ++j) {
2535 if (isPartOfMultiColumn(i, j))
2537 if (onlydata && j > 0)
2539 ret += asciiPrintCell(buf, os, runparams,
2540 cell, i, j, clen, onlydata);
2545 os << string(depth * 2, ' ');
2546 if (asciiBottomHLine(os, i, clen))
2547 os << string(depth * 2, ' ');
2554 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2556 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2560 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2561 col_type column) const
2563 return cell_info[row][column].inset;
2567 void LyXTabular::setCellInset(row_type row, col_type column,
2568 shared_ptr<InsetText> ins) const
2570 cell_info[row][column].inset = ins;
2574 LyXTabular::idx_type
2575 LyXTabular::getCellFromInset(InsetBase const * inset) const
2577 // is this inset part of the tabular?
2579 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2580 BOOST_ASSERT(false);
2583 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2584 if (getCellInset(cell).get() == inset) {
2585 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2586 << "cell=" << cell << endl;
2590 // We should have found a cell at this point
2591 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2592 << inset << " not found!" << endl;
2593 BOOST_ASSERT(false);
2599 void LyXTabular::validate(LaTeXFeatures & features) const
2601 features.require("NeedTabularnewline");
2602 if (isLongTabular())
2603 features.require("longtable");
2605 features.require("rotating");
2606 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2607 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2608 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2609 features.require("array");
2610 getCellInset(cell)->validate(features);
2615 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2617 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2618 ParagraphList::const_iterator cit = parlist.begin();
2619 ParagraphList::const_iterator end = parlist.end();
2621 for (; cit != end; ++cit)
2622 for (int i = 0; i < cit->size(); ++i)
2623 if (cit->isNewline(i))