3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
23 #include "bufferparams.h"
24 #include "BufferView.h"
27 #include "LaTeXFeatures.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
33 #include "insets/insettabular.h"
35 #include "support/lstrings.h"
36 #include "support/convert.h"
43 using support::prefixIs;
46 using support::suffixIs;
48 using boost::shared_ptr;
49 using boost::dynamic_pointer_cast;
58 using std::ostringstream;
62 #ifndef CXX_GLOBAL_CSTD
69 int const WIDTH_OF_LINE = 5;
70 int const default_line_space = 10;
74 string const write_attribute(string const & name, T const & t)
76 string const s = tostr(t);
77 return s.empty() ? s : " " + name + "=\"" + s + "\"";
81 string const write_attribute(string const & name, string const & t)
83 return t.empty() ? t : " " + name + "=\"" + t + "\"";
88 string const write_attribute(string const & name, docstring const & t)
90 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
95 string const write_attribute(string const & name, bool const & b)
97 // we write only true attribute values so we remove a bit of the
98 // file format bloat for tabulars.
99 return b ? write_attribute(name, convert<string>(b)) : string();
104 string const write_attribute(string const & name, int const & i)
106 // we write only true attribute values so we remove a bit of the
107 // file format bloat for tabulars.
108 return i ? write_attribute(name, convert<string>(i)) : string();
113 string const write_attribute(string const & name, LyXTabular::idx_type const & i)
115 // we write only true attribute values so we remove a bit of the
116 // file format bloat for tabulars.
117 return i ? write_attribute(name, convert<string>(i)) : string();
122 string const write_attribute(string const & name, LyXLength const & value)
124 // we write only the value if we really have one same reson as above.
125 return value.zero() ? string() : write_attribute(name, value.asString());
129 string const tostr(LyXAlignment const & num)
134 case LYX_ALIGN_BLOCK:
138 case LYX_ALIGN_CENTER:
140 case LYX_ALIGN_RIGHT:
142 case LYX_ALIGN_LAYOUT:
144 case LYX_ALIGN_SPECIAL:
151 string const tostr(LyXTabular::VAlignment const & num)
154 case LyXTabular::LYX_VALIGN_TOP:
156 case LyXTabular::LYX_VALIGN_MIDDLE:
158 case LyXTabular::LYX_VALIGN_BOTTOM:
165 string const tostr(LyXTabular::BoxType const & num)
168 case LyXTabular::BOX_NONE:
170 case LyXTabular::BOX_PARBOX:
172 case LyXTabular::BOX_MINIPAGE:
179 // I would have liked a fromstr template a lot better. (Lgb)
180 bool string2type(string const str, LyXAlignment & num)
183 num = LYX_ALIGN_NONE;
184 else if (str == "block")
185 num = LYX_ALIGN_BLOCK;
186 else if (str == "left")
187 num = LYX_ALIGN_LEFT;
188 else if (str == "center")
189 num = LYX_ALIGN_CENTER;
190 else if (str == "right")
191 num = LYX_ALIGN_RIGHT;
198 bool string2type(string const str, LyXTabular::VAlignment & num)
201 num = LyXTabular::LYX_VALIGN_TOP;
202 else if (str == "middle" )
203 num = LyXTabular::LYX_VALIGN_MIDDLE;
204 else if (str == "bottom")
205 num = LyXTabular::LYX_VALIGN_BOTTOM;
212 bool string2type(string const str, LyXTabular::BoxType & num)
215 num = LyXTabular::BOX_NONE;
216 else if (str == "parbox")
217 num = LyXTabular::BOX_PARBOX;
218 else if (str == "minipage")
219 num = LyXTabular::BOX_MINIPAGE;
226 bool string2type(string const str, bool & num)
230 else if (str == "false")
238 bool getTokenValue(string const & str, char const * token, string & ret)
241 size_t token_length = strlen(token);
242 string::size_type pos = str.find(token);
244 if (pos == string::npos || pos + token_length + 1 >= str.length()
245 || str[pos + token_length] != '=')
247 pos += token_length + 1;
249 if (ch != '"' && ch != '\'') { // only read till next space
253 while (pos < str.length() - 1 && str[++pos] != ch)
260 bool getTokenValue(string const & str, char const * token, docstring & ret)
263 bool const success = getTokenValue(str, token, tmp);
264 ret = from_utf8(tmp);
269 bool getTokenValue(string const & str, char const * token, int & num)
273 if (!getTokenValue(str, token, tmp))
275 num = convert<int>(tmp);
280 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
283 return getTokenValue(str, token, tmp) && string2type(tmp, num);
287 bool getTokenValue(string const & str, char const * token,
288 LyXTabular::VAlignment & num)
291 return getTokenValue(str, token, tmp) && string2type(tmp, num);
295 bool getTokenValue(string const & str, char const * token,
296 LyXTabular::BoxType & num)
299 return getTokenValue(str, token, tmp) && string2type(tmp, num);
303 bool getTokenValue(string const & str, char const * token, bool & flag)
305 // set the flag always to false as this should be the default for bools
306 // not in the file-format.
309 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
313 bool getTokenValue(string const & str, char const * token, LyXLength & len)
315 // set the lenght to be zero() as default as this it should be if not
316 // in the file format.
319 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
323 bool getTokenValue(string const & str, char const * token, LyXLength & len, bool & flag)
328 if (!getTokenValue(str, token, tmp))
330 if (tmp == "default") {
334 return isValidLength(tmp, &len);
338 void l_getline(istream & is, string & str)
341 while (str.empty()) {
343 if (!str.empty() && str[str.length() - 1] == '\r')
344 str.erase(str.length() - 1);
350 /// Define a few methods for the inner structs
352 LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
355 multicolumn(LyXTabular::CELL_NORMAL),
356 alignment(LYX_ALIGN_CENTER),
357 valignment(LYX_VALIGN_TOP),
364 inset(new InsetText(bp))
368 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
370 width_of_cell(cs.width_of_cell),
371 multicolumn(cs.multicolumn),
372 alignment(cs.alignment),
373 valignment(cs.valignment),
374 top_line(cs.top_line),
375 bottom_line(cs.bottom_line),
376 left_line(cs.left_line),
377 right_line(cs.right_line),
380 align_special(cs.align_special),
382 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
386 LyXTabular::cellstruct &
387 LyXTabular::cellstruct::operator=(cellstruct cs)
395 LyXTabular::cellstruct::swap(cellstruct & rhs)
397 std::swap(cellno, rhs.cellno);
398 std::swap(width_of_cell, rhs.width_of_cell);
399 std::swap(multicolumn, rhs.multicolumn);
400 std::swap(alignment, rhs.alignment);
401 std::swap(valignment, rhs.valignment);
402 std::swap(top_line, rhs.top_line);
403 std::swap(bottom_line, rhs.bottom_line);
404 std::swap(left_line, rhs.left_line);
405 std::swap(right_line, rhs.right_line);
406 std::swap(usebox, rhs.usebox);
407 std::swap(rotate, rhs.rotate);
408 std::swap(align_special, rhs.align_special);
409 p_width.swap(rhs.p_width);
410 inset.swap(rhs.inset);
414 LyXTabular::rowstruct::rowstruct()
419 top_space_default(false),
420 bottom_space_default(false),
421 interline_space_default(false),
430 LyXTabular::columnstruct::columnstruct()
431 : alignment(LYX_ALIGN_CENTER),
432 valignment(LYX_VALIGN_TOP),
440 LyXTabular::ltType::ltType()
447 LyXTabular::LyXTabular(BufferParams const & bp, row_type rows_arg,
448 col_type columns_arg)
450 init(bp, rows_arg, columns_arg);
454 // activates all lines and sets all widths to 0
455 void LyXTabular::init(BufferParams const & bp, row_type rows_arg,
456 col_type columns_arg)
459 columns_ = columns_arg;
460 row_info = row_vector(rows_);
461 column_info = column_vector(columns_);
462 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
463 row_info.reserve(10);
464 column_info.reserve(10);
465 cell_info.reserve(100);
467 for (row_type i = 0; i < rows_; ++i)
468 cell_info[i].back().right_line = true;
469 row_info.back().bottom_line = true;
470 row_info.front().bottom_line = true;
471 column_info.back().right_line = true;
472 is_long_tabular = false;
474 use_booktabs = false;
478 void LyXTabular::fixCellNums()
481 for (row_type i = 0; i < rows_; ++i) {
482 for (col_type j = 0; j < columns_; ++j) {
483 // When debugging it can be nice to set
485 cell_info[i][j].inset->setDrawFrame(false);
486 cell_info[i][j].cellno = cellno++;
488 cell_info[i].back().right_line = true;
491 set_row_column_number_info();
495 void LyXTabular::appendRow(BufferParams const & bp, idx_type const cell)
499 row_type const row = row_of_cell(cell);
501 row_vector::iterator rit = row_info.begin() + row;
502 row_info.insert(rit, rowstruct());
503 // now set the values of the row before
504 row_info[row] = row_info[row + 1];
506 cell_vvector old(rows_ - 1);
507 for (row_type i = 0; i < rows_ - 1; ++i)
508 swap(cell_info[i], old[i]);
510 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
512 for (row_type i = 0; i <= row; ++i)
513 swap(cell_info[i], old[i]);
514 for (row_type i = row + 2; i < rows_; ++i)
515 swap(cell_info[i], old[i - 1]);
518 for (col_type j = 0; j < columns_; ++j)
519 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
521 set_row_column_number_info();
525 void LyXTabular::deleteRow(row_type const row)
527 // Not allowed to delete last row
531 row_info.erase(row_info.begin() + row);
532 cell_info.erase(cell_info.begin() + row);
538 void LyXTabular::copyRow(BufferParams const & bp, row_type const row)
542 row_info.insert(row_info.begin() + row, row_info[row]);
543 cell_info.insert(cell_info.begin() + row, cell_info[row]);
546 for (col_type j = 0; j < columns_; ++j)
547 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
549 set_row_column_number_info();
553 void LyXTabular::appendColumn(BufferParams const & bp, idx_type const cell)
557 col_type const column = column_of_cell(cell);
558 column_vector::iterator cit = column_info.begin() + column + 1;
559 column_info.insert(cit, columnstruct());
560 // set the column values of the column before
561 column_info[column + 1] = column_info[column];
563 for (row_type i = 0; i < rows_; ++i) {
564 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
566 // care about multicolumns
567 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
568 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
570 if (column + 2 >= columns_
571 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
572 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
575 for (row_type i = 0; i < rows_; ++i) {
576 cell_info[i][column + 1].inset->clear();
578 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
584 void LyXTabular::deleteColumn(col_type const column)
586 // Not allowed to delete last column
590 column_info.erase(column_info.begin() + column);
591 for (row_type i = 0; i < rows_; ++i)
592 cell_info[i].erase(cell_info[i].begin() + column);
598 void LyXTabular::copyColumn(BufferParams const & bp, col_type const column)
602 column_info.insert(column_info.begin() + column, column_info[column]);
604 for (row_type i = 0; i < rows_; ++i)
605 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
608 for (row_type i = 0; i < rows_; ++i)
609 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
614 void LyXTabular::set_row_column_number_info()
617 for (row_type row = 0; row < rows_; ++row) {
618 for (col_type column = 0; column < columns_; ++column) {
619 if (cell_info[row][column].multicolumn
620 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
622 if (numberofcells == 0)
623 // FIXME: Is this intended?
624 cell_info[row][column].cellno = npos;
626 cell_info[row][column].cellno =
631 rowofcell.resize(numberofcells);
632 columnofcell.resize(numberofcells);
637 c < numberofcells && row < rows_ && column < columns_;) {
639 columnofcell[c] = column;
643 } while (column < columns_ &&
644 cell_info[row][column].multicolumn
645 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
647 if (column == columns_) {
653 for (row_type row = 0; row < rows_; ++row) {
654 for (col_type column = 0; column < columns_; ++column) {
655 if (isPartOfMultiColumn(row,column))
657 cell_info[row][column].inset->setAutoBreakRows(
658 !getPWidth(getCellNumber(row, column)).zero());
664 LyXTabular::idx_type LyXTabular::getNumberOfCells() const
666 return numberofcells;
670 LyXTabular::idx_type LyXTabular::numberOfCellsInRow(idx_type const cell) const
672 row_type const row = row_of_cell(cell);
674 for (col_type i = 0; i < columns_; ++i)
675 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
681 bool LyXTabular::topLine(idx_type const cell, bool const wholerow) const
683 if (!wholerow && isMultiColumn(cell) &&
684 !(use_booktabs && row_of_cell(cell) == 0))
685 return cellinfo_of_cell(cell).top_line;
686 return row_info[row_of_cell(cell)].top_line;
690 bool LyXTabular::bottomLine(idx_type const cell, bool wholerow) const
692 if (!wholerow && isMultiColumn(cell) &&
693 !(use_booktabs && isLastRow(cell)))
694 return cellinfo_of_cell(cell).bottom_line;
695 return row_info[row_of_cell(cell)].bottom_line;
699 bool LyXTabular::leftLine(idx_type cell, bool wholecolumn) const
703 if (!wholecolumn && isMultiColumn(cell) &&
704 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
706 if (cellinfo_of_cell(cell).align_special.empty())
707 return cellinfo_of_cell(cell).left_line;
708 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), '|');
710 if (column_info[column_of_cell(cell)].align_special.empty())
711 return column_info[column_of_cell(cell)].left_line;
712 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), '|');
716 bool LyXTabular::rightLine(idx_type cell, bool wholecolumn) const
720 if (!wholecolumn && isMultiColumn(cell) &&
721 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
723 if (cellinfo_of_cell(cell).align_special.empty())
724 return cellinfo_of_cell(cell).right_line;
725 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), '|');
727 if (column_info[column_of_cell(cell)].align_special.empty())
728 return column_info[right_column_of_cell(cell)].right_line;
729 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), '|');
733 bool LyXTabular::topAlreadyDrawn(idx_type cell) const
735 row_type row = row_of_cell(cell);
736 if (row > 0 && !getAdditionalHeight(row)) {
737 col_type column = column_of_cell(cell);
740 && cell_info[row][column].multicolumn
741 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
743 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
744 return row_info[row].bottom_line;
746 return cell_info[row][column].bottom_line;
752 bool LyXTabular::leftAlreadyDrawn(idx_type cell) const
754 col_type column = column_of_cell(cell);
756 row_type row = row_of_cell(cell);
758 (cell_info[row][column].multicolumn ==
759 LyXTabular::CELL_PART_OF_MULTICOLUMN));
760 if (getAdditionalWidth(cell_info[row][column].cellno))
762 return rightLine(cell_info[row][column].cellno);
768 bool LyXTabular::isLastRow(idx_type cell) const
770 return row_of_cell(cell) == rows_ - 1;
774 int LyXTabular::getAdditionalHeight(row_type row) const
776 if (!row || row >= rows_)
782 for (col_type column = 0; column < columns_ && bottom; ++column) {
783 switch (cell_info[row - 1][column].multicolumn) {
784 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
785 bottom = cell_info[row - 1][column].bottom_line;
787 case LyXTabular::CELL_NORMAL:
788 bottom = row_info[row - 1].bottom_line;
791 for (col_type column = 0; column < columns_ && top; ++column) {
792 switch (cell_info[row][column].multicolumn) {
793 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
794 top = cell_info[row][column].top_line;
796 case LyXTabular::CELL_NORMAL:
797 top = row_info[row].top_line;
800 int const interline_space = row_info[row - 1].interline_space_default ?
802 row_info[row - 1].interline_space.inPixels(width_of_tabular);
804 return interline_space + WIDTH_OF_LINE;
805 return interline_space;
809 int LyXTabular::getAdditionalWidth(idx_type cell) const
811 // internally already set in setWidthOfCell
812 // used to get it back in text.C
813 col_type const col = right_column_of_cell(cell);
814 row_type const row = row_of_cell(cell);
815 if (col < columns_ - 1 && rightLine(cell) &&
816 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
818 return WIDTH_OF_LINE;
824 // returns the maximum over all rows
825 int LyXTabular::getWidthOfColumn(idx_type cell) const
827 col_type const column1 = column_of_cell(cell);
828 col_type const column2 = right_column_of_cell(cell);
830 for (col_type i = column1; i <= column2; ++i)
831 result += column_info[i].width_of_column;
836 int LyXTabular::getWidthOfTabular() const
838 return width_of_tabular;
842 // returns true if a complete update is necessary, otherwise false
843 bool LyXTabular::setWidthOfMulticolCell(idx_type cell, int new_width)
845 if (!isMultiColumn(cell))
848 row_type const row = row_of_cell(cell);
849 col_type const column1 = column_of_cell(cell);
850 col_type const column2 = right_column_of_cell(cell);
851 int const old_val = cell_info[row][column2].width_of_cell;
853 // first set columns to 0 so we can calculate the right width
854 for (col_type i = column1; i <= column2; ++i) {
855 cell_info[row][i].width_of_cell = 0;
857 // set the width to MAX_WIDTH until width > 0
858 int width = new_width + 2 * WIDTH_OF_LINE;
859 col_type i = column1;
860 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
861 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
862 width -= column_info[i].width_of_column;
865 cell_info[row][i].width_of_cell = width;
867 if (old_val != cell_info[row][column2].width_of_cell) {
868 // in this case we have to recalculate all multicolumn cells which
869 // have this column as one of theirs but not as last one
870 calculate_width_of_column_NMC(i);
871 recalculateMulticolumnsOfColumn(i);
872 calculate_width_of_column(i);
878 void LyXTabular::recalculateMulticolumnsOfColumn(col_type column)
880 // the last column does not have to be recalculated because all
881 // multicolumns will have here there last multicolumn cell which
882 // always will have the whole rest of the width of the cell.
883 if (columns_ < 2 || column > (columns_ - 2))
885 for (row_type row = 0; row < rows_; ++row) {
886 int mc = cell_info[row][column].multicolumn;
887 int nmc = cell_info[row][column+1].multicolumn;
888 // we only have to update multicolumns which do not have this
889 // column as their last column!
890 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
891 (mc == CELL_PART_OF_MULTICOLUMN &&
892 nmc == CELL_PART_OF_MULTICOLUMN))
894 idx_type const cellno = cell_info[row][column].cellno;
895 setWidthOfMulticolCell(cellno,
896 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
902 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
904 row_type const row = row_of_cell(cell);
905 col_type const column1 = column_of_cell(cell);
910 if (rightLine(cell_info[row][column1].cellno, true) &&
911 column1 < columns_ - 1 &&
912 leftLine(cell_info[row][column1+1].cellno, true))
914 add_width = WIDTH_OF_LINE;
917 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
920 if (isMultiColumnReal(cell)) {
921 tmp = setWidthOfMulticolCell(cell, new_width);
923 width = new_width + 2 * WIDTH_OF_LINE + add_width;
924 cell_info[row][column1].width_of_cell = width;
925 tmp = calculate_width_of_column_NMC(column1);
927 recalculateMulticolumnsOfColumn(column1);
930 for (col_type i = 0; i < columns_; ++i)
931 calculate_width_of_column(i);
932 calculate_width_of_tabular();
937 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
940 if (!isMultiColumn(cell) || onlycolumn)
941 column_info[column_of_cell(cell)].alignment = align;
943 cellinfo_of_cell(cell).alignment = align;
947 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
950 if (!isMultiColumn(cell) || onlycolumn)
951 column_info[column_of_cell(cell)].valignment = align;
953 cellinfo_of_cell(cell).valignment = align;
960 * Allow line and paragraph breaks for fixed width cells or disallow them,
961 * merge cell paragraphs and reset layout to standard for variable width
964 void toggleFixedWidth(LCursor & cur, InsetText * inset, bool fixedWidth)
966 inset->setAutoBreakRows(fixedWidth);
970 // merge all paragraphs to one
971 BufferParams const & bp = cur.bv().buffer()->params();
972 while (inset->paragraphs().size() > 1)
973 mergeParagraph(bp, inset->paragraphs(), 0);
977 // undo information has already been recorded
978 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
979 bp.getLyXTextClass().defaultLayoutName());
986 void LyXTabular::setColumnPWidth(LCursor & cur, idx_type cell,
987 LyXLength const & width)
989 col_type const j = column_of_cell(cell);
991 column_info[j].p_width = width;
992 for (row_type i = 0; i < rows_; ++i) {
993 idx_type const cell = getCellNumber(i, j);
994 // because of multicolumns
995 toggleFixedWidth(cur, getCellInset(cell).get(),
996 !getPWidth(cell).zero());
998 // cur paragraph can become invalid after paragraphs were merged
999 if (cur.pit() > cur.lastpit())
1000 cur.pit() = cur.lastpit();
1001 // cur position can become invalid after newlines were removed
1002 if (cur.pos() > cur.lastpos())
1003 cur.pos() = cur.lastpos();
1007 bool LyXTabular::setMColumnPWidth(LCursor & cur, idx_type cell,
1008 LyXLength const & width)
1010 if (!isMultiColumn(cell))
1013 cellinfo_of_cell(cell).p_width = width;
1014 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1015 // cur paragraph can become invalid after paragraphs were merged
1016 if (cur.pit() > cur.lastpit())
1017 cur.pit() = cur.lastpit();
1018 // cur position can become invalid after newlines were removed
1019 if (cur.pos() > cur.lastpos())
1020 cur.pos() = cur.lastpos();
1025 void LyXTabular::setAlignSpecial(idx_type cell, docstring const & special,
1026 LyXTabular::Feature what)
1028 if (what == SET_SPECIAL_MULTI)
1029 cellinfo_of_cell(cell).align_special = special;
1031 column_info[column_of_cell(cell)].align_special = special;
1035 void LyXTabular::setAllLines(idx_type cell, bool line)
1037 setTopLine(cell, line);
1038 setBottomLine(cell, line);
1039 setRightLine(cell, line);
1040 setLeftLine(cell, line);
1044 void LyXTabular::setTopLine(idx_type cell, bool line, bool wholerow)
1046 row_type const row = row_of_cell(cell);
1047 if (wholerow || !isMultiColumn(cell))
1048 row_info[row].top_line = line;
1050 cellinfo_of_cell(cell).top_line = line;
1054 void LyXTabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1056 if (wholerow || !isMultiColumn(cell))
1057 row_info[row_of_cell(cell)].bottom_line = line;
1059 cellinfo_of_cell(cell).bottom_line = line;
1063 void LyXTabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1065 if (wholecolumn || !isMultiColumn(cell))
1066 column_info[column_of_cell(cell)].left_line = line;
1068 cellinfo_of_cell(cell).left_line = line;
1072 void LyXTabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1074 if (wholecolumn || !isMultiColumn(cell))
1075 column_info[right_column_of_cell(cell)].right_line = line;
1077 cellinfo_of_cell(cell).right_line = line;
1081 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
1083 if (!onlycolumn && isMultiColumn(cell))
1084 return cellinfo_of_cell(cell).alignment;
1085 return column_info[column_of_cell(cell)].alignment;
1089 LyXTabular::VAlignment
1090 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
1092 if (!onlycolumn && isMultiColumn(cell))
1093 return cellinfo_of_cell(cell).valignment;
1094 return column_info[column_of_cell(cell)].valignment;
1098 LyXLength const LyXTabular::getPWidth(idx_type cell) const
1100 if (isMultiColumn(cell))
1101 return cellinfo_of_cell(cell).p_width;
1102 return column_info[column_of_cell(cell)].p_width;
1106 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
1108 return column_info[column_of_cell(cell)].p_width;
1112 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
1114 if (isMultiColumn(cell))
1115 return cellinfo_of_cell(cell).p_width;
1120 docstring const LyXTabular::getAlignSpecial(idx_type cell, int what) const
1122 if (what == SET_SPECIAL_MULTI)
1123 return cellinfo_of_cell(cell).align_special;
1124 return column_info[column_of_cell(cell)].align_special;
1128 int LyXTabular::getWidthOfCell(idx_type cell) const
1130 row_type const row = row_of_cell(cell);
1131 col_type const column1 = column_of_cell(cell);
1132 col_type const column2 = right_column_of_cell(cell);
1134 for (col_type i = column1; i <= column2; ++i)
1135 result += cell_info[row][i].width_of_cell;
1140 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1144 switch (getAlignment(cell)) {
1145 case LYX_ALIGN_CENTER:
1146 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1148 case LYX_ALIGN_RIGHT:
1149 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1150 // + getAdditionalWidth(cell);
1153 // LYX_ALIGN_LEFT: nothing :-)
1157 // the LaTeX Way :-(
1163 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1165 return column_of_cell(cell) == 0;
1169 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1171 if (row > rows_ - 1)
1173 return cell_info[row][0].cellno;
1177 bool LyXTabular::isLastCellInRow(idx_type cell) const
1179 return right_column_of_cell(cell) == columns_ - 1;
1183 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1185 if (row > rows_ - 1)
1187 return cell_info[row][columns_-1].cellno;
1191 void LyXTabular::calculate_width_of_column(col_type column)
1194 for (row_type i = 0; i < rows_; ++i)
1195 maximum = max(cell_info[i][column].width_of_cell, maximum);
1196 column_info[column].width_of_column = maximum;
1201 // Calculate the columns regarding ONLY the normal cells and if this
1202 // column is inside a multicolumn cell then use it only if its the last
1203 // column of this multicolumn cell as this gives an added width to the
1204 // column, all the rest should be adapted!
1206 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1208 int const old_column_width = column_info[column].width_of_column;
1210 for (row_type i = 0; i < rows_; ++i) {
1211 idx_type cell = getCellNumber(i, column);
1212 bool ismulti = isMultiColumnReal(cell);
1213 if ((!ismulti || column == right_column_of_cell(cell)) &&
1214 cell_info[i][column].width_of_cell > max)
1216 max = cell_info[i][column].width_of_cell;
1219 column_info[column].width_of_column = max;
1220 return column_info[column].width_of_column != old_column_width;
1224 void LyXTabular::calculate_width_of_tabular()
1226 width_of_tabular = 0;
1227 for (col_type i = 0; i < columns_; ++i)
1228 width_of_tabular += column_info[i].width_of_column;
1232 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1234 if (cell >= numberofcells)
1238 return rowofcell[cell];
1242 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1244 if (cell >= numberofcells)
1245 return columns_ - 1;
1248 return columnofcell[cell];
1252 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1254 row_type const row = row_of_cell(cell);
1255 col_type column = column_of_cell(cell);
1256 while (column < columns_ - 1 &&
1257 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1263 void LyXTabular::write(Buffer const & buf, ostream & os) const
1267 << write_attribute("version", 3)
1268 << write_attribute("rows", rows_)
1269 << write_attribute("columns", columns_)
1271 // global longtable options
1273 << write_attribute("rotate", rotate)
1274 << write_attribute("booktabs", use_booktabs)
1275 << write_attribute("islongtable", is_long_tabular)
1276 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1277 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1278 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1279 << write_attribute("headTopDL", endhead.topDL)
1280 << write_attribute("headBottomDL", endhead.bottomDL)
1281 << write_attribute("footTopDL", endfoot.topDL)
1282 << write_attribute("footBottomDL", endfoot.bottomDL)
1283 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1284 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1285 << write_attribute("lastFootEmpty", endlastfoot.empty)
1287 for (col_type j = 0; j < columns_; ++j) {
1289 << write_attribute("alignment", column_info[j].alignment)
1290 << write_attribute("valignment", column_info[j].valignment)
1291 << write_attribute("leftline", column_info[j].left_line)
1292 << write_attribute("rightline", column_info[j].right_line)
1293 << write_attribute("width", column_info[j].p_width.asString())
1294 << write_attribute("special", column_info[j].align_special)
1297 for (row_type i = 0; i < rows_; ++i) {
1299 << write_attribute("topline", row_info[i].top_line)
1300 << write_attribute("bottomline", row_info[i].bottom_line);
1301 static const string def("default");
1302 if (row_info[i].top_space_default)
1303 os << write_attribute("topspace", def);
1305 os << write_attribute("topspace", row_info[i].top_space);
1306 if (row_info[i].bottom_space_default)
1307 os << write_attribute("bottomspace", def);
1309 os << write_attribute("bottomspace", row_info[i].bottom_space);
1310 if (row_info[i].interline_space_default)
1311 os << write_attribute("interlinespace", def);
1313 os << write_attribute("interlinespace", row_info[i].interline_space);
1314 os << write_attribute("endhead", row_info[i].endhead)
1315 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1316 << write_attribute("endfoot", row_info[i].endfoot)
1317 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1318 << write_attribute("newpage", row_info[i].newpage)
1320 for (col_type j = 0; j < columns_; ++j) {
1322 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1323 << write_attribute("alignment", cell_info[i][j].alignment)
1324 << write_attribute("valignment", cell_info[i][j].valignment)
1325 << write_attribute("topline", cell_info[i][j].top_line)
1326 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1327 << write_attribute("leftline", cell_info[i][j].left_line)
1328 << write_attribute("rightline", cell_info[i][j].right_line)
1329 << write_attribute("rotate", cell_info[i][j].rotate)
1330 << write_attribute("usebox", cell_info[i][j].usebox)
1331 << write_attribute("width", cell_info[i][j].p_width)
1332 << write_attribute("special", cell_info[i][j].align_special)
1334 os << "\\begin_inset ";
1335 cell_info[i][j].inset->write(buf, os);
1336 os << "\n\\end_inset\n"
1341 os << "</lyxtabular>\n";
1345 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1348 istream & is = lex.getStream();
1350 l_getline(is, line);
1351 if (!prefixIs(line, "<lyxtabular ")
1352 && !prefixIs(line, "<LyXTabular ")) {
1353 BOOST_ASSERT(false);
1358 if (!getTokenValue(line, "version", version))
1360 BOOST_ASSERT(version >= 2);
1363 if (!getTokenValue(line, "rows", rows_arg))
1366 if (!getTokenValue(line, "columns", columns_arg))
1368 init(buf.params(), rows_arg, columns_arg);
1369 l_getline(is, line);
1370 if (!prefixIs(line, "<features")) {
1371 lyxerr << "Wrong tabular format (expected <features ...> got"
1372 << line << ')' << endl;
1375 getTokenValue(line, "rotate", rotate);
1376 getTokenValue(line, "booktabs", use_booktabs);
1377 getTokenValue(line, "islongtable", is_long_tabular);
1378 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1379 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1380 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1381 getTokenValue(line, "headTopDL", endhead.topDL);
1382 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1383 getTokenValue(line, "footTopDL", endfoot.topDL);
1384 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1385 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1386 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1387 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1389 for (col_type j = 0; j < columns_; ++j) {
1391 if (!prefixIs(line,"<column")) {
1392 lyxerr << "Wrong tabular format (expected <column ...> got"
1393 << line << ')' << endl;
1396 getTokenValue(line, "alignment", column_info[j].alignment);
1397 getTokenValue(line, "valignment", column_info[j].valignment);
1398 getTokenValue(line, "leftline", column_info[j].left_line);
1399 getTokenValue(line, "rightline", column_info[j].right_line);
1400 getTokenValue(line, "width", column_info[j].p_width);
1401 getTokenValue(line, "special", column_info[j].align_special);
1404 for (row_type i = 0; i < rows_; ++i) {
1405 l_getline(is, line);
1406 if (!prefixIs(line, "<row")) {
1407 lyxerr << "Wrong tabular format (expected <row ...> got"
1408 << line << ')' << endl;
1411 getTokenValue(line, "topline", row_info[i].top_line);
1412 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1413 getTokenValue(line, "topspace", row_info[i].top_space,
1414 row_info[i].top_space_default);
1415 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1416 row_info[i].bottom_space_default);
1417 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1418 row_info[i].interline_space_default);
1419 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1420 getTokenValue(line, "endhead", row_info[i].endhead);
1421 getTokenValue(line, "endfoot", row_info[i].endfoot);
1422 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1423 getTokenValue(line, "newpage", row_info[i].newpage);
1424 for (col_type j = 0; j < columns_; ++j) {
1425 l_getline(is, line);
1426 if (!prefixIs(line, "<cell")) {
1427 lyxerr << "Wrong tabular format (expected <cell ...> got"
1428 << line << ')' << endl;
1431 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1432 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1433 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1434 getTokenValue(line, "topline", cell_info[i][j].top_line);
1435 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1436 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1437 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1438 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1439 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1440 getTokenValue(line, "width", cell_info[i][j].p_width);
1441 getTokenValue(line, "special", cell_info[i][j].align_special);
1442 l_getline(is, line);
1443 if (prefixIs(line, "\\begin_inset")) {
1444 cell_info[i][j].inset->read(buf, lex);
1445 l_getline(is, line);
1447 if (!prefixIs(line, "</cell>")) {
1448 lyxerr << "Wrong tabular format (expected </cell> got"
1449 << line << ')' << endl;
1453 l_getline(is, line);
1454 if (!prefixIs(line, "</row>")) {
1455 lyxerr << "Wrong tabular format (expected </row> got"
1456 << line << ')' << endl;
1460 while (!prefixIs(line, "</lyxtabular>")) {
1461 l_getline(is, line);
1463 set_row_column_number_info();
1467 bool LyXTabular::isMultiColumn(idx_type cell) const
1469 return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1473 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1475 return column_of_cell(cell) != right_column_of_cell(cell) &&
1476 cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1480 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1482 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1486 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1489 cellstruct & cs = cellinfo_of_cell(cell);
1490 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1491 cs.alignment = column_info[column_of_cell(cell)].alignment;
1492 cs.top_line = row_info[row_of_cell(cell)].top_line;
1493 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1494 cs.left_line = column_info[column_of_cell(cell)].left_line;
1495 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1496 for (idx_type i = 1; i < number; ++i) {
1497 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1498 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1499 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1502 set_row_column_number_info();
1506 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1508 row_type const row = row_of_cell(cell);
1509 col_type column = column_of_cell(cell);
1510 idx_type result = 1;
1512 while (column < columns_ &&
1513 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1522 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1524 row_type const row = row_of_cell(cell);
1525 col_type column = column_of_cell(cell);
1527 idx_type result = 0;
1529 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1530 cell_info[row][column].multicolumn = CELL_NORMAL;
1532 while (column < columns_ &&
1533 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1535 cell_info[row][column].multicolumn = CELL_NORMAL;
1540 set_row_column_number_info();
1545 void LyXTabular::setBookTabs(bool what)
1547 use_booktabs = what;
1551 bool LyXTabular::useBookTabs() const
1553 return use_booktabs;
1557 void LyXTabular::setLongTabular(bool what)
1559 is_long_tabular = what;
1563 bool LyXTabular::isLongTabular() const
1565 return is_long_tabular;
1569 void LyXTabular::setRotateTabular(bool flag)
1575 bool LyXTabular::getRotateTabular() const
1581 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1583 cellinfo_of_cell(cell).rotate = flag;
1587 bool LyXTabular::getRotateCell(idx_type cell) const
1589 return cellinfo_of_cell(cell).rotate;
1593 bool LyXTabular::needRotating() const
1597 for (row_type i = 0; i < rows_; ++i)
1598 for (col_type j = 0; j < columns_; ++j)
1599 if (cell_info[i][j].rotate)
1605 bool LyXTabular::isLastCell(idx_type cell) const
1607 if (cell + 1 < numberofcells)
1613 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1615 if (row_of_cell(cell) > 0)
1616 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1621 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1623 if (row_of_cell(cell) + 1 < rows_)
1624 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1629 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1631 if (row_of_cell(cell) == 0)
1633 if (!isMultiColumn(cell))
1634 return getCellAbove(cell);
1635 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1639 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1641 if (row_of_cell(cell) + 1 >= rows_)
1643 if (!isMultiColumn(cell))
1644 return getCellBelow(cell);
1645 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1649 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1650 col_type column) const
1652 BOOST_ASSERT(column != npos && column < columns_ &&
1653 row != npos && row < rows_);
1654 return cell_info[row][column].cellno;
1658 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1660 cellinfo_of_cell(cell).usebox = type;
1664 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1666 if (column_info[column_of_cell(cell)].p_width.zero() &&
1667 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1669 if (cellinfo_of_cell(cell).usebox > 1)
1670 return cellinfo_of_cell(cell).usebox;
1671 return useParbox(cell);
1676 // This are functions used for the longtable support
1678 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1684 row_info[row].endfirsthead = flag;
1688 row_info[row].endhead = flag;
1693 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1696 hd.set = haveLTHead();
1697 return row_info[row].endhead;
1701 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1704 hd.set = haveLTFirstHead();
1705 return row_info[row].endfirsthead;
1709 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1715 row_info[row].endlastfoot = flag;
1719 row_info[row].endfoot = flag;
1724 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1727 fd.set = haveLTFoot();
1728 return row_info[row].endfoot;
1732 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1735 fd.set = haveLTLastFoot();
1736 return row_info[row].endlastfoot;
1740 void LyXTabular::setLTNewPage(row_type row, bool what)
1742 row_info[row].newpage = what;
1746 bool LyXTabular::getLTNewPage(row_type row) const
1748 return row_info[row].newpage;
1752 bool LyXTabular::haveLTHead() const
1754 for (row_type i = 0; i < rows_; ++i)
1755 if (row_info[i].endhead)
1761 bool LyXTabular::haveLTFirstHead() const
1763 if (endfirsthead.empty)
1765 for (row_type i = 0; i < rows_; ++i)
1766 if (row_info[i].endfirsthead)
1772 bool LyXTabular::haveLTFoot() const
1774 for (row_type i = 0; i < rows_; ++i)
1775 if (row_info[i].endfoot)
1781 bool LyXTabular::haveLTLastFoot() const
1783 if (endlastfoot.empty)
1785 for (row_type i = 0; i < rows_; ++i)
1786 if (row_info[i].endlastfoot)
1792 // end longtable support functions
1794 void LyXTabular::setAscentOfRow(row_type row, int height)
1796 if (row >= rows_ || row_info[row].ascent_of_row == height)
1798 row_info[row].ascent_of_row = height;
1802 void LyXTabular::setDescentOfRow(row_type row, int height)
1804 if (row >= rows_ || row_info[row].descent_of_row == height)
1806 row_info[row].descent_of_row = height;
1810 int LyXTabular::getAscentOfRow(row_type row) const
1814 return row_info[row].ascent_of_row;
1818 int LyXTabular::getDescentOfRow(row_type row) const
1820 BOOST_ASSERT(row < rows_);
1821 return row_info[row].descent_of_row;
1825 int LyXTabular::getHeightOfTabular() const
1828 for (row_type row = 0; row < rows_; ++row)
1829 height += getAscentOfRow(row) + getDescentOfRow(row) +
1830 getAdditionalHeight(row);
1835 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1837 BOOST_ASSERT(row < rows_);
1838 BOOST_ASSERT(column < columns_);
1839 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1843 int LyXTabular::TeXTopHLine(odocstream & os, row_type row) const
1845 // FIXME: assert or return 0 as in TeXBottomHLine()?
1846 BOOST_ASSERT(row != npos);
1847 BOOST_ASSERT(row < rows_);
1849 idx_type const fcell = getFirstCellInRow(row);
1850 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1853 for (idx_type i = fcell; i < n; ++i) {
1857 if (use_booktabs && row == 0) {
1859 } else if (tmp == n - fcell) {
1860 os << (use_booktabs ? "\\midrule " : "\\hline ");
1862 for (idx_type i = fcell; i < n; ++i) {
1864 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1865 << column_of_cell(i) + 1
1867 << right_column_of_cell(i) + 1
1879 int LyXTabular::TeXBottomHLine(odocstream & os, row_type row) const
1881 // FIXME: return 0 or assert as in TeXTopHLine()?
1882 if (row == npos || row >= rows_)
1885 idx_type const fcell = getFirstCellInRow(row);
1886 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1889 for (idx_type i = fcell; i < n; ++i) {
1893 if (use_booktabs && row == rows_ - 1) {
1894 os << "\\bottomrule";
1895 } else if (tmp == n - fcell) {
1896 os << (use_booktabs ? "\\midrule" : "\\hline");
1898 for (idx_type i = fcell; i < n; ++i) {
1899 if (bottomLine(i)) {
1900 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1901 << column_of_cell(i) + 1
1903 << right_column_of_cell(i) + 1
1915 int LyXTabular::TeXCellPreamble(odocstream & os, idx_type cell) const
1919 if (getRotateCell(cell)) {
1920 os << "\\begin{sideways}\n";
1923 if (isMultiColumn(cell)) {
1924 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1925 if (!cellinfo_of_cell(cell).align_special.empty()) {
1926 os << cellinfo_of_cell(cell).align_special << "}{";
1928 if (leftLine(cell) &&
1929 (isFirstCellInRow(cell) ||
1930 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1931 !rightLine(cell - 1, true))))
1935 if (!getPWidth(cell).zero()) {
1936 switch (getVAlignment(cell)) {
1937 case LYX_VALIGN_TOP:
1940 case LYX_VALIGN_MIDDLE:
1943 case LYX_VALIGN_BOTTOM:
1948 << from_ascii(getPWidth(cell).asLatexString())
1951 switch (getAlignment(cell)) {
1952 case LYX_ALIGN_LEFT:
1955 case LYX_ALIGN_RIGHT:
1963 if (rightLine(cell))
1965 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1971 if (getUsebox(cell) == BOX_PARBOX) {
1973 switch (getVAlignment(cell)) {
1974 case LYX_VALIGN_TOP:
1977 case LYX_VALIGN_MIDDLE:
1980 case LYX_VALIGN_BOTTOM:
1984 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1986 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1987 os << "\\begin{minipage}[";
1988 switch (getVAlignment(cell)) {
1989 case LYX_VALIGN_TOP:
1992 case LYX_VALIGN_MIDDLE:
1995 case LYX_VALIGN_BOTTOM:
1999 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2007 int LyXTabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2012 if (getUsebox(cell) == BOX_PARBOX)
2014 else if (getUsebox(cell) == BOX_MINIPAGE) {
2015 os << "%\n\\end{minipage}";
2018 if (isMultiColumn(cell)) {
2021 if (getRotateCell(cell)) {
2022 os << "%\n\\end{sideways}";
2029 int LyXTabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2030 OutputParams const & runparams) const
2032 if (!is_long_tabular)
2036 // output header info
2038 if (endhead.topDL) {
2042 for (row_type i = 0; i < rows_; ++i) {
2043 if (row_info[i].endhead) {
2044 ret += TeXRow(os, i, buf, runparams);
2047 if (endhead.bottomDL) {
2051 os << "\\endhead\n";
2053 if (endfirsthead.empty) {
2054 os << "\\endfirsthead\n";
2058 // output firstheader info
2059 if (haveLTFirstHead()) {
2060 if (endfirsthead.topDL) {
2064 for (row_type i = 0; i < rows_; ++i) {
2065 if (row_info[i].endfirsthead) {
2066 ret += TeXRow(os, i, buf, runparams);
2069 if (endfirsthead.bottomDL) {
2073 os << "\\endfirsthead\n";
2076 // output footer info
2078 if (endfoot.topDL) {
2082 for (row_type i = 0; i < rows_; ++i) {
2083 if (row_info[i].endfoot) {
2084 ret += TeXRow(os, i, buf, runparams);
2087 if (endfoot.bottomDL) {
2091 os << "\\endfoot\n";
2093 if (endlastfoot.empty) {
2094 os << "\\endlastfoot\n";
2098 // output lastfooter info
2099 if (haveLTLastFoot()) {
2100 if (endlastfoot.topDL) {
2104 for (row_type i = 0; i < rows_; ++i) {
2105 if (row_info[i].endlastfoot) {
2106 ret += TeXRow(os, i, buf, runparams);
2109 if (endlastfoot.bottomDL) {
2113 os << "\\endlastfoot\n";
2120 bool LyXTabular::isValidRow(row_type row) const
2122 if (!is_long_tabular)
2124 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2125 !row_info[row].endfoot && !row_info[row].endlastfoot;
2129 int LyXTabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2130 OutputParams const & runparams) const
2132 idx_type cell = getCellNumber(i, 0);
2133 int ret = TeXTopHLine(os, i);
2134 if (row_info[i].top_space_default) {
2136 os << "\\addlinespace\n";
2138 os << "\\noalign{\\vskip\\doublerulesep}\n";
2139 } else if(!row_info[i].top_space.zero()) {
2141 os << "\\addlinespace["
2142 << from_ascii(row_info[i].top_space.asLatexString())
2145 os << "\\noalign{\\vskip"
2146 << from_ascii(row_info[i].top_space.asLatexString())
2152 for (col_type j = 0; j < columns_; ++j) {
2153 if (isPartOfMultiColumn(i, j))
2155 ret += TeXCellPreamble(os, cell);
2156 shared_ptr<InsetText> inset = getCellInset(cell);
2158 Paragraph const & par = inset->paragraphs().front();
2159 bool rtl = par.isRightToLeftPar(buf.params())
2161 && getPWidth(cell).zero();
2165 ret += inset->latex(buf, os, runparams);
2169 ret += TeXCellPostamble(os, cell);
2170 if (!isLastCellInRow(cell)) { // not last cell in row
2175 os << "\\tabularnewline";
2176 if (row_info[i].bottom_space_default) {
2178 os << "\\addlinespace";
2180 os << "[\\doublerulesep]";
2181 } else if (!row_info[i].bottom_space.zero()) {
2183 os << "\\addlinespace";
2185 << from_ascii(row_info[i].bottom_space.asLatexString())
2190 ret += TeXBottomHLine(os, i);
2191 if (row_info[i].interline_space_default) {
2193 os << "\\addlinespace\n";
2195 os << "\\noalign{\\vskip\\doublerulesep}\n";
2196 } else if (!row_info[i].interline_space.zero()) {
2198 os << "\\addlinespace["
2199 << from_ascii(row_info[i].interline_space.asLatexString())
2202 os << "\\noalign{\\vskip"
2203 << from_ascii(row_info[i].interline_space.asLatexString())
2211 int LyXTabular::latex(Buffer const & buf, odocstream & os,
2212 OutputParams const & runparams) const
2216 //+---------------------------------------------------------------------
2217 //+ first the opening preamble +
2218 //+---------------------------------------------------------------------
2221 os << "\\begin{sideways}\n";
2224 if (is_long_tabular)
2225 os << "\\begin{longtable}{";
2227 os << "\\begin{tabular}{";
2228 for (col_type i = 0; i < columns_; ++i) {
2229 if (!column_info[i].align_special.empty()) {
2230 os << column_info[i].align_special;
2232 if (!use_booktabs && column_info[i].left_line)
2234 if (!column_info[i].p_width.zero()) {
2235 switch (column_info[i].alignment) {
2236 case LYX_ALIGN_LEFT:
2237 os << ">{\\raggedright}";
2239 case LYX_ALIGN_RIGHT:
2240 os << ">{\\raggedleft}";
2242 case LYX_ALIGN_CENTER:
2243 os << ">{\\centering}";
2245 case LYX_ALIGN_NONE:
2246 case LYX_ALIGN_BLOCK:
2247 case LYX_ALIGN_LAYOUT:
2248 case LYX_ALIGN_SPECIAL:
2252 switch (column_info[i].valignment) {
2253 case LYX_VALIGN_TOP:
2256 case LYX_VALIGN_MIDDLE:
2259 case LYX_VALIGN_BOTTOM:
2264 << from_ascii(column_info[i].p_width.asLatexString())
2267 switch (column_info[i].alignment) {
2268 case LYX_ALIGN_LEFT:
2271 case LYX_ALIGN_RIGHT:
2279 if (!use_booktabs && column_info[i].right_line)
2286 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2288 //+---------------------------------------------------------------------
2289 //+ the single row and columns (cells) +
2290 //+---------------------------------------------------------------------
2292 for (row_type i = 0; i < rows_; ++i) {
2293 if (isValidRow(i)) {
2294 ret += TeXRow(os, i, buf, runparams);
2295 if (is_long_tabular && row_info[i].newpage) {
2296 os << "\\newpage\n";
2302 //+---------------------------------------------------------------------
2303 //+ the closing of the tabular +
2304 //+---------------------------------------------------------------------
2306 if (is_long_tabular)
2307 os << "\\end{longtable}";
2309 os << "\\end{tabular}";
2311 os << "\n\\end{sideways}";
2319 int LyXTabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2320 OutputParams const & runparams) const
2323 idx_type cell = getFirstCellInRow(row);
2326 for (col_type j = 0; j < columns_; ++j) {
2327 if (isPartOfMultiColumn(row, j))
2330 os << "<entry align=\"";
2331 switch (getAlignment(cell)) {
2332 case LYX_ALIGN_LEFT:
2335 case LYX_ALIGN_RIGHT:
2343 os << "\" valign=\"";
2344 switch (getVAlignment(cell)) {
2345 case LYX_VALIGN_TOP:
2348 case LYX_VALIGN_BOTTOM:
2351 case LYX_VALIGN_MIDDLE:
2356 if (isMultiColumn(cell)) {
2357 os << " namest=\"col" << j << "\" ";
2358 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2362 ret += getCellInset(cell)->docbook(buf, os, runparams);
2371 int LyXTabular::docbook(Buffer const & buf, odocstream & os,
2372 OutputParams const & runparams) const
2376 //+---------------------------------------------------------------------
2377 //+ first the opening preamble +
2378 //+---------------------------------------------------------------------
2380 os << "<tgroup cols=\"" << columns_
2381 << "\" colsep=\"1\" rowsep=\"1\">\n";
2383 for (col_type i = 0; i < columns_; ++i) {
2384 os << "<colspec colname=\"col" << i << "\" align=\"";
2385 switch (column_info[i].alignment) {
2386 case LYX_ALIGN_LEFT:
2389 case LYX_ALIGN_RIGHT:
2397 if (runparams.flavor == OutputParams::XML)
2403 //+---------------------------------------------------------------------
2404 //+ Long Tabular case +
2405 //+---------------------------------------------------------------------
2407 // output header info
2408 if (haveLTHead() || haveLTFirstHead()) {
2411 for (row_type i = 0; i < rows_; ++i) {
2412 if (row_info[i].endhead || row_info[i].endfirsthead) {
2413 ret += docbookRow(buf, os, i, runparams);
2419 // output footer info
2420 if (haveLTFoot() || haveLTLastFoot()) {
2423 for (row_type i = 0; i < rows_; ++i) {
2424 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2425 ret += docbookRow(buf, os, i, runparams);
2432 //+---------------------------------------------------------------------
2433 //+ the single row and columns (cells) +
2434 //+---------------------------------------------------------------------
2438 for (row_type i = 0; i < rows_; ++i) {
2439 if (isValidRow(i)) {
2440 ret += docbookRow(buf, os, i, runparams);
2445 //+---------------------------------------------------------------------
2446 //+ the closing of the tabular +
2447 //+---------------------------------------------------------------------
2456 int LyXTabular::plaintextTopHLine(odocstream & os, row_type row,
2457 vector<unsigned int> const & clen) const
2459 idx_type const fcell = getFirstCellInRow(row);
2460 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2463 for (idx_type i = fcell; i < n; ++i) {
2473 for (idx_type i = fcell; i < n; ++i) {
2484 col_type column = column_of_cell(i);
2485 int len = clen[column];
2486 while (column < columns_ - 1
2487 && isPartOfMultiColumn(row, ++column))
2488 len += clen[column] + 4;
2489 os << docstring(len, ch);
2504 int LyXTabular::plaintextBottomHLine(odocstream & os, row_type row,
2505 vector<unsigned int> const & clen) const
2507 idx_type const fcell = getFirstCellInRow(row);
2508 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2511 for (idx_type i = fcell; i < n; ++i) {
2512 if (bottomLine(i)) {
2521 for (idx_type i = fcell; i < n; ++i) {
2522 if (bottomLine(i)) {
2532 col_type column = column_of_cell(i);
2533 int len = clen[column];
2534 while (column < columns_ -1
2535 && isPartOfMultiColumn(row, ++column))
2536 len += clen[column] + 4;
2537 os << docstring(len, ch);
2538 if (bottomLine(i)) {
2552 int LyXTabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2553 OutputParams const & runparams,
2554 idx_type cell, row_type row, col_type column,
2555 vector<unsigned int> const & clen,
2556 bool onlydata) const
2558 odocstringstream sstr;
2559 int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2571 unsigned int len1 = sstr.str().length();
2572 unsigned int len2 = clen[column];
2573 while (column < columns_ -1
2574 && isPartOfMultiColumn(row, ++column))
2575 len2 += clen[column] + 4;
2578 switch (getAlignment(cell)) {
2580 case LYX_ALIGN_LEFT:
2583 case LYX_ALIGN_RIGHT:
2587 case LYX_ALIGN_CENTER:
2593 os << docstring(len1, ' ') << sstr.str()
2594 << docstring(len2, ' ');
2596 if (rightLine(cell))
2605 int LyXTabular::plaintext(Buffer const & buf, odocstream & os,
2606 OutputParams const & runparams,
2608 bool onlydata, unsigned char delim) const
2612 // first calculate the width of the single columns
2613 vector<unsigned int> clen(columns_);
2616 // first all non (real) multicolumn cells!
2617 for (col_type j = 0; j < columns_; ++j) {
2619 for (row_type i = 0; i < rows_; ++i) {
2620 idx_type cell = getCellNumber(i, j);
2621 if (isMultiColumnReal(cell))
2623 odocstringstream sstr;
2624 getCellInset(cell)->plaintext(buf, sstr, runparams);
2625 if (clen[j] < sstr.str().length())
2626 clen[j] = sstr.str().length();
2629 // then all (real) multicolumn cells!
2630 for (col_type j = 0; j < columns_; ++j) {
2631 for (row_type i = 0; i < rows_; ++i) {
2632 idx_type cell = getCellNumber(i, j);
2633 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2635 odocstringstream sstr;
2636 getCellInset(cell)->plaintext(buf, sstr, runparams);
2637 int len = int(sstr.str().length());
2638 idx_type const n = cells_in_multicolumn(cell);
2639 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2641 if (len > int(clen[j + n - 1]))
2642 clen[j + n - 1] = len;
2647 for (row_type i = 0; i < rows_; ++i) {
2648 if (!onlydata && plaintextTopHLine(os, i, clen))
2649 os << docstring(depth * 2, ' ');
2650 for (col_type j = 0; j < columns_; ++j) {
2651 if (isPartOfMultiColumn(i, j))
2653 if (onlydata && j > 0)
2655 ret += plaintextPrintCell(buf, os, runparams,
2656 cell, i, j, clen, onlydata);
2661 os << docstring(depth * 2, ' ');
2662 if (plaintextBottomHLine(os, i, clen))
2663 os << docstring(depth * 2, ' ');
2670 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2672 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2676 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2677 col_type column) const
2679 return cell_info[row][column].inset;
2683 void LyXTabular::setCellInset(row_type row, col_type column,
2684 shared_ptr<InsetText> ins) const
2686 cell_info[row][column].inset = ins;
2690 LyXTabular::idx_type
2691 LyXTabular::getCellFromInset(InsetBase const * inset) const
2693 // is this inset part of the tabular?
2695 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2696 BOOST_ASSERT(false);
2699 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2700 if (getCellInset(cell).get() == inset) {
2701 lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2702 << "cell=" << cell << endl;
2706 // We should have found a cell at this point
2707 lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2708 << inset << " not found!" << endl;
2709 BOOST_ASSERT(false);
2715 void LyXTabular::validate(LaTeXFeatures & features) const
2717 features.require("NeedTabularnewline");
2719 features.require("booktabs");
2720 if (isLongTabular())
2721 features.require("longtable");
2723 features.require("rotating");
2724 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2725 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2726 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2727 features.require("array");
2728 getCellInset(cell)->validate(features);
2733 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2735 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2736 ParagraphList::const_iterator cit = parlist.begin();
2737 ParagraphList::const_iterator end = parlist.end();
2739 for (; cit != end; ++cit)
2740 for (int i = 0; i < cit->size(); ++i)
2741 if (cit->isNewline(i))