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;
45 using std::ostringstream;
48 #ifndef CXX_GLOBAL_CSTD
54 int const WIDTH_OF_LINE = 5;
57 string const write_attribute(string const & name, T const & t)
59 string const s = tostr(t);
60 return s.empty() ? s : " " + name + "=\"" + s + "\"";
63 string const write_attribute(string const & name, string const & t)
65 return t.empty() ? t : " " + name + "=\"" + t + "\"";
69 string const write_attribute(string const & name, bool const & b)
71 // we write only true attribute values so we remove a bit of the
72 // file format bloat for tabulars.
73 return b ? write_attribute(name, tostr(b)) : string();
77 string const write_attribute(string const & name, int const & i)
79 // we write only true attribute values so we remove a bit of the
80 // file format bloat for tabulars.
81 return i ? write_attribute(name, tostr(i)) : string();
85 string const write_attribute(string const & name, LyXLength const & value)
87 // we write only the value if we really have one same reson as above.
88 return value.zero() ? string() : write_attribute(name, value.asString());
92 string const tostr(LyXAlignment const & num)
101 case LYX_ALIGN_CENTER:
103 case LYX_ALIGN_RIGHT:
105 case LYX_ALIGN_LAYOUT:
107 case LYX_ALIGN_SPECIAL:
114 string const tostr(LyXTabular::VAlignment const & num)
117 case LyXTabular::LYX_VALIGN_TOP:
119 case LyXTabular::LYX_VALIGN_MIDDLE:
121 case LyXTabular::LYX_VALIGN_BOTTOM:
128 string const tostr(LyXTabular::BoxType const & num)
131 case LyXTabular::BOX_NONE:
133 case LyXTabular::BOX_PARBOX:
135 case LyXTabular::BOX_MINIPAGE:
141 // I would have liked a fromstr template a lot better. (Lgb)
142 bool string2type(string const str, LyXAlignment & num)
145 num = LYX_ALIGN_NONE;
146 else if (str == "block")
147 num = LYX_ALIGN_BLOCK;
148 else if (str == "left")
149 num = LYX_ALIGN_LEFT;
150 else if (str == "center")
151 num = LYX_ALIGN_CENTER;
152 else if (str == "right")
153 num = LYX_ALIGN_RIGHT;
160 bool string2type(string const str, LyXTabular::VAlignment & num)
163 num = LyXTabular::LYX_VALIGN_TOP;
164 else if (str == "middle" )
165 num = LyXTabular::LYX_VALIGN_MIDDLE;
166 else if (str == "bottom")
167 num = LyXTabular::LYX_VALIGN_BOTTOM;
174 bool string2type(string const str, LyXTabular::BoxType & num)
177 num = LyXTabular::BOX_NONE;
178 else if (str == "parbox")
179 num = LyXTabular::BOX_PARBOX;
180 else if (str == "minipage")
181 num = LyXTabular::BOX_MINIPAGE;
188 bool string2type(string const str, bool & num)
192 else if (str == "false")
200 bool getTokenValue(string const & str, const char * token, string & ret)
203 size_t token_length = strlen(token);
204 string::size_type pos = str.find(token);
206 if (pos == string::npos || pos + token_length + 1 >= str.length()
207 || str[pos + token_length] != '=')
209 pos += token_length + 1;
211 if ((ch != '"') && (ch != '\'')) { // only read till next space
215 while ((pos < str.length() - 1) && (str[++pos] != ch))
222 bool getTokenValue(string const & str, const char * token, int & num)
226 if (!getTokenValue(str, token, tmp))
233 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
236 if (!getTokenValue(str, token, tmp))
238 return string2type(tmp, num);
242 bool getTokenValue(string const & str, const char * token,
243 LyXTabular::VAlignment & num)
246 if (!getTokenValue(str, token, tmp))
248 return string2type(tmp, num);
252 bool getTokenValue(string const & str, const char * token,
253 LyXTabular::BoxType & num)
256 if (!getTokenValue(str, token, tmp))
258 return string2type(tmp, num);
262 bool getTokenValue(string const & str, const char * token, bool & flag)
264 // set the flag always to false as this should be the default for bools
265 // not in the file-format.
268 if (!getTokenValue(str, token, tmp))
270 return string2type(tmp, flag);
274 bool getTokenValue(string const & str, const char * token, LyXLength & len)
276 // set the lenght to be zero() as default as this it should be if not
277 // in the file format.
280 if (!getTokenValue(str, token, tmp))
282 return isValidLength(tmp, &len);
286 void l_getline(istream & is, string & str)
289 while (str.empty()) {
291 if (!str.empty() && str[str.length() - 1] == '\r')
292 str.erase(str.length() - 1);
298 /// Define a few methods for the inner structs
300 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
305 multicolumn = LyXTabular::CELL_NORMAL;
306 alignment = LYX_ALIGN_CENTER;
307 valignment = LYX_VALIGN_TOP;
317 LyXTabular::rowstruct::rowstruct()
324 endfirsthead = false;
331 LyXTabular::columnstruct::columnstruct()
335 alignment = LYX_ALIGN_CENTER;
336 valignment = LYX_VALIGN_TOP;
341 LyXTabular::ltType::ltType()
349 LyXTabular::LyXTabular(BufferParams const & bp,
350 InsetTabular * inset, int rows_arg, int columns_arg)
354 init(bp, rows_arg, columns_arg);
358 LyXTabular::LyXTabular(BufferParams const & bp,
359 InsetTabular * inset, LyXTabular const & lt)
363 init(bp, lt.rows_, lt.columns_, <);
367 LyXTabular::LyXTabular(Buffer const & buf, InsetTabular * inset, LyXLex & lex)
375 // activates all lines and sets all widths to 0
376 void LyXTabular::init(BufferParams const & bp,
377 int rows_arg, int columns_arg, LyXTabular const * lt)
380 columns_ = columns_arg;
381 row_info = row_vector(rows_, rowstruct());
382 column_info = column_vector(columns_, columnstruct());
383 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
391 for (int i = 0; i < rows_; ++i) {
392 for (int j = 0; j < columns_; ++j) {
393 cell_info[i][j].inset.setOwner(owner_);
394 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
395 cell_info[i][j].cellno = cellno++;
397 cell_info[i].back().right_line = true;
399 row_info.back().bottom_line = true;
400 row_info.front().bottom_line = true;
402 for (int i = 0; i < columns_; ++i)
403 calculate_width_of_column(i);
405 column_info.back().right_line = true;
407 calculate_width_of_tabular();
410 columnofcell.clear();
411 set_row_column_number_info();
412 is_long_tabular = false;
417 void LyXTabular::appendRow(BufferParams const & bp, int cell)
421 int row = row_of_cell(cell);
423 row_vector::iterator rit = row_info.begin() + row;
424 row_info.insert(rit, rowstruct());
425 // now set the values of the row before
426 row_info[row] = row_info[row + 1];
429 cell_vvector::iterator cit = cell_info.begin() + row;
430 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
432 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
435 for (int i = 0; i <= row; ++i)
436 for (int j = 0; j < columns_; ++j)
437 c_info[i][j] = cell_info[i][j];
439 for (int i = row + 1; i < rows_; ++i)
440 for (int j = 0; j < columns_; ++j)
441 c_info[i][j] = cell_info[i-1][j];
445 for (int j = 0; j < columns_; ++j) {
446 cell_info[row][j].inset.clear(false);
447 if (bp.tracking_changes)
448 cell_info[row][j].inset.markNew(true);
455 void LyXTabular::deleteRow(int row)
457 // Not allowed to delete last row
461 row_info.erase(row_info.begin() + row);
462 cell_info.erase(cell_info.begin() + row);
468 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
472 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
474 int const column = column_of_cell(cell);
475 column_vector::iterator cit = column_info.begin() + column + 1;
476 column_info.insert(cit, columnstruct());
477 // set the column values of the column before
478 column_info[column + 1] = column_info[column];
480 for (int i = 0; i < rows_; ++i) {
481 for (int j = 0; j <= column; ++j)
482 c_info[i][j] = cell_info[i][j];
484 for (int j = column + 1; j < columns_; ++j)
485 c_info[i][j] = cell_info[i][j - 1];
487 // care about multicolumns
488 if (c_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
489 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
491 if (column + 2 >= columns_
492 || c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
493 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
497 for (int i = 0; i < rows_; ++i) {
498 cell_info[i][column + 1].inset.clear(false);
499 if (bp.tracking_changes)
500 cell_info[i][column + 1].inset.markNew(true);
506 void LyXTabular::deleteColumn(int column)
508 // Not allowed to delete last column
512 column_info.erase(column_info.begin() + column);
513 for (int i = 0; i < rows_; ++i)
514 cell_info[i].erase(cell_info[i].begin() + column);
520 void LyXTabular::reinit()
526 void LyXTabular::Reinit(bool reset_widths)
529 for (int i = 0; i < rows_; ++i) {
530 for (int j = 0; j < columns_; ++j) {
531 cell_info[i][j].width_of_cell = 0;
532 cell_info[i][j].inset.setOwner(owner_);
537 for (int i = 0; i < columns_; ++i)
538 calculate_width_of_column(i);
540 calculate_width_of_tabular();
542 set_row_column_number_info();
546 void LyXTabular::set_row_column_number_info(bool oldformat)
549 for (int row = 0; row < rows_; ++row) {
550 for (int column = 0; column < columns_; ++column) {
551 if (cell_info[row][column].multicolumn
552 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
554 cell_info[row][column].cellno = numberofcells;
557 ++numberofcells; // because this is one more than as we start from 0
559 rowofcell.resize(numberofcells);
560 columnofcell.resize(numberofcells);
562 for (int row = 0, column = 0, c = 0;
563 c < numberofcells && row < rows_ && column < columns_;) {
565 columnofcell[c] = column;
569 } while (column < columns_ &&
570 cell_info[row][column].multicolumn
571 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
573 if (column == columns_) {
579 for (int row = 0; row < rows_; ++row) {
580 for (int column = 0; column < columns_; ++column) {
581 if (isPartOfMultiColumn(row,column))
583 // now set the right line of multicolumns right for oldformat read
585 cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
587 int cn = cells_in_multicolumn(cell_info[row][column].cellno);
588 cell_info[row][column].right_line =
589 cell_info[row][column+cn-1].right_line;
591 cell_info[row][column].inset.setAutoBreakRows(
592 !getPWidth(getCellNumber(row, column)).zero());
598 int LyXTabular::getNumberOfCells() const
600 return numberofcells;
604 int LyXTabular::numberOfCellsInRow(int cell) const
606 int const row = row_of_cell(cell);
608 for (int i = 0; i < columns_; ++i)
609 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
615 // returns 1 if there is a topline, returns 0 if not
616 bool LyXTabular::topLine(int cell, bool onlycolumn) const
618 if (!onlycolumn && isMultiColumn(cell))
619 return cellinfo_of_cell(cell).top_line;
620 return row_info[row_of_cell(cell)].top_line;
624 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
626 if (!onlycolumn && isMultiColumn(cell))
627 return cellinfo_of_cell(cell).bottom_line;
628 return row_info[row_of_cell(cell)].bottom_line;
632 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
634 if (!onlycolumn && isMultiColumn(cell) &&
635 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
637 if (cellinfo_of_cell(cell).align_special.empty())
638 return cellinfo_of_cell(cell).left_line;
639 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
641 if (column_info[column_of_cell(cell)].align_special.empty())
642 return column_info[column_of_cell(cell)].left_line;
643 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
647 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
649 if (!onlycolumn && isMultiColumn(cell) &&
650 (isLastCellInRow(cell) || isMultiColumn(cell+1)))
652 if (cellinfo_of_cell(cell).align_special.empty())
653 return cellinfo_of_cell(cell).right_line;
654 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
656 if (column_info[column_of_cell(cell)].align_special.empty())
657 return column_info[right_column_of_cell(cell)].right_line;
658 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
662 bool LyXTabular::topAlreadyDrawn(int cell) const
664 int row = row_of_cell(cell);
665 if (row > 0 && !getAdditionalHeight(row)) {
666 int column = column_of_cell(cell);
669 && cell_info[row][column].multicolumn
670 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
672 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
673 return row_info[row].bottom_line;
675 return cell_info[row][column].bottom_line;
681 bool LyXTabular::leftAlreadyDrawn(int cell) const
683 int column = column_of_cell(cell);
685 int row = row_of_cell(cell);
687 (cell_info[row][column].multicolumn ==
688 LyXTabular::CELL_PART_OF_MULTICOLUMN));
689 if (getAdditionalWidth(cell_info[row][column].cellno))
691 return rightLine(cell_info[row][column].cellno);
697 bool LyXTabular::isLastRow(int cell) const
699 return (row_of_cell(cell) == rows_ - 1);
703 int LyXTabular::getAdditionalHeight(int row) const
705 if (!row || row >= rows_)
711 for (int column = 0; column < columns_ && bottom; ++column) {
712 switch (cell_info[row - 1][column].multicolumn) {
713 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
714 bottom = cell_info[row - 1][column].bottom_line;
716 case LyXTabular::CELL_NORMAL:
717 bottom = row_info[row - 1].bottom_line;
720 for (int column = 0; column < columns_ && top; ++column) {
721 switch (cell_info[row][column].multicolumn) {
722 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
723 top = cell_info[row][column].top_line;
725 case LyXTabular::CELL_NORMAL:
726 top = row_info[row].top_line;
730 return WIDTH_OF_LINE;
735 int LyXTabular::getAdditionalWidth(int cell) const
737 // internally already set in setWidthOfCell
738 // used to get it back in text.C
739 int const col = right_column_of_cell(cell);
740 int const row = row_of_cell(cell);
741 if (col < columns_ - 1 && rightLine(cell) &&
742 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
744 return WIDTH_OF_LINE;
750 // returns the maximum over all rows
751 int LyXTabular::getWidthOfColumn(int cell) const
753 int const column1 = column_of_cell(cell);
754 int const column2 = right_column_of_cell(cell);
756 for (int i = column1; i <= column2; ++i)
757 result += column_info[i].width_of_column;
762 int LyXTabular::getWidthOfTabular() const
764 return width_of_tabular;
768 // returns true if a complete update is necessary, otherwise false
769 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
771 if (!isMultiColumn(cell))
774 int const row = row_of_cell(cell);
775 int const column1 = column_of_cell(cell);
776 int const column2 = right_column_of_cell(cell);
777 int const old_val = cell_info[row][column2].width_of_cell;
779 // first set columns to 0 so we can calculate the right width
780 for (int i = column1; i <= column2; ++i) {
781 cell_info[row][i].width_of_cell = 0;
783 // set the width to MAX_WIDTH until width > 0
784 int width = new_width + 2 * WIDTH_OF_LINE;
786 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
787 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
788 width -= column_info[i].width_of_column;
791 cell_info[row][i].width_of_cell = width;
793 if (old_val != cell_info[row][column2].width_of_cell) {
794 // in this case we have to recalculate all multicolumn cells which
795 // have this column as one of theirs but not as last one
796 calculate_width_of_column_NMC(i);
797 recalculateMulticolumnsOfColumn(i);
798 calculate_width_of_column(i);
804 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
806 // the last column does not have to be recalculated because all
807 // multicolumns will have here there last multicolumn cell which
808 // always will have the whole rest of the width of the cell.
809 if (column > (columns_ - 2))
811 for(int row = 0; row < rows_; ++row) {
812 int mc = cell_info[row][column].multicolumn;
813 int nmc = cell_info[row][column+1].multicolumn;
814 // we only have to update multicolumns which do not have this
815 // column as their last column!
816 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
817 (mc == CELL_PART_OF_MULTICOLUMN &&
818 nmc == CELL_PART_OF_MULTICOLUMN))
820 int const cellno = cell_info[row][column].cellno;
821 setWidthOfMulticolCell(cellno,
822 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
828 // returns 1 if a complete update is necessary, otherwise 0
829 void LyXTabular::setWidthOfCell(int cell, int new_width)
831 int const row = row_of_cell(cell);
832 int const column1 = column_of_cell(cell);
837 if (rightLine(cell_info[row][column1].cellno, true) &&
838 column1 < columns_ - 1 &&
839 leftLine(cell_info[row][column1+1].cellno, true))
841 add_width = WIDTH_OF_LINE;
844 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
847 if (isMultiColumn(cell, true)) {
848 tmp = setWidthOfMulticolCell(cell, new_width);
850 width = new_width + 2 * WIDTH_OF_LINE + add_width;
851 cell_info[row][column1].width_of_cell = width;
852 tmp = calculate_width_of_column_NMC(column1);
854 recalculateMulticolumnsOfColumn(column1);
857 for (int i = 0; i < columns_; ++i)
858 calculate_width_of_column(i);
859 calculate_width_of_tabular();
864 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
866 if (!isMultiColumn(cell) || onlycolumn)
867 column_info[column_of_cell(cell)].alignment = align;
869 cellinfo_of_cell(cell).alignment = align;
873 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
875 if (!isMultiColumn(cell) || onlycolumn)
876 column_info[column_of_cell(cell)].valignment = align;
878 cellinfo_of_cell(cell).valignment = align;
882 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
884 int const j = column_of_cell(cell);
886 column_info[j].p_width = width;
887 for (int i = 0; i < rows_; ++i) {
888 int const cell = getCellNumber(i, j);
889 // because of multicolumns
890 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
895 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
897 bool const flag = !width.zero();
899 cellinfo_of_cell(cell).p_width = width;
900 if (isMultiColumn(cell)) {
901 getCellInset(cell).setAutoBreakRows(flag);
908 void LyXTabular::setAlignSpecial(int cell, string const & special,
909 LyXTabular::Feature what)
911 if (what == SET_SPECIAL_MULTI)
912 cellinfo_of_cell(cell).align_special = special;
914 column_info[column_of_cell(cell)].align_special = special;
918 void LyXTabular::setAllLines(int cell, bool line)
920 setTopLine(cell, line);
921 setBottomLine(cell, line);
922 setRightLine(cell, line);
923 setLeftLine(cell, line);
927 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
929 int const row = row_of_cell(cell);
930 if (onlycolumn || !isMultiColumn(cell))
931 row_info[row].top_line = line;
933 cellinfo_of_cell(cell).top_line = line;
937 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
939 if (onlycolumn || !isMultiColumn(cell))
940 row_info[row_of_cell(cell)].bottom_line = line;
942 cellinfo_of_cell(cell).bottom_line = line;
946 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
948 if (onlycolumn || !isMultiColumn(cell))
949 column_info[column_of_cell(cell)].left_line = line;
951 cellinfo_of_cell(cell).left_line = line;
955 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
957 if (onlycolumn || !isMultiColumn(cell))
958 column_info[right_column_of_cell(cell)].right_line = line;
960 cellinfo_of_cell(cell).right_line = line;
964 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
966 if (!onlycolumn && isMultiColumn(cell))
967 return cellinfo_of_cell(cell).alignment;
968 return column_info[column_of_cell(cell)].alignment;
972 LyXTabular::VAlignment
973 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
975 if (!onlycolumn && isMultiColumn(cell))
976 return cellinfo_of_cell(cell).valignment;
977 return column_info[column_of_cell(cell)].valignment;
981 LyXLength const LyXTabular::getPWidth(int cell) const
983 if (isMultiColumn(cell))
984 return cellinfo_of_cell(cell).p_width;
985 return column_info[column_of_cell(cell)].p_width;
989 LyXLength const LyXTabular::getColumnPWidth(int cell) const
991 return column_info[column_of_cell(cell)].p_width;
995 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
997 if (isMultiColumn(cell))
998 return cellinfo_of_cell(cell).p_width;
1003 string const LyXTabular::getAlignSpecial(int cell, int what) const
1005 if (what == SET_SPECIAL_MULTI)
1006 return cellinfo_of_cell(cell).align_special;
1007 return column_info[column_of_cell(cell)].align_special;
1011 int LyXTabular::getWidthOfCell(int cell) const
1013 int const row = row_of_cell(cell);
1014 int const column1 = column_of_cell(cell);
1015 int const column2 = right_column_of_cell(cell);
1017 for (int i = column1; i <= column2; ++i)
1018 result += cell_info[row][i].width_of_cell;
1023 int LyXTabular::getBeginningOfTextInCell(int cell) const
1027 switch (getAlignment(cell)) {
1028 case LYX_ALIGN_CENTER:
1029 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1031 case LYX_ALIGN_RIGHT:
1032 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1033 // + getAdditionalWidth(cell);
1036 // LYX_ALIGN_LEFT: nothing :-)
1040 // the LaTeX Way :-(
1046 bool LyXTabular::isFirstCellInRow(int cell) const
1048 return column_of_cell(cell) == 0;
1052 int LyXTabular::getFirstCellInRow(int row) const
1054 if (row > rows_ - 1)
1056 return cell_info[row][0].cellno;
1060 bool LyXTabular::isLastCellInRow(int cell) const
1062 return right_column_of_cell(cell) == columns_ - 1;
1066 int LyXTabular::getLastCellInRow(int row) const
1068 if (row > rows_ - 1)
1070 return cell_info[row][columns_-1].cellno;
1074 void LyXTabular::calculate_width_of_column(int column)
1077 for (int i = 0; i < rows_; ++i)
1078 maximum = max(cell_info[i][column].width_of_cell, maximum);
1079 column_info[column].width_of_column = maximum;
1084 // Calculate the columns regarding ONLY the normal cells and if this
1085 // column is inside a multicolumn cell then use it only if its the last
1086 // column of this multicolumn cell as this gives an added width to the
1087 // column, all the rest should be adapted!
1089 bool LyXTabular::calculate_width_of_column_NMC(int column)
1091 int const old_column_width = column_info[column].width_of_column;
1093 for (int i = 0; i < rows_; ++i) {
1094 int cell = getCellNumber(i, column);
1095 bool ismulti = isMultiColumn(cell, true);
1096 if ((!ismulti || column == right_column_of_cell(cell)) &&
1097 cell_info[i][column].width_of_cell > max)
1099 max = cell_info[i][column].width_of_cell;
1102 column_info[column].width_of_column = max;
1103 return column_info[column].width_of_column != old_column_width;
1107 void LyXTabular::calculate_width_of_tabular()
1109 width_of_tabular = 0;
1110 for (int i = 0; i < columns_; ++i)
1111 width_of_tabular += column_info[i].width_of_column;
1115 int LyXTabular::row_of_cell(int cell) const
1117 if (cell >= numberofcells)
1121 return rowofcell[cell];
1125 int LyXTabular::column_of_cell(int cell) const
1127 if (cell >= numberofcells)
1128 return columns_ - 1;
1131 return columnofcell[cell];
1135 int LyXTabular::right_column_of_cell(int cell) const
1137 int const row = row_of_cell(cell);
1138 int column = column_of_cell(cell);
1139 while (column < columns_ - 1 &&
1140 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1146 void LyXTabular::write(Buffer const & buf, ostream & os) const
1150 << write_attribute("version", 3)
1151 << write_attribute("rows", rows_)
1152 << write_attribute("columns", columns_)
1154 // global longtable options
1156 << write_attribute("rotate", rotate)
1157 << write_attribute("islongtable", is_long_tabular)
1158 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1159 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1160 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1161 << write_attribute("headTopDL", endhead.topDL)
1162 << write_attribute("headBottomDL", endhead.bottomDL)
1163 << write_attribute("footTopDL", endfoot.topDL)
1164 << write_attribute("footBottomDL", endfoot.bottomDL)
1165 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1166 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1167 << write_attribute("lastFootEmpty", endlastfoot.empty)
1169 for (int j = 0; j < columns_; ++j) {
1171 << write_attribute("alignment", column_info[j].alignment)
1172 << write_attribute("valignment", column_info[j].valignment)
1173 << write_attribute("leftline", column_info[j].left_line)
1174 << write_attribute("rightline", column_info[j].right_line)
1175 << write_attribute("width", column_info[j].p_width.asString())
1176 << write_attribute("special", column_info[j].align_special)
1179 for (int i = 0; i < rows_; ++i) {
1181 << write_attribute("topline", row_info[i].top_line)
1182 << write_attribute("bottomline", row_info[i].bottom_line)
1183 << write_attribute("endhead", row_info[i].endhead)
1184 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1185 << write_attribute("endfoot", row_info[i].endfoot)
1186 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1187 << write_attribute("newpage", row_info[i].newpage)
1189 for (int j = 0; j < columns_; ++j) {
1191 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1192 << write_attribute("alignment", cell_info[i][j].alignment)
1193 << write_attribute("valignment", cell_info[i][j].valignment)
1194 << write_attribute("topline", cell_info[i][j].top_line)
1195 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1196 << write_attribute("leftline", cell_info[i][j].left_line)
1197 << write_attribute("rightline", cell_info[i][j].right_line)
1198 << write_attribute("rotate", cell_info[i][j].rotate)
1199 << write_attribute("usebox", cell_info[i][j].usebox)
1200 << write_attribute("width", cell_info[i][j].p_width)
1201 << write_attribute("special", cell_info[i][j].align_special)
1203 os << "\\begin_inset ";
1204 cell_info[i][j].inset.write(buf, os);
1205 os << "\n\\end_inset \n"
1210 os << "</lyxtabular>\n";
1214 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1218 row_info[--hr].endhead = true;
1220 // set firstheader info
1221 if (fhr && fhr < rows_) {
1222 if (row_info[fhr].endhead) {
1224 row_info[--fhr].endfirsthead = true;
1225 row_info[fhr].endhead = false;
1227 } else if (row_info[fhr - 1].endhead) {
1228 endfirsthead.empty = true;
1230 while (fhr > 0 && !row_info[--fhr].endhead) {
1231 row_info[fhr].endfirsthead = true;
1236 if (fr && fr < rows_) {
1237 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1238 while (fr > 0 && !row_info[--fr].endhead) {
1239 row_info[fr].endfoot = true;
1240 row_info[fr].endhead = false;
1242 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1243 while (fr > 0 && !row_info[--fr].endfirsthead) {
1244 row_info[fr].endfoot = true;
1245 row_info[fr].endfirsthead = false;
1247 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1248 while (fr > 0 && !row_info[--fr].endhead &&
1249 !row_info[fr].endfirsthead)
1251 row_info[fr].endfoot = true;
1255 // set lastfooter info
1256 if (lfr && lfr < rows_) {
1257 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1258 while (lfr > 0 && !row_info[--lfr].endhead) {
1259 row_info[lfr].endlastfoot = true;
1260 row_info[lfr].endhead = false;
1262 } else if (row_info[lfr].endfirsthead &&
1263 row_info[lfr - 1].endfirsthead)
1265 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1266 row_info[lfr].endlastfoot = true;
1267 row_info[lfr].endfirsthead = false;
1269 } else if (row_info[lfr].endfoot
1270 && row_info[lfr - 1].endfoot) {
1271 while (lfr > 0 && !row_info[--lfr].endfoot) {
1272 row_info[lfr].endlastfoot = true;
1273 row_info[lfr].endfoot = false;
1275 } else if (!row_info[fr - 1].endhead
1276 && !row_info[fr - 1].endfirsthead &&
1277 !row_info[fr - 1].endfoot)
1280 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1281 !row_info[lfr].endfoot)
1283 row_info[lfr].endlastfoot = true;
1285 } else if (haveLTFoot()) {
1286 endlastfoot.empty = true;
1292 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1295 istream & is = lex.getStream();
1297 l_getline(is, line);
1298 if (!prefixIs(line, "<lyxtabular ")
1299 && !prefixIs(line, "<LyXTabular ")) {
1305 if (!getTokenValue(line, "version", version))
1307 Assert(version >= 2);
1310 if (!getTokenValue(line, "rows", rows_arg))
1313 if (!getTokenValue(line, "columns", columns_arg))
1315 init(buf.params, rows_arg, columns_arg);
1316 l_getline(is, line);
1317 if (!prefixIs(line, "<features")) {
1318 lyxerr << "Wrong tabular format (expected <features ...> got"
1319 << line << ')' << endl;
1322 getTokenValue(line, "rotate", rotate);
1323 getTokenValue(line, "islongtable", is_long_tabular);
1324 // compatibility read for old longtable options. Now we can make any
1325 // row part of the header/footer type we want before it was strict
1326 // sequential from the first row down (as LaTeX does it!). So now when
1327 // we find a header/footer line we have to go up the rows and set it
1328 // on all preceding rows till the first or one with already a h/f option
1329 // set. If we find a firstheader on the same line as a header or a
1330 // lastfooter on the same line as a footer then this should be set empty.
1338 getTokenValue(line, "endhead", hrow);
1339 getTokenValue(line, "endfirsthead", fhrow);
1340 getTokenValue(line, "endfoot", frow);
1341 getTokenValue(line, "endlastfoot", lfrow);
1342 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1344 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1345 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1346 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1347 getTokenValue(line, "headTopDL", endhead.topDL);
1348 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1349 getTokenValue(line, "footTopDL", endfoot.topDL);
1350 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1351 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1352 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1353 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1355 for (int j = 0; j < columns_; ++j) {
1357 if (!prefixIs(line,"<column")) {
1358 lyxerr << "Wrong tabular format (expected <column ...> got"
1359 << line << ')' << endl;
1362 getTokenValue(line, "alignment", column_info[j].alignment);
1363 getTokenValue(line, "valignment", column_info[j].valignment);
1364 getTokenValue(line, "leftline", column_info[j].left_line);
1365 getTokenValue(line, "rightline", column_info[j].right_line);
1366 getTokenValue(line, "width", column_info[j].p_width);
1367 getTokenValue(line, "special", column_info[j].align_special);
1370 for (int i = 0; i < rows_; ++i) {
1371 l_getline(is, line);
1372 if (!prefixIs(line, "<row")) {
1373 lyxerr << "Wrong tabular format (expected <row ...> got"
1374 << line << ')' << endl;
1377 getTokenValue(line, "topline", row_info[i].top_line);
1378 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1379 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1380 getTokenValue(line, "endhead", row_info[i].endhead);
1381 getTokenValue(line, "endfoot", row_info[i].endfoot);
1382 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1383 getTokenValue(line, "newpage", row_info[i].newpage);
1384 for (int j = 0; j < columns_; ++j) {
1385 l_getline(is, line);
1386 if (!prefixIs(line, "<cell")) {
1387 lyxerr << "Wrong tabular format (expected <cell ...> got"
1388 << line << ')' << endl;
1391 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1392 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1393 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1394 getTokenValue(line, "topline", cell_info[i][j].top_line);
1395 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1396 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1397 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1398 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1399 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1400 getTokenValue(line, "width", cell_info[i][j].p_width);
1401 getTokenValue(line, "special", cell_info[i][j].align_special);
1402 l_getline(is, line);
1403 if (prefixIs(line, "\\begin_inset")) {
1404 cell_info[i][j].inset.read(buf, lex);
1405 l_getline(is, line);
1407 if (!prefixIs(line, "</cell>")) {
1408 lyxerr << "Wrong tabular format (expected </cell> got"
1409 << line << ')' << endl;
1413 l_getline(is, line);
1414 if (!prefixIs(line, "</row>")) {
1415 lyxerr << "Wrong tabular format (expected </row> got"
1416 << line << ')' << endl;
1420 while (!prefixIs(line, "</lyxtabular>")) {
1421 l_getline(is, line);
1423 set_row_column_number_info();
1427 bool LyXTabular::isMultiColumn(int cell, bool real) const
1429 return (!real || column_of_cell(cell) != right_column_of_cell(cell)) &&
1430 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1434 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1436 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1440 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1442 cellstruct & cs = cellinfo_of_cell(cell);
1443 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1444 cs.alignment = column_info[column_of_cell(cell)].alignment;
1445 cs.top_line = row_info[row_of_cell(cell)].top_line;
1446 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1447 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1448 for (int i = 1; i < number; ++i) {
1449 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1450 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1451 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1452 cs1.inset.clear(false);
1454 set_row_column_number_info();
1458 int LyXTabular::cells_in_multicolumn(int cell) const
1460 int const row = row_of_cell(cell);
1461 int column = column_of_cell(cell);
1464 while (column < columns_ &&
1465 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1474 int LyXTabular::unsetMultiColumn(int cell)
1476 int const row = row_of_cell(cell);
1477 int column = column_of_cell(cell);
1481 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1482 cell_info[row][column].multicolumn = CELL_NORMAL;
1484 while (column < columns_ &&
1485 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1487 cell_info[row][column].multicolumn = CELL_NORMAL;
1492 set_row_column_number_info();
1497 void LyXTabular::setLongTabular(bool what)
1499 is_long_tabular = what;
1503 bool LyXTabular::isLongTabular() const
1505 return is_long_tabular;
1509 void LyXTabular::setRotateTabular(bool flag)
1515 bool LyXTabular::getRotateTabular() const
1521 void LyXTabular::setRotateCell(int cell, bool flag)
1523 cellinfo_of_cell(cell).rotate = flag;
1527 bool LyXTabular::getRotateCell(int cell) const
1529 return cellinfo_of_cell(cell).rotate;
1533 bool LyXTabular::needRotating() const
1537 for (int i = 0; i < rows_; ++i)
1538 for (int j = 0; j < columns_; ++j)
1539 if (cell_info[i][j].rotate)
1545 bool LyXTabular::isLastCell(int cell) const
1547 if (cell + 1 < numberofcells)
1553 int LyXTabular::getCellAbove(int cell) const
1555 if (row_of_cell(cell) > 0)
1556 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1561 int LyXTabular::getCellBelow(int cell) const
1563 if (row_of_cell(cell) + 1 < rows_)
1564 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1569 int LyXTabular::getLastCellAbove(int cell) const
1571 if (row_of_cell(cell) <= 0)
1573 if (!isMultiColumn(cell))
1574 return getCellAbove(cell);
1575 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1579 int LyXTabular::getLastCellBelow(int cell) const
1581 if (row_of_cell(cell) + 1 >= rows_)
1583 if (!isMultiColumn(cell))
1584 return getCellBelow(cell);
1585 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1589 int LyXTabular::getCellNumber(int row, int column) const
1591 Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1592 return cell_info[row][column].cellno;
1596 void LyXTabular::setUsebox(int cell, BoxType type)
1598 cellinfo_of_cell(cell).usebox = type;
1602 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1604 if (column_info[column_of_cell(cell)].p_width.zero() &&
1605 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1607 if (cellinfo_of_cell(cell).usebox > 1)
1608 return cellinfo_of_cell(cell).usebox;
1609 return useParbox(cell);
1614 // This are functions used for the longtable support
1616 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1621 row_info[row].endfirsthead = flag;
1625 row_info[row].endhead = flag;
1630 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1633 hd.set = haveLTHead();
1634 return row_info[row].endhead;
1638 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1641 hd.set = haveLTFirstHead();
1642 return row_info[row].endfirsthead;
1646 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1651 row_info[row].endlastfoot = flag;
1655 row_info[row].endfoot = flag;
1660 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1663 fd.set = haveLTFoot();
1664 return row_info[row].endfoot;
1668 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1671 fd.set = haveLTLastFoot();
1672 return row_info[row].endlastfoot;
1676 void LyXTabular::setLTNewPage(int row, bool what)
1678 row_info[row].newpage = what;
1682 bool LyXTabular::getLTNewPage(int row) const
1684 return row_info[row].newpage;
1688 bool LyXTabular::haveLTHead() const
1690 for (int i = 0; i < rows_; ++i)
1691 if (row_info[i].endhead)
1697 bool LyXTabular::haveLTFirstHead() const
1699 if (endfirsthead.empty)
1701 for (int i = 0; i < rows_; ++i)
1702 if (row_info[i].endfirsthead)
1708 bool LyXTabular::haveLTFoot() const
1710 for (int i = 0; i < rows_; ++i)
1711 if (row_info[i].endfoot)
1717 bool LyXTabular::haveLTLastFoot() const
1719 if (endlastfoot.empty)
1721 for (int i = 0; i < rows_; ++i)
1722 if (row_info[i].endlastfoot)
1728 // end longtable support functions
1730 void LyXTabular::setAscentOfRow(int row, int height)
1732 if (row >= rows_ || row_info[row].ascent_of_row == height)
1734 row_info[row].ascent_of_row = height;
1738 void LyXTabular::setDescentOfRow(int row, int height)
1740 if (row >= rows_ || row_info[row].descent_of_row == height)
1742 row_info[row].descent_of_row = height;
1746 int LyXTabular::getAscentOfRow(int row) const
1750 return row_info[row].ascent_of_row;
1754 int LyXTabular::getDescentOfRow(int row) const
1758 return row_info[row].descent_of_row;
1762 int LyXTabular::getHeightOfTabular() const
1765 for (int row = 0; row < rows_; ++row)
1766 height += getAscentOfRow(row) + getDescentOfRow(row) +
1767 getAdditionalHeight(row);
1772 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1774 if (row >= rows_ || column >= columns_)
1776 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1780 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1782 if (row < 0 || row >= rows_)
1785 int const fcell = getFirstCellInRow(row);
1786 int const n = numberOfCellsInRow(fcell) + fcell;
1789 for (int i = fcell; i < n; ++i) {
1793 if (tmp == n - fcell) {
1796 for (int i = fcell; i < n; ++i) {
1799 << column_of_cell(i) + 1
1801 << right_column_of_cell(i) + 1
1813 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1815 if (row < 0 || row >= rows_)
1818 int const fcell = getFirstCellInRow(row);
1819 int const n = numberOfCellsInRow(fcell) + fcell;
1822 for (int i = fcell; i < n; ++i) {
1826 if (tmp == n - fcell) {
1829 for (int i = fcell; i < n; ++i) {
1830 if (bottomLine(i)) {
1832 << column_of_cell(i) + 1
1834 << right_column_of_cell(i) + 1
1846 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1850 if (getRotateCell(cell)) {
1851 os << "\\begin{sideways}\n";
1854 if (isMultiColumn(cell)) {
1855 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1856 if (!cellinfo_of_cell(cell).align_special.empty()) {
1857 os << cellinfo_of_cell(cell).align_special << "}{";
1859 if (leftLine(cell) &&
1860 (isFirstCellInRow(cell) ||
1861 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1862 !rightLine(cell - 1, true))))
1866 if (!getPWidth(cell).zero()) {
1867 switch (getVAlignment(cell)) {
1868 case LYX_VALIGN_TOP:
1871 case LYX_VALIGN_MIDDLE:
1874 case LYX_VALIGN_BOTTOM:
1879 << getPWidth(cell).asLatexString()
1882 switch (getAlignment(cell)) {
1883 case LYX_ALIGN_LEFT:
1886 case LYX_ALIGN_RIGHT:
1894 if (rightLine(cell))
1896 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1902 if (getUsebox(cell) == BOX_PARBOX) {
1904 switch (getVAlignment(cell)) {
1905 case LYX_VALIGN_TOP:
1908 case LYX_VALIGN_MIDDLE:
1911 case LYX_VALIGN_BOTTOM:
1915 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1916 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1917 os << "\\begin{minipage}[";
1918 switch (getVAlignment(cell)) {
1919 case LYX_VALIGN_TOP:
1922 case LYX_VALIGN_MIDDLE:
1925 case LYX_VALIGN_BOTTOM:
1929 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1936 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1941 if (getUsebox(cell) == BOX_PARBOX)
1943 else if (getUsebox(cell) == BOX_MINIPAGE) {
1944 os << "%\n\\end{minipage}";
1947 if (isMultiColumn(cell)) {
1950 if (getRotateCell(cell)) {
1951 os << "%\n\\end{sideways}";
1958 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1959 LatexRunParams const & runparams) const
1961 if (!is_long_tabular)
1965 // output header info
1967 if (endhead.topDL) {
1971 for (int i = 0; i < rows_; ++i) {
1972 if (row_info[i].endhead) {
1973 ret += TeXRow(os, i, buf, runparams);
1976 if (endhead.bottomDL) {
1980 os << "\\endhead\n";
1982 if (endfirsthead.empty) {
1983 os << "\\endfirsthead\n";
1987 // output firstheader info
1988 if (haveLTFirstHead()) {
1989 if (endfirsthead.topDL) {
1993 for (int i = 0; i < rows_; ++i) {
1994 if (row_info[i].endfirsthead) {
1995 ret += TeXRow(os, i, buf, runparams);
1998 if (endfirsthead.bottomDL) {
2002 os << "\\endfirsthead\n";
2005 // output footer info
2007 if (endfoot.topDL) {
2011 for (int i = 0; i < rows_; ++i) {
2012 if (row_info[i].endfoot) {
2013 ret += TeXRow(os, i, buf, runparams);
2016 if (endfoot.bottomDL) {
2020 os << "\\endfoot\n";
2022 if (endlastfoot.empty) {
2023 os << "\\endlastfoot\n";
2027 // output lastfooter info
2028 if (haveLTLastFoot()) {
2029 if (endlastfoot.topDL) {
2033 for (int i = 0; i < rows_; ++i) {
2034 if (row_info[i].endlastfoot) {
2035 ret += TeXRow(os, i, buf, runparams);
2038 if (endlastfoot.bottomDL) {
2042 os << "\\endlastfoot\n";
2049 bool LyXTabular::isValidRow(int const row) const
2051 if (!is_long_tabular)
2053 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2054 !row_info[row].endfoot && !row_info[row].endlastfoot);
2058 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const & buf,
2059 LatexRunParams const & runparams) const
2062 int cell = getCellNumber(i, 0);
2064 ret += TeXTopHLine(os, i);
2065 for (int j = 0; j < columns_; ++j) {
2066 if (isPartOfMultiColumn(i, j))
2068 ret += TeXCellPreamble(os, cell);
2069 InsetText & inset = getCellInset(cell);
2071 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(buf.params) &&
2072 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2076 ret += inset.latex(buf, os, runparams);
2080 ret += TeXCellPostamble(os, cell);
2081 if (!isLastCellInRow(cell)) { // not last cell in row
2087 os << "\\tabularnewline\n";
2089 ret += TeXBottomHLine(os, i);
2094 int LyXTabular::latex(Buffer const & buf, ostream & os,
2095 LatexRunParams const & runparams) const
2099 //+---------------------------------------------------------------------
2100 //+ first the opening preamble +
2101 //+---------------------------------------------------------------------
2104 os << "\\begin{sideways}\n";
2107 if (is_long_tabular)
2108 os << "\\begin{longtable}{";
2110 os << "\\begin{tabular}{";
2111 for (int i = 0; i < columns_; ++i) {
2112 if (!column_info[i].align_special.empty()) {
2113 os << column_info[i].align_special;
2115 if (column_info[i].left_line)
2117 if (!column_info[i].p_width.zero()) {
2118 switch (column_info[i].alignment) {
2119 case LYX_ALIGN_LEFT:
2120 os << ">{\\raggedright}";
2122 case LYX_ALIGN_RIGHT:
2123 os << ">{\\raggedleft}";
2125 case LYX_ALIGN_CENTER:
2126 os << ">{\\centering}";
2128 case LYX_ALIGN_NONE:
2129 case LYX_ALIGN_BLOCK:
2130 case LYX_ALIGN_LAYOUT:
2131 case LYX_ALIGN_SPECIAL:
2135 switch (column_info[i].valignment) {
2136 case LYX_VALIGN_TOP:
2139 case LYX_VALIGN_MIDDLE:
2142 case LYX_VALIGN_BOTTOM:
2147 << column_info[i].p_width.asLatexString()
2150 switch (column_info[i].alignment) {
2151 case LYX_ALIGN_LEFT:
2154 case LYX_ALIGN_RIGHT:
2162 if (column_info[i].right_line)
2169 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2171 //+---------------------------------------------------------------------
2172 //+ the single row and columns (cells) +
2173 //+---------------------------------------------------------------------
2175 for (int i = 0; i < rows_; ++i) {
2176 if (isValidRow(i)) {
2177 ret += TeXRow(os, i, buf, runparams);
2178 if (is_long_tabular && row_info[i].newpage) {
2179 os << "\\newpage\n";
2185 //+---------------------------------------------------------------------
2186 //+ the closing of the tabular +
2187 //+---------------------------------------------------------------------
2189 if (is_long_tabular)
2190 os << "\\end{longtable}";
2192 os << "\\end{tabular}";
2194 os << "\n\\end{sideways}";
2202 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os) const
2204 os << "<tabular ca=\"";
2205 for (int i = 0; i < columns_; ++i) {
2206 switch (column_info[i].alignment) {
2207 case LYX_ALIGN_LEFT:
2210 case LYX_ALIGN_RIGHT:
2221 for (int i = 0; i < rows_; ++i) {
2222 for (int j = 0; j < columns_; ++j) {
2223 if (isPartOfMultiColumn(i, j))
2225 InsetText & inset = getCellInset(cell);
2227 ret += inset.linuxdoc(buf, os);
2229 if (isLastCellInRow(cell)) {
2238 os << "</tabular>\n";
2243 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row) const
2246 int cell = getFirstCellInRow(row);
2249 for (int j = 0; j < columns_; ++j) {
2250 if (isPartOfMultiColumn(row, j))
2253 os << "<entry align=\"";
2254 switch (getAlignment(cell)) {
2255 case LYX_ALIGN_LEFT:
2258 case LYX_ALIGN_RIGHT:
2266 os << "\" valign=\"";
2267 switch (getVAlignment(cell)) {
2268 case LYX_VALIGN_TOP:
2271 case LYX_VALIGN_BOTTOM:
2274 case LYX_VALIGN_MIDDLE:
2279 if (isMultiColumn(cell)) {
2280 os << " namest=\"col" << j << "\" ";
2281 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2285 ret += getCellInset(cell).docbook(buf, os, true);
2294 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2295 bool /*mixcont*/) const
2299 //+---------------------------------------------------------------------
2300 //+ first the opening preamble +
2301 //+---------------------------------------------------------------------
2303 os << "<tgroup cols=\"" << columns_
2304 << "\" colsep=\"1\" rowsep=\"1\">\n";
2306 for (int i = 0; i < columns_; ++i) {
2307 os << "<colspec colname=\"col" << i << "\" align=\"";
2308 switch (column_info[i].alignment) {
2309 case LYX_ALIGN_LEFT:
2312 case LYX_ALIGN_RIGHT:
2323 //+---------------------------------------------------------------------
2324 //+ Long Tabular case +
2325 //+---------------------------------------------------------------------
2327 // output header info
2328 if (haveLTHead() || haveLTFirstHead()) {
2331 for (int i = 0; i < rows_; ++i) {
2332 if (row_info[i].endhead || row_info[i].endfirsthead) {
2333 ret += docbookRow(buf, os, i);
2339 // output footer info
2340 if (haveLTFoot() || haveLTLastFoot()) {
2343 for (int i = 0; i < rows_; ++i) {
2344 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2345 ret += docbookRow(buf, os, i);
2352 //+---------------------------------------------------------------------
2353 //+ the single row and columns (cells) +
2354 //+---------------------------------------------------------------------
2358 for (int i = 0; i < rows_; ++i) {
2359 if (isValidRow(i)) {
2360 ret += docbookRow(buf, os, i);
2365 //+---------------------------------------------------------------------
2366 //+ the closing of the tabular +
2367 //+---------------------------------------------------------------------
2376 int LyXTabular::asciiTopHLine(ostream & os, int row,
2377 vector<unsigned int> const & clen) const
2379 int const fcell = getFirstCellInRow(row);
2380 int const n = numberOfCellsInRow(fcell) + fcell;
2383 for (int i = fcell; i < n; ++i) {
2393 for (int i = fcell; i < n; ++i) {
2404 int column = column_of_cell(i);
2405 int len = clen[column];
2406 while (isPartOfMultiColumn(row, ++column))
2407 len += clen[column] + 4;
2408 os << string(len, ch);
2423 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2424 vector<unsigned int> const & clen) const
2426 int const fcell = getFirstCellInRow(row);
2427 int const n = numberOfCellsInRow(fcell) + fcell;
2430 for (int i = fcell; i < n; ++i) {
2431 if (bottomLine(i)) {
2440 for (int i = fcell; i < n; ++i) {
2441 if (bottomLine(i)) {
2451 int column = column_of_cell(i);
2452 int len = clen[column];
2453 while (isPartOfMultiColumn(row, ++column))
2454 len += clen[column] + 4;
2455 os << string(len, ch);
2456 if (bottomLine(i)) {
2470 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2471 int cell, int row, int column,
2472 vector<unsigned int> const & clen,
2473 bool onlydata) const
2476 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2488 unsigned int len1 = sstr.str().length();
2489 unsigned int len2 = clen[column];
2490 while (isPartOfMultiColumn(row, ++column))
2491 len2 += clen[column] + 4;
2494 switch (getAlignment(cell)) {
2496 case LYX_ALIGN_LEFT:
2499 case LYX_ALIGN_RIGHT:
2503 case LYX_ALIGN_CENTER:
2509 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2511 if (rightLine(cell))
2520 int LyXTabular::ascii(Buffer const & buf, ostream & os, int const depth,
2521 bool onlydata, unsigned char delim) const
2525 // first calculate the width of the single columns
2526 vector<unsigned int> clen(columns_);
2529 // first all non (real) multicolumn cells!
2530 for (int j = 0; j < columns_; ++j) {
2532 for (int i = 0; i < rows_; ++i) {
2533 int cell = getCellNumber(i, j);
2534 if (isMultiColumn(cell, true))
2537 getCellInset(cell).ascii(buf, sstr, 0);
2538 if (clen[j] < sstr.str().length())
2539 clen[j] = sstr.str().length();
2542 // then all (real) multicolumn cells!
2543 for (int j = 0; j < columns_; ++j) {
2544 for (int i = 0; i < rows_; ++i) {
2545 int cell = getCellNumber(i, j);
2546 if (!isMultiColumn(cell, true) || isPartOfMultiColumn(i, j))
2549 getCellInset(cell).ascii(buf, sstr, 0);
2550 int len = int(sstr.str().length());
2551 int const n = cells_in_multicolumn(cell);
2552 for (int k = j; len > 0 && k < j + n - 1; ++k)
2554 if (len > int(clen[j + n - 1]))
2555 clen[j + n - 1] = len;
2560 for (int i = 0; i < rows_; ++i) {
2561 if (!onlydata && asciiTopHLine(os, i, clen))
2562 for (int j = 0; j < depth; ++j)
2564 for (int j = 0; j < columns_; ++j) {
2565 if (isPartOfMultiColumn(i, j))
2567 if (onlydata && j > 0)
2569 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2574 for (int j = 0; j < depth; ++j)
2576 if (asciiBottomHLine(os, i, clen))
2577 for (int j = 0; j < depth; ++j)
2585 InsetText & LyXTabular::getCellInset(int cell) const
2588 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2592 InsetText & LyXTabular::getCellInset(int row, int column) const
2594 cur_cell = getCellNumber(row, column);
2595 return cell_info[row][column].inset;
2599 int LyXTabular::getCellFromInset(InsetOld const * inset, int maybe_cell) const
2601 // is this inset part of the tabular?
2602 if (!inset || inset->owner() != owner_) {
2603 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2607 const int save_cur_cell = cur_cell;
2608 int cell = cur_cell;
2609 if (&getCellInset(cell) != inset) {
2611 if (cell == -1 || &getCellInset(cell) != inset)
2616 for (cell = getNumberOfCells(); cell >= 0; --cell)
2617 if (&getCellInset(cell) == inset)
2620 lyxerr[Debug::INSETTEXT]
2621 << "LyXTabular::getCellFromInset: "
2623 << ", cur_cell=" << save_cur_cell
2624 << ", maybe_cell=" << maybe_cell
2626 // We should have found a cell at this point
2628 lyxerr << "LyXTabular::getCellFromInset: "
2629 << "Cell not found!" << endl;
2637 void LyXTabular::validate(LaTeXFeatures & features) const
2639 features.require("NeedTabularnewline");
2640 if (isLongTabular())
2641 features.require("longtable");
2643 features.require("rotating");
2644 for (int cell = 0; cell < numberofcells; ++cell) {
2645 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2646 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2647 features.require("array");
2648 getCellInset(cell).validate(features);
2653 void LyXTabular::getLabelList(std::vector<string> & list) const
2655 for (int i = 0; i < rows_; ++i)
2656 for (int j = 0; j < columns_; ++j)
2657 getCellInset(i, j).getLabelList(list);
2661 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2663 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2664 ParagraphList::const_iterator cit = parlist.begin();
2665 ParagraphList::const_iterator end = parlist.end();
2667 for (; cit != end; ++cit)
2668 for (int i = 0; i < cit->size(); ++i)
2669 if (cit->isNewline(i))