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.
24 #include "LaTeXFeatures.h"
26 #include "paragraph.h"
28 #include "insets/insettabular.h"
30 #include "support/LAssert.h"
31 #include "support/lstrings.h"
32 #include "support/tostr.h"
34 #include "support/std_sstream.h"
36 using namespace lyx::support;
40 using std::ostringstream;
47 #ifndef CXX_GLOBAL_CSTD
53 int const WIDTH_OF_LINE = 5;
56 string const write_attribute(string const & name, T const & t)
58 string const s = tostr(t);
59 return s.empty() ? s : " " + name + "=\"" + s + "\"";
62 string const write_attribute(string const & name, string const & t)
64 return t.empty() ? t : " " + name + "=\"" + t + "\"";
68 string const write_attribute(string const & name, bool const & b)
70 // we write only true attribute values so we remove a bit of the
71 // file format bloat for tabulars.
72 return b ? write_attribute(name, tostr(b)) : string();
76 string const write_attribute(string const & name, int const & i)
78 // we write only true attribute values so we remove a bit of the
79 // file format bloat for tabulars.
80 return i ? write_attribute(name, tostr(i)) : string();
84 string const write_attribute(string const & name, LyXLength const & value)
86 // we write only the value if we really have one same reson as above.
87 return value.zero() ? string() : write_attribute(name, value.asString());
91 string const tostr(LyXAlignment const & num)
100 case LYX_ALIGN_CENTER:
102 case LYX_ALIGN_RIGHT:
104 case LYX_ALIGN_LAYOUT:
106 case LYX_ALIGN_SPECIAL:
113 string const tostr(LyXTabular::VAlignment const & num)
116 case LyXTabular::LYX_VALIGN_TOP:
118 case LyXTabular::LYX_VALIGN_MIDDLE:
120 case LyXTabular::LYX_VALIGN_BOTTOM:
127 string const tostr(LyXTabular::BoxType const & num)
130 case LyXTabular::BOX_NONE:
132 case LyXTabular::BOX_PARBOX:
134 case LyXTabular::BOX_MINIPAGE:
140 // I would have liked a fromstr template a lot better. (Lgb)
141 bool string2type(string const str, LyXAlignment & num)
144 num = LYX_ALIGN_NONE;
145 else if (str == "block")
146 num = LYX_ALIGN_BLOCK;
147 else if (str == "left")
148 num = LYX_ALIGN_LEFT;
149 else if (str == "center")
150 num = LYX_ALIGN_CENTER;
151 else if (str == "right")
152 num = LYX_ALIGN_RIGHT;
159 bool string2type(string const str, LyXTabular::VAlignment & num)
162 num = LyXTabular::LYX_VALIGN_TOP;
163 else if (str == "middle" )
164 num = LyXTabular::LYX_VALIGN_MIDDLE;
165 else if (str == "bottom")
166 num = LyXTabular::LYX_VALIGN_BOTTOM;
173 bool string2type(string const str, LyXTabular::BoxType & num)
176 num = LyXTabular::BOX_NONE;
177 else if (str == "parbox")
178 num = LyXTabular::BOX_PARBOX;
179 else if (str == "minipage")
180 num = LyXTabular::BOX_MINIPAGE;
187 bool string2type(string const str, bool & num)
191 else if (str == "false")
199 bool getTokenValue(string const & str, const char * token, string & ret)
202 size_t token_length = strlen(token);
203 string::size_type pos = str.find(token);
205 if (pos == string::npos || pos + token_length + 1 >= str.length()
206 || str[pos + token_length] != '=')
208 pos += token_length + 1;
210 if ((ch != '"') && (ch != '\'')) { // only read till next space
214 while ((pos < str.length() - 1) && (str[++pos] != ch))
221 bool getTokenValue(string const & str, const char * token, int & num)
225 if (!getTokenValue(str, token, tmp))
232 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
235 if (!getTokenValue(str, token, tmp))
237 return string2type(tmp, num);
241 bool getTokenValue(string const & str, const char * token,
242 LyXTabular::VAlignment & num)
245 if (!getTokenValue(str, token, tmp))
247 return string2type(tmp, num);
251 bool getTokenValue(string const & str, const char * token,
252 LyXTabular::BoxType & num)
255 if (!getTokenValue(str, token, tmp))
257 return string2type(tmp, num);
261 bool getTokenValue(string const & str, const char * token, bool & flag)
263 // set the flag always to false as this should be the default for bools
264 // not in the file-format.
267 if (!getTokenValue(str, token, tmp))
269 return string2type(tmp, flag);
273 bool getTokenValue(string const & str, const char * token, LyXLength & len)
275 // set the lenght to be zero() as default as this it should be if not
276 // in the file format.
279 if (!getTokenValue(str, token, tmp))
281 return isValidLength(tmp, &len);
285 void l_getline(istream & is, string & str)
288 while (str.empty()) {
290 if (!str.empty() && str[str.length() - 1] == '\r')
291 str.erase(str.length() - 1);
297 /// Define a few methods for the inner structs
299 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
304 multicolumn = LyXTabular::CELL_NORMAL;
305 alignment = LYX_ALIGN_CENTER;
306 valignment = LYX_VALIGN_TOP;
316 LyXTabular::rowstruct::rowstruct()
323 endfirsthead = false;
330 LyXTabular::columnstruct::columnstruct()
334 alignment = LYX_ALIGN_CENTER;
335 valignment = LYX_VALIGN_TOP;
340 LyXTabular::ltType::ltType()
348 LyXTabular::LyXTabular(BufferParams const & bp,
349 InsetTabular * inset, int rows_arg, int columns_arg)
353 init(bp, rows_arg, columns_arg);
357 LyXTabular::LyXTabular(BufferParams const & bp,
358 InsetTabular * inset, LyXTabular const & lt)
362 init(bp, lt.rows_, lt.columns_, <);
366 LyXTabular::LyXTabular(Buffer const & buf, InsetTabular * inset, LyXLex & lex)
374 // activates all lines and sets all widths to 0
375 void LyXTabular::init(BufferParams const & bp,
376 int rows_arg, int columns_arg, LyXTabular const * lt)
379 columns_ = columns_arg;
380 row_info = row_vector(rows_, rowstruct());
381 column_info = column_vector(columns_, columnstruct());
382 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
390 for (int i = 0; i < rows_; ++i) {
391 for (int j = 0; j < columns_; ++j) {
392 cell_info[i][j].inset.setOwner(owner_);
393 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
394 cell_info[i][j].cellno = cellno++;
396 cell_info[i].back().right_line = true;
398 row_info.back().bottom_line = true;
399 row_info.front().bottom_line = true;
401 for (int i = 0; i < columns_; ++i)
402 calculate_width_of_column(i);
404 column_info.back().right_line = true;
406 calculate_width_of_tabular();
409 columnofcell.clear();
410 set_row_column_number_info();
411 is_long_tabular = false;
416 void LyXTabular::appendRow(BufferParams const & bp, int cell)
420 int row = row_of_cell(cell);
422 row_vector::iterator rit = row_info.begin() + row;
423 row_info.insert(rit, rowstruct());
424 // now set the values of the row before
425 row_info[row] = row_info[row + 1];
428 cell_vvector::iterator cit = cell_info.begin() + row;
429 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
431 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
434 for (int i = 0; i <= row; ++i)
435 for (int j = 0; j < columns_; ++j)
436 c_info[i][j] = cell_info[i][j];
438 for (int i = row + 1; i < rows_; ++i)
439 for (int j = 0; j < columns_; ++j)
440 c_info[i][j] = cell_info[i-1][j];
444 for (int j = 0; j < columns_; ++j) {
445 cell_info[row][j].inset.clear(false);
446 if (bp.tracking_changes)
447 cell_info[row][j].inset.markNew(true);
454 void LyXTabular::deleteRow(int row)
456 // Not allowed to delete last row
460 row_info.erase(row_info.begin() + row);
461 cell_info.erase(cell_info.begin() + row);
467 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
471 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
473 int const column = column_of_cell(cell);
474 column_vector::iterator cit = column_info.begin() + column + 1;
475 column_info.insert(cit, columnstruct());
476 // set the column values of the column before
477 column_info[column + 1] = column_info[column];
479 for (int i = 0; i < rows_; ++i) {
480 for (int j = 0; j <= column; ++j)
481 c_info[i][j] = cell_info[i][j];
483 for (int j = column + 1; j < columns_; ++j)
484 c_info[i][j] = cell_info[i][j - 1];
486 // care about multicolumns
487 if (c_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
488 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
490 if (column + 2 >= columns_
491 || c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
492 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
496 for (int i = 0; i < rows_; ++i) {
497 cell_info[i][column + 1].inset.clear(false);
498 if (bp.tracking_changes)
499 cell_info[i][column + 1].inset.markNew(true);
505 void LyXTabular::deleteColumn(int column)
507 // Not allowed to delete last column
511 column_info.erase(column_info.begin() + column);
512 for (int i = 0; i < rows_; ++i)
513 cell_info[i].erase(cell_info[i].begin() + column);
519 void LyXTabular::reinit()
525 void LyXTabular::Reinit(bool reset_widths)
528 for (int i = 0; i < rows_; ++i) {
529 for (int j = 0; j < columns_; ++j) {
530 cell_info[i][j].width_of_cell = 0;
531 cell_info[i][j].inset.setOwner(owner_);
536 for (int i = 0; i < columns_; ++i)
537 calculate_width_of_column(i);
539 calculate_width_of_tabular();
541 set_row_column_number_info();
545 void LyXTabular::set_row_column_number_info(bool oldformat)
548 for (int row = 0; row < rows_; ++row) {
549 for (int column = 0; column < columns_; ++column) {
550 if (cell_info[row][column].multicolumn
551 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
553 cell_info[row][column].cellno = numberofcells;
556 ++numberofcells; // because this is one more than as we start from 0
558 rowofcell.resize(numberofcells);
559 columnofcell.resize(numberofcells);
561 for (int row = 0, column = 0, c = 0;
562 c < numberofcells && row < rows_ && column < columns_;) {
564 columnofcell[c] = column;
568 } while (column < columns_ &&
569 cell_info[row][column].multicolumn
570 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
572 if (column == columns_) {
578 for (int row = 0; row < rows_; ++row) {
579 for (int column = 0; column < columns_; ++column) {
580 if (isPartOfMultiColumn(row,column))
582 // now set the right line of multicolumns right for oldformat read
584 cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
586 int cn = cells_in_multicolumn(cell_info[row][column].cellno);
587 cell_info[row][column].right_line =
588 cell_info[row][column+cn-1].right_line;
590 cell_info[row][column].inset.setAutoBreakRows(
591 !getPWidth(getCellNumber(row, column)).zero());
597 int LyXTabular::getNumberOfCells() const
599 return numberofcells;
603 int LyXTabular::numberOfCellsInRow(int cell) const
605 int const row = row_of_cell(cell);
607 for (int i = 0; i < columns_; ++i)
608 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
614 // returns 1 if there is a topline, returns 0 if not
615 bool LyXTabular::topLine(int cell, bool onlycolumn) const
617 if (!onlycolumn && isMultiColumn(cell))
618 return cellinfo_of_cell(cell).top_line;
619 return row_info[row_of_cell(cell)].top_line;
623 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
625 if (!onlycolumn && isMultiColumn(cell))
626 return cellinfo_of_cell(cell).bottom_line;
627 return row_info[row_of_cell(cell)].bottom_line;
631 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
633 if (!onlycolumn && isMultiColumn(cell) &&
634 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
636 if (cellinfo_of_cell(cell).align_special.empty())
637 return cellinfo_of_cell(cell).left_line;
638 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
640 if (column_info[column_of_cell(cell)].align_special.empty())
641 return column_info[column_of_cell(cell)].left_line;
642 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
646 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
648 if (!onlycolumn && isMultiColumn(cell) &&
649 (isLastCellInRow(cell) || isMultiColumn(cell+1)))
651 if (cellinfo_of_cell(cell).align_special.empty())
652 return cellinfo_of_cell(cell).right_line;
653 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
655 if (column_info[column_of_cell(cell)].align_special.empty())
656 return column_info[right_column_of_cell(cell)].right_line;
657 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
661 bool LyXTabular::topAlreadyDrawn(int cell) const
663 int row = row_of_cell(cell);
664 if (row > 0 && !getAdditionalHeight(row)) {
665 int column = column_of_cell(cell);
668 && cell_info[row][column].multicolumn
669 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
671 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
672 return row_info[row].bottom_line;
674 return cell_info[row][column].bottom_line;
680 bool LyXTabular::leftAlreadyDrawn(int cell) const
682 int column = column_of_cell(cell);
684 int row = row_of_cell(cell);
686 (cell_info[row][column].multicolumn ==
687 LyXTabular::CELL_PART_OF_MULTICOLUMN));
688 if (getAdditionalWidth(cell_info[row][column].cellno))
690 return rightLine(cell_info[row][column].cellno);
696 bool LyXTabular::isLastRow(int cell) const
698 return (row_of_cell(cell) == rows_ - 1);
702 int LyXTabular::getAdditionalHeight(int row) const
704 if (!row || row >= rows_)
710 for (int column = 0; column < columns_ && bottom; ++column) {
711 switch (cell_info[row - 1][column].multicolumn) {
712 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
713 bottom = cell_info[row - 1][column].bottom_line;
715 case LyXTabular::CELL_NORMAL:
716 bottom = row_info[row - 1].bottom_line;
719 for (int column = 0; column < columns_ && top; ++column) {
720 switch (cell_info[row][column].multicolumn) {
721 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
722 top = cell_info[row][column].top_line;
724 case LyXTabular::CELL_NORMAL:
725 top = row_info[row].top_line;
729 return WIDTH_OF_LINE;
734 int LyXTabular::getAdditionalWidth(int cell) const
736 // internally already set in setWidthOfCell
737 // used to get it back in text.C
738 int const col = right_column_of_cell(cell);
739 int const row = row_of_cell(cell);
740 if (col < columns_ - 1 && rightLine(cell) &&
741 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
743 return WIDTH_OF_LINE;
749 // returns the maximum over all rows
750 int LyXTabular::getWidthOfColumn(int cell) const
752 int const column1 = column_of_cell(cell);
753 int const column2 = right_column_of_cell(cell);
755 for (int i = column1; i <= column2; ++i)
756 result += column_info[i].width_of_column;
761 int LyXTabular::getWidthOfTabular() const
763 return width_of_tabular;
767 // returns true if a complete update is necessary, otherwise false
768 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
770 if (!isMultiColumn(cell))
773 int const row = row_of_cell(cell);
774 int const column1 = column_of_cell(cell);
775 int const column2 = right_column_of_cell(cell);
776 int const old_val = cell_info[row][column2].width_of_cell;
778 // first set columns to 0 so we can calculate the right width
779 for (int i = column1; i <= column2; ++i) {
780 cell_info[row][i].width_of_cell = 0;
782 // set the width to MAX_WIDTH until width > 0
783 int width = new_width + 2 * WIDTH_OF_LINE;
785 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
786 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
787 width -= column_info[i].width_of_column;
790 cell_info[row][i].width_of_cell = width;
792 if (old_val != cell_info[row][column2].width_of_cell) {
793 // in this case we have to recalculate all multicolumn cells which
794 // have this column as one of theirs but not as last one
795 calculate_width_of_column_NMC(i);
796 recalculateMulticolumnsOfColumn(i);
797 calculate_width_of_column(i);
803 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
805 // the last column does not have to be recalculated because all
806 // multicolumns will have here there last multicolumn cell which
807 // always will have the whole rest of the width of the cell.
808 if (column > (columns_ - 2))
810 for(int row = 0; row < rows_; ++row) {
811 int mc = cell_info[row][column].multicolumn;
812 int nmc = cell_info[row][column+1].multicolumn;
813 // we only have to update multicolumns which do not have this
814 // column as their last column!
815 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
816 (mc == CELL_PART_OF_MULTICOLUMN &&
817 nmc == CELL_PART_OF_MULTICOLUMN))
819 int const cellno = cell_info[row][column].cellno;
820 setWidthOfMulticolCell(cellno,
821 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
827 // returns 1 if a complete update is necessary, otherwise 0
828 void LyXTabular::setWidthOfCell(int cell, int new_width)
830 int const row = row_of_cell(cell);
831 int const column1 = column_of_cell(cell);
836 if (rightLine(cell_info[row][column1].cellno, true) &&
837 column1 < columns_ - 1 &&
838 leftLine(cell_info[row][column1+1].cellno, true))
840 add_width = WIDTH_OF_LINE;
843 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
846 if (isMultiColumn(cell, true)) {
847 tmp = setWidthOfMulticolCell(cell, new_width);
849 width = new_width + 2 * WIDTH_OF_LINE + add_width;
850 cell_info[row][column1].width_of_cell = width;
851 tmp = calculate_width_of_column_NMC(column1);
853 recalculateMulticolumnsOfColumn(column1);
856 for (int i = 0; i < columns_; ++i)
857 calculate_width_of_column(i);
858 calculate_width_of_tabular();
863 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
865 if (!isMultiColumn(cell) || onlycolumn)
866 column_info[column_of_cell(cell)].alignment = align;
868 cellinfo_of_cell(cell).alignment = align;
872 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
874 if (!isMultiColumn(cell) || onlycolumn)
875 column_info[column_of_cell(cell)].valignment = align;
877 cellinfo_of_cell(cell).valignment = align;
881 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
883 int const j = column_of_cell(cell);
885 column_info[j].p_width = width;
886 for (int i = 0; i < rows_; ++i) {
887 int const cell = getCellNumber(i, j);
888 // because of multicolumns
889 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
894 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
896 bool const flag = !width.zero();
898 cellinfo_of_cell(cell).p_width = width;
899 if (isMultiColumn(cell)) {
900 getCellInset(cell).setAutoBreakRows(flag);
907 void LyXTabular::setAlignSpecial(int cell, string const & special,
908 LyXTabular::Feature what)
910 if (what == SET_SPECIAL_MULTI)
911 cellinfo_of_cell(cell).align_special = special;
913 column_info[column_of_cell(cell)].align_special = special;
917 void LyXTabular::setAllLines(int cell, bool line)
919 setTopLine(cell, line);
920 setBottomLine(cell, line);
921 setRightLine(cell, line);
922 setLeftLine(cell, line);
926 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
928 int const row = row_of_cell(cell);
929 if (onlycolumn || !isMultiColumn(cell))
930 row_info[row].top_line = line;
932 cellinfo_of_cell(cell).top_line = line;
936 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
938 if (onlycolumn || !isMultiColumn(cell))
939 row_info[row_of_cell(cell)].bottom_line = line;
941 cellinfo_of_cell(cell).bottom_line = line;
945 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
947 if (onlycolumn || !isMultiColumn(cell))
948 column_info[column_of_cell(cell)].left_line = line;
950 cellinfo_of_cell(cell).left_line = line;
954 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
956 if (onlycolumn || !isMultiColumn(cell))
957 column_info[right_column_of_cell(cell)].right_line = line;
959 cellinfo_of_cell(cell).right_line = line;
963 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
965 if (!onlycolumn && isMultiColumn(cell))
966 return cellinfo_of_cell(cell).alignment;
967 return column_info[column_of_cell(cell)].alignment;
971 LyXTabular::VAlignment
972 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
974 if (!onlycolumn && isMultiColumn(cell))
975 return cellinfo_of_cell(cell).valignment;
976 return column_info[column_of_cell(cell)].valignment;
980 LyXLength const LyXTabular::getPWidth(int cell) const
982 if (isMultiColumn(cell))
983 return cellinfo_of_cell(cell).p_width;
984 return column_info[column_of_cell(cell)].p_width;
988 LyXLength const LyXTabular::getColumnPWidth(int cell) const
990 return column_info[column_of_cell(cell)].p_width;
994 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
996 if (isMultiColumn(cell))
997 return cellinfo_of_cell(cell).p_width;
1002 string const LyXTabular::getAlignSpecial(int cell, int what) const
1004 if (what == SET_SPECIAL_MULTI)
1005 return cellinfo_of_cell(cell).align_special;
1006 return column_info[column_of_cell(cell)].align_special;
1010 int LyXTabular::getWidthOfCell(int cell) const
1012 int const row = row_of_cell(cell);
1013 int const column1 = column_of_cell(cell);
1014 int const column2 = right_column_of_cell(cell);
1016 for (int i = column1; i <= column2; ++i)
1017 result += cell_info[row][i].width_of_cell;
1022 int LyXTabular::getBeginningOfTextInCell(int cell) const
1026 switch (getAlignment(cell)) {
1027 case LYX_ALIGN_CENTER:
1028 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1030 case LYX_ALIGN_RIGHT:
1031 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1032 // + getAdditionalWidth(cell);
1035 // LYX_ALIGN_LEFT: nothing :-)
1039 // the LaTeX Way :-(
1045 bool LyXTabular::isFirstCellInRow(int cell) const
1047 return column_of_cell(cell) == 0;
1051 int LyXTabular::getFirstCellInRow(int row) const
1053 if (row > rows_ - 1)
1055 return cell_info[row][0].cellno;
1059 bool LyXTabular::isLastCellInRow(int cell) const
1061 return right_column_of_cell(cell) == columns_ - 1;
1065 int LyXTabular::getLastCellInRow(int row) const
1067 if (row > rows_ - 1)
1069 return cell_info[row][columns_-1].cellno;
1073 void LyXTabular::calculate_width_of_column(int column)
1076 for (int i = 0; i < rows_; ++i)
1077 maximum = max(cell_info[i][column].width_of_cell, maximum);
1078 column_info[column].width_of_column = maximum;
1083 // Calculate the columns regarding ONLY the normal cells and if this
1084 // column is inside a multicolumn cell then use it only if its the last
1085 // column of this multicolumn cell as this gives an added width to the
1086 // column, all the rest should be adapted!
1088 bool LyXTabular::calculate_width_of_column_NMC(int column)
1090 int const old_column_width = column_info[column].width_of_column;
1092 for (int i = 0; i < rows_; ++i) {
1093 int cell = getCellNumber(i, column);
1094 bool ismulti = isMultiColumn(cell, true);
1095 if ((!ismulti || column == right_column_of_cell(cell)) &&
1096 cell_info[i][column].width_of_cell > max)
1098 max = cell_info[i][column].width_of_cell;
1101 column_info[column].width_of_column = max;
1102 return column_info[column].width_of_column != old_column_width;
1106 void LyXTabular::calculate_width_of_tabular()
1108 width_of_tabular = 0;
1109 for (int i = 0; i < columns_; ++i)
1110 width_of_tabular += column_info[i].width_of_column;
1114 int LyXTabular::row_of_cell(int cell) const
1116 if (cell >= numberofcells)
1120 return rowofcell[cell];
1124 int LyXTabular::column_of_cell(int cell) const
1126 if (cell >= numberofcells)
1127 return columns_ - 1;
1130 return columnofcell[cell];
1134 int LyXTabular::right_column_of_cell(int cell) const
1136 int const row = row_of_cell(cell);
1137 int column = column_of_cell(cell);
1138 while (column < columns_ - 1 &&
1139 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1145 void LyXTabular::write(Buffer const & buf, ostream & os) const
1149 << write_attribute("version", 3)
1150 << write_attribute("rows", rows_)
1151 << write_attribute("columns", columns_)
1153 // global longtable options
1155 << write_attribute("rotate", rotate)
1156 << write_attribute("islongtable", is_long_tabular)
1157 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1158 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1159 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1160 << write_attribute("headTopDL", endhead.topDL)
1161 << write_attribute("headBottomDL", endhead.bottomDL)
1162 << write_attribute("footTopDL", endfoot.topDL)
1163 << write_attribute("footBottomDL", endfoot.bottomDL)
1164 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1165 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1166 << write_attribute("lastFootEmpty", endlastfoot.empty)
1168 for (int j = 0; j < columns_; ++j) {
1170 << write_attribute("alignment", column_info[j].alignment)
1171 << write_attribute("valignment", column_info[j].valignment)
1172 << write_attribute("leftline", column_info[j].left_line)
1173 << write_attribute("rightline", column_info[j].right_line)
1174 << write_attribute("width", column_info[j].p_width.asString())
1175 << write_attribute("special", column_info[j].align_special)
1178 for (int i = 0; i < rows_; ++i) {
1180 << write_attribute("topline", row_info[i].top_line)
1181 << write_attribute("bottomline", row_info[i].bottom_line)
1182 << write_attribute("endhead", row_info[i].endhead)
1183 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1184 << write_attribute("endfoot", row_info[i].endfoot)
1185 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1186 << write_attribute("newpage", row_info[i].newpage)
1188 for (int j = 0; j < columns_; ++j) {
1190 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1191 << write_attribute("alignment", cell_info[i][j].alignment)
1192 << write_attribute("valignment", cell_info[i][j].valignment)
1193 << write_attribute("topline", cell_info[i][j].top_line)
1194 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1195 << write_attribute("leftline", cell_info[i][j].left_line)
1196 << write_attribute("rightline", cell_info[i][j].right_line)
1197 << write_attribute("rotate", cell_info[i][j].rotate)
1198 << write_attribute("usebox", cell_info[i][j].usebox)
1199 << write_attribute("width", cell_info[i][j].p_width)
1200 << write_attribute("special", cell_info[i][j].align_special)
1202 os << "\\begin_inset ";
1203 cell_info[i][j].inset.write(buf, os);
1204 os << "\n\\end_inset \n"
1209 os << "</lyxtabular>\n";
1213 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1217 row_info[--hr].endhead = true;
1219 // set firstheader info
1220 if (fhr && fhr < rows_) {
1221 if (row_info[fhr].endhead) {
1223 row_info[--fhr].endfirsthead = true;
1224 row_info[fhr].endhead = false;
1226 } else if (row_info[fhr - 1].endhead) {
1227 endfirsthead.empty = true;
1229 while (fhr > 0 && !row_info[--fhr].endhead) {
1230 row_info[fhr].endfirsthead = true;
1235 if (fr && fr < rows_) {
1236 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1237 while (fr > 0 && !row_info[--fr].endhead) {
1238 row_info[fr].endfoot = true;
1239 row_info[fr].endhead = false;
1241 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1242 while (fr > 0 && !row_info[--fr].endfirsthead) {
1243 row_info[fr].endfoot = true;
1244 row_info[fr].endfirsthead = false;
1246 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1247 while (fr > 0 && !row_info[--fr].endhead &&
1248 !row_info[fr].endfirsthead)
1250 row_info[fr].endfoot = true;
1254 // set lastfooter info
1255 if (lfr && lfr < rows_) {
1256 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1257 while (lfr > 0 && !row_info[--lfr].endhead) {
1258 row_info[lfr].endlastfoot = true;
1259 row_info[lfr].endhead = false;
1261 } else if (row_info[lfr].endfirsthead &&
1262 row_info[lfr - 1].endfirsthead)
1264 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1265 row_info[lfr].endlastfoot = true;
1266 row_info[lfr].endfirsthead = false;
1268 } else if (row_info[lfr].endfoot
1269 && row_info[lfr - 1].endfoot) {
1270 while (lfr > 0 && !row_info[--lfr].endfoot) {
1271 row_info[lfr].endlastfoot = true;
1272 row_info[lfr].endfoot = false;
1274 } else if (!row_info[fr - 1].endhead
1275 && !row_info[fr - 1].endfirsthead &&
1276 !row_info[fr - 1].endfoot)
1279 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1280 !row_info[lfr].endfoot)
1282 row_info[lfr].endlastfoot = true;
1284 } else if (haveLTFoot()) {
1285 endlastfoot.empty = true;
1291 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1294 istream & is = lex.getStream();
1296 l_getline(is, line);
1297 if (!prefixIs(line, "<lyxtabular ")
1298 && !prefixIs(line, "<LyXTabular ")) {
1304 if (!getTokenValue(line, "version", version))
1306 Assert(version >= 2);
1309 if (!getTokenValue(line, "rows", rows_arg))
1312 if (!getTokenValue(line, "columns", columns_arg))
1314 init(buf.params, rows_arg, columns_arg);
1315 l_getline(is, line);
1316 if (!prefixIs(line, "<features")) {
1317 lyxerr << "Wrong tabular format (expected <features ...> got"
1318 << line << ')' << endl;
1321 getTokenValue(line, "rotate", rotate);
1322 getTokenValue(line, "islongtable", is_long_tabular);
1323 // compatibility read for old longtable options. Now we can make any
1324 // row part of the header/footer type we want before it was strict
1325 // sequential from the first row down (as LaTeX does it!). So now when
1326 // we find a header/footer line we have to go up the rows and set it
1327 // on all preceding rows till the first or one with already a h/f option
1328 // set. If we find a firstheader on the same line as a header or a
1329 // lastfooter on the same line as a footer then this should be set empty.
1337 getTokenValue(line, "endhead", hrow);
1338 getTokenValue(line, "endfirsthead", fhrow);
1339 getTokenValue(line, "endfoot", frow);
1340 getTokenValue(line, "endlastfoot", lfrow);
1341 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1343 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1344 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1345 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1346 getTokenValue(line, "headTopDL", endhead.topDL);
1347 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1348 getTokenValue(line, "footTopDL", endfoot.topDL);
1349 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1350 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1351 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1352 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1354 for (int j = 0; j < columns_; ++j) {
1356 if (!prefixIs(line,"<column")) {
1357 lyxerr << "Wrong tabular format (expected <column ...> got"
1358 << line << ')' << endl;
1361 getTokenValue(line, "alignment", column_info[j].alignment);
1362 getTokenValue(line, "valignment", column_info[j].valignment);
1363 getTokenValue(line, "leftline", column_info[j].left_line);
1364 getTokenValue(line, "rightline", column_info[j].right_line);
1365 getTokenValue(line, "width", column_info[j].p_width);
1366 getTokenValue(line, "special", column_info[j].align_special);
1369 for (int i = 0; i < rows_; ++i) {
1370 l_getline(is, line);
1371 if (!prefixIs(line, "<row")) {
1372 lyxerr << "Wrong tabular format (expected <row ...> got"
1373 << line << ')' << endl;
1376 getTokenValue(line, "topline", row_info[i].top_line);
1377 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1378 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1379 getTokenValue(line, "endhead", row_info[i].endhead);
1380 getTokenValue(line, "endfoot", row_info[i].endfoot);
1381 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1382 getTokenValue(line, "newpage", row_info[i].newpage);
1383 for (int j = 0; j < columns_; ++j) {
1384 l_getline(is, line);
1385 if (!prefixIs(line, "<cell")) {
1386 lyxerr << "Wrong tabular format (expected <cell ...> got"
1387 << line << ')' << endl;
1390 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1391 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1392 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1393 getTokenValue(line, "topline", cell_info[i][j].top_line);
1394 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1395 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1396 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1397 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1398 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1399 getTokenValue(line, "width", cell_info[i][j].p_width);
1400 getTokenValue(line, "special", cell_info[i][j].align_special);
1401 l_getline(is, line);
1402 if (prefixIs(line, "\\begin_inset")) {
1403 cell_info[i][j].inset.read(buf, lex);
1404 l_getline(is, line);
1406 if (!prefixIs(line, "</cell>")) {
1407 lyxerr << "Wrong tabular format (expected </cell> got"
1408 << line << ')' << endl;
1412 l_getline(is, line);
1413 if (!prefixIs(line, "</row>")) {
1414 lyxerr << "Wrong tabular format (expected </row> got"
1415 << line << ')' << endl;
1419 while (!prefixIs(line, "</lyxtabular>")) {
1420 l_getline(is, line);
1422 set_row_column_number_info();
1426 bool LyXTabular::isMultiColumn(int cell, bool real) const
1428 return (!real || column_of_cell(cell) != right_column_of_cell(cell)) &&
1429 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1433 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1435 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1439 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1441 cellstruct & cs = cellinfo_of_cell(cell);
1442 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1443 cs.alignment = column_info[column_of_cell(cell)].alignment;
1444 cs.top_line = row_info[row_of_cell(cell)].top_line;
1445 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1446 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1447 for (int i = 1; i < number; ++i) {
1448 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1449 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1450 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1451 cs1.inset.clear(false);
1453 set_row_column_number_info();
1457 int LyXTabular::cells_in_multicolumn(int cell) const
1459 int const row = row_of_cell(cell);
1460 int column = column_of_cell(cell);
1463 while (column < columns_ &&
1464 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1473 int LyXTabular::unsetMultiColumn(int cell)
1475 int const row = row_of_cell(cell);
1476 int column = column_of_cell(cell);
1480 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1481 cell_info[row][column].multicolumn = CELL_NORMAL;
1483 while (column < columns_ &&
1484 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1486 cell_info[row][column].multicolumn = CELL_NORMAL;
1491 set_row_column_number_info();
1496 void LyXTabular::setLongTabular(bool what)
1498 is_long_tabular = what;
1502 bool LyXTabular::isLongTabular() const
1504 return is_long_tabular;
1508 void LyXTabular::setRotateTabular(bool flag)
1514 bool LyXTabular::getRotateTabular() const
1520 void LyXTabular::setRotateCell(int cell, bool flag)
1522 cellinfo_of_cell(cell).rotate = flag;
1526 bool LyXTabular::getRotateCell(int cell) const
1528 return cellinfo_of_cell(cell).rotate;
1532 bool LyXTabular::needRotating() const
1536 for (int i = 0; i < rows_; ++i)
1537 for (int j = 0; j < columns_; ++j)
1538 if (cell_info[i][j].rotate)
1544 bool LyXTabular::isLastCell(int cell) const
1546 if (cell + 1 < numberofcells)
1552 int LyXTabular::getCellAbove(int cell) const
1554 if (row_of_cell(cell) > 0)
1555 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1560 int LyXTabular::getCellBelow(int cell) const
1562 if (row_of_cell(cell) + 1 < rows_)
1563 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1568 int LyXTabular::getLastCellAbove(int cell) const
1570 if (row_of_cell(cell) <= 0)
1572 if (!isMultiColumn(cell))
1573 return getCellAbove(cell);
1574 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1578 int LyXTabular::getLastCellBelow(int cell) const
1580 if (row_of_cell(cell) + 1 >= rows_)
1582 if (!isMultiColumn(cell))
1583 return getCellBelow(cell);
1584 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1588 int LyXTabular::getCellNumber(int row, int column) const
1590 Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1591 return cell_info[row][column].cellno;
1595 void LyXTabular::setUsebox(int cell, BoxType type)
1597 cellinfo_of_cell(cell).usebox = type;
1601 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1603 if (column_info[column_of_cell(cell)].p_width.zero() &&
1604 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1606 if (cellinfo_of_cell(cell).usebox > 1)
1607 return cellinfo_of_cell(cell).usebox;
1608 return useParbox(cell);
1613 // This are functions used for the longtable support
1615 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1620 row_info[row].endfirsthead = flag;
1624 row_info[row].endhead = flag;
1629 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1632 hd.set = haveLTHead();
1633 return row_info[row].endhead;
1637 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1640 hd.set = haveLTFirstHead();
1641 return row_info[row].endfirsthead;
1645 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1650 row_info[row].endlastfoot = flag;
1654 row_info[row].endfoot = flag;
1659 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1662 fd.set = haveLTFoot();
1663 return row_info[row].endfoot;
1667 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1670 fd.set = haveLTLastFoot();
1671 return row_info[row].endlastfoot;
1675 void LyXTabular::setLTNewPage(int row, bool what)
1677 row_info[row].newpage = what;
1681 bool LyXTabular::getLTNewPage(int row) const
1683 return row_info[row].newpage;
1687 bool LyXTabular::haveLTHead() const
1689 for (int i = 0; i < rows_; ++i)
1690 if (row_info[i].endhead)
1696 bool LyXTabular::haveLTFirstHead() const
1698 if (endfirsthead.empty)
1700 for (int i = 0; i < rows_; ++i)
1701 if (row_info[i].endfirsthead)
1707 bool LyXTabular::haveLTFoot() const
1709 for (int i = 0; i < rows_; ++i)
1710 if (row_info[i].endfoot)
1716 bool LyXTabular::haveLTLastFoot() const
1718 if (endlastfoot.empty)
1720 for (int i = 0; i < rows_; ++i)
1721 if (row_info[i].endlastfoot)
1727 // end longtable support functions
1729 void LyXTabular::setAscentOfRow(int row, int height)
1731 if (row >= rows_ || row_info[row].ascent_of_row == height)
1733 row_info[row].ascent_of_row = height;
1737 void LyXTabular::setDescentOfRow(int row, int height)
1739 if (row >= rows_ || row_info[row].descent_of_row == height)
1741 row_info[row].descent_of_row = height;
1745 int LyXTabular::getAscentOfRow(int row) const
1749 return row_info[row].ascent_of_row;
1753 int LyXTabular::getDescentOfRow(int row) const
1757 return row_info[row].descent_of_row;
1761 int LyXTabular::getHeightOfTabular() const
1764 for (int row = 0; row < rows_; ++row)
1765 height += getAscentOfRow(row) + getDescentOfRow(row) +
1766 getAdditionalHeight(row);
1771 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1773 if (row >= rows_ || column >= columns_)
1775 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1779 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1781 if (row < 0 || row >= rows_)
1784 int const fcell = getFirstCellInRow(row);
1785 int const n = numberOfCellsInRow(fcell) + fcell;
1788 for (int i = fcell; i < n; ++i) {
1792 if (tmp == n - fcell) {
1795 for (int i = fcell; i < n; ++i) {
1798 << column_of_cell(i) + 1
1800 << right_column_of_cell(i) + 1
1812 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1814 if (row < 0 || row >= rows_)
1817 int const fcell = getFirstCellInRow(row);
1818 int const n = numberOfCellsInRow(fcell) + fcell;
1821 for (int i = fcell; i < n; ++i) {
1825 if (tmp == n - fcell) {
1828 for (int i = fcell; i < n; ++i) {
1829 if (bottomLine(i)) {
1831 << column_of_cell(i) + 1
1833 << right_column_of_cell(i) + 1
1845 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1849 if (getRotateCell(cell)) {
1850 os << "\\begin{sideways}\n";
1853 if (isMultiColumn(cell)) {
1854 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1855 if (!cellinfo_of_cell(cell).align_special.empty()) {
1856 os << cellinfo_of_cell(cell).align_special << "}{";
1858 if (leftLine(cell) &&
1859 (isFirstCellInRow(cell) ||
1860 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1861 !rightLine(cell - 1, true))))
1865 if (!getPWidth(cell).zero()) {
1866 switch (getVAlignment(cell)) {
1867 case LYX_VALIGN_TOP:
1870 case LYX_VALIGN_MIDDLE:
1873 case LYX_VALIGN_BOTTOM:
1878 << getPWidth(cell).asLatexString()
1881 switch (getAlignment(cell)) {
1882 case LYX_ALIGN_LEFT:
1885 case LYX_ALIGN_RIGHT:
1893 if (rightLine(cell))
1895 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1901 if (getUsebox(cell) == BOX_PARBOX) {
1903 switch (getVAlignment(cell)) {
1904 case LYX_VALIGN_TOP:
1907 case LYX_VALIGN_MIDDLE:
1910 case LYX_VALIGN_BOTTOM:
1914 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1915 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1916 os << "\\begin{minipage}[";
1917 switch (getVAlignment(cell)) {
1918 case LYX_VALIGN_TOP:
1921 case LYX_VALIGN_MIDDLE:
1924 case LYX_VALIGN_BOTTOM:
1928 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1935 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1940 if (getUsebox(cell) == BOX_PARBOX)
1942 else if (getUsebox(cell) == BOX_MINIPAGE) {
1943 os << "%\n\\end{minipage}";
1946 if (isMultiColumn(cell)) {
1949 if (getRotateCell(cell)) {
1950 os << "%\n\\end{sideways}";
1957 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1958 LatexRunParams const & runparams) const
1960 if (!is_long_tabular)
1964 // output header info
1966 if (endhead.topDL) {
1970 for (int i = 0; i < rows_; ++i) {
1971 if (row_info[i].endhead) {
1972 ret += TeXRow(os, i, buf, runparams);
1975 if (endhead.bottomDL) {
1979 os << "\\endhead\n";
1981 if (endfirsthead.empty) {
1982 os << "\\endfirsthead\n";
1986 // output firstheader info
1987 if (haveLTFirstHead()) {
1988 if (endfirsthead.topDL) {
1992 for (int i = 0; i < rows_; ++i) {
1993 if (row_info[i].endfirsthead) {
1994 ret += TeXRow(os, i, buf, runparams);
1997 if (endfirsthead.bottomDL) {
2001 os << "\\endfirsthead\n";
2004 // output footer info
2006 if (endfoot.topDL) {
2010 for (int i = 0; i < rows_; ++i) {
2011 if (row_info[i].endfoot) {
2012 ret += TeXRow(os, i, buf, runparams);
2015 if (endfoot.bottomDL) {
2019 os << "\\endfoot\n";
2021 if (endlastfoot.empty) {
2022 os << "\\endlastfoot\n";
2026 // output lastfooter info
2027 if (haveLTLastFoot()) {
2028 if (endlastfoot.topDL) {
2032 for (int i = 0; i < rows_; ++i) {
2033 if (row_info[i].endlastfoot) {
2034 ret += TeXRow(os, i, buf, runparams);
2037 if (endlastfoot.bottomDL) {
2041 os << "\\endlastfoot\n";
2048 bool LyXTabular::isValidRow(int const row) const
2050 if (!is_long_tabular)
2052 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2053 !row_info[row].endfoot && !row_info[row].endlastfoot);
2057 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const & buf,
2058 LatexRunParams const & runparams) const
2061 int cell = getCellNumber(i, 0);
2063 ret += TeXTopHLine(os, i);
2064 for (int j = 0; j < columns_; ++j) {
2065 if (isPartOfMultiColumn(i, j))
2067 ret += TeXCellPreamble(os, cell);
2068 InsetText & inset = getCellInset(cell);
2070 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(buf.params) &&
2071 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2075 ret += inset.latex(buf, os, runparams);
2079 ret += TeXCellPostamble(os, cell);
2080 if (!isLastCellInRow(cell)) { // not last cell in row
2086 os << "\\tabularnewline\n";
2088 ret += TeXBottomHLine(os, i);
2093 int LyXTabular::latex(Buffer const & buf, ostream & os,
2094 LatexRunParams const & runparams) const
2098 //+---------------------------------------------------------------------
2099 //+ first the opening preamble +
2100 //+---------------------------------------------------------------------
2103 os << "\\begin{sideways}\n";
2106 if (is_long_tabular)
2107 os << "\\begin{longtable}{";
2109 os << "\\begin{tabular}{";
2110 for (int i = 0; i < columns_; ++i) {
2111 if (!column_info[i].align_special.empty()) {
2112 os << column_info[i].align_special;
2114 if (column_info[i].left_line)
2116 if (!column_info[i].p_width.zero()) {
2117 switch (column_info[i].alignment) {
2118 case LYX_ALIGN_LEFT:
2119 os << ">{\\raggedright}";
2121 case LYX_ALIGN_RIGHT:
2122 os << ">{\\raggedleft}";
2124 case LYX_ALIGN_CENTER:
2125 os << ">{\\centering}";
2127 case LYX_ALIGN_NONE:
2128 case LYX_ALIGN_BLOCK:
2129 case LYX_ALIGN_LAYOUT:
2130 case LYX_ALIGN_SPECIAL:
2134 switch (column_info[i].valignment) {
2135 case LYX_VALIGN_TOP:
2138 case LYX_VALIGN_MIDDLE:
2141 case LYX_VALIGN_BOTTOM:
2146 << column_info[i].p_width.asLatexString()
2149 switch (column_info[i].alignment) {
2150 case LYX_ALIGN_LEFT:
2153 case LYX_ALIGN_RIGHT:
2161 if (column_info[i].right_line)
2168 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2170 //+---------------------------------------------------------------------
2171 //+ the single row and columns (cells) +
2172 //+---------------------------------------------------------------------
2174 for (int i = 0; i < rows_; ++i) {
2175 if (isValidRow(i)) {
2176 ret += TeXRow(os, i, buf, runparams);
2177 if (is_long_tabular && row_info[i].newpage) {
2178 os << "\\newpage\n";
2184 //+---------------------------------------------------------------------
2185 //+ the closing of the tabular +
2186 //+---------------------------------------------------------------------
2188 if (is_long_tabular)
2189 os << "\\end{longtable}";
2191 os << "\\end{tabular}";
2193 os << "\n\\end{sideways}";
2201 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os) const
2203 os << "<tabular ca=\"";
2204 for (int i = 0; i < columns_; ++i) {
2205 switch (column_info[i].alignment) {
2206 case LYX_ALIGN_LEFT:
2209 case LYX_ALIGN_RIGHT:
2220 for (int i = 0; i < rows_; ++i) {
2221 for (int j = 0; j < columns_; ++j) {
2222 if (isPartOfMultiColumn(i, j))
2224 InsetText & inset = getCellInset(cell);
2226 ret += inset.linuxdoc(buf, os);
2228 if (isLastCellInRow(cell)) {
2237 os << "</tabular>\n";
2242 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row) const
2245 int cell = getFirstCellInRow(row);
2248 for (int j = 0; j < columns_; ++j) {
2249 if (isPartOfMultiColumn(row, j))
2252 os << "<entry align=\"";
2253 switch (getAlignment(cell)) {
2254 case LYX_ALIGN_LEFT:
2257 case LYX_ALIGN_RIGHT:
2265 os << "\" valign=\"";
2266 switch (getVAlignment(cell)) {
2267 case LYX_VALIGN_TOP:
2270 case LYX_VALIGN_BOTTOM:
2273 case LYX_VALIGN_MIDDLE:
2278 if (isMultiColumn(cell)) {
2279 os << " namest=\"col" << j << "\" ";
2280 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2284 ret += getCellInset(cell).docbook(buf, os, true);
2293 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2294 bool /*mixcont*/) const
2298 //+---------------------------------------------------------------------
2299 //+ first the opening preamble +
2300 //+---------------------------------------------------------------------
2302 os << "<tgroup cols=\"" << columns_
2303 << "\" colsep=\"1\" rowsep=\"1\">\n";
2305 for (int i = 0; i < columns_; ++i) {
2306 os << "<colspec colname=\"col" << i << "\" align=\"";
2307 switch (column_info[i].alignment) {
2308 case LYX_ALIGN_LEFT:
2311 case LYX_ALIGN_RIGHT:
2322 //+---------------------------------------------------------------------
2323 //+ Long Tabular case +
2324 //+---------------------------------------------------------------------
2326 // output header info
2327 if (haveLTHead() || haveLTFirstHead()) {
2330 for (int i = 0; i < rows_; ++i) {
2331 if (row_info[i].endhead || row_info[i].endfirsthead) {
2332 ret += docbookRow(buf, os, i);
2338 // output footer info
2339 if (haveLTFoot() || haveLTLastFoot()) {
2342 for (int i = 0; i < rows_; ++i) {
2343 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2344 ret += docbookRow(buf, os, i);
2351 //+---------------------------------------------------------------------
2352 //+ the single row and columns (cells) +
2353 //+---------------------------------------------------------------------
2357 for (int i = 0; i < rows_; ++i) {
2358 if (isValidRow(i)) {
2359 ret += docbookRow(buf, os, i);
2364 //+---------------------------------------------------------------------
2365 //+ the closing of the tabular +
2366 //+---------------------------------------------------------------------
2375 int LyXTabular::asciiTopHLine(ostream & os, int row,
2376 vector<unsigned int> const & clen) const
2378 int const fcell = getFirstCellInRow(row);
2379 int const n = numberOfCellsInRow(fcell) + fcell;
2382 for (int i = fcell; i < n; ++i) {
2392 for (int i = fcell; i < n; ++i) {
2403 int column = column_of_cell(i);
2404 int len = clen[column];
2405 while (isPartOfMultiColumn(row, ++column))
2406 len += clen[column] + 4;
2407 os << string(len, ch);
2422 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2423 vector<unsigned int> const & clen) const
2425 int const fcell = getFirstCellInRow(row);
2426 int const n = numberOfCellsInRow(fcell) + fcell;
2429 for (int i = fcell; i < n; ++i) {
2430 if (bottomLine(i)) {
2439 for (int i = fcell; i < n; ++i) {
2440 if (bottomLine(i)) {
2450 int column = column_of_cell(i);
2451 int len = clen[column];
2452 while (isPartOfMultiColumn(row, ++column))
2453 len += clen[column] + 4;
2454 os << string(len, ch);
2455 if (bottomLine(i)) {
2469 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2470 int cell, int row, int column,
2471 vector<unsigned int> const & clen,
2472 bool onlydata) const
2475 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2487 unsigned int len1 = sstr.str().length();
2488 unsigned int len2 = clen[column];
2489 while (isPartOfMultiColumn(row, ++column))
2490 len2 += clen[column] + 4;
2493 switch (getAlignment(cell)) {
2495 case LYX_ALIGN_LEFT:
2498 case LYX_ALIGN_RIGHT:
2502 case LYX_ALIGN_CENTER:
2508 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2510 if (rightLine(cell))
2519 int LyXTabular::ascii(Buffer const & buf, ostream & os, int const depth,
2520 bool onlydata, unsigned char delim) const
2524 // first calculate the width of the single columns
2525 vector<unsigned int> clen(columns_);
2528 // first all non (real) multicolumn cells!
2529 for (int j = 0; j < columns_; ++j) {
2531 for (int i = 0; i < rows_; ++i) {
2532 int cell = getCellNumber(i, j);
2533 if (isMultiColumn(cell, true))
2536 getCellInset(cell).ascii(buf, sstr, 0);
2537 if (clen[j] < sstr.str().length())
2538 clen[j] = sstr.str().length();
2541 // then all (real) multicolumn cells!
2542 for (int j = 0; j < columns_; ++j) {
2543 for (int i = 0; i < rows_; ++i) {
2544 int cell = getCellNumber(i, j);
2545 if (!isMultiColumn(cell, true) || isPartOfMultiColumn(i, j))
2548 getCellInset(cell).ascii(buf, sstr, 0);
2549 int len = int(sstr.str().length());
2550 int const n = cells_in_multicolumn(cell);
2551 for (int k = j; len > 0 && k < j + n - 1; ++k)
2553 if (len > int(clen[j + n - 1]))
2554 clen[j + n - 1] = len;
2559 for (int i = 0; i < rows_; ++i) {
2560 if (!onlydata && asciiTopHLine(os, i, clen))
2561 for (int j = 0; j < depth; ++j)
2563 for (int j = 0; j < columns_; ++j) {
2564 if (isPartOfMultiColumn(i, j))
2566 if (onlydata && j > 0)
2568 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2573 for (int j = 0; j < depth; ++j)
2575 if (asciiBottomHLine(os, i, clen))
2576 for (int j = 0; j < depth; ++j)
2584 InsetText & LyXTabular::getCellInset(int cell) const
2587 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2591 InsetText & LyXTabular::getCellInset(int row, int column) const
2593 cur_cell = getCellNumber(row, column);
2594 return cell_info[row][column].inset;
2598 int LyXTabular::getCellFromInset(InsetOld const * inset, int maybe_cell) const
2600 // is this inset part of the tabular?
2601 if (!inset || inset->owner() != owner_) {
2602 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2606 const int save_cur_cell = cur_cell;
2607 int cell = cur_cell;
2608 if (&getCellInset(cell) != inset) {
2610 if (cell == -1 || &getCellInset(cell) != inset)
2615 for (cell = getNumberOfCells(); cell >= 0; --cell)
2616 if (&getCellInset(cell) == inset)
2619 lyxerr[Debug::INSETTEXT]
2620 << "LyXTabular::getCellFromInset: "
2622 << ", cur_cell=" << save_cur_cell
2623 << ", maybe_cell=" << maybe_cell
2625 // We should have found a cell at this point
2627 lyxerr << "LyXTabular::getCellFromInset: "
2628 << "Cell not found!" << endl;
2636 void LyXTabular::validate(LaTeXFeatures & features) const
2638 features.require("NeedTabularnewline");
2639 if (isLongTabular())
2640 features.require("longtable");
2642 features.require("rotating");
2643 for (int cell = 0; cell < numberofcells; ++cell) {
2644 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2645 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2646 features.require("array");
2647 getCellInset(cell).validate(features);
2652 void LyXTabular::getLabelList(std::vector<string> & list) const
2654 for (int i = 0; i < rows_; ++i)
2655 for (int j = 0; j < columns_; ++j)
2656 getCellInset(i, j).getLabelList(list);
2660 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2662 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2663 ParagraphList::const_iterator cit = parlist.begin();
2664 ParagraphList::const_iterator end = parlist.end();
2666 for (; cit != end; ++cit)
2667 for (int i = 0; i < cit->size(); ++i)
2668 if (cit->isNewline(i))