1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2002 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #include "LaTeXFeatures.h"
24 #include "insets/insettabular.h"
25 #include "insets/insettext.h"
27 #include "support/LAssert.h"
28 #include "support/LIstream.h"
29 #include "support/lstrings.h"
30 #include "support/lyxmanip.h"
31 #include "support/tostr.h"
33 #include "frontends/Alert.h"
34 #include "frontends/Painter.h"
39 using namespace lyx::support;
49 #ifndef CXX_GLOBAL_CSTD
55 int const WIDTH_OF_LINE = 5;
57 string const write_attribute(string const & name, string const & t)
59 return t.empty() ? t : " " + name + "=\"" + t + "\"";
63 string const write_attribute(string const & name, bool const & b)
65 // we write only true attribute values so we remove a bit of the
66 // file format bloat for tabulars.
67 return b ? write_attribute(name, tostr(b)) : string();
71 string const write_attribute(string const & name, int const & i)
73 // we write only true attribute values so we remove a bit of the
74 // file format bloat for tabulars.
75 return i ? write_attribute(name, tostr(i)) : string();
79 string const write_attribute(string const & name, LyXLength const & value)
81 // we write only the value if we really have one same reson as above.
82 return value.zero() ? string() : write_attribute(name, value.asString());
86 string const tostr(LyXAlignment const & num)
95 case LYX_ALIGN_CENTER:
99 case LYX_ALIGN_LAYOUT:
101 case LYX_ALIGN_SPECIAL:
108 string const tostr(LyXTabular::VAlignment const & num)
111 case LyXTabular::LYX_VALIGN_TOP:
113 case LyXTabular::LYX_VALIGN_CENTER:
115 case LyXTabular::LYX_VALIGN_BOTTOM:
122 string const tostr(LyXTabular::BoxType const & num)
125 case LyXTabular::BOX_NONE:
127 case LyXTabular::BOX_PARBOX:
129 case LyXTabular::BOX_MINIPAGE:
135 // I would have liked a fromstr template a lot better. (Lgb)
136 bool string2type(string const str, LyXAlignment & num)
139 num = LYX_ALIGN_NONE;
140 else if (str == "block")
141 num = LYX_ALIGN_BLOCK;
142 else if (str == "left")
143 num = LYX_ALIGN_LEFT;
144 else if (str == "center")
145 num = LYX_ALIGN_CENTER;
146 else if (str == "right")
147 num = LYX_ALIGN_RIGHT;
154 bool string2type(string const str, LyXTabular::VAlignment & num)
157 num = LyXTabular::LYX_VALIGN_TOP;
158 else if (str == "center")
159 num = LyXTabular::LYX_VALIGN_CENTER;
160 else if (str == "bottom")
161 num = LyXTabular::LYX_VALIGN_BOTTOM;
168 bool string2type(string const str, LyXTabular::BoxType & num)
171 num = LyXTabular::BOX_NONE;
172 else if (str == "parbox")
173 num = LyXTabular::BOX_PARBOX;
174 else if (str == "minipage")
175 num = LyXTabular::BOX_MINIPAGE;
182 bool string2type(string const str, bool & num)
186 else if (str == "false")
194 bool getTokenValue(string const & str, const char * token, string & ret)
197 size_t token_length = strlen(token);
198 string::size_type pos = str.find(token);
200 if (pos == string::npos || pos + token_length + 1 >= str.length()
201 || str[pos + token_length] != '=')
203 pos += token_length + 1;
205 if ((ch != '"') && (ch != '\'')) { // only read till next space
209 while ((pos < str.length() - 1) && (str[++pos] != ch))
216 bool getTokenValue(string const & str, const char * token, int & num)
220 if (!getTokenValue(str, token, tmp))
227 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
230 if (!getTokenValue(str, token, tmp))
232 return string2type(tmp, num);
236 bool getTokenValue(string const & str, const char * token,
237 LyXTabular::VAlignment & num)
240 if (!getTokenValue(str, token, tmp))
242 return string2type(tmp, num);
246 bool getTokenValue(string const & str, const char * token,
247 LyXTabular::BoxType & num)
250 if (!getTokenValue(str, token, tmp))
252 return string2type(tmp, num);
256 bool getTokenValue(string const & str, const char * token, bool & flag)
258 // set the flag always to false as this should be the default for bools
259 // not in the file-format.
262 if (!getTokenValue(str, token, tmp))
264 return string2type(tmp, flag);
268 bool getTokenValue(string const & str, const char * token, LyXLength & len)
270 // set the lenght to be zero() as default as this it should be if not
271 // in the file format.
274 if (!getTokenValue(str, token, tmp))
276 return isValidLength(tmp, &len);
280 void l_getline(istream & is, string & str)
283 while (str.empty()) {
285 if (!str.empty() && str[str.length() - 1] == '\r')
286 str.erase(str.length() - 1);
292 /// Define a few methods for the inner structs
294 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
299 multicolumn = LyXTabular::CELL_NORMAL;
300 alignment = LYX_ALIGN_CENTER;
301 valignment = LYX_VALIGN_TOP;
311 LyXTabular::rowstruct::rowstruct()
318 endfirsthead = false;
325 LyXTabular::columnstruct::columnstruct()
329 alignment = LYX_ALIGN_CENTER;
330 valignment = LYX_VALIGN_TOP;
335 LyXTabular::ltType::ltType()
343 LyXTabular::LyXTabular(BufferParams const & bp,
344 InsetTabular * inset, int rows_arg, int columns_arg)
348 init(bp, rows_arg, columns_arg);
352 LyXTabular::LyXTabular(BufferParams const & bp,
353 InsetTabular * inset, LyXTabular const & lt)
357 init(bp, lt.rows_, lt.columns_, <);
361 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
369 // activates all lines and sets all widths to 0
370 void LyXTabular::init(BufferParams const & bp,
371 int rows_arg, int columns_arg, LyXTabular const * lt)
374 columns_ = columns_arg;
375 row_info = row_vector(rows_, rowstruct());
376 column_info = column_vector(columns_, columnstruct());
377 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
385 for (int i = 0; i < rows_; ++i) {
386 for (int j = 0; j < columns_; ++j) {
387 cell_info[i][j].inset.setOwner(owner_);
388 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
389 cell_info[i][j].cellno = cellno++;
391 cell_info[i].back().right_line = true;
393 row_info.back().bottom_line = true;
394 row_info.front().bottom_line = true;
396 for (int i = 0; i < columns_; ++i) {
397 calculate_width_of_column(i);
399 column_info.back().right_line = true;
401 calculate_width_of_tabular();
403 rowofcell = vector<int>();
404 columnofcell = vector<int>();
405 set_row_column_number_info();
406 is_long_tabular = false;
411 void LyXTabular::appendRow(BufferParams const & bp, int cell)
415 int row = row_of_cell(cell);
417 row_vector::iterator rit = row_info.begin() + row;
418 row_info.insert(rit, rowstruct());
419 // now set the values of the row before
420 row_info[row] = row_info[row + 1];
423 cell_vvector::iterator cit = cell_info.begin() + row;
424 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
426 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
429 for (int i = 0; i <= row; ++i) {
430 for (int j = 0; j < columns_; ++j) {
431 c_info[i][j] = cell_info[i][j];
434 for (int i = row + 1; i < rows_; ++i) {
435 for (int j = 0; j < columns_; ++j) {
436 c_info[i][j] = cell_info[i-1][j];
441 for (int j = 0; j < columns_; ++j) {
442 cell_info[row][j].inset.clear(false);
443 if (bp.tracking_changes)
444 cell_info[row][j].inset.markNew(true);
451 void LyXTabular::deleteRow(int row)
453 // Not allowed to delete last row
457 row_info.erase(row_info.begin() + row);
458 cell_info.erase(cell_info.begin() + row);
464 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
468 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
470 int const column = column_of_cell(cell);
471 column_vector::iterator cit = column_info.begin() + column + 1;
472 column_info.insert(cit, columnstruct());
473 // set the column values of the column before
474 column_info[column + 1] = column_info[column];
476 for (int i = 0; i < rows_; ++i) {
477 for (int j = 0; j <= column; ++j) {
478 c_info[i][j] = cell_info[i][j];
480 for (int j = column + 1; j < columns_; ++j) {
481 c_info[i][j] = cell_info[i][j - 1];
483 // care about multicolumns
484 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
486 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
488 if (column + 2 >= columns_ ||
489 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
491 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
496 for (int i = 0; i < rows_; ++i) {
497 cell_info[i][column + 1].inset.clear(false);
498 if (bp.tracking_changes)
499 cell_info[i][column + 1].inset.markNew(true);
505 void LyXTabular::deleteColumn(int column)
507 // Not allowed to delete last column
511 column_info.erase(column_info.begin() + column);
512 for (int i = 0; i < rows_; ++i)
513 cell_info[i].erase(cell_info[i].begin() + column);
519 void LyXTabular::reinit()
525 void LyXTabular::Reinit(bool reset_widths)
528 for (int i = 0; i < rows_; ++i) {
529 for (int j = 0; j < columns_; ++j) {
530 cell_info[i][j].width_of_cell = 0;
531 cell_info[i][j].inset.setOwner(owner_);
536 for (int i = 0; i < columns_; ++i)
537 calculate_width_of_column(i);
539 calculate_width_of_tabular();
541 set_row_column_number_info();
545 void LyXTabular::set_row_column_number_info(bool oldformat)
548 for (int row = 0; row < rows_; ++row) {
549 for (int column = 0; column < columns_; ++column) {
550 if (cell_info[row][column].multicolumn
551 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
553 cell_info[row][column].cellno = numberofcells;
556 ++numberofcells; // because this is one more than as we start from 0
558 rowofcell.resize(numberofcells);
559 columnofcell.resize(numberofcells);
561 for (int row = 0, column = 0, c = 0;
562 c < numberofcells && row < rows_ && column < columns_;) {
564 columnofcell[c] = column;
568 } while (column < columns_ &&
569 cell_info[row][column].multicolumn
570 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
571 if (column == columns_) {
577 for (int row = 0; row < rows_; ++row) {
578 for (int column = 0; column < columns_; ++column) {
579 if (isPartOfMultiColumn(row,column))
581 // now set the right line of multicolumns right for oldformat read
583 cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
585 int cn = cells_in_multicolumn(cell_info[row][column].cellno);
586 cell_info[row][column].right_line =
587 cell_info[row][column+cn-1].right_line;
589 cell_info[row][column].inset.setAutoBreakRows(
590 !getPWidth(getCellNumber(row, column)).zero());
596 int LyXTabular::getNumberOfCells() const
598 return numberofcells;
602 int LyXTabular::numberOfCellsInRow(int cell) const
604 int const row = row_of_cell(cell);
606 for (int i = 0; i < columns_; ++i)
607 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
613 // returns 1 if there is a topline, returns 0 if not
614 bool LyXTabular::topLine(int cell, bool onlycolumn) const
616 if (!onlycolumn && isMultiColumn(cell))
617 return cellinfo_of_cell(cell).top_line;
618 return row_info[row_of_cell(cell)].top_line;
622 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
624 if (!onlycolumn && isMultiColumn(cell))
625 return cellinfo_of_cell(cell).bottom_line;
626 return row_info[row_of_cell(cell)].bottom_line;
630 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
632 if (!onlycolumn && isMultiColumn(cell) &&
633 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
635 if (cellinfo_of_cell(cell).align_special.empty())
636 return cellinfo_of_cell(cell).left_line;
637 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
639 if (column_info[column_of_cell(cell)].align_special.empty())
640 return column_info[column_of_cell(cell)].left_line;
641 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
645 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
647 if (!onlycolumn && isMultiColumn(cell) &&
648 (isLastCellInRow(cell) || isMultiColumn(cell+1)))
650 if (cellinfo_of_cell(cell).align_special.empty())
651 return cellinfo_of_cell(cell).right_line;
652 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
654 if (column_info[column_of_cell(cell)].align_special.empty())
655 return column_info[right_column_of_cell(cell)].right_line;
656 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
660 bool LyXTabular::topAlreadyDrawn(int cell) const
662 int row = row_of_cell(cell);
663 if (row > 0 && !getAdditionalHeight(row)) {
664 int column = column_of_cell(cell);
667 && cell_info[row][column].multicolumn
668 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
670 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
671 return row_info[row].bottom_line;
673 return cell_info[row][column].bottom_line;
679 bool LyXTabular::leftAlreadyDrawn(int cell) const
681 int column = column_of_cell(cell);
683 int row = row_of_cell(cell);
685 (cell_info[row][column].multicolumn ==
686 LyXTabular::CELL_PART_OF_MULTICOLUMN));
687 if (getAdditionalWidth(cell_info[row][column].cellno))
689 return rightLine(cell_info[row][column].cellno);
695 bool LyXTabular::isLastRow(int cell) const
697 return (row_of_cell(cell) == rows_ - 1);
701 int LyXTabular::getAdditionalHeight(int row) const
703 if (!row || row >= rows_)
709 for (int column = 0; column < columns_ && bottom; ++column) {
710 switch (cell_info[row - 1][column].multicolumn) {
711 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
712 bottom = cell_info[row - 1][column].bottom_line;
714 case LyXTabular::CELL_NORMAL:
715 bottom = row_info[row - 1].bottom_line;
718 for (int column = 0; column < columns_ && top; ++column) {
719 switch (cell_info[row][column].multicolumn) {
720 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
721 top = cell_info[row][column].top_line;
723 case LyXTabular::CELL_NORMAL:
724 top = row_info[row].top_line;
728 return WIDTH_OF_LINE;
733 int LyXTabular::getAdditionalWidth(int cell) const
735 // internally already set in setWidthOfCell
736 // used to get it back in text.C
737 int const col = right_column_of_cell(cell);
738 int const row = row_of_cell(cell);
739 if (col < columns_ - 1 && rightLine(cell) &&
740 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
742 return WIDTH_OF_LINE;
748 // returns the maximum over all rows
749 int LyXTabular::getWidthOfColumn(int cell) const
751 int const column1 = column_of_cell(cell);
752 int const column2 = right_column_of_cell(cell);
754 for (int i = column1; i <= column2; ++i)
755 result += column_info[i].width_of_column;
760 int LyXTabular::getWidthOfTabular() const
762 return width_of_tabular;
766 // returns true if a complete update is necessary, otherwise false
767 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
769 if (!isMultiColumn(cell))
772 int const row = row_of_cell(cell);
773 int const column1 = column_of_cell(cell);
774 int const column2 = right_column_of_cell(cell);
775 int const old_val = cell_info[row][column2].width_of_cell;
777 // first set columns to 0 so we can calculate the right width
778 for (int i = column1; i <= column2; ++i) {
779 cell_info[row][i].width_of_cell = 0;
781 // set the width to MAX_WIDTH until width > 0
782 int width = new_width + 2 * WIDTH_OF_LINE;
784 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
785 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
786 width -= column_info[i].width_of_column;
789 cell_info[row][i].width_of_cell = width;
791 if (old_val != cell_info[row][column2].width_of_cell) {
792 // in this case we have to recalculate all multicolumn cells which
793 // have this column as one of theirs but not as last one
794 calculate_width_of_column_NMC(i);
795 recalculateMulticolumnsOfColumn(i);
796 calculate_width_of_column(i);
802 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
804 // the last column does not have to be recalculated because all
805 // multicolumns will have here there last multicolumn cell which
806 // always will have the whole rest of the width of the cell.
807 if (column > (columns_ - 2))
809 for(int row = 0; row < rows_; ++row) {
810 int mc = cell_info[row][column].multicolumn;
811 int nmc = cell_info[row][column+1].multicolumn;
812 // we only have to update multicolumns which do not have this
813 // column as their last column!
814 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
815 (mc == CELL_PART_OF_MULTICOLUMN &&
816 nmc == CELL_PART_OF_MULTICOLUMN))
818 int const cellno = cell_info[row][column].cellno;
819 setWidthOfMulticolCell(cellno,
820 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
826 // returns 1 if a complete update is necessary, otherwise 0
827 bool LyXTabular::setWidthOfCell(int cell, int new_width)
829 int const row = row_of_cell(cell);
830 int const column1 = column_of_cell(cell);
835 if (rightLine(cell_info[row][column1].cellno, true) &&
836 column1 < columns_ - 1 &&
837 leftLine(cell_info[row][column1+1].cellno, true))
840 add_width = WIDTH_OF_LINE;
842 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width) {
845 if (isMultiColumn(cell, true)) {
846 tmp = setWidthOfMulticolCell(cell, new_width);
848 width = new_width + 2 * WIDTH_OF_LINE + add_width;
849 cell_info[row][column1].width_of_cell = width;
850 tmp = calculate_width_of_column_NMC(column1);
852 recalculateMulticolumnsOfColumn(column1);
855 for (int i = 0; i < columns_; ++i)
856 calculate_width_of_column(i);
857 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 bool 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;
1739 bool LyXTabular::setDescentOfRow(int row, int height)
1741 if (row >= rows_ || row_info[row].descent_of_row == height)
1743 row_info[row].descent_of_row = height;
1748 int LyXTabular::getAscentOfRow(int row) const
1752 return row_info[row].ascent_of_row;
1756 int LyXTabular::getDescentOfRow(int row) const
1760 return row_info[row].descent_of_row;
1764 int LyXTabular::getHeightOfTabular() const
1767 for (int row = 0; row < rows_; ++row)
1768 height += getAscentOfRow(row) + getDescentOfRow(row) +
1769 getAdditionalHeight(row);
1774 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1776 if (row >= rows_ || column >= columns_)
1778 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1782 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1784 if (row < 0 || row >= rows_)
1787 int const fcell = getFirstCellInRow(row);
1788 int const n = numberOfCellsInRow(fcell) + fcell;
1791 for (int i = fcell; i < n; ++i) {
1795 if (tmp == n - fcell) {
1798 for (int i = fcell; i < n; ++i) {
1801 << column_of_cell(i) + 1
1803 << right_column_of_cell(i) + 1
1815 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1817 if (row < 0 || row >= rows_)
1820 int const fcell = getFirstCellInRow(row);
1821 int const n = numberOfCellsInRow(fcell) + fcell;
1824 for (int i = fcell; i < n; ++i) {
1828 if (tmp == n - fcell) {
1831 for (int i = fcell; i < n; ++i) {
1832 if (bottomLine(i)) {
1834 << column_of_cell(i) + 1
1836 << right_column_of_cell(i) + 1
1848 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1852 if (getRotateCell(cell)) {
1853 os << "\\begin{sideways}\n";
1856 if (isMultiColumn(cell)) {
1857 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1858 if (!cellinfo_of_cell(cell).align_special.empty()) {
1859 os << cellinfo_of_cell(cell).align_special << "}{";
1861 if (leftLine(cell) &&
1862 (isFirstCellInRow(cell) ||
1863 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1864 !rightLine(cell - 1, true))))
1868 if (!getPWidth(cell).zero()) {
1869 switch (getVAlignment(cell)) {
1870 case LYX_VALIGN_TOP:
1873 case LYX_VALIGN_CENTER:
1876 case LYX_VALIGN_BOTTOM:
1881 << getPWidth(cell).asLatexString()
1884 switch (getAlignment(cell)) {
1885 case LYX_ALIGN_LEFT:
1888 case LYX_ALIGN_RIGHT:
1896 if (rightLine(cell))
1898 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1904 if (getUsebox(cell) == BOX_PARBOX) {
1906 switch (getVAlignment(cell)) {
1907 case LYX_VALIGN_TOP:
1910 case LYX_VALIGN_CENTER:
1913 case LYX_VALIGN_BOTTOM:
1917 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1918 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1919 os << "\\begin{minipage}[";
1920 switch (getVAlignment(cell)) {
1921 case LYX_VALIGN_TOP:
1924 case LYX_VALIGN_CENTER:
1927 case LYX_VALIGN_BOTTOM:
1931 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1938 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1943 if (getUsebox(cell) == BOX_PARBOX)
1945 else if (getUsebox(cell) == BOX_MINIPAGE) {
1946 os << "%\n\\end{minipage}";
1949 if (isMultiColumn(cell)) {
1952 if (getRotateCell(cell)) {
1953 os << "%\n\\end{sideways}";
1960 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
1961 LatexRunParams const & runparams) const
1963 if (!is_long_tabular)
1967 // output header info
1969 if (endhead.topDL) {
1973 for (int i = 0; i < rows_; ++i) {
1974 if (row_info[i].endhead) {
1975 ret += TeXRow(os, i, buf, runparams);
1978 if (endhead.bottomDL) {
1982 os << "\\endhead\n";
1984 if (endfirsthead.empty) {
1985 os << "\\endfirsthead\n";
1989 // output firstheader info
1990 if (haveLTFirstHead()) {
1991 if (endfirsthead.topDL) {
1995 for (int i = 0; i < rows_; ++i) {
1996 if (row_info[i].endfirsthead) {
1997 ret += TeXRow(os, i, buf, runparams);
2000 if (endfirsthead.bottomDL) {
2004 os << "\\endfirsthead\n";
2007 // output footer info
2009 if (endfoot.topDL) {
2013 for (int i = 0; i < rows_; ++i) {
2014 if (row_info[i].endfoot) {
2015 ret += TeXRow(os, i, buf, runparams);
2018 if (endfoot.bottomDL) {
2022 os << "\\endfoot\n";
2024 if (endlastfoot.empty) {
2025 os << "\\endlastfoot\n";
2029 // output lastfooter info
2030 if (haveLTLastFoot()) {
2031 if (endlastfoot.topDL) {
2035 for (int i = 0; i < rows_; ++i) {
2036 if (row_info[i].endlastfoot) {
2037 ret += TeXRow(os, i, buf, runparams);
2040 if (endlastfoot.bottomDL) {
2044 os << "\\endlastfoot\n";
2051 bool LyXTabular::isValidRow(int const row) const
2053 if (!is_long_tabular)
2055 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2056 !row_info[row].endfoot && !row_info[row].endlastfoot);
2060 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2061 LatexRunParams const & runparams) const
2064 int cell = getCellNumber(i, 0);
2066 ret += TeXTopHLine(os, i);
2067 for (int j = 0; j < columns_; ++j) {
2068 if (isPartOfMultiColumn(i, j))
2070 ret += TeXCellPreamble(os, cell);
2071 InsetText & inset = getCellInset(cell);
2073 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(buf->params) &&
2074 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2078 ret += inset.latex(buf, os, runparams);
2082 ret += TeXCellPostamble(os, cell);
2083 if (!isLastCellInRow(cell)) { // not last cell in row
2089 os << "\\tabularnewline\n";
2091 ret += TeXBottomHLine(os, i);
2096 int LyXTabular::latex(Buffer const * buf, ostream & os,
2097 LatexRunParams const & runparams) const
2101 //+---------------------------------------------------------------------
2102 //+ first the opening preamble +
2103 //+---------------------------------------------------------------------
2106 os << "\\begin{sideways}\n";
2109 if (is_long_tabular)
2110 os << "\\begin{longtable}{";
2112 os << "\\begin{tabular}{";
2113 for (int i = 0; i < columns_; ++i) {
2114 if (!column_info[i].align_special.empty()) {
2115 os << column_info[i].align_special;
2117 if (column_info[i].left_line)
2119 if (!column_info[i].p_width.zero()) {
2120 switch (column_info[i].alignment) {
2121 case LYX_ALIGN_LEFT:
2122 os << ">{\\raggedright}";
2124 case LYX_ALIGN_RIGHT:
2125 os << ">{\\raggedleft}";
2127 case LYX_ALIGN_CENTER:
2128 os << ">{\\centering}";
2130 case LYX_ALIGN_NONE:
2131 case LYX_ALIGN_BLOCK:
2132 case LYX_ALIGN_LAYOUT:
2133 case LYX_ALIGN_SPECIAL:
2137 switch (column_info[i].valignment) {
2138 case LYX_VALIGN_TOP:
2141 case LYX_VALIGN_CENTER:
2144 case LYX_VALIGN_BOTTOM:
2149 << column_info[i].p_width.asLatexString()
2152 switch (column_info[i].alignment) {
2153 case LYX_ALIGN_LEFT:
2156 case LYX_ALIGN_RIGHT:
2164 if (column_info[i].right_line)
2171 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2173 //+---------------------------------------------------------------------
2174 //+ the single row and columns (cells) +
2175 //+---------------------------------------------------------------------
2177 for (int i = 0; i < rows_; ++i) {
2178 if (isValidRow(i)) {
2179 ret += TeXRow(os, i, buf, runparams);
2180 if (is_long_tabular && row_info[i].newpage) {
2181 os << "\\newpage\n";
2187 //+---------------------------------------------------------------------
2188 //+ the closing of the tabular +
2189 //+---------------------------------------------------------------------
2191 if (is_long_tabular)
2192 os << "\\end{longtable}";
2194 os << "\\end{tabular}";
2196 os << "\n\\end{sideways}";
2204 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2207 int cell = getFirstCellInRow(row);
2210 for (int j = 0; j < columns_; ++j) {
2211 if (isPartOfMultiColumn(row, j))
2214 os << "<entry align=\"";
2215 switch (getAlignment(cell)) {
2216 case LYX_ALIGN_LEFT:
2219 case LYX_ALIGN_RIGHT:
2227 os << "\" valign=\"";
2228 switch (getVAlignment(cell)) {
2229 case LYX_VALIGN_TOP:
2232 case LYX_VALIGN_BOTTOM:
2235 case LYX_VALIGN_CENTER:
2240 if (isMultiColumn(cell)) {
2241 os << " namest=\"col" << j << "\" ";
2242 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2246 ret += getCellInset(cell).docbook(buf, os, true);
2255 int LyXTabular::docbook(Buffer const * buf, ostream & os,
2256 bool /*mixcont*/) const
2260 //+---------------------------------------------------------------------
2261 //+ first the opening preamble +
2262 //+---------------------------------------------------------------------
2264 os << "<tgroup cols=\"" << columns_
2265 << "\" colsep=\"1\" rowsep=\"1\">\n";
2267 for (int i = 0; i < columns_; ++i) {
2268 os << "<colspec colname=\"col" << i << "\" align=\"";
2269 switch (column_info[i].alignment) {
2270 case LYX_ALIGN_LEFT:
2273 case LYX_ALIGN_RIGHT:
2284 //+---------------------------------------------------------------------
2285 //+ Long Tabular case +
2286 //+---------------------------------------------------------------------
2288 // output header info
2289 if (haveLTHead() || haveLTFirstHead()) {
2292 for (int i = 0; i < rows_; ++i) {
2293 if (row_info[i].endhead || row_info[i].endfirsthead) {
2294 ret += docbookRow(buf, os, i);
2300 // output footer info
2301 if (haveLTFoot() || haveLTLastFoot()) {
2304 for (int i = 0; i < rows_; ++i) {
2305 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2306 ret += docbookRow(buf, os, i);
2313 //+---------------------------------------------------------------------
2314 //+ the single row and columns (cells) +
2315 //+---------------------------------------------------------------------
2319 for (int i = 0; i < rows_; ++i) {
2320 if (isValidRow(i)) {
2321 ret += docbookRow(buf, os, i);
2326 //+---------------------------------------------------------------------
2327 //+ the closing of the tabular +
2328 //+---------------------------------------------------------------------
2337 int LyXTabular::asciiTopHLine(ostream & os, int row,
2338 vector<unsigned int> const & clen) const
2340 int const fcell = getFirstCellInRow(row);
2341 int const n = numberOfCellsInRow(fcell) + fcell;
2344 for (int i = fcell; i < n; ++i) {
2354 for (int i = fcell; i < n; ++i) {
2365 int column = column_of_cell(i);
2366 int len = clen[column];
2367 while (isPartOfMultiColumn(row, ++column))
2368 len += clen[column] + 4;
2369 os << string(len, ch);
2384 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2385 vector<unsigned int> const & clen) const
2387 int const fcell = getFirstCellInRow(row);
2388 int const n = numberOfCellsInRow(fcell) + fcell;
2391 for (int i = fcell; i < n; ++i) {
2392 if (bottomLine(i)) {
2401 for (int i = fcell; i < n; ++i) {
2402 if (bottomLine(i)) {
2412 int column = column_of_cell(i);
2413 int len = clen[column];
2414 while (isPartOfMultiColumn(row, ++column))
2415 len += clen[column] + 4;
2416 os << string(len, ch);
2417 if (bottomLine(i)) {
2431 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2432 int cell, int row, int column,
2433 vector<unsigned int> const & clen,
2434 bool onlydata) const
2437 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2449 unsigned int len1 = sstr.str().length();
2450 unsigned int len2 = clen[column];
2451 while (isPartOfMultiColumn(row, ++column))
2452 len2 += clen[column] + 4;
2455 switch (getAlignment(cell)) {
2457 case LYX_ALIGN_LEFT:
2460 case LYX_ALIGN_RIGHT:
2464 case LYX_ALIGN_CENTER:
2470 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2472 if (rightLine(cell))
2481 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2482 bool onlydata, unsigned char delim) const
2486 // first calculate the width of the single columns
2487 vector<unsigned int> clen(columns_);
2490 // first all non (real) multicolumn cells!
2491 for (int j = 0; j < columns_; ++j) {
2493 for (int i = 0; i < rows_; ++i) {
2494 int cell = getCellNumber(i, j);
2495 if (isMultiColumn(cell, true))
2498 getCellInset(cell).ascii(buf, sstr, 0);
2499 if (clen[j] < sstr.str().length())
2500 clen[j] = sstr.str().length();
2503 // then all (real) multicolumn cells!
2504 for (int j = 0; j < columns_; ++j) {
2505 for (int i = 0; i < rows_; ++i) {
2506 int cell = getCellNumber(i, j);
2507 if (!isMultiColumn(cell, true) || isPartOfMultiColumn(i, j))
2510 getCellInset(cell).ascii(buf, sstr, 0);
2511 int len = int(sstr.str().length());
2512 int const n = cells_in_multicolumn(cell);
2513 for (int k = j; len > 0 && k < j + n - 1; ++k)
2515 if (len > int(clen[j + n - 1]))
2516 clen[j + n - 1] = len;
2521 for (int i = 0; i < rows_; ++i) {
2522 if (!onlydata && asciiTopHLine(os, i, clen))
2523 for (int j = 0; j < depth; ++j)
2525 for (int j = 0; j < columns_; ++j) {
2526 if (isPartOfMultiColumn(i, j))
2528 if (onlydata && j > 0)
2530 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2535 for (int j = 0; j < depth; ++j)
2537 if (asciiBottomHLine(os, i, clen))
2538 for (int j = 0; j < depth; ++j)
2546 InsetText & LyXTabular::getCellInset(int cell) const
2549 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2553 InsetText & LyXTabular::getCellInset(int row, int column) const
2555 cur_cell = getCellNumber(row, column);
2556 return cell_info[row][column].inset;
2560 int LyXTabular::getCellFromInset(InsetOld const * inset, int maybe_cell) const
2562 // is this inset part of the tabular?
2563 if (!inset || inset->owner() != owner_) {
2564 lyxerr << "this is not a cell of the tabular!" << endl;
2568 const int save_cur_cell = cur_cell;
2569 int cell = cur_cell;
2570 if (&getCellInset(cell) != inset) {
2572 if (cell == -1 || &getCellInset(cell) != inset) {
2578 for (cell = getNumberOfCells(); cell >= 0; --cell) {
2579 if (&getCellInset(cell) == inset)
2582 lyxerr[Debug::INSETTEXT]
2583 << "LyXTabular::getCellFromInset: "
2585 << ", cur_cell=" << save_cur_cell
2586 << ", maybe_cell=" << maybe_cell
2588 // We should have found a cell at this point
2590 lyxerr << "LyXTabular::getCellFromInset: "
2591 << "Cell not found!" << endl;
2599 void LyXTabular::validate(LaTeXFeatures & features) const
2601 features.require("NeedTabularnewline");
2602 if (isLongTabular())
2603 features.require("longtable");
2605 features.require("rotating");
2606 for (int cell = 0; cell < numberofcells; ++cell) {
2607 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2608 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2609 features.require("array");
2610 getCellInset(cell).validate(features);
2615 void LyXTabular::getLabelList(std::vector<string> & list) const
2617 for (int i = 0; i < rows_; ++i)
2618 for (int j = 0; j < columns_; ++j)
2619 getCellInset(i, j).getLabelList(list);
2623 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2625 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2626 ParagraphList::const_iterator cit = parlist.begin();
2627 ParagraphList::const_iterator end = parlist.end();
2629 for (; cit != end; ++cit)
2630 for (int i = 0; i < cit->size(); ++i)
2631 if (cit->isNewline(i))