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"
41 using lyx::support::ltrim;
42 using lyx::support::prefixIs;
43 using lyx::support::rtrim;
44 using lyx::support::suffixIs;
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
56 using std::ostringstream;
60 #ifndef CXX_GLOBAL_CSTD
67 int const WIDTH_OF_LINE = 5;
68 int const default_line_space = 10;
72 string const write_attribute(string const & name, T const & t)
74 string const s = tostr(t);
75 return s.empty() ? s : " " + name + "=\"" + s + "\"";
79 string const write_attribute(string const & name, string const & t)
81 return t.empty() ? t : " " + name + "=\"" + t + "\"";
86 string const write_attribute(string const & name, bool const & b)
88 // we write only true attribute values so we remove a bit of the
89 // file format bloat for tabulars.
90 return b ? write_attribute(name, convert<string>(b)) : string();
95 string const write_attribute(string const & name, int const & i)
97 // we write only true attribute values so we remove a bit of the
98 // file format bloat for tabulars.
99 return i ? write_attribute(name, convert<string>(i)) : string();
104 string const write_attribute(string const & name, LyXTabular::idx_type const & i)
106 // we write only true attribute values so we remove a bit of the
107 // file format bloat for tabulars.
108 return i ? write_attribute(name, convert<string>(i)) : string();
113 string const write_attribute(string const & name, LyXLength const & value)
115 // we write only the value if we really have one same reson as above.
116 return value.zero() ? string() : write_attribute(name, value.asString());
120 string const tostr(LyXAlignment const & num)
125 case LYX_ALIGN_BLOCK:
129 case LYX_ALIGN_CENTER:
131 case LYX_ALIGN_RIGHT:
133 case LYX_ALIGN_LAYOUT:
135 case LYX_ALIGN_SPECIAL:
142 string const tostr(LyXTabular::VAlignment const & num)
145 case LyXTabular::LYX_VALIGN_TOP:
147 case LyXTabular::LYX_VALIGN_MIDDLE:
149 case LyXTabular::LYX_VALIGN_BOTTOM:
156 string const tostr(LyXTabular::BoxType const & num)
159 case LyXTabular::BOX_NONE:
161 case LyXTabular::BOX_PARBOX:
163 case LyXTabular::BOX_MINIPAGE:
170 // I would have liked a fromstr template a lot better. (Lgb)
171 bool string2type(string const str, LyXAlignment & num)
174 num = LYX_ALIGN_NONE;
175 else if (str == "block")
176 num = LYX_ALIGN_BLOCK;
177 else if (str == "left")
178 num = LYX_ALIGN_LEFT;
179 else if (str == "center")
180 num = LYX_ALIGN_CENTER;
181 else if (str == "right")
182 num = LYX_ALIGN_RIGHT;
189 bool string2type(string const str, LyXTabular::VAlignment & num)
192 num = LyXTabular::LYX_VALIGN_TOP;
193 else if (str == "middle" )
194 num = LyXTabular::LYX_VALIGN_MIDDLE;
195 else if (str == "bottom")
196 num = LyXTabular::LYX_VALIGN_BOTTOM;
203 bool string2type(string const str, LyXTabular::BoxType & num)
206 num = LyXTabular::BOX_NONE;
207 else if (str == "parbox")
208 num = LyXTabular::BOX_PARBOX;
209 else if (str == "minipage")
210 num = LyXTabular::BOX_MINIPAGE;
217 bool string2type(string const str, bool & num)
221 else if (str == "false")
229 bool getTokenValue(string const & str, char const * token, string & ret)
232 size_t token_length = strlen(token);
233 string::size_type pos = str.find(token);
235 if (pos == string::npos || pos + token_length + 1 >= str.length()
236 || str[pos + token_length] != '=')
238 pos += token_length + 1;
240 if (ch != '"' && ch != '\'') { // only read till next space
244 while (pos < str.length() - 1 && str[++pos] != ch)
251 bool getTokenValue(string const & str, char const * token, int & num)
255 if (!getTokenValue(str, token, tmp))
257 num = convert<int>(tmp);
262 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
265 return getTokenValue(str, token, tmp) && string2type(tmp, num);
269 bool getTokenValue(string const & str, char const * token,
270 LyXTabular::VAlignment & num)
273 return getTokenValue(str, token, tmp) && string2type(tmp, num);
277 bool getTokenValue(string const & str, char const * token,
278 LyXTabular::BoxType & num)
281 return getTokenValue(str, token, tmp) && string2type(tmp, num);
285 bool getTokenValue(string const & str, char const * token, bool & flag)
287 // set the flag always to false as this should be the default for bools
288 // not in the file-format.
291 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
295 bool getTokenValue(string const & str, char const * token, LyXLength & len)
297 // set the lenght to be zero() as default as this it should be if not
298 // in the file format.
301 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
305 bool getTokenValue(string const & str, char const * token, LyXLength & len, bool & flag)
310 if (!getTokenValue(str, token, tmp))
312 if (tmp == "default") {
316 return isValidLength(tmp, &len);
320 void l_getline(istream & is, string & str)
323 while (str.empty()) {
325 if (!str.empty() && str[str.length() - 1] == '\r')
326 str.erase(str.length() - 1);
332 /// Define a few methods for the inner structs
334 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
337 multicolumn(LyXTabular::CELL_NORMAL),
338 alignment(LYX_ALIGN_CENTER),
339 valignment(LYX_VALIGN_TOP),
346 inset(new InsetText(bp))
350 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
352 width_of_cell(cs.width_of_cell),
353 multicolumn(cs.multicolumn),
354 alignment(cs.alignment),
355 valignment(cs.valignment),
356 top_line(cs.top_line),
357 bottom_line(cs.bottom_line),
358 left_line(cs.left_line),
359 right_line(cs.right_line),
362 align_special(cs.align_special),
364 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
368 LyXTabular::cellstruct &
369 LyXTabular::cellstruct::operator=(cellstruct cs)
377 LyXTabular::cellstruct::swap(cellstruct & rhs)
379 std::swap(cellno, rhs.cellno);
380 std::swap(width_of_cell, rhs.width_of_cell);
381 std::swap(multicolumn, rhs.multicolumn);
382 std::swap(alignment, rhs.alignment);
383 std::swap(valignment, rhs.valignment);
384 std::swap(top_line, rhs.top_line);
385 std::swap(bottom_line, rhs.bottom_line);
386 std::swap(left_line, rhs.left_line);
387 std::swap(right_line, rhs.right_line);
388 std::swap(usebox, rhs.usebox);
389 std::swap(rotate, rhs.rotate);
390 std::swap(align_special, rhs.align_special);
391 p_width.swap(rhs.p_width);
392 inset.swap(rhs.inset);
396 LyXTabular::rowstruct::rowstruct()
401 top_space_default(false),
402 bottom_space_default(false),
403 interline_space_default(false),
412 LyXTabular::columnstruct::columnstruct()
413 : alignment(LYX_ALIGN_CENTER),
414 valignment(LYX_VALIGN_TOP),
422 LyXTabular::ltType::ltType()
429 LyXTabular::LyXTabular(BufferParams const & bp, row_type rows_arg,
430 col_type columns_arg)
432 init(bp, rows_arg, columns_arg);
436 // activates all lines and sets all widths to 0
437 void LyXTabular::init(BufferParams const & bp, row_type rows_arg,
438 col_type columns_arg)
441 columns_ = columns_arg;
442 row_info = row_vector(rows_);
443 column_info = column_vector(columns_);
444 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
445 row_info.reserve(10);
446 column_info.reserve(10);
447 cell_info.reserve(100);
449 for (row_type i = 0; i < rows_; ++i)
450 cell_info[i].back().right_line = true;
451 row_info.back().bottom_line = true;
452 row_info.front().bottom_line = true;
453 column_info.back().right_line = true;
454 is_long_tabular = false;
456 use_booktabs = false;
460 void LyXTabular::fixCellNums()
463 for (row_type i = 0; i < rows_; ++i) {
464 for (col_type j = 0; j < columns_; ++j) {
465 // When debugging it can be nice to set
467 cell_info[i][j].inset->setDrawFrame(false);
468 cell_info[i][j].cellno = cellno++;
470 cell_info[i].back().right_line = true;
473 set_row_column_number_info();
477 void LyXTabular::appendRow(BufferParams const & bp, idx_type const cell)
481 row_type const row = row_of_cell(cell);
483 row_vector::iterator rit = row_info.begin() + row;
484 row_info.insert(rit, rowstruct());
485 // now set the values of the row before
486 row_info[row] = row_info[row + 1];
488 cell_vvector old(rows_ - 1);
489 for (row_type i = 0; i < rows_ - 1; ++i)
490 swap(cell_info[i], old[i]);
492 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
494 for (row_type i = 0; i <= row; ++i)
495 swap(cell_info[i], old[i]);
496 for (row_type i = row + 2; i < rows_; ++i)
497 swap(cell_info[i], old[i - 1]);
500 // FIXME: Change Tracking (MG)
501 for (col_type j = 0; j < columns_; ++j)
502 cell_info[row + 1][j].inset->markNew(true);
504 set_row_column_number_info();
508 void LyXTabular::deleteRow(row_type const row)
510 // Not allowed to delete last row
514 row_info.erase(row_info.begin() + row);
515 cell_info.erase(cell_info.begin() + row);
521 void LyXTabular::copyRow(BufferParams const & bp, row_type const row)
525 row_info.insert(row_info.begin() + row, row_info[row]);
526 cell_info.insert(cell_info.begin() + row, cell_info[row]);
529 // FIXME: Change Tracking (MG)
530 for (col_type j = 0; j < columns_; ++j)
531 cell_info[row + 1][j].inset->markNew(true);
533 set_row_column_number_info();
537 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
541 col_type const column = column_of_cell(cell);
542 column_vector::iterator cit = column_info.begin() + column + 1;
543 column_info.insert(cit, columnstruct());
544 // set the column values of the column before
545 column_info[column + 1] = column_info[column];
547 for (row_type i = 0; i < rows_; ++i) {
548 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
550 // care about multicolumns
551 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
552 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
554 if (column + 2 >= columns_
555 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
556 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
559 for (row_type i = 0; i < rows_; ++i) {
560 cell_info[i][column + 1].inset->clear();
562 // FIXME: Change Tracking (MG)
563 cell_info[i][column + 1].inset->markNew(true);
569 void LyXTabular::deleteColumn(col_type const column)
571 // Not allowed to delete last column
575 column_info.erase(column_info.begin() + column);
576 for (row_type i = 0; i < rows_; ++i)
577 cell_info[i].erase(cell_info[i].begin() + column);
583 void LyXTabular::copyColumn(BufferParams const & bp, col_type const column)
587 column_info.insert(column_info.begin() + column, column_info[column]);
589 for (row_type i = 0; i < rows_; ++i)
590 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
593 // FIXME: Change Tracking (MG)
594 for (row_type i = 0; i < rows_; ++i)
595 cell_info[i][column + 1].inset->markNew(true);
600 void LyXTabular::set_row_column_number_info()
603 for (row_type row = 0; row < rows_; ++row) {
604 for (col_type column = 0; column < columns_; ++column) {
605 if (cell_info[row][column].multicolumn
606 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
608 if (numberofcells == 0)
609 // FIXME: Is this intended?
610 cell_info[row][column].cellno = npos;
612 cell_info[row][column].cellno =
617 rowofcell.resize(numberofcells);
618 columnofcell.resize(numberofcells);
623 c < numberofcells && row < rows_ && column < columns_;) {
625 columnofcell[c] = column;
629 } while (column < columns_ &&
630 cell_info[row][column].multicolumn
631 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
633 if (column == columns_) {
639 for (row_type row = 0; row < rows_; ++row) {
640 for (col_type column = 0; column < columns_; ++column) {
641 if (isPartOfMultiColumn(row,column))
643 cell_info[row][column].inset->setAutoBreakRows(
644 !getPWidth(getCellNumber(row, column)).zero());
650 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
652 return numberofcells;
656 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
658 row_type const row = row_of_cell(cell);
660 for (col_type i = 0; i < columns_; ++i)
661 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
667 bool LyXTabular::topLine(idx_type const cell, bool const onlycolumn) const
669 if (!onlycolumn && isMultiColumn(cell) &&
670 !(use_booktabs && row_of_cell(cell) == 0))
671 return cellinfo_of_cell(cell).top_line;
672 return row_info[row_of_cell(cell)].top_line;
676 bool LyXTabular::bottomLine(idx_type const cell, bool onlycolumn) const
678 if (!onlycolumn && isMultiColumn(cell) &&
679 !(use_booktabs && isLastRow(cell)))
680 return cellinfo_of_cell(cell).bottom_line;
681 return row_info[row_of_cell(cell)].bottom_line;
685 bool LyXTabular::leftLine(idx_type cell, bool onlycolumn) const
689 if (!onlycolumn && isMultiColumn(cell) &&
690 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
692 if (cellinfo_of_cell(cell).align_special.empty())
693 return cellinfo_of_cell(cell).left_line;
694 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
696 if (column_info[column_of_cell(cell)].align_special.empty())
697 return column_info[column_of_cell(cell)].left_line;
698 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
702 bool LyXTabular::rightLine(idx_type cell, bool onlycolumn) const
706 if (!onlycolumn && isMultiColumn(cell) &&
707 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
709 if (cellinfo_of_cell(cell).align_special.empty())
710 return cellinfo_of_cell(cell).right_line;
711 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
713 if (column_info[column_of_cell(cell)].align_special.empty())
714 return column_info[right_column_of_cell(cell)].right_line;
715 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
719 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
721 row_type row = row_of_cell(cell);
722 if (row > 0 && !getAdditionalHeight(row)) {
723 col_type column = column_of_cell(cell);
726 && cell_info[row][column].multicolumn
727 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
729 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
730 return row_info[row].bottom_line;
732 return cell_info[row][column].bottom_line;
738 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
740 col_type column = column_of_cell(cell);
742 row_type row = row_of_cell(cell);
744 (cell_info[row][column].multicolumn ==
745 LyXTabular::CELL_PART_OF_MULTICOLUMN));
746 if (getAdditionalWidth(cell_info[row][column].cellno))
748 return rightLine(cell_info[row][column].cellno);
754 bool LyXTabular::isLastRow(idx_type cell) const
756 return row_of_cell(cell) == rows_ - 1;
760 int LyXTabular::getAdditionalHeight(row_type row) const
762 if (!row || row >= rows_)
768 for (col_type column = 0; column < columns_ && bottom; ++column) {
769 switch (cell_info[row - 1][column].multicolumn) {
770 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
771 bottom = cell_info[row - 1][column].bottom_line;
773 case LyXTabular::CELL_NORMAL:
774 bottom = row_info[row - 1].bottom_line;
777 for (col_type column = 0; column < columns_ && top; ++column) {
778 switch (cell_info[row][column].multicolumn) {
779 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
780 top = cell_info[row][column].top_line;
782 case LyXTabular::CELL_NORMAL:
783 top = row_info[row].top_line;
786 int const interline_space = row_info[row - 1].interline_space_default ?
788 row_info[row - 1].interline_space.inPixels(width_of_tabular);
790 return interline_space + WIDTH_OF_LINE;
791 return interline_space;
795 int LyXTabular::getAdditionalWidth(idx_type cell) const
797 // internally already set in setWidthOfCell
798 // used to get it back in text.C
799 col_type const col = right_column_of_cell(cell);
800 row_type const row = row_of_cell(cell);
801 if (col < columns_ - 1 && rightLine(cell) &&
802 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
804 return WIDTH_OF_LINE;
810 // returns the maximum over all rows
811 int LyXTabular::getWidthOfColumn(idx_type cell) const
813 col_type const column1 = column_of_cell(cell);
814 col_type const column2 = right_column_of_cell(cell);
816 for (col_type i = column1; i <= column2; ++i)
817 result += column_info[i].width_of_column;
822 int LyXTabular::getWidthOfTabular() const
824 return width_of_tabular;
828 // returns true if a complete update is necessary, otherwise false
829 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
831 if (!isMultiColumn(cell))
834 row_type const row = row_of_cell(cell);
835 col_type const column1 = column_of_cell(cell);
836 col_type const column2 = right_column_of_cell(cell);
837 int const old_val = cell_info[row][column2].width_of_cell;
839 // first set columns to 0 so we can calculate the right width
840 for (col_type i = column1; i <= column2; ++i) {
841 cell_info[row][i].width_of_cell = 0;
843 // set the width to MAX_WIDTH until width > 0
844 int width = new_width + 2 * WIDTH_OF_LINE;
845 col_type i = column1;
846 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
847 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
848 width -= column_info[i].width_of_column;
851 cell_info[row][i].width_of_cell = width;
853 if (old_val != cell_info[row][column2].width_of_cell) {
854 // in this case we have to recalculate all multicolumn cells which
855 // have this column as one of theirs but not as last one
856 calculate_width_of_column_NMC(i);
857 recalculateMulticolumnsOfColumn(i);
858 calculate_width_of_column(i);
864 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
866 // the last column does not have to be recalculated because all
867 // multicolumns will have here there last multicolumn cell which
868 // always will have the whole rest of the width of the cell.
869 if (columns_ < 2 || column > (columns_ - 2))
871 for (row_type row = 0; row < rows_; ++row) {
872 int mc = cell_info[row][column].multicolumn;
873 int nmc = cell_info[row][column+1].multicolumn;
874 // we only have to update multicolumns which do not have this
875 // column as their last column!
876 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
877 (mc == CELL_PART_OF_MULTICOLUMN &&
878 nmc == CELL_PART_OF_MULTICOLUMN))
880 idx_type const cellno = cell_info[row][column].cellno;
881 setWidthOfMulticolCell(cellno,
882 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
888 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
890 row_type const row = row_of_cell(cell);
891 col_type const column1 = column_of_cell(cell);
896 if (rightLine(cell_info[row][column1].cellno, true) &&
897 column1 < columns_ - 1 &&
898 leftLine(cell_info[row][column1+1].cellno, true))
900 add_width = WIDTH_OF_LINE;
903 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
906 if (isMultiColumnReal(cell)) {
907 tmp = setWidthOfMulticolCell(cell, new_width);
909 width = new_width + 2 * WIDTH_OF_LINE + add_width;
910 cell_info[row][column1].width_of_cell = width;
911 tmp = calculate_width_of_column_NMC(column1);
913 recalculateMulticolumnsOfColumn(column1);
916 for (col_type i = 0; i < columns_; ++i)
917 calculate_width_of_column(i);
918 calculate_width_of_tabular();
923 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
926 if (!isMultiColumn(cell) || onlycolumn)
927 column_info[column_of_cell(cell)].alignment = align;
929 cellinfo_of_cell(cell).alignment = align;
933 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
936 if (!isMultiColumn(cell) || onlycolumn)
937 column_info[column_of_cell(cell)].valignment = align;
939 cellinfo_of_cell(cell).valignment = align;
946 * Allow line and paragraph breaks for fixed width cells or disallow them,
947 * merge cell paragraphs and reset layout to standard for variable width
950 void toggleFixedWidth(LCursor & cur, InsetText * inset, bool fixedWidth)
952 inset->setAutoBreakRows(fixedWidth);
956 // merge all paragraphs to one
957 BufferParams const & bp =
958 inset->getText(0)->bv_owner->buffer()->params();
959 while (inset->paragraphs().size() > 1)
960 mergeParagraph(bp, inset->paragraphs(), 0);
964 // undo information has already been recorded
965 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
966 bp.getLyXTextClass().defaultLayoutName());
973 void LyXTabular::setColumnPWidth(LCursor & cur, idx_type cell,
974 LyXLength const & width)
976 col_type const j = column_of_cell(cell);
978 column_info[j].p_width = width;
979 for (row_type i = 0; i < rows_; ++i) {
980 idx_type const cell = getCellNumber(i, j);
981 // because of multicolumns
982 toggleFixedWidth(cur, getCellInset(cell).get(),
983 !getPWidth(cell).zero());
985 // cur paragraph can become invalid after paragraphs were merged
986 if (cur.pit() > cur.lastpit())
987 cur.pit() = cur.lastpit();
988 // cur position can become invalid after newlines were removed
989 if (cur.pos() > cur.lastpos())
990 cur.pos() = cur.lastpos();
994 bool LyXTabular::setMColumnPWidth(LCursor & cur, idx_type cell,
995 LyXLength const & width)
997 if (!isMultiColumn(cell))
1000 cellinfo_of_cell(cell).p_width = width;
1001 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1002 // cur paragraph can become invalid after paragraphs were merged
1003 if (cur.pit() > cur.lastpit())
1004 cur.pit() = cur.lastpit();
1005 // cur position can become invalid after newlines were removed
1006 if (cur.pos() > cur.lastpos())
1007 cur.pos() = cur.lastpos();
1012 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
1013 LyXTabular::Feature what)
1015 if (what == SET_SPECIAL_MULTI)
1016 cellinfo_of_cell(cell).align_special = special;
1018 column_info[column_of_cell(cell)].align_special = special;
1022 void LyXTabular::setAllLines(idx_type cell, bool line)
1024 setTopLine(cell, line);
1025 setBottomLine(cell, line);
1026 setRightLine(cell, line);
1027 setLeftLine(cell, line);
1031 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
1033 row_type const row = row_of_cell(cell);
1034 if (onlycolumn || !isMultiColumn(cell))
1035 row_info[row].top_line = line;
1037 cellinfo_of_cell(cell).top_line = line;
1041 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
1043 if (onlycolumn || !isMultiColumn(cell))
1044 row_info[row_of_cell(cell)].bottom_line = line;
1046 cellinfo_of_cell(cell).bottom_line = line;
1050 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
1052 if (onlycolumn || !isMultiColumn(cell))
1053 column_info[column_of_cell(cell)].left_line = line;
1055 cellinfo_of_cell(cell).left_line = line;
1059 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
1061 if (onlycolumn || !isMultiColumn(cell))
1062 column_info[right_column_of_cell(cell)].right_line = line;
1064 cellinfo_of_cell(cell).right_line = line;
1068 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
1070 if (!onlycolumn && isMultiColumn(cell))
1071 return cellinfo_of_cell(cell).alignment;
1072 return column_info[column_of_cell(cell)].alignment;
1076 LyXTabular::VAlignment
1077 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
1079 if (!onlycolumn && isMultiColumn(cell))
1080 return cellinfo_of_cell(cell).valignment;
1081 return column_info[column_of_cell(cell)].valignment;
1085 LyXLength const LyXTabular::getPWidth(idx_type cell) const
1087 if (isMultiColumn(cell))
1088 return cellinfo_of_cell(cell).p_width;
1089 return column_info[column_of_cell(cell)].p_width;
1093 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
1095 return column_info[column_of_cell(cell)].p_width;
1099 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
1101 if (isMultiColumn(cell))
1102 return cellinfo_of_cell(cell).p_width;
1107 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
1109 if (what == SET_SPECIAL_MULTI)
1110 return cellinfo_of_cell(cell).align_special;
1111 return column_info[column_of_cell(cell)].align_special;
1115 int LyXTabular::getWidthOfCell(idx_type cell) const
1117 row_type const row = row_of_cell(cell);
1118 col_type const column1 = column_of_cell(cell);
1119 col_type const column2 = right_column_of_cell(cell);
1121 for (col_type i = column1; i <= column2; ++i)
1122 result += cell_info[row][i].width_of_cell;
1127 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1131 switch (getAlignment(cell)) {
1132 case LYX_ALIGN_CENTER:
1133 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1135 case LYX_ALIGN_RIGHT:
1136 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1137 // + getAdditionalWidth(cell);
1140 // LYX_ALIGN_LEFT: nothing :-)
1144 // the LaTeX Way :-(
1150 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1152 return column_of_cell(cell) == 0;
1156 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1158 if (row > rows_ - 1)
1160 return cell_info[row][0].cellno;
1164 bool LyXTabular::isLastCellInRow(idx_type cell) const
1166 return right_column_of_cell(cell) == columns_ - 1;
1170 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1172 if (row > rows_ - 1)
1174 return cell_info[row][columns_-1].cellno;
1178 void LyXTabular::calculate_width_of_column(col_type column)
1181 for (row_type i = 0; i < rows_; ++i)
1182 maximum = max(cell_info[i][column].width_of_cell, maximum);
1183 column_info[column].width_of_column = maximum;
1188 // Calculate the columns regarding ONLY the normal cells and if this
1189 // column is inside a multicolumn cell then use it only if its the last
1190 // column of this multicolumn cell as this gives an added width to the
1191 // column, all the rest should be adapted!
1193 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1195 int const old_column_width = column_info[column].width_of_column;
1197 for (row_type i = 0; i < rows_; ++i) {
1198 idx_type cell = getCellNumber(i, column);
1199 bool ismulti = isMultiColumnReal(cell);
1200 if ((!ismulti || column == right_column_of_cell(cell)) &&
1201 cell_info[i][column].width_of_cell > max)
1203 max = cell_info[i][column].width_of_cell;
1206 column_info[column].width_of_column = max;
1207 return column_info[column].width_of_column != old_column_width;
1211 void LyXTabular::calculate_width_of_tabular()
1213 width_of_tabular = 0;
1214 for (col_type i = 0; i < columns_; ++i)
1215 width_of_tabular += column_info[i].width_of_column;
1219 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1221 if (cell >= numberofcells)
1225 return rowofcell[cell];
1229 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1231 if (cell >= numberofcells)
1232 return columns_ - 1;
1235 return columnofcell[cell];
1239 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1241 row_type const row = row_of_cell(cell);
1242 col_type column = column_of_cell(cell);
1243 while (column < columns_ - 1 &&
1244 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1250 void LyXTabular::write(Buffer const & buf, ostream & os) const
1254 << write_attribute("version", 3)
1255 << write_attribute("rows", rows_)
1256 << write_attribute("columns", columns_)
1258 // global longtable options
1260 << write_attribute("rotate", rotate)
1261 << write_attribute("booktabs", use_booktabs)
1262 << write_attribute("islongtable", is_long_tabular)
1263 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1264 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1265 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1266 << write_attribute("headTopDL", endhead.topDL)
1267 << write_attribute("headBottomDL", endhead.bottomDL)
1268 << write_attribute("footTopDL", endfoot.topDL)
1269 << write_attribute("footBottomDL", endfoot.bottomDL)
1270 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1271 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1272 << write_attribute("lastFootEmpty", endlastfoot.empty)
1274 for (col_type j = 0; j < columns_; ++j) {
1276 << write_attribute("alignment", column_info[j].alignment)
1277 << write_attribute("valignment", column_info[j].valignment)
1278 << write_attribute("leftline", column_info[j].left_line)
1279 << write_attribute("rightline", column_info[j].right_line)
1280 << write_attribute("width", column_info[j].p_width.asString())
1281 << write_attribute("special", column_info[j].align_special)
1284 for (row_type i = 0; i < rows_; ++i) {
1286 << write_attribute("topline", row_info[i].top_line)
1287 << write_attribute("bottomline", row_info[i].bottom_line);
1288 static const string def("default");
1289 if (row_info[i].top_space_default)
1290 os << write_attribute("topspace", def);
1292 os << write_attribute("topspace", row_info[i].top_space);
1293 if (row_info[i].bottom_space_default)
1294 os << write_attribute("bottomspace", def);
1296 os << write_attribute("bottomspace", row_info[i].bottom_space);
1297 if (row_info[i].interline_space_default)
1298 os << write_attribute("interlinespace", def);
1300 os << write_attribute("interlinespace", row_info[i].interline_space);
1301 os << write_attribute("endhead", row_info[i].endhead)
1302 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1303 << write_attribute("endfoot", row_info[i].endfoot)
1304 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1305 << write_attribute("newpage", row_info[i].newpage)
1307 for (col_type j = 0; j < columns_; ++j) {
1309 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1310 << write_attribute("alignment", cell_info[i][j].alignment)
1311 << write_attribute("valignment", cell_info[i][j].valignment)
1312 << write_attribute("topline", cell_info[i][j].top_line)
1313 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1314 << write_attribute("leftline", cell_info[i][j].left_line)
1315 << write_attribute("rightline", cell_info[i][j].right_line)
1316 << write_attribute("rotate", cell_info[i][j].rotate)
1317 << write_attribute("usebox", cell_info[i][j].usebox)
1318 << write_attribute("width", cell_info[i][j].p_width)
1319 << write_attribute("special", cell_info[i][j].align_special)
1321 os << "\\begin_inset ";
1322 cell_info[i][j].inset->write(buf, os);
1323 os << "\n\\end_inset\n"
1328 os << "</lyxtabular>\n";
1332 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1335 istream & is = lex.getStream();
1337 l_getline(is, line);
1338 if (!prefixIs(line, "<lyxtabular ")
1339 && !prefixIs(line, "<LyXTabular ")) {
1340 BOOST_ASSERT(false);
1345 if (!getTokenValue(line, "version", version))
1347 BOOST_ASSERT(version >= 2);
1350 if (!getTokenValue(line, "rows", rows_arg))
1353 if (!getTokenValue(line, "columns", columns_arg))
1355 init(buf.params(), rows_arg, columns_arg);
1356 l_getline(is, line);
1357 if (!prefixIs(line, "<features")) {
1358 lyxerr << "Wrong tabular format (expected <features ...> got"
1359 << line << ')' << endl;
1362 getTokenValue(line, "rotate", rotate);
1363 getTokenValue(line, "booktabs", use_booktabs);
1364 getTokenValue(line, "islongtable", is_long_tabular);
1365 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1366 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1367 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1368 getTokenValue(line, "headTopDL", endhead.topDL);
1369 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1370 getTokenValue(line, "footTopDL", endfoot.topDL);
1371 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1372 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1373 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1374 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1376 for (col_type j = 0; j < columns_; ++j) {
1378 if (!prefixIs(line,"<column")) {
1379 lyxerr << "Wrong tabular format (expected <column ...> got"
1380 << line << ')' << endl;
1383 getTokenValue(line, "alignment", column_info[j].alignment);
1384 getTokenValue(line, "valignment", column_info[j].valignment);
1385 getTokenValue(line, "leftline", column_info[j].left_line);
1386 getTokenValue(line, "rightline", column_info[j].right_line);
1387 getTokenValue(line, "width", column_info[j].p_width);
1388 getTokenValue(line, "special", column_info[j].align_special);
1391 for (row_type i = 0; i < rows_; ++i) {
1392 l_getline(is, line);
1393 if (!prefixIs(line, "<row")) {
1394 lyxerr << "Wrong tabular format (expected <row ...> got"
1395 << line << ')' << endl;
1398 getTokenValue(line, "topline", row_info[i].top_line);
1399 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1400 getTokenValue(line, "topspace", row_info[i].top_space,
1401 row_info[i].top_space_default);
1402 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1403 row_info[i].bottom_space_default);
1404 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1405 row_info[i].interline_space_default);
1406 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1407 getTokenValue(line, "endhead", row_info[i].endhead);
1408 getTokenValue(line, "endfoot", row_info[i].endfoot);
1409 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1410 getTokenValue(line, "newpage", row_info[i].newpage);
1411 for (col_type j = 0; j < columns_; ++j) {
1412 l_getline(is, line);
1413 if (!prefixIs(line, "<cell")) {
1414 lyxerr << "Wrong tabular format (expected <cell ...> got"
1415 << line << ')' << endl;
1418 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1419 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1420 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1421 getTokenValue(line, "topline", cell_info[i][j].top_line);
1422 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1423 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1424 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1425 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1426 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1427 getTokenValue(line, "width", cell_info[i][j].p_width);
1428 getTokenValue(line, "special", cell_info[i][j].align_special);
1429 l_getline(is, line);
1430 if (prefixIs(line, "\\begin_inset")) {
1431 cell_info[i][j].inset->read(buf, lex);
1432 l_getline(is, line);
1434 if (!prefixIs(line, "</cell>")) {
1435 lyxerr << "Wrong tabular format (expected </cell> got"
1436 << line << ')' << endl;
1440 l_getline(is, line);
1441 if (!prefixIs(line, "</row>")) {
1442 lyxerr << "Wrong tabular format (expected </row> got"
1443 << line << ')' << endl;
1447 while (!prefixIs(line, "</lyxtabular>")) {
1448 l_getline(is, line);
1450 set_row_column_number_info();
1454 bool LyXTabular::isMultiColumn(idx_type cell) const
1456 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1460 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1462 return column_of_cell(cell) != right_column_of_cell(cell) &&
1463 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1467 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1469 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1473 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1476 cellstruct & cs = cellinfo_of_cell(cell);
1477 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1478 cs.alignment = column_info[column_of_cell(cell)].alignment;
1479 cs.top_line = row_info[row_of_cell(cell)].top_line;
1480 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1481 cs.left_line = column_info[column_of_cell(cell)].left_line;
1482 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1483 for (idx_type i = 1; i < number; ++i) {
1484 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1485 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1486 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1489 set_row_column_number_info();
1493 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1495 row_type const row = row_of_cell(cell);
1496 col_type column = column_of_cell(cell);
1497 idx_type result = 1;
1499 while (column < columns_ &&
1500 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1509 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1511 row_type const row = row_of_cell(cell);
1512 col_type column = column_of_cell(cell);
1514 idx_type result = 0;
1516 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1517 cell_info[row][column].multicolumn = CELL_NORMAL;
1519 while (column < columns_ &&
1520 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1522 cell_info[row][column].multicolumn = CELL_NORMAL;
1527 set_row_column_number_info();
1532 void LyXTabular::setBookTabs(bool what)
1534 use_booktabs = what;
1538 bool LyXTabular::useBookTabs() const
1540 return use_booktabs;
1544 void LyXTabular::setLongTabular(bool what)
1546 is_long_tabular = what;
1550 bool LyXTabular::isLongTabular() const
1552 return is_long_tabular;
1556 void LyXTabular::setRotateTabular(bool flag)
1562 bool LyXTabular::getRotateTabular() const
1568 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1570 cellinfo_of_cell(cell).rotate = flag;
1574 bool LyXTabular::getRotateCell(idx_type cell) const
1576 return cellinfo_of_cell(cell).rotate;
1580 bool LyXTabular::needRotating() const
1584 for (row_type i = 0; i < rows_; ++i)
1585 for (col_type j = 0; j < columns_; ++j)
1586 if (cell_info[i][j].rotate)
1592 bool LyXTabular::isLastCell(idx_type cell) const
1594 if (cell + 1 < numberofcells)
1600 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1602 if (row_of_cell(cell) > 0)
1603 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1608 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1610 if (row_of_cell(cell) + 1 < rows_)
1611 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1616 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1618 if (row_of_cell(cell) == 0)
1620 if (!isMultiColumn(cell))
1621 return getCellAbove(cell);
1622 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1626 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1628 if (row_of_cell(cell) + 1 >= rows_)
1630 if (!isMultiColumn(cell))
1631 return getCellBelow(cell);
1632 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1636 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1637 col_type column) const
1639 BOOST_ASSERT(column != npos && column < columns_ &&
1640 row != npos && row < rows_);
1641 return cell_info[row][column].cellno;
1645 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1647 cellinfo_of_cell(cell).usebox = type;
1651 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1653 if (column_info[column_of_cell(cell)].p_width.zero() &&
1654 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1656 if (cellinfo_of_cell(cell).usebox > 1)
1657 return cellinfo_of_cell(cell).usebox;
1658 return useParbox(cell);
1663 // This are functions used for the longtable support
1665 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1671 row_info[row].endfirsthead = flag;
1675 row_info[row].endhead = flag;
1680 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1683 hd.set = haveLTHead();
1684 return row_info[row].endhead;
1688 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1691 hd.set = haveLTFirstHead();
1692 return row_info[row].endfirsthead;
1696 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1702 row_info[row].endlastfoot = flag;
1706 row_info[row].endfoot = flag;
1711 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1714 fd.set = haveLTFoot();
1715 return row_info[row].endfoot;
1719 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1722 fd.set = haveLTLastFoot();
1723 return row_info[row].endlastfoot;
1727 void LyXTabular::setLTNewPage(row_type row, bool what)
1729 row_info[row].newpage = what;
1733 bool LyXTabular::getLTNewPage(row_type row) const
1735 return row_info[row].newpage;
1739 bool LyXTabular::haveLTHead() const
1741 for (row_type i = 0; i < rows_; ++i)
1742 if (row_info[i].endhead)
1748 bool LyXTabular::haveLTFirstHead() const
1750 if (endfirsthead.empty)
1752 for (row_type i = 0; i < rows_; ++i)
1753 if (row_info[i].endfirsthead)
1759 bool LyXTabular::haveLTFoot() const
1761 for (row_type i = 0; i < rows_; ++i)
1762 if (row_info[i].endfoot)
1768 bool LyXTabular::haveLTLastFoot() const
1770 if (endlastfoot.empty)
1772 for (row_type i = 0; i < rows_; ++i)
1773 if (row_info[i].endlastfoot)
1779 // end longtable support functions
1781 void LyXTabular::setAscentOfRow(row_type row, int height)
1783 if (row >= rows_ || row_info[row].ascent_of_row == height)
1785 row_info[row].ascent_of_row = height;
1789 void LyXTabular::setDescentOfRow(row_type row, int height)
1791 if (row >= rows_ || row_info[row].descent_of_row == height)
1793 row_info[row].descent_of_row = height;
1797 int LyXTabular::getAscentOfRow(row_type row) const
1801 return row_info[row].ascent_of_row;
1805 int LyXTabular::getDescentOfRow(row_type row) const
1807 BOOST_ASSERT(row < rows_);
1808 return row_info[row].descent_of_row;
1812 int LyXTabular::getHeightOfTabular() const
1815 for (row_type row = 0; row < rows_; ++row)
1816 height += getAscentOfRow(row) + getDescentOfRow(row) +
1817 getAdditionalHeight(row);
1822 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1824 BOOST_ASSERT(row < rows_);
1825 BOOST_ASSERT(column < columns_);
1826 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1830 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1832 // FIXME: assert or return 0 as in TeXBottomHLine()?
1833 BOOST_ASSERT(row != npos);
1834 BOOST_ASSERT(row < rows_);
1836 idx_type const fcell = getFirstCellInRow(row);
1837 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1840 for (idx_type i = fcell; i < n; ++i) {
1844 if (use_booktabs && row == 0) {
1846 } else if (tmp == n - fcell) {
1847 os << (use_booktabs ? "\\midrule " : "\\hline ");
1849 for (idx_type i = fcell; i < n; ++i) {
1851 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1852 << column_of_cell(i) + 1
1854 << right_column_of_cell(i) + 1
1866 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1868 // FIXME: return 0 or assert as in TeXTopHLine()?
1869 if (row == npos || row >= rows_)
1872 idx_type const fcell = getFirstCellInRow(row);
1873 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1876 for (idx_type i = fcell; i < n; ++i) {
1880 if (use_booktabs && row == rows_ - 1) {
1881 os << "\\bottomrule";
1882 } else if (tmp == n - fcell) {
1883 os << (use_booktabs ? "\\midrule" : "\\hline");
1885 for (idx_type i = fcell; i < n; ++i) {
1886 if (bottomLine(i)) {
1887 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1888 << column_of_cell(i) + 1
1890 << right_column_of_cell(i) + 1
1902 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1906 if (getRotateCell(cell)) {
1907 os << "\\begin{sideways}\n";
1910 if (isMultiColumn(cell)) {
1911 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1912 if (!cellinfo_of_cell(cell).align_special.empty()) {
1913 os << cellinfo_of_cell(cell).align_special << "}{";
1915 if (leftLine(cell) &&
1916 (isFirstCellInRow(cell) ||
1917 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1918 !rightLine(cell - 1, true))))
1922 if (!getPWidth(cell).zero()) {
1923 switch (getVAlignment(cell)) {
1924 case LYX_VALIGN_TOP:
1927 case LYX_VALIGN_MIDDLE:
1930 case LYX_VALIGN_BOTTOM:
1935 << getPWidth(cell).asLatexString()
1938 switch (getAlignment(cell)) {
1939 case LYX_ALIGN_LEFT:
1942 case LYX_ALIGN_RIGHT:
1950 if (rightLine(cell))
1952 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1958 if (getUsebox(cell) == BOX_PARBOX) {
1960 switch (getVAlignment(cell)) {
1961 case LYX_VALIGN_TOP:
1964 case LYX_VALIGN_MIDDLE:
1967 case LYX_VALIGN_BOTTOM:
1971 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1972 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1973 os << "\\begin{minipage}[";
1974 switch (getVAlignment(cell)) {
1975 case LYX_VALIGN_TOP:
1978 case LYX_VALIGN_MIDDLE:
1981 case LYX_VALIGN_BOTTOM:
1985 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1992 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1997 if (getUsebox(cell) == BOX_PARBOX)
1999 else if (getUsebox(cell) == BOX_MINIPAGE) {
2000 os << "%\n\\end{minipage}";
2003 if (isMultiColumn(cell)) {
2006 if (getRotateCell(cell)) {
2007 os << "%\n\\end{sideways}";
2014 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
2015 OutputParams const & runparams) const
2017 if (!is_long_tabular)
2021 // output header info
2023 if (endhead.topDL) {
2027 for (row_type i = 0; i < rows_; ++i) {
2028 if (row_info[i].endhead) {
2029 ret += TeXRow(os, i, buf, runparams);
2032 if (endhead.bottomDL) {
2036 os << "\\endhead\n";
2038 if (endfirsthead.empty) {
2039 os << "\\endfirsthead\n";
2043 // output firstheader info
2044 if (haveLTFirstHead()) {
2045 if (endfirsthead.topDL) {
2049 for (row_type i = 0; i < rows_; ++i) {
2050 if (row_info[i].endfirsthead) {
2051 ret += TeXRow(os, i, buf, runparams);
2054 if (endfirsthead.bottomDL) {
2058 os << "\\endfirsthead\n";
2061 // output footer info
2063 if (endfoot.topDL) {
2067 for (row_type i = 0; i < rows_; ++i) {
2068 if (row_info[i].endfoot) {
2069 ret += TeXRow(os, i, buf, runparams);
2072 if (endfoot.bottomDL) {
2076 os << "\\endfoot\n";
2078 if (endlastfoot.empty) {
2079 os << "\\endlastfoot\n";
2083 // output lastfooter info
2084 if (haveLTLastFoot()) {
2085 if (endlastfoot.topDL) {
2089 for (row_type i = 0; i < rows_; ++i) {
2090 if (row_info[i].endlastfoot) {
2091 ret += TeXRow(os, i, buf, runparams);
2094 if (endlastfoot.bottomDL) {
2098 os << "\\endlastfoot\n";
2105 bool LyXTabular::isValidRow(row_type row) const
2107 if (!is_long_tabular)
2109 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2110 !row_info[row].endfoot && !row_info[row].endlastfoot;
2114 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
2115 OutputParams const & runparams) const
2117 idx_type cell = getCellNumber(i, 0);
2118 int ret = TeXTopHLine(os, i);
2119 if (row_info[i].top_space_default) {
2121 os << "\\addlinespace\n";
2123 os << "\\noalign{\\vskip\\doublerulesep}\n";
2124 } else if(!row_info[i].top_space.zero()) {
2126 os << "\\addlinespace["
2127 << row_info[i].top_space.asLatexString() << "]\n";
2129 os << "\\noalign{\\vskip"
2130 << row_info[i].top_space.asLatexString() << "}\n";
2135 for (col_type j = 0; j < columns_; ++j) {
2136 if (isPartOfMultiColumn(i, j))
2138 ret += TeXCellPreamble(os, cell);
2139 shared_ptr<InsetText> inset = getCellInset(cell);
2141 Paragraph const & par = inset->paragraphs().front();
2142 bool rtl = par.isRightToLeftPar(buf.params())
2144 && getPWidth(cell).zero();
2148 ret += inset->latex(buf, os, runparams);
2152 ret += TeXCellPostamble(os, cell);
2153 if (!isLastCellInRow(cell)) { // not last cell in row
2159 os << "\\tabularnewline";
2160 if (row_info[i].bottom_space_default) {
2162 os << "\\addlinespace";
2164 os << "[\\doublerulesep]";
2165 } else if (!row_info[i].bottom_space.zero()) {
2167 os << "\\addlinespace";
2168 os << '[' << row_info[i].bottom_space.asLatexString() << ']';
2172 ret += TeXBottomHLine(os, i);
2173 if (row_info[i].interline_space_default) {
2175 os << "\\addlinespace\n";
2177 os << "\\noalign{\\vskip\\doublerulesep}\n";
2178 } else if (!row_info[i].interline_space.zero()) {
2180 os << "\\addlinespace["
2181 << row_info[i].interline_space.asLatexString()
2184 os << "\\noalign{\\vskip"
2185 << row_info[i].interline_space.asLatexString()
2193 int LyXTabular::latex(Buffer const & buf, ostream & os,
2194 OutputParams const & runparams) const
2198 //+---------------------------------------------------------------------
2199 //+ first the opening preamble +
2200 //+---------------------------------------------------------------------
2203 os << "\\begin{sideways}\n";
2206 if (is_long_tabular)
2207 os << "\\begin{longtable}{";
2209 os << "\\begin{tabular}{";
2210 for (col_type i = 0; i < columns_; ++i) {
2211 if (!column_info[i].align_special.empty()) {
2212 os << column_info[i].align_special;
2214 if (!use_booktabs && column_info[i].left_line)
2216 if (!column_info[i].p_width.zero()) {
2217 switch (column_info[i].alignment) {
2218 case LYX_ALIGN_LEFT:
2219 os << ">{\\raggedright}";
2221 case LYX_ALIGN_RIGHT:
2222 os << ">{\\raggedleft}";
2224 case LYX_ALIGN_CENTER:
2225 os << ">{\\centering}";
2227 case LYX_ALIGN_NONE:
2228 case LYX_ALIGN_BLOCK:
2229 case LYX_ALIGN_LAYOUT:
2230 case LYX_ALIGN_SPECIAL:
2234 switch (column_info[i].valignment) {
2235 case LYX_VALIGN_TOP:
2238 case LYX_VALIGN_MIDDLE:
2241 case LYX_VALIGN_BOTTOM:
2246 << column_info[i].p_width.asLatexString()
2249 switch (column_info[i].alignment) {
2250 case LYX_ALIGN_LEFT:
2253 case LYX_ALIGN_RIGHT:
2261 if (!use_booktabs && column_info[i].right_line)
2268 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2270 //+---------------------------------------------------------------------
2271 //+ the single row and columns (cells) +
2272 //+---------------------------------------------------------------------
2274 for (row_type i = 0; i < rows_; ++i) {
2275 if (isValidRow(i)) {
2276 ret += TeXRow(os, i, buf, runparams);
2277 if (is_long_tabular && row_info[i].newpage) {
2278 os << "\\newpage\n";
2284 //+---------------------------------------------------------------------
2285 //+ the closing of the tabular +
2286 //+---------------------------------------------------------------------
2288 if (is_long_tabular)
2289 os << "\\end{longtable}";
2291 os << "\\end{tabular}";
2293 os << "\n\\end{sideways}";
2301 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2302 OutputParams const & runparams) const
2305 idx_type cell = getFirstCellInRow(row);
2308 for (col_type j = 0; j < columns_; ++j) {
2309 if (isPartOfMultiColumn(row, j))
2312 os << "<entry align=\"";
2313 switch (getAlignment(cell)) {
2314 case LYX_ALIGN_LEFT:
2317 case LYX_ALIGN_RIGHT:
2325 os << "\" valign=\"";
2326 switch (getVAlignment(cell)) {
2327 case LYX_VALIGN_TOP:
2330 case LYX_VALIGN_BOTTOM:
2333 case LYX_VALIGN_MIDDLE:
2338 if (isMultiColumn(cell)) {
2339 os << " namest=\"col" << j << "\" ";
2340 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2344 ret += getCellInset(cell)->docbook(buf, os, runparams);
2353 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2354 OutputParams const & runparams) const
2358 //+---------------------------------------------------------------------
2359 //+ first the opening preamble +
2360 //+---------------------------------------------------------------------
2362 os << "<tgroup cols=\"" << columns_
2363 << "\" colsep=\"1\" rowsep=\"1\">\n";
2365 for (col_type i = 0; i < columns_; ++i) {
2366 os << "<colspec colname=\"col" << i << "\" align=\"";
2367 switch (column_info[i].alignment) {
2368 case LYX_ALIGN_LEFT:
2371 case LYX_ALIGN_RIGHT:
2379 if (runparams.flavor == OutputParams::XML)
2385 //+---------------------------------------------------------------------
2386 //+ Long Tabular case +
2387 //+---------------------------------------------------------------------
2389 // output header info
2390 if (haveLTHead() || haveLTFirstHead()) {
2393 for (row_type i = 0; i < rows_; ++i) {
2394 if (row_info[i].endhead || row_info[i].endfirsthead) {
2395 ret += docbookRow(buf, os, i, runparams);
2401 // output footer info
2402 if (haveLTFoot() || haveLTLastFoot()) {
2405 for (row_type i = 0; i < rows_; ++i) {
2406 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2407 ret += docbookRow(buf, os, i, runparams);
2414 //+---------------------------------------------------------------------
2415 //+ the single row and columns (cells) +
2416 //+---------------------------------------------------------------------
2420 for (row_type i = 0; i < rows_; ++i) {
2421 if (isValidRow(i)) {
2422 ret += docbookRow(buf, os, i, runparams);
2427 //+---------------------------------------------------------------------
2428 //+ the closing of the tabular +
2429 //+---------------------------------------------------------------------
2438 int LyXTabular::asciiTopHLine(lyx::odocstream & os, row_type row,
2439 vector<unsigned int> const & clen) const
2441 idx_type const fcell = getFirstCellInRow(row);
2442 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2445 for (idx_type i = fcell; i < n; ++i) {
2455 for (idx_type i = fcell; i < n; ++i) {
2466 col_type column = column_of_cell(i);
2467 int len = clen[column];
2468 while (column < columns_ - 1
2469 && isPartOfMultiColumn(row, ++column))
2470 len += clen[column] + 4;
2471 os << docstring(len, ch);
2486 int LyXTabular::asciiBottomHLine(lyx::odocstream & os, row_type row,
2487 vector<unsigned int> const & clen) const
2489 idx_type const fcell = getFirstCellInRow(row);
2490 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2493 for (idx_type i = fcell; i < n; ++i) {
2494 if (bottomLine(i)) {
2503 for (idx_type i = fcell; i < n; ++i) {
2504 if (bottomLine(i)) {
2514 col_type column = column_of_cell(i);
2515 int len = clen[column];
2516 while (column < columns_ -1
2517 && isPartOfMultiColumn(row, ++column))
2518 len += clen[column] + 4;
2519 os << docstring(len, ch);
2520 if (bottomLine(i)) {
2534 int LyXTabular::asciiPrintCell(Buffer const & buf, lyx::odocstream & os,
2535 OutputParams const & runparams,
2536 idx_type cell, row_type row, col_type column,
2537 vector<unsigned int> const & clen,
2538 bool onlydata) const
2540 lyx::odocstringstream sstr;
2541 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2553 unsigned int len1 = sstr.str().length();
2554 unsigned int len2 = clen[column];
2555 while (column < columns_ -1
2556 && isPartOfMultiColumn(row, ++column))
2557 len2 += clen[column] + 4;
2560 switch (getAlignment(cell)) {
2562 case LYX_ALIGN_LEFT:
2565 case LYX_ALIGN_RIGHT:
2569 case LYX_ALIGN_CENTER:
2575 os << docstring(len1, ' ') << sstr.str()
2576 << docstring(len2, ' ');
2578 if (rightLine(cell))
2587 int LyXTabular::plaintext(Buffer const & buf, lyx::odocstream & os,
2588 OutputParams const & runparams,
2590 bool onlydata, unsigned char delim) const
2594 // first calculate the width of the single columns
2595 vector<unsigned int> clen(columns_);
2598 // first all non (real) multicolumn cells!
2599 for (col_type j = 0; j < columns_; ++j) {
2601 for (row_type i = 0; i < rows_; ++i) {
2602 idx_type cell = getCellNumber(i, j);
2603 if (isMultiColumnReal(cell))
2605 lyx::odocstringstream sstr;
2606 getCellInset(cell)->plaintext(buf, sstr, runparams);
2607 if (clen[j] < sstr.str().length())
2608 clen[j] = sstr.str().length();
2611 // then all (real) multicolumn cells!
2612 for (col_type j = 0; j < columns_; ++j) {
2613 for (row_type i = 0; i < rows_; ++i) {
2614 idx_type cell = getCellNumber(i, j);
2615 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2617 lyx::odocstringstream sstr;
2618 getCellInset(cell)->plaintext(buf, sstr, runparams);
2619 int len = int(sstr.str().length());
2620 idx_type const n = cells_in_multicolumn(cell);
2621 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2623 if (len > int(clen[j + n - 1]))
2624 clen[j + n - 1] = len;
2629 for (row_type i = 0; i < rows_; ++i) {
2630 if (!onlydata && asciiTopHLine(os, i, clen))
2631 os << docstring(depth * 2, ' ');
2632 for (col_type j = 0; j < columns_; ++j) {
2633 if (isPartOfMultiColumn(i, j))
2635 if (onlydata && j > 0)
2637 ret += asciiPrintCell(buf, os, runparams,
2638 cell, i, j, clen, onlydata);
2643 os << docstring(depth * 2, ' ');
2644 if (asciiBottomHLine(os, i, clen))
2645 os << docstring(depth * 2, ' ');
2652 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2654 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2658 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2659 col_type column) const
2661 return cell_info[row][column].inset;
2665 void LyXTabular::setCellInset(row_type row, col_type column,
2666 shared_ptr<InsetText> ins) const
2668 cell_info[row][column].inset = ins;
2672 LyXTabular::idx_type
2673 LyXTabular::getCellFromInset(InsetBase const * inset) const
2675 // is this inset part of the tabular?
2677 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2678 BOOST_ASSERT(false);
2681 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2682 if (getCellInset(cell).get() == inset) {
2683 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2684 << "cell=" << cell << endl;
2688 // We should have found a cell at this point
2689 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2690 << inset << " not found!" << endl;
2691 BOOST_ASSERT(false);
2697 void LyXTabular::validate(LaTeXFeatures & features) const
2699 features.require("NeedTabularnewline");
2701 features.require("booktabs");
2702 if (isLongTabular())
2703 features.require("longtable");
2705 features.require("rotating");
2706 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2707 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2708 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2709 features.require("array");
2710 getCellInset(cell)->validate(features);
2715 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2717 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2718 ParagraphList::const_iterator cit = parlist.begin();
2719 ParagraphList::const_iterator end = parlist.end();
2721 for (; cit != end; ++cit)
2722 for (int i = 0; i < cit->size(); ++i)
2723 if (cit->isNewline(i))