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(0, 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();
410 rowofcell = vector<int>();
411 columnofcell = vector<int>();
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];
441 for (int i = row + 1; i < rows_; ++i) {
442 for (int j = 0; j < columns_; ++j) {
443 c_info[i][j] = cell_info[i-1][j];
448 for (int j = 0; j < columns_; ++j) {
449 cell_info[row][j].inset.clear(false);
450 if (bp.tracking_changes)
451 cell_info[row][j].inset.markNew(true);
458 void LyXTabular::deleteRow(int row)
460 // Not allowed to delete last row
464 row_info.erase(row_info.begin() + row);
465 cell_info.erase(cell_info.begin() + row);
471 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
475 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
477 int const column = column_of_cell(cell);
478 column_vector::iterator cit = column_info.begin() + column + 1;
479 column_info.insert(cit, columnstruct());
480 // set the column values of the column before
481 column_info[column + 1] = column_info[column];
483 for (int i = 0; i < rows_; ++i) {
484 for (int j = 0; j <= column; ++j) {
485 c_info[i][j] = cell_info[i][j];
487 for (int j = column + 1; j < columns_; ++j) {
488 c_info[i][j] = cell_info[i][j - 1];
490 // care about multicolumns
491 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
493 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
495 if (column + 2 >= columns_ ||
496 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
498 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
503 for (int i = 0; i < rows_; ++i) {
504 cell_info[i][column + 1].inset.clear(false);
505 if (bp.tracking_changes)
506 cell_info[i][column + 1].inset.markNew(true);
512 void LyXTabular::deleteColumn(int column)
514 // Not allowed to delete last column
518 column_info.erase(column_info.begin() + column);
519 for (int i = 0; i < rows_; ++i)
520 cell_info[i].erase(cell_info[i].begin() + column);
526 void LyXTabular::reinit()
532 void LyXTabular::Reinit(bool reset_widths)
535 for (int i = 0; i < rows_; ++i) {
536 for (int j = 0; j < columns_; ++j) {
537 cell_info[i][j].width_of_cell = 0;
538 cell_info[i][j].inset.setOwner(owner_);
543 for (int i = 0; i < columns_; ++i)
544 calculate_width_of_column(i);
546 calculate_width_of_tabular();
548 set_row_column_number_info();
552 void LyXTabular::set_row_column_number_info(bool oldformat)
555 for (int row = 0; row < rows_; ++row) {
556 for (int column = 0; column < columns_; ++column) {
557 if (cell_info[row][column].multicolumn
558 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
560 cell_info[row][column].cellno = numberofcells;
563 ++numberofcells; // because this is one more than as we start from 0
565 rowofcell.resize(numberofcells);
566 columnofcell.resize(numberofcells);
568 for (int row = 0, column = 0, c = 0;
569 c < numberofcells && row < rows_ && column < columns_;) {
571 columnofcell[c] = column;
575 } while (column < columns_ &&
576 cell_info[row][column].multicolumn
577 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
578 if (column == columns_) {
584 for (int row = 0; row < rows_; ++row) {
585 for (int column = 0; column < columns_; ++column) {
586 if (isPartOfMultiColumn(row,column))
588 // now set the right line of multicolumns right for oldformat read
590 cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
592 int cn = cells_in_multicolumn(cell_info[row][column].cellno);
593 cell_info[row][column].right_line =
594 cell_info[row][column+cn-1].right_line;
596 cell_info[row][column].inset.setAutoBreakRows(
597 !getPWidth(getCellNumber(row, column)).zero());
603 int LyXTabular::getNumberOfCells() const
605 return numberofcells;
609 int LyXTabular::numberOfCellsInRow(int cell) const
611 int const row = row_of_cell(cell);
613 for (int i = 0; i < columns_; ++i)
614 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
620 // returns 1 if there is a topline, returns 0 if not
621 bool LyXTabular::topLine(int cell, bool onlycolumn) const
623 if (!onlycolumn && isMultiColumn(cell))
624 return cellinfo_of_cell(cell).top_line;
625 return row_info[row_of_cell(cell)].top_line;
629 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
631 if (!onlycolumn && isMultiColumn(cell))
632 return cellinfo_of_cell(cell).bottom_line;
633 return row_info[row_of_cell(cell)].bottom_line;
637 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
639 if (!onlycolumn && isMultiColumn(cell) &&
640 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
642 if (cellinfo_of_cell(cell).align_special.empty())
643 return cellinfo_of_cell(cell).left_line;
644 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), "|");
646 if (column_info[column_of_cell(cell)].align_special.empty())
647 return column_info[column_of_cell(cell)].left_line;
648 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
652 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
654 if (!onlycolumn && isMultiColumn(cell) &&
655 (isLastCellInRow(cell) || isMultiColumn(cell+1)))
657 if (cellinfo_of_cell(cell).align_special.empty())
658 return cellinfo_of_cell(cell).right_line;
659 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), "|");
661 if (column_info[column_of_cell(cell)].align_special.empty())
662 return column_info[right_column_of_cell(cell)].right_line;
663 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
667 bool LyXTabular::topAlreadyDrawn(int cell) const
669 int row = row_of_cell(cell);
670 if (row > 0 && !getAdditionalHeight(row)) {
671 int column = column_of_cell(cell);
674 && cell_info[row][column].multicolumn
675 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
677 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
678 return row_info[row].bottom_line;
680 return cell_info[row][column].bottom_line;
686 bool LyXTabular::leftAlreadyDrawn(int cell) const
688 int column = column_of_cell(cell);
690 int row = row_of_cell(cell);
692 (cell_info[row][column].multicolumn ==
693 LyXTabular::CELL_PART_OF_MULTICOLUMN));
694 if (getAdditionalWidth(cell_info[row][column].cellno))
696 return rightLine(cell_info[row][column].cellno);
702 bool LyXTabular::isLastRow(int cell) const
704 return (row_of_cell(cell) == rows_ - 1);
708 int LyXTabular::getAdditionalHeight(int row) const
710 if (!row || row >= rows_)
716 for (int column = 0; column < columns_ && bottom; ++column) {
717 switch (cell_info[row - 1][column].multicolumn) {
718 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
719 bottom = cell_info[row - 1][column].bottom_line;
721 case LyXTabular::CELL_NORMAL:
722 bottom = row_info[row - 1].bottom_line;
725 for (int column = 0; column < columns_ && top; ++column) {
726 switch (cell_info[row][column].multicolumn) {
727 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
728 top = cell_info[row][column].top_line;
730 case LyXTabular::CELL_NORMAL:
731 top = row_info[row].top_line;
735 return WIDTH_OF_LINE;
740 int LyXTabular::getAdditionalWidth(int cell) const
742 // internally already set in setWidthOfCell
743 // used to get it back in text.C
744 int const col = right_column_of_cell(cell);
745 int const row = row_of_cell(cell);
746 if (col < columns_ - 1 && rightLine(cell) &&
747 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
749 return WIDTH_OF_LINE;
755 // returns the maximum over all rows
756 int LyXTabular::getWidthOfColumn(int cell) const
758 int const column1 = column_of_cell(cell);
759 int const column2 = right_column_of_cell(cell);
761 for (int i = column1; i <= column2; ++i)
762 result += column_info[i].width_of_column;
767 int LyXTabular::getWidthOfTabular() const
769 return width_of_tabular;
773 // returns true if a complete update is necessary, otherwise false
774 bool LyXTabular::setWidthOfMulticolCell(int cell, int new_width)
776 if (!isMultiColumn(cell))
779 int const row = row_of_cell(cell);
780 int const column1 = column_of_cell(cell);
781 int const column2 = right_column_of_cell(cell);
782 int const old_val = cell_info[row][column2].width_of_cell;
784 // first set columns to 0 so we can calculate the right width
785 for (int i = column1; i <= column2; ++i) {
786 cell_info[row][i].width_of_cell = 0;
788 // set the width to MAX_WIDTH until width > 0
789 int width = new_width + 2 * WIDTH_OF_LINE;
791 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
792 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
793 width -= column_info[i].width_of_column;
796 cell_info[row][i].width_of_cell = width;
798 if (old_val != cell_info[row][column2].width_of_cell) {
799 // in this case we have to recalculate all multicolumn cells which
800 // have this column as one of theirs but not as last one
801 calculate_width_of_column_NMC(i);
802 recalculateMulticolumnsOfColumn(i);
803 calculate_width_of_column(i);
809 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
811 // the last column does not have to be recalculated because all
812 // multicolumns will have here there last multicolumn cell which
813 // always will have the whole rest of the width of the cell.
814 if (column > (columns_ - 2))
816 for(int row = 0; row < rows_; ++row) {
817 int mc = cell_info[row][column].multicolumn;
818 int nmc = cell_info[row][column+1].multicolumn;
819 // we only have to update multicolumns which do not have this
820 // column as their last column!
821 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
822 (mc == CELL_PART_OF_MULTICOLUMN &&
823 nmc == CELL_PART_OF_MULTICOLUMN))
825 int const cellno = cell_info[row][column].cellno;
826 setWidthOfMulticolCell(cellno,
827 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
833 // returns 1 if a complete update is necessary, otherwise 0
834 bool LyXTabular::setWidthOfCell(int cell, int new_width)
836 int const row = row_of_cell(cell);
837 int const column1 = column_of_cell(cell);
842 if (rightLine(cell_info[row][column1].cellno, true) &&
843 column1 < columns_ - 1 &&
844 leftLine(cell_info[row][column1+1].cellno, true))
847 add_width = WIDTH_OF_LINE;
849 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width) {
852 if (isMultiColumn(cell, true)) {
853 tmp = setWidthOfMulticolCell(cell, new_width);
855 width = new_width + 2 * WIDTH_OF_LINE + add_width;
856 cell_info[row][column1].width_of_cell = width;
857 tmp = calculate_width_of_column_NMC(column1);
859 recalculateMulticolumnsOfColumn(column1);
862 for (int i = 0; i < columns_; ++i)
863 calculate_width_of_column(i);
864 calculate_width_of_tabular();
871 void LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
873 if (!isMultiColumn(cell) || onlycolumn)
874 column_info[column_of_cell(cell)].alignment = align;
876 cellinfo_of_cell(cell).alignment = align;
880 void LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
882 if (!isMultiColumn(cell) || onlycolumn)
883 column_info[column_of_cell(cell)].valignment = align;
885 cellinfo_of_cell(cell).valignment = align;
889 void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
891 int const j = column_of_cell(cell);
893 column_info[j].p_width = width;
894 for (int i = 0; i < rows_; ++i) {
895 int const cell = getCellNumber(i, j);
896 // because of multicolumns
897 getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
902 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
904 bool const flag = !width.zero();
906 cellinfo_of_cell(cell).p_width = width;
907 if (isMultiColumn(cell)) {
908 getCellInset(cell).setAutoBreakRows(flag);
915 void LyXTabular::setAlignSpecial(int cell, string const & special,
916 LyXTabular::Feature what)
918 if (what == SET_SPECIAL_MULTI)
919 cellinfo_of_cell(cell).align_special = special;
921 column_info[column_of_cell(cell)].align_special = special;
925 void LyXTabular::setAllLines(int cell, bool line)
927 setTopLine(cell, line);
928 setBottomLine(cell, line);
929 setRightLine(cell, line);
930 setLeftLine(cell, line);
934 void LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
936 int const row = row_of_cell(cell);
937 if (onlycolumn || !isMultiColumn(cell))
938 row_info[row].top_line = line;
940 cellinfo_of_cell(cell).top_line = line;
944 void LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
946 if (onlycolumn || !isMultiColumn(cell))
947 row_info[row_of_cell(cell)].bottom_line = line;
949 cellinfo_of_cell(cell).bottom_line = line;
953 void LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
955 if (onlycolumn || !isMultiColumn(cell))
956 column_info[column_of_cell(cell)].left_line = line;
958 cellinfo_of_cell(cell).left_line = line;
962 void LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
964 if (onlycolumn || !isMultiColumn(cell))
965 column_info[right_column_of_cell(cell)].right_line = line;
967 cellinfo_of_cell(cell).right_line = line;
971 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
973 if (!onlycolumn && isMultiColumn(cell))
974 return cellinfo_of_cell(cell).alignment;
975 return column_info[column_of_cell(cell)].alignment;
979 LyXTabular::VAlignment
980 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
982 if (!onlycolumn && isMultiColumn(cell))
983 return cellinfo_of_cell(cell).valignment;
984 return column_info[column_of_cell(cell)].valignment;
988 LyXLength const LyXTabular::getPWidth(int cell) const
990 if (isMultiColumn(cell))
991 return cellinfo_of_cell(cell).p_width;
992 return column_info[column_of_cell(cell)].p_width;
996 LyXLength const LyXTabular::getColumnPWidth(int cell) const
998 return column_info[column_of_cell(cell)].p_width;
1002 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
1004 if (isMultiColumn(cell))
1005 return cellinfo_of_cell(cell).p_width;
1010 string const LyXTabular::getAlignSpecial(int cell, int what) const
1012 if (what == SET_SPECIAL_MULTI)
1013 return cellinfo_of_cell(cell).align_special;
1014 return column_info[column_of_cell(cell)].align_special;
1018 int LyXTabular::getWidthOfCell(int cell) const
1020 int const row = row_of_cell(cell);
1021 int const column1 = column_of_cell(cell);
1022 int const column2 = right_column_of_cell(cell);
1024 for (int i = column1; i <= column2; ++i)
1025 result += cell_info[row][i].width_of_cell;
1030 int LyXTabular::getBeginningOfTextInCell(int cell) const
1034 switch (getAlignment(cell)) {
1035 case LYX_ALIGN_CENTER:
1036 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1038 case LYX_ALIGN_RIGHT:
1039 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1040 // + getAdditionalWidth(cell);
1043 // LYX_ALIGN_LEFT: nothing :-)
1047 // the LaTeX Way :-(
1053 bool LyXTabular::isFirstCellInRow(int cell) const
1055 return column_of_cell(cell) == 0;
1059 int LyXTabular::getFirstCellInRow(int row) const
1061 if (row > rows_ - 1)
1063 return cell_info[row][0].cellno;
1067 bool LyXTabular::isLastCellInRow(int cell) const
1069 return right_column_of_cell(cell) == columns_ - 1;
1073 int LyXTabular::getLastCellInRow(int row) const
1075 if (row > rows_ - 1)
1077 return cell_info[row][columns_-1].cellno;
1081 void LyXTabular::calculate_width_of_column(int column)
1084 for (int i = 0; i < rows_; ++i)
1085 maximum = max(cell_info[i][column].width_of_cell, maximum);
1086 column_info[column].width_of_column = maximum;
1091 // Calculate the columns regarding ONLY the normal cells and if this
1092 // column is inside a multicolumn cell then use it only if its the last
1093 // column of this multicolumn cell as this gives an added width to the
1094 // column, all the rest should be adapted!
1096 bool LyXTabular::calculate_width_of_column_NMC(int column)
1098 int const old_column_width = column_info[column].width_of_column;
1100 for (int i = 0; i < rows_; ++i) {
1101 int cell = getCellNumber(i, column);
1102 bool ismulti = isMultiColumn(cell, true);
1103 if ((!ismulti || column == right_column_of_cell(cell)) &&
1104 cell_info[i][column].width_of_cell > max)
1106 max = cell_info[i][column].width_of_cell;
1109 column_info[column].width_of_column = max;
1110 return column_info[column].width_of_column != old_column_width;
1114 void LyXTabular::calculate_width_of_tabular()
1116 width_of_tabular = 0;
1117 for (int i = 0; i < columns_; ++i)
1118 width_of_tabular += column_info[i].width_of_column;
1122 int LyXTabular::row_of_cell(int cell) const
1124 if (cell >= numberofcells)
1128 return rowofcell[cell];
1132 int LyXTabular::column_of_cell(int cell) const
1134 if (cell >= numberofcells)
1135 return columns_ - 1;
1138 return columnofcell[cell];
1142 int LyXTabular::right_column_of_cell(int cell) const
1144 int const row = row_of_cell(cell);
1145 int column = column_of_cell(cell);
1146 while (column < columns_ - 1 &&
1147 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1153 void LyXTabular::write(Buffer const * buf, ostream & os) const
1157 << write_attribute("version", 3)
1158 << write_attribute("rows", rows_)
1159 << write_attribute("columns", columns_)
1161 // global longtable options
1163 << write_attribute("rotate", rotate)
1164 << write_attribute("islongtable", is_long_tabular)
1165 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1166 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1167 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1168 << write_attribute("headTopDL", endhead.topDL)
1169 << write_attribute("headBottomDL", endhead.bottomDL)
1170 << write_attribute("footTopDL", endfoot.topDL)
1171 << write_attribute("footBottomDL", endfoot.bottomDL)
1172 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1173 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1174 << write_attribute("lastFootEmpty", endlastfoot.empty)
1176 for (int j = 0; j < columns_; ++j) {
1178 << write_attribute("alignment", column_info[j].alignment)
1179 << write_attribute("valignment", column_info[j].valignment)
1180 << write_attribute("leftline", column_info[j].left_line)
1181 << write_attribute("rightline", column_info[j].right_line)
1182 << write_attribute("width", column_info[j].p_width.asString())
1183 << write_attribute("special", column_info[j].align_special)
1186 for (int i = 0; i < rows_; ++i) {
1188 << write_attribute("topline", row_info[i].top_line)
1189 << write_attribute("bottomline", row_info[i].bottom_line)
1190 << write_attribute("endhead", row_info[i].endhead)
1191 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1192 << write_attribute("endfoot", row_info[i].endfoot)
1193 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1194 << write_attribute("newpage", row_info[i].newpage)
1196 for (int j = 0; j < columns_; ++j) {
1198 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1199 << write_attribute("alignment", cell_info[i][j].alignment)
1200 << write_attribute("valignment", cell_info[i][j].valignment)
1201 << write_attribute("topline", cell_info[i][j].top_line)
1202 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1203 << write_attribute("leftline", cell_info[i][j].left_line)
1204 << write_attribute("rightline", cell_info[i][j].right_line)
1205 << write_attribute("rotate", cell_info[i][j].rotate)
1206 << write_attribute("usebox", cell_info[i][j].usebox)
1207 << write_attribute("width", cell_info[i][j].p_width)
1208 << write_attribute("special", cell_info[i][j].align_special)
1210 os << "\\begin_inset ";
1211 cell_info[i][j].inset.write(buf, os);
1212 os << "\n\\end_inset \n"
1217 os << "</lyxtabular>\n";
1221 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1225 row_info[--hr].endhead = true;
1227 // set firstheader info
1228 if (fhr && fhr < rows_) {
1229 if (row_info[fhr].endhead) {
1231 row_info[--fhr].endfirsthead = true;
1232 row_info[fhr].endhead = false;
1234 } else if (row_info[fhr - 1].endhead) {
1235 endfirsthead.empty = true;
1237 while (fhr > 0 && !row_info[--fhr].endhead) {
1238 row_info[fhr].endfirsthead = true;
1243 if (fr && fr < rows_) {
1244 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1245 while (fr > 0 && !row_info[--fr].endhead) {
1246 row_info[fr].endfoot = true;
1247 row_info[fr].endhead = false;
1249 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1250 while (fr > 0 && !row_info[--fr].endfirsthead) {
1251 row_info[fr].endfoot = true;
1252 row_info[fr].endfirsthead = false;
1254 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1255 while (fr > 0 && !row_info[--fr].endhead &&
1256 !row_info[fr].endfirsthead)
1258 row_info[fr].endfoot = true;
1262 // set lastfooter info
1263 if (lfr && lfr < rows_) {
1264 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1265 while (lfr > 0 && !row_info[--lfr].endhead) {
1266 row_info[lfr].endlastfoot = true;
1267 row_info[lfr].endhead = false;
1269 } else if (row_info[lfr].endfirsthead &&
1270 row_info[lfr - 1].endfirsthead)
1272 while (lfr > 0 && !row_info[--lfr].endfirsthead) {
1273 row_info[lfr].endlastfoot = true;
1274 row_info[lfr].endfirsthead = false;
1276 } else if (row_info[lfr].endfoot
1277 && row_info[lfr - 1].endfoot) {
1278 while (lfr > 0 && !row_info[--lfr].endfoot) {
1279 row_info[lfr].endlastfoot = true;
1280 row_info[lfr].endfoot = false;
1282 } else if (!row_info[fr - 1].endhead
1283 && !row_info[fr - 1].endfirsthead &&
1284 !row_info[fr - 1].endfoot)
1287 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1288 !row_info[lfr].endfoot)
1290 row_info[lfr].endlastfoot = true;
1292 } else if (haveLTFoot()) {
1293 endlastfoot.empty = true;
1299 void LyXTabular::read(Buffer const * buf, LyXLex & lex)
1302 istream & is = lex.getStream();
1304 l_getline(is, line);
1305 if (!prefixIs(line, "<lyxtabular ")
1306 && !prefixIs(line, "<LyXTabular ")) {
1312 if (!getTokenValue(line, "version", version))
1314 Assert(version >= 2);
1317 if (!getTokenValue(line, "rows", rows_arg))
1320 if (!getTokenValue(line, "columns", columns_arg))
1322 init(buf->params, rows_arg, columns_arg);
1323 l_getline(is, line);
1324 if (!prefixIs(line, "<features")) {
1325 lyxerr << "Wrong tabular format (expected <features ...> got"
1326 << line << ')' << endl;
1329 getTokenValue(line, "rotate", rotate);
1330 getTokenValue(line, "islongtable", is_long_tabular);
1331 // compatibility read for old longtable options. Now we can make any
1332 // row part of the header/footer type we want before it was strict
1333 // sequential from the first row down (as LaTeX does it!). So now when
1334 // we find a header/footer line we have to go up the rows and set it
1335 // on all preceding rows till the first or one with already a h/f option
1336 // set. If we find a firstheader on the same line as a header or a
1337 // lastfooter on the same line as a footer then this should be set empty.
1345 getTokenValue(line, "endhead", hrow);
1346 getTokenValue(line, "endfirsthead", fhrow);
1347 getTokenValue(line, "endfoot", frow);
1348 getTokenValue(line, "endlastfoot", lfrow);
1349 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1351 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1352 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1353 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1354 getTokenValue(line, "headTopDL", endhead.topDL);
1355 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1356 getTokenValue(line, "footTopDL", endfoot.topDL);
1357 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1358 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1359 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1360 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1362 for (int j = 0; j < columns_; ++j) {
1364 if (!prefixIs(line,"<column")) {
1365 lyxerr << "Wrong tabular format (expected <column ...> got"
1366 << line << ')' << endl;
1369 getTokenValue(line, "alignment", column_info[j].alignment);
1370 getTokenValue(line, "valignment", column_info[j].valignment);
1371 getTokenValue(line, "leftline", column_info[j].left_line);
1372 getTokenValue(line, "rightline", column_info[j].right_line);
1373 getTokenValue(line, "width", column_info[j].p_width);
1374 getTokenValue(line, "special", column_info[j].align_special);
1377 for (int i = 0; i < rows_; ++i) {
1378 l_getline(is, line);
1379 if (!prefixIs(line, "<row")) {
1380 lyxerr << "Wrong tabular format (expected <row ...> got"
1381 << line << ')' << endl;
1384 getTokenValue(line, "topline", row_info[i].top_line);
1385 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1386 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1387 getTokenValue(line, "endhead", row_info[i].endhead);
1388 getTokenValue(line, "endfoot", row_info[i].endfoot);
1389 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1390 getTokenValue(line, "newpage", row_info[i].newpage);
1391 for (int j = 0; j < columns_; ++j) {
1392 l_getline(is, line);
1393 if (!prefixIs(line, "<cell")) {
1394 lyxerr << "Wrong tabular format (expected <cell ...> got"
1395 << line << ')' << endl;
1398 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1399 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1400 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1401 getTokenValue(line, "topline", cell_info[i][j].top_line);
1402 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1403 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1404 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1405 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1406 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1407 getTokenValue(line, "width", cell_info[i][j].p_width);
1408 getTokenValue(line, "special", cell_info[i][j].align_special);
1409 l_getline(is, line);
1410 if (prefixIs(line, "\\begin_inset")) {
1411 cell_info[i][j].inset.read(buf, lex);
1412 l_getline(is, line);
1414 if (!prefixIs(line, "</cell>")) {
1415 lyxerr << "Wrong tabular format (expected </cell> got"
1416 << line << ')' << endl;
1420 l_getline(is, line);
1421 if (!prefixIs(line, "</row>")) {
1422 lyxerr << "Wrong tabular format (expected </row> got"
1423 << line << ')' << endl;
1427 while (!prefixIs(line, "</lyxtabular>")) {
1428 l_getline(is, line);
1430 set_row_column_number_info();
1434 bool LyXTabular::isMultiColumn(int cell, bool real) const
1436 return (!real || column_of_cell(cell) != right_column_of_cell(cell)) &&
1437 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1441 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(int cell) const
1443 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1447 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1449 cellstruct & cs = cellinfo_of_cell(cell);
1450 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1451 cs.alignment = column_info[column_of_cell(cell)].alignment;
1452 cs.top_line = row_info[row_of_cell(cell)].top_line;
1453 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1454 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1455 for (int i = 1; i < number; ++i) {
1456 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1457 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1458 cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
1459 cs1.inset.clear(false);
1461 set_row_column_number_info();
1465 int LyXTabular::cells_in_multicolumn(int cell) const
1467 int const row = row_of_cell(cell);
1468 int column = column_of_cell(cell);
1471 while (column < columns_ &&
1472 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1481 int LyXTabular::unsetMultiColumn(int cell)
1483 int const row = row_of_cell(cell);
1484 int column = column_of_cell(cell);
1488 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1489 cell_info[row][column].multicolumn = CELL_NORMAL;
1491 while (column < columns_ &&
1492 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1494 cell_info[row][column].multicolumn = CELL_NORMAL;
1499 set_row_column_number_info();
1504 void LyXTabular::setLongTabular(bool what)
1506 is_long_tabular = what;
1510 bool LyXTabular::isLongTabular() const
1512 return is_long_tabular;
1516 void LyXTabular::setRotateTabular(bool flag)
1522 bool LyXTabular::getRotateTabular() const
1528 void LyXTabular::setRotateCell(int cell, bool flag)
1530 cellinfo_of_cell(cell).rotate = flag;
1534 bool LyXTabular::getRotateCell(int cell) const
1536 return cellinfo_of_cell(cell).rotate;
1540 bool LyXTabular::needRotating() const
1544 for (int i = 0; i < rows_; ++i)
1545 for (int j = 0; j < columns_; ++j)
1546 if (cell_info[i][j].rotate)
1552 bool LyXTabular::isLastCell(int cell) const
1554 if (cell + 1 < numberofcells)
1560 int LyXTabular::getCellAbove(int cell) const
1562 if (row_of_cell(cell) > 0)
1563 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1568 int LyXTabular::getCellBelow(int cell) const
1570 if (row_of_cell(cell) + 1 < rows_)
1571 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1576 int LyXTabular::getLastCellAbove(int cell) const
1578 if (row_of_cell(cell) <= 0)
1580 if (!isMultiColumn(cell))
1581 return getCellAbove(cell);
1582 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1586 int LyXTabular::getLastCellBelow(int cell) const
1588 if (row_of_cell(cell) + 1 >= rows_)
1590 if (!isMultiColumn(cell))
1591 return getCellBelow(cell);
1592 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1596 int LyXTabular::getCellNumber(int row, int column) const
1598 Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1599 return cell_info[row][column].cellno;
1603 void LyXTabular::setUsebox(int cell, BoxType type)
1605 cellinfo_of_cell(cell).usebox = type;
1609 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1611 if (column_info[column_of_cell(cell)].p_width.zero() &&
1612 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1614 if (cellinfo_of_cell(cell).usebox > 1)
1615 return cellinfo_of_cell(cell).usebox;
1616 return useParbox(cell);
1621 // This are functions used for the longtable support
1623 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1628 row_info[row].endfirsthead = flag;
1632 row_info[row].endhead = flag;
1637 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1640 hd.set = haveLTHead();
1641 return row_info[row].endhead;
1645 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1648 hd.set = haveLTFirstHead();
1649 return row_info[row].endfirsthead;
1653 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1658 row_info[row].endlastfoot = flag;
1662 row_info[row].endfoot = flag;
1667 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1670 fd.set = haveLTFoot();
1671 return row_info[row].endfoot;
1675 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1678 fd.set = haveLTLastFoot();
1679 return row_info[row].endlastfoot;
1683 void LyXTabular::setLTNewPage(int row, bool what)
1685 row_info[row].newpage = what;
1689 bool LyXTabular::getLTNewPage(int row) const
1691 return row_info[row].newpage;
1695 bool LyXTabular::haveLTHead() const
1697 for (int i = 0; i < rows_; ++i)
1698 if (row_info[i].endhead)
1704 bool LyXTabular::haveLTFirstHead() const
1706 if (endfirsthead.empty)
1708 for (int i = 0; i < rows_; ++i)
1709 if (row_info[i].endfirsthead)
1715 bool LyXTabular::haveLTFoot() const
1717 for (int i = 0; i < rows_; ++i)
1718 if (row_info[i].endfoot)
1724 bool LyXTabular::haveLTLastFoot() const
1726 if (endlastfoot.empty)
1728 for (int i = 0; i < rows_; ++i)
1729 if (row_info[i].endlastfoot)
1735 // end longtable support functions
1737 bool LyXTabular::setAscentOfRow(int row, int height)
1739 if (row >= rows_ || row_info[row].ascent_of_row == height)
1741 row_info[row].ascent_of_row = height;
1746 bool LyXTabular::setDescentOfRow(int row, int height)
1748 if (row >= rows_ || row_info[row].descent_of_row == height)
1750 row_info[row].descent_of_row = height;
1755 int LyXTabular::getAscentOfRow(int row) const
1759 return row_info[row].ascent_of_row;
1763 int LyXTabular::getDescentOfRow(int row) const
1767 return row_info[row].descent_of_row;
1771 int LyXTabular::getHeightOfTabular() const
1774 for (int row = 0; row < rows_; ++row)
1775 height += getAscentOfRow(row) + getDescentOfRow(row) +
1776 getAdditionalHeight(row);
1781 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1783 if (row >= rows_ || column >= columns_)
1785 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1789 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1791 if (row < 0 || row >= rows_)
1794 int const fcell = getFirstCellInRow(row);
1795 int const n = numberOfCellsInRow(fcell) + fcell;
1798 for (int i = fcell; i < n; ++i) {
1802 if (tmp == n - fcell) {
1805 for (int i = fcell; i < n; ++i) {
1808 << column_of_cell(i) + 1
1810 << right_column_of_cell(i) + 1
1822 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1824 if (row < 0 || row >= rows_)
1827 int const fcell = getFirstCellInRow(row);
1828 int const n = numberOfCellsInRow(fcell) + fcell;
1831 for (int i = fcell; i < n; ++i) {
1835 if (tmp == n - fcell) {
1838 for (int i = fcell; i < n; ++i) {
1839 if (bottomLine(i)) {
1841 << column_of_cell(i) + 1
1843 << right_column_of_cell(i) + 1
1855 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1859 if (getRotateCell(cell)) {
1860 os << "\\begin{sideways}\n";
1863 if (isMultiColumn(cell)) {
1864 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1865 if (!cellinfo_of_cell(cell).align_special.empty()) {
1866 os << cellinfo_of_cell(cell).align_special << "}{";
1868 if (leftLine(cell) &&
1869 (isFirstCellInRow(cell) ||
1870 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1871 !rightLine(cell - 1, true))))
1875 if (!getPWidth(cell).zero()) {
1876 switch (getVAlignment(cell)) {
1877 case LYX_VALIGN_TOP:
1880 case LYX_VALIGN_CENTER:
1883 case LYX_VALIGN_BOTTOM:
1888 << getPWidth(cell).asLatexString()
1891 switch (getAlignment(cell)) {
1892 case LYX_ALIGN_LEFT:
1895 case LYX_ALIGN_RIGHT:
1903 if (rightLine(cell))
1905 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1911 if (getUsebox(cell) == BOX_PARBOX) {
1913 switch (getVAlignment(cell)) {
1914 case LYX_VALIGN_TOP:
1917 case LYX_VALIGN_CENTER:
1920 case LYX_VALIGN_BOTTOM:
1924 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1925 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1926 os << "\\begin{minipage}[";
1927 switch (getVAlignment(cell)) {
1928 case LYX_VALIGN_TOP:
1931 case LYX_VALIGN_CENTER:
1934 case LYX_VALIGN_BOTTOM:
1938 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1945 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1950 if (getUsebox(cell) == BOX_PARBOX)
1952 else if (getUsebox(cell) == BOX_MINIPAGE) {
1953 os << "%\n\\end{minipage}";
1956 if (isMultiColumn(cell)) {
1959 if (getRotateCell(cell)) {
1960 os << "%\n\\end{sideways}";
1967 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
1968 LatexRunParams const & runparams) const
1970 if (!is_long_tabular)
1974 // output header info
1976 if (endhead.topDL) {
1980 for (int i = 0; i < rows_; ++i) {
1981 if (row_info[i].endhead) {
1982 ret += TeXRow(os, i, buf, runparams);
1985 if (endhead.bottomDL) {
1989 os << "\\endhead\n";
1991 if (endfirsthead.empty) {
1992 os << "\\endfirsthead\n";
1996 // output firstheader info
1997 if (haveLTFirstHead()) {
1998 if (endfirsthead.topDL) {
2002 for (int i = 0; i < rows_; ++i) {
2003 if (row_info[i].endfirsthead) {
2004 ret += TeXRow(os, i, buf, runparams);
2007 if (endfirsthead.bottomDL) {
2011 os << "\\endfirsthead\n";
2014 // output footer info
2016 if (endfoot.topDL) {
2020 for (int i = 0; i < rows_; ++i) {
2021 if (row_info[i].endfoot) {
2022 ret += TeXRow(os, i, buf, runparams);
2025 if (endfoot.bottomDL) {
2029 os << "\\endfoot\n";
2031 if (endlastfoot.empty) {
2032 os << "\\endlastfoot\n";
2036 // output lastfooter info
2037 if (haveLTLastFoot()) {
2038 if (endlastfoot.topDL) {
2042 for (int i = 0; i < rows_; ++i) {
2043 if (row_info[i].endlastfoot) {
2044 ret += TeXRow(os, i, buf, runparams);
2047 if (endlastfoot.bottomDL) {
2051 os << "\\endlastfoot\n";
2058 bool LyXTabular::isValidRow(int const row) const
2060 if (!is_long_tabular)
2062 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2063 !row_info[row].endfoot && !row_info[row].endlastfoot);
2067 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2068 LatexRunParams const & runparams) const
2071 int cell = getCellNumber(i, 0);
2073 ret += TeXTopHLine(os, i);
2074 for (int j = 0; j < columns_; ++j) {
2075 if (isPartOfMultiColumn(i, j))
2077 ret += TeXCellPreamble(os, cell);
2078 InsetText & inset = getCellInset(cell);
2080 bool rtl = inset.paragraphs.begin()->isRightToLeftPar(buf->params) &&
2081 !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
2085 ret += inset.latex(buf, os, runparams);
2089 ret += TeXCellPostamble(os, cell);
2090 if (!isLastCellInRow(cell)) { // not last cell in row
2096 os << "\\tabularnewline\n";
2098 ret += TeXBottomHLine(os, i);
2103 int LyXTabular::latex(Buffer const * buf, ostream & os,
2104 LatexRunParams const & runparams) const
2108 //+---------------------------------------------------------------------
2109 //+ first the opening preamble +
2110 //+---------------------------------------------------------------------
2113 os << "\\begin{sideways}\n";
2116 if (is_long_tabular)
2117 os << "\\begin{longtable}{";
2119 os << "\\begin{tabular}{";
2120 for (int i = 0; i < columns_; ++i) {
2121 if (!column_info[i].align_special.empty()) {
2122 os << column_info[i].align_special;
2124 if (column_info[i].left_line)
2126 if (!column_info[i].p_width.zero()) {
2127 switch (column_info[i].alignment) {
2128 case LYX_ALIGN_LEFT:
2129 os << ">{\\raggedright}";
2131 case LYX_ALIGN_RIGHT:
2132 os << ">{\\raggedleft}";
2134 case LYX_ALIGN_CENTER:
2135 os << ">{\\centering}";
2137 case LYX_ALIGN_NONE:
2138 case LYX_ALIGN_BLOCK:
2139 case LYX_ALIGN_LAYOUT:
2140 case LYX_ALIGN_SPECIAL:
2144 switch (column_info[i].valignment) {
2145 case LYX_VALIGN_TOP:
2148 case LYX_VALIGN_CENTER:
2151 case LYX_VALIGN_BOTTOM:
2156 << column_info[i].p_width.asLatexString()
2159 switch (column_info[i].alignment) {
2160 case LYX_ALIGN_LEFT:
2163 case LYX_ALIGN_RIGHT:
2171 if (column_info[i].right_line)
2178 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2180 //+---------------------------------------------------------------------
2181 //+ the single row and columns (cells) +
2182 //+---------------------------------------------------------------------
2184 for (int i = 0; i < rows_; ++i) {
2185 if (isValidRow(i)) {
2186 ret += TeXRow(os, i, buf, runparams);
2187 if (is_long_tabular && row_info[i].newpage) {
2188 os << "\\newpage\n";
2194 //+---------------------------------------------------------------------
2195 //+ the closing of the tabular +
2196 //+---------------------------------------------------------------------
2198 if (is_long_tabular)
2199 os << "\\end{longtable}";
2201 os << "\\end{tabular}";
2203 os << "\n\\end{sideways}";
2211 int LyXTabular::linuxdoc(Buffer const * buf, ostream & os) const
2213 os << "<tabular ca=\"";
2214 for (int i = 0; i < columns_; ++i) {
2215 switch (column_info[i].alignment) {
2216 case LYX_ALIGN_LEFT:
2219 case LYX_ALIGN_RIGHT:
2230 for (int i = 0; i < rows_; ++i) {
2231 for (int j = 0; j < columns_; ++j) {
2232 if (isPartOfMultiColumn(i, j))
2234 InsetText & inset = getCellInset(cell);
2236 ret += inset.linuxdoc(buf, os);
2238 if (isLastCellInRow(cell)) {
2247 os << "</tabular>\n";
2252 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2255 int cell = getFirstCellInRow(row);
2258 for (int j = 0; j < columns_; ++j) {
2259 if (isPartOfMultiColumn(row, j))
2262 os << "<entry align=\"";
2263 switch (getAlignment(cell)) {
2264 case LYX_ALIGN_LEFT:
2267 case LYX_ALIGN_RIGHT:
2275 os << "\" valign=\"";
2276 switch (getVAlignment(cell)) {
2277 case LYX_VALIGN_TOP:
2280 case LYX_VALIGN_BOTTOM:
2283 case LYX_VALIGN_CENTER:
2288 if (isMultiColumn(cell)) {
2289 os << " namest=\"col" << j << "\" ";
2290 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2294 ret += getCellInset(cell).docbook(buf, os, true);
2303 int LyXTabular::docbook(Buffer const * buf, ostream & os,
2304 bool /*mixcont*/) const
2308 //+---------------------------------------------------------------------
2309 //+ first the opening preamble +
2310 //+---------------------------------------------------------------------
2312 os << "<tgroup cols=\"" << columns_
2313 << "\" colsep=\"1\" rowsep=\"1\">\n";
2315 for (int i = 0; i < columns_; ++i) {
2316 os << "<colspec colname=\"col" << i << "\" align=\"";
2317 switch (column_info[i].alignment) {
2318 case LYX_ALIGN_LEFT:
2321 case LYX_ALIGN_RIGHT:
2332 //+---------------------------------------------------------------------
2333 //+ Long Tabular case +
2334 //+---------------------------------------------------------------------
2336 // output header info
2337 if (haveLTHead() || haveLTFirstHead()) {
2340 for (int i = 0; i < rows_; ++i) {
2341 if (row_info[i].endhead || row_info[i].endfirsthead) {
2342 ret += docbookRow(buf, os, i);
2348 // output footer info
2349 if (haveLTFoot() || haveLTLastFoot()) {
2352 for (int i = 0; i < rows_; ++i) {
2353 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2354 ret += docbookRow(buf, os, i);
2361 //+---------------------------------------------------------------------
2362 //+ the single row and columns (cells) +
2363 //+---------------------------------------------------------------------
2367 for (int i = 0; i < rows_; ++i) {
2368 if (isValidRow(i)) {
2369 ret += docbookRow(buf, os, i);
2374 //+---------------------------------------------------------------------
2375 //+ the closing of the tabular +
2376 //+---------------------------------------------------------------------
2385 int LyXTabular::asciiTopHLine(ostream & os, int row,
2386 vector<unsigned int> const & clen) const
2388 int const fcell = getFirstCellInRow(row);
2389 int const n = numberOfCellsInRow(fcell) + fcell;
2392 for (int i = fcell; i < n; ++i) {
2402 for (int i = fcell; i < n; ++i) {
2413 int column = column_of_cell(i);
2414 int len = clen[column];
2415 while (isPartOfMultiColumn(row, ++column))
2416 len += clen[column] + 4;
2417 os << string(len, ch);
2432 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2433 vector<unsigned int> const & clen) const
2435 int const fcell = getFirstCellInRow(row);
2436 int const n = numberOfCellsInRow(fcell) + fcell;
2439 for (int i = fcell; i < n; ++i) {
2440 if (bottomLine(i)) {
2449 for (int i = fcell; i < n; ++i) {
2450 if (bottomLine(i)) {
2460 int column = column_of_cell(i);
2461 int len = clen[column];
2462 while (isPartOfMultiColumn(row, ++column))
2463 len += clen[column] + 4;
2464 os << string(len, ch);
2465 if (bottomLine(i)) {
2479 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2480 int cell, int row, int column,
2481 vector<unsigned int> const & clen,
2482 bool onlydata) const
2485 int ret = getCellInset(cell).ascii(buf, sstr, 0);
2497 unsigned int len1 = sstr.str().length();
2498 unsigned int len2 = clen[column];
2499 while (isPartOfMultiColumn(row, ++column))
2500 len2 += clen[column] + 4;
2503 switch (getAlignment(cell)) {
2505 case LYX_ALIGN_LEFT:
2508 case LYX_ALIGN_RIGHT:
2512 case LYX_ALIGN_CENTER:
2518 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2520 if (rightLine(cell))
2529 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2530 bool onlydata, unsigned char delim) const
2534 // first calculate the width of the single columns
2535 vector<unsigned int> clen(columns_);
2538 // first all non (real) multicolumn cells!
2539 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))
2546 getCellInset(cell).ascii(buf, sstr, 0);
2547 if (clen[j] < sstr.str().length())
2548 clen[j] = sstr.str().length();
2551 // then all (real) multicolumn cells!
2552 for (int j = 0; j < columns_; ++j) {
2553 for (int i = 0; i < rows_; ++i) {
2554 int cell = getCellNumber(i, j);
2555 if (!isMultiColumn(cell, true) || isPartOfMultiColumn(i, j))
2558 getCellInset(cell).ascii(buf, sstr, 0);
2559 int len = int(sstr.str().length());
2560 int const n = cells_in_multicolumn(cell);
2561 for (int k = j; len > 0 && k < j + n - 1; ++k)
2563 if (len > int(clen[j + n - 1]))
2564 clen[j + n - 1] = len;
2569 for (int i = 0; i < rows_; ++i) {
2570 if (!onlydata && asciiTopHLine(os, i, clen))
2571 for (int j = 0; j < depth; ++j)
2573 for (int j = 0; j < columns_; ++j) {
2574 if (isPartOfMultiColumn(i, j))
2576 if (onlydata && j > 0)
2578 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2583 for (int j = 0; j < depth; ++j)
2585 if (asciiBottomHLine(os, i, clen))
2586 for (int j = 0; j < depth; ++j)
2594 InsetText & LyXTabular::getCellInset(int cell) const
2597 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2601 InsetText & LyXTabular::getCellInset(int row, int column) const
2603 cur_cell = getCellNumber(row, column);
2604 return cell_info[row][column].inset;
2608 int LyXTabular::getCellFromInset(InsetOld const * inset, int maybe_cell) const
2610 // is this inset part of the tabular?
2611 if (!inset || inset->owner() != owner_) {
2612 lyxerr << "this is not a cell of the tabular!" << endl;
2616 const int save_cur_cell = cur_cell;
2617 int cell = cur_cell;
2618 if (&getCellInset(cell) != inset) {
2620 if (cell == -1 || &getCellInset(cell) != inset) {
2626 for (cell = getNumberOfCells(); cell >= 0; --cell) {
2627 if (&getCellInset(cell) == inset)
2630 lyxerr[Debug::INSETTEXT]
2631 << "LyXTabular::getCellFromInset: "
2633 << ", cur_cell=" << save_cur_cell
2634 << ", maybe_cell=" << maybe_cell
2636 // We should have found a cell at this point
2638 lyxerr << "LyXTabular::getCellFromInset: "
2639 << "Cell not found!" << endl;
2647 void LyXTabular::validate(LaTeXFeatures & features) const
2649 features.require("NeedTabularnewline");
2650 if (isLongTabular())
2651 features.require("longtable");
2653 features.require("rotating");
2654 for (int cell = 0; cell < numberofcells; ++cell) {
2655 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2656 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2657 features.require("array");
2658 getCellInset(cell).validate(features);
2663 void LyXTabular::getLabelList(std::vector<string> & list) const
2665 for (int i = 0; i < rows_; ++i)
2666 for (int j = 0; j < columns_; ++j)
2667 getCellInset(i, j).getLabelList(list);
2671 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2673 ParagraphList const & parlist = getCellInset(cell).paragraphs;
2674 ParagraphList::const_iterator cit = parlist.begin();
2675 ParagraphList::const_iterator end = parlist.end();
2677 for (; cit != end; ++cit)
2678 for (int i = 0; i < cit->size(); ++i)
2679 if (cit->isNewline(i))