3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
23 #include "bufferparams.h"
25 #include "LaTeXFeatures.h"
27 #include "paragraph.h"
29 #include "insets/insettabular.h"
31 #include "support/lstrings.h"
32 #include "support/tostr.h"
34 #include "support/std_sstream.h"
36 using lyx::support::ltrim;
37 using lyx::support::prefixIs;
38 using lyx::support::rtrim;
39 using lyx::support::strToInt;
40 using lyx::support::suffixIs;
49 using std::ostringstream;
53 #ifndef CXX_GLOBAL_CSTD
60 int const WIDTH_OF_LINE = 5;
63 string const write_attribute(string const & name, T const & t)
65 string const s = tostr(t);
66 return s.empty() ? s : " " + name + "=\"" + s + "\"";
69 string const write_attribute(string const & name, string const & t)
71 return t.empty() ? t : " " + name + "=\"" + t + "\"";
75 string const write_attribute(string const & name, bool const & b)
77 // we write only true attribute values so we remove a bit of the
78 // file format bloat for tabulars.
79 return b ? write_attribute(name, tostr(b)) : string();
83 string const write_attribute(string const & name, int const & i)
85 // we write only true attribute values so we remove a bit of the
86 // file format bloat for tabulars.
87 return i ? write_attribute(name, tostr(i)) : string();
91 string const write_attribute(string const & name, LyXLength const & value)
93 // we write only the value if we really have one same reson as above.
94 return value.zero() ? string() : write_attribute(name, value.asString());
98 string const tostr(LyXAlignment const & num)
103 case LYX_ALIGN_BLOCK:
107 case LYX_ALIGN_CENTER:
109 case LYX_ALIGN_RIGHT:
111 case LYX_ALIGN_LAYOUT:
113 case LYX_ALIGN_SPECIAL:
120 string const tostr(LyXTabular::VAlignment const & num)
123 case LyXTabular::LYX_VALIGN_TOP:
125 case LyXTabular::LYX_VALIGN_MIDDLE:
127 case LyXTabular::LYX_VALIGN_BOTTOM:
134 string const tostr(LyXTabular::BoxType const & num)
137 case LyXTabular::BOX_NONE:
139 case LyXTabular::BOX_PARBOX:
141 case LyXTabular::BOX_MINIPAGE:
148 // I would have liked a fromstr template a lot better. (Lgb)
149 bool string2type(string const str, LyXAlignment & num)
152 num = LYX_ALIGN_NONE;
153 else if (str == "block")
154 num = LYX_ALIGN_BLOCK;
155 else if (str == "left")
156 num = LYX_ALIGN_LEFT;
157 else if (str == "center")
158 num = LYX_ALIGN_CENTER;
159 else if (str == "right")
160 num = LYX_ALIGN_RIGHT;
167 bool string2type(string const str, LyXTabular::VAlignment & num)
170 num = LyXTabular::LYX_VALIGN_TOP;
171 else if (str == "middle" )
172 num = LyXTabular::LYX_VALIGN_MIDDLE;
173 else if (str == "bottom")
174 num = LyXTabular::LYX_VALIGN_BOTTOM;
181 bool string2type(string const str, LyXTabular::BoxType & num)
184 num = LyXTabular::BOX_NONE;
185 else if (str == "parbox")
186 num = LyXTabular::BOX_PARBOX;
187 else if (str == "minipage")
188 num = LyXTabular::BOX_MINIPAGE;
195 bool string2type(string const str, bool & num)
199 else if (str == "false")
207 bool getTokenValue(string const & str, const char * token, string & ret)
210 size_t token_length = strlen(token);
211 string::size_type pos = str.find(token);
213 if (pos == string::npos || pos + token_length + 1 >= str.length()
214 || str[pos + token_length] != '=')
216 pos += token_length + 1;
218 if (ch != '"' && ch != '\'') { // only read till next space
222 while (pos < str.length() - 1 && str[++pos] != ch)
229 bool getTokenValue(string const & str, const char * token, int & num)
233 if (!getTokenValue(str, token, tmp))
240 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
243 if (!getTokenValue(str, token, tmp))
245 return string2type(tmp, num);
249 bool getTokenValue(string const & str, const char * token,
250 LyXTabular::VAlignment & num)
253 if (!getTokenValue(str, token, tmp))
255 return string2type(tmp, num);
259 bool getTokenValue(string const & str, const char * token,
260 LyXTabular::BoxType & num)
263 if (!getTokenValue(str, token, tmp))
265 return string2type(tmp, num);
269 bool getTokenValue(string const & str, const char * token, bool & flag)
271 // set the flag always to false as this should be the default for bools
272 // not in the file-format.
275 if (!getTokenValue(str, token, tmp))
277 return string2type(tmp, flag);
281 bool getTokenValue(string const & str, const char * token, LyXLength & len)
283 // set the lenght to be zero() as default as this it should be if not
284 // in the file format.
287 if (!getTokenValue(str, token, tmp))
289 return isValidLength(tmp, &len);
293 void l_getline(istream & is, string & str)
296 while (str.empty()) {
298 if (!str.empty() && str[str.length() - 1] == '\r')
299 str.erase(str.length() - 1);
305 /// Define a few methods for the inner structs
307 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
312 multicolumn = LyXTabular::CELL_NORMAL;
313 alignment = LYX_ALIGN_CENTER;
314 valignment = LYX_VALIGN_TOP;
324 LyXTabular::rowstruct::rowstruct()
331 endfirsthead = false;
338 LyXTabular::columnstruct::columnstruct()
342 alignment = LYX_ALIGN_CENTER;
343 valignment = LYX_VALIGN_TOP;
348 LyXTabular::ltType::ltType()
356 LyXTabular::LyXTabular(BufferParams const & bp, int rows_arg, int columns_arg)
358 init(bp, rows_arg, columns_arg);
362 // activates all lines and sets all widths to 0
363 void LyXTabular::init(BufferParams const & bp, int rows_arg, int columns_arg)
366 columns_ = columns_arg;
367 row_info = row_vector(rows_);
368 column_info = column_vector(columns_);
369 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
370 row_info.reserve(10);
371 column_info.reserve(10);
372 cell_info.reserve(100);
374 for (int i = 0; i < rows_; ++i)
375 cell_info[i].back().right_line = true;
376 row_info.back().bottom_line = true;
377 row_info.front().bottom_line = true;
378 column_info.back().right_line = true;
379 is_long_tabular = false;
384 void LyXTabular::fixCellNums()
386 BOOST_ASSERT(rows_ == row_info.size());
387 BOOST_ASSERT(columns_ == column_info.size());
388 BOOST_ASSERT(rows_ == cell_info.size());
390 for (int i = 0; i < rows_; ++i) {
391 for (int j = 0; j < columns_; ++j) {
392 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
393 cell_info[i][j].cellno = cellno++;
395 cell_info[i].back().right_line = true;
398 set_row_column_number_info();
399 BOOST_ASSERT(rows_ == row_info.size());
400 BOOST_ASSERT(columns_ == column_info.size());
401 BOOST_ASSERT(rows_ == cell_info.size());
405 void LyXTabular::setOwner(InsetTabular * inset)
407 for (int i = 0; i < rows_; ++i)
408 for (int j = 0; j < columns_; ++j) {
409 cell_info[i][j].inset.setOwner(inset);
410 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
415 #warning for some strange reason, cellstruct does not seem to have copy-semantics
417 // work around using 'swap' only...
419 void LyXTabular::appendRow(BufferParams const & bp, int cell)
423 int row = row_of_cell(cell);
425 row_vector::iterator rit = row_info.begin() + row;
426 row_info.insert(rit, rowstruct());
427 // now set the values of the row before
428 row_info[row] = row_info[row + 1];
430 cell_vvector old(rows_ - 1);
431 for (int i = 0; i < rows_ - 1; ++i)
432 swap(cell_info[i], old[i]);
434 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
436 for (int i = 0; i <= row; ++i)
437 swap(cell_info[i], old[i]);
438 for (int i = row + 2; i < rows_; ++i)
439 swap(cell_info[i], old[i - 1]);
441 if (bp.tracking_changes)
442 for (int j = 0; j < columns_; ++j)
443 cell_info[row + 1][j].inset.markNew(true);
445 set_row_column_number_info();
449 void LyXTabular::deleteRow(int row)
451 // Not allowed to delete last row
455 row_info.erase(row_info.begin() + row);
456 cell_info.erase(cell_info.begin() + row);
462 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
466 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
468 int const column = column_of_cell(cell);
469 column_vector::iterator cit = column_info.begin() + column + 1;
470 column_info.insert(cit, columnstruct());
471 // set the column values of the column before
472 column_info[column + 1] = column_info[column];
474 for (int i = 0; i < rows_; ++i) {
475 for (int j = 0; j <= column; ++j)
476 c_info[i][j] = cell_info[i][j];
478 for (int j = column + 1; j < columns_; ++j)
479 c_info[i][j] = cell_info[i][j - 1];
481 // care about multicolumns
482 if (c_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
483 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
485 if (column + 2 >= columns_
486 || c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
487 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
491 for (int i = 0; i < rows_; ++i) {
492 cell_info[i][column + 1].inset.clear(false);
493 if (bp.tracking_changes)
494 cell_info[i][column + 1].inset.markNew(true);
500 void LyXTabular::deleteColumn(int column)
502 // Not allowed to delete last column
506 column_info.erase(column_info.begin() + column);
507 for (int i = 0; i < rows_; ++i)
508 cell_info[i].erase(cell_info[i].begin() + column);
514 void LyXTabular::set_row_column_number_info()
517 for (int row = 0; row < rows_; ++row) {
518 for (int column = 0; column < columns_; ++column) {
519 if (cell_info[row][column].multicolumn
520 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
522 cell_info[row][column].cellno = numberofcells;
525 ++numberofcells; // because this is one more than as we start from 0
527 rowofcell.resize(numberofcells);
528 columnofcell.resize(numberofcells);
530 for (int row = 0, column = 0, c = 0;
531 c < numberofcells && row < rows_ && column < columns_;) {
533 columnofcell[c] = column;
537 } while (column < columns_ &&
538 cell_info[row][column].multicolumn
539 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
541 if (column == columns_) {
547 for (int row = 0; row < rows_; ++row) {
548 for (int column = 0; column < columns_; ++column) {
549 if (isPartOfMultiColumn(row,column))
551 cell_info[row][column].inset.setAutoBreakRows(
552 !getPWidth(getCellNumber(row, column)).zero());
558 int LyXTabular::getNumberOfCells() const
560 return numberofcells;
564 int LyXTabular::numberOfCellsInRow(int cell) const
566 int const row = row_of_cell(cell);
568 for (int i = 0; i < columns_; ++i)
569 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
575 // returns 1 if there is a topline, returns 0 if not
576 bool LyXTabular::topLine(int cell, bool onlycolumn) const
578 if (!onlycolumn && isMultiColumn(cell))
579 return cellinfo_of_cell(cell).top_line;
580 return row_info[row_of_cell(cell)].top_line;
584 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
586 if (!onlycolumn && isMultiColumn(cell))
587 return cellinfo_of_cell(cell).bottom_line;
588 return row_info[row_of_cell(cell)].bottom_line;
592 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
594 if (!onlycolumn && isMultiColumn(cell) &&
595 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
597 if (cellinfo_of_cell(cell).align_special.empty())
598 return cellinfo_of_cell(cell).left_line;
599 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
601 if (column_info[column_of_cell(cell)].align_special.empty())
602 return column_info[column_of_cell(cell)].left_line;
603 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
607 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
609 if (!onlycolumn && isMultiColumn(cell) &&
610 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
612 if (cellinfo_of_cell(cell).align_special.empty())
613 return cellinfo_of_cell(cell).right_line;
614 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
616 if (column_info[column_of_cell(cell)].align_special.empty())
617 return column_info[right_column_of_cell(cell)].right_line;
618 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
622 bool LyXTabular::topAlreadyDrawn(int cell) const
624 int row = row_of_cell(cell);
625 if (row > 0 && !getAdditionalHeight(row)) {
626 int column = column_of_cell(cell);
629 && cell_info[row][column].multicolumn
630 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
632 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
633 return row_info[row].bottom_line;
635 return cell_info[row][column].bottom_line;
641 bool LyXTabular::leftAlreadyDrawn(int cell) const
643 int column = column_of_cell(cell);
645 int row = row_of_cell(cell);
647 (cell_info[row][column].multicolumn ==
648 LyXTabular::CELL_PART_OF_MULTICOLUMN));
649 if (getAdditionalWidth(cell_info[row][column].cellno))
651 return rightLine(cell_info[row][column].cellno);
657 bool LyXTabular::isLastRow(int cell) const
659 return row_of_cell(cell) == rows_ - 1;
663 int LyXTabular::getAdditionalHeight(int row) const
665 if (!row || row >= rows_)
671 for (int column = 0; column < columns_ && bottom; ++column) {
672 switch (cell_info[row - 1][column].multicolumn) {
673 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
674 bottom = cell_info[row - 1][column].bottom_line;
676 case LyXTabular::CELL_NORMAL:
677 bottom = row_info[row - 1].bottom_line;
680 for (int column = 0; column < columns_ && top; ++column) {
681 switch (cell_info[row][column].multicolumn) {
682 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
683 top = cell_info[row][column].top_line;
685 case LyXTabular::CELL_NORMAL:
686 top = row_info[row].top_line;
690 return WIDTH_OF_LINE;
695 int LyXTabular::getAdditionalWidth(int cell) const
697 // internally already set in setWidthOfCell
698 // used to get it back in text.C
699 int const col = right_column_of_cell(cell);
700 int const row = row_of_cell(cell);
701 if (col < columns_ - 1 && rightLine(cell) &&
702 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
704 return WIDTH_OF_LINE;
710 // returns the maximum over all rows
711 int LyXTabular::getWidthOfColumn(int cell) const
713 int const column1 = column_of_cell(cell);
714 int const column2 = right_column_of_cell(cell);
716 for (int i = column1; i <= column2; ++i)
717 result += column_info[i].width_of_column;
722 int LyXTabular::getWidthOfTabular() const
724 return width_of_tabular;
728 // returns true if a complete update is necessary, otherwise false
729 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
731 if (!isMultiColumn(cell))
734 int const row = row_of_cell(cell);
735 int const column1 = column_of_cell(cell);
736 int const column2 = right_column_of_cell(cell);
737 int const old_val = cell_info[row][column2].width_of_cell;
739 // first set columns to 0 so we can calculate the right width
740 for (int i = column1; i <= column2; ++i) {
741 cell_info[row][i].width_of_cell = 0;
743 // set the width to MAX_WIDTH until width > 0
744 int width = new_width + 2 * WIDTH_OF_LINE;
746 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
747 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
748 width -= column_info[i].width_of_column;
751 cell_info[row][i].width_of_cell = width;
753 if (old_val != cell_info[row][column2].width_of_cell) {
754 // in this case we have to recalculate all multicolumn cells which
755 // have this column as one of theirs but not as last one
756 calculate_width_of_column_NMC(i);
757 recalculateMulticolumnsOfColumn(i);
758 calculate_width_of_column(i);
764 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
766 // the last column does not have to be recalculated because all
767 // multicolumns will have here there last multicolumn cell which
768 // always will have the whole rest of the width of the cell.
769 if (column > (columns_ - 2))
771 for(int row = 0; row < rows_; ++row) {
772 int mc = cell_info[row][column].multicolumn;
773 int nmc = cell_info[row][column+1].multicolumn;
774 // we only have to update multicolumns which do not have this
775 // column as their last column!
776 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
777 (mc == CELL_PART_OF_MULTICOLUMN &&
778 nmc == CELL_PART_OF_MULTICOLUMN))
780 int const cellno = cell_info[row][column].cellno;
781 setWidthOfMulticolCell(cellno,
782 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
788 // returns 1 if a complete update is necessary, otherwise 0
789 void LyXTabular::setWidthOfCell(int cell, int new_width)
791 int const row = row_of_cell(cell);
792 int const column1 = column_of_cell(cell);
797 if (rightLine(cell_info[row][column1].cellno, true) &&
798 column1 < columns_ - 1 &&
799 leftLine(cell_info[row][column1+1].cellno, true))
801 add_width = WIDTH_OF_LINE;
804 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
807 if (isMultiColumnReal(cell)) {
808 tmp = setWidthOfMulticolCell(cell, new_width);
810 width = new_width + 2 * WIDTH_OF_LINE + add_width;
811 cell_info[row][column1].width_of_cell = width;
812 tmp = calculate_width_of_column_NMC(column1);
814 recalculateMulticolumnsOfColumn(column1);
817 for (int i = 0; i < columns_; ++i)
818 calculate_width_of_column(i);
819 calculate_width_of_tabular();
824 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
826 if (!isMultiColumn(cell) || onlycolumn)
827 column_info[column_of_cell(cell)].alignment = align;
829 cellinfo_of_cell(cell).alignment = align;
833 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
835 if (!isMultiColumn(cell) || onlycolumn)
836 column_info[column_of_cell(cell)].valignment = align;
838 cellinfo_of_cell(cell).valignment = align;
842 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
844 int const j = column_of_cell(cell);
846 column_info[j].p_width = width;
847 for (int i = 0; i < rows_; ++i) {
848 int const cell = getCellNumber(i, j);
849 // because of multicolumns
850 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
855 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
857 if (!isMultiColumn(cell))
860 cellinfo_of_cell(cell).p_width = width;
861 getCellInset(cell).setAutoBreakRows(!width.zero());
866 void LyXTabular::setAlignSpecial(int cell, string const & special,
867 LyXTabular::Feature what)
869 if (what == SET_SPECIAL_MULTI)
870 cellinfo_of_cell(cell).align_special = special;
872 column_info[column_of_cell(cell)].align_special = special;
876 void LyXTabular::setAllLines(int cell, bool line)
878 setTopLine(cell, line);
879 setBottomLine(cell, line);
880 setRightLine(cell, line);
881 setLeftLine(cell, line);
885 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
887 int const row = row_of_cell(cell);
888 if (onlycolumn || !isMultiColumn(cell))
889 row_info[row].top_line = line;
891 cellinfo_of_cell(cell).top_line = line;
895 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
897 if (onlycolumn || !isMultiColumn(cell))
898 row_info[row_of_cell(cell)].bottom_line = line;
900 cellinfo_of_cell(cell).bottom_line = line;
904 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
906 if (onlycolumn || !isMultiColumn(cell))
907 column_info[column_of_cell(cell)].left_line = line;
909 cellinfo_of_cell(cell).left_line = line;
913 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
915 if (onlycolumn || !isMultiColumn(cell))
916 column_info[right_column_of_cell(cell)].right_line = line;
918 cellinfo_of_cell(cell).right_line = line;
922 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
924 if (!onlycolumn && isMultiColumn(cell))
925 return cellinfo_of_cell(cell).alignment;
926 return column_info[column_of_cell(cell)].alignment;
930 LyXTabular::VAlignment
931 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
933 if (!onlycolumn && isMultiColumn(cell))
934 return cellinfo_of_cell(cell).valignment;
935 return column_info[column_of_cell(cell)].valignment;
939 LyXLength const LyXTabular::getPWidth(int cell) const
941 if (isMultiColumn(cell))
942 return cellinfo_of_cell(cell).p_width;
943 return column_info[column_of_cell(cell)].p_width;
947 LyXLength const LyXTabular::getColumnPWidth(int cell) const
949 return column_info[column_of_cell(cell)].p_width;
953 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
955 if (isMultiColumn(cell))
956 return cellinfo_of_cell(cell).p_width;
961 string const LyXTabular::getAlignSpecial(int cell, int what) const
963 if (what == SET_SPECIAL_MULTI)
964 return cellinfo_of_cell(cell).align_special;
965 return column_info[column_of_cell(cell)].align_special;
969 int LyXTabular::getWidthOfCell(int cell) const
971 int const row = row_of_cell(cell);
972 int const column1 = column_of_cell(cell);
973 int const column2 = right_column_of_cell(cell);
975 for (int i = column1; i <= column2; ++i)
976 result += cell_info[row][i].width_of_cell;
981 int LyXTabular::getBeginningOfTextInCell(int cell) const
985 switch (getAlignment(cell)) {
986 case LYX_ALIGN_CENTER:
987 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
989 case LYX_ALIGN_RIGHT:
990 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
991 // + getAdditionalWidth(cell);
994 // LYX_ALIGN_LEFT: nothing :-)
1004 bool LyXTabular::isFirstCellInRow(int cell) const
1006 return column_of_cell(cell) == 0;
1010 int LyXTabular::getFirstCellInRow(int row) const
1012 if (row > rows_ - 1)
1014 return cell_info[row][0].cellno;
1018 bool LyXTabular::isLastCellInRow(int cell) const
1020 return right_column_of_cell(cell) == columns_ - 1;
1024 int LyXTabular::getLastCellInRow(int row) const
1026 if (row > rows_ - 1)
1028 return cell_info[row][columns_-1].cellno;
1032 void LyXTabular::calculate_width_of_column(int column)
1035 for (int i = 0; i < rows_; ++i)
1036 maximum = max(cell_info[i][column].width_of_cell, maximum);
1037 column_info[column].width_of_column = maximum;
1042 // Calculate the columns regarding ONLY the normal cells and if this
1043 // column is inside a multicolumn cell then use it only if its the last
1044 // column of this multicolumn cell as this gives an added width to the
1045 // column, all the rest should be adapted!
1047 bool LyXTabular::calculate_width_of_column_NMC(int column)
1049 int const old_column_width = column_info[column].width_of_column;
1051 for (int i = 0; i < rows_; ++i) {
1052 int cell = getCellNumber(i, column);
1053 bool ismulti = isMultiColumnReal(cell);
1054 if ((!ismulti || column == right_column_of_cell(cell)) &&
1055 cell_info[i][column].width_of_cell > max)
1057 max = cell_info[i][column].width_of_cell;
1060 column_info[column].width_of_column = max;
1061 return column_info[column].width_of_column != old_column_width;
1065 void LyXTabular::calculate_width_of_tabular()
1067 width_of_tabular = 0;
1068 for (int i = 0; i < columns_; ++i)
1069 width_of_tabular += column_info[i].width_of_column;
1073 int LyXTabular::row_of_cell(int cell) const
1075 if (cell >= numberofcells)
1079 return rowofcell[cell];
1083 int LyXTabular::column_of_cell(int cell) const
1085 if (cell >= numberofcells)
1086 return columns_ - 1;
1089 return columnofcell[cell];
1093 int LyXTabular::right_column_of_cell(int cell) const
1095 int const row = row_of_cell(cell);
1096 int column = column_of_cell(cell);
1097 while (column < columns_ - 1 &&
1098 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1104 void LyXTabular::write(Buffer const & buf, ostream & os) const
1108 << write_attribute("version", 3)
1109 << write_attribute("rows", rows_)
1110 << write_attribute("columns", columns_)
1112 // global longtable options
1114 << write_attribute("rotate", rotate)
1115 << write_attribute("islongtable", is_long_tabular)
1116 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1117 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1118 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1119 << write_attribute("headTopDL", endhead.topDL)
1120 << write_attribute("headBottomDL", endhead.bottomDL)
1121 << write_attribute("footTopDL", endfoot.topDL)
1122 << write_attribute("footBottomDL", endfoot.bottomDL)
1123 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1124 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1125 << write_attribute("lastFootEmpty", endlastfoot.empty)
1127 for (int j = 0; j < columns_; ++j) {
1129 << write_attribute("alignment", column_info[j].alignment)
1130 << write_attribute("valignment", column_info[j].valignment)
1131 << write_attribute("leftline", column_info[j].left_line)
1132 << write_attribute("rightline", column_info[j].right_line)
1133 << write_attribute("width", column_info[j].p_width.asString())
1134 << write_attribute("special", column_info[j].align_special)
1137 for (int i = 0; i < rows_; ++i) {
1139 << write_attribute("topline", row_info[i].top_line)
1140 << write_attribute("bottomline", row_info[i].bottom_line)
1141 << write_attribute("endhead", row_info[i].endhead)
1142 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1143 << write_attribute("endfoot", row_info[i].endfoot)
1144 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1145 << write_attribute("newpage", row_info[i].newpage)
1147 for (int j = 0; j < columns_; ++j) {
1149 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1150 << write_attribute("alignment", cell_info[i][j].alignment)
1151 << write_attribute("valignment", cell_info[i][j].valignment)
1152 << write_attribute("topline", cell_info[i][j].top_line)
1153 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1154 << write_attribute("leftline", cell_info[i][j].left_line)
1155 << write_attribute("rightline", cell_info[i][j].right_line)
1156 << write_attribute("rotate", cell_info[i][j].rotate)
1157 << write_attribute("usebox", cell_info[i][j].usebox)
1158 << write_attribute("width", cell_info[i][j].p_width)
1159 << write_attribute("special", cell_info[i][j].align_special)
1161 os << "\\begin_inset ";
1162 cell_info[i][j].inset.write(buf, os);
1163 os << "\n\\end_inset \n"
1168 os << "</lyxtabular>\n";
1172 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1176 row_info[--hr].endhead = true;
1178 // set firstheader info
1179 if (fhr && fhr < rows_) {
1180 if (row_info[fhr].endhead) {
1182 row_info[--fhr].endfirsthead = true;
1183 row_info[fhr].endhead = false;
1185 } else if (row_info[fhr - 1].endhead) {
1186 endfirsthead.empty = true;
1188 while (fhr > 0 && !row_info[--fhr].endhead) {
1189 row_info[fhr].endfirsthead = true;
1194 if (fr && fr < rows_) {
1195 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1196 while (fr > 0 && !row_info[--fr].endhead) {
1197 row_info[fr].endfoot = true;
1198 row_info[fr].endhead = false;
1200 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1201 while (fr > 0 && !row_info[--fr].endfirsthead) {
1202 row_info[fr].endfoot = true;
1203 row_info[fr].endfirsthead = false;
1205 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1206 while (fr > 0 && !row_info[--fr].endhead &&
1207 !row_info[fr].endfirsthead)
1209 row_info[fr].endfoot = true;
1213 // set lastfooter info
1214 if (lfr && lfr < rows_) {
1215 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1216 while (lfr > 0 && !row_info[--lfr].endhead) {
1217 row_info[lfr].endlastfoot = true;
1218 row_info[lfr].endhead = false;
1220 } else if (row_info[lfr].endfirsthead &&
1221 row_info[lfr - 1].endfirsthead)
1223 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1224 row_info[lfr].endlastfoot = true;
1225 row_info[lfr].endfirsthead = false;
1227 } else if (row_info[lfr].endfoot
1228 && row_info[lfr - 1].endfoot) {
1229 while (lfr > 0 && !row_info[--lfr].endfoot) {
1230 row_info[lfr].endlastfoot = true;
1231 row_info[lfr].endfoot = false;
1233 } else if (!row_info[fr - 1].endhead
1234 && !row_info[fr - 1].endfirsthead &&
1235 !row_info[fr - 1].endfoot)
1238 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1239 !row_info[lfr].endfoot)
1241 row_info[lfr].endlastfoot = true;
1243 } else if (haveLTFoot()) {
1244 endlastfoot.empty = true;
1250 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1253 istream & is = lex.getStream();
1255 l_getline(is, line);
1256 if (!prefixIs(line, "<lyxtabular ")
1257 && !prefixIs(line, "<LyXTabular ")) {
1258 BOOST_ASSERT(false);
1263 if (!getTokenValue(line, "version", version))
1265 BOOST_ASSERT(version >= 2);
1268 if (!getTokenValue(line, "rows", rows_arg))
1271 if (!getTokenValue(line, "columns", columns_arg))
1273 init(buf.params(), rows_arg, columns_arg);
1274 l_getline(is, line);
1275 if (!prefixIs(line, "<features")) {
1276 lyxerr << "Wrong tabular format (expected <features ...> got"
1277 << line << ')' << endl;
1280 getTokenValue(line, "rotate", rotate);
1281 getTokenValue(line, "islongtable", is_long_tabular);
1282 // compatibility read for old longtable options. Now we can make any
1283 // row part of the header/footer type we want before it was strict
1284 // sequential from the first row down (as LaTeX does it!). So now when
1285 // we find a header/footer line we have to go up the rows and set it
1286 // on all preceding rows till the first or one with already a h/f option
1287 // set. If we find a firstheader on the same line as a header or a
1288 // lastfooter on the same line as a footer then this should be set empty.
1296 getTokenValue(line, "endhead", hrow);
1297 getTokenValue(line, "endfirsthead", fhrow);
1298 getTokenValue(line, "endfoot", frow);
1299 getTokenValue(line, "endlastfoot", lfrow);
1300 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1302 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1303 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1304 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1305 getTokenValue(line, "headTopDL", endhead.topDL);
1306 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1307 getTokenValue(line, "footTopDL", endfoot.topDL);
1308 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1309 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1310 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1311 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1313 for (int j = 0; j < columns_; ++j) {
1315 if (!prefixIs(line,"<column")) {
1316 lyxerr << "Wrong tabular format (expected <column ...> got"
1317 << line << ')' << endl;
1320 getTokenValue(line, "alignment", column_info[j].alignment);
1321 getTokenValue(line, "valignment", column_info[j].valignment);
1322 getTokenValue(line, "leftline", column_info[j].left_line);
1323 getTokenValue(line, "rightline", column_info[j].right_line);
1324 getTokenValue(line, "width", column_info[j].p_width);
1325 getTokenValue(line, "special", column_info[j].align_special);
1328 for (int i = 0; i < rows_; ++i) {
1329 l_getline(is, line);
1330 if (!prefixIs(line, "<row")) {
1331 lyxerr << "Wrong tabular format (expected <row ...> got"
1332 << line << ')' << endl;
1335 getTokenValue(line, "topline", row_info[i].top_line);
1336 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1337 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1338 getTokenValue(line, "endhead", row_info[i].endhead);
1339 getTokenValue(line, "endfoot", row_info[i].endfoot);
1340 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1341 getTokenValue(line, "newpage", row_info[i].newpage);
1342 for (int j = 0; j < columns_; ++j) {
1343 l_getline(is, line);
1344 if (!prefixIs(line, "<cell")) {
1345 lyxerr << "Wrong tabular format (expected <cell ...> got"
1346 << line << ')' << endl;
1349 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1350 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1351 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1352 getTokenValue(line, "topline", cell_info[i][j].top_line);
1353 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1354 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1355 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1356 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1357 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1358 getTokenValue(line, "width", cell_info[i][j].p_width);
1359 getTokenValue(line, "special", cell_info[i][j].align_special);
1360 l_getline(is, line);
1361 if (prefixIs(line, "\\begin_inset")) {
1362 cell_info[i][j].inset.read(buf, lex);
1363 l_getline(is, line);
1365 if (!prefixIs(line, "</cell>")) {
1366 lyxerr << "Wrong tabular format (expected </cell> got"
1367 << line << ')' << endl;
1371 l_getline(is, line);
1372 if (!prefixIs(line, "</row>")) {
1373 lyxerr << "Wrong tabular format (expected </row> got"
1374 << line << ')' << endl;
1378 while (!prefixIs(line, "</lyxtabular>")) {
1379 l_getline(is, line);
1381 set_row_column_number_info();
1385 bool LyXTabular::isMultiColumn(int cell) const
1387 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1391 bool LyXTabular::isMultiColumnReal(int cell) const
1393 return column_of_cell(cell) != right_column_of_cell(cell) &&
1394 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1398 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1400 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1404 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1406 cellstruct & cs = cellinfo_of_cell(cell);
1407 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1408 cs.alignment = column_info[column_of_cell(cell)].alignment;
1409 cs.top_line = row_info[row_of_cell(cell)].top_line;
1410 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1411 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1412 for (int i = 1; i < number; ++i) {
1413 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1414 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1415 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1416 cs1.inset.clear(false);
1418 set_row_column_number_info();
1422 int LyXTabular::cells_in_multicolumn(int cell) const
1424 int const row = row_of_cell(cell);
1425 int column = column_of_cell(cell);
1428 while (column < columns_ &&
1429 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1438 int LyXTabular::unsetMultiColumn(int cell)
1440 int const row = row_of_cell(cell);
1441 int column = column_of_cell(cell);
1445 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1446 cell_info[row][column].multicolumn = CELL_NORMAL;
1448 while (column < columns_ &&
1449 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1451 cell_info[row][column].multicolumn = CELL_NORMAL;
1456 set_row_column_number_info();
1461 void LyXTabular::setLongTabular(bool what)
1463 is_long_tabular = what;
1467 bool LyXTabular::isLongTabular() const
1469 return is_long_tabular;
1473 void LyXTabular::setRotateTabular(bool flag)
1479 bool LyXTabular::getRotateTabular() const
1485 void LyXTabular::setRotateCell(int cell, bool flag)
1487 cellinfo_of_cell(cell).rotate = flag;
1491 bool LyXTabular::getRotateCell(int cell) const
1493 return cellinfo_of_cell(cell).rotate;
1497 bool LyXTabular::needRotating() const
1501 for (int i = 0; i < rows_; ++i)
1502 for (int j = 0; j < columns_; ++j)
1503 if (cell_info[i][j].rotate)
1509 bool LyXTabular::isLastCell(int cell) const
1511 if (cell + 1 < numberofcells)
1517 int LyXTabular::getCellAbove(int cell) const
1519 if (row_of_cell(cell) > 0)
1520 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1525 int LyXTabular::getCellBelow(int cell) const
1527 if (row_of_cell(cell) + 1 < rows_)
1528 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1533 int LyXTabular::getLastCellAbove(int cell) const
1535 if (row_of_cell(cell) <= 0)
1537 if (!isMultiColumn(cell))
1538 return getCellAbove(cell);
1539 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1543 int LyXTabular::getLastCellBelow(int cell) const
1545 if (row_of_cell(cell) + 1 >= rows_)
1547 if (!isMultiColumn(cell))
1548 return getCellBelow(cell);
1549 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1553 int LyXTabular::getCellNumber(int row, int column) const
1555 BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1556 return cell_info[row][column].cellno;
1560 void LyXTabular::setUsebox(int cell, BoxType type)
1562 cellinfo_of_cell(cell).usebox = type;
1566 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1568 if (column_info[column_of_cell(cell)].p_width.zero() &&
1569 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1571 if (cellinfo_of_cell(cell).usebox > 1)
1572 return cellinfo_of_cell(cell).usebox;
1573 return useParbox(cell);
1578 // This are functions used for the longtable support
1580 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1585 row_info[row].endfirsthead = flag;
1589 row_info[row].endhead = flag;
1594 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1597 hd.set = haveLTHead();
1598 return row_info[row].endhead;
1602 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1605 hd.set = haveLTFirstHead();
1606 return row_info[row].endfirsthead;
1610 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1615 row_info[row].endlastfoot = flag;
1619 row_info[row].endfoot = flag;
1624 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1627 fd.set = haveLTFoot();
1628 return row_info[row].endfoot;
1632 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1635 fd.set = haveLTLastFoot();
1636 return row_info[row].endlastfoot;
1640 void LyXTabular::setLTNewPage(int row, bool what)
1642 row_info[row].newpage = what;
1646 bool LyXTabular::getLTNewPage(int row) const
1648 return row_info[row].newpage;
1652 bool LyXTabular::haveLTHead() const
1654 for (int i = 0; i < rows_; ++i)
1655 if (row_info[i].endhead)
1661 bool LyXTabular::haveLTFirstHead() const
1663 if (endfirsthead.empty)
1665 for (int i = 0; i < rows_; ++i)
1666 if (row_info[i].endfirsthead)
1672 bool LyXTabular::haveLTFoot() const
1674 for (int i = 0; i < rows_; ++i)
1675 if (row_info[i].endfoot)
1681 bool LyXTabular::haveLTLastFoot() const
1683 if (endlastfoot.empty)
1685 for (int i = 0; i < rows_; ++i)
1686 if (row_info[i].endlastfoot)
1692 // end longtable support functions
1694 void LyXTabular::setAscentOfRow(int row, int height)
1696 if (row >= rows_ || row_info[row].ascent_of_row == height)
1698 row_info[row].ascent_of_row = height;
1702 void LyXTabular::setDescentOfRow(int row, int height)
1704 if (row >= rows_ || row_info[row].descent_of_row == height)
1706 row_info[row].descent_of_row = height;
1710 int LyXTabular::getAscentOfRow(int row) const
1714 return row_info[row].ascent_of_row;
1718 int LyXTabular::getDescentOfRow(int row) const
1722 return row_info[row].descent_of_row;
1726 int LyXTabular::getHeightOfTabular() const
1729 for (int row = 0; row < rows_; ++row)
1730 height += getAscentOfRow(row) + getDescentOfRow(row) +
1731 getAdditionalHeight(row);
1736 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1738 if (row >= rows_ || column >= columns_)
1740 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1744 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1746 if (row < 0 || row >= rows_)
1749 int const fcell = getFirstCellInRow(row);
1750 int const n = numberOfCellsInRow(fcell) + fcell;
1753 for (int i = fcell; i < n; ++i) {
1757 if (tmp == n - fcell) {
1760 for (int i = fcell; i < n; ++i) {
1763 << column_of_cell(i) + 1
1765 << right_column_of_cell(i) + 1
1777 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1779 if (row < 0 || row >= rows_)
1782 int const fcell = getFirstCellInRow(row);
1783 int const n = numberOfCellsInRow(fcell) + fcell;
1786 for (int i = fcell; i < n; ++i) {
1790 if (tmp == n - fcell) {
1793 for (int i = fcell; i < n; ++i) {
1794 if (bottomLine(i)) {
1796 << column_of_cell(i) + 1
1798 << right_column_of_cell(i) + 1
1810 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1814 if (getRotateCell(cell)) {
1815 os << "\\begin{sideways}\n";
1818 if (isMultiColumn(cell)) {
1819 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1820 if (!cellinfo_of_cell(cell).align_special.empty()) {
1821 os << cellinfo_of_cell(cell).align_special << "}{";
1823 if (leftLine(cell) &&
1824 (isFirstCellInRow(cell) ||
1825 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1826 !rightLine(cell - 1, true))))
1830 if (!getPWidth(cell).zero()) {
1831 switch (getVAlignment(cell)) {
1832 case LYX_VALIGN_TOP:
1835 case LYX_VALIGN_MIDDLE:
1838 case LYX_VALIGN_BOTTOM:
1843 << getPWidth(cell).asLatexString()
1846 switch (getAlignment(cell)) {
1847 case LYX_ALIGN_LEFT:
1850 case LYX_ALIGN_RIGHT:
1858 if (rightLine(cell))
1860 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1866 if (getUsebox(cell) == BOX_PARBOX) {
1868 switch (getVAlignment(cell)) {
1869 case LYX_VALIGN_TOP:
1872 case LYX_VALIGN_MIDDLE:
1875 case LYX_VALIGN_BOTTOM:
1879 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1880 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1881 os << "\\begin{minipage}[";
1882 switch (getVAlignment(cell)) {
1883 case LYX_VALIGN_TOP:
1886 case LYX_VALIGN_MIDDLE:
1889 case LYX_VALIGN_BOTTOM:
1893 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1900 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1905 if (getUsebox(cell) == BOX_PARBOX)
1907 else if (getUsebox(cell) == BOX_MINIPAGE) {
1908 os << "%\n\\end{minipage}";
1911 if (isMultiColumn(cell)) {
1914 if (getRotateCell(cell)) {
1915 os << "%\n\\end{sideways}";
1922 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1923 LatexRunParams const & runparams) const
1925 if (!is_long_tabular)
1929 // output header info
1931 if (endhead.topDL) {
1935 for (int i = 0; i < rows_; ++i) {
1936 if (row_info[i].endhead) {
1937 ret += TeXRow(os, i, buf, runparams);
1940 if (endhead.bottomDL) {
1944 os << "\\endhead\n";
1946 if (endfirsthead.empty) {
1947 os << "\\endfirsthead\n";
1951 // output firstheader info
1952 if (haveLTFirstHead()) {
1953 if (endfirsthead.topDL) {
1957 for (int i = 0; i < rows_; ++i) {
1958 if (row_info[i].endfirsthead) {
1959 ret += TeXRow(os, i, buf, runparams);
1962 if (endfirsthead.bottomDL) {
1966 os << "\\endfirsthead\n";
1969 // output footer info
1971 if (endfoot.topDL) {
1975 for (int i = 0; i < rows_; ++i) {
1976 if (row_info[i].endfoot) {
1977 ret += TeXRow(os, i, buf, runparams);
1980 if (endfoot.bottomDL) {
1984 os << "\\endfoot\n";
1986 if (endlastfoot.empty) {
1987 os << "\\endlastfoot\n";
1991 // output lastfooter info
1992 if (haveLTLastFoot()) {
1993 if (endlastfoot.topDL) {
1997 for (int i = 0; i < rows_; ++i) {
1998 if (row_info[i].endlastfoot) {
1999 ret += TeXRow(os, i, buf, runparams);
2002 if (endlastfoot.bottomDL) {
2006 os << "\\endlastfoot\n";
2013 bool LyXTabular::isValidRow(int row) const
2015 if (!is_long_tabular)
2017 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2018 !row_info[row].endfoot && !row_info[row].endlastfoot;
2022 int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
2023 LatexRunParams const & runparams) const
2026 int cell = getCellNumber(i, 0);
2027 BufferParams const & bufferparams = buf.params();
2029 ret += TeXTopHLine(os, i);
2030 for (int j = 0; j < columns_; ++j) {
2031 if (isPartOfMultiColumn(i, j))
2033 ret += TeXCellPreamble(os, cell);
2034 InsetText & inset = getCellInset(cell);
2036 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(bufferparams) &&
2037 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2041 ret += inset.latex(buf, os, runparams);
2045 ret += TeXCellPostamble(os, cell);
2046 if (!isLastCellInRow(cell)) { // not last cell in row
2052 os << "\\tabularnewline\n";
2054 ret += TeXBottomHLine(os, i);
2059 int LyXTabular::latex(Buffer const & buf, ostream & os,
2060 LatexRunParams const & runparams) const
2064 //+---------------------------------------------------------------------
2065 //+ first the opening preamble +
2066 //+---------------------------------------------------------------------
2069 os << "\\begin{sideways}\n";
2072 if (is_long_tabular)
2073 os << "\\begin{longtable}{";
2075 os << "\\begin{tabular}{";
2076 for (int i = 0; i < columns_; ++i) {
2077 if (!column_info[i].align_special.empty()) {
2078 os << column_info[i].align_special;
2080 if (column_info[i].left_line)
2082 if (!column_info[i].p_width.zero()) {
2083 switch (column_info[i].alignment) {
2084 case LYX_ALIGN_LEFT:
2085 os << ">{\\raggedright}";
2087 case LYX_ALIGN_RIGHT:
2088 os << ">{\\raggedleft}";
2090 case LYX_ALIGN_CENTER:
2091 os << ">{\\centering}";
2093 case LYX_ALIGN_NONE:
2094 case LYX_ALIGN_BLOCK:
2095 case LYX_ALIGN_LAYOUT:
2096 case LYX_ALIGN_SPECIAL:
2100 switch (column_info[i].valignment) {
2101 case LYX_VALIGN_TOP:
2104 case LYX_VALIGN_MIDDLE:
2107 case LYX_VALIGN_BOTTOM:
2112 << column_info[i].p_width.asLatexString()
2115 switch (column_info[i].alignment) {
2116 case LYX_ALIGN_LEFT:
2119 case LYX_ALIGN_RIGHT:
2127 if (column_info[i].right_line)
2134 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2136 //+---------------------------------------------------------------------
2137 //+ the single row and columns (cells) +
2138 //+---------------------------------------------------------------------
2140 for (int i = 0; i < rows_; ++i) {
2141 if (isValidRow(i)) {
2142 ret += TeXRow(os, i, buf, runparams);
2143 if (is_long_tabular && row_info[i].newpage) {
2144 os << "\\newpage\n";
2150 //+---------------------------------------------------------------------
2151 //+ the closing of the tabular +
2152 //+---------------------------------------------------------------------
2154 if (is_long_tabular)
2155 os << "\\end{longtable}";
2157 os << "\\end{tabular}";
2159 os << "\n\\end{sideways}";
2167 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os) const
2169 os << "<tabular ca=\"";
2170 for (int i = 0; i < columns_; ++i) {
2171 switch (column_info[i].alignment) {
2172 case LYX_ALIGN_LEFT:
2175 case LYX_ALIGN_RIGHT:
2186 for (int i = 0; i < rows_; ++i) {
2187 for (int j = 0; j < columns_; ++j) {
2188 if (isPartOfMultiColumn(i, j))
2190 InsetText & inset = getCellInset(cell);
2192 ret += inset.linuxdoc(buf, os);
2194 if (isLastCellInRow(cell)) {
2203 os << "</tabular>\n";
2208 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row) const
2211 int cell = getFirstCellInRow(row);
2214 for (int j = 0; j < columns_; ++j) {
2215 if (isPartOfMultiColumn(row, j))
2218 os << "<entry align=\"";
2219 switch (getAlignment(cell)) {
2220 case LYX_ALIGN_LEFT:
2223 case LYX_ALIGN_RIGHT:
2231 os << "\" valign=\"";
2232 switch (getVAlignment(cell)) {
2233 case LYX_VALIGN_TOP:
2236 case LYX_VALIGN_BOTTOM:
2239 case LYX_VALIGN_MIDDLE:
2244 if (isMultiColumn(cell)) {
2245 os << " namest=\"col" << j << "\" ";
2246 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2250 ret += getCellInset(cell).docbook(buf, os, true);
2259 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2260 bool /*mixcont*/) const
2264 //+---------------------------------------------------------------------
2265 //+ first the opening preamble +
2266 //+---------------------------------------------------------------------
2268 os << "<tgroup cols=\"" << columns_
2269 << "\" colsep=\"1\" rowsep=\"1\">\n";
2271 for (int i = 0; i < columns_; ++i) {
2272 os << "<colspec colname=\"col" << i << "\" align=\"";
2273 switch (column_info[i].alignment) {
2274 case LYX_ALIGN_LEFT:
2277 case LYX_ALIGN_RIGHT:
2288 //+---------------------------------------------------------------------
2289 //+ Long Tabular case +
2290 //+---------------------------------------------------------------------
2292 // output header info
2293 if (haveLTHead() || haveLTFirstHead()) {
2296 for (int i = 0; i < rows_; ++i) {
2297 if (row_info[i].endhead || row_info[i].endfirsthead) {
2298 ret += docbookRow(buf, os, i);
2304 // output footer info
2305 if (haveLTFoot() || haveLTLastFoot()) {
2308 for (int i = 0; i < rows_; ++i) {
2309 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2310 ret += docbookRow(buf, os, i);
2317 //+---------------------------------------------------------------------
2318 //+ the single row and columns (cells) +
2319 //+---------------------------------------------------------------------
2323 for (int i = 0; i < rows_; ++i) {
2324 if (isValidRow(i)) {
2325 ret += docbookRow(buf, os, i);
2330 //+---------------------------------------------------------------------
2331 //+ the closing of the tabular +
2332 //+---------------------------------------------------------------------
2341 int LyXTabular::asciiTopHLine(ostream & os, int row,
2342 vector<unsigned int> const & clen) const
2344 int const fcell = getFirstCellInRow(row);
2345 int const n = numberOfCellsInRow(fcell) + fcell;
2348 for (int i = fcell; i < n; ++i) {
2358 for (int i = fcell; i < n; ++i) {
2369 int column = column_of_cell(i);
2370 int len = clen[column];
2371 while (isPartOfMultiColumn(row, ++column))
2372 len += clen[column] + 4;
2373 os << string(len, ch);
2388 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2389 vector<unsigned int> const & clen) const
2391 int const fcell = getFirstCellInRow(row);
2392 int const n = numberOfCellsInRow(fcell) + fcell;
2395 for (int i = fcell; i < n; ++i) {
2396 if (bottomLine(i)) {
2405 for (int i = fcell; i < n; ++i) {
2406 if (bottomLine(i)) {
2416 int column = column_of_cell(i);
2417 int len = clen[column];
2418 while (isPartOfMultiColumn(row, ++column))
2419 len += clen[column] + 4;
2420 os << string(len, ch);
2421 if (bottomLine(i)) {
2435 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2436 int cell, int row, int column,
2437 vector<unsigned int> const & clen,
2438 bool onlydata) const
2441 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2453 unsigned int len1 = sstr.str().length();
2454 unsigned int len2 = clen[column];
2455 while (isPartOfMultiColumn(row, ++column))
2456 len2 += clen[column] + 4;
2459 switch (getAlignment(cell)) {
2461 case LYX_ALIGN_LEFT:
2464 case LYX_ALIGN_RIGHT:
2468 case LYX_ALIGN_CENTER:
2474 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2476 if (rightLine(cell))
2485 int LyXTabular::ascii(Buffer const & buf, ostream & os, int const depth,
2486 bool onlydata, unsigned char delim) const
2490 // first calculate the width of the single columns
2491 vector<unsigned int> clen(columns_);
2494 // first all non (real) multicolumn cells!
2495 for (int j = 0; j < columns_; ++j) {
2497 for (int i = 0; i < rows_; ++i) {
2498 int cell = getCellNumber(i, j);
2499 if (isMultiColumnReal(cell))
2502 getCellInset(cell).ascii(buf, sstr, 0);
2503 if (clen[j] < sstr.str().length())
2504 clen[j] = sstr.str().length();
2507 // then all (real) multicolumn cells!
2508 for (int j = 0; j < columns_; ++j) {
2509 for (int i = 0; i < rows_; ++i) {
2510 int cell = getCellNumber(i, j);
2511 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2514 getCellInset(cell).ascii(buf, sstr, 0);
2515 int len = int(sstr.str().length());
2516 int const n = cells_in_multicolumn(cell);
2517 for (int k = j; len > 0 && k < j + n - 1; ++k)
2519 if (len > int(clen[j + n - 1]))
2520 clen[j + n - 1] = len;
2525 for (int i = 0; i < rows_; ++i) {
2526 if (!onlydata && asciiTopHLine(os, i, clen))
2527 for (int j = 0; j < depth; ++j)
2529 for (int j = 0; j < columns_; ++j) {
2530 if (isPartOfMultiColumn(i, j))
2532 if (onlydata && j > 0)
2534 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2539 for (int j = 0; j < depth; ++j)
2541 if (asciiBottomHLine(os, i, clen))
2542 for (int j = 0; j < depth; ++j)
2550 InsetText & LyXTabular::getCellInset(int cell) const
2552 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2556 InsetText & LyXTabular::getCellInset(int row, int column) const
2558 return cell_info[row][column].inset;
2562 int LyXTabular::getCellFromInset(InsetOld const * inset) const
2564 // is this inset part of the tabular?
2566 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2570 for (int cell = getNumberOfCells(); cell >= 0; --cell)
2571 if (&getCellInset(cell) == inset) {
2572 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2573 << "cell=" << cell << endl;
2577 // We should have found a cell at this point
2578 lyxerr << "LyXTabular::getCellFromInset: Cell not found!" << endl;
2583 void LyXTabular::validate(LaTeXFeatures & features) const
2585 features.require("NeedTabularnewline");
2586 if (isLongTabular())
2587 features.require("longtable");
2589 features.require("rotating");
2590 for (int cell = 0; cell < numberofcells; ++cell) {
2591 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2592 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2593 features.require("array");
2594 getCellInset(cell).validate(features);
2599 void LyXTabular::getLabelList(Buffer const & buffer,
2600 std::vector<string> & list) const
2602 for (int i = 0; i < rows_; ++i)
2603 for (int j = 0; j < columns_; ++j)
2604 getCellInset(i, j).getLabelList(buffer, list);
2608 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2610 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2611 ParagraphList::const_iterator cit = parlist.begin();
2612 ParagraphList::const_iterator end = parlist.end();
2614 for (; cit != end; ++cit)
2615 for (int i = 0; i < cit->size(); ++i)
2616 if (cit->isNewline(i))