2 * \file InsetTabular.cpp
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 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
73 using boost::shared_ptr;
78 using cap::dirtyTabularStack;
79 using cap::tabularStackDirty;
81 using graphics::PreviewLoader;
83 using frontend::Painter;
84 using frontend::Clipboard;
86 namespace Alert = frontend::Alert;
91 int const ADD_TO_HEIGHT = 2; // in cell
92 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
93 int const default_line_space = 10; // ?
94 int const WIDTH_OF_LINE = 5; // space between double lines
98 boost::scoped_ptr<Tabular> paste_tabular;
101 struct TabularFeature {
102 Tabular::Feature action;
108 TabularFeature tabularFeature[] =
110 { Tabular::APPEND_ROW, "append-row", false },
111 { Tabular::APPEND_COLUMN, "append-column", false },
112 { Tabular::DELETE_ROW, "delete-row", false },
113 { Tabular::DELETE_COLUMN, "delete-column", false },
114 { Tabular::COPY_ROW, "copy-row", false },
115 { Tabular::COPY_COLUMN, "copy-column", false },
116 { Tabular::SET_LINE_TOP, "set-line-top", true },
117 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
118 { Tabular::SET_LINE_LEFT, "set-line-left", true },
119 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
120 //FIXME: get rid of those 4 TOGGLE actions in favor of the 4 above.
121 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
122 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
123 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
124 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
125 { Tabular::ALIGN_LEFT, "align-left", false },
126 { Tabular::ALIGN_RIGHT, "align-right", false },
127 { Tabular::ALIGN_CENTER, "align-center", false },
128 { Tabular::ALIGN_BLOCK, "align-block", false },
129 { Tabular::VALIGN_TOP, "valign-top", false },
130 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
131 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
132 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
133 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
134 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
135 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
136 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
137 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
138 { Tabular::MULTICOLUMN, "multicolumn", false },
139 { Tabular::MULTIROW, "multirow", false },
140 { Tabular::SET_ALL_LINES, "set-all-lines", false },
141 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
142 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
143 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
144 { Tabular::SET_PWIDTH, "set-pwidth", true },
145 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
146 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
147 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
148 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
149 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
150 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
151 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
152 { Tabular::SET_USEBOX, "set-usebox", true },
153 { Tabular::SET_LTHEAD, "set-lthead", true },
154 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
155 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
156 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
157 { Tabular::SET_LTFOOT, "set-ltfoot", true },
158 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
159 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
160 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
161 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
162 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
163 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
164 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
165 { Tabular::SET_SPECIAL_MULTIROW, "set-special-multirow", false },
166 { Tabular::SET_BOOKTABS, "set-booktabs", false },
167 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
168 { Tabular::SET_TOP_SPACE, "set-top-space", true },
169 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
170 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
171 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
172 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
173 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
174 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
175 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
176 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
177 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
178 { Tabular::LAST_ACTION, "", false }
183 string const write_attribute(string const & name, T const & t)
185 string const s = tostr(t);
186 return s.empty() ? s : " " + name + "=\"" + s + "\"";
190 string const write_attribute(string const & name, string const & t)
192 return t.empty() ? t : " " + name + "=\"" + t + "\"";
197 string const write_attribute(string const & name, docstring const & t)
199 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
204 string const write_attribute(string const & name, bool const & b)
206 // we write only true attribute values so we remove a bit of the
207 // file format bloat for tabulars.
208 return b ? write_attribute(name, convert<string>(b)) : string();
213 string const write_attribute(string const & name, int const & i)
215 // we write only true attribute values so we remove a bit of the
216 // file format bloat for tabulars.
217 return i ? write_attribute(name, convert<string>(i)) : string();
222 string const write_attribute(string const & name, Tabular::idx_type const & i)
224 // we write only true attribute values so we remove a bit of the
225 // file format bloat for tabulars.
226 return i ? write_attribute(name, convert<string>(i)) : string();
231 string const write_attribute(string const & name, Length const & value)
233 // we write only the value if we really have one same reson as above.
234 return value.zero() ? string() : write_attribute(name, value.asString());
238 string const tostr(LyXAlignment const & num)
243 case LYX_ALIGN_BLOCK:
247 case LYX_ALIGN_CENTER:
249 case LYX_ALIGN_RIGHT:
251 case LYX_ALIGN_LAYOUT:
253 case LYX_ALIGN_SPECIAL:
260 string const tostr(Tabular::HAlignment const & num)
263 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
265 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
267 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
274 string const tostr(Tabular::VAlignment const & num)
277 case Tabular::LYX_VALIGN_TOP:
279 case Tabular::LYX_VALIGN_MIDDLE:
281 case Tabular::LYX_VALIGN_BOTTOM:
288 string const tostr(Tabular::BoxType const & num)
291 case Tabular::BOX_NONE:
293 case Tabular::BOX_PARBOX:
295 case Tabular::BOX_MINIPAGE:
302 // I would have liked a fromstr template a lot better. (Lgb)
303 bool string2type(string const str, LyXAlignment & num)
306 num = LYX_ALIGN_NONE;
307 else if (str == "block")
308 num = LYX_ALIGN_BLOCK;
309 else if (str == "left")
310 num = LYX_ALIGN_LEFT;
311 else if (str == "center")
312 num = LYX_ALIGN_CENTER;
313 else if (str == "right")
314 num = LYX_ALIGN_RIGHT;
321 bool string2type(string const str, Tabular::HAlignment & num)
324 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
325 else if (str == "center" )
326 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
327 else if (str == "right")
328 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
335 bool string2type(string const str, Tabular::VAlignment & num)
338 num = Tabular::LYX_VALIGN_TOP;
339 else if (str == "middle" )
340 num = Tabular::LYX_VALIGN_MIDDLE;
341 else if (str == "bottom")
342 num = Tabular::LYX_VALIGN_BOTTOM;
349 bool string2type(string const str, Tabular::BoxType & num)
352 num = Tabular::BOX_NONE;
353 else if (str == "parbox")
354 num = Tabular::BOX_PARBOX;
355 else if (str == "minipage")
356 num = Tabular::BOX_MINIPAGE;
363 bool string2type(string const str, bool & num)
367 else if (str == "false")
375 bool getTokenValue(string const & str, char const * token, string & ret)
378 size_t token_length = strlen(token);
379 size_t pos = str.find(token);
381 if (pos == string::npos || pos + token_length + 1 >= str.length()
382 || str[pos + token_length] != '=')
384 pos += token_length + 1;
386 if (ch != '"' && ch != '\'') { // only read till next space
390 while (pos < str.length() - 1 && str[++pos] != ch)
397 bool getTokenValue(string const & str, char const * token, docstring & ret)
400 bool const success = getTokenValue(str, token, tmp);
401 ret = from_utf8(tmp);
406 bool getTokenValue(string const & str, char const * token, int & num)
410 if (!getTokenValue(str, token, tmp))
412 num = convert<int>(tmp);
417 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
420 return getTokenValue(str, token, tmp) && string2type(tmp, num);
424 bool getTokenValue(string const & str, char const * token,
425 Tabular::HAlignment & num)
428 return getTokenValue(str, token, tmp) && string2type(tmp, num);
432 bool getTokenValue(string const & str, char const * token,
433 Tabular::VAlignment & num)
436 return getTokenValue(str, token, tmp) && string2type(tmp, num);
440 bool getTokenValue(string const & str, char const * token,
441 Tabular::BoxType & num)
444 return getTokenValue(str, token, tmp) && string2type(tmp, num);
448 bool getTokenValue(string const & str, char const * token, bool & flag)
450 // set the flag always to false as this should be the default for bools
451 // not in the file-format.
454 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
458 bool getTokenValue(string const & str, char const * token, Length & len)
460 // set the length to be zero() as default as this it should be if not
461 // in the file format.
464 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
468 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
473 if (!getTokenValue(str, token, tmp))
475 if (tmp == "default") {
479 return isValidLength(tmp, &len);
483 void l_getline(istream & is, string & str)
486 while (str.empty()) {
488 if (!str.empty() && str[str.length() - 1] == '\r')
489 str.erase(str.length() - 1);
496 string const featureAsString(Tabular::Feature action)
498 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
499 if (tabularFeature[i].action == action)
500 return tabularFeature[i].feature;
507 /////////////////////////////////////////////////////////////////////
511 /////////////////////////////////////////////////////////////////////
514 Tabular::CellData::CellData(Buffer * buf)
520 multicolumn(Tabular::CELL_NORMAL),
521 multirow(Tabular::CELL_NORMAL),
522 alignment(LYX_ALIGN_CENTER),
523 valignment(LYX_VALIGN_TOP),
530 inset(new InsetTableCell(buf))
532 inset->setBuffer(*buf);
536 Tabular::CellData::CellData(CellData const & cs)
539 multicolumn(cs.multicolumn),
540 multirow(cs.multirow),
541 alignment(cs.alignment),
542 valignment(cs.valignment),
543 top_line(cs.top_line),
544 bottom_line(cs.bottom_line),
545 left_line(cs.left_line),
546 right_line(cs.right_line),
549 align_special(cs.align_special),
551 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
555 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
561 void Tabular::CellData::swap(CellData & rhs)
563 std::swap(cellno, rhs.cellno);
564 std::swap(width, rhs.width);
565 std::swap(multicolumn, rhs.multicolumn);
566 std::swap(multirow, rhs.multirow);
567 std::swap(alignment, rhs.alignment);
568 std::swap(valignment, rhs.valignment);
569 std::swap(top_line, rhs.top_line);
570 std::swap(bottom_line, rhs.bottom_line);
571 std::swap(left_line, rhs.left_line);
572 std::swap(right_line, rhs.right_line);
573 std::swap(usebox, rhs.usebox);
574 std::swap(rotate, rhs.rotate);
575 std::swap(align_special, rhs.align_special);
576 p_width.swap(rhs.p_width);
577 inset.swap(rhs.inset);
581 Tabular::RowData::RowData()
584 top_space_default(false),
585 bottom_space_default(false),
586 interline_space_default(false),
593 valignment(LYX_VALIGN_TOP),
598 Tabular::ColumnData::ColumnData()
599 : alignment(LYX_ALIGN_CENTER),
600 valignment(LYX_VALIGN_TOP),
606 Tabular::ltType::ltType()
613 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
615 init(buffer, rows_arg, columns_arg);
619 void Tabular::setBuffer(Buffer & buffer)
622 for (row_type i = 0; i < nrows(); ++i)
623 for (col_type j = 0; j < ncols(); ++j)
624 cell_info[i][j].inset->setBuffer(*buffer_);
628 // activates all lines and sets all widths to 0
629 void Tabular::init(Buffer * buf, row_type rows_arg,
630 col_type columns_arg)
633 row_info = row_vector(rows_arg);
634 column_info = column_vector(columns_arg);
635 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
636 row_info.reserve(10);
637 column_info.reserve(10);
638 cell_info.reserve(100);
640 is_long_tabular = false;
641 tabular_valignment = LYX_VALIGN_MIDDLE;
642 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
644 use_booktabs = false;
645 // set silly default lines
646 for (row_type r = 0; r < nrows(); ++r)
647 for (col_type c = 0; c < ncols(); ++c) {
648 cell_info[r][c].inset->setBuffer(*buffer_);
649 cell_info[r][c].top_line = true;
650 cell_info[r][c].left_line = true;
651 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
652 cell_info[r][c].right_line = c == ncols() - 1;
657 void Tabular::appendRow(idx_type const cell)
659 row_type const row = cellRow(cell);
661 row_info.insert(row_info.begin() + row + 1, RowData());
662 row_info[row + 1] = row_info[row];
664 cell_info.insert(cell_info.begin() + row + 1,
665 cell_vector(ncols(), CellData(buffer_)));
666 for (col_type c = 0; c < ncols(); ++c) {
667 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
668 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
670 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
674 for (col_type c = 0; c < ncols(); ++c) {
675 if (isPartOfMultiRow(row, c))
677 // inherit line settings
678 idx_type const i = cellIndex(row + 1, c);
679 idx_type const j = cellIndex(row, c);
680 setLeftLine(i, leftLine(j));
681 setRightLine(i, rightLine(j));
682 setTopLine(i, topLine(j));
683 if (topLine(j) && bottomLine(j)) {
684 setBottomLine(i, true);
685 setBottomLine(j, false);
687 // mark track changes
688 if (buffer().params().trackChanges)
689 cellInfo(i).inset->setChange(Change(Change::INSERTED));
694 void Tabular::deleteRow(row_type const row)
696 // Not allowed to delete last row
700 for (col_type c = 0; c < ncols(); ++c) {
701 // Care about multirow cells
702 if (row + 1 < nrows() &&
703 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
704 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
705 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
708 row_info.erase(row_info.begin() + row);
709 cell_info.erase(cell_info.begin() + row);
714 void Tabular::copyRow(row_type const row)
716 row_info.insert(row_info.begin() + row, row_info[row]);
717 cell_info.insert(cell_info.begin() + row, cell_info[row]);
719 if (buffer().params().trackChanges)
720 for (col_type c = 0; c < ncols(); ++c)
721 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
727 void Tabular::appendColumn(idx_type const cell)
729 col_type const c = cellColumn(cell);
731 column_info.insert(column_info.begin() + c + 1, ColumnData());
732 column_info[c + 1] = column_info[c];
734 for (row_type r = 0; r < nrows(); ++r) {
735 cell_info[r].insert(cell_info[r].begin() + c + 1,
737 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
738 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
740 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
743 for (row_type r = 0; r < nrows(); ++r) {
744 // inherit line settings
745 idx_type const i = cellIndex(r, c + 1);
746 idx_type const j = cellIndex(r, c);
747 setBottomLine(i, bottomLine(j));
748 setTopLine(i, topLine(j));
749 setLeftLine(i, leftLine(j));
750 if (rightLine(j) && rightLine(j)) {
751 setRightLine(i, true);
752 setRightLine(j, false);
754 if (buffer().params().trackChanges)
755 cellInfo(i).inset->setChange(Change(Change::INSERTED));
760 void Tabular::deleteColumn(col_type const col)
762 // Not allowed to delete last column
766 for (row_type r = 0; r < nrows(); ++r) {
767 // Care about multicolumn cells
768 if (col + 1 < ncols() &&
769 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
770 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
771 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
773 cell_info[r].erase(cell_info[r].begin() + col);
775 column_info.erase(column_info.begin() + col);
780 void Tabular::copyColumn(col_type const col)
782 BufferParams const & bp = buffer().params();
783 column_info.insert(column_info.begin() + col, column_info[col]);
785 for (row_type r = 0; r < nrows(); ++r) {
786 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
788 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
794 void Tabular::updateIndexes()
798 for (row_type row = 0; row < nrows(); ++row)
799 for (col_type column = 0; column < ncols(); ++column) {
800 if (!isPartOfMultiColumn(row, column)
801 && !isPartOfMultiRow(row, column))
803 if (isPartOfMultiRow(row, column))
804 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
806 cell_info[row][column].cellno = numberofcells - 1;
809 rowofcell.resize(numberofcells);
810 columnofcell.resize(numberofcells);
812 for (row_type row = 0; row < nrows(); ++row)
813 for (col_type column = 0; column < ncols(); ++column) {
814 if (isPartOfMultiColumn(row, column)
815 || isPartOfMultiRow(row, column))
818 columnofcell[i] = column;
819 setFixedWidth(row, column);
820 updateContentAlignment(row, column);
826 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
829 for (col_type c = 0; c < ncols(); ++c)
830 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
836 bool Tabular::topLine(idx_type const cell) const
838 return cellInfo(cell).top_line;
842 bool Tabular::bottomLine(idx_type const cell) const
844 return cellInfo(cell).bottom_line;
848 bool Tabular::leftLine(idx_type cell) const
852 return cellInfo(cell).left_line;
856 bool Tabular::rightLine(idx_type cell) const
860 return cellInfo(cell).right_line;
864 int Tabular::interRowSpace(row_type row) const
866 if (!row || row >= nrows())
869 int const interline_space = row_info[row - 1].interline_space_default ?
871 row_info[row - 1].interline_space.inPixels(width());
872 if (rowTopLine(row) && rowBottomLine(row - 1))
873 return interline_space + WIDTH_OF_LINE;
874 return interline_space;
878 int Tabular::interColumnSpace(idx_type cell) const
880 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
881 if (rightLine(cell) && nextcol < ncols()
882 && leftLine(cellIndex(cellRow(cell), nextcol)))
883 return WIDTH_OF_LINE;
888 int Tabular::columnWidth(idx_type cell) const
891 col_type const span = columnSpan(cell);
892 col_type const col = cellColumn(cell);
893 for(col_type c = col; c < col + span ; ++c)
894 w += column_info[c].width;
899 int Tabular::cellHeight(idx_type cell) const
901 row_type const span = rowSpan(cell);
902 row_type const row = cellRow(cell);
904 for(row_type r = row; r < row + span ; ++r) {
905 h += rowAscent(r) + rowDescent(r);
906 if (r != row + span - 1)
907 h += interRowSpace(r + 1);
914 bool Tabular::updateColumnWidths()
917 // for each col get max of single col cells
918 for(col_type c = 0; c < ncols(); ++c) {
920 for(row_type r = 0; r < nrows(); ++r) {
921 idx_type const i = cellIndex(r, c);
922 if (columnSpan(i) == 1)
923 new_width = max(new_width, cellInfo(i).width);
926 if (column_info[c].width != new_width) {
927 column_info[c].width = new_width;
931 // update col widths to fit merged cells
932 for(col_type c = 0; c < ncols(); ++c)
933 for(row_type r = 0; r < nrows(); ++r) {
934 idx_type const i = cellIndex(r, c);
935 int const span = columnSpan(i);
936 if (span == 1 || c > cellColumn(i))
940 for(col_type j = c; j < c + span ; ++j)
941 old_width += column_info[j].width;
943 if (cellInfo(i).width > old_width) {
944 column_info[c + span - 1].width += cellInfo(i).width - old_width;
953 int Tabular::width() const
956 for (col_type c = 0; c < ncols(); ++c)
957 width += column_info[c].width;
962 void Tabular::setCellWidth(idx_type cell, int new_width)
964 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
965 + interColumnSpace(cell);
969 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
972 if (!isMultiColumn(cell) || onlycolumn)
973 column_info[cellColumn(cell)].alignment = align;
975 cellInfo(cell).alignment = align;
976 cellInset(cell).get()->setContentAlignment(align);
980 void Tabular::setVAlignment(idx_type cell, VAlignment align,
983 if (!isMultiColumn(cell) || onlycolumn)
984 column_info[cellColumn(cell)].valignment = align;
986 cellInfo(cell).valignment = align;
993 * Allow line and paragraph breaks for fixed width cells or disallow them,
994 * merge cell paragraphs and reset layout to standard for variable width
997 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
999 inset->setAutoBreakRows(fixedWidth);
1000 inset->toggleFixedWidth(fixedWidth);
1004 // merge all paragraphs to one
1005 BufferParams const & bp = cur.bv().buffer().params();
1006 while (inset->paragraphs().size() > 1)
1007 mergeParagraph(bp, inset->paragraphs(), 0);
1011 // undo information has already been recorded
1012 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1013 bp.documentClass().plainLayoutName());
1020 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1021 Length const & width)
1023 col_type const c = cellColumn(cell);
1025 column_info[c].p_width = width;
1026 // reset the vertical alignment to top if the fixed with
1027 // is removed or zero because only fixed width columns can
1028 // have a vertical alignment
1029 if (column_info[c].p_width.zero())
1030 column_info[c].valignment = LYX_VALIGN_TOP;
1031 for (row_type r = 0; r < nrows(); ++r) {
1032 idx_type const cell = cellIndex(r, c);
1033 // because of multicolumns
1034 toggleFixedWidth(cur, cellInset(cell).get(),
1035 !getPWidth(cell).zero());
1037 // cur paragraph can become invalid after paragraphs were merged
1038 if (cur.pit() > cur.lastpit())
1039 cur.pit() = cur.lastpit();
1040 // cur position can become invalid after newlines were removed
1041 if (cur.pos() > cur.lastpos())
1042 cur.pos() = cur.lastpos();
1046 bool Tabular::setFixedWidth(row_type r, col_type c)
1048 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1049 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1050 || (multicol && !cell_info[r][c].p_width.zero());
1051 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1056 void Tabular::updateContentAlignment(row_type r, col_type c)
1058 cell_info[r][c].inset->setContentAlignment(
1059 getAlignment(cellIndex(r, c)));
1063 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1064 Length const & width)
1066 if (!isMultiColumn(cell))
1069 cellInfo(cell).p_width = width;
1070 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1071 // cur paragraph can become invalid after paragraphs were merged
1072 if (cur.pit() > cur.lastpit())
1073 cur.pit() = cur.lastpit();
1074 // cur position can become invalid after newlines were removed
1075 if (cur.pos() > cur.lastpos())
1076 cur.pos() = cur.lastpos();
1081 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1082 Tabular::Feature what)
1084 if (what == SET_SPECIAL_MULTICOLUMN)
1085 cellInfo(cell).align_special = special;
1087 column_info[cellColumn(cell)].align_special = special;
1091 void Tabular::setTopLine(idx_type i, bool line)
1093 cellInfo(i).top_line = line;
1097 void Tabular::setBottomLine(idx_type i, bool line)
1099 cellInfo(i).bottom_line = line;
1103 void Tabular::setLeftLine(idx_type cell, bool line)
1105 cellInfo(cell).left_line = line;
1109 void Tabular::setRightLine(idx_type cell, bool line)
1111 cellInfo(cell).right_line = line;
1114 bool Tabular::rowTopLine(row_type r) const
1116 bool all_rows_set = true;
1117 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1118 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1119 return all_rows_set;
1123 bool Tabular::rowBottomLine(row_type r) const
1125 bool all_rows_set = true;
1126 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1127 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1128 return all_rows_set;
1132 bool Tabular::columnLeftLine(col_type c) const
1139 for (row_type r = 0; r < nrows(); ++r) {
1140 idx_type const i = cellIndex(r, c);
1141 if (c == cellColumn(i)) {
1143 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1144 if (cellInfo(i).left_line || right)
1148 return 2 * nrows_left >= total;
1152 bool Tabular::columnRightLine(col_type c) const
1157 int nrows_right = 0;
1159 for (row_type r = 0; r < nrows(); ++r) {
1160 idx_type i = cellIndex(r, c);
1161 if (c == cellColumn(i) + columnSpan(i) - 1) {
1163 bool left = (c + 1 < ncols()
1164 && cellInfo(cellIndex(r, c + 1)).left_line)
1165 || c + 1 == ncols();
1166 if (cellInfo(i).right_line && left)
1170 return 2 * nrows_right >= total;
1174 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1176 if (!onlycolumn && isMultiColumn(cell))
1177 return cellInfo(cell).alignment;
1178 return column_info[cellColumn(cell)].alignment;
1183 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1185 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1186 return cellInfo(cell).valignment;
1187 return column_info[cellColumn(cell)].valignment;
1191 Length const Tabular::getPWidth(idx_type cell) const
1193 if (isMultiColumn(cell))
1194 return cellInfo(cell).p_width;
1195 return column_info[cellColumn(cell)].p_width;
1199 int Tabular::cellWidth(idx_type cell) const
1201 return cellInfo(cell).width;
1205 int Tabular::textHOffset(idx_type cell) const
1207 // the LaTeX Way :-(
1208 int x = WIDTH_OF_LINE;
1210 switch (getAlignment(cell)) {
1211 case LYX_ALIGN_CENTER:
1212 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1214 case LYX_ALIGN_RIGHT:
1215 x += columnWidth(cell) - cellWidth(cell);
1216 // + interColumnSpace(cell);
1219 // LYX_ALIGN_LEFT: nothing :-)
1227 int Tabular::textVOffset(idx_type cell) const
1229 row_type const r = cellRow(cell);
1230 int y = cellHeight(cell) - rowDescent(r) - rowAscent(r);
1231 switch (getVAlignment(cell)) {
1232 case LYX_VALIGN_TOP:
1235 case LYX_VALIGN_MIDDLE:
1238 case LYX_VALIGN_BOTTOM:
1246 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1249 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1251 return cell_info[row][c].cellno;
1255 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1257 col_type c = ncols() - 1;
1258 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1259 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1261 return cell_info[row][c].cellno;
1265 Tabular::row_type Tabular::cellRow(idx_type cell) const
1267 if (cell >= numberofcells)
1271 return rowofcell[cell];
1275 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1277 if (cell >= numberofcells)
1281 return columnofcell[cell];
1285 void Tabular::write(ostream & os) const
1289 << write_attribute("version", 3)
1290 << write_attribute("rows", nrows())
1291 << write_attribute("columns", ncols())
1293 // global longtable options
1295 << write_attribute("rotate", rotate)
1296 << write_attribute("booktabs", use_booktabs)
1297 << write_attribute("islongtable", is_long_tabular)
1298 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1299 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1300 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1301 << write_attribute("headTopDL", endhead.topDL)
1302 << write_attribute("headBottomDL", endhead.bottomDL)
1303 << write_attribute("footTopDL", endfoot.topDL)
1304 << write_attribute("footBottomDL", endfoot.bottomDL)
1305 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1306 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1307 << write_attribute("lastFootEmpty", endlastfoot.empty);
1308 // longtables cannot be aligned vertically
1309 if (!is_long_tabular)
1310 os << write_attribute("tabularvalignment", tabular_valignment);
1311 if (is_long_tabular)
1312 os << write_attribute("longtabularalignment",
1313 longtabular_alignment);
1315 for (col_type c = 0; c < ncols(); ++c) {
1317 << write_attribute("alignment", column_info[c].alignment)
1318 << write_attribute("valignment", column_info[c].valignment)
1319 << write_attribute("width", column_info[c].p_width.asString())
1320 << write_attribute("special", column_info[c].align_special)
1323 for (row_type r = 0; r < nrows(); ++r) {
1324 static const string def("default");
1326 if (row_info[r].top_space_default)
1327 os << write_attribute("topspace", def);
1329 os << write_attribute("topspace", row_info[r].top_space);
1330 if (row_info[r].bottom_space_default)
1331 os << write_attribute("bottomspace", def);
1333 os << write_attribute("bottomspace", row_info[r].bottom_space);
1334 if (row_info[r].interline_space_default)
1335 os << write_attribute("interlinespace", def);
1337 os << write_attribute("interlinespace", row_info[r].interline_space);
1338 os << write_attribute("endhead", row_info[r].endhead)
1339 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1340 << write_attribute("endfoot", row_info[r].endfoot)
1341 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1342 << write_attribute("newpage", row_info[r].newpage)
1343 << write_attribute("caption", row_info[r].caption)
1345 for (col_type c = 0; c < ncols(); ++c) {
1347 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1348 << write_attribute("multirow", cell_info[r][c].multirow)
1349 << write_attribute("alignment", cell_info[r][c].alignment)
1350 << write_attribute("valignment", cell_info[r][c].valignment)
1351 << write_attribute("topline", cell_info[r][c].top_line)
1352 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1353 << write_attribute("leftline", cell_info[r][c].left_line)
1354 << write_attribute("rightline", cell_info[r][c].right_line)
1355 << write_attribute("rotate", cell_info[r][c].rotate)
1356 << write_attribute("usebox", cell_info[r][c].usebox)
1357 << write_attribute("width", cell_info[r][c].p_width)
1358 << write_attribute("special", cell_info[r][c].align_special)
1360 os << "\\begin_inset ";
1361 cell_info[r][c].inset->write(os);
1362 os << "\n\\end_inset\n"
1367 os << "</lyxtabular>\n";
1371 void Tabular::read(Lexer & lex)
1374 istream & is = lex.getStream();
1376 l_getline(is, line);
1377 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1378 LASSERT(false, /**/);
1383 if (!getTokenValue(line, "version", version))
1385 LASSERT(version >= 2, /**/);
1388 if (!getTokenValue(line, "rows", rows_arg))
1391 if (!getTokenValue(line, "columns", columns_arg))
1393 init(buffer_, rows_arg, columns_arg);
1394 l_getline(is, line);
1395 if (!prefixIs(line, "<features")) {
1396 lyxerr << "Wrong tabular format (expected <features ...> got"
1397 << line << ')' << endl;
1400 getTokenValue(line, "rotate", rotate);
1401 getTokenValue(line, "booktabs", use_booktabs);
1402 getTokenValue(line, "islongtable", is_long_tabular);
1403 getTokenValue(line, "tabularvalignment", tabular_valignment);
1404 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1405 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1406 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1407 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1408 getTokenValue(line, "headTopDL", endhead.topDL);
1409 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1410 getTokenValue(line, "footTopDL", endfoot.topDL);
1411 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1412 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1413 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1414 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1416 for (col_type c = 0; c < ncols(); ++c) {
1418 if (!prefixIs(line,"<column")) {
1419 lyxerr << "Wrong tabular format (expected <column ...> got"
1420 << line << ')' << endl;
1423 getTokenValue(line, "alignment", column_info[c].alignment);
1424 getTokenValue(line, "valignment", column_info[c].valignment);
1425 getTokenValue(line, "width", column_info[c].p_width);
1426 getTokenValue(line, "special", column_info[c].align_special);
1429 for (row_type i = 0; i < nrows(); ++i) {
1430 l_getline(is, line);
1431 if (!prefixIs(line, "<row")) {
1432 lyxerr << "Wrong tabular format (expected <row ...> got"
1433 << line << ')' << endl;
1436 getTokenValue(line, "topspace", row_info[i].top_space,
1437 row_info[i].top_space_default);
1438 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1439 row_info[i].bottom_space_default);
1440 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1441 row_info[i].interline_space_default);
1442 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1443 getTokenValue(line, "endhead", row_info[i].endhead);
1444 getTokenValue(line, "endfoot", row_info[i].endfoot);
1445 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1446 getTokenValue(line, "newpage", row_info[i].newpage);
1447 getTokenValue(line, "caption", row_info[i].caption);
1448 for (col_type j = 0; j < ncols(); ++j) {
1449 l_getline(is, line);
1450 if (!prefixIs(line, "<cell")) {
1451 lyxerr << "Wrong tabular format (expected <cell ...> got"
1452 << line << ')' << endl;
1455 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1456 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1457 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1458 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1459 getTokenValue(line, "topline", cell_info[i][j].top_line);
1460 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1461 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1462 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1463 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1464 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1465 getTokenValue(line, "width", cell_info[i][j].p_width);
1467 getTokenValue(line, "special", cell_info[i][j].align_special);
1468 l_getline(is, line);
1469 if (prefixIs(line, "\\begin_inset")) {
1470 cell_info[i][j].inset->setBuffer(*buffer_);
1471 cell_info[i][j].inset->read(lex);
1472 l_getline(is, line);
1474 if (!prefixIs(line, "</cell>")) {
1475 lyxerr << "Wrong tabular format (expected </cell> got"
1476 << line << ')' << endl;
1480 l_getline(is, line);
1481 if (!prefixIs(line, "</row>")) {
1482 lyxerr << "Wrong tabular format (expected </row> got"
1483 << line << ')' << endl;
1487 while (!prefixIs(line, "</lyxtabular>")) {
1488 l_getline(is, line);
1494 bool Tabular::isMultiColumn(idx_type cell) const
1496 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1497 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1501 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1503 return cell_info[cellRow(cell)][cellColumn(cell)];
1507 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1509 idx_type const col = cellColumn(cell);
1510 idx_type const row = cellRow(cell);
1511 for (idx_type i = 0; i < number; ++i)
1512 unsetMultiRow(cellIndex(row, col + i));
1514 // unsetting of multirow may have invalidated cell index
1515 cell = cellIndex(row, col);
1516 CellData & cs = cellInfo(cell);
1517 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1518 cs.alignment = column_info[col].alignment;
1520 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1522 for (idx_type i = 1; i < number; ++i) {
1523 CellData & cs1 = cellInfo(cell + i);
1524 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1525 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1533 bool Tabular::isMultiRow(idx_type cell) const
1535 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1536 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1540 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1542 idx_type const col = cellColumn(cell);
1543 idx_type const row = cellRow(cell);
1544 for (idx_type i = 0; i < number; ++i)
1545 unsetMultiColumn(cellIndex(row + i, col));
1547 // unsetting of multicol may have invalidated cell index
1548 cell = cellIndex(row, col);
1549 CellData & cs = cellInfo(cell);
1550 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1551 cs.valignment = LYX_VALIGN_MIDDLE;
1552 // FIXME: the horizontal alignment of multirow cells can only
1553 // be changed for the whole table,
1554 // support for this needs to be implemented but us a fileformat
1555 // change (assigning this to uwestoehr)
1556 // until LyX supports this, use the deault alignment of multirow
1558 cs.alignment = LYX_ALIGN_LEFT;
1560 // set the bottom row of the last selected cell
1561 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1563 for (idx_type i = 1; i < number; ++i) {
1564 CellData & cs1 = cell_info[row + i][col];
1565 cs1.multirow = CELL_PART_OF_MULTIROW;
1566 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1574 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1576 row_type const row = cellRow(cell);
1577 col_type column = cellColumn(cell) + 1;
1578 while (column < ncols() && isPartOfMultiColumn(row, column))
1581 return column - cellColumn(cell);
1585 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1587 col_type const column = cellColumn(cell);
1588 col_type row = cellRow(cell) + 1;
1589 while (row < nrows() && isPartOfMultiRow(row, column))
1592 return row - cellRow(cell);
1596 void Tabular::unsetMultiColumn(idx_type cell)
1598 if (!isMultiColumn(cell))
1601 row_type const row = cellRow(cell);
1602 col_type const col = cellColumn(cell);
1603 row_type const span = columnSpan(cell);
1604 for (col_type c = 0; c < span; ++c)
1605 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1610 void Tabular::unsetMultiRow(idx_type cell)
1612 if (!isMultiRow(cell))
1615 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1616 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1617 row_type const row = cellRow(cell);
1618 col_type const col = cellColumn(cell);
1619 row_type const span = rowSpan(cell);
1620 for (row_type r = 0; r < span; ++r)
1621 cell_info[row + r][col].multirow = CELL_NORMAL;
1626 void Tabular::setRotateCell(idx_type cell, bool flag)
1628 cellInfo(cell).rotate = flag;
1632 bool Tabular::getRotateCell(idx_type cell) const
1634 return cellInfo(cell).rotate;
1638 bool Tabular::needRotating() const
1642 for (row_type r = 0; r < nrows(); ++r)
1643 for (col_type c = 0; c < ncols(); ++c)
1644 if (cell_info[r][c].rotate)
1650 bool Tabular::isLastCell(idx_type cell) const
1652 if (cell + 1 < numberofcells)
1658 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1660 if (cellRow(cell) == 0)
1663 col_type const col = cellColumn(cell);
1664 row_type r = cellRow(cell) - 1;
1665 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1668 return cell_info[r][col].cellno;
1672 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1674 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1675 if (nextrow < nrows())
1676 return cell_info[nextrow][cellColumn(cell)].cellno;
1681 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1683 LASSERT(column != npos && column < ncols()
1684 && row != npos && row < nrows(), /**/);
1685 return cell_info[row][column].cellno;
1689 void Tabular::setUsebox(idx_type cell, BoxType type)
1691 cellInfo(cell).usebox = type;
1695 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1696 // adds line breaks, see bug 4886.
1697 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1699 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1700 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1702 if (cellInfo(cell).usebox > 1)
1703 return cellInfo(cell).usebox;
1704 return useParbox(cell);
1709 // This are functions used for the longtable support
1711 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1717 row_info[row].endfirsthead = flag;
1721 row_info[row].endhead = flag;
1726 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1729 hd.set = haveLTHead();
1730 return row_info[row].endhead;
1734 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1737 hd.set = haveLTFirstHead();
1738 return row_info[row].endfirsthead;
1742 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1748 row_info[row].endlastfoot = flag;
1752 row_info[row].endfoot = flag;
1757 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1760 fd.set = haveLTFoot();
1761 return row_info[row].endfoot;
1765 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1768 fd.set = haveLTLastFoot();
1769 return row_info[row].endlastfoot;
1773 void Tabular::setLTNewPage(row_type row, bool what)
1775 row_info[row].newpage = what;
1779 bool Tabular::getLTNewPage(row_type row) const
1781 return row_info[row].newpage;
1785 bool Tabular::haveLTHead() const
1787 for (row_type i = 0; i < nrows(); ++i)
1788 if (row_info[i].endhead)
1794 bool Tabular::haveLTFirstHead() const
1796 if (endfirsthead.empty)
1798 for (row_type r = 0; r < nrows(); ++r)
1799 if (row_info[r].endfirsthead)
1805 bool Tabular::haveLTFoot() const
1807 for (row_type r = 0; r < nrows(); ++r)
1808 if (row_info[r].endfoot)
1814 bool Tabular::haveLTLastFoot() const
1816 if (endlastfoot.empty)
1818 for (row_type r = 0; r < nrows(); ++r)
1819 if (row_info[r].endlastfoot)
1825 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1827 idx_type i = getFirstCellInRow(row);
1829 setMultiColumn(i, numberOfCellsInRow(row));
1830 setTopLine(i, false);
1831 setBottomLine(i, false);
1832 setLeftLine(i, false);
1833 setRightLine(i, false);
1835 unsetMultiColumn(i);
1836 // When unsetting a caption row, also all existing
1837 // captions in this row must be dissolved.
1839 row_info[row].caption = what;
1844 bool Tabular::ltCaption(row_type row) const
1846 return row_info[row].caption;
1850 bool Tabular::haveLTCaption() const
1852 for (row_type r = 0; r < nrows(); ++r)
1853 if (row_info[r].caption)
1859 // end longtable support functions
1861 void Tabular::setRowAscent(row_type row, int height)
1863 if (row >= nrows() || row_info[row].ascent == height)
1865 row_info[row].ascent = height;
1869 void Tabular::setRowDescent(row_type row, int height)
1871 if (row >= nrows() || row_info[row].descent == height)
1873 row_info[row].descent = height;
1877 int Tabular::rowAscent(row_type row) const
1879 LASSERT(row < nrows(), /**/);
1880 return row_info[row].ascent;
1884 int Tabular::rowDescent(row_type row) const
1886 LASSERT(row < nrows(), /**/);
1887 return row_info[row].descent;
1891 int Tabular::height() const
1894 for (row_type row = 0; row < nrows(); ++row)
1895 height += rowAscent(row) + rowDescent(row) +
1901 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1903 LASSERT(row < nrows(), /**/);
1904 LASSERT(column < ncols(), /**/);
1905 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1909 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1911 LASSERT(row < nrows(), /**/);
1912 LASSERT(column < ncols(), /**/);
1913 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1917 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1919 // we only output complete row lines and the 1st row here, the rest
1920 // is done in Tabular::TeXBottomHLine(...)
1922 // get for each column the topline (if any)
1923 vector<bool> topline;
1925 for (col_type c = 0; c < ncols(); ++c) {
1926 topline.push_back(topLine(cellIndex(row, c)));
1927 // If cell is part of a multirow and not the first cell of the
1928 // multirow, no line must be drawn.
1930 if (isMultiRow(cellIndex(row, c))
1931 && isMultiRow(cellIndex(row - 1, c)))
1937 // do nothing if empty first row, or incomplete row line after
1938 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
1941 // only output complete row lines and the 1st row's clines
1942 if (nset == ncols()) {
1944 os << (row == 0 ? "\\toprule " : "\\midrule ");
1948 } else if (row == 0) {
1949 for (col_type c = 0; c < ncols(); ++c) {
1951 //babel makes the "-" character an active one, so we have to suppress this here
1952 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1953 if (lang == "slovak" || lang == "czech")
1954 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1955 "\\expandafter\\cline\\expandafter{\\expandafter")
1956 << c + 1 << "\\string-";
1958 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1959 // get to last column of line span
1960 while (c < ncols() && topline[c])
1971 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
1973 // we output bottomlines of row r and the toplines of row r+1
1974 // if the latter do not span the whole tabular
1976 // get the bottomlines of row r, and toplines in next row
1977 bool lastrow = row == nrows() - 1;
1978 vector<bool> bottomline, topline;
1979 bool nextrowset = true;
1980 for (col_type c = 0; c < ncols(); ++c) {
1981 bottomline.push_back(bottomLine(cellIndex(row, c)));
1982 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1983 // If cell is part of a multirow and not the last or first cell of the
1984 // multirow, no line must be drawn.
1986 if (isMultiRow(cellIndex(row, c))
1987 && isMultiRow(cellIndex(row + 1, c))) {
1988 bottomline[c] = false;
1991 nextrowset &= topline[c];
1994 // combine this row's bottom lines and next row's toplines if necessary
1996 for (col_type c = 0; c < ncols(); ++c) {
1998 bottomline[c] = bottomline[c] || topline[c];
2003 // do nothing if empty, OR incomplete row line with a topline in next row
2004 if (nset == 0 || (nextrowset && nset != ncols()))
2007 if (nset == ncols()) {
2009 os << (lastrow ? "\\bottomrule" : "\\midrule");
2013 for (col_type c = 0; c < ncols(); ++c) {
2014 if (bottomline[c]) {
2015 //babel makes the "-" character an active one, so we have to suppress this here
2016 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2017 if (lang == "slovak" || lang == "czech")
2018 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
2019 "\\expandafter\\cline\\expandafter{\\expandafter")
2020 << c + 1 << "\\string-";
2022 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2023 // get to last column of line span
2024 while (c < ncols() && bottomline[c])
2035 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2036 bool & ismulticol, bool & ismultirow) const
2039 row_type const r = cellRow(cell);
2040 if (is_long_tabular && row_info[r].caption)
2043 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2044 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2045 // figure out how to set the lines
2046 // we always set double lines to the right of the cell
2047 col_type const c = cellColumn(cell);
2048 col_type const nextcol = c + columnSpan(cell);
2049 bool colright = columnRightLine(c);
2050 bool colleft = columnLeftLine(c);
2051 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2052 bool nextcellleft = nextcol < ncols()
2053 && leftLine(cellIndex(r, nextcol));
2054 bool coldouble = colright && nextcolleft;
2055 bool celldouble = rightLine(cell) && nextcellleft;
2057 ismulticol = isMultiColumn(cell)
2058 || (c == 0 && colleft != leftLine(cell))
2059 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2060 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2061 || (coldouble != celldouble);
2063 os << "\\multicolumn{" << columnSpan(cell) << "}{";
2064 if (c ==0 && leftLine(cell))
2066 if (!cellInfo(cell).align_special.empty()) {
2067 os << cellInfo(cell).align_special;
2069 if (!getPWidth(cell).zero()) {
2071 case LYX_ALIGN_LEFT:
2072 os << ">{\\raggedright}";
2074 case LYX_ALIGN_RIGHT:
2075 os << ">{\\raggedleft}";
2077 case LYX_ALIGN_CENTER:
2078 os << ">{\\centering}";
2084 case LYX_VALIGN_TOP:
2087 case LYX_VALIGN_MIDDLE:
2090 case LYX_VALIGN_BOTTOM:
2095 << from_ascii(getPWidth(cell).asLatexString())
2099 case LYX_ALIGN_LEFT:
2102 case LYX_ALIGN_RIGHT:
2109 } // end if else !getPWidth
2110 } // end if else !cellinfo_of_cell
2111 if (rightLine(cell) || nextcellleft)
2114 // add extra vertical line if we want a double one
2117 } // end if ismulticol
2119 // we only need code for the first multirow cell
2120 ismultirow = isMultiRow(cell);
2122 os << "\\multirow{" << rowSpan(cell) << "}{";
2123 if (!getPWidth(cell).zero())
2124 os << from_ascii(getPWidth(cell).asLatexString());
2126 // we need to set a default value
2127 // needs to be discussed
2130 } // end if ismultirow
2132 if (getRotateCell(cell)) {
2133 os << "\\begin{sideways}\n";
2136 if (getUsebox(cell) == BOX_PARBOX) {
2139 case LYX_VALIGN_TOP:
2142 case LYX_VALIGN_MIDDLE:
2145 case LYX_VALIGN_BOTTOM:
2149 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2151 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2152 os << "\\begin{minipage}[";
2154 case LYX_VALIGN_TOP:
2157 case LYX_VALIGN_MIDDLE:
2160 case LYX_VALIGN_BOTTOM:
2164 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2172 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2173 bool ismulticol, bool ismultirow) const
2176 row_type const r = cellRow(cell);
2177 if (is_long_tabular && row_info[r].caption)
2181 if (getUsebox(cell) == BOX_PARBOX)
2183 else if (getUsebox(cell) == BOX_MINIPAGE) {
2184 os << "%\n\\end{minipage}";
2187 if (getRotateCell(cell)) {
2188 os << "%\n\\end{sideways}";
2191 if (ismulticol || ismultirow) {
2198 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2199 OutputParams const & runparams) const
2201 if (!is_long_tabular)
2206 // the caption must be output before the headers
2207 if (haveLTCaption()) {
2208 for (row_type r = 0; r < nrows(); ++r) {
2209 if (row_info[r].caption) {
2210 ret += TeXRow(os, r, runparams);
2214 // output first header info
2215 // first header must be output before the header, otherwise the
2216 // correct caption placement becomes really weird
2217 if (haveLTFirstHead()) {
2218 if (endfirsthead.topDL) {
2222 for (row_type r = 0; r < nrows(); ++r) {
2223 if (row_info[r].endfirsthead) {
2224 ret += TeXRow(os, r, runparams);
2227 if (endfirsthead.bottomDL) {
2231 os << "\\endfirsthead\n";
2234 // output header info
2236 if (endfirsthead.empty && !haveLTFirstHead()) {
2237 os << "\\endfirsthead\n";
2240 if (endhead.topDL) {
2244 for (row_type r = 0; r < nrows(); ++r) {
2245 if (row_info[r].endhead) {
2246 ret += TeXRow(os, r, runparams);
2249 if (endhead.bottomDL) {
2253 os << "\\endhead\n";
2256 // output footer info
2258 if (endfoot.topDL) {
2262 for (row_type r = 0; r < nrows(); ++r) {
2263 if (row_info[r].endfoot) {
2264 ret += TeXRow(os, r, runparams);
2267 if (endfoot.bottomDL) {
2271 os << "\\endfoot\n";
2273 if (endlastfoot.empty && !haveLTLastFoot()) {
2274 os << "\\endlastfoot\n";
2278 // output lastfooter info
2279 if (haveLTLastFoot()) {
2280 if (endlastfoot.topDL) {
2284 for (row_type r = 0; r < nrows(); ++r) {
2285 if (row_info[r].endlastfoot) {
2286 ret += TeXRow(os, r, runparams);
2289 if (endlastfoot.bottomDL) {
2293 os << "\\endlastfoot\n";
2300 bool Tabular::isValidRow(row_type row) const
2302 if (!is_long_tabular)
2304 return !row_info[row].endhead && !row_info[row].endfirsthead
2305 && !row_info[row].endfoot && !row_info[row].endlastfoot
2306 && !row_info[row].caption;
2310 int Tabular::TeXRow(odocstream & os, row_type row,
2311 OutputParams const & runparams) const
2313 idx_type cell = cellIndex(row, 0);
2314 shared_ptr<InsetTableCell> inset = cellInset(cell);
2315 Paragraph const & par = inset->paragraphs().front();
2316 string const lang = par.getParLanguage(buffer().params())->lang();
2318 //output the top line
2319 int ret = TeXTopHLine(os, row, lang);
2321 if (row_info[row].top_space_default) {
2323 os << "\\addlinespace\n";
2325 os << "\\noalign{\\vskip\\doublerulesep}\n";
2327 } else if(!row_info[row].top_space.zero()) {
2329 os << "\\addlinespace["
2330 << from_ascii(row_info[row].top_space.asLatexString())
2333 os << "\\noalign{\\vskip"
2334 << from_ascii(row_info[row].top_space.asLatexString())
2339 bool ismulticol = false;
2340 bool ismultirow = false;
2341 for (col_type c = 0; c < ncols(); ++c) {
2342 if (isPartOfMultiRow(row, c))
2343 os << " & "; // we need to add a further column
2344 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2346 cell = cellIndex(row, c);
2347 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2348 shared_ptr<InsetTableCell> inset = cellInset(cell);
2350 Paragraph const & par = inset->paragraphs().front();
2351 bool rtl = par.isRTL(buffer().params())
2353 && getPWidth(cell).zero();
2357 par.getParLanguage(buffer().params())->lang();
2358 if (lang == "farsi")
2360 else if (lang == "arabic_arabi")
2362 // currently, remaning RTL languages are
2363 // arabic_arabtex and hebrew
2367 // pass to the OutputParams that we are in a cell and
2368 // which alignment we have set.
2369 // InsetNewline needs this context information.
2370 OutputParams newrp(runparams);
2371 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2372 ? OutputParams::PLAIN
2373 : OutputParams::ALIGNED;
2374 ret += inset->latex(os, newrp);
2375 runparams.encoding = newrp.encoding;
2379 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2380 if (cell != getLastCellInRow(row)) { // not last cell in row
2384 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2385 // if no first header and no empty first header is used,
2386 // the caption needs to be terminated by \endfirsthead
2388 os << "\\endfirsthead";
2390 os << "\\tabularnewline";
2391 if (row_info[row].bottom_space_default) {
2393 os << "\\addlinespace";
2395 os << "[\\doublerulesep]";
2396 } else if (!row_info[row].bottom_space.zero()) {
2398 os << "\\addlinespace";
2400 << from_ascii(row_info[row].bottom_space.asLatexString())
2406 //output the bottom line
2407 ret += TeXBottomHLine(os, row, lang);
2409 if (row_info[row].interline_space_default) {
2411 os << "\\addlinespace\n";
2413 os << "\\noalign{\\vskip\\doublerulesep}\n";
2415 } else if (!row_info[row].interline_space.zero()) {
2417 os << "\\addlinespace["
2418 << from_ascii(row_info[row].interline_space.asLatexString())
2421 os << "\\noalign{\\vskip"
2422 << from_ascii(row_info[row].interline_space.asLatexString())
2430 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2434 //+---------------------------------------------------------------------
2435 //+ first the opening preamble +
2436 //+---------------------------------------------------------------------
2439 os << "\\begin{sideways}\n";
2442 if (is_long_tabular) {
2443 os << "\\begin{longtable}";
2444 switch (longtabular_alignment) {
2445 case LYX_LONGTABULAR_ALIGN_LEFT:
2448 case LYX_LONGTABULAR_ALIGN_CENTER:
2450 case LYX_LONGTABULAR_ALIGN_RIGHT:
2455 os << "\\begin{tabular}";
2456 switch (tabular_valignment) {
2457 case LYX_VALIGN_TOP:
2460 case LYX_VALIGN_MIDDLE:
2462 case LYX_VALIGN_BOTTOM:
2470 for (col_type c = 0; c < ncols(); ++c) {
2471 if (columnLeftLine(c))
2473 if (!column_info[c].align_special.empty()) {
2474 os << column_info[c].align_special;
2476 if (!column_info[c].p_width.zero()) {
2477 switch (column_info[c].alignment) {
2478 case LYX_ALIGN_LEFT:
2479 os << ">{\\raggedright}";
2481 case LYX_ALIGN_RIGHT:
2482 os << ">{\\raggedleft}";
2484 case LYX_ALIGN_CENTER:
2485 os << ">{\\centering}";
2487 case LYX_ALIGN_NONE:
2488 case LYX_ALIGN_BLOCK:
2489 case LYX_ALIGN_LAYOUT:
2490 case LYX_ALIGN_SPECIAL:
2494 switch (column_info[c].valignment) {
2495 case LYX_VALIGN_TOP:
2498 case LYX_VALIGN_MIDDLE:
2501 case LYX_VALIGN_BOTTOM:
2506 << from_ascii(column_info[c].p_width.asLatexString())
2509 switch (column_info[c].alignment) {
2510 case LYX_ALIGN_LEFT:
2513 case LYX_ALIGN_RIGHT:
2520 } // end if else !column_info[i].p_width
2521 } // end if else !column_info[i].align_special
2522 if (columnRightLine(c))
2528 ret += TeXLongtableHeaderFooter(os, runparams);
2530 //+---------------------------------------------------------------------
2531 //+ the single row and columns (cells) +
2532 //+---------------------------------------------------------------------
2534 for (row_type r = 0; r < nrows(); ++r) {
2535 if (isValidRow(r)) {
2536 ret += TeXRow(os, r, runparams);
2537 if (is_long_tabular && row_info[r].newpage) {
2538 os << "\\newpage\n";
2544 //+---------------------------------------------------------------------
2545 //+ the closing of the tabular +
2546 //+---------------------------------------------------------------------
2548 if (is_long_tabular)
2549 os << "\\end{longtable}";
2551 os << "\\end{tabular}";
2553 os << "\n\\end{sideways}";
2561 int Tabular::docbookRow(odocstream & os, row_type row,
2562 OutputParams const & runparams) const
2565 idx_type cell = getFirstCellInRow(row);
2568 for (col_type c = 0; c < ncols(); ++c) {
2569 if (isPartOfMultiColumn(row, c))
2572 os << "<entry align=\"";
2573 switch (getAlignment(cell)) {
2574 case LYX_ALIGN_LEFT:
2577 case LYX_ALIGN_RIGHT:
2585 os << "\" valign=\"";
2586 switch (getVAlignment(cell)) {
2587 case LYX_VALIGN_TOP:
2590 case LYX_VALIGN_BOTTOM:
2593 case LYX_VALIGN_MIDDLE:
2598 if (isMultiColumn(cell)) {
2599 os << " namest=\"col" << c << "\" ";
2600 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2604 ret += cellInset(cell)->docbook(os, runparams);
2613 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2617 //+---------------------------------------------------------------------
2618 //+ first the opening preamble +
2619 //+---------------------------------------------------------------------
2621 os << "<tgroup cols=\"" << ncols()
2622 << "\" colsep=\"1\" rowsep=\"1\">\n";
2624 for (col_type c = 0; c < ncols(); ++c) {
2625 os << "<colspec colname=\"col" << c << "\" align=\"";
2626 switch (column_info[c].alignment) {
2627 case LYX_ALIGN_LEFT:
2630 case LYX_ALIGN_RIGHT:
2638 if (runparams.flavor == OutputParams::XML)
2644 //+---------------------------------------------------------------------
2645 //+ Long Tabular case +
2646 //+---------------------------------------------------------------------
2648 // output caption info
2649 if (haveLTCaption()) {
2650 os << "<caption>\n";
2652 for (row_type r = 0; r < nrows(); ++r) {
2653 if (row_info[r].caption) {
2654 ret += docbookRow(os, r, runparams);
2657 os << "</caption>\n";
2660 // output header info
2661 if (haveLTHead() || haveLTFirstHead()) {
2664 for (row_type r = 0; r < nrows(); ++r) {
2665 if (row_info[r].endhead || row_info[r].endfirsthead) {
2666 ret += docbookRow(os, r, runparams);
2672 // output footer info
2673 if (haveLTFoot() || haveLTLastFoot()) {
2676 for (row_type r = 0; r < nrows(); ++r) {
2677 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2678 ret += docbookRow(os, r, runparams);
2685 //+---------------------------------------------------------------------
2686 //+ the single row and columns (cells) +
2687 //+---------------------------------------------------------------------
2691 for (row_type r = 0; r < nrows(); ++r) {
2692 if (isValidRow(r)) {
2693 ret += docbookRow(os, r, runparams);
2698 //+---------------------------------------------------------------------
2699 //+ the closing of the tabular +
2700 //+---------------------------------------------------------------------
2709 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2710 OutputParams const & runparams) const
2713 idx_type cell = getFirstCellInRow(row);
2715 xs << html::StartTag("tr");
2716 for (col_type c = 0; c < ncols(); ++c) {
2717 if (isPartOfMultiColumn(row, c))
2722 switch (getAlignment(cell)) {
2723 case LYX_ALIGN_LEFT:
2726 case LYX_ALIGN_RIGHT:
2734 attr << " valign='";
2735 switch (getVAlignment(cell)) {
2736 case LYX_VALIGN_TOP:
2739 case LYX_VALIGN_BOTTOM:
2742 case LYX_VALIGN_MIDDLE:
2747 if (isMultiColumn(cell))
2748 attr << " colspan='" << columnSpan(cell) << "'";
2750 xs << html::StartTag("td", attr.str());
2751 ret += cellInset(cell)->xhtml(xs, runparams);
2752 xs << html::EndTag("td");
2755 xs << html::EndTag("tr");
2760 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2763 // It's unclear to me if we need to mess with the long table stuff.
2764 // We can borrow that too from docbook, if so.
2766 xs << html::StartTag("tbody");
2767 for (row_type r = 0; r < nrows(); ++r) {
2768 if (isValidRow(r)) {
2769 ret += xhtmlRow(xs, r, runparams);
2772 xs << html::EndTag("tbody");
2777 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2778 vector<unsigned int> const & clen) const
2780 idx_type const fcell = getFirstCellInRow(row);
2781 idx_type const n = numberOfCellsInRow(row) + fcell;
2784 for (idx_type i = fcell; i < n; ++i) {
2794 for (idx_type i = fcell; i < n; ++i) {
2805 col_type column = cellColumn(i);
2806 int len = clen[column];
2807 while (column < ncols() - 1
2808 && isPartOfMultiColumn(row, ++column))
2809 len += clen[column] + 4;
2810 os << docstring(len, ch);
2825 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2826 vector<unsigned int> const & clen) const
2828 idx_type const fcell = getFirstCellInRow(row);
2829 idx_type const n = numberOfCellsInRow(row) + fcell;
2832 for (idx_type i = fcell; i < n; ++i) {
2833 if (bottomLine(i)) {
2842 for (idx_type i = fcell; i < n; ++i) {
2843 if (bottomLine(i)) {
2853 col_type column = cellColumn(i);
2854 int len = clen[column];
2855 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2856 len += clen[column] + 4;
2857 os << docstring(len, ch);
2858 if (bottomLine(i)) {
2872 void Tabular::plaintextPrintCell(odocstream & os,
2873 OutputParams const & runparams,
2874 idx_type cell, row_type row, col_type column,
2875 vector<unsigned int> const & clen,
2876 bool onlydata) const
2878 odocstringstream sstr;
2879 cellInset(cell)->plaintext(sstr, runparams);
2891 unsigned int len1 = sstr.str().length();
2892 unsigned int len2 = clen[column];
2893 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2894 len2 += clen[column] + 4;
2897 switch (getAlignment(cell)) {
2899 case LYX_ALIGN_LEFT:
2902 case LYX_ALIGN_RIGHT:
2906 case LYX_ALIGN_CENTER:
2912 os << docstring(len1, ' ') << sstr.str()
2913 << docstring(len2, ' ');
2915 if (rightLine(cell))
2922 void Tabular::plaintext(odocstream & os,
2923 OutputParams const & runparams, int const depth,
2924 bool onlydata, char_type delim) const
2926 // first calculate the width of the single columns
2927 vector<unsigned int> clen(ncols());
2930 // first all non multicolumn cells!
2931 for (col_type c = 0; c < ncols(); ++c) {
2933 for (row_type r = 0; r < nrows(); ++r) {
2934 idx_type cell = cellIndex(r, c);
2935 if (isMultiColumn(cell))
2937 odocstringstream sstr;
2938 cellInset(cell)->plaintext(sstr, runparams);
2939 if (clen[c] < sstr.str().length())
2940 clen[c] = sstr.str().length();
2943 // then all multicolumn cells!
2944 for (col_type c = 0; c < ncols(); ++c) {
2945 for (row_type r = 0; r < nrows(); ++r) {
2946 idx_type cell = cellIndex(r, c);
2947 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
2949 odocstringstream sstr;
2950 cellInset(cell)->plaintext(sstr, runparams);
2951 int len = int(sstr.str().length());
2952 idx_type const n = columnSpan(cell);
2953 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
2955 if (len > int(clen[c + n - 1]))
2956 clen[c + n - 1] = len;
2961 for (row_type r = 0; r < nrows(); ++r) {
2962 if (!onlydata && plaintextTopHLine(os, r, clen))
2963 os << docstring(depth * 2, ' ');
2964 for (col_type c = 0; c < ncols(); ++c) {
2965 if (isPartOfMultiColumn(r, c))
2967 if (onlydata && c > 0)
2968 // we don't use operator<< for single UCS4 character.
2969 // see explanation in docstream.h
2971 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
2976 os << docstring(depth * 2, ' ');
2977 if (plaintextBottomHLine(os, r, clen))
2978 os << docstring(depth * 2, ' ');
2984 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2986 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2990 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2991 col_type column) const
2993 return cell_info[row][column].inset;
2997 void Tabular::setCellInset(row_type row, col_type column,
2998 shared_ptr<InsetTableCell> ins) const
3000 CellData & cd = cell_info[row][column];
3005 void Tabular::validate(LaTeXFeatures & features) const
3007 features.require("NeedTabularnewline");
3009 features.require("booktabs");
3010 if (is_long_tabular)
3011 features.require("longtable");
3013 features.require("rotating");
3014 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3015 if (isMultiRow(cell))
3016 features.require("multirow");
3017 if (getVAlignment(cell) != LYX_VALIGN_TOP
3018 || !getPWidth(cell).zero())
3019 features.require("array");
3020 cellInset(cell)->validate(features);
3025 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3027 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3028 ParagraphList::const_iterator cit = parlist.begin();
3029 ParagraphList::const_iterator end = parlist.end();
3031 for (; cit != end; ++cit)
3032 for (int i = 0; i < cit->size(); ++i)
3033 if (cit->isNewline(i))
3040 /////////////////////////////////////////////////////////////////////
3044 /////////////////////////////////////////////////////////////////////
3046 InsetTableCell::InsetTableCell(Buffer * buf)
3047 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3048 contentAlign(LYX_ALIGN_CENTER)
3052 bool InsetTableCell::forcePlainLayout(idx_type) const
3054 return !isFixedWidth;
3058 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3060 return isFixedWidth;
3064 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3065 FuncStatus & status) const
3068 switch (cmd.action) {
3070 enabled = !forcePlainLayout();
3072 case LFUN_LAYOUT_PARAGRAPH:
3073 enabled = allowParagraphCustomization();
3076 return InsetText::getStatus(cur, cmd, status);
3078 status.setEnabled(enabled);
3082 docstring InsetTableCell::asString(bool intoInsets)
3085 if (paragraphs().empty())
3087 ParagraphList::const_iterator it = paragraphs().begin();
3088 ParagraphList::const_iterator en = paragraphs().end();
3090 for (; it != en; ++it) {
3095 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3101 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3104 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3105 return InsetText::xhtml(xs, rp);
3110 /////////////////////////////////////////////////////////////////////
3114 /////////////////////////////////////////////////////////////////////
3116 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3118 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3119 rowselect_(false), colselect_(false)
3124 InsetTabular::InsetTabular(InsetTabular const & tab)
3125 : Inset(tab), tabular(tab.tabular), scx_(0)
3130 InsetTabular::~InsetTabular()
3132 hideDialogs("tabular", this);
3136 void InsetTabular::setBuffer(Buffer & buf)
3138 tabular.setBuffer(buf);
3139 Inset::setBuffer(buf);
3143 bool InsetTabular::insetAllowed(InsetCode code) const
3145 if (code == MATHMACRO_CODE
3146 || (code == CAPTION_CODE && !tabular.is_long_tabular))
3153 void InsetTabular::write(ostream & os) const
3155 os << "Tabular" << endl;
3160 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3162 // FIXME: depending on the selection state, we could offer a different menu.
3163 return from_ascii("context-tabular");
3167 void InsetTabular::read(Lexer & lex)
3169 //bool const old_format = (lex.getString() == "\\LyXTable");
3177 string token = lex.getString();
3178 while (lex && token != "\\end_inset") {
3180 token = lex.getString();
3183 lex.printError("Missing \\end_inset at this point. ");
3187 int InsetTabular::rowFromY(Cursor & cur, int y) const
3189 // top y coordinate of tabular
3190 int h = yo(cur.bv()) - tabular.rowAscent(0);
3192 for (; r < tabular.nrows() && y > h; ++r)
3193 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3194 + tabular.interRowSpace(r);
3200 int InsetTabular::columnFromX(Cursor & cur, int x) const
3202 // left x coordinate of tabular
3203 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3205 for (; c < tabular.ncols() && x > w; ++c)
3206 w += tabular.columnWidth(c);
3211 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3213 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3214 // mi.base.textwidth << "\n";
3217 LASSERT(false, /**/);
3220 for (row_type r = 0; r < tabular.nrows(); ++r) {
3223 for (col_type c = 0; c < tabular.ncols(); ++c) {
3224 if (tabular.isPartOfMultiColumn(r, c)
3225 || tabular.isPartOfMultiRow(r, c))
3226 // multicolumn or multirow cell, but not first one
3228 idx_type const cell = tabular.cellIndex(r, c);
3232 if (tabular.cell_info[r][c].multicolumn ==
3233 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3234 p_width = tabular.cellInfo(cell).p_width;
3236 p_width = tabular.column_info[c].p_width;
3237 if (!p_width.zero())
3238 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3239 tabular.cellInset(cell)->metrics(m, dim);
3240 if (!p_width.zero())
3241 dim.wid = m.base.textwidth;
3242 tabular.setCellWidth(cell, dim.wid);
3243 maxAsc = max(maxAsc, dim.asc);
3244 maxDesc = max(maxDesc, dim.des);
3246 // store the height for every cell
3247 // this is later needed in InsetTabular::draw to determine the valignment
3248 tabular.cell_info[r][c].height = dim.asc + dim.des;
3249 tabular.cell_info[r][c].ascent = dim.asc;
3250 tabular.cell_info[r][c].descent = dim.des;
3252 int const top_space = tabular.row_info[r].top_space_default ?
3253 default_line_space :
3254 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3255 tabular.setRowAscent(r, maxAsc + ADD_TO_HEIGHT + top_space);
3256 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3257 default_line_space :
3258 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3259 tabular.setRowDescent(r, maxDesc + ADD_TO_HEIGHT + bottom_space);
3261 tabular.updateColumnWidths();
3262 dim.asc = tabular.rowAscent(0);
3263 dim.des = tabular.height() - dim.asc;
3264 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3268 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3271 if (&cur.inset() == this && cur.selection()) {
3272 if (cur.selIsMultiCell()) {
3275 getSelection(cur, rs, re, cs, ce);
3277 idx_type const cell = tabular.cellIndex(row, col);
3278 col_type const cspan = tabular.columnSpan(cell);
3279 row_type const rspan = tabular.rowSpan(cell);
3280 if (col + cspan - 1 >= cs && col <= ce
3281 && row + rspan - 1 >= rs && row <= re)
3284 if (col == tabular.cellColumn(cur.idx())
3285 && row == tabular.cellRow(cur.idx())) {
3286 CursorSlice const & beg = cur.selBegin();
3287 CursorSlice const & end = cur.selEnd();
3289 if ((end.lastpos() > 0 || end.lastpit() > 0)
3290 && end.pos() == end.lastpos() && beg.pos() == 0
3291 && end.pit() == end.lastpit() && beg.pit() == 0)
3299 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3301 x += scx_ + ADD_TO_TABULAR_WIDTH;
3303 BufferView * bv = pi.base.bv;
3304 Cursor & cur = pi.base.bv->cursor();
3307 // FIXME: As the full background is painted in drawSelection(),
3308 // we have no choice but to do a full repaint for the Text cells.
3309 pi.full_repaint = true;
3311 bool const original_selection_state = pi.selected;
3315 first_visible_cell = Tabular::npos;
3317 // determine the highest cell because its valignment sets the row valignment
3318 // also store its height
3319 for (row_type r = 0; r < tabular.nrows(); ++r) {
3320 for (col_type c = 0; c < tabular.ncols(); ++c) {
3321 if (tabular.cell_info[r][c].height >= tabular.row_info[r].maxheight) {
3322 tabular.row_info[r].maxheight = tabular.cell_info[r][c].height;
3323 switch (tabular.getVAlignment(tabular.cellIndex(r, c))) {
3324 case Tabular::LYX_VALIGN_TOP:
3325 tabular.row_info[r].valignment = Tabular::LYX_VALIGN_TOP;
3327 case Tabular::LYX_VALIGN_MIDDLE:
3328 tabular.row_info[r].valignment = Tabular::LYX_VALIGN_MIDDLE;
3330 case Tabular::LYX_VALIGN_BOTTOM:
3331 tabular.row_info[r].valignment = Tabular::LYX_VALIGN_BOTTOM;
3338 // step over all cells
3339 for (row_type r = 0; r < tabular.nrows(); ++r) {
3341 for (col_type c = 0; c < tabular.ncols(); ++c) {
3342 if (tabular.isPartOfMultiColumn(r, c))
3345 idx = tabular.cellIndex(r, c);
3347 if (tabular.isPartOfMultiRow(r, c)) {
3348 nx += tabular.columnWidth(idx);
3352 if (first_visible_cell == Tabular::npos)
3353 first_visible_cell = idx;
3355 pi.selected |= isCellSelected(cur, r, c);
3357 // set the position for the vertical alignment
3358 // if the cell is not the highest, align it according to the row valignment
3359 // and according to its own valignment
3360 // Note: The fractions of the scent values were found by testing
3361 // there is no bettwer way for the calculation because of LyX's definition
3362 // of ascent and descent. The correct way would be to calculate based
3363 // on the text height, the line space and paragraph separation.
3365 if (tabular.cell_info[r][c].height < tabular.row_info[r].maxheight) {
3366 switch (tabular.row_info[r].valignment) {
3367 case Tabular::LYX_VALIGN_TOP:
3368 switch (tabular.getVAlignment(idx)) {
3369 case Tabular::LYX_VALIGN_TOP:
3371 case Tabular::LYX_VALIGN_MIDDLE:
3372 cy += - tabular.cell_info[r][c].descent / 2
3373 + 0.3 * tabular.cell_info[r][c].ascent;
3374 // FIXME the row also needs to be increased at the upper border
3376 case Tabular::LYX_VALIGN_BOTTOM:
3377 cy += - tabular.cell_info[r][c].descent
3378 + 0.6 * tabular.cell_info[r][c].ascent;
3379 // FIXME the row also needs to be increased at the upper border
3383 case Tabular::LYX_VALIGN_MIDDLE:
3384 switch (tabular.getVAlignment(idx)) {
3385 case Tabular::LYX_VALIGN_TOP:
3386 cy += tabular.row_info[r].maxheight / 2
3387 - 0.75 * tabular.cell_info[r][c].ascent;
3389 case Tabular::LYX_VALIGN_MIDDLE:
3390 cy += tabular.row_info[r].maxheight / 2
3391 - tabular.cell_info[r][c].descent / 2
3392 - 0.5 * tabular.cell_info[r][c].ascent;
3394 case Tabular::LYX_VALIGN_BOTTOM:
3395 cy += tabular.row_info[r].maxheight / 2
3396 - tabular.cell_info[r][c].descent
3397 - 0.2 * tabular.cell_info[r][c].ascent;
3401 case Tabular::LYX_VALIGN_BOTTOM:
3402 switch (tabular.getVAlignment(idx)) {
3403 case Tabular::LYX_VALIGN_TOP:
3404 cy += tabular.row_info[r].maxheight
3405 - 1.5 * tabular.cell_info[r][c].ascent;
3407 case Tabular::LYX_VALIGN_MIDDLE:
3408 cy += tabular.row_info[r].maxheight
3409 - tabular.cell_info[r][c].descent / 2
3410 - 1.2 * tabular.cell_info[r][c].ascent;
3411 // FIXME the row also needs to be increased at the lower border
3413 case Tabular::LYX_VALIGN_BOTTOM:
3414 cy += tabular.row_info[r].maxheight
3415 - tabular.cell_info[r][c].descent
3416 - 0.9 * tabular.cell_info[r][c].ascent;
3417 // FIXME the row also needs to be increased at the lower border
3424 int const cx = nx + tabular.textHOffset(idx);
3425 // Cache the Inset position.
3426 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3427 cell(idx)->draw(pi, cx, cy);
3428 drawCellLines(pi.pain, nx, y, r, idx, pi.change_);
3429 nx += tabular.columnWidth(idx);
3430 pi.selected = original_selection_state;
3433 if (r + 1 < tabular.nrows())
3434 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3435 + tabular.interRowSpace(r + 1);
3440 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3442 Cursor & cur = pi.base.bv->cursor();
3445 x += scx_ + ADD_TO_TABULAR_WIDTH;
3447 // FIXME: it is wrong to completely paint the background
3448 // if we want to do single row painting.
3450 // Paint background of current tabular
3451 int const w = tabular.width();
3452 int const h = tabular.height();
3453 int yy = y - tabular.rowAscent(0);
3454 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3456 if (!cur.selection())
3458 if (&cur.inset() != this)
3463 bool const full_cell_selected = isCellSelected(cur,
3464 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3466 if (cur.selIsMultiCell() || full_cell_selected) {
3467 for (row_type r = 0; r < tabular.nrows(); ++r) {
3469 for (col_type c = 0; c < tabular.ncols(); ++c) {
3470 if (tabular.isPartOfMultiColumn(r, c))
3473 idx_type const cell = tabular.cellIndex(r, c);
3475 if (tabular.isPartOfMultiRow(r, c)) {
3476 xx += tabular.columnWidth(cell);
3479 int const w = tabular.columnWidth(cell);
3480 int const h = tabular.cellHeight(cell);
3481 int const yy = y - tabular.rowAscent(r);
3482 if (isCellSelected(cur, r, c))
3483 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3486 if (r + 1 < tabular.nrows())
3487 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3488 + tabular.interRowSpace(r + 1);
3492 x += cellXPos(cur.idx());
3493 x += tabular.textHOffset(cur.idx());
3494 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3499 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3500 row_type row, idx_type cell, Change const & change) const
3502 y -= tabular.rowAscent(row);
3503 int const w = tabular.columnWidth(cell);
3504 int const h = tabular.cellHeight(cell);
3505 Color linecolor = change.changed() ? change.color() : Color_tabularline;
3506 Color gridcolor = change.changed() ? change.color() : Color_tabularonoffline;
3509 bool drawline = tabular.topLine(cell)
3510 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3511 pain.line(x, y, x + w, y,
3512 drawline ? linecolor : gridcolor,
3513 drawline ? Painter::line_solid : Painter::line_onoffdash);
3516 drawline = tabular.bottomLine(cell);
3517 pain.line(x, y + h, x + w, y + h,
3518 drawline ? linecolor : gridcolor,
3519 drawline ? Painter::line_solid : Painter::line_onoffdash);
3522 col_type const col = tabular.cellColumn(cell);
3523 drawline = tabular.leftLine(cell)
3524 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3525 pain.line(x, y, x, y + h,
3526 drawline ? linecolor : gridcolor,
3527 drawline ? Painter::line_solid : Painter::line_onoffdash);
3530 x -= tabular.interColumnSpace(cell);
3531 drawline = tabular.rightLine(cell)
3532 || (col + 1 < tabular.ncols()
3533 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3534 pain.line(x + w, y, x + w, y + h,
3535 drawline ? linecolor : gridcolor,
3536 drawline ? Painter::line_solid : Painter::line_onoffdash);
3540 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3542 //lyxerr << "InsetTabular::edit: " << this << endl;
3544 cur.setSelection(false);
3547 if (isRightToLeft(cur))
3548 cur.idx() = tabular.getLastCellInRow(0);
3554 if (isRightToLeft(cur))
3555 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3557 cur.idx() = tabular.numberofcells - 1;
3559 cur.pos() = cur.lastpos(); // FIXME crude guess
3561 cur.setCurrentFont();
3562 // FIXME: this accesses the position cache before it is initialized
3564 //cur.bv().fitCursor();
3568 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3570 // In a longtable, tell captions what the current float is
3571 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3572 string const saveflt = cnts.current_float();
3573 if (tabular.is_long_tabular)
3574 cnts.current_float("table");
3576 ParIterator it2 = it;
3578 size_t const end = it2.nargs();
3579 for ( ; it2.idx() < end; it2.top().forwardIdx())
3580 buffer().updateBuffer(it2, utype);
3583 if (tabular.is_long_tabular)
3584 cnts.current_float(saveflt);
3588 void InsetTabular::addToToc(DocIterator const & cpit)
3590 DocIterator dit = cpit;
3592 size_t const end = dit.nargs();
3593 for ( ; dit.idx() < end; dit.top().forwardIdx())
3594 cell(dit.idx())->addToToc(dit);
3598 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3600 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3601 << "\n cur:" << cur);
3602 CursorSlice sl = cur.top();
3603 Cursor & bvcur = cur.bv().cursor();
3605 switch (cmd.action) {
3607 case LFUN_MOUSE_PRESS: {
3608 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3610 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3611 || cmd.x > xo(cur.bv()) + tabular.width()) {
3612 row_type r = rowFromY(cur, cmd.y);
3613 cur.idx() = tabular.getFirstCellInRow(r);
3616 cur.idx() = tabular.getLastCellInRow(r);
3617 cur.pos() = cur.lastpos();
3618 cur.setSelection(true);
3624 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3625 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3626 || cmd.y > y0 + tabular.height()) {
3627 col_type c = columnFromX(cur, cmd.x);
3628 cur.idx() = tabular.cellIndex(0, c);
3631 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3632 cur.pos() = cur.lastpos();
3633 cur.setSelection(true);
3638 // do not reset cursor/selection if we have selected
3639 // some cells (bug 2715).
3640 if (cmd.button() == mouse_button::button3
3641 && &bvcur.selBegin().inset() == this
3642 && bvcur.selIsMultiCell())
3645 // Let InsetTableCell do it
3646 cell(cur.idx())->dispatch(cur, cmd);
3649 case LFUN_MOUSE_MOTION:
3650 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3651 if (cmd.button() == mouse_button::button1) {
3652 // only accept motions to places not deeper nested than the real anchor
3653 if (!bvcur.anchor_.hasPart(cur)) {
3657 // select (additional) row
3659 row_type r = rowFromY(cur, cmd.y);
3660 cur.idx() = tabular.getLastCellInRow(r);
3661 // we need to reset the cursor's pit and pos now, as the old ones
3662 // may no longer be valid.
3665 bvcur.setCursor(cur);
3666 bvcur.setSelection(true);
3669 // select (additional) column
3671 col_type c = columnFromX(cur, cmd.x);
3672 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3673 // we need to reset the cursor's pit and pos now, as the old ones
3674 // may no longer be valid.
3677 bvcur.setCursor(cur);
3678 bvcur.setSelection(true);
3681 // only update if selection changes
3682 if (bvcur.idx() == cur.idx() &&
3683 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3685 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3686 bvcur.setCursor(cur);
3687 bvcur.setSelection(true);
3688 // if this is a multicell selection, we just set the cursor to
3689 // the beginning of the cell's text.
3690 if (bvcur.selIsMultiCell()) {
3691 bvcur.pit() = bvcur.lastpit();
3692 bvcur.pos() = bvcur.lastpos();
3697 case LFUN_MOUSE_RELEASE:
3702 case LFUN_CELL_BACKWARD:
3704 cur.setSelection(false);
3707 case LFUN_CELL_FORWARD:
3709 cur.setSelection(false);
3712 case LFUN_CHAR_FORWARD_SELECT:
3713 case LFUN_CHAR_FORWARD:
3714 case LFUN_CHAR_BACKWARD_SELECT:
3715 case LFUN_CHAR_BACKWARD:
3716 case LFUN_CHAR_RIGHT_SELECT:
3717 case LFUN_CHAR_RIGHT:
3718 case LFUN_CHAR_LEFT_SELECT:
3719 case LFUN_CHAR_LEFT: {
3720 // determine whether we move to next or previous cell, where to enter
3721 // the new cell from, and which command to "finish" (i.e., exit the
3724 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3725 FuncCode finish_lfun;
3727 if (cmd.action == LFUN_CHAR_FORWARD
3728 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3730 finish_lfun = LFUN_FINISHED_FORWARD;
3732 else if (cmd.action == LFUN_CHAR_BACKWARD
3733 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3735 finish_lfun = LFUN_FINISHED_BACKWARD;
3737 // LEFT or RIGHT commands --- the interpretation will depend on the
3738 // table's direction.
3740 bool const right = cmd.action == LFUN_CHAR_RIGHT
3741 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3742 next_cell = isRightToLeft(cur) != right;
3744 if (lyxrc.visual_cursor)
3745 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3747 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3750 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3751 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3752 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3753 cmd.action == LFUN_CHAR_LEFT_SELECT;
3755 // If we have a multicell selection or we're
3756 // not doing some LFUN_*_SELECT thing anyway...
3757 if (!cur.selIsMultiCell() || !select) {
3758 col_type const c = tabular.cellColumn(cur.idx());
3759 row_type const r = tabular.cellRow(cur.idx());
3760 // Are we trying to select the whole cell and is the whole cell
3761 // not yet selected?
3762 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3763 ((next_cell && cur.pit() == cur.lastpit()
3764 && cur.pos() == cur.lastpos())
3765 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3767 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3769 // ...try to dispatch to the cell's inset.
3770 cell(cur.idx())->dispatch(cur, cmd);
3772 // When we already have a selection we want to select the whole cell
3773 // before going to the next cell.
3774 if (select_whole && !empty_cell){
3775 getText(cur.idx())->selectAll(cur);
3780 // FIXME: When we support the selection of an empty cell, remove
3781 // the !empty_cell from this condition. For now we jump to the next
3782 // cell if the current cell is empty.
3783 if (cur.result().dispatched() && !empty_cell)
3787 // move to next/prev cell, as appropriate
3788 // note that we will always do this if we're selecting and we have
3789 // a multicell selection
3790 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3791 << " cell from: " << int(entry_from));
3793 moveNextCell(cur, entry_from);
3795 movePrevCell(cur, entry_from);
3796 // if we're exiting the table, call the appropriate FINISHED lfun
3797 if (sl == cur.top())
3798 cmd = FuncRequest(finish_lfun);
3805 case LFUN_DOWN_SELECT:
3807 if (!(cur.selection() && cur.selIsMultiCell()))
3808 cell(cur.idx())->dispatch(cur, cmd);
3810 cur.dispatched(); // override the cell's decision
3811 if (sl == cur.top()) {
3812 // if our Text didn't do anything to the cursor
3813 // then we try to put the cursor into the cell below
3814 // setting also the right targetX.
3815 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3816 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3817 cur.idx() = tabular.cellBelow(cur.idx());
3819 TextMetrics const & tm =
3820 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3821 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3822 cur.setCurrentFont();
3825 if (sl == cur.top()) {
3826 // we trick it to go to forward after leaving the
3828 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3831 if (cur.selIsMultiCell()) {
3832 cur.pit() = cur.lastpit();
3833 cur.pos() = cur.lastpos();
3834 cur.setCurrentFont();
3839 case LFUN_UP_SELECT:
3841 if (!(cur.selection() && cur.selIsMultiCell()))
3842 cell(cur.idx())->dispatch(cur, cmd);
3843 cur.dispatched(); // override the cell's decision
3844 if (sl == cur.top()) {
3845 // if our Text didn't do anything to the cursor
3846 // then we try to put the cursor into the cell above
3847 // setting also the right targetX.
3848 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3849 if (tabular.cellRow(cur.idx()) != 0) {
3850 cur.idx() = tabular.cellAbove(cur.idx());
3851 cur.pit() = cur.lastpit();
3852 Text const * text = cell(cur.idx())->getText(0);
3853 TextMetrics const & tm = cur.bv().textMetrics(text);
3854 ParagraphMetrics const & pm =
3855 tm.parMetrics(cur.lastpit());
3856 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3857 cur.setCurrentFont();
3860 if (sl == cur.top()) {
3861 cmd = FuncRequest(LFUN_UP);
3864 if (cur.selIsMultiCell()) {
3866 cur.pos() = cur.lastpos();
3867 cur.setCurrentFont();
3872 // case LFUN_SCREEN_DOWN: {
3873 // //if (hasSelection())
3874 // // cur.selection() = false;
3875 // col_type const col = tabular.cellColumn(cur.idx());
3876 // int const t = cur.bv().top_y() + cur.bv().height();
3877 // if (t < yo() + tabular.getHeightOfTabular()) {
3878 // cur.bv().scrollDocView(t);
3879 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3881 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3888 // case LFUN_SCREEN_UP: {
3889 // //if (hasSelection())
3890 // // cur.selection() = false;
3891 // col_type const col = tabular.cellColumn(cur.idx());
3892 // int const t = cur.bv().top_y() + cur.bv().height();
3894 // cur.bv().scrollDocView(t);
3898 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3902 // cur.par() = cur.lastpar();
3903 // cur.pos() = cur.lastpos();
3907 case LFUN_LAYOUT_TABULAR:
3908 cur.bv().showDialog("tabular");
3911 case LFUN_INSET_MODIFY:
3912 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3916 // insert file functions
3917 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3918 case LFUN_FILE_INSERT_PLAINTEXT:
3920 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3921 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3922 FileName(to_utf8(cmd.argument())));
3925 cur.recordUndoInset(INSERT_UNDO);
3926 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3927 // content has been replaced,
3928 // so cursor might be invalid
3929 cur.pos() = cur.lastpos();
3930 cur.pit() = cur.lastpit();
3931 bvcur.setCursor(cur);
3938 if (cur.selIsMultiCell()) {
3939 if (copySelection(cur)) {
3940 cur.recordUndoInset(DELETE_UNDO);
3944 cell(cur.idx())->dispatch(cur, cmd);
3947 case LFUN_SELF_INSERT:
3948 if (cur.selIsMultiCell()) {
3949 cur.recordUndoInset(DELETE_UNDO);
3952 cell(cur.idx())->dispatch(cur, cmd);
3955 case LFUN_CHAR_DELETE_BACKWARD:
3956 case LFUN_CHAR_DELETE_FORWARD:
3957 if (cur.selIsMultiCell()) {
3958 cur.recordUndoInset(DELETE_UNDO);
3961 cell(cur.idx())->dispatch(cur, cmd);
3965 if (!cur.selection())
3967 if (cur.selIsMultiCell()) {
3971 cell(cur.idx())->dispatch(cur, cmd);
3974 case LFUN_CLIPBOARD_PASTE:
3975 case LFUN_PRIMARY_SELECTION_PASTE: {
3976 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3977 theClipboard().getAsText() :
3978 theSelection().get();
3981 // pass to InsertPlaintextString, but
3982 // only if we have multi-cell content
3983 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3984 cur.recordUndoInset(INSERT_UNDO);
3985 if (insertPlaintextString(cur.bv(), clip, false)) {
3986 // content has been replaced,
3987 // so cursor might be invalid
3988 cur.pos() = cur.lastpos();
3989 cur.pit() = cur.lastpit();
3990 bvcur.setCursor(cur);
3994 // Let the cell handle normal text
3995 cell(cur.idx())->dispatch(cur, cmd);
4000 if (!tabularStackDirty()) {
4001 if (!cur.selIsMultiCell())
4002 cell(cur.idx())->dispatch(cur, cmd);
4005 if (theClipboard().isInternal() ||
4006 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
4007 cur.recordUndoInset(INSERT_UNDO);
4008 pasteClipboard(cur);
4012 case LFUN_FONT_EMPH:
4013 case LFUN_FONT_BOLD:
4014 case LFUN_FONT_BOLDSYMBOL:
4015 case LFUN_FONT_ROMAN:
4016 case LFUN_FONT_NOUN:
4017 case LFUN_FONT_ITAL:
4018 case LFUN_FONT_FRAK:
4019 case LFUN_FONT_TYPEWRITER:
4020 case LFUN_FONT_SANS:
4021 case LFUN_TEXTSTYLE_APPLY:
4022 case LFUN_TEXTSTYLE_UPDATE:
4023 case LFUN_FONT_SIZE:
4024 case LFUN_FONT_UNDERLINE:
4025 case LFUN_FONT_STRIKEOUT:
4026 case LFUN_FONT_UULINE:
4027 case LFUN_FONT_UWAVE:
4029 case LFUN_WORD_CAPITALIZE:
4030 case LFUN_WORD_UPCASE:
4031 case LFUN_WORD_LOWCASE:
4032 case LFUN_CHARS_TRANSPOSE:
4033 if (cur.selIsMultiCell()) {
4036 getSelection(cur, rs, re, cs, ce);
4037 Cursor tmpcur = cur;
4038 for (row_type r = rs; r <= re; ++r) {
4039 for (col_type c = cs; c <= ce; ++c) {
4040 // cursor follows cell:
4041 tmpcur.idx() = tabular.cellIndex(r, c);
4042 // select this cell only:
4045 tmpcur.resetAnchor();
4046 tmpcur.pit() = tmpcur.lastpit();
4047 tmpcur.pos() = tmpcur.top().lastpos();
4048 tmpcur.setCursor(tmpcur);
4049 tmpcur.setSelection();
4050 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4055 cell(cur.idx())->dispatch(cur, cmd);
4059 case LFUN_INSET_SETTINGS:
4060 // relay this lfun to Inset, not to the cell.
4061 Inset::doDispatch(cur, cmd);
4065 // we try to handle this event in the insets dispatch function.
4066 cell(cur.idx())->dispatch(cur, cmd);
4072 // function sets an object as defined in func_status.h:
4073 // states OK, Unknown, Disabled, On, Off.
4074 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4075 FuncStatus & status) const
4077 switch (cmd.action) {
4078 case LFUN_INSET_MODIFY: {
4079 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4082 string const s = cmd.getArg(1);
4083 // FIXME: We only check for the very first argument...
4084 int action = Tabular::LAST_ACTION;
4086 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4087 if (tabularFeature[i].feature == s) {
4088 action = tabularFeature[i].action;
4092 if (action == Tabular::LAST_ACTION) {
4094 status.unknown(true);
4098 string const argument = cmd.getLongArg(2);
4100 row_type sel_row_start = 0;
4101 row_type sel_row_end = 0;
4102 col_type sel_col_start = 0;
4103 col_type sel_col_end = 0;
4104 Tabular::ltType dummyltt;
4107 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4110 case Tabular::SET_PWIDTH:
4111 case Tabular::SET_MPWIDTH:
4112 case Tabular::SET_SPECIAL_COLUMN:
4113 case Tabular::SET_SPECIAL_MULTICOLUMN:
4114 case Tabular::SET_SPECIAL_MULTIROW:
4115 case Tabular::APPEND_ROW:
4116 case Tabular::APPEND_COLUMN:
4117 case Tabular::DELETE_ROW:
4118 case Tabular::DELETE_COLUMN:
4119 case Tabular::COPY_ROW:
4120 case Tabular::COPY_COLUMN:
4121 case Tabular::SET_TOP_SPACE:
4122 case Tabular::SET_BOTTOM_SPACE:
4123 case Tabular::SET_INTERLINE_SPACE:
4127 case Tabular::MULTICOLUMN:
4128 // If a row is set as longtable caption, it must not be allowed
4129 // to unset that this row is a multicolumn.
4130 status.setEnabled(sel_row_start == sel_row_end
4131 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4132 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4135 case Tabular::MULTIROW:
4136 // If a row is set as longtable caption, it must not be allowed
4137 // to unset that this row is a multirow.
4138 status.setEnabled(sel_col_start == sel_col_end
4139 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4140 status.setOnOff(tabular.isMultiRow(cur.idx()));
4143 case Tabular::SET_ALL_LINES:
4144 case Tabular::UNSET_ALL_LINES:
4145 case Tabular::SET_BORDER_LINES:
4146 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4149 case Tabular::SET_LINE_TOP:
4150 case Tabular::SET_LINE_BOTTOM:
4151 case Tabular::SET_LINE_LEFT:
4152 case Tabular::SET_LINE_RIGHT:
4153 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4156 case Tabular::TOGGLE_LINE_TOP:
4157 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4158 status.setOnOff(tabular.topLine(cur.idx()));
4161 case Tabular::TOGGLE_LINE_BOTTOM:
4162 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4163 status.setOnOff(tabular.bottomLine(cur.idx()));
4166 case Tabular::TOGGLE_LINE_LEFT:
4167 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4168 status.setOnOff(tabular.leftLine(cur.idx()));
4171 case Tabular::TOGGLE_LINE_RIGHT:
4172 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4173 status.setOnOff(tabular.rightLine(cur.idx()));
4176 case Tabular::M_ALIGN_LEFT:
4178 case Tabular::ALIGN_LEFT:
4179 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4180 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4183 case Tabular::M_ALIGN_RIGHT:
4185 case Tabular::ALIGN_RIGHT:
4186 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4187 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4190 case Tabular::M_ALIGN_CENTER:
4192 case Tabular::ALIGN_CENTER:
4193 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4194 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4197 case Tabular::ALIGN_BLOCK:
4198 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4199 && !tabular.isMultiRow(cur.idx()));
4200 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4203 case Tabular::M_VALIGN_TOP:
4205 case Tabular::VALIGN_TOP:
4206 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4207 && !tabular.isMultiRow(cur.idx()));
4209 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4212 case Tabular::M_VALIGN_BOTTOM:
4214 case Tabular::VALIGN_BOTTOM:
4215 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4216 && !tabular.isMultiRow(cur.idx()));
4218 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4221 case Tabular::M_VALIGN_MIDDLE:
4223 case Tabular::VALIGN_MIDDLE:
4224 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4225 && !tabular.isMultiRow(cur.idx()));
4227 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4230 case Tabular::SET_LONGTABULAR:
4231 status.setOnOff(tabular.is_long_tabular);
4234 case Tabular::UNSET_LONGTABULAR:
4235 status.setOnOff(!tabular.is_long_tabular);
4238 case Tabular::TOGGLE_ROTATE_TABULAR:
4239 case Tabular::SET_ROTATE_TABULAR:
4240 status.setOnOff(tabular.rotate);
4243 case Tabular::TABULAR_VALIGN_TOP:
4244 status.setOnOff(tabular.tabular_valignment
4245 == Tabular::LYX_VALIGN_TOP);
4247 case Tabular::TABULAR_VALIGN_MIDDLE:
4248 status.setOnOff(tabular.tabular_valignment
4249 == Tabular::LYX_VALIGN_MIDDLE);
4251 case Tabular::TABULAR_VALIGN_BOTTOM:
4252 status.setOnOff(tabular.tabular_valignment
4253 == Tabular::LYX_VALIGN_BOTTOM);
4256 case Tabular::LONGTABULAR_ALIGN_LEFT:
4257 status.setOnOff(tabular.longtabular_alignment
4258 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4260 case Tabular::LONGTABULAR_ALIGN_CENTER:
4261 status.setOnOff(tabular.longtabular_alignment
4262 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4264 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4265 status.setOnOff(tabular.longtabular_alignment
4266 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4269 case Tabular::UNSET_ROTATE_TABULAR:
4270 status.setOnOff(!tabular.rotate);
4273 case Tabular::TOGGLE_ROTATE_CELL:
4274 case Tabular::SET_ROTATE_CELL:
4275 status.setOnOff(!oneCellHasRotationState(false,
4276 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4279 case Tabular::UNSET_ROTATE_CELL:
4280 status.setOnOff(!oneCellHasRotationState(true,
4281 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4284 case Tabular::SET_USEBOX:
4285 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4288 // every row can only be one thing:
4289 // either a footer or header or caption
4290 case Tabular::SET_LTFIRSTHEAD:
4291 status.setEnabled(sel_row_start == sel_row_end
4292 && !tabular.ltCaption(sel_row_start));
4293 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4296 case Tabular::UNSET_LTFIRSTHEAD:
4297 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4300 case Tabular::SET_LTHEAD:
4301 status.setEnabled(sel_row_start == sel_row_end
4302 && !tabular.ltCaption(sel_row_start));
4303 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4306 case Tabular::UNSET_LTHEAD:
4307 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4310 case Tabular::SET_LTFOOT:
4311 status.setEnabled(sel_row_start == sel_row_end
4312 && !tabular.ltCaption(sel_row_start));
4313 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4316 case Tabular::UNSET_LTFOOT:
4317 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4320 case Tabular::SET_LTLASTFOOT:
4321 status.setEnabled(sel_row_start == sel_row_end
4322 && !tabular.ltCaption(sel_row_start));
4323 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4326 case Tabular::UNSET_LTLASTFOOT:
4327 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4330 case Tabular::SET_LTNEWPAGE:
4331 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4334 // only one row can be the caption
4335 // and a multirow cannot be set as caption
4336 case Tabular::TOGGLE_LTCAPTION:
4337 status.setEnabled(sel_row_start == sel_row_end
4338 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4339 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4340 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4341 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4342 && (!tabular.haveLTCaption()
4343 || tabular.ltCaption(sel_row_start))
4344 && !tabular.isMultiRow(sel_row_start));
4345 status.setOnOff(tabular.ltCaption(sel_row_start));
4348 case Tabular::SET_BOOKTABS:
4349 status.setOnOff(tabular.use_booktabs);
4352 case Tabular::UNSET_BOOKTABS:
4353 status.setOnOff(!tabular.use_booktabs);
4358 status.setEnabled(false);
4364 // These are only enabled inside tabular
4365 case LFUN_CELL_BACKWARD:
4366 case LFUN_CELL_FORWARD:
4367 status.setEnabled(true);
4370 // disable these with multiple cells selected
4371 case LFUN_INSET_INSERT:
4372 case LFUN_TABULAR_INSERT:
4373 case LFUN_FLEX_INSERT:
4374 case LFUN_FLOAT_INSERT:
4375 case LFUN_FLOAT_WIDE_INSERT:
4376 case LFUN_FOOTNOTE_INSERT:
4377 case LFUN_MARGINALNOTE_INSERT:
4378 case LFUN_MATH_INSERT:
4379 case LFUN_MATH_MODE:
4380 case LFUN_MATH_MUTATE:
4381 case LFUN_MATH_DISPLAY:
4382 case LFUN_NOTE_INSERT:
4383 case LFUN_OPTIONAL_INSERT:
4384 case LFUN_BOX_INSERT:
4385 case LFUN_BRANCH_INSERT:
4386 case LFUN_PHANTOM_INSERT:
4387 case LFUN_WRAP_INSERT:
4388 case LFUN_ERT_INSERT: {
4389 if (cur.selIsMultiCell()) {
4390 status.setEnabled(false);
4393 return cell(cur.idx())->getStatus(cur, cmd, status);
4396 // disable in non-fixed-width cells
4397 case LFUN_NEWLINE_INSERT:
4398 case LFUN_BREAK_PARAGRAPH: {
4399 if (tabular.getPWidth(cur.idx()).zero()) {
4400 status.setEnabled(false);
4403 return cell(cur.idx())->getStatus(cur, cmd, status);
4406 case LFUN_NEWPAGE_INSERT:
4407 status.setEnabled(false);
4411 if (tabularStackDirty() && theClipboard().isInternal()) {
4412 if (cur.selIsMultiCell()) {
4415 getSelection(cur, rs, re, cs, ce);
4416 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4417 && paste_tabular->nrows() == re - rs + 1)
4418 status.setEnabled(true);
4420 status.setEnabled(false);
4421 status.message(_("Selection size should match clipboard content."));
4424 status.setEnabled(true);
4427 return cell(cur.idx())->getStatus(cur, cmd, status);
4429 case LFUN_INSET_SETTINGS:
4430 // relay this lfun to Inset, not to the cell.
4431 return Inset::getStatus(cur, cmd, status);
4434 // we try to handle this event in the insets dispatch function.
4435 return cell(cur.idx())->getStatus(cur, cmd, status);
4441 Inset::DisplayType InsetTabular::display() const
4443 if (tabular.is_long_tabular) {
4444 switch (tabular.longtabular_alignment) {
4445 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4447 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4449 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4459 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4461 return tabular.latex(os, runparams);
4465 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4467 os << '\n'; // output table on a new line
4468 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4469 tabular.plaintext(os, runparams, dp, false, 0);
4470 return PLAINTEXT_NEWLINE;
4474 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4479 // FIXME: Why not pass a proper DocIterator here?
4481 // if the table is inside a float it doesn't need the informaltable
4482 // wrapper. Search for it.
4483 for (master = owner(); master; master = master->owner())
4484 if (master->lyxCode() == FLOAT_CODE)
4489 os << "<informaltable>";
4492 ret += tabular.docbook(os, runparams);
4494 os << "</informaltable>";
4501 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4504 // It'd be better to be able to get this from an InsetLayout, but at present
4505 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4506 xs << html::StartTag("table");
4507 docstring ret = tabular.xhtml(xs, rp);
4508 xs << html::EndTag("table");
4513 void InsetTabular::validate(LaTeXFeatures & features) const
4515 tabular.validate(features);
4517 // It'd be better to be able to get this from an InsetLayout, but at present
4518 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4519 if (features.runparams().flavor == OutputParams::HTML)
4520 features.addPreambleSnippet("<style type=\"text/css\">\n"
4521 "table { border: 1px solid black; display: inline-block; }\n"
4522 "td { border: 1px solid black; padding: 0.5ex; }\n"
4527 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4529 return tabular.cellInset(idx);
4533 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4535 return tabular.cellInset(idx);
4539 void InsetTabular::cursorPos(BufferView const & bv,
4540 CursorSlice const & sl, bool boundary, int & x, int & y) const
4542 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4544 int const row = tabular.cellRow(sl.idx());
4545 int const col = tabular.cellColumn(sl.idx());
4547 // y offset correction
4548 for (int r = 0; r < row; ++r)
4549 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4550 + tabular.interRowSpace(r + 1);
4552 y += tabular.textVOffset(sl.idx());
4554 // x offset correction
4555 for (int c = 0; c < col; ++c)
4556 x += tabular.column_info[c].width;
4558 x += tabular.textHOffset(sl.idx());
4559 x += ADD_TO_TABULAR_WIDTH;
4564 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4568 Inset const & inset = *tabular.cellInset(cell);
4569 Point o = bv.coordCache().getInsets().xy(&inset);
4570 int const xbeg = o.x_ - tabular.textHOffset(cell);
4571 int const xend = xbeg + tabular.columnWidth(cell);
4572 row_type const row = tabular.cellRow(cell);
4573 int const ybeg = o.y_ - tabular.rowAscent(row)
4574 - tabular.interRowSpace(row);
4575 int const yend = ybeg + tabular.cellHeight(cell);
4587 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4588 // << " ybeg=" << ybeg << " yend=" << yend
4589 // << " xx=" << xx << " yy=" << yy
4590 // << " dist=" << xx + yy << endl;
4595 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4597 //lyxerr << "InsetTabular::editXY: " << this << endl;
4598 cur.setSelection(false);
4600 cur.idx() = getNearestCell(cur.bv(), x, y);
4602 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4606 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4608 cur.idx() = getNearestCell(cur.bv(), x, y);
4609 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4613 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4615 idx_type idx_min = 0;
4616 int dist_min = numeric_limits<int>::max();
4617 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4618 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4619 int const d = dist(bv, i, x, y);
4630 int InsetTabular::cellXPos(idx_type const cell) const
4632 col_type col = tabular.cellColumn(cell);
4634 for (col_type c = 0; c < col; ++c)
4635 lx += tabular.columnWidth(c);
4641 void InsetTabular::resetPos(Cursor & cur) const
4643 BufferView & bv = cur.bv();
4644 int const maxwidth = bv.workWidth();
4646 int const scx_old = scx_;
4647 int const i = cur.find(this);
4652 int const X2 = maxwidth;
4653 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4654 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4655 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4658 scx_ = X1 + 20 - x1;
4660 scx_ = X2 - 20 - x2;
4665 // only update if offset changed
4666 if (scx_ != scx_old)
4667 cur.updateFlags(Update::Force | Update::FitCursor);
4671 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4673 row_type const row = tabular.cellRow(cur.idx());
4674 col_type const col = tabular.cellColumn(cur.idx());
4676 if (isRightToLeft(cur)) {
4677 if (tabular.cellColumn(cur.idx()) == 0) {
4678 if (row == tabular.nrows() - 1)
4680 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4685 cur.idx() = tabular.getLastCellInRow(row - 1);
4687 cur.idx() = tabular.cellIndex(row, col - 1);
4690 if (tabular.isLastCell(cur.idx()))
4692 if (cur.idx() == tabular.getLastCellInRow(row))
4693 cur.idx() = tabular.cellIndex(row + 1, 0);
4695 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4696 cur.idx() = tabular.cellIndex(row, colnextcell);
4700 cur.boundary(false);
4702 if (cur.selIsMultiCell()) {
4703 cur.pit() = cur.lastpit();
4704 cur.pos() = cur.lastpos();
4712 // in visual mode, place cursor at extreme left or right
4714 switch(entry_from) {
4716 case ENTRY_DIRECTION_RIGHT:
4717 cur.posVisToRowExtremity(false /* !left */);
4719 case ENTRY_DIRECTION_LEFT:
4720 cur.posVisToRowExtremity(true /* left */);
4722 case ENTRY_DIRECTION_IGNORE:
4723 // nothing to do in this case
4727 cur.setCurrentFont();
4732 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4734 row_type const row = tabular.cellRow(cur.idx());
4735 col_type const col = tabular.cellColumn(cur.idx());
4737 if (isRightToLeft(cur)) {
4738 if (cur.idx() == tabular.getLastCellInRow(row)) {
4741 cur.idx() = tabular.getFirstCellInRow(row);
4742 cur.idx() = tabular.cellAbove(cur.idx());
4744 if (tabular.isLastCell(cur.idx()))
4746 if (cur.idx() == tabular.getLastCellInRow(row))
4747 cur.idx() = tabular.cellIndex(row + 1, 0);
4749 cur.idx() = tabular.cellIndex(row, col + 1);
4752 if (cur.idx() == 0) // first cell
4755 cur.idx() = tabular.getLastCellInRow(row - 1);
4757 cur.idx() = tabular.cellIndex(row, col - 1);
4760 if (cur.selIsMultiCell()) {
4761 cur.pit() = cur.lastpit();
4762 cur.pos() = cur.lastpos();
4767 cur.pit() = cur.lastpit();
4768 cur.pos() = cur.lastpos();
4770 // in visual mode, place cursor at extreme left or right
4772 switch(entry_from) {
4774 case ENTRY_DIRECTION_RIGHT:
4775 cur.posVisToRowExtremity(false /* !left */);
4777 case ENTRY_DIRECTION_LEFT:
4778 cur.posVisToRowExtremity(true /* left */);
4780 case ENTRY_DIRECTION_IGNORE:
4781 // nothing to do in this case
4785 cur.setCurrentFont();
4790 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4792 istringstream is(argument);
4795 if (insetCode(s) != TABULAR_CODE)
4799 size_t safe_guard = 0;
4804 if (safe_guard > 1000) {
4805 LYXERR0("parameter max count reached!");
4809 Tabular::Feature action = Tabular::LAST_ACTION;
4812 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4813 if (s != tabularFeature[i].feature)
4816 action = tabularFeature[i].action;
4819 if (action == Tabular::LAST_ACTION) {
4820 LYXERR0("Feature not found " << s);
4824 if (tabularFeature[i].need_value)
4826 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4827 tabularFeatures(cur, action, val);
4833 static void checkLongtableSpecial(Tabular::ltType & ltt,
4834 string const & special, bool & flag)
4836 if (special == "dl_above") {
4839 } else if (special == "dl_below") {
4840 ltt.bottomDL = flag;
4842 } else if (special == "empty") {
4852 bool InsetTabular::oneCellHasRotationState(bool rotated,
4853 row_type row_start, row_type row_end,
4854 col_type col_start, col_type col_end) const
4856 for (row_type r = row_start; r <= row_end; ++r)
4857 for (col_type c = col_start; c <= col_end; ++c)
4858 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4864 void InsetTabular::tabularFeatures(Cursor & cur,
4865 Tabular::Feature feature, string const & value)
4867 col_type sel_col_start;
4868 col_type sel_col_end;
4869 row_type sel_row_start;
4870 row_type sel_row_end;
4871 bool setLines = false;
4872 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4873 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4877 case Tabular::M_ALIGN_LEFT:
4878 case Tabular::ALIGN_LEFT:
4879 setAlign = LYX_ALIGN_LEFT;
4882 case Tabular::M_ALIGN_RIGHT:
4883 case Tabular::ALIGN_RIGHT:
4884 setAlign = LYX_ALIGN_RIGHT;
4887 case Tabular::M_ALIGN_CENTER:
4888 case Tabular::ALIGN_CENTER:
4889 setAlign = LYX_ALIGN_CENTER;
4892 case Tabular::ALIGN_BLOCK:
4893 setAlign = LYX_ALIGN_BLOCK;
4896 case Tabular::M_VALIGN_TOP:
4897 case Tabular::VALIGN_TOP:
4898 setVAlign = Tabular::LYX_VALIGN_TOP;
4901 case Tabular::M_VALIGN_BOTTOM:
4902 case Tabular::VALIGN_BOTTOM:
4903 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4906 case Tabular::M_VALIGN_MIDDLE:
4907 case Tabular::VALIGN_MIDDLE:
4908 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4915 cur.recordUndoInset(ATOMIC_UNDO);
4917 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4918 row_type const row = tabular.cellRow(cur.idx());
4919 col_type const column = tabular.cellColumn(cur.idx());
4921 Tabular::ltType ltt;
4925 case Tabular::SET_PWIDTH: {
4926 Length const len(value);
4927 tabular.setColumnPWidth(cur, cur.idx(), len);
4929 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4930 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4934 case Tabular::SET_MPWIDTH:
4935 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4938 case Tabular::SET_SPECIAL_COLUMN:
4939 case Tabular::SET_SPECIAL_MULTICOLUMN:
4940 if (value == "none")
4941 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
4943 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4946 case Tabular::SET_SPECIAL_MULTIROW:
4947 //FIXME: noting to do here?
4950 case Tabular::APPEND_ROW:
4951 // append the row into the tabular
4952 tabular.appendRow(cur.idx());
4955 case Tabular::APPEND_COLUMN:
4956 // append the column into the tabular
4957 tabular.appendColumn(cur.idx());
4958 cur.idx() = tabular.cellIndex(row, column);
4961 case Tabular::DELETE_ROW:
4962 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4963 tabular.deleteRow(sel_row_start);
4964 if (sel_row_start >= tabular.nrows())
4966 cur.idx() = tabular.cellIndex(sel_row_start, column);
4969 cur.setSelection(false);
4972 case Tabular::DELETE_COLUMN:
4973 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4974 tabular.deleteColumn(sel_col_start);
4975 if (sel_col_start >= tabular.ncols())
4977 cur.idx() = tabular.cellIndex(row, sel_col_start);
4980 cur.setSelection(false);
4983 case Tabular::COPY_ROW:
4984 tabular.copyRow(row);
4987 case Tabular::COPY_COLUMN:
4988 tabular.copyColumn(column);
4989 cur.idx() = tabular.cellIndex(row, column);
4992 case Tabular::SET_LINE_TOP:
4993 case Tabular::TOGGLE_LINE_TOP: {
4994 bool lineSet = (feature == Tabular::SET_LINE_TOP)
4995 ? (value == "true") : !tabular.topLine(cur.idx());
4996 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4997 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4998 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5002 case Tabular::SET_LINE_BOTTOM:
5003 case Tabular::TOGGLE_LINE_BOTTOM: {
5004 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5005 ? (value == "true") : !tabular.bottomLine(cur.idx());
5006 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5007 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5008 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5012 case Tabular::SET_LINE_LEFT:
5013 case Tabular::TOGGLE_LINE_LEFT: {
5014 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5015 ? (value == "true") : !tabular.leftLine(cur.idx());
5016 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5017 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5018 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5022 case Tabular::SET_LINE_RIGHT:
5023 case Tabular::TOGGLE_LINE_RIGHT: {
5024 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5025 ? (value == "true") : !tabular.rightLine(cur.idx());
5026 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5027 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5028 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5032 case Tabular::M_ALIGN_LEFT:
5033 case Tabular::M_ALIGN_RIGHT:
5034 case Tabular::M_ALIGN_CENTER:
5036 case Tabular::ALIGN_LEFT:
5037 case Tabular::ALIGN_RIGHT:
5038 case Tabular::ALIGN_CENTER:
5039 case Tabular::ALIGN_BLOCK:
5040 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5041 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5042 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
5045 case Tabular::M_VALIGN_TOP:
5046 case Tabular::M_VALIGN_BOTTOM:
5047 case Tabular::M_VALIGN_MIDDLE:
5049 case Tabular::VALIGN_TOP:
5050 case Tabular::VALIGN_BOTTOM:
5051 case Tabular::VALIGN_MIDDLE:
5052 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5053 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5054 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5057 case Tabular::MULTICOLUMN: {
5058 if (!cur.selection()) {
5059 // just multicol for one single cell
5060 // check whether we are completely in a multicol
5061 if (tabular.isMultiColumn(cur.idx()))
5062 tabular.unsetMultiColumn(cur.idx());
5064 tabular.setMultiColumn(cur.idx(), 1);
5067 // we have a selection so this means we just add all this
5068 // cells to form a multicolumn cell
5069 idx_type const s_start = cur.selBegin().idx();
5070 row_type const col_start = tabular.cellColumn(s_start);
5071 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5072 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5075 cur.setSelection(false);
5079 case Tabular::MULTIROW: {
5080 if (!cur.selection()) {
5081 // just multirow for one single cell
5082 // check whether we are completely in a multirow
5083 if (tabular.isMultiRow(cur.idx()))
5084 tabular.unsetMultiRow(cur.idx());
5086 tabular.setMultiRow(cur.idx(), 1);
5089 // we have a selection so this means we just add all this
5090 // cells to form a multirow cell
5091 idx_type const s_start = cur.selBegin().idx();
5092 row_type const row_start = tabular.cellRow(s_start);
5093 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5094 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5097 cur.setSelection(false);
5101 case Tabular::SET_ALL_LINES:
5103 case Tabular::UNSET_ALL_LINES:
5104 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5105 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5106 idx_type const cell = tabular.cellIndex(r, c);
5107 tabular.setTopLine(cell, setLines);
5108 tabular.setBottomLine(cell, setLines);
5109 tabular.setRightLine(cell, setLines);
5110 tabular.setLeftLine(cell, setLines);
5114 case Tabular::SET_BORDER_LINES:
5115 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5116 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5117 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5119 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5120 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5121 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5125 case Tabular::SET_LONGTABULAR:
5126 tabular.is_long_tabular = true;
5129 case Tabular::UNSET_LONGTABULAR:
5130 for (row_type r = 0; r < tabular.nrows(); ++r) {
5131 if (tabular.ltCaption(r)) {
5132 cur.idx() = tabular.cellIndex(r, 0);
5135 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5138 tabular.is_long_tabular = false;
5141 case Tabular::SET_ROTATE_TABULAR:
5142 tabular.rotate = true;
5145 case Tabular::UNSET_ROTATE_TABULAR:
5146 tabular.rotate = false;
5149 case Tabular::TOGGLE_ROTATE_TABULAR:
5150 tabular.rotate = !tabular.rotate;
5153 case Tabular::TABULAR_VALIGN_TOP:
5154 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5157 case Tabular::TABULAR_VALIGN_MIDDLE:
5158 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5161 case Tabular::TABULAR_VALIGN_BOTTOM:
5162 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5165 case Tabular::LONGTABULAR_ALIGN_LEFT:
5166 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5169 case Tabular::LONGTABULAR_ALIGN_CENTER:
5170 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5173 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5174 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5179 case Tabular::SET_ROTATE_CELL:
5180 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5181 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5182 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5185 case Tabular::UNSET_ROTATE_CELL:
5186 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5187 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5188 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5191 case Tabular::TOGGLE_ROTATE_CELL:
5193 bool oneNotRotated = oneCellHasRotationState(false,
5194 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5196 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5197 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5198 tabular.setRotateCell(tabular.cellIndex(r, c),
5203 case Tabular::SET_USEBOX: {
5204 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5205 if (val == tabular.getUsebox(cur.idx()))
5206 val = Tabular::BOX_NONE;
5207 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5208 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5209 tabular.setUsebox(tabular.cellIndex(r, c), val);
5213 case Tabular::UNSET_LTFIRSTHEAD:
5215 case Tabular::SET_LTFIRSTHEAD:
5216 tabular.getRowOfLTFirstHead(row, ltt);
5217 checkLongtableSpecial(ltt, value, flag);
5218 tabular.setLTHead(row, flag, ltt, true);
5221 case Tabular::UNSET_LTHEAD:
5223 case Tabular::SET_LTHEAD:
5224 tabular.getRowOfLTHead(row, ltt);
5225 checkLongtableSpecial(ltt, value, flag);
5226 tabular.setLTHead(row, flag, ltt, false);
5229 case Tabular::UNSET_LTFOOT:
5231 case Tabular::SET_LTFOOT:
5232 tabular.getRowOfLTFoot(row, ltt);
5233 checkLongtableSpecial(ltt, value, flag);
5234 tabular.setLTFoot(row, flag, ltt, false);
5237 case Tabular::UNSET_LTLASTFOOT:
5239 case Tabular::SET_LTLASTFOOT:
5240 tabular.getRowOfLTLastFoot(row, ltt);
5241 checkLongtableSpecial(ltt, value, flag);
5242 tabular.setLTFoot(row, flag, ltt, true);
5245 case Tabular::SET_LTNEWPAGE:
5246 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5249 case Tabular::TOGGLE_LTCAPTION: {
5250 bool const set = !tabular.ltCaption(row);
5251 cur.idx() = tabular.setLTCaption(row, set);
5254 cur.setSelection(false);
5257 // When a row is set as caption, then also insert
5258 // a caption. Otherwise the LaTeX output is broken.
5259 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5260 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5262 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5263 if (lyx::getStatus(fr).enabled())
5269 case Tabular::SET_BOOKTABS:
5270 tabular.use_booktabs = true;
5273 case Tabular::UNSET_BOOKTABS:
5274 tabular.use_booktabs = false;
5277 case Tabular::SET_TOP_SPACE: {
5279 if (value == "default")
5280 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5281 tabular.row_info[r].top_space_default = true;
5282 else if (value == "none")
5283 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5284 tabular.row_info[r].top_space_default = false;
5285 tabular.row_info[r].top_space = len;
5287 else if (isValidLength(value, &len))
5288 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5289 tabular.row_info[r].top_space_default = false;
5290 tabular.row_info[r].top_space = len;
5295 case Tabular::SET_BOTTOM_SPACE: {
5297 if (value == "default")
5298 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5299 tabular.row_info[r].bottom_space_default = true;
5300 else if (value == "none")
5301 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5302 tabular.row_info[r].bottom_space_default = false;
5303 tabular.row_info[r].bottom_space = len;
5305 else if (isValidLength(value, &len))
5306 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5307 tabular.row_info[r].bottom_space_default = false;
5308 tabular.row_info[r].bottom_space = len;
5313 case Tabular::SET_INTERLINE_SPACE: {
5315 if (value == "default")
5316 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5317 tabular.row_info[r].interline_space_default = true;
5318 else if (value == "none")
5319 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5320 tabular.row_info[r].interline_space_default = false;
5321 tabular.row_info[r].interline_space = len;
5323 else if (isValidLength(value, &len))
5324 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5325 tabular.row_info[r].interline_space_default = false;
5326 tabular.row_info[r].interline_space = len;
5331 // dummy stuff just to avoid warnings
5332 case Tabular::LAST_ACTION:
5338 bool InsetTabular::copySelection(Cursor & cur)
5340 if (!cur.selection())
5345 getSelection(cur, rs, re, cs, ce);
5347 paste_tabular.reset(new Tabular(tabular));
5349 for (row_type r = 0; r < rs; ++r)
5350 paste_tabular->deleteRow(0);
5352 row_type const rows = re - rs + 1;
5353 while (paste_tabular->nrows() > rows)
5354 paste_tabular->deleteRow(rows);
5356 for (col_type c = 0; c < cs; ++c)
5357 paste_tabular->deleteColumn(0);
5359 col_type const columns = ce - cs + 1;
5360 while (paste_tabular->ncols() > columns)
5361 paste_tabular->deleteColumn(columns);
5363 paste_tabular->setBuffer(tabular.buffer());
5365 odocstringstream os;
5366 OutputParams const runparams(0);
5367 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5368 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5369 cap::copySelection(cur, os.str());
5371 // mark tabular stack dirty
5372 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5373 // when we (hopefully) have a one-for-all paste mechanism.
5374 // This must be called after cap::copySelection.
5375 dirtyTabularStack(true);
5381 bool InsetTabular::pasteClipboard(Cursor & cur)
5385 col_type actcol = tabular.cellColumn(cur.idx());
5386 row_type actrow = tabular.cellRow(cur.idx());
5388 if (cur.selIsMultiCell()) {
5391 getSelection(cur, actrow, re, actcol, ce);
5394 for (row_type r1 = 0, r2 = actrow;
5395 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5397 for (col_type c1 = 0, c2 = actcol;
5398 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5400 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5401 tabular.isPartOfMultiColumn(r2, c2))
5403 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5407 if (tabular.isPartOfMultiColumn(r2, c2)) {
5411 shared_ptr<InsetTableCell> inset(
5412 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5413 tabular.setCellInset(r2, c2, inset);
5414 // FIXME?: why do we need to do this explicitly? (EL)
5415 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5417 // FIXME: change tracking (MG)
5418 inset->setChange(Change(buffer().params().trackChanges ?
5419 Change::INSERTED : Change::UNCHANGED));
5427 void InsetTabular::cutSelection(Cursor & cur)
5429 if (!cur.selection())
5434 getSelection(cur, rs, re, cs, ce);
5435 for (row_type r = rs; r <= re; ++r) {
5436 for (col_type c = cs; c <= ce; ++c) {
5437 shared_ptr<InsetTableCell> t
5438 = cell(tabular.cellIndex(r, c));
5439 if (buffer().params().trackChanges)
5440 // FIXME: Change tracking (MG)
5441 t->setChange(Change(Change::DELETED));
5447 // cursor position might be invalid now
5448 if (cur.pit() > cur.lastpit())
5449 cur.pit() = cur.lastpit();
5450 if (cur.pos() > cur.lastpos())
5451 cur.pos() = cur.lastpos();
5452 cur.clearSelection();
5456 bool InsetTabular::isRightToLeft(Cursor & cur) const
5458 LASSERT(cur.depth() > 1, /**/);
5459 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5460 pos_type const parentpos = cur[cur.depth() - 2].pos();
5461 return parentpar.getFontSettings(buffer().params(),
5462 parentpos).language()->rightToLeft();
5466 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5469 LASSERT(stidx <= enidx, return docstring());
5471 col_type const col1 = tabular.cellColumn(stidx);
5472 col_type const col2 = tabular.cellColumn(enidx);
5473 row_type const row1 = tabular.cellRow(stidx);
5474 row_type const row2 = tabular.cellRow(enidx);
5476 for (col_type col = col1; col <= col2; col++)
5477 for (row_type row = row1; row <= row2; row++) {
5482 retval += tabular.cellInset(row, col)->asString(intoInsets);
5488 void InsetTabular::getSelection(Cursor & cur,
5489 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5491 CursorSlice const & beg = cur.selBegin();
5492 CursorSlice const & end = cur.selEnd();
5493 cs = tabular.cellColumn(beg.idx());
5494 ce = tabular.cellColumn(end.idx());
5498 rs = tabular.cellRow(beg.idx());
5499 re = tabular.cellRow(end.idx());
5505 Text * InsetTabular::getText(int idx) const
5507 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5511 void InsetTabular::setChange(Change const & change)
5513 for (idx_type idx = 0; idx < nargs(); ++idx)
5514 cell(idx)->setChange(change);
5518 void InsetTabular::acceptChanges()
5520 for (idx_type idx = 0; idx < nargs(); ++idx)
5521 cell(idx)->acceptChanges();
5525 void InsetTabular::rejectChanges()
5527 for (idx_type idx = 0; idx < nargs(); ++idx)
5528 cell(idx)->rejectChanges();
5532 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5534 return tabular.getPWidth(cell).zero();
5538 bool InsetTabular::forcePlainLayout(idx_type cell) const
5540 return !tabular.getPWidth(cell).zero();
5544 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5547 if (buf.length() <= 0)
5552 col_type maxCols = 1;
5553 size_t const len = buf.length();
5557 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5565 maxCols = max(cols, maxCols);
5571 maxCols = max(cols, maxCols);
5577 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5578 loctab = paste_tabular.get();
5580 dirtyTabularStack(true);
5583 cell = bv.cursor().idx();
5584 ocol = tabular.cellColumn(cell);
5585 row = tabular.cellRow(cell);
5589 idx_type const cells = loctab->numberofcells;
5592 rows = loctab->nrows();
5593 col_type const columns = loctab->ncols();
5595 while (cell < cells && p < len && row < rows &&
5596 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5602 // we can only set this if we are not too far right
5603 if (cols < columns) {
5604 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5605 Font const font = bv.textMetrics(&inset->text()).
5607 inset->setText(buf.substr(op, p - op), font,
5608 buffer().params().trackChanges);
5614 // we can only set this if we are not too far right
5615 if (cols < columns) {
5616 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5617 Font const font = bv.textMetrics(&inset->text()).
5619 inset->setText(buf.substr(op, p - op), font,
5620 buffer().params().trackChanges);
5625 cell = loctab->cellIndex(row, cols);
5631 // check for the last cell if there is no trailing '\n'
5632 if (cell < cells && op < len) {
5633 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5634 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5635 inset->setText(buf.substr(op, len - op), font,
5636 buffer().params().trackChanges);
5642 void InsetTabular::addPreview(DocIterator const & inset_pos,
5643 PreviewLoader & loader) const
5645 DocIterator cell_pos = inset_pos;
5647 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5648 for (row_type r = 0; r < tabular.nrows(); ++r) {
5649 for (col_type c = 0; c < tabular.ncols(); ++c) {
5650 cell_pos.top().idx() = tabular.cellIndex(r, c);
5651 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5657 bool InsetTabular::completionSupported(Cursor const & cur) const
5659 Cursor const & bvCur = cur.bv().cursor();
5660 if (&bvCur.inset() != this)
5662 return cur.text()->completionSupported(cur);
5666 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5668 return completionSupported(cur);
5672 bool InsetTabular::automaticInlineCompletion() const
5674 return lyxrc.completion_inline_text;
5678 bool InsetTabular::automaticPopupCompletion() const
5680 return lyxrc.completion_popup_text;
5684 bool InsetTabular::showCompletionCursor() const
5686 return lyxrc.completion_cursor_text;
5690 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5692 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5696 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5698 if (!completionSupported(cur))
5700 return cur.text()->completionPrefix(cur);
5704 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5706 if (!completionSupported(cur))
5709 return cur.text()->insertCompletion(cur, s, finished);
5713 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5714 Dimension & dim) const
5716 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5717 tm.completionPosAndDim(cur, x, y, dim);
5721 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5723 istringstream data(in);
5725 lex.setStream(data);
5732 if (!lex || token != "tabular") {
5733 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5737 // This is part of the inset proper that is usually swallowed
5738 // by Buffer::readInset
5740 if (!lex || token != "Tabular") {
5741 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5749 string InsetTabular::params2string(InsetTabular const & inset)
5752 data << "tabular" << ' ';
5754 data << "\\end_inset\n";