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.
21 #include "LaTeXFeatures.h"
22 #include "support/std_sstream.h"
27 #include "insets/insettabular.h"
29 #include "support/LAssert.h"
30 #include "support/lstrings.h"
31 #include "support/tostr.h"
35 using namespace lyx::support;
45 #ifndef CXX_GLOBAL_CSTD
51 int const WIDTH_OF_LINE = 5;
54 string const write_attribute(string const & name, T const & t)
56 string const s = tostr(t);
57 return s.empty() ? s : " " + name + "=\"" + s + "\"";
60 string const write_attribute(string const & name, string const & t)
62 return t.empty() ? t : " " + name + "=\"" + t + "\"";
66 string const write_attribute(string const & name, bool const & b)
68 // we write only true attribute values so we remove a bit of the
69 // file format bloat for tabulars.
70 return b ? write_attribute(name, tostr(b)) : string();
74 string const write_attribute(string const & name, int const & i)
76 // we write only true attribute values so we remove a bit of the
77 // file format bloat for tabulars.
78 return i ? write_attribute(name, tostr(i)) : string();
82 string const write_attribute(string const & name, LyXLength const & value)
84 // we write only the value if we really have one same reson as above.
85 return value.zero() ? string() : write_attribute(name, value.asString());
89 string const tostr(LyXAlignment const & num)
98 case LYX_ALIGN_CENTER:
100 case LYX_ALIGN_RIGHT:
102 case LYX_ALIGN_LAYOUT:
104 case LYX_ALIGN_SPECIAL:
111 string const tostr(LyXTabular::VAlignment const & num)
114 case LyXTabular::LYX_VALIGN_TOP:
116 case LyXTabular::LYX_VALIGN_MIDDLE:
118 case LyXTabular::LYX_VALIGN_BOTTOM:
125 string const tostr(LyXTabular::BoxType const & num)
128 case LyXTabular::BOX_NONE:
130 case LyXTabular::BOX_PARBOX:
132 case LyXTabular::BOX_MINIPAGE:
138 // I would have liked a fromstr template a lot better. (Lgb)
139 bool string2type(string const str, LyXAlignment & num)
142 num = LYX_ALIGN_NONE;
143 else if (str == "block")
144 num = LYX_ALIGN_BLOCK;
145 else if (str == "left")
146 num = LYX_ALIGN_LEFT;
147 else if (str == "center")
148 num = LYX_ALIGN_CENTER;
149 else if (str == "right")
150 num = LYX_ALIGN_RIGHT;
157 bool string2type(string const str, LyXTabular::VAlignment & num)
160 num = LyXTabular::LYX_VALIGN_TOP;
161 else if (str == "middle" )
162 num = LyXTabular::LYX_VALIGN_MIDDLE;
163 else if (str == "bottom")
164 num = LyXTabular::LYX_VALIGN_BOTTOM;
171 bool string2type(string const str, LyXTabular::BoxType & num)
174 num = LyXTabular::BOX_NONE;
175 else if (str == "parbox")
176 num = LyXTabular::BOX_PARBOX;
177 else if (str == "minipage")
178 num = LyXTabular::BOX_MINIPAGE;
185 bool string2type(string const str, bool & num)
189 else if (str == "false")
197 bool getTokenValue(string const & str, const char * token, string & ret)
200 size_t token_length = strlen(token);
201 string::size_type pos = str.find(token);
203 if (pos == string::npos || pos + token_length + 1 >= str.length()
204 || str[pos + token_length] != '=')
206 pos += token_length + 1;
208 if ((ch != '"') && (ch != '\'')) { // only read till next space
212 while ((pos < str.length() - 1) && (str[++pos] != ch))
219 bool getTokenValue(string const & str, const char * token, int & num)
223 if (!getTokenValue(str, token, tmp))
230 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
233 if (!getTokenValue(str, token, tmp))
235 return string2type(tmp, num);
239 bool getTokenValue(string const & str, const char * token,
240 LyXTabular::VAlignment & num)
243 if (!getTokenValue(str, token, tmp))
245 return string2type(tmp, num);
249 bool getTokenValue(string const & str, const char * token,
250 LyXTabular::BoxType & num)
253 if (!getTokenValue(str, token, tmp))
255 return string2type(tmp, num);
259 bool getTokenValue(string const & str, const char * token, bool & flag)
261 // set the flag always to false as this should be the default for bools
262 // not in the file-format.
265 if (!getTokenValue(str, token, tmp))
267 return string2type(tmp, flag);
271 bool getTokenValue(string const & str, const char * token, LyXLength & len)
273 // set the lenght to be zero() as default as this it should be if not
274 // in the file format.
277 if (!getTokenValue(str, token, tmp))
279 return isValidLength(tmp, &len);
283 void l_getline(istream & is, string & str)
286 while (str.empty()) {
288 if (!str.empty() && str[str.length() - 1] == '\r')
289 str.erase(str.length() - 1);
295 /// Define a few methods for the inner structs
297 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
302 multicolumn = LyXTabular::CELL_NORMAL;
303 alignment = LYX_ALIGN_CENTER;
304 valignment = LYX_VALIGN_TOP;
314 LyXTabular::rowstruct::rowstruct()
321 endfirsthead = false;
328 LyXTabular::columnstruct::columnstruct()
332 alignment = LYX_ALIGN_CENTER;
333 valignment = LYX_VALIGN_TOP;
338 LyXTabular::ltType::ltType()
346 LyXTabular::LyXTabular(BufferParams const & bp,
347 InsetTabular * inset, int rows_arg, int columns_arg)
351 init(bp, rows_arg, columns_arg);
355 LyXTabular::LyXTabular(BufferParams const & bp,
356 InsetTabular * inset, LyXTabular const & lt)
360 init(bp, lt.rows_, lt.columns_, <);
364 LyXTabular::LyXTabular(Buffer const & buf, InsetTabular * inset, LyXLex & lex)
372 // activates all lines and sets all widths to 0
373 void LyXTabular::init(BufferParams const & bp,
374 int rows_arg, int columns_arg, LyXTabular const * lt)
377 columns_ = columns_arg;
378 row_info = row_vector(rows_, rowstruct());
379 column_info = column_vector(columns_, columnstruct());
380 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
388 for (int i = 0; i < rows_; ++i) {
389 for (int j = 0; j < columns_; ++j) {
390 cell_info[i][j].inset.setOwner(owner_);
391 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
392 cell_info[i][j].cellno = cellno++;
394 cell_info[i].back().right_line = true;
396 row_info.back().bottom_line = true;
397 row_info.front().bottom_line = true;
399 for (int i = 0; i < columns_; ++i)
400 calculate_width_of_column(i);
402 column_info.back().right_line = true;
404 calculate_width_of_tabular();
407 columnofcell.clear();
408 set_row_column_number_info();
409 is_long_tabular = false;
414 void LyXTabular::appendRow(BufferParams const & bp, int cell)
418 int row = row_of_cell(cell);
420 row_vector::iterator rit = row_info.begin() + row;
421 row_info.insert(rit, rowstruct());
422 // now set the values of the row before
423 row_info[row] = row_info[row + 1];
426 cell_vvector::iterator cit = cell_info.begin() + row;
427 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
429 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
432 for (int i = 0; i <= row; ++i)
433 for (int j = 0; j < columns_; ++j)
434 c_info[i][j] = cell_info[i][j];
436 for (int i = row + 1; i < rows_; ++i)
437 for (int j = 0; j < columns_; ++j)
438 c_info[i][j] = cell_info[i-1][j];
442 for (int j = 0; j < columns_; ++j) {
443 cell_info[row][j].inset.clear(false);
444 if (bp.tracking_changes)
445 cell_info[row][j].inset.markNew(true);
452 void LyXTabular::deleteRow(int row)
454 // Not allowed to delete last row
458 row_info.erase(row_info.begin() + row);
459 cell_info.erase(cell_info.begin() + row);
465 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
469 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
471 int const column = column_of_cell(cell);
472 column_vector::iterator cit = column_info.begin() + column + 1;
473 column_info.insert(cit, columnstruct());
474 // set the column values of the column before
475 column_info[column + 1] = column_info[column];
477 for (int i = 0; i < rows_; ++i) {
478 for (int j = 0; j <= column; ++j)
479 c_info[i][j] = cell_info[i][j];
481 for (int j = column + 1; j < columns_; ++j)
482 c_info[i][j] = cell_info[i][j - 1];
484 // care about multicolumns
485 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)
490 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
494 for (int i = 0; i < rows_; ++i) {
495 cell_info[i][column + 1].inset.clear(false);
496 if (bp.tracking_changes)
497 cell_info[i][column + 1].inset.markNew(true);
503 void LyXTabular::deleteColumn(int column)
505 // Not allowed to delete last column
509 column_info.erase(column_info.begin() + column);
510 for (int i = 0; i < rows_; ++i)
511 cell_info[i].erase(cell_info[i].begin() + column);
517 void LyXTabular::reinit()
523 void LyXTabular::Reinit(bool reset_widths)
526 for (int i = 0; i < rows_; ++i) {
527 for (int j = 0; j < columns_; ++j) {
528 cell_info[i][j].width_of_cell = 0;
529 cell_info[i][j].inset.setOwner(owner_);
534 for (int i = 0; i < columns_; ++i)
535 calculate_width_of_column(i);
537 calculate_width_of_tabular();
539 set_row_column_number_info();
543 void LyXTabular::set_row_column_number_info(bool oldformat)
546 for (int row = 0; row < rows_; ++row) {
547 for (int column = 0; column < columns_; ++column) {
548 if (cell_info[row][column].multicolumn
549 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
551 cell_info[row][column].cellno = numberofcells;
554 ++numberofcells; // because this is one more than as we start from 0
556 rowofcell.resize(numberofcells);
557 columnofcell.resize(numberofcells);
559 for (int row = 0, column = 0, c = 0;
560 c < numberofcells && row < rows_ && column < columns_;) {
562 columnofcell[c] = column;
566 } while (column < columns_ &&
567 cell_info[row][column].multicolumn
568 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
570 if (column == columns_) {
576 for (int row = 0; row < rows_; ++row) {
577 for (int column = 0; column < columns_; ++column) {
578 if (isPartOfMultiColumn(row,column))
580 // now set the right line of multicolumns right for oldformat read
582 cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
584 int cn = cells_in_multicolumn(cell_info[row][column].cellno);
585 cell_info[row][column].right_line =
586 cell_info[row][column+cn-1].right_line;
588 cell_info[row][column].inset.setAutoBreakRows(
589 !getPWidth(getCellNumber(row, column)).zero());
595 int LyXTabular::getNumberOfCells() const
597 return numberofcells;
601 int LyXTabular::numberOfCellsInRow(int cell) const
603 int const row = row_of_cell(cell);
605 for (int i = 0; i < columns_; ++i)
606 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
612 // returns 1 if there is a topline, returns 0 if not
613 bool LyXTabular::topLine(int cell, bool onlycolumn) const
615 if (!onlycolumn && isMultiColumn(cell))
616 return cellinfo_of_cell(cell).top_line;
617 return row_info[row_of_cell(cell)].top_line;
621 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
623 if (!onlycolumn && isMultiColumn(cell))
624 return cellinfo_of_cell(cell).bottom_line;
625 return row_info[row_of_cell(cell)].bottom_line;
629 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
631 if (!onlycolumn && isMultiColumn(cell) &&
632 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
634 if (cellinfo_of_cell(cell).align_special.empty())
635 return cellinfo_of_cell(cell).left_line;
636 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
638 if (column_info[column_of_cell(cell)].align_special.empty())
639 return column_info[column_of_cell(cell)].left_line;
640 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
644 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
646 if (!onlycolumn && isMultiColumn(cell) &&
647 (isLastCellInRow(cell) || isMultiColumn(cell+1)))
649 if (cellinfo_of_cell(cell).align_special.empty())
650 return cellinfo_of_cell(cell).right_line;
651 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
653 if (column_info[column_of_cell(cell)].align_special.empty())
654 return column_info[right_column_of_cell(cell)].right_line;
655 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
659 bool LyXTabular::topAlreadyDrawn(int cell) const
661 int row = row_of_cell(cell);
662 if (row > 0 && !getAdditionalHeight(row)) {
663 int column = column_of_cell(cell);
666 && cell_info[row][column].multicolumn
667 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
669 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
670 return row_info[row].bottom_line;
672 return cell_info[row][column].bottom_line;
678 bool LyXTabular::leftAlreadyDrawn(int cell) const
680 int column = column_of_cell(cell);
682 int row = row_of_cell(cell);
684 (cell_info[row][column].multicolumn ==
685 LyXTabular::CELL_PART_OF_MULTICOLUMN));
686 if (getAdditionalWidth(cell_info[row][column].cellno))
688 return rightLine(cell_info[row][column].cellno);
694 bool LyXTabular::isLastRow(int cell) const
696 return (row_of_cell(cell) == rows_ - 1);
700 int LyXTabular::getAdditionalHeight(int row) const
702 if (!row || row >= rows_)
708 for (int column = 0; column < columns_ && bottom; ++column) {
709 switch (cell_info[row - 1][column].multicolumn) {
710 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
711 bottom = cell_info[row - 1][column].bottom_line;
713 case LyXTabular::CELL_NORMAL:
714 bottom = row_info[row - 1].bottom_line;
717 for (int column = 0; column < columns_ && top; ++column) {
718 switch (cell_info[row][column].multicolumn) {
719 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
720 top = cell_info[row][column].top_line;
722 case LyXTabular::CELL_NORMAL:
723 top = row_info[row].top_line;
727 return WIDTH_OF_LINE;
732 int LyXTabular::getAdditionalWidth(int cell) const
734 // internally already set in setWidthOfCell
735 // used to get it back in text.C
736 int const col = right_column_of_cell(cell);
737 int const row = row_of_cell(cell);
738 if (col < columns_ - 1 && rightLine(cell) &&
739 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
741 return WIDTH_OF_LINE;
747 // returns the maximum over all rows
748 int LyXTabular::getWidthOfColumn(int cell) const
750 int const column1 = column_of_cell(cell);
751 int const column2 = right_column_of_cell(cell);
753 for (int i = column1; i <= column2; ++i)
754 result += column_info[i].width_of_column;
759 int LyXTabular::getWidthOfTabular() const
761 return width_of_tabular;
765 // returns true if a complete update is necessary, otherwise false
766 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
768 if (!isMultiColumn(cell))
771 int const row = row_of_cell(cell);
772 int const column1 = column_of_cell(cell);
773 int const column2 = right_column_of_cell(cell);
774 int const old_val = cell_info[row][column2].width_of_cell;
776 // first set columns to 0 so we can calculate the right width
777 for (int i = column1; i <= column2; ++i) {
778 cell_info[row][i].width_of_cell = 0;
780 // set the width to MAX_WIDTH until width > 0
781 int width = new_width + 2 * WIDTH_OF_LINE;
783 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
784 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
785 width -= column_info[i].width_of_column;
788 cell_info[row][i].width_of_cell = width;
790 if (old_val != cell_info[row][column2].width_of_cell) {
791 // in this case we have to recalculate all multicolumn cells which
792 // have this column as one of theirs but not as last one
793 calculate_width_of_column_NMC(i);
794 recalculateMulticolumnsOfColumn(i);
795 calculate_width_of_column(i);
801 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
803 // the last column does not have to be recalculated because all
804 // multicolumns will have here there last multicolumn cell which
805 // always will have the whole rest of the width of the cell.
806 if (column > (columns_ - 2))
808 for(int row = 0; row < rows_; ++row) {
809 int mc = cell_info[row][column].multicolumn;
810 int nmc = cell_info[row][column+1].multicolumn;
811 // we only have to update multicolumns which do not have this
812 // column as their last column!
813 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
814 (mc == CELL_PART_OF_MULTICOLUMN &&
815 nmc == CELL_PART_OF_MULTICOLUMN))
817 int const cellno = cell_info[row][column].cellno;
818 setWidthOfMulticolCell(cellno,
819 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
825 // returns 1 if a complete update is necessary, otherwise 0
826 void LyXTabular::setWidthOfCell(int cell, int new_width)
828 int const row = row_of_cell(cell);
829 int const column1 = column_of_cell(cell);
834 if (rightLine(cell_info[row][column1].cellno, true) &&
835 column1 < columns_ - 1 &&
836 leftLine(cell_info[row][column1+1].cellno, true))
838 add_width = WIDTH_OF_LINE;
841 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
844 if (isMultiColumn(cell, true)) {
845 tmp = setWidthOfMulticolCell(cell, new_width);
847 width = new_width + 2 * WIDTH_OF_LINE + add_width;
848 cell_info[row][column1].width_of_cell = width;
849 tmp = calculate_width_of_column_NMC(column1);
851 recalculateMulticolumnsOfColumn(column1);
854 for (int i = 0; i < columns_; ++i)
855 calculate_width_of_column(i);
856 calculate_width_of_tabular();
861 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
863 if (!isMultiColumn(cell) || onlycolumn)
864 column_info[column_of_cell(cell)].alignment = align;
866 cellinfo_of_cell(cell).alignment = align;
870 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
872 if (!isMultiColumn(cell) || onlycolumn)
873 column_info[column_of_cell(cell)].valignment = align;
875 cellinfo_of_cell(cell).valignment = align;
879 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
881 int const j = column_of_cell(cell);
883 column_info[j].p_width = width;
884 for (int i = 0; i < rows_; ++i) {
885 int const cell = getCellNumber(i, j);
886 // because of multicolumns
887 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
892 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
894 bool const flag = !width.zero();
896 cellinfo_of_cell(cell).p_width = width;
897 if (isMultiColumn(cell)) {
898 getCellInset(cell).setAutoBreakRows(flag);
905 void LyXTabular::setAlignSpecial(int cell, string const & special,
906 LyXTabular::Feature what)
908 if (what == SET_SPECIAL_MULTI)
909 cellinfo_of_cell(cell).align_special = special;
911 column_info[column_of_cell(cell)].align_special = special;
915 void LyXTabular::setAllLines(int cell, bool line)
917 setTopLine(cell, line);
918 setBottomLine(cell, line);
919 setRightLine(cell, line);
920 setLeftLine(cell, line);
924 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
926 int const row = row_of_cell(cell);
927 if (onlycolumn || !isMultiColumn(cell))
928 row_info[row].top_line = line;
930 cellinfo_of_cell(cell).top_line = line;
934 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
936 if (onlycolumn || !isMultiColumn(cell))
937 row_info[row_of_cell(cell)].bottom_line = line;
939 cellinfo_of_cell(cell).bottom_line = line;
943 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
945 if (onlycolumn || !isMultiColumn(cell))
946 column_info[column_of_cell(cell)].left_line = line;
948 cellinfo_of_cell(cell).left_line = line;
952 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
954 if (onlycolumn || !isMultiColumn(cell))
955 column_info[right_column_of_cell(cell)].right_line = line;
957 cellinfo_of_cell(cell).right_line = line;
961 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
963 if (!onlycolumn && isMultiColumn(cell))
964 return cellinfo_of_cell(cell).alignment;
965 return column_info[column_of_cell(cell)].alignment;
969 LyXTabular::VAlignment
970 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
972 if (!onlycolumn && isMultiColumn(cell))
973 return cellinfo_of_cell(cell).valignment;
974 return column_info[column_of_cell(cell)].valignment;
978 LyXLength const LyXTabular::getPWidth(int cell) const
980 if (isMultiColumn(cell))
981 return cellinfo_of_cell(cell).p_width;
982 return column_info[column_of_cell(cell)].p_width;
986 LyXLength const LyXTabular::getColumnPWidth(int cell) const
988 return column_info[column_of_cell(cell)].p_width;
992 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
994 if (isMultiColumn(cell))
995 return cellinfo_of_cell(cell).p_width;
1000 string const LyXTabular::getAlignSpecial(int cell, int what) const
1002 if (what == SET_SPECIAL_MULTI)
1003 return cellinfo_of_cell(cell).align_special;
1004 return column_info[column_of_cell(cell)].align_special;
1008 int LyXTabular::getWidthOfCell(int cell) const
1010 int const row = row_of_cell(cell);
1011 int const column1 = column_of_cell(cell);
1012 int const column2 = right_column_of_cell(cell);
1014 for (int i = column1; i <= column2; ++i)
1015 result += cell_info[row][i].width_of_cell;
1020 int LyXTabular::getBeginningOfTextInCell(int cell) const
1024 switch (getAlignment(cell)) {
1025 case LYX_ALIGN_CENTER:
1026 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1028 case LYX_ALIGN_RIGHT:
1029 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1030 // + getAdditionalWidth(cell);
1033 // LYX_ALIGN_LEFT: nothing :-)
1037 // the LaTeX Way :-(
1043 bool LyXTabular::isFirstCellInRow(int cell) const
1045 return column_of_cell(cell) == 0;
1049 int LyXTabular::getFirstCellInRow(int row) const
1051 if (row > rows_ - 1)
1053 return cell_info[row][0].cellno;
1057 bool LyXTabular::isLastCellInRow(int cell) const
1059 return right_column_of_cell(cell) == columns_ - 1;
1063 int LyXTabular::getLastCellInRow(int row) const
1065 if (row > rows_ - 1)
1067 return cell_info[row][columns_-1].cellno;
1071 void LyXTabular::calculate_width_of_column(int column)
1074 for (int i = 0; i < rows_; ++i)
1075 maximum = max(cell_info[i][column].width_of_cell, maximum);
1076 column_info[column].width_of_column = maximum;
1081 // Calculate the columns regarding ONLY the normal cells and if this
1082 // column is inside a multicolumn cell then use it only if its the last
1083 // column of this multicolumn cell as this gives an added width to the
1084 // column, all the rest should be adapted!
1086 bool LyXTabular::calculate_width_of_column_NMC(int column)
1088 int const old_column_width = column_info[column].width_of_column;
1090 for (int i = 0; i < rows_; ++i) {
1091 int cell = getCellNumber(i, column);
1092 bool ismulti = isMultiColumn(cell, true);
1093 if ((!ismulti || column == right_column_of_cell(cell)) &&
1094 cell_info[i][column].width_of_cell > max)
1096 max = cell_info[i][column].width_of_cell;
1099 column_info[column].width_of_column = max;
1100 return column_info[column].width_of_column != old_column_width;
1104 void LyXTabular::calculate_width_of_tabular()
1106 width_of_tabular = 0;
1107 for (int i = 0; i < columns_; ++i)
1108 width_of_tabular += column_info[i].width_of_column;
1112 int LyXTabular::row_of_cell(int cell) const
1114 if (cell >= numberofcells)
1118 return rowofcell[cell];
1122 int LyXTabular::column_of_cell(int cell) const
1124 if (cell >= numberofcells)
1125 return columns_ - 1;
1128 return columnofcell[cell];
1132 int LyXTabular::right_column_of_cell(int cell) const
1134 int const row = row_of_cell(cell);
1135 int column = column_of_cell(cell);
1136 while (column < columns_ - 1 &&
1137 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1143 void LyXTabular::write(Buffer const & buf, ostream & os) const
1147 << write_attribute("version", 3)
1148 << write_attribute("rows", rows_)
1149 << write_attribute("columns", columns_)
1151 // global longtable options
1153 << write_attribute("rotate", rotate)
1154 << write_attribute("islongtable", is_long_tabular)
1155 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1156 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1157 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1158 << write_attribute("headTopDL", endhead.topDL)
1159 << write_attribute("headBottomDL", endhead.bottomDL)
1160 << write_attribute("footTopDL", endfoot.topDL)
1161 << write_attribute("footBottomDL", endfoot.bottomDL)
1162 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1163 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1164 << write_attribute("lastFootEmpty", endlastfoot.empty)
1166 for (int j = 0; j < columns_; ++j) {
1168 << write_attribute("alignment", column_info[j].alignment)
1169 << write_attribute("valignment", column_info[j].valignment)
1170 << write_attribute("leftline", column_info[j].left_line)
1171 << write_attribute("rightline", column_info[j].right_line)
1172 << write_attribute("width", column_info[j].p_width.asString())
1173 << write_attribute("special", column_info[j].align_special)
1176 for (int i = 0; i < rows_; ++i) {
1178 << write_attribute("topline", row_info[i].top_line)
1179 << write_attribute("bottomline", row_info[i].bottom_line)
1180 << write_attribute("endhead", row_info[i].endhead)
1181 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1182 << write_attribute("endfoot", row_info[i].endfoot)
1183 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1184 << write_attribute("newpage", row_info[i].newpage)
1186 for (int j = 0; j < columns_; ++j) {
1188 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1189 << write_attribute("alignment", cell_info[i][j].alignment)
1190 << write_attribute("valignment", cell_info[i][j].valignment)
1191 << write_attribute("topline", cell_info[i][j].top_line)
1192 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1193 << write_attribute("leftline", cell_info[i][j].left_line)
1194 << write_attribute("rightline", cell_info[i][j].right_line)
1195 << write_attribute("rotate", cell_info[i][j].rotate)
1196 << write_attribute("usebox", cell_info[i][j].usebox)
1197 << write_attribute("width", cell_info[i][j].p_width)
1198 << write_attribute("special", cell_info[i][j].align_special)
1200 os << "\\begin_inset ";
1201 cell_info[i][j].inset.write(buf, os);
1202 os << "\n\\end_inset \n"
1207 os << "</lyxtabular>\n";
1211 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1215 row_info[--hr].endhead = true;
1217 // set firstheader info
1218 if (fhr && fhr < rows_) {
1219 if (row_info[fhr].endhead) {
1221 row_info[--fhr].endfirsthead = true;
1222 row_info[fhr].endhead = false;
1224 } else if (row_info[fhr - 1].endhead) {
1225 endfirsthead.empty = true;
1227 while (fhr > 0 && !row_info[--fhr].endhead) {
1228 row_info[fhr].endfirsthead = true;
1233 if (fr && fr < rows_) {
1234 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1235 while (fr > 0 && !row_info[--fr].endhead) {
1236 row_info[fr].endfoot = true;
1237 row_info[fr].endhead = false;
1239 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1240 while (fr > 0 && !row_info[--fr].endfirsthead) {
1241 row_info[fr].endfoot = true;
1242 row_info[fr].endfirsthead = false;
1244 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1245 while (fr > 0 && !row_info[--fr].endhead &&
1246 !row_info[fr].endfirsthead)
1248 row_info[fr].endfoot = true;
1252 // set lastfooter info
1253 if (lfr && lfr < rows_) {
1254 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1255 while (lfr > 0 && !row_info[--lfr].endhead) {
1256 row_info[lfr].endlastfoot = true;
1257 row_info[lfr].endhead = false;
1259 } else if (row_info[lfr].endfirsthead &&
1260 row_info[lfr - 1].endfirsthead)
1262 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1263 row_info[lfr].endlastfoot = true;
1264 row_info[lfr].endfirsthead = false;
1266 } else if (row_info[lfr].endfoot
1267 && row_info[lfr - 1].endfoot) {
1268 while (lfr > 0 && !row_info[--lfr].endfoot) {
1269 row_info[lfr].endlastfoot = true;
1270 row_info[lfr].endfoot = false;
1272 } else if (!row_info[fr - 1].endhead
1273 && !row_info[fr - 1].endfirsthead &&
1274 !row_info[fr - 1].endfoot)
1277 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1278 !row_info[lfr].endfoot)
1280 row_info[lfr].endlastfoot = true;
1282 } else if (haveLTFoot()) {
1283 endlastfoot.empty = true;
1289 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1292 istream & is = lex.getStream();
1294 l_getline(is, line);
1295 if (!prefixIs(line, "<lyxtabular ")
1296 && !prefixIs(line, "<LyXTabular ")) {
1302 if (!getTokenValue(line, "version", version))
1304 Assert(version >= 2);
1307 if (!getTokenValue(line, "rows", rows_arg))
1310 if (!getTokenValue(line, "columns", columns_arg))
1312 init(buf.params, rows_arg, columns_arg);
1313 l_getline(is, line);
1314 if (!prefixIs(line, "<features")) {
1315 lyxerr << "Wrong tabular format (expected <features ...> got"
1316 << line << ')' << endl;
1319 getTokenValue(line, "rotate", rotate);
1320 getTokenValue(line, "islongtable", is_long_tabular);
1321 // compatibility read for old longtable options. Now we can make any
1322 // row part of the header/footer type we want before it was strict
1323 // sequential from the first row down (as LaTeX does it!). So now when
1324 // we find a header/footer line we have to go up the rows and set it
1325 // on all preceding rows till the first or one with already a h/f option
1326 // set. If we find a firstheader on the same line as a header or a
1327 // lastfooter on the same line as a footer then this should be set empty.
1335 getTokenValue(line, "endhead", hrow);
1336 getTokenValue(line, "endfirsthead", fhrow);
1337 getTokenValue(line, "endfoot", frow);
1338 getTokenValue(line, "endlastfoot", lfrow);
1339 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1341 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1342 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1343 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1344 getTokenValue(line, "headTopDL", endhead.topDL);
1345 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1346 getTokenValue(line, "footTopDL", endfoot.topDL);
1347 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1348 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1349 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1350 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1352 for (int j = 0; j < columns_; ++j) {
1354 if (!prefixIs(line,"<column")) {
1355 lyxerr << "Wrong tabular format (expected <column ...> got"
1356 << line << ')' << endl;
1359 getTokenValue(line, "alignment", column_info[j].alignment);
1360 getTokenValue(line, "valignment", column_info[j].valignment);
1361 getTokenValue(line, "leftline", column_info[j].left_line);
1362 getTokenValue(line, "rightline", column_info[j].right_line);
1363 getTokenValue(line, "width", column_info[j].p_width);
1364 getTokenValue(line, "special", column_info[j].align_special);
1367 for (int i = 0; i < rows_; ++i) {
1368 l_getline(is, line);
1369 if (!prefixIs(line, "<row")) {
1370 lyxerr << "Wrong tabular format (expected <row ...> got"
1371 << line << ')' << endl;
1374 getTokenValue(line, "topline", row_info[i].top_line);
1375 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1376 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1377 getTokenValue(line, "endhead", row_info[i].endhead);
1378 getTokenValue(line, "endfoot", row_info[i].endfoot);
1379 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1380 getTokenValue(line, "newpage", row_info[i].newpage);
1381 for (int j = 0; j < columns_; ++j) {
1382 l_getline(is, line);
1383 if (!prefixIs(line, "<cell")) {
1384 lyxerr << "Wrong tabular format (expected <cell ...> got"
1385 << line << ')' << endl;
1388 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1389 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1390 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1391 getTokenValue(line, "topline", cell_info[i][j].top_line);
1392 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1393 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1394 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1395 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1396 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1397 getTokenValue(line, "width", cell_info[i][j].p_width);
1398 getTokenValue(line, "special", cell_info[i][j].align_special);
1399 l_getline(is, line);
1400 if (prefixIs(line, "\\begin_inset")) {
1401 cell_info[i][j].inset.read(buf, lex);
1402 l_getline(is, line);
1404 if (!prefixIs(line, "</cell>")) {
1405 lyxerr << "Wrong tabular format (expected </cell> got"
1406 << line << ')' << endl;
1410 l_getline(is, line);
1411 if (!prefixIs(line, "</row>")) {
1412 lyxerr << "Wrong tabular format (expected </row> got"
1413 << line << ')' << endl;
1417 while (!prefixIs(line, "</lyxtabular>")) {
1418 l_getline(is, line);
1420 set_row_column_number_info();
1424 bool LyXTabular::isMultiColumn(int cell, bool real) const
1426 return (!real || column_of_cell(cell) != right_column_of_cell(cell)) &&
1427 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1431 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1433 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1437 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1439 cellstruct & cs = cellinfo_of_cell(cell);
1440 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1441 cs.alignment = column_info[column_of_cell(cell)].alignment;
1442 cs.top_line = row_info[row_of_cell(cell)].top_line;
1443 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1444 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1445 for (int i = 1; i < number; ++i) {
1446 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1447 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1448 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1449 cs1.inset.clear(false);
1451 set_row_column_number_info();
1455 int LyXTabular::cells_in_multicolumn(int cell) const
1457 int const row = row_of_cell(cell);
1458 int column = column_of_cell(cell);
1461 while (column < columns_ &&
1462 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1471 int LyXTabular::unsetMultiColumn(int cell)
1473 int const row = row_of_cell(cell);
1474 int column = column_of_cell(cell);
1478 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1479 cell_info[row][column].multicolumn = CELL_NORMAL;
1481 while (column < columns_ &&
1482 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1484 cell_info[row][column].multicolumn = CELL_NORMAL;
1489 set_row_column_number_info();
1494 void LyXTabular::setLongTabular(bool what)
1496 is_long_tabular = what;
1500 bool LyXTabular::isLongTabular() const
1502 return is_long_tabular;
1506 void LyXTabular::setRotateTabular(bool flag)
1512 bool LyXTabular::getRotateTabular() const
1518 void LyXTabular::setRotateCell(int cell, bool flag)
1520 cellinfo_of_cell(cell).rotate = flag;
1524 bool LyXTabular::getRotateCell(int cell) const
1526 return cellinfo_of_cell(cell).rotate;
1530 bool LyXTabular::needRotating() const
1534 for (int i = 0; i < rows_; ++i)
1535 for (int j = 0; j < columns_; ++j)
1536 if (cell_info[i][j].rotate)
1542 bool LyXTabular::isLastCell(int cell) const
1544 if (cell + 1 < numberofcells)
1550 int LyXTabular::getCellAbove(int cell) const
1552 if (row_of_cell(cell) > 0)
1553 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1558 int LyXTabular::getCellBelow(int cell) const
1560 if (row_of_cell(cell) + 1 < rows_)
1561 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1566 int LyXTabular::getLastCellAbove(int cell) const
1568 if (row_of_cell(cell) <= 0)
1570 if (!isMultiColumn(cell))
1571 return getCellAbove(cell);
1572 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1576 int LyXTabular::getLastCellBelow(int cell) const
1578 if (row_of_cell(cell) + 1 >= rows_)
1580 if (!isMultiColumn(cell))
1581 return getCellBelow(cell);
1582 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1586 int LyXTabular::getCellNumber(int row, int column) const
1588 Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1589 return cell_info[row][column].cellno;
1593 void LyXTabular::setUsebox(int cell, BoxType type)
1595 cellinfo_of_cell(cell).usebox = type;
1599 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1601 if (column_info[column_of_cell(cell)].p_width.zero() &&
1602 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1604 if (cellinfo_of_cell(cell).usebox > 1)
1605 return cellinfo_of_cell(cell).usebox;
1606 return useParbox(cell);
1611 // This are functions used for the longtable support
1613 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1618 row_info[row].endfirsthead = flag;
1622 row_info[row].endhead = flag;
1627 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1630 hd.set = haveLTHead();
1631 return row_info[row].endhead;
1635 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1638 hd.set = haveLTFirstHead();
1639 return row_info[row].endfirsthead;
1643 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1648 row_info[row].endlastfoot = flag;
1652 row_info[row].endfoot = flag;
1657 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1660 fd.set = haveLTFoot();
1661 return row_info[row].endfoot;
1665 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1668 fd.set = haveLTLastFoot();
1669 return row_info[row].endlastfoot;
1673 void LyXTabular::setLTNewPage(int row, bool what)
1675 row_info[row].newpage = what;
1679 bool LyXTabular::getLTNewPage(int row) const
1681 return row_info[row].newpage;
1685 bool LyXTabular::haveLTHead() const
1687 for (int i = 0; i < rows_; ++i)
1688 if (row_info[i].endhead)
1694 bool LyXTabular::haveLTFirstHead() const
1696 if (endfirsthead.empty)
1698 for (int i = 0; i < rows_; ++i)
1699 if (row_info[i].endfirsthead)
1705 bool LyXTabular::haveLTFoot() const
1707 for (int i = 0; i < rows_; ++i)
1708 if (row_info[i].endfoot)
1714 bool LyXTabular::haveLTLastFoot() const
1716 if (endlastfoot.empty)
1718 for (int i = 0; i < rows_; ++i)
1719 if (row_info[i].endlastfoot)
1725 // end longtable support functions
1727 void LyXTabular::setAscentOfRow(int row, int height)
1729 if (row >= rows_ || row_info[row].ascent_of_row == height)
1731 row_info[row].ascent_of_row = height;
1735 void LyXTabular::setDescentOfRow(int row, int height)
1737 if (row >= rows_ || row_info[row].descent_of_row == height)
1739 row_info[row].descent_of_row = height;
1743 int LyXTabular::getAscentOfRow(int row) const
1747 return row_info[row].ascent_of_row;
1751 int LyXTabular::getDescentOfRow(int row) const
1755 return row_info[row].descent_of_row;
1759 int LyXTabular::getHeightOfTabular() const
1762 for (int row = 0; row < rows_; ++row)
1763 height += getAscentOfRow(row) + getDescentOfRow(row) +
1764 getAdditionalHeight(row);
1769 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1771 if (row >= rows_ || column >= columns_)
1773 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1777 int LyXTabular::TeXTopHLine(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) {
1796 << column_of_cell(i) + 1
1798 << right_column_of_cell(i) + 1
1810 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1812 if (row < 0 || row >= rows_)
1815 int const fcell = getFirstCellInRow(row);
1816 int const n = numberOfCellsInRow(fcell) + fcell;
1819 for (int i = fcell; i < n; ++i) {
1823 if (tmp == n - fcell) {
1826 for (int i = fcell; i < n; ++i) {
1827 if (bottomLine(i)) {
1829 << column_of_cell(i) + 1
1831 << right_column_of_cell(i) + 1
1843 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1847 if (getRotateCell(cell)) {
1848 os << "\\begin{sideways}\n";
1851 if (isMultiColumn(cell)) {
1852 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1853 if (!cellinfo_of_cell(cell).align_special.empty()) {
1854 os << cellinfo_of_cell(cell).align_special << "}{";
1856 if (leftLine(cell) &&
1857 (isFirstCellInRow(cell) ||
1858 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1859 !rightLine(cell - 1, true))))
1863 if (!getPWidth(cell).zero()) {
1864 switch (getVAlignment(cell)) {
1865 case LYX_VALIGN_TOP:
1868 case LYX_VALIGN_MIDDLE:
1871 case LYX_VALIGN_BOTTOM:
1876 << getPWidth(cell).asLatexString()
1879 switch (getAlignment(cell)) {
1880 case LYX_ALIGN_LEFT:
1883 case LYX_ALIGN_RIGHT:
1891 if (rightLine(cell))
1893 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1899 if (getUsebox(cell) == BOX_PARBOX) {
1901 switch (getVAlignment(cell)) {
1902 case LYX_VALIGN_TOP:
1905 case LYX_VALIGN_MIDDLE:
1908 case LYX_VALIGN_BOTTOM:
1912 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1913 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1914 os << "\\begin{minipage}[";
1915 switch (getVAlignment(cell)) {
1916 case LYX_VALIGN_TOP:
1919 case LYX_VALIGN_MIDDLE:
1922 case LYX_VALIGN_BOTTOM:
1926 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1933 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1938 if (getUsebox(cell) == BOX_PARBOX)
1940 else if (getUsebox(cell) == BOX_MINIPAGE) {
1941 os << "%\n\\end{minipage}";
1944 if (isMultiColumn(cell)) {
1947 if (getRotateCell(cell)) {
1948 os << "%\n\\end{sideways}";
1955 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1956 LatexRunParams const & runparams) const
1958 if (!is_long_tabular)
1962 // output header info
1964 if (endhead.topDL) {
1968 for (int i = 0; i < rows_; ++i) {
1969 if (row_info[i].endhead) {
1970 ret += TeXRow(os, i, buf, runparams);
1973 if (endhead.bottomDL) {
1977 os << "\\endhead\n";
1979 if (endfirsthead.empty) {
1980 os << "\\endfirsthead\n";
1984 // output firstheader info
1985 if (haveLTFirstHead()) {
1986 if (endfirsthead.topDL) {
1990 for (int i = 0; i < rows_; ++i) {
1991 if (row_info[i].endfirsthead) {
1992 ret += TeXRow(os, i, buf, runparams);
1995 if (endfirsthead.bottomDL) {
1999 os << "\\endfirsthead\n";
2002 // output footer info
2004 if (endfoot.topDL) {
2008 for (int i = 0; i < rows_; ++i) {
2009 if (row_info[i].endfoot) {
2010 ret += TeXRow(os, i, buf, runparams);
2013 if (endfoot.bottomDL) {
2017 os << "\\endfoot\n";
2019 if (endlastfoot.empty) {
2020 os << "\\endlastfoot\n";
2024 // output lastfooter info
2025 if (haveLTLastFoot()) {
2026 if (endlastfoot.topDL) {
2030 for (int i = 0; i < rows_; ++i) {
2031 if (row_info[i].endlastfoot) {
2032 ret += TeXRow(os, i, buf, runparams);
2035 if (endlastfoot.bottomDL) {
2039 os << "\\endlastfoot\n";
2046 bool LyXTabular::isValidRow(int const row) const
2048 if (!is_long_tabular)
2050 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2051 !row_info[row].endfoot && !row_info[row].endlastfoot);
2055 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const & buf,
2056 LatexRunParams const & runparams) const
2059 int cell = getCellNumber(i, 0);
2061 ret += TeXTopHLine(os, i);
2062 for (int j = 0; j < columns_; ++j) {
2063 if (isPartOfMultiColumn(i, j))
2065 ret += TeXCellPreamble(os, cell);
2066 InsetText & inset = getCellInset(cell);
2068 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(buf.params) &&
2069 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2073 ret += inset.latex(buf, os, runparams);
2077 ret += TeXCellPostamble(os, cell);
2078 if (!isLastCellInRow(cell)) { // not last cell in row
2084 os << "\\tabularnewline\n";
2086 ret += TeXBottomHLine(os, i);
2091 int LyXTabular::latex(Buffer const & buf, ostream & os,
2092 LatexRunParams const & runparams) const
2096 //+---------------------------------------------------------------------
2097 //+ first the opening preamble +
2098 //+---------------------------------------------------------------------
2101 os << "\\begin{sideways}\n";
2104 if (is_long_tabular)
2105 os << "\\begin{longtable}{";
2107 os << "\\begin{tabular}{";
2108 for (int i = 0; i < columns_; ++i) {
2109 if (!column_info[i].align_special.empty()) {
2110 os << column_info[i].align_special;
2112 if (column_info[i].left_line)
2114 if (!column_info[i].p_width.zero()) {
2115 switch (column_info[i].alignment) {
2116 case LYX_ALIGN_LEFT:
2117 os << ">{\\raggedright}";
2119 case LYX_ALIGN_RIGHT:
2120 os << ">{\\raggedleft}";
2122 case LYX_ALIGN_CENTER:
2123 os << ">{\\centering}";
2125 case LYX_ALIGN_NONE:
2126 case LYX_ALIGN_BLOCK:
2127 case LYX_ALIGN_LAYOUT:
2128 case LYX_ALIGN_SPECIAL:
2132 switch (column_info[i].valignment) {
2133 case LYX_VALIGN_TOP:
2136 case LYX_VALIGN_MIDDLE:
2139 case LYX_VALIGN_BOTTOM:
2144 << column_info[i].p_width.asLatexString()
2147 switch (column_info[i].alignment) {
2148 case LYX_ALIGN_LEFT:
2151 case LYX_ALIGN_RIGHT:
2159 if (column_info[i].right_line)
2166 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2168 //+---------------------------------------------------------------------
2169 //+ the single row and columns (cells) +
2170 //+---------------------------------------------------------------------
2172 for (int i = 0; i < rows_; ++i) {
2173 if (isValidRow(i)) {
2174 ret += TeXRow(os, i, buf, runparams);
2175 if (is_long_tabular && row_info[i].newpage) {
2176 os << "\\newpage\n";
2182 //+---------------------------------------------------------------------
2183 //+ the closing of the tabular +
2184 //+---------------------------------------------------------------------
2186 if (is_long_tabular)
2187 os << "\\end{longtable}";
2189 os << "\\end{tabular}";
2191 os << "\n\\end{sideways}";
2199 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os) const
2201 os << "<tabular ca=\"";
2202 for (int i = 0; i < columns_; ++i) {
2203 switch (column_info[i].alignment) {
2204 case LYX_ALIGN_LEFT:
2207 case LYX_ALIGN_RIGHT:
2218 for (int i = 0; i < rows_; ++i) {
2219 for (int j = 0; j < columns_; ++j) {
2220 if (isPartOfMultiColumn(i, j))
2222 InsetText & inset = getCellInset(cell);
2224 ret += inset.linuxdoc(buf, os);
2226 if (isLastCellInRow(cell)) {
2235 os << "</tabular>\n";
2240 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row) const
2243 int cell = getFirstCellInRow(row);
2246 for (int j = 0; j < columns_; ++j) {
2247 if (isPartOfMultiColumn(row, j))
2250 os << "<entry align=\"";
2251 switch (getAlignment(cell)) {
2252 case LYX_ALIGN_LEFT:
2255 case LYX_ALIGN_RIGHT:
2263 os << "\" valign=\"";
2264 switch (getVAlignment(cell)) {
2265 case LYX_VALIGN_TOP:
2268 case LYX_VALIGN_BOTTOM:
2271 case LYX_VALIGN_MIDDLE:
2276 if (isMultiColumn(cell)) {
2277 os << " namest=\"col" << j << "\" ";
2278 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2282 ret += getCellInset(cell).docbook(buf, os, true);
2291 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2292 bool /*mixcont*/) const
2296 //+---------------------------------------------------------------------
2297 //+ first the opening preamble +
2298 //+---------------------------------------------------------------------
2300 os << "<tgroup cols=\"" << columns_
2301 << "\" colsep=\"1\" rowsep=\"1\">\n";
2303 for (int i = 0; i < columns_; ++i) {
2304 os << "<colspec colname=\"col" << i << "\" align=\"";
2305 switch (column_info[i].alignment) {
2306 case LYX_ALIGN_LEFT:
2309 case LYX_ALIGN_RIGHT:
2320 //+---------------------------------------------------------------------
2321 //+ Long Tabular case +
2322 //+---------------------------------------------------------------------
2324 // output header info
2325 if (haveLTHead() || haveLTFirstHead()) {
2328 for (int i = 0; i < rows_; ++i) {
2329 if (row_info[i].endhead || row_info[i].endfirsthead) {
2330 ret += docbookRow(buf, os, i);
2336 // output footer info
2337 if (haveLTFoot() || haveLTLastFoot()) {
2340 for (int i = 0; i < rows_; ++i) {
2341 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2342 ret += docbookRow(buf, os, i);
2349 //+---------------------------------------------------------------------
2350 //+ the single row and columns (cells) +
2351 //+---------------------------------------------------------------------
2355 for (int i = 0; i < rows_; ++i) {
2356 if (isValidRow(i)) {
2357 ret += docbookRow(buf, os, i);
2362 //+---------------------------------------------------------------------
2363 //+ the closing of the tabular +
2364 //+---------------------------------------------------------------------
2373 int LyXTabular::asciiTopHLine(ostream & os, int row,
2374 vector<unsigned int> const & clen) const
2376 int const fcell = getFirstCellInRow(row);
2377 int const n = numberOfCellsInRow(fcell) + fcell;
2380 for (int i = fcell; i < n; ++i) {
2390 for (int i = fcell; i < n; ++i) {
2401 int column = column_of_cell(i);
2402 int len = clen[column];
2403 while (isPartOfMultiColumn(row, ++column))
2404 len += clen[column] + 4;
2405 os << string(len, ch);
2420 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2421 vector<unsigned int> const & clen) const
2423 int const fcell = getFirstCellInRow(row);
2424 int const n = numberOfCellsInRow(fcell) + fcell;
2427 for (int i = fcell; i < n; ++i) {
2428 if (bottomLine(i)) {
2437 for (int i = fcell; i < n; ++i) {
2438 if (bottomLine(i)) {
2448 int column = column_of_cell(i);
2449 int len = clen[column];
2450 while (isPartOfMultiColumn(row, ++column))
2451 len += clen[column] + 4;
2452 os << string(len, ch);
2453 if (bottomLine(i)) {
2467 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2468 int cell, int row, int column,
2469 vector<unsigned int> const & clen,
2470 bool onlydata) const
2473 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2485 unsigned int len1 = sstr.str().length();
2486 unsigned int len2 = clen[column];
2487 while (isPartOfMultiColumn(row, ++column))
2488 len2 += clen[column] + 4;
2491 switch (getAlignment(cell)) {
2493 case LYX_ALIGN_LEFT:
2496 case LYX_ALIGN_RIGHT:
2500 case LYX_ALIGN_CENTER:
2506 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2508 if (rightLine(cell))
2517 int LyXTabular::ascii(Buffer const & buf, ostream & os, int const depth,
2518 bool onlydata, unsigned char delim) const
2522 // first calculate the width of the single columns
2523 vector<unsigned int> clen(columns_);
2526 // first all non (real) multicolumn cells!
2527 for (int j = 0; j < columns_; ++j) {
2529 for (int i = 0; i < rows_; ++i) {
2530 int cell = getCellNumber(i, j);
2531 if (isMultiColumn(cell, true))
2534 getCellInset(cell).ascii(buf, sstr, 0);
2535 if (clen[j] < sstr.str().length())
2536 clen[j] = sstr.str().length();
2539 // then all (real) multicolumn cells!
2540 for (int j = 0; j < columns_; ++j) {
2541 for (int i = 0; i < rows_; ++i) {
2542 int cell = getCellNumber(i, j);
2543 if (!isMultiColumn(cell, true) || isPartOfMultiColumn(i, j))
2546 getCellInset(cell).ascii(buf, sstr, 0);
2547 int len = int(sstr.str().length());
2548 int const n = cells_in_multicolumn(cell);
2549 for (int k = j; len > 0 && k < j + n - 1; ++k)
2551 if (len > int(clen[j + n - 1]))
2552 clen[j + n - 1] = len;
2557 for (int i = 0; i < rows_; ++i) {
2558 if (!onlydata && asciiTopHLine(os, i, clen))
2559 for (int j = 0; j < depth; ++j)
2561 for (int j = 0; j < columns_; ++j) {
2562 if (isPartOfMultiColumn(i, j))
2564 if (onlydata && j > 0)
2566 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2571 for (int j = 0; j < depth; ++j)
2573 if (asciiBottomHLine(os, i, clen))
2574 for (int j = 0; j < depth; ++j)
2582 InsetText & LyXTabular::getCellInset(int cell) const
2585 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2589 InsetText & LyXTabular::getCellInset(int row, int column) const
2591 cur_cell = getCellNumber(row, column);
2592 return cell_info[row][column].inset;
2596 int LyXTabular::getCellFromInset(InsetOld const * inset, int maybe_cell) const
2598 // is this inset part of the tabular?
2599 if (!inset || inset->owner() != owner_) {
2600 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2604 const int save_cur_cell = cur_cell;
2605 int cell = cur_cell;
2606 if (&getCellInset(cell) != inset) {
2608 if (cell == -1 || &getCellInset(cell) != inset)
2613 for (cell = getNumberOfCells(); cell >= 0; --cell)
2614 if (&getCellInset(cell) == inset)
2617 lyxerr[Debug::INSETTEXT]
2618 << "LyXTabular::getCellFromInset: "
2620 << ", cur_cell=" << save_cur_cell
2621 << ", maybe_cell=" << maybe_cell
2623 // We should have found a cell at this point
2625 lyxerr << "LyXTabular::getCellFromInset: "
2626 << "Cell not found!" << endl;
2634 void LyXTabular::validate(LaTeXFeatures & features) const
2636 features.require("NeedTabularnewline");
2637 if (isLongTabular())
2638 features.require("longtable");
2640 features.require("rotating");
2641 for (int cell = 0; cell < numberofcells; ++cell) {
2642 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2643 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2644 features.require("array");
2645 getCellInset(cell).validate(features);
2650 void LyXTabular::getLabelList(std::vector<string> & list) const
2652 for (int i = 0; i < rows_; ++i)
2653 for (int j = 0; j < columns_; ++j)
2654 getCellInset(i, j).getLabelList(list);
2658 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2660 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2661 ParagraphList::const_iterator cit = parlist.begin();
2662 ParagraphList::const_iterator end = parlist.end();
2664 for (; cit != end; ++cit)
2665 for (int i = 0; i < cit->size(); ++i)
2666 if (cit->isNewline(i))