3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
23 #include "bufferparams.h"
25 #include "LaTeXFeatures.h"
27 #include "outputparams.h"
28 #include "paragraph.h"
30 #include "insets/insettabular.h"
32 #include "support/lstrings.h"
33 #include "support/convert.h"
37 using lyx::support::ltrim;
38 using lyx::support::prefixIs;
39 using lyx::support::rtrim;
40 using lyx::support::strToInt;
41 using lyx::support::suffixIs;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
53 using std::ostringstream;
57 #ifndef CXX_GLOBAL_CSTD
64 int const WIDTH_OF_LINE = 5;
67 string const write_attribute(string const & name, T const & t)
69 string const s = tostr(t);
70 return s.empty() ? s : " " + name + "=\"" + s + "\"";
74 string const write_attribute(string const & name, string const & t)
76 return t.empty() ? t : " " + name + "=\"" + t + "\"";
81 string const write_attribute(string const & name, bool const & b)
83 // we write only true attribute values so we remove a bit of the
84 // file format bloat for tabulars.
85 return b ? write_attribute(name, convert<string>(b)) : string();
90 string const write_attribute(string const & name, int const & i)
92 // we write only true attribute values so we remove a bit of the
93 // file format bloat for tabulars.
94 return i ? write_attribute(name, convert<string>(i)) : string();
99 string const write_attribute(string const & name, LyXTabular::idx_type const & i)
101 // we write only true attribute values so we remove a bit of the
102 // file format bloat for tabulars.
103 return i ? write_attribute(name, convert<string>(i)) : string();
108 string const write_attribute(string const & name, LyXLength const & value)
110 // we write only the value if we really have one same reson as above.
111 return value.zero() ? string() : write_attribute(name, value.asString());
115 string const tostr(LyXAlignment const & num)
120 case LYX_ALIGN_BLOCK:
124 case LYX_ALIGN_CENTER:
126 case LYX_ALIGN_RIGHT:
128 case LYX_ALIGN_LAYOUT:
130 case LYX_ALIGN_SPECIAL:
137 string const tostr(LyXTabular::VAlignment const & num)
140 case LyXTabular::LYX_VALIGN_TOP:
142 case LyXTabular::LYX_VALIGN_MIDDLE:
144 case LyXTabular::LYX_VALIGN_BOTTOM:
151 string const tostr(LyXTabular::BoxType const & num)
154 case LyXTabular::BOX_NONE:
156 case LyXTabular::BOX_PARBOX:
158 case LyXTabular::BOX_MINIPAGE:
165 // I would have liked a fromstr template a lot better. (Lgb)
166 bool string2type(string const str, LyXAlignment & num)
169 num = LYX_ALIGN_NONE;
170 else if (str == "block")
171 num = LYX_ALIGN_BLOCK;
172 else if (str == "left")
173 num = LYX_ALIGN_LEFT;
174 else if (str == "center")
175 num = LYX_ALIGN_CENTER;
176 else if (str == "right")
177 num = LYX_ALIGN_RIGHT;
184 bool string2type(string const str, LyXTabular::VAlignment & num)
187 num = LyXTabular::LYX_VALIGN_TOP;
188 else if (str == "middle" )
189 num = LyXTabular::LYX_VALIGN_MIDDLE;
190 else if (str == "bottom")
191 num = LyXTabular::LYX_VALIGN_BOTTOM;
198 bool string2type(string const str, LyXTabular::BoxType & num)
201 num = LyXTabular::BOX_NONE;
202 else if (str == "parbox")
203 num = LyXTabular::BOX_PARBOX;
204 else if (str == "minipage")
205 num = LyXTabular::BOX_MINIPAGE;
212 bool string2type(string const str, bool & num)
216 else if (str == "false")
224 bool getTokenValue(string const & str, char const * token, string & ret)
227 size_t token_length = strlen(token);
228 string::size_type pos = str.find(token);
230 if (pos == string::npos || pos + token_length + 1 >= str.length()
231 || str[pos + token_length] != '=')
233 pos += token_length + 1;
235 if (ch != '"' && ch != '\'') { // only read till next space
239 while (pos < str.length() - 1 && str[++pos] != ch)
246 bool getTokenValue(string const & str, char const * token, int & num)
250 if (!getTokenValue(str, token, tmp))
257 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
260 return getTokenValue(str, token, tmp) && string2type(tmp, num);
264 bool getTokenValue(string const & str, char const * token,
265 LyXTabular::VAlignment & num)
268 return getTokenValue(str, token, tmp) && string2type(tmp, num);
272 bool getTokenValue(string const & str, char const * token,
273 LyXTabular::BoxType & num)
276 return getTokenValue(str, token, tmp) && string2type(tmp, num);
280 bool getTokenValue(string const & str, char const * token, bool & flag)
282 // set the flag always to false as this should be the default for bools
283 // not in the file-format.
286 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
290 bool getTokenValue(string const & str, char const * token, LyXLength & len)
292 // set the lenght to be zero() as default as this it should be if not
293 // in the file format.
296 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
300 void l_getline(istream & is, string & str)
303 while (str.empty()) {
305 if (!str.empty() && str[str.length() - 1] == '\r')
306 str.erase(str.length() - 1);
312 /// Define a few methods for the inner structs
314 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
317 multicolumn(LyXTabular::CELL_NORMAL),
318 alignment(LYX_ALIGN_CENTER),
319 valignment(LYX_VALIGN_TOP),
326 inset(new InsetText(bp))
330 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
332 width_of_cell(cs.width_of_cell),
333 multicolumn(cs.multicolumn),
334 alignment(cs.alignment),
335 valignment(cs.valignment),
336 top_line(cs.top_line),
337 bottom_line(cs.bottom_line),
338 left_line(cs.left_line),
339 right_line(cs.right_line),
342 align_special(cs.align_special),
344 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
348 LyXTabular::cellstruct &
349 LyXTabular::cellstruct::operator=(cellstruct cs)
357 LyXTabular::cellstruct::swap(cellstruct & rhs)
359 std::swap(cellno, rhs.cellno);
360 std::swap(width_of_cell, rhs.width_of_cell);
361 std::swap(multicolumn, rhs.multicolumn);
362 std::swap(alignment, rhs.alignment);
363 std::swap(valignment, rhs.valignment);
364 std::swap(top_line, rhs.top_line);
365 std::swap(bottom_line, rhs.bottom_line);
366 std::swap(left_line, rhs.left_line);
367 std::swap(right_line, rhs.right_line);
368 std::swap(usebox, rhs.usebox);
369 std::swap(rotate, rhs.rotate);
370 std::swap(align_special, rhs.align_special);
371 p_width.swap(rhs.p_width);
372 inset.swap(rhs.inset);
376 LyXTabular::rowstruct::rowstruct()
389 LyXTabular::columnstruct::columnstruct()
390 : alignment(LYX_ALIGN_CENTER),
391 valignment(LYX_VALIGN_TOP),
399 LyXTabular::ltType::ltType()
406 LyXTabular::LyXTabular(BufferParams const & bp, row_type rows_arg,
407 col_type columns_arg)
409 init(bp, rows_arg, columns_arg);
413 // activates all lines and sets all widths to 0
414 void LyXTabular::init(BufferParams const & bp, row_type rows_arg,
415 col_type columns_arg)
418 columns_ = columns_arg;
419 row_info = row_vector(rows_);
420 column_info = column_vector(columns_);
421 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
422 row_info.reserve(10);
423 column_info.reserve(10);
424 cell_info.reserve(100);
426 for (row_type i = 0; i < rows_; ++i)
427 cell_info[i].back().right_line = true;
428 row_info.back().bottom_line = true;
429 row_info.front().bottom_line = true;
430 column_info.back().right_line = true;
431 is_long_tabular = false;
436 void LyXTabular::fixCellNums()
439 for (row_type i = 0; i < rows_; ++i) {
440 for (col_type j = 0; j < columns_; ++j) {
441 // When debugging it can be nice to set
443 cell_info[i][j].inset->setDrawFrame(false);
444 cell_info[i][j].cellno = cellno++;
446 cell_info[i].back().right_line = true;
449 set_row_column_number_info();
453 void LyXTabular::appendRow(BufferParams const & bp, idx_type const cell)
457 row_type const row = row_of_cell(cell);
459 row_vector::iterator rit = row_info.begin() + row;
460 row_info.insert(rit, rowstruct());
461 // now set the values of the row before
462 row_info[row] = row_info[row + 1];
464 cell_vvector old(rows_ - 1);
465 for (row_type i = 0; i < rows_ - 1; ++i)
466 swap(cell_info[i], old[i]);
468 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
470 for (row_type i = 0; i <= row; ++i)
471 swap(cell_info[i], old[i]);
472 for (row_type i = row + 2; i < rows_; ++i)
473 swap(cell_info[i], old[i - 1]);
475 if (bp.tracking_changes)
476 for (col_type j = 0; j < columns_; ++j)
477 cell_info[row + 1][j].inset->markNew(true);
479 set_row_column_number_info();
483 void LyXTabular::deleteRow(row_type const row)
485 // Not allowed to delete last row
489 row_info.erase(row_info.begin() + row);
490 cell_info.erase(cell_info.begin() + row);
496 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
500 col_type const column = column_of_cell(cell);
501 column_vector::iterator cit = column_info.begin() + column + 1;
502 column_info.insert(cit, columnstruct());
503 // set the column values of the column before
504 column_info[column + 1] = column_info[column];
506 for (row_type i = 0; i < rows_; ++i) {
507 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
509 // care about multicolumns
510 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
511 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
513 if (column + 2 >= columns_
514 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
515 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
518 for (row_type i = 0; i < rows_; ++i) {
519 cell_info[i][column + 1].inset->clear(false);
520 if (bp.tracking_changes)
521 cell_info[i][column + 1].inset->markNew(true);
527 void LyXTabular::deleteColumn(col_type const column)
529 // Not allowed to delete last column
533 column_info.erase(column_info.begin() + column);
534 for (row_type i = 0; i < rows_; ++i)
535 cell_info[i].erase(cell_info[i].begin() + column);
541 void LyXTabular::set_row_column_number_info()
544 for (row_type row = 0; row < rows_; ++row) {
545 for (col_type column = 0; column < columns_; ++column) {
546 if (cell_info[row][column].multicolumn
547 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
549 if (numberofcells == 0)
550 // FIXME: Is this intended?
551 cell_info[row][column].cellno = npos;
553 cell_info[row][column].cellno =
558 rowofcell.resize(numberofcells);
559 columnofcell.resize(numberofcells);
564 c < numberofcells && row < rows_ && column < columns_;) {
566 columnofcell[c] = column;
570 } while (column < columns_ &&
571 cell_info[row][column].multicolumn
572 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
574 if (column == columns_) {
580 for (row_type row = 0; row < rows_; ++row) {
581 for (col_type column = 0; column < columns_; ++column) {
582 if (isPartOfMultiColumn(row,column))
584 cell_info[row][column].inset->setAutoBreakRows(
585 !getPWidth(getCellNumber(row, column)).zero());
591 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
593 return numberofcells;
597 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
599 row_type const row = row_of_cell(cell);
601 for (col_type i = 0; i < columns_; ++i)
602 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
608 // returns 1 if there is a topline, returns 0 if not
609 bool LyXTabular::topLine(idx_type const cell, bool const onlycolumn) const
611 if (!onlycolumn && isMultiColumn(cell))
612 return cellinfo_of_cell(cell).top_line;
613 return row_info[row_of_cell(cell)].top_line;
617 bool LyXTabular::bottomLine(idx_type const cell, bool onlycolumn) const
619 if (!onlycolumn && isMultiColumn(cell))
620 return cellinfo_of_cell(cell).bottom_line;
621 return row_info[row_of_cell(cell)].bottom_line;
625 bool LyXTabular::leftLine(idx_type cell, bool onlycolumn) const
627 if (!onlycolumn && isMultiColumn(cell) &&
628 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
630 if (cellinfo_of_cell(cell).align_special.empty())
631 return cellinfo_of_cell(cell).left_line;
632 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
634 if (column_info[column_of_cell(cell)].align_special.empty())
635 return column_info[column_of_cell(cell)].left_line;
636 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
640 bool LyXTabular::rightLine(idx_type cell, bool onlycolumn) const
642 if (!onlycolumn && isMultiColumn(cell) &&
643 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
645 if (cellinfo_of_cell(cell).align_special.empty())
646 return cellinfo_of_cell(cell).right_line;
647 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
649 if (column_info[column_of_cell(cell)].align_special.empty())
650 return column_info[right_column_of_cell(cell)].right_line;
651 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
655 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
657 row_type row = row_of_cell(cell);
658 if (row > 0 && !getAdditionalHeight(row)) {
659 col_type column = column_of_cell(cell);
662 && cell_info[row][column].multicolumn
663 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
665 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
666 return row_info[row].bottom_line;
668 return cell_info[row][column].bottom_line;
674 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
676 col_type column = column_of_cell(cell);
678 row_type row = row_of_cell(cell);
680 (cell_info[row][column].multicolumn ==
681 LyXTabular::CELL_PART_OF_MULTICOLUMN));
682 if (getAdditionalWidth(cell_info[row][column].cellno))
684 return rightLine(cell_info[row][column].cellno);
690 bool LyXTabular::isLastRow(idx_type cell) const
692 return row_of_cell(cell) == rows_ - 1;
696 int LyXTabular::getAdditionalHeight(row_type row) const
698 if (!row || row >= rows_)
704 for (col_type column = 0; column < columns_ && bottom; ++column) {
705 switch (cell_info[row - 1][column].multicolumn) {
706 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
707 bottom = cell_info[row - 1][column].bottom_line;
709 case LyXTabular::CELL_NORMAL:
710 bottom = row_info[row - 1].bottom_line;
713 for (col_type column = 0; column < columns_ && top; ++column) {
714 switch (cell_info[row][column].multicolumn) {
715 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
716 top = cell_info[row][column].top_line;
718 case LyXTabular::CELL_NORMAL:
719 top = row_info[row].top_line;
723 return WIDTH_OF_LINE;
728 int LyXTabular::getAdditionalWidth(idx_type cell) const
730 // internally already set in setWidthOfCell
731 // used to get it back in text.C
732 col_type const col = right_column_of_cell(cell);
733 row_type const row = row_of_cell(cell);
734 if (col < columns_ - 1 && rightLine(cell) &&
735 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
737 return WIDTH_OF_LINE;
743 // returns the maximum over all rows
744 int LyXTabular::getWidthOfColumn(idx_type cell) const
746 col_type const column1 = column_of_cell(cell);
747 col_type const column2 = right_column_of_cell(cell);
749 for (col_type i = column1; i <= column2; ++i)
750 result += column_info[i].width_of_column;
755 int LyXTabular::getWidthOfTabular() const
757 return width_of_tabular;
761 // returns true if a complete update is necessary, otherwise false
762 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
764 if (!isMultiColumn(cell))
767 row_type const row = row_of_cell(cell);
768 col_type const column1 = column_of_cell(cell);
769 col_type const column2 = right_column_of_cell(cell);
770 int const old_val = cell_info[row][column2].width_of_cell;
772 // first set columns to 0 so we can calculate the right width
773 for (col_type i = column1; i <= column2; ++i) {
774 cell_info[row][i].width_of_cell = 0;
776 // set the width to MAX_WIDTH until width > 0
777 int width = new_width + 2 * WIDTH_OF_LINE;
778 col_type i = column1;
779 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
780 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
781 width -= column_info[i].width_of_column;
784 cell_info[row][i].width_of_cell = width;
786 if (old_val != cell_info[row][column2].width_of_cell) {
787 // in this case we have to recalculate all multicolumn cells which
788 // have this column as one of theirs but not as last one
789 calculate_width_of_column_NMC(i);
790 recalculateMulticolumnsOfColumn(i);
791 calculate_width_of_column(i);
797 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
799 // the last column does not have to be recalculated because all
800 // multicolumns will have here there last multicolumn cell which
801 // always will have the whole rest of the width of the cell.
802 if (column > (columns_ - 2))
804 for(row_type row = 0; row < rows_; ++row) {
805 int mc = cell_info[row][column].multicolumn;
806 int nmc = cell_info[row][column+1].multicolumn;
807 // we only have to update multicolumns which do not have this
808 // column as their last column!
809 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
810 (mc == CELL_PART_OF_MULTICOLUMN &&
811 nmc == CELL_PART_OF_MULTICOLUMN))
813 idx_type const cellno = cell_info[row][column].cellno;
814 setWidthOfMulticolCell(cellno,
815 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
821 // returns 1 if a complete update is necessary, otherwise 0
822 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
824 row_type const row = row_of_cell(cell);
825 col_type const column1 = column_of_cell(cell);
830 if (rightLine(cell_info[row][column1].cellno, true) &&
831 column1 < columns_ - 1 &&
832 leftLine(cell_info[row][column1+1].cellno, true))
834 add_width = WIDTH_OF_LINE;
837 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
840 if (isMultiColumnReal(cell)) {
841 tmp = setWidthOfMulticolCell(cell, new_width);
843 width = new_width + 2 * WIDTH_OF_LINE + add_width;
844 cell_info[row][column1].width_of_cell = width;
845 tmp = calculate_width_of_column_NMC(column1);
847 recalculateMulticolumnsOfColumn(column1);
850 for (col_type i = 0; i < columns_; ++i)
851 calculate_width_of_column(i);
852 calculate_width_of_tabular();
857 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
860 if (!isMultiColumn(cell) || onlycolumn)
861 column_info[column_of_cell(cell)].alignment = align;
863 cellinfo_of_cell(cell).alignment = align;
867 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
870 if (!isMultiColumn(cell) || onlycolumn)
871 column_info[column_of_cell(cell)].valignment = align;
873 cellinfo_of_cell(cell).valignment = align;
877 void LyXTabular::setColumnPWidth(idx_type cell, LyXLength const & width)
879 col_type const j = column_of_cell(cell);
881 column_info[j].p_width = width;
882 for (row_type i = 0; i < rows_; ++i) {
883 idx_type const cell = getCellNumber(i, j);
884 // because of multicolumns
885 getCellInset(cell)->setAutoBreakRows(!getPWidth(cell).zero());
890 bool LyXTabular::setMColumnPWidth(idx_type cell, LyXLength const & width)
892 if (!isMultiColumn(cell))
895 cellinfo_of_cell(cell).p_width = width;
896 getCellInset(cell)->setAutoBreakRows(!width.zero());
901 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
902 LyXTabular::Feature what)
904 if (what == SET_SPECIAL_MULTI)
905 cellinfo_of_cell(cell).align_special = special;
907 column_info[column_of_cell(cell)].align_special = special;
911 void LyXTabular::setAllLines(idx_type cell, bool line)
913 setTopLine(cell, line);
914 setBottomLine(cell, line);
915 setRightLine(cell, line);
916 setLeftLine(cell, line);
920 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
922 row_type const row = row_of_cell(cell);
923 if (onlycolumn || !isMultiColumn(cell))
924 row_info[row].top_line = line;
926 cellinfo_of_cell(cell).top_line = line;
930 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
932 if (onlycolumn || !isMultiColumn(cell))
933 row_info[row_of_cell(cell)].bottom_line = line;
935 cellinfo_of_cell(cell).bottom_line = line;
939 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
941 if (onlycolumn || !isMultiColumn(cell))
942 column_info[column_of_cell(cell)].left_line = line;
944 cellinfo_of_cell(cell).left_line = line;
948 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
950 if (onlycolumn || !isMultiColumn(cell))
951 column_info[right_column_of_cell(cell)].right_line = line;
953 cellinfo_of_cell(cell).right_line = line;
957 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
959 if (!onlycolumn && isMultiColumn(cell))
960 return cellinfo_of_cell(cell).alignment;
961 return column_info[column_of_cell(cell)].alignment;
965 LyXTabular::VAlignment
966 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
968 if (!onlycolumn && isMultiColumn(cell))
969 return cellinfo_of_cell(cell).valignment;
970 return column_info[column_of_cell(cell)].valignment;
974 LyXLength const LyXTabular::getPWidth(idx_type cell) const
976 if (isMultiColumn(cell))
977 return cellinfo_of_cell(cell).p_width;
978 return column_info[column_of_cell(cell)].p_width;
982 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
984 return column_info[column_of_cell(cell)].p_width;
988 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
990 if (isMultiColumn(cell))
991 return cellinfo_of_cell(cell).p_width;
996 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
998 if (what == SET_SPECIAL_MULTI)
999 return cellinfo_of_cell(cell).align_special;
1000 return column_info[column_of_cell(cell)].align_special;
1004 int LyXTabular::getWidthOfCell(idx_type cell) const
1006 row_type const row = row_of_cell(cell);
1007 col_type const column1 = column_of_cell(cell);
1008 col_type const column2 = right_column_of_cell(cell);
1010 for (col_type i = column1; i <= column2; ++i)
1011 result += cell_info[row][i].width_of_cell;
1016 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1020 switch (getAlignment(cell)) {
1021 case LYX_ALIGN_CENTER:
1022 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1024 case LYX_ALIGN_RIGHT:
1025 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1026 // + getAdditionalWidth(cell);
1029 // LYX_ALIGN_LEFT: nothing :-)
1033 // the LaTeX Way :-(
1039 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1041 return column_of_cell(cell) == 0;
1045 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1047 if (row > rows_ - 1)
1049 return cell_info[row][0].cellno;
1053 bool LyXTabular::isLastCellInRow(idx_type cell) const
1055 return right_column_of_cell(cell) == columns_ - 1;
1059 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1061 if (row > rows_ - 1)
1063 return cell_info[row][columns_-1].cellno;
1067 void LyXTabular::calculate_width_of_column(col_type column)
1070 for (row_type i = 0; i < rows_; ++i)
1071 maximum = max(cell_info[i][column].width_of_cell, maximum);
1072 column_info[column].width_of_column = maximum;
1077 // Calculate the columns regarding ONLY the normal cells and if this
1078 // column is inside a multicolumn cell then use it only if its the last
1079 // column of this multicolumn cell as this gives an added width to the
1080 // column, all the rest should be adapted!
1082 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1084 int const old_column_width = column_info[column].width_of_column;
1086 for (row_type i = 0; i < rows_; ++i) {
1087 idx_type cell = getCellNumber(i, column);
1088 bool ismulti = isMultiColumnReal(cell);
1089 if ((!ismulti || column == right_column_of_cell(cell)) &&
1090 cell_info[i][column].width_of_cell > max)
1092 max = cell_info[i][column].width_of_cell;
1095 column_info[column].width_of_column = max;
1096 return column_info[column].width_of_column != old_column_width;
1100 void LyXTabular::calculate_width_of_tabular()
1102 width_of_tabular = 0;
1103 for (col_type i = 0; i < columns_; ++i)
1104 width_of_tabular += column_info[i].width_of_column;
1108 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1110 if (cell >= numberofcells)
1114 return rowofcell[cell];
1118 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1120 if (cell >= numberofcells)
1121 return columns_ - 1;
1124 return columnofcell[cell];
1128 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1130 row_type const row = row_of_cell(cell);
1131 col_type column = column_of_cell(cell);
1132 while (column < columns_ - 1 &&
1133 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1139 void LyXTabular::write(Buffer const & buf, ostream & os) const
1143 << write_attribute("version", 3)
1144 << write_attribute("rows", rows_)
1145 << write_attribute("columns", columns_)
1147 // global longtable options
1149 << write_attribute("rotate", rotate)
1150 << write_attribute("islongtable", is_long_tabular)
1151 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1152 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1153 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1154 << write_attribute("headTopDL", endhead.topDL)
1155 << write_attribute("headBottomDL", endhead.bottomDL)
1156 << write_attribute("footTopDL", endfoot.topDL)
1157 << write_attribute("footBottomDL", endfoot.bottomDL)
1158 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1159 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1160 << write_attribute("lastFootEmpty", endlastfoot.empty)
1162 for (col_type j = 0; j < columns_; ++j) {
1164 << write_attribute("alignment", column_info[j].alignment)
1165 << write_attribute("valignment", column_info[j].valignment)
1166 << write_attribute("leftline", column_info[j].left_line)
1167 << write_attribute("rightline", column_info[j].right_line)
1168 << write_attribute("width", column_info[j].p_width.asString())
1169 << write_attribute("special", column_info[j].align_special)
1172 for (row_type i = 0; i < rows_; ++i) {
1174 << write_attribute("topline", row_info[i].top_line)
1175 << write_attribute("bottomline", row_info[i].bottom_line)
1176 << write_attribute("endhead", row_info[i].endhead)
1177 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1178 << write_attribute("endfoot", row_info[i].endfoot)
1179 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1180 << write_attribute("newpage", row_info[i].newpage)
1182 for (col_type j = 0; j < columns_; ++j) {
1184 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1185 << write_attribute("alignment", cell_info[i][j].alignment)
1186 << write_attribute("valignment", cell_info[i][j].valignment)
1187 << write_attribute("topline", cell_info[i][j].top_line)
1188 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1189 << write_attribute("leftline", cell_info[i][j].left_line)
1190 << write_attribute("rightline", cell_info[i][j].right_line)
1191 << write_attribute("rotate", cell_info[i][j].rotate)
1192 << write_attribute("usebox", cell_info[i][j].usebox)
1193 << write_attribute("width", cell_info[i][j].p_width)
1194 << write_attribute("special", cell_info[i][j].align_special)
1196 os << "\\begin_inset ";
1197 cell_info[i][j].inset->write(buf, os);
1198 os << "\n\\end_inset\n"
1203 os << "</lyxtabular>\n";
1207 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1210 istream & is = lex.getStream();
1212 l_getline(is, line);
1213 if (!prefixIs(line, "<lyxtabular ")
1214 && !prefixIs(line, "<LyXTabular ")) {
1215 BOOST_ASSERT(false);
1220 if (!getTokenValue(line, "version", version))
1222 BOOST_ASSERT(version >= 2);
1225 if (!getTokenValue(line, "rows", rows_arg))
1228 if (!getTokenValue(line, "columns", columns_arg))
1230 init(buf.params(), rows_arg, columns_arg);
1231 l_getline(is, line);
1232 if (!prefixIs(line, "<features")) {
1233 lyxerr << "Wrong tabular format (expected <features ...> got"
1234 << line << ')' << endl;
1237 getTokenValue(line, "rotate", rotate);
1238 getTokenValue(line, "islongtable", is_long_tabular);
1239 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1240 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1241 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1242 getTokenValue(line, "headTopDL", endhead.topDL);
1243 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1244 getTokenValue(line, "footTopDL", endfoot.topDL);
1245 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1246 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1247 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1248 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1250 for (col_type j = 0; j < columns_; ++j) {
1252 if (!prefixIs(line,"<column")) {
1253 lyxerr << "Wrong tabular format (expected <column ...> got"
1254 << line << ')' << endl;
1257 getTokenValue(line, "alignment", column_info[j].alignment);
1258 getTokenValue(line, "valignment", column_info[j].valignment);
1259 getTokenValue(line, "leftline", column_info[j].left_line);
1260 getTokenValue(line, "rightline", column_info[j].right_line);
1261 getTokenValue(line, "width", column_info[j].p_width);
1262 getTokenValue(line, "special", column_info[j].align_special);
1265 for (row_type i = 0; i < rows_; ++i) {
1266 l_getline(is, line);
1267 if (!prefixIs(line, "<row")) {
1268 lyxerr << "Wrong tabular format (expected <row ...> got"
1269 << line << ')' << endl;
1272 getTokenValue(line, "topline", row_info[i].top_line);
1273 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1274 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1275 getTokenValue(line, "endhead", row_info[i].endhead);
1276 getTokenValue(line, "endfoot", row_info[i].endfoot);
1277 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1278 getTokenValue(line, "newpage", row_info[i].newpage);
1279 for (col_type j = 0; j < columns_; ++j) {
1280 l_getline(is, line);
1281 if (!prefixIs(line, "<cell")) {
1282 lyxerr << "Wrong tabular format (expected <cell ...> got"
1283 << line << ')' << endl;
1286 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1287 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1288 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1289 getTokenValue(line, "topline", cell_info[i][j].top_line);
1290 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1291 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1292 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1293 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1294 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1295 getTokenValue(line, "width", cell_info[i][j].p_width);
1296 getTokenValue(line, "special", cell_info[i][j].align_special);
1297 l_getline(is, line);
1298 if (prefixIs(line, "\\begin_inset")) {
1299 cell_info[i][j].inset->read(buf, lex);
1300 l_getline(is, line);
1302 if (!prefixIs(line, "</cell>")) {
1303 lyxerr << "Wrong tabular format (expected </cell> got"
1304 << line << ')' << endl;
1308 l_getline(is, line);
1309 if (!prefixIs(line, "</row>")) {
1310 lyxerr << "Wrong tabular format (expected </row> got"
1311 << line << ')' << endl;
1315 while (!prefixIs(line, "</lyxtabular>")) {
1316 l_getline(is, line);
1318 set_row_column_number_info();
1322 bool LyXTabular::isMultiColumn(idx_type cell) const
1324 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1328 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1330 return column_of_cell(cell) != right_column_of_cell(cell) &&
1331 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1335 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1337 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1341 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1344 cellstruct & cs = cellinfo_of_cell(cell);
1345 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1346 cs.alignment = column_info[column_of_cell(cell)].alignment;
1347 cs.top_line = row_info[row_of_cell(cell)].top_line;
1348 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1349 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1350 for (idx_type i = 1; i < number; ++i) {
1351 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1352 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1353 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1354 cs1.inset->clear(false);
1356 set_row_column_number_info();
1360 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1362 row_type const row = row_of_cell(cell);
1363 col_type column = column_of_cell(cell);
1364 idx_type result = 1;
1366 while (column < columns_ &&
1367 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1376 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1378 row_type const row = row_of_cell(cell);
1379 col_type column = column_of_cell(cell);
1381 idx_type result = 0;
1383 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1384 cell_info[row][column].multicolumn = CELL_NORMAL;
1386 while (column < columns_ &&
1387 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1389 cell_info[row][column].multicolumn = CELL_NORMAL;
1394 set_row_column_number_info();
1399 void LyXTabular::setLongTabular(bool what)
1401 is_long_tabular = what;
1405 bool LyXTabular::isLongTabular() const
1407 return is_long_tabular;
1411 void LyXTabular::setRotateTabular(bool flag)
1417 bool LyXTabular::getRotateTabular() const
1423 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1425 cellinfo_of_cell(cell).rotate = flag;
1429 bool LyXTabular::getRotateCell(idx_type cell) const
1431 return cellinfo_of_cell(cell).rotate;
1435 bool LyXTabular::needRotating() const
1439 for (row_type i = 0; i < rows_; ++i)
1440 for (col_type j = 0; j < columns_; ++j)
1441 if (cell_info[i][j].rotate)
1447 bool LyXTabular::isLastCell(idx_type cell) const
1449 if (cell + 1 < numberofcells)
1455 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1457 if (row_of_cell(cell) > 0)
1458 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1463 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1465 if (row_of_cell(cell) + 1 < rows_)
1466 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1471 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1473 if (row_of_cell(cell) == 0)
1475 if (!isMultiColumn(cell))
1476 return getCellAbove(cell);
1477 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1481 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1483 if (row_of_cell(cell) + 1 >= rows_)
1485 if (!isMultiColumn(cell))
1486 return getCellBelow(cell);
1487 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1491 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1492 col_type column) const
1494 BOOST_ASSERT(column != npos && column < columns_ &&
1495 row != npos && row < rows_);
1496 return cell_info[row][column].cellno;
1500 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1502 cellinfo_of_cell(cell).usebox = type;
1506 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1508 if (column_info[column_of_cell(cell)].p_width.zero() &&
1509 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1511 if (cellinfo_of_cell(cell).usebox > 1)
1512 return cellinfo_of_cell(cell).usebox;
1513 return useParbox(cell);
1518 // This are functions used for the longtable support
1520 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1526 row_info[row].endfirsthead = flag;
1530 row_info[row].endhead = flag;
1535 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1538 hd.set = haveLTHead();
1539 return row_info[row].endhead;
1543 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1546 hd.set = haveLTFirstHead();
1547 return row_info[row].endfirsthead;
1551 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1557 row_info[row].endlastfoot = flag;
1561 row_info[row].endfoot = flag;
1566 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1569 fd.set = haveLTFoot();
1570 return row_info[row].endfoot;
1574 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1577 fd.set = haveLTLastFoot();
1578 return row_info[row].endlastfoot;
1582 void LyXTabular::setLTNewPage(row_type row, bool what)
1584 row_info[row].newpage = what;
1588 bool LyXTabular::getLTNewPage(row_type row) const
1590 return row_info[row].newpage;
1594 bool LyXTabular::haveLTHead() const
1596 for (row_type i = 0; i < rows_; ++i)
1597 if (row_info[i].endhead)
1603 bool LyXTabular::haveLTFirstHead() const
1605 if (endfirsthead.empty)
1607 for (row_type i = 0; i < rows_; ++i)
1608 if (row_info[i].endfirsthead)
1614 bool LyXTabular::haveLTFoot() const
1616 for (row_type i = 0; i < rows_; ++i)
1617 if (row_info[i].endfoot)
1623 bool LyXTabular::haveLTLastFoot() const
1625 if (endlastfoot.empty)
1627 for (row_type i = 0; i < rows_; ++i)
1628 if (row_info[i].endlastfoot)
1634 // end longtable support functions
1636 void LyXTabular::setAscentOfRow(row_type row, int height)
1638 if (row >= rows_ || row_info[row].ascent_of_row == height)
1640 row_info[row].ascent_of_row = height;
1644 void LyXTabular::setDescentOfRow(row_type row, int height)
1646 if (row >= rows_ || row_info[row].descent_of_row == height)
1648 row_info[row].descent_of_row = height;
1652 int LyXTabular::getAscentOfRow(row_type row) const
1656 return row_info[row].ascent_of_row;
1660 int LyXTabular::getDescentOfRow(row_type row) const
1662 BOOST_ASSERT(row < rows_);
1663 return row_info[row].descent_of_row;
1667 int LyXTabular::getHeightOfTabular() const
1670 for (row_type row = 0; row < rows_; ++row)
1671 height += getAscentOfRow(row) + getDescentOfRow(row) +
1672 getAdditionalHeight(row);
1677 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1679 BOOST_ASSERT(row < rows_);
1680 BOOST_ASSERT(column < columns_);
1681 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1685 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1687 // FIXME: assert or return 0 as in TeXBottomHLine()?
1688 BOOST_ASSERT(row != npos);
1689 BOOST_ASSERT(row < rows_);
1691 idx_type const fcell = getFirstCellInRow(row);
1692 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1695 for (idx_type i = fcell; i < n; ++i) {
1699 if (tmp == n - fcell) {
1702 for (idx_type i = fcell; i < n; ++i) {
1705 << column_of_cell(i) + 1
1707 << right_column_of_cell(i) + 1
1719 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1721 // FIXME: return 0 or assert as in TeXTopHLine()?
1722 if (row == npos || row >= rows_)
1725 idx_type const fcell = getFirstCellInRow(row);
1726 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1729 for (idx_type i = fcell; i < n; ++i) {
1733 if (tmp == n - fcell) {
1736 for (idx_type i = fcell; i < n; ++i) {
1737 if (bottomLine(i)) {
1739 << column_of_cell(i) + 1
1741 << right_column_of_cell(i) + 1
1753 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1757 if (getRotateCell(cell)) {
1758 os << "\\begin{sideways}\n";
1761 if (isMultiColumn(cell)) {
1762 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1763 if (!cellinfo_of_cell(cell).align_special.empty()) {
1764 os << cellinfo_of_cell(cell).align_special << "}{";
1766 if (leftLine(cell) &&
1767 (isFirstCellInRow(cell) ||
1768 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1769 !rightLine(cell - 1, true))))
1773 if (!getPWidth(cell).zero()) {
1774 switch (getVAlignment(cell)) {
1775 case LYX_VALIGN_TOP:
1778 case LYX_VALIGN_MIDDLE:
1781 case LYX_VALIGN_BOTTOM:
1786 << getPWidth(cell).asLatexString()
1789 switch (getAlignment(cell)) {
1790 case LYX_ALIGN_LEFT:
1793 case LYX_ALIGN_RIGHT:
1801 if (rightLine(cell))
1803 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1809 if (getUsebox(cell) == BOX_PARBOX) {
1811 switch (getVAlignment(cell)) {
1812 case LYX_VALIGN_TOP:
1815 case LYX_VALIGN_MIDDLE:
1818 case LYX_VALIGN_BOTTOM:
1822 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1823 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1824 os << "\\begin{minipage}[";
1825 switch (getVAlignment(cell)) {
1826 case LYX_VALIGN_TOP:
1829 case LYX_VALIGN_MIDDLE:
1832 case LYX_VALIGN_BOTTOM:
1836 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1843 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1848 if (getUsebox(cell) == BOX_PARBOX)
1850 else if (getUsebox(cell) == BOX_MINIPAGE) {
1851 os << "%\n\\end{minipage}";
1854 if (isMultiColumn(cell)) {
1857 if (getRotateCell(cell)) {
1858 os << "%\n\\end{sideways}";
1865 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1866 OutputParams const & runparams) const
1868 if (!is_long_tabular)
1872 // output header info
1874 if (endhead.topDL) {
1878 for (row_type i = 0; i < rows_; ++i) {
1879 if (row_info[i].endhead) {
1880 ret += TeXRow(os, i, buf, runparams);
1883 if (endhead.bottomDL) {
1887 os << "\\endhead\n";
1889 if (endfirsthead.empty) {
1890 os << "\\endfirsthead\n";
1894 // output firstheader info
1895 if (haveLTFirstHead()) {
1896 if (endfirsthead.topDL) {
1900 for (row_type i = 0; i < rows_; ++i) {
1901 if (row_info[i].endfirsthead) {
1902 ret += TeXRow(os, i, buf, runparams);
1905 if (endfirsthead.bottomDL) {
1909 os << "\\endfirsthead\n";
1912 // output footer info
1914 if (endfoot.topDL) {
1918 for (row_type i = 0; i < rows_; ++i) {
1919 if (row_info[i].endfoot) {
1920 ret += TeXRow(os, i, buf, runparams);
1923 if (endfoot.bottomDL) {
1927 os << "\\endfoot\n";
1929 if (endlastfoot.empty) {
1930 os << "\\endlastfoot\n";
1934 // output lastfooter info
1935 if (haveLTLastFoot()) {
1936 if (endlastfoot.topDL) {
1940 for (row_type i = 0; i < rows_; ++i) {
1941 if (row_info[i].endlastfoot) {
1942 ret += TeXRow(os, i, buf, runparams);
1945 if (endlastfoot.bottomDL) {
1949 os << "\\endlastfoot\n";
1956 bool LyXTabular::isValidRow(row_type row) const
1958 if (!is_long_tabular)
1960 return !row_info[row].endhead && !row_info[row].endfirsthead &&
1961 !row_info[row].endfoot && !row_info[row].endlastfoot;
1965 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
1966 OutputParams const & runparams) const
1968 idx_type cell = getCellNumber(i, 0);
1970 int ret = TeXTopHLine(os, i);
1971 for (col_type j = 0; j < columns_; ++j) {
1972 if (isPartOfMultiColumn(i, j))
1974 ret += TeXCellPreamble(os, cell);
1975 shared_ptr<InsetText> inset = getCellInset(cell);
1977 Paragraph const & par = inset->paragraphs().front();
1978 bool rtl = par.isRightToLeftPar(buf.params())
1980 && getPWidth(cell).zero();
1984 ret += inset->latex(buf, os, runparams);
1988 ret += TeXCellPostamble(os, cell);
1989 if (!isLastCellInRow(cell)) { // not last cell in row
1995 os << "\\tabularnewline\n";
1997 ret += TeXBottomHLine(os, i);
2002 int LyXTabular::latex(Buffer const & buf, ostream & os,
2003 OutputParams const & runparams) const
2007 //+---------------------------------------------------------------------
2008 //+ first the opening preamble +
2009 //+---------------------------------------------------------------------
2012 os << "\\begin{sideways}\n";
2015 if (is_long_tabular)
2016 os << "\\begin{longtable}{";
2018 os << "\\begin{tabular}{";
2019 for (col_type i = 0; i < columns_; ++i) {
2020 if (!column_info[i].align_special.empty()) {
2021 os << column_info[i].align_special;
2023 if (column_info[i].left_line)
2025 if (!column_info[i].p_width.zero()) {
2026 switch (column_info[i].alignment) {
2027 case LYX_ALIGN_LEFT:
2028 os << ">{\\raggedright}";
2030 case LYX_ALIGN_RIGHT:
2031 os << ">{\\raggedleft}";
2033 case LYX_ALIGN_CENTER:
2034 os << ">{\\centering}";
2036 case LYX_ALIGN_NONE:
2037 case LYX_ALIGN_BLOCK:
2038 case LYX_ALIGN_LAYOUT:
2039 case LYX_ALIGN_SPECIAL:
2043 switch (column_info[i].valignment) {
2044 case LYX_VALIGN_TOP:
2047 case LYX_VALIGN_MIDDLE:
2050 case LYX_VALIGN_BOTTOM:
2055 << column_info[i].p_width.asLatexString()
2058 switch (column_info[i].alignment) {
2059 case LYX_ALIGN_LEFT:
2062 case LYX_ALIGN_RIGHT:
2070 if (column_info[i].right_line)
2077 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2079 //+---------------------------------------------------------------------
2080 //+ the single row and columns (cells) +
2081 //+---------------------------------------------------------------------
2083 for (row_type i = 0; i < rows_; ++i) {
2084 if (isValidRow(i)) {
2085 ret += TeXRow(os, i, buf, runparams);
2086 if (is_long_tabular && row_info[i].newpage) {
2087 os << "\\newpage\n";
2093 //+---------------------------------------------------------------------
2094 //+ the closing of the tabular +
2095 //+---------------------------------------------------------------------
2097 if (is_long_tabular)
2098 os << "\\end{longtable}";
2100 os << "\\end{tabular}";
2102 os << "\n\\end{sideways}";
2110 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2111 const OutputParams & runparams) const
2113 os << "<tabular ca=\"";
2114 for (col_type i = 0; i < columns_; ++i) {
2115 switch (column_info[i].alignment) {
2116 case LYX_ALIGN_LEFT:
2119 case LYX_ALIGN_RIGHT:
2130 for (row_type i = 0; i < rows_; ++i) {
2131 for (col_type j = 0; j < columns_; ++j) {
2132 if (isPartOfMultiColumn(i, j))
2134 shared_ptr<InsetText> inset = getCellInset(cell);
2136 ret += inset->linuxdoc(buf, os, runparams);
2138 if (isLastCellInRow(cell)) {
2147 os << "</tabular>\n";
2152 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2153 OutputParams const & runparams) const
2156 idx_type cell = getFirstCellInRow(row);
2159 for (col_type j = 0; j < columns_; ++j) {
2160 if (isPartOfMultiColumn(row, j))
2163 os << "<entry align=\"";
2164 switch (getAlignment(cell)) {
2165 case LYX_ALIGN_LEFT:
2168 case LYX_ALIGN_RIGHT:
2176 os << "\" valign=\"";
2177 switch (getVAlignment(cell)) {
2178 case LYX_VALIGN_TOP:
2181 case LYX_VALIGN_BOTTOM:
2184 case LYX_VALIGN_MIDDLE:
2189 if (isMultiColumn(cell)) {
2190 os << " namest=\"col" << j << "\" ";
2191 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2195 ret += getCellInset(cell)->docbook(buf, os, runparams);
2204 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2205 OutputParams const & runparams) const
2209 //+---------------------------------------------------------------------
2210 //+ first the opening preamble +
2211 //+---------------------------------------------------------------------
2213 os << "<tgroup cols=\"" << columns_
2214 << "\" colsep=\"1\" rowsep=\"1\">\n";
2216 for (col_type i = 0; i < columns_; ++i) {
2217 os << "<colspec colname=\"col" << i << "\" align=\"";
2218 switch (column_info[i].alignment) {
2219 case LYX_ALIGN_LEFT:
2222 case LYX_ALIGN_RIGHT:
2230 if (runparams.flavor == OutputParams::XML)
2236 //+---------------------------------------------------------------------
2237 //+ Long Tabular case +
2238 //+---------------------------------------------------------------------
2240 // output header info
2241 if (haveLTHead() || haveLTFirstHead()) {
2244 for (row_type i = 0; i < rows_; ++i) {
2245 if (row_info[i].endhead || row_info[i].endfirsthead) {
2246 ret += docbookRow(buf, os, i, runparams);
2252 // output footer info
2253 if (haveLTFoot() || haveLTLastFoot()) {
2256 for (row_type i = 0; i < rows_; ++i) {
2257 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2258 ret += docbookRow(buf, os, i, runparams);
2265 //+---------------------------------------------------------------------
2266 //+ the single row and columns (cells) +
2267 //+---------------------------------------------------------------------
2271 for (row_type i = 0; i < rows_; ++i) {
2272 if (isValidRow(i)) {
2273 ret += docbookRow(buf, os, i, runparams);
2278 //+---------------------------------------------------------------------
2279 //+ the closing of the tabular +
2280 //+---------------------------------------------------------------------
2289 int LyXTabular::asciiTopHLine(ostream & os, row_type row,
2290 vector<unsigned int> const & clen) const
2292 idx_type const fcell = getFirstCellInRow(row);
2293 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2296 for (idx_type i = fcell; i < n; ++i) {
2306 for (idx_type i = fcell; i < n; ++i) {
2317 col_type column = column_of_cell(i);
2318 int len = clen[column];
2319 while (column < columns_ - 1
2320 && isPartOfMultiColumn(row, ++column))
2321 len += clen[column] + 4;
2322 os << string(len, ch);
2337 int LyXTabular::asciiBottomHLine(ostream & os, row_type row,
2338 vector<unsigned int> const & clen) const
2340 idx_type const fcell = getFirstCellInRow(row);
2341 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2344 for (idx_type i = fcell; i < n; ++i) {
2345 if (bottomLine(i)) {
2354 for (idx_type i = fcell; i < n; ++i) {
2355 if (bottomLine(i)) {
2365 col_type column = column_of_cell(i);
2366 int len = clen[column];
2367 while (column < columns_ -1
2368 && isPartOfMultiColumn(row, ++column))
2369 len += clen[column] + 4;
2370 os << string(len, ch);
2371 if (bottomLine(i)) {
2385 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2386 OutputParams const & runparams,
2387 idx_type cell, row_type row, col_type column,
2388 vector<unsigned int> const & clen,
2389 bool onlydata) const
2392 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2404 unsigned int len1 = sstr.str().length();
2405 unsigned int len2 = clen[column];
2406 while (column < columns_ -1
2407 && isPartOfMultiColumn(row, ++column))
2408 len2 += clen[column] + 4;
2411 switch (getAlignment(cell)) {
2413 case LYX_ALIGN_LEFT:
2416 case LYX_ALIGN_RIGHT:
2420 case LYX_ALIGN_CENTER:
2426 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2428 if (rightLine(cell))
2437 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2438 OutputParams const & runparams,
2440 bool onlydata, unsigned char delim) const
2444 // first calculate the width of the single columns
2445 vector<unsigned int> clen(columns_);
2448 // first all non (real) multicolumn cells!
2449 for (col_type j = 0; j < columns_; ++j) {
2451 for (row_type i = 0; i < rows_; ++i) {
2452 idx_type cell = getCellNumber(i, j);
2453 if (isMultiColumnReal(cell))
2456 getCellInset(cell)->plaintext(buf, sstr, runparams);
2457 if (clen[j] < sstr.str().length())
2458 clen[j] = sstr.str().length();
2461 // then all (real) multicolumn cells!
2462 for (col_type j = 0; j < columns_; ++j) {
2463 for (row_type i = 0; i < rows_; ++i) {
2464 idx_type cell = getCellNumber(i, j);
2465 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2468 getCellInset(cell)->plaintext(buf, sstr, runparams);
2469 int len = int(sstr.str().length());
2470 idx_type const n = cells_in_multicolumn(cell);
2471 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2473 if (len > int(clen[j + n - 1]))
2474 clen[j + n - 1] = len;
2479 for (row_type i = 0; i < rows_; ++i) {
2480 if (!onlydata && asciiTopHLine(os, i, clen))
2481 os << string(depth * 2, ' ');
2482 for (col_type j = 0; j < columns_; ++j) {
2483 if (isPartOfMultiColumn(i, j))
2485 if (onlydata && j > 0)
2487 ret += asciiPrintCell(buf, os, runparams,
2488 cell, i, j, clen, onlydata);
2493 os << string(depth * 2, ' ');
2494 if (asciiBottomHLine(os, i, clen))
2495 os << string(depth * 2, ' ');
2502 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2504 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2508 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2509 col_type column) const
2511 return cell_info[row][column].inset;
2515 LyXTabular::idx_type
2516 LyXTabular::getCellFromInset(InsetBase const * inset) const
2518 // is this inset part of the tabular?
2520 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2521 BOOST_ASSERT(false);
2524 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2525 if (getCellInset(cell).get() == inset) {
2526 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2527 << "cell=" << cell << endl;
2531 // We should have found a cell at this point
2532 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2533 << inset << " not found!" << endl;
2534 BOOST_ASSERT(false);
2540 void LyXTabular::validate(LaTeXFeatures & features) const
2542 features.require("NeedTabularnewline");
2543 if (isLongTabular())
2544 features.require("longtable");
2546 features.require("rotating");
2547 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2548 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2549 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2550 features.require("array");
2551 getCellInset(cell)->validate(features);
2556 void LyXTabular::getLabelList(Buffer const & buffer,
2557 std::vector<string> & list) const
2559 for (row_type i = 0; i < rows_; ++i)
2560 for (col_type j = 0; j < columns_; ++j)
2561 getCellInset(i, j)->getLabelList(buffer, list);
2565 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2567 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2568 ParagraphList::const_iterator cit = parlist.begin();
2569 ParagraphList::const_iterator end = parlist.end();
2571 for (; cit != end; ++cit)
2572 for (int i = 0; i < cit->size(); ++i)
2573 if (cit->isNewline(i))