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;
58 string const write_attribute(string const & name, T const & t)
60 string const s = tostr(t);
61 return s.empty() ? s : " " + name + "=\"" + s + "\"";
64 string const write_attribute(string const & name, string const & t)
66 return t.empty() ? t : " " + name + "=\"" + t + "\"";
70 string const write_attribute(string const & name, bool const & b)
72 // we write only true attribute values so we remove a bit of the
73 // file format bloat for tabulars.
74 return b ? write_attribute(name, tostr(b)) : string();
78 string const write_attribute(string const & name, int const & i)
80 // we write only true attribute values so we remove a bit of the
81 // file format bloat for tabulars.
82 return i ? write_attribute(name, tostr(i)) : string();
86 string const write_attribute(string const & name, LyXLength const & value)
88 // we write only the value if we really have one same reson as above.
89 return value.zero() ? string() : write_attribute(name, value.asString());
93 string const tostr(LyXAlignment const & num)
102 case LYX_ALIGN_CENTER:
104 case LYX_ALIGN_RIGHT:
106 case LYX_ALIGN_LAYOUT:
108 case LYX_ALIGN_SPECIAL:
115 string const tostr(LyXTabular::VAlignment const & num)
118 case LyXTabular::LYX_VALIGN_TOP:
120 case LyXTabular::LYX_VALIGN_CENTER:
122 case LyXTabular::LYX_VALIGN_BOTTOM:
129 string const tostr(LyXTabular::BoxType const & num)
132 case LyXTabular::BOX_NONE:
134 case LyXTabular::BOX_PARBOX:
136 case LyXTabular::BOX_MINIPAGE:
142 // I would have liked a fromstr template a lot better. (Lgb)
143 bool string2type(string const str, LyXAlignment & num)
146 num = LYX_ALIGN_NONE;
147 else if (str == "block")
148 num = LYX_ALIGN_BLOCK;
149 else if (str == "left")
150 num = LYX_ALIGN_LEFT;
151 else if (str == "center")
152 num = LYX_ALIGN_CENTER;
153 else if (str == "right")
154 num = LYX_ALIGN_RIGHT;
161 bool string2type(string const str, LyXTabular::VAlignment & num)
164 num = LyXTabular::LYX_VALIGN_TOP;
165 else if (str == "center")
166 num = LyXTabular::LYX_VALIGN_CENTER;
167 else if (str == "bottom")
168 num = LyXTabular::LYX_VALIGN_BOTTOM;
175 bool string2type(string const str, LyXTabular::BoxType & num)
178 num = LyXTabular::BOX_NONE;
179 else if (str == "parbox")
180 num = LyXTabular::BOX_PARBOX;
181 else if (str == "minipage")
182 num = LyXTabular::BOX_MINIPAGE;
189 bool string2type(string const str, bool & num)
193 else if (str == "false")
201 bool getTokenValue(string const & str, const char * token, string & ret)
204 size_t token_length = strlen(token);
205 string::size_type pos = str.find(token);
207 if (pos == string::npos || pos + token_length + 1 >= str.length()
208 || str[pos + token_length] != '=')
210 pos += token_length + 1;
212 if ((ch != '"') && (ch != '\'')) { // only read till next space
216 while ((pos < str.length() - 1) && (str[++pos] != ch))
223 bool getTokenValue(string const & str, const char * token, int & num)
227 if (!getTokenValue(str, token, tmp))
234 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
237 if (!getTokenValue(str, token, tmp))
239 return string2type(tmp, num);
243 bool getTokenValue(string const & str, const char * token,
244 LyXTabular::VAlignment & num)
247 if (!getTokenValue(str, token, tmp))
249 return string2type(tmp, num);
253 bool getTokenValue(string const & str, const char * token,
254 LyXTabular::BoxType & num)
257 if (!getTokenValue(str, token, tmp))
259 return string2type(tmp, num);
263 bool getTokenValue(string const & str, const char * token, bool & flag)
265 // set the flag always to false as this should be the default for bools
266 // not in the file-format.
269 if (!getTokenValue(str, token, tmp))
271 return string2type(tmp, flag);
275 bool getTokenValue(string const & str, const char * token, LyXLength & len)
277 // set the lenght to be zero() as default as this it should be if not
278 // in the file format.
281 if (!getTokenValue(str, token, tmp))
283 return isValidLength(tmp, &len);
287 void l_getline(istream & is, string & str)
290 while (str.empty()) {
292 if (!str.empty() && str[str.length() - 1] == '\r')
293 str.erase(str.length() - 1);
299 /// Define a few methods for the inner structs
301 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
306 multicolumn = LyXTabular::CELL_NORMAL;
307 alignment = LYX_ALIGN_CENTER;
308 valignment = LYX_VALIGN_TOP;
318 LyXTabular::rowstruct::rowstruct()
325 endfirsthead = false;
332 LyXTabular::columnstruct::columnstruct()
336 alignment = LYX_ALIGN_CENTER;
337 valignment = LYX_VALIGN_TOP;
342 LyXTabular::ltType::ltType()
350 LyXTabular::LyXTabular(BufferParams const & bp,
351 InsetTabular * inset, int rows_arg, int columns_arg)
355 init(bp, rows_arg, columns_arg);
359 LyXTabular::LyXTabular(BufferParams const & bp,
360 InsetTabular * inset, LyXTabular const & lt)
364 init(bp, lt.rows_, lt.columns_, <);
368 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
376 // activates all lines and sets all widths to 0
377 void LyXTabular::init(BufferParams const & bp,
378 int rows_arg, int columns_arg, LyXTabular const * lt)
381 columns_ = columns_arg;
382 row_info = row_vector(rows_, rowstruct());
383 column_info = column_vector(columns_, columnstruct());
384 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
392 for (int i = 0; i < rows_; ++i) {
393 for (int j = 0; j < columns_; ++j) {
394 cell_info[i][j].inset.setOwner(owner_);
395 cell_info[i][j].inset.setDrawFrame(InsetText::LOCKED);
396 cell_info[i][j].cellno = cellno++;
398 cell_info[i].back().right_line = true;
400 row_info.back().bottom_line = true;
401 row_info.front().bottom_line = true;
403 for (int i = 0; i < columns_; ++i)
404 calculate_width_of_column(i);
406 column_info.back().right_line = true;
408 calculate_width_of_tabular();
411 columnofcell.clear();
412 set_row_column_number_info();
413 is_long_tabular = false;
418 void LyXTabular::appendRow(BufferParams const & bp, int cell)
422 int row = row_of_cell(cell);
424 row_vector::iterator rit = row_info.begin() + row;
425 row_info.insert(rit, rowstruct());
426 // now set the values of the row before
427 row_info[row] = row_info[row + 1];
430 cell_vvector::iterator cit = cell_info.begin() + row;
431 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
433 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
436 for (int i = 0; i <= row; ++i)
437 for (int j = 0; j < columns_; ++j)
438 c_info[i][j] = cell_info[i][j];
440 for (int i = row + 1; i < rows_; ++i)
441 for (int j = 0; j < columns_; ++j)
442 c_info[i][j] = cell_info[i-1][j];
446 for (int j = 0; j < columns_; ++j) {
447 cell_info[row][j].inset.clear(false);
448 if (bp.tracking_changes)
449 cell_info[row][j].inset.markNew(true);
456 void LyXTabular::deleteRow(int row)
458 // Not allowed to delete last row
462 row_info.erase(row_info.begin() + row);
463 cell_info.erase(cell_info.begin() + row);
469 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
473 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
475 int const column = column_of_cell(cell);
476 column_vector::iterator cit = column_info.begin() + column + 1;
477 column_info.insert(cit, columnstruct());
478 // set the column values of the column before
479 column_info[column + 1] = column_info[column];
481 for (int i = 0; i < rows_; ++i) {
482 for (int j = 0; j <= column; ++j)
483 c_info[i][j] = cell_info[i][j];
485 for (int j = column + 1; j < columns_; ++j)
486 c_info[i][j] = cell_info[i][j - 1];
488 // care about multicolumns
489 if (c_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
490 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
492 if (column + 2 >= columns_
493 || c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
494 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
498 for (int i = 0; i < rows_; ++i) {
499 cell_info[i][column + 1].inset.clear(false);
500 if (bp.tracking_changes)
501 cell_info[i][column + 1].inset.markNew(true);
507 void LyXTabular::deleteColumn(int column)
509 // Not allowed to delete last column
513 column_info.erase(column_info.begin() + column);
514 for (int i = 0; i < rows_; ++i)
515 cell_info[i].erase(cell_info[i].begin() + column);
521 void LyXTabular::reinit()
527 void LyXTabular::Reinit(bool reset_widths)
530 for (int i = 0; i < rows_; ++i) {
531 for (int j = 0; j < columns_; ++j) {
532 cell_info[i][j].width_of_cell = 0;
533 cell_info[i][j].inset.setOwner(owner_);
538 for (int i = 0; i < columns_; ++i)
539 calculate_width_of_column(i);
541 calculate_width_of_tabular();
543 set_row_column_number_info();
547 void LyXTabular::set_row_column_number_info(bool oldformat)
550 for (int row = 0; row < rows_; ++row) {
551 for (int column = 0; column < columns_; ++column) {
552 if (cell_info[row][column].multicolumn
553 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
555 cell_info[row][column].cellno = numberofcells;
558 ++numberofcells; // because this is one more than as we start from 0
560 rowofcell.resize(numberofcells);
561 columnofcell.resize(numberofcells);
563 for (int row = 0, column = 0, c = 0;
564 c < numberofcells && row < rows_ && column < columns_;) {
566 columnofcell[c] = column;
570 } while (column < columns_ &&
571 cell_info[row][column].multicolumn
572 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
574 if (column == columns_) {
580 for (int row = 0; row < rows_; ++row) {
581 for (int column = 0; column < columns_; ++column) {
582 if (isPartOfMultiColumn(row,column))
584 // now set the right line of multicolumns right for oldformat read
586 cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
588 int cn = cells_in_multicolumn(cell_info[row][column].cellno);
589 cell_info[row][column].right_line =
590 cell_info[row][column+cn-1].right_line;
592 cell_info[row][column].inset.setAutoBreakRows(
593 !getPWidth(getCellNumber(row, column)).zero());
599 int LyXTabular::getNumberOfCells() const
601 return numberofcells;
605 int LyXTabular::numberOfCellsInRow(int cell) const
607 int const row = row_of_cell(cell);
609 for (int i = 0; i < columns_; ++i)
610 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
616 // returns 1 if there is a topline, returns 0 if not
617 bool LyXTabular::topLine(int cell, bool onlycolumn) const
619 if (!onlycolumn && isMultiColumn(cell))
620 return cellinfo_of_cell(cell).top_line;
621 return row_info[row_of_cell(cell)].top_line;
625 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
627 if (!onlycolumn && isMultiColumn(cell))
628 return cellinfo_of_cell(cell).bottom_line;
629 return row_info[row_of_cell(cell)].bottom_line;
633 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
635 if (!onlycolumn && isMultiColumn(cell) &&
636 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
638 if (cellinfo_of_cell(cell).align_special.empty())
639 return cellinfo_of_cell(cell).left_line;
640 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
642 if (column_info[column_of_cell(cell)].align_special.empty())
643 return column_info[column_of_cell(cell)].left_line;
644 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
648 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
650 if (!onlycolumn && isMultiColumn(cell) &&
651 (isLastCellInRow(cell) || isMultiColumn(cell+1)))
653 if (cellinfo_of_cell(cell).align_special.empty())
654 return cellinfo_of_cell(cell).right_line;
655 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
657 if (column_info[column_of_cell(cell)].align_special.empty())
658 return column_info[right_column_of_cell(cell)].right_line;
659 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
663 bool LyXTabular::topAlreadyDrawn(int cell) const
665 int row = row_of_cell(cell);
666 if (row > 0 && !getAdditionalHeight(row)) {
667 int column = column_of_cell(cell);
670 && cell_info[row][column].multicolumn
671 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
673 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
674 return row_info[row].bottom_line;
676 return cell_info[row][column].bottom_line;
682 bool LyXTabular::leftAlreadyDrawn(int cell) const
684 int column = column_of_cell(cell);
686 int row = row_of_cell(cell);
688 (cell_info[row][column].multicolumn ==
689 LyXTabular::CELL_PART_OF_MULTICOLUMN));
690 if (getAdditionalWidth(cell_info[row][column].cellno))
692 return rightLine(cell_info[row][column].cellno);
698 bool LyXTabular::isLastRow(int cell) const
700 return (row_of_cell(cell) == rows_ - 1);
704 int LyXTabular::getAdditionalHeight(int row) const
706 if (!row || row >= rows_)
712 for (int column = 0; column < columns_ && bottom; ++column) {
713 switch (cell_info[row - 1][column].multicolumn) {
714 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
715 bottom = cell_info[row - 1][column].bottom_line;
717 case LyXTabular::CELL_NORMAL:
718 bottom = row_info[row - 1].bottom_line;
721 for (int column = 0; column < columns_ && top; ++column) {
722 switch (cell_info[row][column].multicolumn) {
723 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
724 top = cell_info[row][column].top_line;
726 case LyXTabular::CELL_NORMAL:
727 top = row_info[row].top_line;
731 return WIDTH_OF_LINE;
736 int LyXTabular::getAdditionalWidth(int cell) const
738 // internally already set in setWidthOfCell
739 // used to get it back in text.C
740 int const col = right_column_of_cell(cell);
741 int const row = row_of_cell(cell);
742 if (col < columns_ - 1 && rightLine(cell) &&
743 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
745 return WIDTH_OF_LINE;
751 // returns the maximum over all rows
752 int LyXTabular::getWidthOfColumn(int cell) const
754 int const column1 = column_of_cell(cell);
755 int const column2 = right_column_of_cell(cell);
757 for (int i = column1; i <= column2; ++i)
758 result += column_info[i].width_of_column;
763 int LyXTabular::getWidthOfTabular() const
765 return width_of_tabular;
769 // returns true if a complete update is necessary, otherwise false
770 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
772 if (!isMultiColumn(cell))
775 int const row = row_of_cell(cell);
776 int const column1 = column_of_cell(cell);
777 int const column2 = right_column_of_cell(cell);
778 int const old_val = cell_info[row][column2].width_of_cell;
780 // first set columns to 0 so we can calculate the right width
781 for (int i = column1; i <= column2; ++i) {
782 cell_info[row][i].width_of_cell = 0;
784 // set the width to MAX_WIDTH until width > 0
785 int width = new_width + 2 * WIDTH_OF_LINE;
787 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
788 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
789 width -= column_info[i].width_of_column;
792 cell_info[row][i].width_of_cell = width;
794 if (old_val != cell_info[row][column2].width_of_cell) {
795 // in this case we have to recalculate all multicolumn cells which
796 // have this column as one of theirs but not as last one
797 calculate_width_of_column_NMC(i);
798 recalculateMulticolumnsOfColumn(i);
799 calculate_width_of_column(i);
805 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
807 // the last column does not have to be recalculated because all
808 // multicolumns will have here there last multicolumn cell which
809 // always will have the whole rest of the width of the cell.
810 if (column > (columns_ - 2))
812 for(int row = 0; row < rows_; ++row) {
813 int mc = cell_info[row][column].multicolumn;
814 int nmc = cell_info[row][column+1].multicolumn;
815 // we only have to update multicolumns which do not have this
816 // column as their last column!
817 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
818 (mc == CELL_PART_OF_MULTICOLUMN &&
819 nmc == CELL_PART_OF_MULTICOLUMN))
821 int const cellno = cell_info[row][column].cellno;
822 setWidthOfMulticolCell(cellno,
823 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
829 // returns 1 if a complete update is necessary, otherwise 0
830 void LyXTabular::setWidthOfCell(int cell, int new_width)
832 int const row = row_of_cell(cell);
833 int const column1 = column_of_cell(cell);
838 if (rightLine(cell_info[row][column1].cellno, true) &&
839 column1 < columns_ - 1 &&
840 leftLine(cell_info[row][column1+1].cellno, true))
842 add_width = WIDTH_OF_LINE;
845 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
848 if (isMultiColumn(cell, true)) {
849 tmp = setWidthOfMulticolCell(cell, new_width);
851 width = new_width + 2 * WIDTH_OF_LINE + add_width;
852 cell_info[row][column1].width_of_cell = width;
853 tmp = calculate_width_of_column_NMC(column1);
855 recalculateMulticolumnsOfColumn(column1);
858 for (int i = 0; i < columns_; ++i)
859 calculate_width_of_column(i);
860 calculate_width_of_tabular();
865 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
867 if (!isMultiColumn(cell) || onlycolumn)
868 column_info[column_of_cell(cell)].alignment = align;
870 cellinfo_of_cell(cell).alignment = align;
874 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
876 if (!isMultiColumn(cell) || onlycolumn)
877 column_info[column_of_cell(cell)].valignment = align;
879 cellinfo_of_cell(cell).valignment = align;
883 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
885 int const j = column_of_cell(cell);
887 column_info[j].p_width = width;
888 for (int i = 0; i < rows_; ++i) {
889 int const cell = getCellNumber(i, j);
890 // because of multicolumns
891 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
896 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
898 bool const flag = !width.zero();
900 cellinfo_of_cell(cell).p_width = width;
901 if (isMultiColumn(cell)) {
902 getCellInset(cell).setAutoBreakRows(flag);
909 void LyXTabular::setAlignSpecial(int cell, string const & special,
910 LyXTabular::Feature what)
912 if (what == SET_SPECIAL_MULTI)
913 cellinfo_of_cell(cell).align_special = special;
915 column_info[column_of_cell(cell)].align_special = special;
919 void LyXTabular::setAllLines(int cell, bool line)
921 setTopLine(cell, line);
922 setBottomLine(cell, line);
923 setRightLine(cell, line);
924 setLeftLine(cell, line);
928 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
930 int const row = row_of_cell(cell);
931 if (onlycolumn || !isMultiColumn(cell))
932 row_info[row].top_line = line;
934 cellinfo_of_cell(cell).top_line = line;
938 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
940 if (onlycolumn || !isMultiColumn(cell))
941 row_info[row_of_cell(cell)].bottom_line = line;
943 cellinfo_of_cell(cell).bottom_line = line;
947 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
949 if (onlycolumn || !isMultiColumn(cell))
950 column_info[column_of_cell(cell)].left_line = line;
952 cellinfo_of_cell(cell).left_line = line;
956 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
958 if (onlycolumn || !isMultiColumn(cell))
959 column_info[right_column_of_cell(cell)].right_line = line;
961 cellinfo_of_cell(cell).right_line = line;
965 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
967 if (!onlycolumn && isMultiColumn(cell))
968 return cellinfo_of_cell(cell).alignment;
969 return column_info[column_of_cell(cell)].alignment;
973 LyXTabular::VAlignment
974 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
976 if (!onlycolumn && isMultiColumn(cell))
977 return cellinfo_of_cell(cell).valignment;
978 return column_info[column_of_cell(cell)].valignment;
982 LyXLength const LyXTabular::getPWidth(int cell) const
984 if (isMultiColumn(cell))
985 return cellinfo_of_cell(cell).p_width;
986 return column_info[column_of_cell(cell)].p_width;
990 LyXLength const LyXTabular::getColumnPWidth(int cell) const
992 return column_info[column_of_cell(cell)].p_width;
996 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
998 if (isMultiColumn(cell))
999 return cellinfo_of_cell(cell).p_width;
1004 string const LyXTabular::getAlignSpecial(int cell, int what) const
1006 if (what == SET_SPECIAL_MULTI)
1007 return cellinfo_of_cell(cell).align_special;
1008 return column_info[column_of_cell(cell)].align_special;
1012 int LyXTabular::getWidthOfCell(int cell) const
1014 int const row = row_of_cell(cell);
1015 int const column1 = column_of_cell(cell);
1016 int const column2 = right_column_of_cell(cell);
1018 for (int i = column1; i <= column2; ++i)
1019 result += cell_info[row][i].width_of_cell;
1024 int LyXTabular::getBeginningOfTextInCell(int cell) const
1028 switch (getAlignment(cell)) {
1029 case LYX_ALIGN_CENTER:
1030 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1032 case LYX_ALIGN_RIGHT:
1033 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1034 // + getAdditionalWidth(cell);
1037 // LYX_ALIGN_LEFT: nothing :-)
1041 // the LaTeX Way :-(
1047 bool LyXTabular::isFirstCellInRow(int cell) const
1049 return column_of_cell(cell) == 0;
1053 int LyXTabular::getFirstCellInRow(int row) const
1055 if (row > rows_ - 1)
1057 return cell_info[row][0].cellno;
1061 bool LyXTabular::isLastCellInRow(int cell) const
1063 return right_column_of_cell(cell) == columns_ - 1;
1067 int LyXTabular::getLastCellInRow(int row) const
1069 if (row > rows_ - 1)
1071 return cell_info[row][columns_-1].cellno;
1075 void LyXTabular::calculate_width_of_column(int column)
1078 for (int i = 0; i < rows_; ++i)
1079 maximum = max(cell_info[i][column].width_of_cell, maximum);
1080 column_info[column].width_of_column = maximum;
1085 // Calculate the columns regarding ONLY the normal cells and if this
1086 // column is inside a multicolumn cell then use it only if its the last
1087 // column of this multicolumn cell as this gives an added width to the
1088 // column, all the rest should be adapted!
1090 bool LyXTabular::calculate_width_of_column_NMC(int column)
1092 int const old_column_width = column_info[column].width_of_column;
1094 for (int i = 0; i < rows_; ++i) {
1095 int cell = getCellNumber(i, column);
1096 bool ismulti = isMultiColumn(cell, true);
1097 if ((!ismulti || column == right_column_of_cell(cell)) &&
1098 cell_info[i][column].width_of_cell > max)
1100 max = cell_info[i][column].width_of_cell;
1103 column_info[column].width_of_column = max;
1104 return column_info[column].width_of_column != old_column_width;
1108 void LyXTabular::calculate_width_of_tabular()
1110 width_of_tabular = 0;
1111 for (int i = 0; i < columns_; ++i)
1112 width_of_tabular += column_info[i].width_of_column;
1116 int LyXTabular::row_of_cell(int cell) const
1118 if (cell >= numberofcells)
1122 return rowofcell[cell];
1126 int LyXTabular::column_of_cell(int cell) const
1128 if (cell >= numberofcells)
1129 return columns_ - 1;
1132 return columnofcell[cell];
1136 int LyXTabular::right_column_of_cell(int cell) const
1138 int const row = row_of_cell(cell);
1139 int column = column_of_cell(cell);
1140 while (column < columns_ - 1 &&
1141 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1147 void LyXTabular::write(Buffer const * buf, ostream & os) const
1151 << write_attribute("version", 3)
1152 << write_attribute("rows", rows_)
1153 << write_attribute("columns", columns_)
1155 // global longtable options
1157 << write_attribute("rotate", rotate)
1158 << write_attribute("islongtable", is_long_tabular)
1159 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1160 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1161 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1162 << write_attribute("headTopDL", endhead.topDL)
1163 << write_attribute("headBottomDL", endhead.bottomDL)
1164 << write_attribute("footTopDL", endfoot.topDL)
1165 << write_attribute("footBottomDL", endfoot.bottomDL)
1166 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1167 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1168 << write_attribute("lastFootEmpty", endlastfoot.empty)
1170 for (int j = 0; j < columns_; ++j) {
1172 << write_attribute("alignment", column_info[j].alignment)
1173 << write_attribute("valignment", column_info[j].valignment)
1174 << write_attribute("leftline", column_info[j].left_line)
1175 << write_attribute("rightline", column_info[j].right_line)
1176 << write_attribute("width", column_info[j].p_width.asString())
1177 << write_attribute("special", column_info[j].align_special)
1180 for (int i = 0; i < rows_; ++i) {
1182 << write_attribute("topline", row_info[i].top_line)
1183 << write_attribute("bottomline", row_info[i].bottom_line)
1184 << write_attribute("endhead", row_info[i].endhead)
1185 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1186 << write_attribute("endfoot", row_info[i].endfoot)
1187 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1188 << write_attribute("newpage", row_info[i].newpage)
1190 for (int j = 0; j < columns_; ++j) {
1192 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1193 << write_attribute("alignment", cell_info[i][j].alignment)
1194 << write_attribute("valignment", cell_info[i][j].valignment)
1195 << write_attribute("topline", cell_info[i][j].top_line)
1196 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1197 << write_attribute("leftline", cell_info[i][j].left_line)
1198 << write_attribute("rightline", cell_info[i][j].right_line)
1199 << write_attribute("rotate", cell_info[i][j].rotate)
1200 << write_attribute("usebox", cell_info[i][j].usebox)
1201 << write_attribute("width", cell_info[i][j].p_width)
1202 << write_attribute("special", cell_info[i][j].align_special)
1204 os << "\\begin_inset ";
1205 cell_info[i][j].inset.write(buf, os);
1206 os << "\n\\end_inset \n"
1211 os << "</lyxtabular>\n";
1215 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1219 row_info[--hr].endhead = true;
1221 // set firstheader info
1222 if (fhr && fhr < rows_) {
1223 if (row_info[fhr].endhead) {
1225 row_info[--fhr].endfirsthead = true;
1226 row_info[fhr].endhead = false;
1228 } else if (row_info[fhr - 1].endhead) {
1229 endfirsthead.empty = true;
1231 while (fhr > 0 && !row_info[--fhr].endhead) {
1232 row_info[fhr].endfirsthead = true;
1237 if (fr && fr < rows_) {
1238 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1239 while (fr > 0 && !row_info[--fr].endhead) {
1240 row_info[fr].endfoot = true;
1241 row_info[fr].endhead = false;
1243 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1244 while (fr > 0 && !row_info[--fr].endfirsthead) {
1245 row_info[fr].endfoot = true;
1246 row_info[fr].endfirsthead = false;
1248 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1249 while (fr > 0 && !row_info[--fr].endhead &&
1250 !row_info[fr].endfirsthead)
1252 row_info[fr].endfoot = true;
1256 // set lastfooter info
1257 if (lfr && lfr < rows_) {
1258 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1259 while (lfr > 0 && !row_info[--lfr].endhead) {
1260 row_info[lfr].endlastfoot = true;
1261 row_info[lfr].endhead = false;
1263 } else if (row_info[lfr].endfirsthead &&
1264 row_info[lfr - 1].endfirsthead)
1266 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1267 row_info[lfr].endlastfoot = true;
1268 row_info[lfr].endfirsthead = false;
1270 } else if (row_info[lfr].endfoot
1271 && row_info[lfr - 1].endfoot) {
1272 while (lfr > 0 && !row_info[--lfr].endfoot) {
1273 row_info[lfr].endlastfoot = true;
1274 row_info[lfr].endfoot = false;
1276 } else if (!row_info[fr - 1].endhead
1277 && !row_info[fr - 1].endfirsthead &&
1278 !row_info[fr - 1].endfoot)
1281 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1282 !row_info[lfr].endfoot)
1284 row_info[lfr].endlastfoot = true;
1286 } else if (haveLTFoot()) {
1287 endlastfoot.empty = true;
1293 void LyXTabular::read(Buffer const * buf, LyXLex & lex)
1296 istream & is = lex.getStream();
1298 l_getline(is, line);
1299 if (!prefixIs(line, "<lyxtabular ")
1300 && !prefixIs(line, "<LyXTabular ")) {
1306 if (!getTokenValue(line, "version", version))
1308 Assert(version >= 2);
1311 if (!getTokenValue(line, "rows", rows_arg))
1314 if (!getTokenValue(line, "columns", columns_arg))
1316 init(buf->params, rows_arg, columns_arg);
1317 l_getline(is, line);
1318 if (!prefixIs(line, "<features")) {
1319 lyxerr << "Wrong tabular format (expected <features ...> got"
1320 << line << ')' << endl;
1323 getTokenValue(line, "rotate", rotate);
1324 getTokenValue(line, "islongtable", is_long_tabular);
1325 // compatibility read for old longtable options. Now we can make any
1326 // row part of the header/footer type we want before it was strict
1327 // sequential from the first row down (as LaTeX does it!). So now when
1328 // we find a header/footer line we have to go up the rows and set it
1329 // on all preceding rows till the first or one with already a h/f option
1330 // set. If we find a firstheader on the same line as a header or a
1331 // lastfooter on the same line as a footer then this should be set empty.
1339 getTokenValue(line, "endhead", hrow);
1340 getTokenValue(line, "endfirsthead", fhrow);
1341 getTokenValue(line, "endfoot", frow);
1342 getTokenValue(line, "endlastfoot", lfrow);
1343 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1345 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1346 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1347 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1348 getTokenValue(line, "headTopDL", endhead.topDL);
1349 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1350 getTokenValue(line, "footTopDL", endfoot.topDL);
1351 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1352 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1353 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1354 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1356 for (int j = 0; j < columns_; ++j) {
1358 if (!prefixIs(line,"<column")) {
1359 lyxerr << "Wrong tabular format (expected <column ...> got"
1360 << line << ')' << endl;
1363 getTokenValue(line, "alignment", column_info[j].alignment);
1364 getTokenValue(line, "valignment", column_info[j].valignment);
1365 getTokenValue(line, "leftline", column_info[j].left_line);
1366 getTokenValue(line, "rightline", column_info[j].right_line);
1367 getTokenValue(line, "width", column_info[j].p_width);
1368 getTokenValue(line, "special", column_info[j].align_special);
1371 for (int i = 0; i < rows_; ++i) {
1372 l_getline(is, line);
1373 if (!prefixIs(line, "<row")) {
1374 lyxerr << "Wrong tabular format (expected <row ...> got"
1375 << line << ')' << endl;
1378 getTokenValue(line, "topline", row_info[i].top_line);
1379 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1380 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1381 getTokenValue(line, "endhead", row_info[i].endhead);
1382 getTokenValue(line, "endfoot", row_info[i].endfoot);
1383 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1384 getTokenValue(line, "newpage", row_info[i].newpage);
1385 for (int j = 0; j < columns_; ++j) {
1386 l_getline(is, line);
1387 if (!prefixIs(line, "<cell")) {
1388 lyxerr << "Wrong tabular format (expected <cell ...> got"
1389 << line << ')' << endl;
1392 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1393 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1394 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1395 getTokenValue(line, "topline", cell_info[i][j].top_line);
1396 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1397 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1398 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1399 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1400 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1401 getTokenValue(line, "width", cell_info[i][j].p_width);
1402 getTokenValue(line, "special", cell_info[i][j].align_special);
1403 l_getline(is, line);
1404 if (prefixIs(line, "\\begin_inset")) {
1405 cell_info[i][j].inset.read(buf, lex);
1406 l_getline(is, line);
1408 if (!prefixIs(line, "</cell>")) {
1409 lyxerr << "Wrong tabular format (expected </cell> got"
1410 << line << ')' << endl;
1414 l_getline(is, line);
1415 if (!prefixIs(line, "</row>")) {
1416 lyxerr << "Wrong tabular format (expected </row> got"
1417 << line << ')' << endl;
1421 while (!prefixIs(line, "</lyxtabular>")) {
1422 l_getline(is, line);
1424 set_row_column_number_info();
1428 bool LyXTabular::isMultiColumn(int cell, bool real) const
1430 return (!real || column_of_cell(cell) != right_column_of_cell(cell)) &&
1431 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1435 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1437 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1441 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1443 cellstruct & cs = cellinfo_of_cell(cell);
1444 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1445 cs.alignment = column_info[column_of_cell(cell)].alignment;
1446 cs.top_line = row_info[row_of_cell(cell)].top_line;
1447 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1448 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1449 for (int i = 1; i < number; ++i) {
1450 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1451 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1452 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1453 cs1.inset.clear(false);
1455 set_row_column_number_info();
1459 int LyXTabular::cells_in_multicolumn(int cell) const
1461 int const row = row_of_cell(cell);
1462 int column = column_of_cell(cell);
1465 while (column < columns_ &&
1466 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1475 int LyXTabular::unsetMultiColumn(int cell)
1477 int const row = row_of_cell(cell);
1478 int column = column_of_cell(cell);
1482 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1483 cell_info[row][column].multicolumn = CELL_NORMAL;
1485 while (column < columns_ &&
1486 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1488 cell_info[row][column].multicolumn = CELL_NORMAL;
1493 set_row_column_number_info();
1498 void LyXTabular::setLongTabular(bool what)
1500 is_long_tabular = what;
1504 bool LyXTabular::isLongTabular() const
1506 return is_long_tabular;
1510 void LyXTabular::setRotateTabular(bool flag)
1516 bool LyXTabular::getRotateTabular() const
1522 void LyXTabular::setRotateCell(int cell, bool flag)
1524 cellinfo_of_cell(cell).rotate = flag;
1528 bool LyXTabular::getRotateCell(int cell) const
1530 return cellinfo_of_cell(cell).rotate;
1534 bool LyXTabular::needRotating() const
1538 for (int i = 0; i < rows_; ++i)
1539 for (int j = 0; j < columns_; ++j)
1540 if (cell_info[i][j].rotate)
1546 bool LyXTabular::isLastCell(int cell) const
1548 if (cell + 1 < numberofcells)
1554 int LyXTabular::getCellAbove(int cell) const
1556 if (row_of_cell(cell) > 0)
1557 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1562 int LyXTabular::getCellBelow(int cell) const
1564 if (row_of_cell(cell) + 1 < rows_)
1565 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1570 int LyXTabular::getLastCellAbove(int cell) const
1572 if (row_of_cell(cell) <= 0)
1574 if (!isMultiColumn(cell))
1575 return getCellAbove(cell);
1576 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1580 int LyXTabular::getLastCellBelow(int cell) const
1582 if (row_of_cell(cell) + 1 >= rows_)
1584 if (!isMultiColumn(cell))
1585 return getCellBelow(cell);
1586 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1590 int LyXTabular::getCellNumber(int row, int column) const
1592 Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1593 return cell_info[row][column].cellno;
1597 void LyXTabular::setUsebox(int cell, BoxType type)
1599 cellinfo_of_cell(cell).usebox = type;
1603 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1605 if (column_info[column_of_cell(cell)].p_width.zero() &&
1606 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1608 if (cellinfo_of_cell(cell).usebox > 1)
1609 return cellinfo_of_cell(cell).usebox;
1610 return useParbox(cell);
1615 // This are functions used for the longtable support
1617 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1622 row_info[row].endfirsthead = flag;
1626 row_info[row].endhead = flag;
1631 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1634 hd.set = haveLTHead();
1635 return row_info[row].endhead;
1639 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1642 hd.set = haveLTFirstHead();
1643 return row_info[row].endfirsthead;
1647 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1652 row_info[row].endlastfoot = flag;
1656 row_info[row].endfoot = flag;
1661 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1664 fd.set = haveLTFoot();
1665 return row_info[row].endfoot;
1669 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1672 fd.set = haveLTLastFoot();
1673 return row_info[row].endlastfoot;
1677 void LyXTabular::setLTNewPage(int row, bool what)
1679 row_info[row].newpage = what;
1683 bool LyXTabular::getLTNewPage(int row) const
1685 return row_info[row].newpage;
1689 bool LyXTabular::haveLTHead() const
1691 for (int i = 0; i < rows_; ++i)
1692 if (row_info[i].endhead)
1698 bool LyXTabular::haveLTFirstHead() const
1700 if (endfirsthead.empty)
1702 for (int i = 0; i < rows_; ++i)
1703 if (row_info[i].endfirsthead)
1709 bool LyXTabular::haveLTFoot() const
1711 for (int i = 0; i < rows_; ++i)
1712 if (row_info[i].endfoot)
1718 bool LyXTabular::haveLTLastFoot() const
1720 if (endlastfoot.empty)
1722 for (int i = 0; i < rows_; ++i)
1723 if (row_info[i].endlastfoot)
1729 // end longtable support functions
1731 void LyXTabular::setAscentOfRow(int row, int height)
1733 if (row >= rows_ || row_info[row].ascent_of_row == height)
1735 row_info[row].ascent_of_row = height;
1739 void 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;
1747 int LyXTabular::getAscentOfRow(int row) const
1751 return row_info[row].ascent_of_row;
1755 int LyXTabular::getDescentOfRow(int row) const
1759 return row_info[row].descent_of_row;
1763 int LyXTabular::getHeightOfTabular() const
1766 for (int row = 0; row < rows_; ++row)
1767 height += getAscentOfRow(row) + getDescentOfRow(row) +
1768 getAdditionalHeight(row);
1773 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1775 if (row >= rows_ || column >= columns_)
1777 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1781 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1783 if (row < 0 || row >= rows_)
1786 int const fcell = getFirstCellInRow(row);
1787 int const n = numberOfCellsInRow(fcell) + fcell;
1790 for (int i = fcell; i < n; ++i) {
1794 if (tmp == n - fcell) {
1797 for (int i = fcell; i < n; ++i) {
1800 << column_of_cell(i) + 1
1802 << right_column_of_cell(i) + 1
1814 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1816 if (row < 0 || row >= rows_)
1819 int const fcell = getFirstCellInRow(row);
1820 int const n = numberOfCellsInRow(fcell) + fcell;
1823 for (int i = fcell; i < n; ++i) {
1827 if (tmp == n - fcell) {
1830 for (int i = fcell; i < n; ++i) {
1831 if (bottomLine(i)) {
1833 << column_of_cell(i) + 1
1835 << right_column_of_cell(i) + 1
1847 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1851 if (getRotateCell(cell)) {
1852 os << "\\begin{sideways}\n";
1855 if (isMultiColumn(cell)) {
1856 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1857 if (!cellinfo_of_cell(cell).align_special.empty()) {
1858 os << cellinfo_of_cell(cell).align_special << "}{";
1860 if (leftLine(cell) &&
1861 (isFirstCellInRow(cell) ||
1862 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1863 !rightLine(cell - 1, true))))
1867 if (!getPWidth(cell).zero()) {
1868 switch (getVAlignment(cell)) {
1869 case LYX_VALIGN_TOP:
1872 case LYX_VALIGN_CENTER:
1875 case LYX_VALIGN_BOTTOM:
1880 << getPWidth(cell).asLatexString()
1883 switch (getAlignment(cell)) {
1884 case LYX_ALIGN_LEFT:
1887 case LYX_ALIGN_RIGHT:
1895 if (rightLine(cell))
1897 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1903 if (getUsebox(cell) == BOX_PARBOX) {
1905 switch (getVAlignment(cell)) {
1906 case LYX_VALIGN_TOP:
1909 case LYX_VALIGN_CENTER:
1912 case LYX_VALIGN_BOTTOM:
1916 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1917 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1918 os << "\\begin{minipage}[";
1919 switch (getVAlignment(cell)) {
1920 case LYX_VALIGN_TOP:
1923 case LYX_VALIGN_CENTER:
1926 case LYX_VALIGN_BOTTOM:
1930 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1937 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1942 if (getUsebox(cell) == BOX_PARBOX)
1944 else if (getUsebox(cell) == BOX_MINIPAGE) {
1945 os << "%\n\\end{minipage}";
1948 if (isMultiColumn(cell)) {
1951 if (getRotateCell(cell)) {
1952 os << "%\n\\end{sideways}";
1959 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
1960 LatexRunParams const & runparams) const
1962 if (!is_long_tabular)
1966 // output header info
1968 if (endhead.topDL) {
1972 for (int i = 0; i < rows_; ++i) {
1973 if (row_info[i].endhead) {
1974 ret += TeXRow(os, i, buf, runparams);
1977 if (endhead.bottomDL) {
1981 os << "\\endhead\n";
1983 if (endfirsthead.empty) {
1984 os << "\\endfirsthead\n";
1988 // output firstheader info
1989 if (haveLTFirstHead()) {
1990 if (endfirsthead.topDL) {
1994 for (int i = 0; i < rows_; ++i) {
1995 if (row_info[i].endfirsthead) {
1996 ret += TeXRow(os, i, buf, runparams);
1999 if (endfirsthead.bottomDL) {
2003 os << "\\endfirsthead\n";
2006 // output footer info
2008 if (endfoot.topDL) {
2012 for (int i = 0; i < rows_; ++i) {
2013 if (row_info[i].endfoot) {
2014 ret += TeXRow(os, i, buf, runparams);
2017 if (endfoot.bottomDL) {
2021 os << "\\endfoot\n";
2023 if (endlastfoot.empty) {
2024 os << "\\endlastfoot\n";
2028 // output lastfooter info
2029 if (haveLTLastFoot()) {
2030 if (endlastfoot.topDL) {
2034 for (int i = 0; i < rows_; ++i) {
2035 if (row_info[i].endlastfoot) {
2036 ret += TeXRow(os, i, buf, runparams);
2039 if (endlastfoot.bottomDL) {
2043 os << "\\endlastfoot\n";
2050 bool LyXTabular::isValidRow(int const row) const
2052 if (!is_long_tabular)
2054 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2055 !row_info[row].endfoot && !row_info[row].endlastfoot);
2059 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2060 LatexRunParams const & runparams) const
2063 int cell = getCellNumber(i, 0);
2065 ret += TeXTopHLine(os, i);
2066 for (int j = 0; j < columns_; ++j) {
2067 if (isPartOfMultiColumn(i, j))
2069 ret += TeXCellPreamble(os, cell);
2070 InsetText & inset = getCellInset(cell);
2072 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(buf->params) &&
2073 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2077 ret += inset.latex(buf, os, runparams);
2081 ret += TeXCellPostamble(os, cell);
2082 if (!isLastCellInRow(cell)) { // not last cell in row
2088 os << "\\tabularnewline\n";
2090 ret += TeXBottomHLine(os, i);
2095 int LyXTabular::latex(Buffer const * buf, ostream & os,
2096 LatexRunParams const & runparams) const
2100 //+---------------------------------------------------------------------
2101 //+ first the opening preamble +
2102 //+---------------------------------------------------------------------
2105 os << "\\begin{sideways}\n";
2108 if (is_long_tabular)
2109 os << "\\begin{longtable}{";
2111 os << "\\begin{tabular}{";
2112 for (int i = 0; i < columns_; ++i) {
2113 if (!column_info[i].align_special.empty()) {
2114 os << column_info[i].align_special;
2116 if (column_info[i].left_line)
2118 if (!column_info[i].p_width.zero()) {
2119 switch (column_info[i].alignment) {
2120 case LYX_ALIGN_LEFT:
2121 os << ">{\\raggedright}";
2123 case LYX_ALIGN_RIGHT:
2124 os << ">{\\raggedleft}";
2126 case LYX_ALIGN_CENTER:
2127 os << ">{\\centering}";
2129 case LYX_ALIGN_NONE:
2130 case LYX_ALIGN_BLOCK:
2131 case LYX_ALIGN_LAYOUT:
2132 case LYX_ALIGN_SPECIAL:
2136 switch (column_info[i].valignment) {
2137 case LYX_VALIGN_TOP:
2140 case LYX_VALIGN_CENTER:
2143 case LYX_VALIGN_BOTTOM:
2148 << column_info[i].p_width.asLatexString()
2151 switch (column_info[i].alignment) {
2152 case LYX_ALIGN_LEFT:
2155 case LYX_ALIGN_RIGHT:
2163 if (column_info[i].right_line)
2170 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2172 //+---------------------------------------------------------------------
2173 //+ the single row and columns (cells) +
2174 //+---------------------------------------------------------------------
2176 for (int i = 0; i < rows_; ++i) {
2177 if (isValidRow(i)) {
2178 ret += TeXRow(os, i, buf, runparams);
2179 if (is_long_tabular && row_info[i].newpage) {
2180 os << "\\newpage\n";
2186 //+---------------------------------------------------------------------
2187 //+ the closing of the tabular +
2188 //+---------------------------------------------------------------------
2190 if (is_long_tabular)
2191 os << "\\end{longtable}";
2193 os << "\\end{tabular}";
2195 os << "\n\\end{sideways}";
2203 int LyXTabular::linuxdoc(Buffer const * buf, ostream & os) const
2205 os << "<tabular ca=\"";
2206 for (int i = 0; i < columns_; ++i) {
2207 switch (column_info[i].alignment) {
2208 case LYX_ALIGN_LEFT:
2211 case LYX_ALIGN_RIGHT:
2222 for (int i = 0; i < rows_; ++i) {
2223 for (int j = 0; j < columns_; ++j) {
2224 if (isPartOfMultiColumn(i, j))
2226 InsetText & inset = getCellInset(cell);
2228 ret += inset.linuxdoc(buf, os);
2230 if (isLastCellInRow(cell)) {
2239 os << "</tabular>\n";
2244 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2247 int cell = getFirstCellInRow(row);
2250 for (int j = 0; j < columns_; ++j) {
2251 if (isPartOfMultiColumn(row, j))
2254 os << "<entry align=\"";
2255 switch (getAlignment(cell)) {
2256 case LYX_ALIGN_LEFT:
2259 case LYX_ALIGN_RIGHT:
2267 os << "\" valign=\"";
2268 switch (getVAlignment(cell)) {
2269 case LYX_VALIGN_TOP:
2272 case LYX_VALIGN_BOTTOM:
2275 case LYX_VALIGN_CENTER:
2280 if (isMultiColumn(cell)) {
2281 os << " namest=\"col" << j << "\" ";
2282 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2286 ret += getCellInset(cell).docbook(buf, os, true);
2295 int LyXTabular::docbook(Buffer const * buf, ostream & os,
2296 bool /*mixcont*/) const
2300 //+---------------------------------------------------------------------
2301 //+ first the opening preamble +
2302 //+---------------------------------------------------------------------
2304 os << "<tgroup cols=\"" << columns_
2305 << "\" colsep=\"1\" rowsep=\"1\">\n";
2307 for (int i = 0; i < columns_; ++i) {
2308 os << "<colspec colname=\"col" << i << "\" align=\"";
2309 switch (column_info[i].alignment) {
2310 case LYX_ALIGN_LEFT:
2313 case LYX_ALIGN_RIGHT:
2324 //+---------------------------------------------------------------------
2325 //+ Long Tabular case +
2326 //+---------------------------------------------------------------------
2328 // output header info
2329 if (haveLTHead() || haveLTFirstHead()) {
2332 for (int i = 0; i < rows_; ++i) {
2333 if (row_info[i].endhead || row_info[i].endfirsthead) {
2334 ret += docbookRow(buf, os, i);
2340 // output footer info
2341 if (haveLTFoot() || haveLTLastFoot()) {
2344 for (int i = 0; i < rows_; ++i) {
2345 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2346 ret += docbookRow(buf, os, i);
2353 //+---------------------------------------------------------------------
2354 //+ the single row and columns (cells) +
2355 //+---------------------------------------------------------------------
2359 for (int i = 0; i < rows_; ++i) {
2360 if (isValidRow(i)) {
2361 ret += docbookRow(buf, os, i);
2366 //+---------------------------------------------------------------------
2367 //+ the closing of the tabular +
2368 //+---------------------------------------------------------------------
2377 int LyXTabular::asciiTopHLine(ostream & os, int row,
2378 vector<unsigned int> const & clen) const
2380 int const fcell = getFirstCellInRow(row);
2381 int const n = numberOfCellsInRow(fcell) + fcell;
2384 for (int i = fcell; i < n; ++i) {
2394 for (int i = fcell; i < n; ++i) {
2405 int column = column_of_cell(i);
2406 int len = clen[column];
2407 while (isPartOfMultiColumn(row, ++column))
2408 len += clen[column] + 4;
2409 os << string(len, ch);
2424 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2425 vector<unsigned int> const & clen) const
2427 int const fcell = getFirstCellInRow(row);
2428 int const n = numberOfCellsInRow(fcell) + fcell;
2431 for (int i = fcell; i < n; ++i) {
2432 if (bottomLine(i)) {
2441 for (int i = fcell; i < n; ++i) {
2442 if (bottomLine(i)) {
2452 int column = column_of_cell(i);
2453 int len = clen[column];
2454 while (isPartOfMultiColumn(row, ++column))
2455 len += clen[column] + 4;
2456 os << string(len, ch);
2457 if (bottomLine(i)) {
2471 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2472 int cell, int row, int column,
2473 vector<unsigned int> const & clen,
2474 bool onlydata) const
2477 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2489 unsigned int len1 = sstr.str().length();
2490 unsigned int len2 = clen[column];
2491 while (isPartOfMultiColumn(row, ++column))
2492 len2 += clen[column] + 4;
2495 switch (getAlignment(cell)) {
2497 case LYX_ALIGN_LEFT:
2500 case LYX_ALIGN_RIGHT:
2504 case LYX_ALIGN_CENTER:
2510 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2512 if (rightLine(cell))
2521 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2522 bool onlydata, unsigned char delim) const
2526 // first calculate the width of the single columns
2527 vector<unsigned int> clen(columns_);
2530 // first all non (real) multicolumn cells!
2531 for (int j = 0; j < columns_; ++j) {
2533 for (int i = 0; i < rows_; ++i) {
2534 int cell = getCellNumber(i, j);
2535 if (isMultiColumn(cell, true))
2538 getCellInset(cell).ascii(buf, sstr, 0);
2539 if (clen[j] < sstr.str().length())
2540 clen[j] = sstr.str().length();
2543 // then all (real) multicolumn cells!
2544 for (int j = 0; j < columns_; ++j) {
2545 for (int i = 0; i < rows_; ++i) {
2546 int cell = getCellNumber(i, j);
2547 if (!isMultiColumn(cell, true) || isPartOfMultiColumn(i, j))
2550 getCellInset(cell).ascii(buf, sstr, 0);
2551 int len = int(sstr.str().length());
2552 int const n = cells_in_multicolumn(cell);
2553 for (int k = j; len > 0 && k < j + n - 1; ++k)
2555 if (len > int(clen[j + n - 1]))
2556 clen[j + n - 1] = len;
2561 for (int i = 0; i < rows_; ++i) {
2562 if (!onlydata && asciiTopHLine(os, i, clen))
2563 for (int j = 0; j < depth; ++j)
2565 for (int j = 0; j < columns_; ++j) {
2566 if (isPartOfMultiColumn(i, j))
2568 if (onlydata && j > 0)
2570 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2575 for (int j = 0; j < depth; ++j)
2577 if (asciiBottomHLine(os, i, clen))
2578 for (int j = 0; j < depth; ++j)
2586 InsetText & LyXTabular::getCellInset(int cell) const
2589 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2593 InsetText & LyXTabular::getCellInset(int row, int column) const
2595 cur_cell = getCellNumber(row, column);
2596 return cell_info[row][column].inset;
2600 int LyXTabular::getCellFromInset(InsetOld const * inset, int maybe_cell) const
2602 // is this inset part of the tabular?
2603 if (!inset || inset->owner() != owner_) {
2604 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2608 const int save_cur_cell = cur_cell;
2609 int cell = cur_cell;
2610 if (&getCellInset(cell) != inset) {
2612 if (cell == -1 || &getCellInset(cell) != inset)
2617 for (cell = getNumberOfCells(); cell >= 0; --cell)
2618 if (&getCellInset(cell) == inset)
2621 lyxerr[Debug::INSETTEXT]
2622 << "LyXTabular::getCellFromInset: "
2624 << ", cur_cell=" << save_cur_cell
2625 << ", maybe_cell=" << maybe_cell
2627 // We should have found a cell at this point
2629 lyxerr << "LyXTabular::getCellFromInset: "
2630 << "Cell not found!" << endl;
2638 void LyXTabular::validate(LaTeXFeatures & features) const
2640 features.require("NeedTabularnewline");
2641 if (isLongTabular())
2642 features.require("longtable");
2644 features.require("rotating");
2645 for (int cell = 0; cell < numberofcells; ++cell) {
2646 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2647 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2648 features.require("array");
2649 getCellInset(cell).validate(features);
2654 void LyXTabular::getLabelList(std::vector<string> & list) const
2656 for (int i = 0; i < rows_; ++i)
2657 for (int j = 0; j < columns_; ++j)
2658 getCellInset(i, j).getLabelList(list);
2662 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2664 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2665 ParagraphList::const_iterator cit = parlist.begin();
2666 ParagraphList::const_iterator end = parlist.end();
2668 for (; cit != end; ++cit)
2669 for (int i = 0; i < cit->size(); ++i)
2670 if (cit->isNewline(i))