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)
517 multicolumn(Tabular::CELL_NORMAL),
518 multirow(Tabular::CELL_NORMAL),
519 alignment(LYX_ALIGN_CENTER),
520 valignment(LYX_VALIGN_TOP),
528 inset(new InsetTableCell(buf))
530 inset->setBuffer(*buf);
534 Tabular::CellData::CellData(CellData const & cs)
537 multicolumn(cs.multicolumn),
538 multirow(cs.multirow),
539 alignment(cs.alignment),
540 valignment(cs.valignment),
542 top_line(cs.top_line),
543 bottom_line(cs.bottom_line),
544 left_line(cs.left_line),
545 right_line(cs.right_line),
548 align_special(cs.align_special),
550 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
554 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
560 void Tabular::CellData::swap(CellData & rhs)
562 std::swap(cellno, rhs.cellno);
563 std::swap(width, rhs.width);
564 std::swap(multicolumn, rhs.multicolumn);
565 std::swap(multirow, rhs.multirow);
566 std::swap(alignment, rhs.alignment);
567 std::swap(valignment, rhs.valignment);
568 std::swap(voffset, rhs.voffset);
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),
596 Tabular::ColumnData::ColumnData()
597 : alignment(LYX_ALIGN_CENTER),
598 valignment(LYX_VALIGN_TOP),
604 Tabular::ltType::ltType()
611 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
613 init(buffer, rows_arg, columns_arg);
617 void Tabular::setBuffer(Buffer & buffer)
620 for (row_type i = 0; i < nrows(); ++i)
621 for (col_type j = 0; j < ncols(); ++j)
622 cell_info[i][j].inset->setBuffer(*buffer_);
626 // activates all lines and sets all widths to 0
627 void Tabular::init(Buffer * buf, row_type rows_arg,
628 col_type columns_arg)
631 row_info = row_vector(rows_arg);
632 column_info = column_vector(columns_arg);
633 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
634 row_info.reserve(10);
635 column_info.reserve(10);
636 cell_info.reserve(100);
638 is_long_tabular = false;
639 tabular_valignment = LYX_VALIGN_MIDDLE;
640 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
642 use_booktabs = false;
643 // set silly default lines
644 for (row_type r = 0; r < nrows(); ++r)
645 for (col_type c = 0; c < ncols(); ++c) {
646 cell_info[r][c].inset->setBuffer(*buffer_);
647 cell_info[r][c].top_line = true;
648 cell_info[r][c].left_line = true;
649 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
650 cell_info[r][c].right_line = c == ncols() - 1;
655 void Tabular::appendRow(idx_type const cell)
657 row_type const row = cellRow(cell);
659 row_info.insert(row_info.begin() + row + 1, RowData());
660 row_info[row + 1] = row_info[row];
662 cell_info.insert(cell_info.begin() + row + 1,
663 cell_vector(ncols(), CellData(buffer_)));
664 for (col_type c = 0; c < ncols(); ++c) {
665 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
666 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
668 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
672 for (col_type c = 0; c < ncols(); ++c) {
673 if (isPartOfMultiRow(row, c))
675 // inherit line settings
676 idx_type const i = cellIndex(row + 1, c);
677 idx_type const j = cellIndex(row, c);
678 setLeftLine(i, leftLine(j));
679 setRightLine(i, rightLine(j));
680 setTopLine(i, topLine(j));
681 if (topLine(j) && bottomLine(j)) {
682 setBottomLine(i, true);
683 setBottomLine(j, false);
685 // mark track changes
686 if (buffer().params().trackChanges)
687 cellInfo(i).inset->setChange(Change(Change::INSERTED));
692 void Tabular::deleteRow(row_type const row)
694 // Not allowed to delete last row
698 for (col_type c = 0; c < ncols(); ++c) {
699 // Care about multirow cells
700 if (row + 1 < nrows() &&
701 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
702 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
703 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
706 row_info.erase(row_info.begin() + row);
707 cell_info.erase(cell_info.begin() + row);
712 void Tabular::copyRow(row_type const row)
714 row_info.insert(row_info.begin() + row, row_info[row]);
715 cell_info.insert(cell_info.begin() + row, cell_info[row]);
717 if (buffer().params().trackChanges)
718 for (col_type c = 0; c < ncols(); ++c)
719 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
725 void Tabular::appendColumn(idx_type const cell)
727 col_type const c = cellColumn(cell);
729 column_info.insert(column_info.begin() + c + 1, ColumnData());
730 column_info[c + 1] = column_info[c];
732 for (row_type r = 0; r < nrows(); ++r) {
733 cell_info[r].insert(cell_info[r].begin() + c + 1,
735 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
736 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
738 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
741 for (row_type r = 0; r < nrows(); ++r) {
742 // inherit line settings
743 idx_type const i = cellIndex(r, c + 1);
744 idx_type const j = cellIndex(r, c);
745 setBottomLine(i, bottomLine(j));
746 setTopLine(i, topLine(j));
747 setLeftLine(i, leftLine(j));
748 if (rightLine(j) && rightLine(j)) {
749 setRightLine(i, true);
750 setRightLine(j, false);
752 if (buffer().params().trackChanges)
753 cellInfo(i).inset->setChange(Change(Change::INSERTED));
758 void Tabular::deleteColumn(col_type const col)
760 // Not allowed to delete last column
764 for (row_type r = 0; r < nrows(); ++r) {
765 // Care about multicolumn cells
766 if (col + 1 < ncols() &&
767 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
768 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
769 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
771 cell_info[r].erase(cell_info[r].begin() + col);
773 column_info.erase(column_info.begin() + col);
778 void Tabular::copyColumn(col_type const col)
780 BufferParams const & bp = buffer().params();
781 column_info.insert(column_info.begin() + col, column_info[col]);
783 for (row_type r = 0; r < nrows(); ++r) {
784 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
786 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
792 void Tabular::updateIndexes()
796 for (row_type row = 0; row < nrows(); ++row)
797 for (col_type column = 0; column < ncols(); ++column) {
798 if (!isPartOfMultiColumn(row, column)
799 && !isPartOfMultiRow(row, column))
801 if (isPartOfMultiRow(row, column))
802 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
804 cell_info[row][column].cellno = numberofcells - 1;
807 rowofcell.resize(numberofcells);
808 columnofcell.resize(numberofcells);
810 for (row_type row = 0; row < nrows(); ++row)
811 for (col_type column = 0; column < ncols(); ++column) {
812 if (isPartOfMultiColumn(row, column)
813 || isPartOfMultiRow(row, column))
816 columnofcell[i] = column;
817 setFixedWidth(row, column);
818 updateContentAlignment(row, column);
824 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
827 for (col_type c = 0; c < ncols(); ++c)
828 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
834 bool Tabular::topLine(idx_type const cell) const
836 return cellInfo(cell).top_line;
840 bool Tabular::bottomLine(idx_type const cell) const
842 return cellInfo(cell).bottom_line;
846 bool Tabular::leftLine(idx_type cell) const
850 return cellInfo(cell).left_line;
854 bool Tabular::rightLine(idx_type cell) const
858 return cellInfo(cell).right_line;
862 int Tabular::interRowSpace(row_type row) const
864 if (!row || row >= nrows())
867 int const interline_space = row_info[row - 1].interline_space_default ?
869 row_info[row - 1].interline_space.inPixels(width());
870 if (rowTopLine(row) && rowBottomLine(row - 1))
871 return interline_space + WIDTH_OF_LINE;
872 return interline_space;
876 int Tabular::interColumnSpace(idx_type cell) const
878 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
879 if (rightLine(cell) && nextcol < ncols()
880 && leftLine(cellIndex(cellRow(cell), nextcol)))
881 return WIDTH_OF_LINE;
886 int Tabular::columnWidth(idx_type cell) const
889 col_type const span = columnSpan(cell);
890 col_type const col = cellColumn(cell);
891 for(col_type c = col; c < col + span ; ++c)
892 w += column_info[c].width;
897 int Tabular::cellHeight(idx_type cell) const
899 row_type const span = rowSpan(cell);
900 row_type const row = cellRow(cell);
902 for(row_type r = row; r < row + span ; ++r) {
903 h += rowAscent(r) + rowDescent(r);
904 if (r != row + span - 1)
905 h += interRowSpace(r + 1);
912 bool Tabular::updateColumnWidths()
915 // for each col get max of single col cells
916 for(col_type c = 0; c < ncols(); ++c) {
918 for(row_type r = 0; r < nrows(); ++r) {
919 idx_type const i = cellIndex(r, c);
920 if (columnSpan(i) == 1)
921 new_width = max(new_width, cellInfo(i).width);
924 if (column_info[c].width != new_width) {
925 column_info[c].width = new_width;
929 // update col widths to fit merged cells
930 for(col_type c = 0; c < ncols(); ++c)
931 for(row_type r = 0; r < nrows(); ++r) {
932 idx_type const i = cellIndex(r, c);
933 int const span = columnSpan(i);
934 if (span == 1 || c > cellColumn(i))
938 for(col_type j = c; j < c + span ; ++j)
939 old_width += column_info[j].width;
941 if (cellInfo(i).width > old_width) {
942 column_info[c + span - 1].width += cellInfo(i).width - old_width;
951 int Tabular::width() const
954 for (col_type c = 0; c < ncols(); ++c)
955 width += column_info[c].width;
960 void Tabular::setCellWidth(idx_type cell, int new_width)
962 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
963 + interColumnSpace(cell);
967 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
970 if (!isMultiColumn(cell) || onlycolumn)
971 column_info[cellColumn(cell)].alignment = align;
973 cellInfo(cell).alignment = align;
974 cellInset(cell).get()->setContentAlignment(align);
978 void Tabular::setVAlignment(idx_type cell, VAlignment align,
981 if (!isMultiColumn(cell) || onlycolumn)
982 column_info[cellColumn(cell)].valignment = align;
984 cellInfo(cell).valignment = align;
991 * Allow line and paragraph breaks for fixed width cells or disallow them,
992 * merge cell paragraphs and reset layout to standard for variable width
995 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
997 inset->setAutoBreakRows(fixedWidth);
998 inset->toggleFixedWidth(fixedWidth);
1002 // merge all paragraphs to one
1003 BufferParams const & bp = cur.bv().buffer().params();
1004 while (inset->paragraphs().size() > 1)
1005 mergeParagraph(bp, inset->paragraphs(), 0);
1009 // undo information has already been recorded
1010 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1011 bp.documentClass().plainLayoutName());
1018 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1019 Length const & width)
1021 col_type const c = cellColumn(cell);
1023 column_info[c].p_width = width;
1024 // reset the vertical alignment to top if the fixed with
1025 // is removed or zero because only fixed width columns can
1026 // have a vertical alignment
1027 if (column_info[c].p_width.zero())
1028 column_info[c].valignment = LYX_VALIGN_TOP;
1029 for (row_type r = 0; r < nrows(); ++r) {
1030 idx_type const cell = cellIndex(r, c);
1031 // because of multicolumns
1032 toggleFixedWidth(cur, cellInset(cell).get(),
1033 !getPWidth(cell).zero());
1035 // cur paragraph can become invalid after paragraphs were merged
1036 if (cur.pit() > cur.lastpit())
1037 cur.pit() = cur.lastpit();
1038 // cur position can become invalid after newlines were removed
1039 if (cur.pos() > cur.lastpos())
1040 cur.pos() = cur.lastpos();
1044 bool Tabular::setFixedWidth(row_type r, col_type c)
1046 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1047 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1048 || (multicol && !cell_info[r][c].p_width.zero());
1049 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1054 void Tabular::updateContentAlignment(row_type r, col_type c)
1056 cell_info[r][c].inset->setContentAlignment(
1057 getAlignment(cellIndex(r, c)));
1061 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1062 Length const & width)
1064 if (!isMultiColumn(cell))
1067 cellInfo(cell).p_width = width;
1068 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1069 // cur paragraph can become invalid after paragraphs were merged
1070 if (cur.pit() > cur.lastpit())
1071 cur.pit() = cur.lastpit();
1072 // cur position can become invalid after newlines were removed
1073 if (cur.pos() > cur.lastpos())
1074 cur.pos() = cur.lastpos();
1079 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1080 Tabular::Feature what)
1082 if (what == SET_SPECIAL_MULTICOLUMN)
1083 cellInfo(cell).align_special = special;
1085 column_info[cellColumn(cell)].align_special = special;
1089 void Tabular::setTopLine(idx_type i, bool line)
1091 cellInfo(i).top_line = line;
1095 void Tabular::setBottomLine(idx_type i, bool line)
1097 cellInfo(i).bottom_line = line;
1101 void Tabular::setLeftLine(idx_type cell, bool line)
1103 cellInfo(cell).left_line = line;
1107 void Tabular::setRightLine(idx_type cell, bool line)
1109 cellInfo(cell).right_line = line;
1112 bool Tabular::rowTopLine(row_type r) const
1114 bool all_rows_set = true;
1115 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1116 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1117 return all_rows_set;
1121 bool Tabular::rowBottomLine(row_type r) const
1123 bool all_rows_set = true;
1124 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1125 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1126 return all_rows_set;
1130 bool Tabular::columnLeftLine(col_type c) const
1137 for (row_type r = 0; r < nrows(); ++r) {
1138 idx_type const i = cellIndex(r, c);
1139 if (c == cellColumn(i)) {
1141 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1142 if (cellInfo(i).left_line || right)
1146 return 2 * nrows_left >= total;
1150 bool Tabular::columnRightLine(col_type c) const
1155 int nrows_right = 0;
1157 for (row_type r = 0; r < nrows(); ++r) {
1158 idx_type i = cellIndex(r, c);
1159 if (c == cellColumn(i) + columnSpan(i) - 1) {
1161 bool left = (c + 1 < ncols()
1162 && cellInfo(cellIndex(r, c + 1)).left_line)
1163 || c + 1 == ncols();
1164 if (cellInfo(i).right_line && left)
1168 return 2 * nrows_right >= total;
1172 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1174 if (!onlycolumn && isMultiColumn(cell))
1175 return cellInfo(cell).alignment;
1176 return column_info[cellColumn(cell)].alignment;
1181 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1183 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1184 return cellInfo(cell).valignment;
1185 return column_info[cellColumn(cell)].valignment;
1189 Length const Tabular::getPWidth(idx_type cell) const
1191 if (isMultiColumn(cell))
1192 return cellInfo(cell).p_width;
1193 return column_info[cellColumn(cell)].p_width;
1197 int Tabular::cellWidth(idx_type cell) const
1199 return cellInfo(cell).width;
1203 int Tabular::textHOffset(idx_type cell) const
1205 // the LaTeX Way :-(
1206 int x = WIDTH_OF_LINE;
1208 switch (getAlignment(cell)) {
1209 case LYX_ALIGN_CENTER:
1210 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1212 case LYX_ALIGN_RIGHT:
1213 x += columnWidth(cell) - cellWidth(cell);
1214 // + interColumnSpace(cell);
1217 // LYX_ALIGN_LEFT: nothing :-)
1225 int Tabular::textVOffset(idx_type cell) const
1227 return cellInfo(cell).voffset;
1231 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1234 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1236 return cell_info[row][c].cellno;
1240 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1242 col_type c = ncols() - 1;
1243 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1244 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1246 return cell_info[row][c].cellno;
1250 Tabular::row_type Tabular::cellRow(idx_type cell) const
1252 if (cell >= numberofcells)
1256 return rowofcell[cell];
1260 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1262 if (cell >= numberofcells)
1266 return columnofcell[cell];
1270 void Tabular::write(ostream & os) const
1274 << write_attribute("version", 3)
1275 << write_attribute("rows", nrows())
1276 << write_attribute("columns", ncols())
1278 // global longtable options
1280 << write_attribute("rotate", rotate)
1281 << write_attribute("booktabs", use_booktabs)
1282 << write_attribute("islongtable", is_long_tabular)
1283 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1284 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1285 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1286 << write_attribute("headTopDL", endhead.topDL)
1287 << write_attribute("headBottomDL", endhead.bottomDL)
1288 << write_attribute("footTopDL", endfoot.topDL)
1289 << write_attribute("footBottomDL", endfoot.bottomDL)
1290 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1291 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1292 << write_attribute("lastFootEmpty", endlastfoot.empty);
1293 // longtables cannot be aligned vertically
1294 if (!is_long_tabular)
1295 os << write_attribute("tabularvalignment", tabular_valignment);
1296 if (is_long_tabular)
1297 os << write_attribute("longtabularalignment",
1298 longtabular_alignment);
1300 for (col_type c = 0; c < ncols(); ++c) {
1302 << write_attribute("alignment", column_info[c].alignment)
1303 << write_attribute("valignment", column_info[c].valignment)
1304 << write_attribute("width", column_info[c].p_width.asString())
1305 << write_attribute("special", column_info[c].align_special)
1308 for (row_type r = 0; r < nrows(); ++r) {
1309 static const string def("default");
1311 if (row_info[r].top_space_default)
1312 os << write_attribute("topspace", def);
1314 os << write_attribute("topspace", row_info[r].top_space);
1315 if (row_info[r].bottom_space_default)
1316 os << write_attribute("bottomspace", def);
1318 os << write_attribute("bottomspace", row_info[r].bottom_space);
1319 if (row_info[r].interline_space_default)
1320 os << write_attribute("interlinespace", def);
1322 os << write_attribute("interlinespace", row_info[r].interline_space);
1323 os << write_attribute("endhead", row_info[r].endhead)
1324 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1325 << write_attribute("endfoot", row_info[r].endfoot)
1326 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1327 << write_attribute("newpage", row_info[r].newpage)
1328 << write_attribute("caption", row_info[r].caption)
1330 for (col_type c = 0; c < ncols(); ++c) {
1332 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1333 << write_attribute("multirow", cell_info[r][c].multirow)
1334 << write_attribute("alignment", cell_info[r][c].alignment)
1335 << write_attribute("valignment", cell_info[r][c].valignment)
1336 << write_attribute("topline", cell_info[r][c].top_line)
1337 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1338 << write_attribute("leftline", cell_info[r][c].left_line)
1339 << write_attribute("rightline", cell_info[r][c].right_line)
1340 << write_attribute("rotate", cell_info[r][c].rotate)
1341 << write_attribute("usebox", cell_info[r][c].usebox)
1342 << write_attribute("width", cell_info[r][c].p_width)
1343 << write_attribute("special", cell_info[r][c].align_special)
1345 os << "\\begin_inset ";
1346 cell_info[r][c].inset->write(os);
1347 os << "\n\\end_inset\n"
1352 os << "</lyxtabular>\n";
1356 void Tabular::read(Lexer & lex)
1359 istream & is = lex.getStream();
1361 l_getline(is, line);
1362 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1363 LASSERT(false, /**/);
1368 if (!getTokenValue(line, "version", version))
1370 LASSERT(version >= 2, /**/);
1373 if (!getTokenValue(line, "rows", rows_arg))
1376 if (!getTokenValue(line, "columns", columns_arg))
1378 init(buffer_, rows_arg, columns_arg);
1379 l_getline(is, line);
1380 if (!prefixIs(line, "<features")) {
1381 lyxerr << "Wrong tabular format (expected <features ...> got"
1382 << line << ')' << endl;
1385 getTokenValue(line, "rotate", rotate);
1386 getTokenValue(line, "booktabs", use_booktabs);
1387 getTokenValue(line, "islongtable", is_long_tabular);
1388 getTokenValue(line, "tabularvalignment", tabular_valignment);
1389 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1390 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1391 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1392 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1393 getTokenValue(line, "headTopDL", endhead.topDL);
1394 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1395 getTokenValue(line, "footTopDL", endfoot.topDL);
1396 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1397 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1398 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1399 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1401 for (col_type c = 0; c < ncols(); ++c) {
1403 if (!prefixIs(line,"<column")) {
1404 lyxerr << "Wrong tabular format (expected <column ...> got"
1405 << line << ')' << endl;
1408 getTokenValue(line, "alignment", column_info[c].alignment);
1409 getTokenValue(line, "valignment", column_info[c].valignment);
1410 getTokenValue(line, "width", column_info[c].p_width);
1411 getTokenValue(line, "special", column_info[c].align_special);
1414 for (row_type i = 0; i < nrows(); ++i) {
1415 l_getline(is, line);
1416 if (!prefixIs(line, "<row")) {
1417 lyxerr << "Wrong tabular format (expected <row ...> got"
1418 << line << ')' << endl;
1421 getTokenValue(line, "topspace", row_info[i].top_space,
1422 row_info[i].top_space_default);
1423 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1424 row_info[i].bottom_space_default);
1425 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1426 row_info[i].interline_space_default);
1427 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1428 getTokenValue(line, "endhead", row_info[i].endhead);
1429 getTokenValue(line, "endfoot", row_info[i].endfoot);
1430 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1431 getTokenValue(line, "newpage", row_info[i].newpage);
1432 getTokenValue(line, "caption", row_info[i].caption);
1433 for (col_type j = 0; j < ncols(); ++j) {
1434 l_getline(is, line);
1435 if (!prefixIs(line, "<cell")) {
1436 lyxerr << "Wrong tabular format (expected <cell ...> got"
1437 << line << ')' << endl;
1440 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1441 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1442 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1443 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1444 getTokenValue(line, "topline", cell_info[i][j].top_line);
1445 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1446 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1447 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1448 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1449 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1450 getTokenValue(line, "width", cell_info[i][j].p_width);
1452 getTokenValue(line, "special", cell_info[i][j].align_special);
1453 l_getline(is, line);
1454 if (prefixIs(line, "\\begin_inset")) {
1455 cell_info[i][j].inset->setBuffer(*buffer_);
1456 cell_info[i][j].inset->read(lex);
1457 l_getline(is, line);
1459 if (!prefixIs(line, "</cell>")) {
1460 lyxerr << "Wrong tabular format (expected </cell> got"
1461 << line << ')' << endl;
1465 l_getline(is, line);
1466 if (!prefixIs(line, "</row>")) {
1467 lyxerr << "Wrong tabular format (expected </row> got"
1468 << line << ')' << endl;
1472 while (!prefixIs(line, "</lyxtabular>")) {
1473 l_getline(is, line);
1479 bool Tabular::isMultiColumn(idx_type cell) const
1481 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1482 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1486 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1488 return cell_info[cellRow(cell)][cellColumn(cell)];
1492 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1494 idx_type const col = cellColumn(cell);
1495 idx_type const row = cellRow(cell);
1496 for (idx_type i = 0; i < number; ++i)
1497 unsetMultiRow(cellIndex(row, col + i));
1499 // unsetting of multirow may have invalidated cell index
1500 cell = cellIndex(row, col);
1501 CellData & cs = cellInfo(cell);
1502 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1503 cs.alignment = column_info[col].alignment;
1505 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1507 for (idx_type i = 1; i < number; ++i) {
1508 CellData & cs1 = cellInfo(cell + i);
1509 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1510 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1518 bool Tabular::isMultiRow(idx_type cell) const
1520 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1521 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1525 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1527 idx_type const col = cellColumn(cell);
1528 idx_type const row = cellRow(cell);
1529 for (idx_type i = 0; i < number; ++i)
1530 unsetMultiColumn(cellIndex(row + i, col));
1532 // unsetting of multicol may have invalidated cell index
1533 cell = cellIndex(row, col);
1534 CellData & cs = cellInfo(cell);
1535 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1536 cs.valignment = LYX_VALIGN_MIDDLE;
1537 // FIXME: the horizontal alignment of multirow cells can only
1538 // be changed for the whole table,
1539 // support for this needs to be implemented but us a fileformat
1540 // change (assigning this to uwestoehr)
1541 // until LyX supports this, use the deault alignment of multirow
1543 cs.alignment = LYX_ALIGN_LEFT;
1545 // set the bottom row of the last selected cell
1546 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1548 for (idx_type i = 1; i < number; ++i) {
1549 CellData & cs1 = cell_info[row + i][col];
1550 cs1.multirow = CELL_PART_OF_MULTIROW;
1551 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1559 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1561 row_type const row = cellRow(cell);
1562 col_type column = cellColumn(cell) + 1;
1563 while (column < ncols() && isPartOfMultiColumn(row, column))
1566 return column - cellColumn(cell);
1570 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1572 col_type const column = cellColumn(cell);
1573 col_type row = cellRow(cell) + 1;
1574 while (row < nrows() && isPartOfMultiRow(row, column))
1577 return row - cellRow(cell);
1581 void Tabular::unsetMultiColumn(idx_type cell)
1583 if (!isMultiColumn(cell))
1586 row_type const row = cellRow(cell);
1587 col_type const col = cellColumn(cell);
1588 row_type const span = columnSpan(cell);
1589 for (col_type c = 0; c < span; ++c)
1590 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1595 void Tabular::unsetMultiRow(idx_type cell)
1597 if (!isMultiRow(cell))
1600 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1601 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1602 row_type const row = cellRow(cell);
1603 col_type const col = cellColumn(cell);
1604 row_type const span = rowSpan(cell);
1605 for (row_type r = 0; r < span; ++r)
1606 cell_info[row + r][col].multirow = CELL_NORMAL;
1611 void Tabular::setRotateCell(idx_type cell, bool flag)
1613 cellInfo(cell).rotate = flag;
1617 bool Tabular::getRotateCell(idx_type cell) const
1619 return cellInfo(cell).rotate;
1623 bool Tabular::needRotating() const
1627 for (row_type r = 0; r < nrows(); ++r)
1628 for (col_type c = 0; c < ncols(); ++c)
1629 if (cell_info[r][c].rotate)
1635 bool Tabular::isLastCell(idx_type cell) const
1637 if (cell + 1 < numberofcells)
1643 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1645 if (cellRow(cell) == 0)
1648 col_type const col = cellColumn(cell);
1649 row_type r = cellRow(cell) - 1;
1650 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1653 return cell_info[r][col].cellno;
1657 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1659 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1660 if (nextrow < nrows())
1661 return cell_info[nextrow][cellColumn(cell)].cellno;
1666 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1668 LASSERT(column != npos && column < ncols()
1669 && row != npos && row < nrows(), /**/);
1670 return cell_info[row][column].cellno;
1674 void Tabular::setUsebox(idx_type cell, BoxType type)
1676 cellInfo(cell).usebox = type;
1680 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1681 // adds line breaks, see bug 4886.
1682 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1684 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1685 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1687 if (cellInfo(cell).usebox > 1)
1688 return cellInfo(cell).usebox;
1689 return useParbox(cell);
1694 // This are functions used for the longtable support
1696 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1702 row_info[row].endfirsthead = flag;
1706 row_info[row].endhead = flag;
1711 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1714 hd.set = haveLTHead();
1715 return row_info[row].endhead;
1719 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1722 hd.set = haveLTFirstHead();
1723 return row_info[row].endfirsthead;
1727 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1733 row_info[row].endlastfoot = flag;
1737 row_info[row].endfoot = flag;
1742 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1745 fd.set = haveLTFoot();
1746 return row_info[row].endfoot;
1750 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1753 fd.set = haveLTLastFoot();
1754 return row_info[row].endlastfoot;
1758 void Tabular::setLTNewPage(row_type row, bool what)
1760 row_info[row].newpage = what;
1764 bool Tabular::getLTNewPage(row_type row) const
1766 return row_info[row].newpage;
1770 bool Tabular::haveLTHead() const
1772 for (row_type i = 0; i < nrows(); ++i)
1773 if (row_info[i].endhead)
1779 bool Tabular::haveLTFirstHead() const
1781 if (endfirsthead.empty)
1783 for (row_type r = 0; r < nrows(); ++r)
1784 if (row_info[r].endfirsthead)
1790 bool Tabular::haveLTFoot() const
1792 for (row_type r = 0; r < nrows(); ++r)
1793 if (row_info[r].endfoot)
1799 bool Tabular::haveLTLastFoot() const
1801 if (endlastfoot.empty)
1803 for (row_type r = 0; r < nrows(); ++r)
1804 if (row_info[r].endlastfoot)
1810 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1812 idx_type i = getFirstCellInRow(row);
1814 setMultiColumn(i, numberOfCellsInRow(row));
1815 setTopLine(i, false);
1816 setBottomLine(i, false);
1817 setLeftLine(i, false);
1818 setRightLine(i, false);
1820 unsetMultiColumn(i);
1821 // When unsetting a caption row, also all existing
1822 // captions in this row must be dissolved.
1824 row_info[row].caption = what;
1829 bool Tabular::ltCaption(row_type row) const
1831 return row_info[row].caption;
1835 bool Tabular::haveLTCaption() const
1837 for (row_type r = 0; r < nrows(); ++r)
1838 if (row_info[r].caption)
1844 // end longtable support functions
1846 void Tabular::setRowAscent(row_type row, int height)
1848 if (row >= nrows() || row_info[row].ascent == height)
1850 row_info[row].ascent = height;
1854 void Tabular::setRowDescent(row_type row, int height)
1856 if (row >= nrows() || row_info[row].descent == height)
1858 row_info[row].descent = height;
1862 int Tabular::rowAscent(row_type row) const
1864 LASSERT(row < nrows(), /**/);
1865 return row_info[row].ascent;
1869 int Tabular::rowDescent(row_type row) const
1871 LASSERT(row < nrows(), /**/);
1872 return row_info[row].descent;
1876 int Tabular::height() const
1879 for (row_type row = 0; row < nrows(); ++row)
1880 height += rowAscent(row) + rowDescent(row) +
1886 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1888 LASSERT(row < nrows(), /**/);
1889 LASSERT(column < ncols(), /**/);
1890 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1894 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1896 LASSERT(row < nrows(), /**/);
1897 LASSERT(column < ncols(), /**/);
1898 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1902 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1904 // we only output complete row lines and the 1st row here, the rest
1905 // is done in Tabular::TeXBottomHLine(...)
1907 // get for each column the topline (if any)
1908 vector<bool> topline;
1910 for (col_type c = 0; c < ncols(); ++c) {
1911 topline.push_back(topLine(cellIndex(row, c)));
1912 // If cell is part of a multirow and not the first cell of the
1913 // multirow, no line must be drawn.
1915 if (isMultiRow(cellIndex(row, c))
1916 && isMultiRow(cellIndex(row - 1, c)))
1922 // do nothing if empty first row, or incomplete row line after
1923 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
1926 // only output complete row lines and the 1st row's clines
1927 if (nset == ncols()) {
1929 os << (row == 0 ? "\\toprule " : "\\midrule ");
1933 } else if (row == 0) {
1934 for (col_type c = 0; c < ncols(); ++c) {
1936 //babel makes the "-" character an active one, so we have to suppress this here
1937 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1938 if (lang == "slovak" || lang == "czech")
1939 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1940 "\\expandafter\\cline\\expandafter{\\expandafter")
1941 << c + 1 << "\\string-";
1943 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1944 // get to last column of line span
1945 while (c < ncols() && topline[c])
1956 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
1958 // we output bottomlines of row r and the toplines of row r+1
1959 // if the latter do not span the whole tabular
1961 // get the bottomlines of row r, and toplines in next row
1962 bool lastrow = row == nrows() - 1;
1963 vector<bool> bottomline, topline;
1964 bool nextrowset = true;
1965 for (col_type c = 0; c < ncols(); ++c) {
1966 bottomline.push_back(bottomLine(cellIndex(row, c)));
1967 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1968 // If cell is part of a multirow and not the last or first cell of the
1969 // multirow, no line must be drawn.
1971 if (isMultiRow(cellIndex(row, c))
1972 && isMultiRow(cellIndex(row + 1, c))) {
1973 bottomline[c] = false;
1976 nextrowset &= topline[c];
1979 // combine this row's bottom lines and next row's toplines if necessary
1981 for (col_type c = 0; c < ncols(); ++c) {
1983 bottomline[c] = bottomline[c] || topline[c];
1988 // do nothing if empty, OR incomplete row line with a topline in next row
1989 if (nset == 0 || (nextrowset && nset != ncols()))
1992 if (nset == ncols()) {
1994 os << (lastrow ? "\\bottomrule" : "\\midrule");
1998 for (col_type c = 0; c < ncols(); ++c) {
1999 if (bottomline[c]) {
2000 //babel makes the "-" character an active one, so we have to suppress this here
2001 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2002 if (lang == "slovak" || lang == "czech")
2003 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
2004 "\\expandafter\\cline\\expandafter{\\expandafter")
2005 << c + 1 << "\\string-";
2007 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2008 // get to last column of line span
2009 while (c < ncols() && bottomline[c])
2020 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2021 bool & ismulticol, bool & ismultirow) const
2024 row_type const r = cellRow(cell);
2025 if (is_long_tabular && row_info[r].caption)
2028 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2029 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2030 // figure out how to set the lines
2031 // we always set double lines to the right of the cell
2032 col_type const c = cellColumn(cell);
2033 col_type const nextcol = c + columnSpan(cell);
2034 bool colright = columnRightLine(c);
2035 bool colleft = columnLeftLine(c);
2036 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2037 bool nextcellleft = nextcol < ncols()
2038 && leftLine(cellIndex(r, nextcol));
2039 bool coldouble = colright && nextcolleft;
2040 bool celldouble = rightLine(cell) && nextcellleft;
2042 ismulticol = isMultiColumn(cell)
2043 || (c == 0 && colleft != leftLine(cell))
2044 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2045 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2046 || (coldouble != celldouble);
2048 os << "\\multicolumn{" << columnSpan(cell) << "}{";
2049 if (c ==0 && leftLine(cell))
2051 if (!cellInfo(cell).align_special.empty()) {
2052 os << cellInfo(cell).align_special;
2054 if (!getPWidth(cell).zero()) {
2056 case LYX_ALIGN_LEFT:
2057 os << ">{\\raggedright}";
2059 case LYX_ALIGN_RIGHT:
2060 os << ">{\\raggedleft}";
2062 case LYX_ALIGN_CENTER:
2063 os << ">{\\centering}";
2069 case LYX_VALIGN_TOP:
2072 case LYX_VALIGN_MIDDLE:
2075 case LYX_VALIGN_BOTTOM:
2080 << from_ascii(getPWidth(cell).asLatexString())
2084 case LYX_ALIGN_LEFT:
2087 case LYX_ALIGN_RIGHT:
2094 } // end if else !getPWidth
2095 } // end if else !cellinfo_of_cell
2096 if (rightLine(cell) || nextcellleft)
2099 // add extra vertical line if we want a double one
2102 } // end if ismulticol
2104 // we only need code for the first multirow cell
2105 ismultirow = isMultiRow(cell);
2107 os << "\\multirow{" << rowSpan(cell) << "}{";
2108 if (!getPWidth(cell).zero())
2109 os << from_ascii(getPWidth(cell).asLatexString());
2111 // we need to set a default value
2112 // needs to be discussed
2115 } // end if ismultirow
2117 if (getRotateCell(cell)) {
2118 os << "\\begin{sideways}\n";
2121 if (getUsebox(cell) == BOX_PARBOX) {
2124 case LYX_VALIGN_TOP:
2127 case LYX_VALIGN_MIDDLE:
2130 case LYX_VALIGN_BOTTOM:
2134 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2136 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2137 os << "\\begin{minipage}[";
2139 case LYX_VALIGN_TOP:
2142 case LYX_VALIGN_MIDDLE:
2145 case LYX_VALIGN_BOTTOM:
2149 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2157 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2158 bool ismulticol, bool ismultirow) const
2161 row_type const r = cellRow(cell);
2162 if (is_long_tabular && row_info[r].caption)
2166 if (getUsebox(cell) == BOX_PARBOX)
2168 else if (getUsebox(cell) == BOX_MINIPAGE) {
2169 os << "%\n\\end{minipage}";
2172 if (getRotateCell(cell)) {
2173 os << "%\n\\end{sideways}";
2176 if (ismulticol || ismultirow) {
2183 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2184 OutputParams const & runparams) const
2186 if (!is_long_tabular)
2191 // the caption must be output before the headers
2192 if (haveLTCaption()) {
2193 for (row_type r = 0; r < nrows(); ++r) {
2194 if (row_info[r].caption) {
2195 ret += TeXRow(os, r, runparams);
2199 // output first header info
2200 // first header must be output before the header, otherwise the
2201 // correct caption placement becomes really weird
2202 if (haveLTFirstHead()) {
2203 if (endfirsthead.topDL) {
2207 for (row_type r = 0; r < nrows(); ++r) {
2208 if (row_info[r].endfirsthead) {
2209 ret += TeXRow(os, r, runparams);
2212 if (endfirsthead.bottomDL) {
2216 os << "\\endfirsthead\n";
2219 // output header info
2221 if (endfirsthead.empty && !haveLTFirstHead()) {
2222 os << "\\endfirsthead\n";
2225 if (endhead.topDL) {
2229 for (row_type r = 0; r < nrows(); ++r) {
2230 if (row_info[r].endhead) {
2231 ret += TeXRow(os, r, runparams);
2234 if (endhead.bottomDL) {
2238 os << "\\endhead\n";
2241 // output footer info
2243 if (endfoot.topDL) {
2247 for (row_type r = 0; r < nrows(); ++r) {
2248 if (row_info[r].endfoot) {
2249 ret += TeXRow(os, r, runparams);
2252 if (endfoot.bottomDL) {
2256 os << "\\endfoot\n";
2258 if (endlastfoot.empty && !haveLTLastFoot()) {
2259 os << "\\endlastfoot\n";
2263 // output lastfooter info
2264 if (haveLTLastFoot()) {
2265 if (endlastfoot.topDL) {
2269 for (row_type r = 0; r < nrows(); ++r) {
2270 if (row_info[r].endlastfoot) {
2271 ret += TeXRow(os, r, runparams);
2274 if (endlastfoot.bottomDL) {
2278 os << "\\endlastfoot\n";
2285 bool Tabular::isValidRow(row_type row) const
2287 if (!is_long_tabular)
2289 return !row_info[row].endhead && !row_info[row].endfirsthead
2290 && !row_info[row].endfoot && !row_info[row].endlastfoot
2291 && !row_info[row].caption;
2295 int Tabular::TeXRow(odocstream & os, row_type row,
2296 OutputParams const & runparams) const
2298 idx_type cell = cellIndex(row, 0);
2299 shared_ptr<InsetTableCell> inset = cellInset(cell);
2300 Paragraph const & par = inset->paragraphs().front();
2301 string const lang = par.getParLanguage(buffer().params())->lang();
2303 //output the top line
2304 int ret = TeXTopHLine(os, row, lang);
2306 if (row_info[row].top_space_default) {
2308 os << "\\addlinespace\n";
2310 os << "\\noalign{\\vskip\\doublerulesep}\n";
2312 } else if(!row_info[row].top_space.zero()) {
2314 os << "\\addlinespace["
2315 << from_ascii(row_info[row].top_space.asLatexString())
2318 os << "\\noalign{\\vskip"
2319 << from_ascii(row_info[row].top_space.asLatexString())
2324 bool ismulticol = false;
2325 bool ismultirow = false;
2326 for (col_type c = 0; c < ncols(); ++c) {
2327 if (isPartOfMultiRow(row, c))
2328 os << " & "; // we need to add a further column
2329 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2331 cell = cellIndex(row, c);
2332 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2333 shared_ptr<InsetTableCell> inset = cellInset(cell);
2335 Paragraph const & par = inset->paragraphs().front();
2336 bool rtl = par.isRTL(buffer().params())
2338 && getPWidth(cell).zero();
2342 par.getParLanguage(buffer().params())->lang();
2343 if (lang == "farsi")
2345 else if (lang == "arabic_arabi")
2347 // currently, remaning RTL languages are
2348 // arabic_arabtex and hebrew
2352 // pass to the OutputParams that we are in a cell and
2353 // which alignment we have set.
2354 // InsetNewline needs this context information.
2355 OutputParams newrp(runparams);
2356 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2357 ? OutputParams::PLAIN
2358 : OutputParams::ALIGNED;
2359 ret += inset->latex(os, newrp);
2360 runparams.encoding = newrp.encoding;
2364 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2365 if (cell != getLastCellInRow(row)) { // not last cell in row
2369 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2370 // if no first header and no empty first header is used,
2371 // the caption needs to be terminated by \endfirsthead
2373 os << "\\endfirsthead";
2375 os << "\\tabularnewline";
2376 if (row_info[row].bottom_space_default) {
2378 os << "\\addlinespace";
2380 os << "[\\doublerulesep]";
2381 } else if (!row_info[row].bottom_space.zero()) {
2383 os << "\\addlinespace";
2385 << from_ascii(row_info[row].bottom_space.asLatexString())
2391 //output the bottom line
2392 ret += TeXBottomHLine(os, row, lang);
2394 if (row_info[row].interline_space_default) {
2396 os << "\\addlinespace\n";
2398 os << "\\noalign{\\vskip\\doublerulesep}\n";
2400 } else if (!row_info[row].interline_space.zero()) {
2402 os << "\\addlinespace["
2403 << from_ascii(row_info[row].interline_space.asLatexString())
2406 os << "\\noalign{\\vskip"
2407 << from_ascii(row_info[row].interline_space.asLatexString())
2415 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2419 //+---------------------------------------------------------------------
2420 //+ first the opening preamble +
2421 //+---------------------------------------------------------------------
2424 os << "\\begin{sideways}\n";
2427 if (is_long_tabular) {
2428 os << "\\begin{longtable}";
2429 switch (longtabular_alignment) {
2430 case LYX_LONGTABULAR_ALIGN_LEFT:
2433 case LYX_LONGTABULAR_ALIGN_CENTER:
2435 case LYX_LONGTABULAR_ALIGN_RIGHT:
2440 os << "\\begin{tabular}";
2441 switch (tabular_valignment) {
2442 case LYX_VALIGN_TOP:
2445 case LYX_VALIGN_MIDDLE:
2447 case LYX_VALIGN_BOTTOM:
2455 for (col_type c = 0; c < ncols(); ++c) {
2456 if (columnLeftLine(c))
2458 if (!column_info[c].align_special.empty()) {
2459 os << column_info[c].align_special;
2461 if (!column_info[c].p_width.zero()) {
2462 switch (column_info[c].alignment) {
2463 case LYX_ALIGN_LEFT:
2464 os << ">{\\raggedright}";
2466 case LYX_ALIGN_RIGHT:
2467 os << ">{\\raggedleft}";
2469 case LYX_ALIGN_CENTER:
2470 os << ">{\\centering}";
2472 case LYX_ALIGN_NONE:
2473 case LYX_ALIGN_BLOCK:
2474 case LYX_ALIGN_LAYOUT:
2475 case LYX_ALIGN_SPECIAL:
2479 switch (column_info[c].valignment) {
2480 case LYX_VALIGN_TOP:
2483 case LYX_VALIGN_MIDDLE:
2486 case LYX_VALIGN_BOTTOM:
2491 << from_ascii(column_info[c].p_width.asLatexString())
2494 switch (column_info[c].alignment) {
2495 case LYX_ALIGN_LEFT:
2498 case LYX_ALIGN_RIGHT:
2505 } // end if else !column_info[i].p_width
2506 } // end if else !column_info[i].align_special
2507 if (columnRightLine(c))
2513 ret += TeXLongtableHeaderFooter(os, runparams);
2515 //+---------------------------------------------------------------------
2516 //+ the single row and columns (cells) +
2517 //+---------------------------------------------------------------------
2519 for (row_type r = 0; r < nrows(); ++r) {
2520 if (isValidRow(r)) {
2521 ret += TeXRow(os, r, runparams);
2522 if (is_long_tabular && row_info[r].newpage) {
2523 os << "\\newpage\n";
2529 //+---------------------------------------------------------------------
2530 //+ the closing of the tabular +
2531 //+---------------------------------------------------------------------
2533 if (is_long_tabular)
2534 os << "\\end{longtable}";
2536 os << "\\end{tabular}";
2538 os << "\n\\end{sideways}";
2546 int Tabular::docbookRow(odocstream & os, row_type row,
2547 OutputParams const & runparams) const
2550 idx_type cell = getFirstCellInRow(row);
2553 for (col_type c = 0; c < ncols(); ++c) {
2554 if (isPartOfMultiColumn(row, c))
2557 os << "<entry align=\"";
2558 switch (getAlignment(cell)) {
2559 case LYX_ALIGN_LEFT:
2562 case LYX_ALIGN_RIGHT:
2570 os << "\" valign=\"";
2571 switch (getVAlignment(cell)) {
2572 case LYX_VALIGN_TOP:
2575 case LYX_VALIGN_BOTTOM:
2578 case LYX_VALIGN_MIDDLE:
2583 if (isMultiColumn(cell)) {
2584 os << " namest=\"col" << c << "\" ";
2585 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2589 ret += cellInset(cell)->docbook(os, runparams);
2598 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2602 //+---------------------------------------------------------------------
2603 //+ first the opening preamble +
2604 //+---------------------------------------------------------------------
2606 os << "<tgroup cols=\"" << ncols()
2607 << "\" colsep=\"1\" rowsep=\"1\">\n";
2609 for (col_type c = 0; c < ncols(); ++c) {
2610 os << "<colspec colname=\"col" << c << "\" align=\"";
2611 switch (column_info[c].alignment) {
2612 case LYX_ALIGN_LEFT:
2615 case LYX_ALIGN_RIGHT:
2623 if (runparams.flavor == OutputParams::XML)
2629 //+---------------------------------------------------------------------
2630 //+ Long Tabular case +
2631 //+---------------------------------------------------------------------
2633 // output caption info
2634 if (haveLTCaption()) {
2635 os << "<caption>\n";
2637 for (row_type r = 0; r < nrows(); ++r) {
2638 if (row_info[r].caption) {
2639 ret += docbookRow(os, r, runparams);
2642 os << "</caption>\n";
2645 // output header info
2646 if (haveLTHead() || haveLTFirstHead()) {
2649 for (row_type r = 0; r < nrows(); ++r) {
2650 if (row_info[r].endhead || row_info[r].endfirsthead) {
2651 ret += docbookRow(os, r, runparams);
2657 // output footer info
2658 if (haveLTFoot() || haveLTLastFoot()) {
2661 for (row_type r = 0; r < nrows(); ++r) {
2662 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2663 ret += docbookRow(os, r, runparams);
2670 //+---------------------------------------------------------------------
2671 //+ the single row and columns (cells) +
2672 //+---------------------------------------------------------------------
2676 for (row_type r = 0; r < nrows(); ++r) {
2677 if (isValidRow(r)) {
2678 ret += docbookRow(os, r, runparams);
2683 //+---------------------------------------------------------------------
2684 //+ the closing of the tabular +
2685 //+---------------------------------------------------------------------
2694 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2695 OutputParams const & runparams) const
2698 idx_type cell = getFirstCellInRow(row);
2700 xs << html::StartTag("tr");
2701 for (col_type c = 0; c < ncols(); ++c) {
2702 if (isPartOfMultiColumn(row, c))
2707 switch (getAlignment(cell)) {
2708 case LYX_ALIGN_LEFT:
2711 case LYX_ALIGN_RIGHT:
2719 attr << " valign='";
2720 switch (getVAlignment(cell)) {
2721 case LYX_VALIGN_TOP:
2724 case LYX_VALIGN_BOTTOM:
2727 case LYX_VALIGN_MIDDLE:
2732 if (isMultiColumn(cell))
2733 attr << " colspan='" << columnSpan(cell) << "'";
2735 xs << html::StartTag("td", attr.str());
2736 ret += cellInset(cell)->xhtml(xs, runparams);
2737 xs << html::EndTag("td");
2740 xs << html::EndTag("tr");
2745 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2748 // It's unclear to me if we need to mess with the long table stuff.
2749 // We can borrow that too from docbook, if so.
2751 xs << html::StartTag("tbody");
2752 for (row_type r = 0; r < nrows(); ++r) {
2753 if (isValidRow(r)) {
2754 ret += xhtmlRow(xs, r, runparams);
2757 xs << html::EndTag("tbody");
2762 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2763 vector<unsigned int> const & clen) const
2765 idx_type const fcell = getFirstCellInRow(row);
2766 idx_type const n = numberOfCellsInRow(row) + fcell;
2769 for (idx_type i = fcell; i < n; ++i) {
2779 for (idx_type i = fcell; i < n; ++i) {
2790 col_type column = cellColumn(i);
2791 int len = clen[column];
2792 while (column < ncols() - 1
2793 && isPartOfMultiColumn(row, ++column))
2794 len += clen[column] + 4;
2795 os << docstring(len, ch);
2810 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2811 vector<unsigned int> const & clen) const
2813 idx_type const fcell = getFirstCellInRow(row);
2814 idx_type const n = numberOfCellsInRow(row) + fcell;
2817 for (idx_type i = fcell; i < n; ++i) {
2818 if (bottomLine(i)) {
2827 for (idx_type i = fcell; i < n; ++i) {
2828 if (bottomLine(i)) {
2838 col_type column = cellColumn(i);
2839 int len = clen[column];
2840 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2841 len += clen[column] + 4;
2842 os << docstring(len, ch);
2843 if (bottomLine(i)) {
2857 void Tabular::plaintextPrintCell(odocstream & os,
2858 OutputParams const & runparams,
2859 idx_type cell, row_type row, col_type column,
2860 vector<unsigned int> const & clen,
2861 bool onlydata) const
2863 odocstringstream sstr;
2864 cellInset(cell)->plaintext(sstr, runparams);
2876 unsigned int len1 = sstr.str().length();
2877 unsigned int len2 = clen[column];
2878 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2879 len2 += clen[column] + 4;
2882 switch (getAlignment(cell)) {
2884 case LYX_ALIGN_LEFT:
2887 case LYX_ALIGN_RIGHT:
2891 case LYX_ALIGN_CENTER:
2897 os << docstring(len1, ' ') << sstr.str()
2898 << docstring(len2, ' ');
2900 if (rightLine(cell))
2907 void Tabular::plaintext(odocstream & os,
2908 OutputParams const & runparams, int const depth,
2909 bool onlydata, char_type delim) const
2911 // first calculate the width of the single columns
2912 vector<unsigned int> clen(ncols());
2915 // first all non multicolumn cells!
2916 for (col_type c = 0; c < ncols(); ++c) {
2918 for (row_type r = 0; r < nrows(); ++r) {
2919 idx_type cell = cellIndex(r, c);
2920 if (isMultiColumn(cell))
2922 odocstringstream sstr;
2923 cellInset(cell)->plaintext(sstr, runparams);
2924 if (clen[c] < sstr.str().length())
2925 clen[c] = sstr.str().length();
2928 // then all multicolumn cells!
2929 for (col_type c = 0; c < ncols(); ++c) {
2930 for (row_type r = 0; r < nrows(); ++r) {
2931 idx_type cell = cellIndex(r, c);
2932 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
2934 odocstringstream sstr;
2935 cellInset(cell)->plaintext(sstr, runparams);
2936 int len = int(sstr.str().length());
2937 idx_type const n = columnSpan(cell);
2938 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
2940 if (len > int(clen[c + n - 1]))
2941 clen[c + n - 1] = len;
2946 for (row_type r = 0; r < nrows(); ++r) {
2947 if (!onlydata && plaintextTopHLine(os, r, clen))
2948 os << docstring(depth * 2, ' ');
2949 for (col_type c = 0; c < ncols(); ++c) {
2950 if (isPartOfMultiColumn(r, c))
2952 if (onlydata && c > 0)
2953 // we don't use operator<< for single UCS4 character.
2954 // see explanation in docstream.h
2956 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
2961 os << docstring(depth * 2, ' ');
2962 if (plaintextBottomHLine(os, r, clen))
2963 os << docstring(depth * 2, ' ');
2969 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2971 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2975 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2976 col_type column) const
2978 return cell_info[row][column].inset;
2982 void Tabular::setCellInset(row_type row, col_type column,
2983 shared_ptr<InsetTableCell> ins) const
2985 CellData & cd = cell_info[row][column];
2990 void Tabular::validate(LaTeXFeatures & features) const
2992 features.require("NeedTabularnewline");
2994 features.require("booktabs");
2995 if (is_long_tabular)
2996 features.require("longtable");
2998 features.require("rotating");
2999 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3000 if (isMultiRow(cell))
3001 features.require("multirow");
3002 if (getVAlignment(cell) != LYX_VALIGN_TOP
3003 || !getPWidth(cell).zero())
3004 features.require("array");
3005 cellInset(cell)->validate(features);
3010 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3012 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3013 ParagraphList::const_iterator cit = parlist.begin();
3014 ParagraphList::const_iterator end = parlist.end();
3016 for (; cit != end; ++cit)
3017 for (int i = 0; i < cit->size(); ++i)
3018 if (cit->isNewline(i))
3025 /////////////////////////////////////////////////////////////////////
3029 /////////////////////////////////////////////////////////////////////
3031 InsetTableCell::InsetTableCell(Buffer * buf)
3032 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3033 contentAlign(LYX_ALIGN_CENTER)
3037 bool InsetTableCell::forcePlainLayout(idx_type) const
3039 return !isFixedWidth;
3043 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3045 return isFixedWidth;
3049 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3050 FuncStatus & status) const
3053 switch (cmd.action) {
3055 enabled = !forcePlainLayout();
3057 case LFUN_LAYOUT_PARAGRAPH:
3058 enabled = allowParagraphCustomization();
3061 return InsetText::getStatus(cur, cmd, status);
3063 status.setEnabled(enabled);
3067 docstring InsetTableCell::asString(bool intoInsets)
3070 if (paragraphs().empty())
3072 ParagraphList::const_iterator it = paragraphs().begin();
3073 ParagraphList::const_iterator en = paragraphs().end();
3075 for (; it != en; ++it) {
3080 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3086 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3089 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3090 return InsetText::xhtml(xs, rp);
3095 /////////////////////////////////////////////////////////////////////
3099 /////////////////////////////////////////////////////////////////////
3101 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3103 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3104 rowselect_(false), colselect_(false)
3109 InsetTabular::InsetTabular(InsetTabular const & tab)
3110 : Inset(tab), tabular(tab.tabular), scx_(0)
3115 InsetTabular::~InsetTabular()
3117 hideDialogs("tabular", this);
3121 void InsetTabular::setBuffer(Buffer & buf)
3123 tabular.setBuffer(buf);
3124 Inset::setBuffer(buf);
3128 bool InsetTabular::insetAllowed(InsetCode code) const
3130 if (code == MATHMACRO_CODE
3131 || (code == CAPTION_CODE && !tabular.is_long_tabular))
3138 void InsetTabular::write(ostream & os) const
3140 os << "Tabular" << endl;
3145 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3147 // FIXME: depending on the selection state, we could offer a different menu.
3148 return from_ascii("context-tabular");
3152 void InsetTabular::read(Lexer & lex)
3154 //bool const old_format = (lex.getString() == "\\LyXTable");
3162 string token = lex.getString();
3163 while (lex && token != "\\end_inset") {
3165 token = lex.getString();
3168 lex.printError("Missing \\end_inset at this point. ");
3172 int InsetTabular::rowFromY(Cursor & cur, int y) const
3174 // top y coordinate of tabular
3175 int h = yo(cur.bv()) - tabular.rowAscent(0);
3177 for (; r < tabular.nrows() && y > h; ++r)
3178 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3179 + tabular.interRowSpace(r);
3185 int InsetTabular::columnFromX(Cursor & cur, int x) const
3187 // left x coordinate of tabular
3188 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3190 for (; c < tabular.ncols() && x > w; ++c)
3191 w += tabular.columnWidth(c);
3196 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3198 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3199 // mi.base.textwidth << "\n";
3202 LASSERT(false, /**/);
3205 for (row_type r = 0; r < tabular.nrows(); ++r) {
3208 for (col_type c = 0; c < tabular.ncols(); ++c) {
3209 if (tabular.isPartOfMultiColumn(r, c)
3210 || tabular.isPartOfMultiRow(r, c))
3211 // multicolumn or multirow cell, but not first one
3213 idx_type const cell = tabular.cellIndex(r, c);
3217 if (tabular.cell_info[r][c].multicolumn ==
3218 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3219 p_width = tabular.cellInfo(cell).p_width;
3221 p_width = tabular.column_info[c].p_width;
3222 if (!p_width.zero())
3223 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3224 tabular.cellInset(cell)->metrics(m, dim);
3225 if (!p_width.zero())
3226 dim.wid = m.base.textwidth;
3227 tabular.setCellWidth(cell, dim.wid);
3229 // FIXME?: do we need a second metrics call
3230 // to get the descent of the text in last par?
3231 TextMetrics const & tm =
3232 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3233 int const backdes = tm.last().second->descent();
3235 switch (tabular.getVAlignment(cell)) {
3236 case Tabular::LYX_VALIGN_TOP:
3237 tabular.cell_info[r][c].voffset = 0;
3239 case Tabular::LYX_VALIGN_MIDDLE:
3240 tabular.cell_info[r][c].voffset = -(dim.des - backdes - TEXT_TO_INSET_OFFSET)/2;
3242 case Tabular::LYX_VALIGN_BOTTOM:
3243 tabular.cell_info[r][c].voffset = -(dim.des - backdes - TEXT_TO_INSET_OFFSET);
3246 maxAsc = max(maxAsc, dim.asc - tabular.cell_info[r][c].voffset);
3247 maxDesc = max(maxDesc, dim.des + tabular.cell_info[r][c].voffset);
3249 int const top_space = tabular.row_info[r].top_space_default ?
3250 default_line_space :
3251 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3252 tabular.setRowAscent(r, maxAsc + ADD_TO_HEIGHT + top_space);
3253 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3254 default_line_space :
3255 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3256 tabular.setRowDescent(r, maxDesc + ADD_TO_HEIGHT + bottom_space);
3258 tabular.updateColumnWidths();
3259 dim.asc = tabular.rowAscent(0);
3260 dim.des = tabular.height() - dim.asc;
3261 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3264 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3267 if (&cur.inset() == this && cur.selection()) {
3268 if (cur.selIsMultiCell()) {
3271 getSelection(cur, rs, re, cs, ce);
3273 idx_type const cell = tabular.cellIndex(row, col);
3274 col_type const cspan = tabular.columnSpan(cell);
3275 row_type const rspan = tabular.rowSpan(cell);
3276 if (col + cspan - 1 >= cs && col <= ce
3277 && row + rspan - 1 >= rs && row <= re)
3280 if (col == tabular.cellColumn(cur.idx())
3281 && row == tabular.cellRow(cur.idx())) {
3282 CursorSlice const & beg = cur.selBegin();
3283 CursorSlice const & end = cur.selEnd();
3285 if ((end.lastpos() > 0 || end.lastpit() > 0)
3286 && end.pos() == end.lastpos() && beg.pos() == 0
3287 && end.pit() == end.lastpit() && beg.pit() == 0)
3295 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3297 x += scx_ + ADD_TO_TABULAR_WIDTH;
3299 BufferView * bv = pi.base.bv;
3300 Cursor & cur = pi.base.bv->cursor();
3303 // FIXME: As the full background is painted in drawSelection(),
3304 // we have no choice but to do a full repaint for the Text cells.
3305 pi.full_repaint = true;
3307 bool const original_selection_state = pi.selected;
3310 first_visible_cell = Tabular::npos;
3311 for (row_type r = 0; r < tabular.nrows(); ++r) {
3313 for (col_type c = 0; c < tabular.ncols(); ++c) {
3314 if (tabular.isPartOfMultiColumn(r, c))
3317 idx = tabular.cellIndex(r, c);
3319 if (tabular.isPartOfMultiRow(r, c)) {
3320 nx += tabular.columnWidth(idx);
3324 if (first_visible_cell == Tabular::npos)
3325 first_visible_cell = idx;
3327 pi.selected |= isCellSelected(cur, r, c);
3328 int const cx = nx + tabular.textHOffset(idx);
3329 int const cy = y + tabular.textVOffset(idx);
3330 // Cache the Inset position.
3331 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3332 cell(idx)->draw(pi, cx, cy);
3333 drawCellLines(pi.pain, nx, y, r, idx, pi.change_);
3334 nx += tabular.columnWidth(idx);
3335 pi.selected = original_selection_state;
3338 if (r + 1 < tabular.nrows())
3339 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3340 + tabular.interRowSpace(r + 1);
3345 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3347 Cursor & cur = pi.base.bv->cursor();
3350 x += scx_ + ADD_TO_TABULAR_WIDTH;
3352 // FIXME: it is wrong to completely paint the background
3353 // if we want to do single row painting.
3355 // Paint background of current tabular
3356 int const w = tabular.width();
3357 int const h = tabular.height();
3358 int yy = y - tabular.rowAscent(0);
3359 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3361 if (!cur.selection())
3363 if (&cur.inset() != this)
3368 bool const full_cell_selected = isCellSelected(cur,
3369 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3371 if (cur.selIsMultiCell() || full_cell_selected) {
3372 for (row_type r = 0; r < tabular.nrows(); ++r) {
3374 for (col_type c = 0; c < tabular.ncols(); ++c) {
3375 if (tabular.isPartOfMultiColumn(r, c))
3378 idx_type const cell = tabular.cellIndex(r, c);
3380 if (tabular.isPartOfMultiRow(r, c)) {
3381 xx += tabular.columnWidth(cell);
3384 int const w = tabular.columnWidth(cell);
3385 int const h = tabular.cellHeight(cell);
3386 int const yy = y - tabular.rowAscent(r);
3387 if (isCellSelected(cur, r, c))
3388 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3391 if (r + 1 < tabular.nrows())
3392 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3393 + tabular.interRowSpace(r + 1);
3397 x += cellXPos(cur.idx());
3398 x += tabular.textHOffset(cur.idx());
3399 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3404 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3405 row_type row, idx_type cell, Change const & change) const
3407 y -= tabular.rowAscent(row);
3408 int const w = tabular.columnWidth(cell);
3409 int const h = tabular.cellHeight(cell);
3410 Color linecolor = change.changed() ? change.color() : Color_tabularline;
3411 Color gridcolor = change.changed() ? change.color() : Color_tabularonoffline;
3414 bool drawline = tabular.topLine(cell)
3415 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3416 pain.line(x, y, x + w, y,
3417 drawline ? linecolor : gridcolor,
3418 drawline ? Painter::line_solid : Painter::line_onoffdash);
3421 drawline = tabular.bottomLine(cell);
3422 pain.line(x, y + h, x + w, y + h,
3423 drawline ? linecolor : gridcolor,
3424 drawline ? Painter::line_solid : Painter::line_onoffdash);
3427 col_type const col = tabular.cellColumn(cell);
3428 drawline = tabular.leftLine(cell)
3429 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3430 pain.line(x, y, x, y + h,
3431 drawline ? linecolor : gridcolor,
3432 drawline ? Painter::line_solid : Painter::line_onoffdash);
3435 x -= tabular.interColumnSpace(cell);
3436 drawline = tabular.rightLine(cell)
3437 || (col + 1 < tabular.ncols()
3438 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3439 pain.line(x + w, y, x + w, y + h,
3440 drawline ? linecolor : gridcolor,
3441 drawline ? Painter::line_solid : Painter::line_onoffdash);
3445 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3447 //lyxerr << "InsetTabular::edit: " << this << endl;
3449 cur.setSelection(false);
3452 if (isRightToLeft(cur))
3453 cur.idx() = tabular.getLastCellInRow(0);
3459 if (isRightToLeft(cur))
3460 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3462 cur.idx() = tabular.numberofcells - 1;
3464 cur.pos() = cur.lastpos(); // FIXME crude guess
3466 cur.setCurrentFont();
3467 // FIXME: this accesses the position cache before it is initialized
3469 //cur.bv().fitCursor();
3473 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3475 // In a longtable, tell captions what the current float is
3476 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3477 string const saveflt = cnts.current_float();
3478 if (tabular.is_long_tabular)
3479 cnts.current_float("table");
3481 ParIterator it2 = it;
3483 size_t const end = it2.nargs();
3484 for ( ; it2.idx() < end; it2.top().forwardIdx())
3485 buffer().updateBuffer(it2, utype);
3488 if (tabular.is_long_tabular)
3489 cnts.current_float(saveflt);
3493 void InsetTabular::addToToc(DocIterator const & cpit)
3495 DocIterator dit = cpit;
3497 size_t const end = dit.nargs();
3498 for ( ; dit.idx() < end; dit.top().forwardIdx())
3499 cell(dit.idx())->addToToc(dit);
3503 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3505 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3506 << "\n cur:" << cur);
3507 CursorSlice sl = cur.top();
3508 Cursor & bvcur = cur.bv().cursor();
3510 switch (cmd.action) {
3512 case LFUN_MOUSE_PRESS: {
3513 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3515 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3516 || cmd.x > xo(cur.bv()) + tabular.width()) {
3517 row_type r = rowFromY(cur, cmd.y);
3518 cur.idx() = tabular.getFirstCellInRow(r);
3521 cur.idx() = tabular.getLastCellInRow(r);
3522 cur.pos() = cur.lastpos();
3523 cur.setSelection(true);
3529 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3530 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3531 || cmd.y > y0 + tabular.height()) {
3532 col_type c = columnFromX(cur, cmd.x);
3533 cur.idx() = tabular.cellIndex(0, c);
3536 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3537 cur.pos() = cur.lastpos();
3538 cur.setSelection(true);
3543 // do not reset cursor/selection if we have selected
3544 // some cells (bug 2715).
3545 if (cmd.button() == mouse_button::button3
3546 && &bvcur.selBegin().inset() == this
3547 && bvcur.selIsMultiCell())
3550 // Let InsetTableCell do it
3551 cell(cur.idx())->dispatch(cur, cmd);
3554 case LFUN_MOUSE_MOTION:
3555 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3556 if (cmd.button() == mouse_button::button1) {
3557 // only accept motions to places not deeper nested than the real anchor
3558 if (!bvcur.anchor_.hasPart(cur)) {
3562 // select (additional) row
3564 row_type r = rowFromY(cur, cmd.y);
3565 cur.idx() = tabular.getLastCellInRow(r);
3566 // we need to reset the cursor's pit and pos now, as the old ones
3567 // may no longer be valid.
3570 bvcur.setCursor(cur);
3571 bvcur.setSelection(true);
3574 // select (additional) column
3576 col_type c = columnFromX(cur, cmd.x);
3577 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3578 // we need to reset the cursor's pit and pos now, as the old ones
3579 // may no longer be valid.
3582 bvcur.setCursor(cur);
3583 bvcur.setSelection(true);
3586 // only update if selection changes
3587 if (bvcur.idx() == cur.idx() &&
3588 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3590 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3591 bvcur.setCursor(cur);
3592 bvcur.setSelection(true);
3593 // if this is a multicell selection, we just set the cursor to
3594 // the beginning of the cell's text.
3595 if (bvcur.selIsMultiCell()) {
3596 bvcur.pit() = bvcur.lastpit();
3597 bvcur.pos() = bvcur.lastpos();
3602 case LFUN_MOUSE_RELEASE:
3607 case LFUN_CELL_BACKWARD:
3609 cur.setSelection(false);
3612 case LFUN_CELL_FORWARD:
3614 cur.setSelection(false);
3617 case LFUN_CHAR_FORWARD_SELECT:
3618 case LFUN_CHAR_FORWARD:
3619 case LFUN_CHAR_BACKWARD_SELECT:
3620 case LFUN_CHAR_BACKWARD:
3621 case LFUN_CHAR_RIGHT_SELECT:
3622 case LFUN_CHAR_RIGHT:
3623 case LFUN_CHAR_LEFT_SELECT:
3624 case LFUN_CHAR_LEFT: {
3625 // determine whether we move to next or previous cell, where to enter
3626 // the new cell from, and which command to "finish" (i.e., exit the
3629 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3630 FuncCode finish_lfun;
3632 if (cmd.action == LFUN_CHAR_FORWARD
3633 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3635 finish_lfun = LFUN_FINISHED_FORWARD;
3637 else if (cmd.action == LFUN_CHAR_BACKWARD
3638 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3640 finish_lfun = LFUN_FINISHED_BACKWARD;
3642 // LEFT or RIGHT commands --- the interpretation will depend on the
3643 // table's direction.
3645 bool const right = cmd.action == LFUN_CHAR_RIGHT
3646 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3647 next_cell = isRightToLeft(cur) != right;
3649 if (lyxrc.visual_cursor)
3650 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3652 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3655 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3656 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3657 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3658 cmd.action == LFUN_CHAR_LEFT_SELECT;
3660 // If we have a multicell selection or we're
3661 // not doing some LFUN_*_SELECT thing anyway...
3662 if (!cur.selIsMultiCell() || !select) {
3663 col_type const c = tabular.cellColumn(cur.idx());
3664 row_type const r = tabular.cellRow(cur.idx());
3665 // Are we trying to select the whole cell and is the whole cell
3666 // not yet selected?
3667 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3668 ((next_cell && cur.pit() == cur.lastpit()
3669 && cur.pos() == cur.lastpos())
3670 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3672 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3674 // ...try to dispatch to the cell's inset.
3675 cell(cur.idx())->dispatch(cur, cmd);
3677 // When we already have a selection we want to select the whole cell
3678 // before going to the next cell.
3679 if (select_whole && !empty_cell){
3680 getText(cur.idx())->selectAll(cur);
3685 // FIXME: When we support the selection of an empty cell, remove
3686 // the !empty_cell from this condition. For now we jump to the next
3687 // cell if the current cell is empty.
3688 if (cur.result().dispatched() && !empty_cell)
3692 // move to next/prev cell, as appropriate
3693 // note that we will always do this if we're selecting and we have
3694 // a multicell selection
3695 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3696 << " cell from: " << int(entry_from));
3698 moveNextCell(cur, entry_from);
3700 movePrevCell(cur, entry_from);
3701 // if we're exiting the table, call the appropriate FINISHED lfun
3702 if (sl == cur.top())
3703 cmd = FuncRequest(finish_lfun);
3710 case LFUN_DOWN_SELECT:
3712 if (!(cur.selection() && cur.selIsMultiCell()))
3713 cell(cur.idx())->dispatch(cur, cmd);
3715 cur.dispatched(); // override the cell's decision
3716 if (sl == cur.top()) {
3717 // if our Text didn't do anything to the cursor
3718 // then we try to put the cursor into the cell below
3719 // setting also the right targetX.
3720 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3721 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3722 cur.idx() = tabular.cellBelow(cur.idx());
3724 TextMetrics const & tm =
3725 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3726 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3727 cur.setCurrentFont();
3730 if (sl == cur.top()) {
3731 // we trick it to go to forward after leaving the
3733 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3736 if (cur.selIsMultiCell()) {
3737 cur.pit() = cur.lastpit();
3738 cur.pos() = cur.lastpos();
3739 cur.setCurrentFont();
3744 case LFUN_UP_SELECT:
3746 if (!(cur.selection() && cur.selIsMultiCell()))
3747 cell(cur.idx())->dispatch(cur, cmd);
3748 cur.dispatched(); // override the cell's decision
3749 if (sl == cur.top()) {
3750 // if our Text didn't do anything to the cursor
3751 // then we try to put the cursor into the cell above
3752 // setting also the right targetX.
3753 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3754 if (tabular.cellRow(cur.idx()) != 0) {
3755 cur.idx() = tabular.cellAbove(cur.idx());
3756 cur.pit() = cur.lastpit();
3757 Text const * text = cell(cur.idx())->getText(0);
3758 TextMetrics const & tm = cur.bv().textMetrics(text);
3759 ParagraphMetrics const & pm =
3760 tm.parMetrics(cur.lastpit());
3761 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3762 cur.setCurrentFont();
3765 if (sl == cur.top()) {
3766 cmd = FuncRequest(LFUN_UP);
3769 if (cur.selIsMultiCell()) {
3771 cur.pos() = cur.lastpos();
3772 cur.setCurrentFont();
3777 // case LFUN_SCREEN_DOWN: {
3778 // //if (hasSelection())
3779 // // cur.selection() = false;
3780 // col_type const col = tabular.cellColumn(cur.idx());
3781 // int const t = cur.bv().top_y() + cur.bv().height();
3782 // if (t < yo() + tabular.getHeightOfTabular()) {
3783 // cur.bv().scrollDocView(t);
3784 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3786 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3793 // case LFUN_SCREEN_UP: {
3794 // //if (hasSelection())
3795 // // cur.selection() = false;
3796 // col_type const col = tabular.cellColumn(cur.idx());
3797 // int const t = cur.bv().top_y() + cur.bv().height();
3799 // cur.bv().scrollDocView(t);
3803 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3807 // cur.par() = cur.lastpar();
3808 // cur.pos() = cur.lastpos();
3812 case LFUN_LAYOUT_TABULAR:
3813 cur.bv().showDialog("tabular");
3816 case LFUN_INSET_MODIFY:
3817 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3821 // insert file functions
3822 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3823 case LFUN_FILE_INSERT_PLAINTEXT:
3825 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3826 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3827 FileName(to_utf8(cmd.argument())));
3830 cur.recordUndoInset(INSERT_UNDO);
3831 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3832 // content has been replaced,
3833 // so cursor might be invalid
3834 cur.pos() = cur.lastpos();
3835 cur.pit() = cur.lastpit();
3836 bvcur.setCursor(cur);
3843 if (cur.selIsMultiCell()) {
3844 if (copySelection(cur)) {
3845 cur.recordUndoInset(DELETE_UNDO);
3849 cell(cur.idx())->dispatch(cur, cmd);
3852 case LFUN_SELF_INSERT:
3853 if (cur.selIsMultiCell()) {
3854 cur.recordUndoInset(DELETE_UNDO);
3857 cell(cur.idx())->dispatch(cur, cmd);
3860 case LFUN_CHAR_DELETE_BACKWARD:
3861 case LFUN_CHAR_DELETE_FORWARD:
3862 if (cur.selIsMultiCell()) {
3863 cur.recordUndoInset(DELETE_UNDO);
3866 cell(cur.idx())->dispatch(cur, cmd);
3870 if (!cur.selection())
3872 if (cur.selIsMultiCell()) {
3876 cell(cur.idx())->dispatch(cur, cmd);
3879 case LFUN_CLIPBOARD_PASTE:
3880 case LFUN_PRIMARY_SELECTION_PASTE: {
3881 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3882 theClipboard().getAsText() :
3883 theSelection().get();
3886 // pass to InsertPlaintextString, but
3887 // only if we have multi-cell content
3888 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3889 cur.recordUndoInset(INSERT_UNDO);
3890 if (insertPlaintextString(cur.bv(), clip, false)) {
3891 // content has been replaced,
3892 // so cursor might be invalid
3893 cur.pos() = cur.lastpos();
3894 cur.pit() = cur.lastpit();
3895 bvcur.setCursor(cur);
3899 // Let the cell handle normal text
3900 cell(cur.idx())->dispatch(cur, cmd);
3905 if (!tabularStackDirty()) {
3906 if (!cur.selIsMultiCell())
3907 cell(cur.idx())->dispatch(cur, cmd);
3910 if (theClipboard().isInternal() ||
3911 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3912 cur.recordUndoInset(INSERT_UNDO);
3913 pasteClipboard(cur);
3917 case LFUN_FONT_EMPH:
3918 case LFUN_FONT_BOLD:
3919 case LFUN_FONT_BOLDSYMBOL:
3920 case LFUN_FONT_ROMAN:
3921 case LFUN_FONT_NOUN:
3922 case LFUN_FONT_ITAL:
3923 case LFUN_FONT_FRAK:
3924 case LFUN_FONT_TYPEWRITER:
3925 case LFUN_FONT_SANS:
3926 case LFUN_TEXTSTYLE_APPLY:
3927 case LFUN_TEXTSTYLE_UPDATE:
3928 case LFUN_FONT_SIZE:
3929 case LFUN_FONT_UNDERLINE:
3930 case LFUN_FONT_STRIKEOUT:
3931 case LFUN_FONT_UULINE:
3932 case LFUN_FONT_UWAVE:
3934 case LFUN_WORD_CAPITALIZE:
3935 case LFUN_WORD_UPCASE:
3936 case LFUN_WORD_LOWCASE:
3937 case LFUN_CHARS_TRANSPOSE:
3938 if (cur.selIsMultiCell()) {
3941 getSelection(cur, rs, re, cs, ce);
3942 Cursor tmpcur = cur;
3943 for (row_type r = rs; r <= re; ++r) {
3944 for (col_type c = cs; c <= ce; ++c) {
3945 // cursor follows cell:
3946 tmpcur.idx() = tabular.cellIndex(r, c);
3947 // select this cell only:
3950 tmpcur.resetAnchor();
3951 tmpcur.pit() = tmpcur.lastpit();
3952 tmpcur.pos() = tmpcur.top().lastpos();
3953 tmpcur.setCursor(tmpcur);
3954 tmpcur.setSelection();
3955 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3960 cell(cur.idx())->dispatch(cur, cmd);
3964 case LFUN_INSET_SETTINGS:
3965 // relay this lfun to Inset, not to the cell.
3966 Inset::doDispatch(cur, cmd);
3970 // we try to handle this event in the insets dispatch function.
3971 cell(cur.idx())->dispatch(cur, cmd);
3977 // function sets an object as defined in func_status.h:
3978 // states OK, Unknown, Disabled, On, Off.
3979 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3980 FuncStatus & status) const
3982 switch (cmd.action) {
3983 case LFUN_INSET_MODIFY: {
3984 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
3987 string const s = cmd.getArg(1);
3988 // FIXME: We only check for the very first argument...
3989 int action = Tabular::LAST_ACTION;
3991 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3992 if (tabularFeature[i].feature == s) {
3993 action = tabularFeature[i].action;
3997 if (action == Tabular::LAST_ACTION) {
3999 status.unknown(true);
4003 string const argument = cmd.getLongArg(2);
4005 row_type sel_row_start = 0;
4006 row_type sel_row_end = 0;
4007 col_type sel_col_start = 0;
4008 col_type sel_col_end = 0;
4009 Tabular::ltType dummyltt;
4012 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4015 case Tabular::SET_PWIDTH:
4016 case Tabular::SET_MPWIDTH:
4017 case Tabular::SET_SPECIAL_COLUMN:
4018 case Tabular::SET_SPECIAL_MULTICOLUMN:
4019 case Tabular::SET_SPECIAL_MULTIROW:
4020 case Tabular::APPEND_ROW:
4021 case Tabular::APPEND_COLUMN:
4022 case Tabular::DELETE_ROW:
4023 case Tabular::DELETE_COLUMN:
4024 case Tabular::COPY_ROW:
4025 case Tabular::COPY_COLUMN:
4026 case Tabular::SET_TOP_SPACE:
4027 case Tabular::SET_BOTTOM_SPACE:
4028 case Tabular::SET_INTERLINE_SPACE:
4032 case Tabular::MULTICOLUMN:
4033 // If a row is set as longtable caption, it must not be allowed
4034 // to unset that this row is a multicolumn.
4035 status.setEnabled(sel_row_start == sel_row_end
4036 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4037 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4040 case Tabular::MULTIROW:
4041 // If a row is set as longtable caption, it must not be allowed
4042 // to unset that this row is a multirow.
4043 status.setEnabled(sel_col_start == sel_col_end
4044 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4045 status.setOnOff(tabular.isMultiRow(cur.idx()));
4048 case Tabular::SET_ALL_LINES:
4049 case Tabular::UNSET_ALL_LINES:
4050 case Tabular::SET_BORDER_LINES:
4051 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4054 case Tabular::SET_LINE_TOP:
4055 case Tabular::SET_LINE_BOTTOM:
4056 case Tabular::SET_LINE_LEFT:
4057 case Tabular::SET_LINE_RIGHT:
4058 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4061 case Tabular::TOGGLE_LINE_TOP:
4062 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4063 status.setOnOff(tabular.topLine(cur.idx()));
4066 case Tabular::TOGGLE_LINE_BOTTOM:
4067 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4068 status.setOnOff(tabular.bottomLine(cur.idx()));
4071 case Tabular::TOGGLE_LINE_LEFT:
4072 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4073 status.setOnOff(tabular.leftLine(cur.idx()));
4076 case Tabular::TOGGLE_LINE_RIGHT:
4077 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4078 status.setOnOff(tabular.rightLine(cur.idx()));
4081 case Tabular::M_ALIGN_LEFT:
4083 case Tabular::ALIGN_LEFT:
4084 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4085 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4088 case Tabular::M_ALIGN_RIGHT:
4090 case Tabular::ALIGN_RIGHT:
4091 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4092 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4095 case Tabular::M_ALIGN_CENTER:
4097 case Tabular::ALIGN_CENTER:
4098 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4099 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4102 case Tabular::ALIGN_BLOCK:
4103 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4104 && !tabular.isMultiRow(cur.idx()));
4105 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4108 case Tabular::M_VALIGN_TOP:
4110 case Tabular::VALIGN_TOP:
4111 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4112 && !tabular.isMultiRow(cur.idx()));
4114 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4117 case Tabular::M_VALIGN_BOTTOM:
4119 case Tabular::VALIGN_BOTTOM:
4120 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4121 && !tabular.isMultiRow(cur.idx()));
4123 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4126 case Tabular::M_VALIGN_MIDDLE:
4128 case Tabular::VALIGN_MIDDLE:
4129 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4130 && !tabular.isMultiRow(cur.idx()));
4132 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4135 case Tabular::SET_LONGTABULAR:
4136 status.setOnOff(tabular.is_long_tabular);
4139 case Tabular::UNSET_LONGTABULAR:
4140 status.setOnOff(!tabular.is_long_tabular);
4143 case Tabular::TOGGLE_ROTATE_TABULAR:
4144 case Tabular::SET_ROTATE_TABULAR:
4145 status.setOnOff(tabular.rotate);
4148 case Tabular::TABULAR_VALIGN_TOP:
4149 status.setOnOff(tabular.tabular_valignment
4150 == Tabular::LYX_VALIGN_TOP);
4152 case Tabular::TABULAR_VALIGN_MIDDLE:
4153 status.setOnOff(tabular.tabular_valignment
4154 == Tabular::LYX_VALIGN_MIDDLE);
4156 case Tabular::TABULAR_VALIGN_BOTTOM:
4157 status.setOnOff(tabular.tabular_valignment
4158 == Tabular::LYX_VALIGN_BOTTOM);
4161 case Tabular::LONGTABULAR_ALIGN_LEFT:
4162 status.setOnOff(tabular.longtabular_alignment
4163 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4165 case Tabular::LONGTABULAR_ALIGN_CENTER:
4166 status.setOnOff(tabular.longtabular_alignment
4167 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4169 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4170 status.setOnOff(tabular.longtabular_alignment
4171 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4174 case Tabular::UNSET_ROTATE_TABULAR:
4175 status.setOnOff(!tabular.rotate);
4178 case Tabular::TOGGLE_ROTATE_CELL:
4179 case Tabular::SET_ROTATE_CELL:
4180 status.setOnOff(!oneCellHasRotationState(false,
4181 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4184 case Tabular::UNSET_ROTATE_CELL:
4185 status.setOnOff(!oneCellHasRotationState(true,
4186 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4189 case Tabular::SET_USEBOX:
4190 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4193 // every row can only be one thing:
4194 // either a footer or header or caption
4195 case Tabular::SET_LTFIRSTHEAD:
4196 status.setEnabled(sel_row_start == sel_row_end
4197 && !tabular.ltCaption(sel_row_start));
4198 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4201 case Tabular::UNSET_LTFIRSTHEAD:
4202 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4205 case Tabular::SET_LTHEAD:
4206 status.setEnabled(sel_row_start == sel_row_end
4207 && !tabular.ltCaption(sel_row_start));
4208 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4211 case Tabular::UNSET_LTHEAD:
4212 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4215 case Tabular::SET_LTFOOT:
4216 status.setEnabled(sel_row_start == sel_row_end
4217 && !tabular.ltCaption(sel_row_start));
4218 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4221 case Tabular::UNSET_LTFOOT:
4222 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4225 case Tabular::SET_LTLASTFOOT:
4226 status.setEnabled(sel_row_start == sel_row_end
4227 && !tabular.ltCaption(sel_row_start));
4228 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4231 case Tabular::UNSET_LTLASTFOOT:
4232 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4235 case Tabular::SET_LTNEWPAGE:
4236 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4239 // only one row can be the caption
4240 // and a multirow cannot be set as caption
4241 case Tabular::TOGGLE_LTCAPTION:
4242 status.setEnabled(sel_row_start == sel_row_end
4243 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4244 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4245 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4246 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4247 && (!tabular.haveLTCaption()
4248 || tabular.ltCaption(sel_row_start))
4249 && !tabular.isMultiRow(sel_row_start));
4250 status.setOnOff(tabular.ltCaption(sel_row_start));
4253 case Tabular::SET_BOOKTABS:
4254 status.setOnOff(tabular.use_booktabs);
4257 case Tabular::UNSET_BOOKTABS:
4258 status.setOnOff(!tabular.use_booktabs);
4263 status.setEnabled(false);
4269 // These are only enabled inside tabular
4270 case LFUN_CELL_BACKWARD:
4271 case LFUN_CELL_FORWARD:
4272 status.setEnabled(true);
4275 // disable these with multiple cells selected
4276 case LFUN_INSET_INSERT:
4277 case LFUN_TABULAR_INSERT:
4278 case LFUN_FLEX_INSERT:
4279 case LFUN_FLOAT_INSERT:
4280 case LFUN_FLOAT_WIDE_INSERT:
4281 case LFUN_FOOTNOTE_INSERT:
4282 case LFUN_MARGINALNOTE_INSERT:
4283 case LFUN_MATH_INSERT:
4284 case LFUN_MATH_MODE:
4285 case LFUN_MATH_MUTATE:
4286 case LFUN_MATH_DISPLAY:
4287 case LFUN_NOTE_INSERT:
4288 case LFUN_OPTIONAL_INSERT:
4289 case LFUN_BOX_INSERT:
4290 case LFUN_BRANCH_INSERT:
4291 case LFUN_PHANTOM_INSERT:
4292 case LFUN_WRAP_INSERT:
4293 case LFUN_ERT_INSERT: {
4294 if (cur.selIsMultiCell()) {
4295 status.setEnabled(false);
4298 return cell(cur.idx())->getStatus(cur, cmd, status);
4301 // disable in non-fixed-width cells
4302 case LFUN_NEWLINE_INSERT:
4303 case LFUN_BREAK_PARAGRAPH: {
4304 if (tabular.getPWidth(cur.idx()).zero()) {
4305 status.setEnabled(false);
4308 return cell(cur.idx())->getStatus(cur, cmd, status);
4311 case LFUN_NEWPAGE_INSERT:
4312 status.setEnabled(false);
4316 if (tabularStackDirty() && theClipboard().isInternal()) {
4317 if (cur.selIsMultiCell()) {
4320 getSelection(cur, rs, re, cs, ce);
4321 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4322 && paste_tabular->nrows() == re - rs + 1)
4323 status.setEnabled(true);
4325 status.setEnabled(false);
4326 status.message(_("Selection size should match clipboard content."));
4329 status.setEnabled(true);
4332 return cell(cur.idx())->getStatus(cur, cmd, status);
4334 case LFUN_INSET_SETTINGS:
4335 // relay this lfun to Inset, not to the cell.
4336 return Inset::getStatus(cur, cmd, status);
4339 // we try to handle this event in the insets dispatch function.
4340 return cell(cur.idx())->getStatus(cur, cmd, status);
4346 Inset::DisplayType InsetTabular::display() const
4348 if (tabular.is_long_tabular) {
4349 switch (tabular.longtabular_alignment) {
4350 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4352 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4354 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4364 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4366 return tabular.latex(os, runparams);
4370 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4372 os << '\n'; // output table on a new line
4373 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4374 tabular.plaintext(os, runparams, dp, false, 0);
4375 return PLAINTEXT_NEWLINE;
4379 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4384 // FIXME: Why not pass a proper DocIterator here?
4386 // if the table is inside a float it doesn't need the informaltable
4387 // wrapper. Search for it.
4388 for (master = owner(); master; master = master->owner())
4389 if (master->lyxCode() == FLOAT_CODE)
4394 os << "<informaltable>";
4397 ret += tabular.docbook(os, runparams);
4399 os << "</informaltable>";
4406 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4409 // It'd be better to be able to get this from an InsetLayout, but at present
4410 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4411 xs << html::StartTag("table");
4412 docstring ret = tabular.xhtml(xs, rp);
4413 xs << html::EndTag("table");
4418 void InsetTabular::validate(LaTeXFeatures & features) const
4420 tabular.validate(features);
4422 // It'd be better to be able to get this from an InsetLayout, but at present
4423 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4424 if (features.runparams().flavor == OutputParams::HTML)
4425 features.addPreambleSnippet("<style type=\"text/css\">\n"
4426 "table { border: 1px solid black; display: inline-block; }\n"
4427 "td { border: 1px solid black; padding: 0.5ex; }\n"
4432 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4434 return tabular.cellInset(idx);
4438 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4440 return tabular.cellInset(idx);
4444 void InsetTabular::cursorPos(BufferView const & bv,
4445 CursorSlice const & sl, bool boundary, int & x, int & y) const
4447 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4449 int const row = tabular.cellRow(sl.idx());
4450 int const col = tabular.cellColumn(sl.idx());
4452 // y offset correction
4453 for (int r = 0; r < row; ++r)
4454 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4455 + tabular.interRowSpace(r + 1);
4457 y += tabular.textVOffset(sl.idx());
4459 // x offset correction
4460 for (int c = 0; c < col; ++c)
4461 x += tabular.column_info[c].width;
4463 x += tabular.textHOffset(sl.idx());
4464 x += ADD_TO_TABULAR_WIDTH;
4469 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4473 Inset const & inset = *tabular.cellInset(cell);
4474 Point o = bv.coordCache().getInsets().xy(&inset);
4475 int const xbeg = o.x_ - tabular.textHOffset(cell);
4476 int const xend = xbeg + tabular.columnWidth(cell);
4477 row_type const row = tabular.cellRow(cell);
4478 int const ybeg = o.y_ - tabular.rowAscent(row)
4479 - tabular.interRowSpace(row);
4480 int const yend = ybeg + tabular.cellHeight(cell);
4492 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4493 // << " ybeg=" << ybeg << " yend=" << yend
4494 // << " xx=" << xx << " yy=" << yy
4495 // << " dist=" << xx + yy << endl;
4500 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4502 //lyxerr << "InsetTabular::editXY: " << this << endl;
4503 cur.setSelection(false);
4505 cur.idx() = getNearestCell(cur.bv(), x, y);
4507 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4511 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4513 cur.idx() = getNearestCell(cur.bv(), x, y);
4514 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4518 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4520 idx_type idx_min = 0;
4521 int dist_min = numeric_limits<int>::max();
4522 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4523 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4524 int const d = dist(bv, i, x, y);
4535 int InsetTabular::cellXPos(idx_type const cell) const
4537 col_type col = tabular.cellColumn(cell);
4539 for (col_type c = 0; c < col; ++c)
4540 lx += tabular.columnWidth(c);
4546 void InsetTabular::resetPos(Cursor & cur) const
4548 BufferView & bv = cur.bv();
4549 int const maxwidth = bv.workWidth();
4551 int const scx_old = scx_;
4552 int const i = cur.find(this);
4557 int const X2 = maxwidth;
4558 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4559 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4560 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4563 scx_ = X1 + 20 - x1;
4565 scx_ = X2 - 20 - x2;
4570 // only update if offset changed
4571 if (scx_ != scx_old)
4572 cur.updateFlags(Update::Force | Update::FitCursor);
4576 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4578 row_type const row = tabular.cellRow(cur.idx());
4579 col_type const col = tabular.cellColumn(cur.idx());
4581 if (isRightToLeft(cur)) {
4582 if (tabular.cellColumn(cur.idx()) == 0) {
4583 if (row == tabular.nrows() - 1)
4585 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4590 cur.idx() = tabular.getLastCellInRow(row - 1);
4592 cur.idx() = tabular.cellIndex(row, col - 1);
4595 if (tabular.isLastCell(cur.idx()))
4597 if (cur.idx() == tabular.getLastCellInRow(row))
4598 cur.idx() = tabular.cellIndex(row + 1, 0);
4600 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4601 cur.idx() = tabular.cellIndex(row, colnextcell);
4605 cur.boundary(false);
4607 if (cur.selIsMultiCell()) {
4608 cur.pit() = cur.lastpit();
4609 cur.pos() = cur.lastpos();
4617 // in visual mode, place cursor at extreme left or right
4619 switch(entry_from) {
4621 case ENTRY_DIRECTION_RIGHT:
4622 cur.posVisToRowExtremity(false /* !left */);
4624 case ENTRY_DIRECTION_LEFT:
4625 cur.posVisToRowExtremity(true /* left */);
4627 case ENTRY_DIRECTION_IGNORE:
4628 // nothing to do in this case
4632 cur.setCurrentFont();
4637 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4639 row_type const row = tabular.cellRow(cur.idx());
4640 col_type const col = tabular.cellColumn(cur.idx());
4642 if (isRightToLeft(cur)) {
4643 if (cur.idx() == tabular.getLastCellInRow(row)) {
4646 cur.idx() = tabular.getFirstCellInRow(row);
4647 cur.idx() = tabular.cellAbove(cur.idx());
4649 if (tabular.isLastCell(cur.idx()))
4651 if (cur.idx() == tabular.getLastCellInRow(row))
4652 cur.idx() = tabular.cellIndex(row + 1, 0);
4654 cur.idx() = tabular.cellIndex(row, col + 1);
4657 if (cur.idx() == 0) // first cell
4660 cur.idx() = tabular.getLastCellInRow(row - 1);
4662 cur.idx() = tabular.cellIndex(row, col - 1);
4665 if (cur.selIsMultiCell()) {
4666 cur.pit() = cur.lastpit();
4667 cur.pos() = cur.lastpos();
4672 cur.pit() = cur.lastpit();
4673 cur.pos() = cur.lastpos();
4675 // in visual mode, place cursor at extreme left or right
4677 switch(entry_from) {
4679 case ENTRY_DIRECTION_RIGHT:
4680 cur.posVisToRowExtremity(false /* !left */);
4682 case ENTRY_DIRECTION_LEFT:
4683 cur.posVisToRowExtremity(true /* left */);
4685 case ENTRY_DIRECTION_IGNORE:
4686 // nothing to do in this case
4690 cur.setCurrentFont();
4695 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4697 istringstream is(argument);
4700 if (insetCode(s) != TABULAR_CODE)
4704 size_t safe_guard = 0;
4709 if (safe_guard > 1000) {
4710 LYXERR0("parameter max count reached!");
4714 Tabular::Feature action = Tabular::LAST_ACTION;
4717 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4718 if (s != tabularFeature[i].feature)
4721 action = tabularFeature[i].action;
4724 if (action == Tabular::LAST_ACTION) {
4725 LYXERR0("Feature not found " << s);
4729 if (tabularFeature[i].need_value)
4731 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4732 tabularFeatures(cur, action, val);
4738 static void checkLongtableSpecial(Tabular::ltType & ltt,
4739 string const & special, bool & flag)
4741 if (special == "dl_above") {
4744 } else if (special == "dl_below") {
4745 ltt.bottomDL = flag;
4747 } else if (special == "empty") {
4757 bool InsetTabular::oneCellHasRotationState(bool rotated,
4758 row_type row_start, row_type row_end,
4759 col_type col_start, col_type col_end) const
4761 for (row_type r = row_start; r <= row_end; ++r)
4762 for (col_type c = col_start; c <= col_end; ++c)
4763 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4769 void InsetTabular::tabularFeatures(Cursor & cur,
4770 Tabular::Feature feature, string const & value)
4772 col_type sel_col_start;
4773 col_type sel_col_end;
4774 row_type sel_row_start;
4775 row_type sel_row_end;
4776 bool setLines = false;
4777 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4778 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4782 case Tabular::M_ALIGN_LEFT:
4783 case Tabular::ALIGN_LEFT:
4784 setAlign = LYX_ALIGN_LEFT;
4787 case Tabular::M_ALIGN_RIGHT:
4788 case Tabular::ALIGN_RIGHT:
4789 setAlign = LYX_ALIGN_RIGHT;
4792 case Tabular::M_ALIGN_CENTER:
4793 case Tabular::ALIGN_CENTER:
4794 setAlign = LYX_ALIGN_CENTER;
4797 case Tabular::ALIGN_BLOCK:
4798 setAlign = LYX_ALIGN_BLOCK;
4801 case Tabular::M_VALIGN_TOP:
4802 case Tabular::VALIGN_TOP:
4803 setVAlign = Tabular::LYX_VALIGN_TOP;
4806 case Tabular::M_VALIGN_BOTTOM:
4807 case Tabular::VALIGN_BOTTOM:
4808 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4811 case Tabular::M_VALIGN_MIDDLE:
4812 case Tabular::VALIGN_MIDDLE:
4813 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4820 cur.recordUndoInset(ATOMIC_UNDO);
4822 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4823 row_type const row = tabular.cellRow(cur.idx());
4824 col_type const column = tabular.cellColumn(cur.idx());
4826 Tabular::ltType ltt;
4830 case Tabular::SET_PWIDTH: {
4831 Length const len(value);
4832 tabular.setColumnPWidth(cur, cur.idx(), len);
4834 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4835 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4839 case Tabular::SET_MPWIDTH:
4840 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4843 case Tabular::SET_SPECIAL_COLUMN:
4844 case Tabular::SET_SPECIAL_MULTICOLUMN:
4845 if (value == "none")
4846 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
4848 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4851 case Tabular::SET_SPECIAL_MULTIROW:
4852 //FIXME: noting to do here?
4855 case Tabular::APPEND_ROW:
4856 // append the row into the tabular
4857 tabular.appendRow(cur.idx());
4860 case Tabular::APPEND_COLUMN:
4861 // append the column into the tabular
4862 tabular.appendColumn(cur.idx());
4863 cur.idx() = tabular.cellIndex(row, column);
4866 case Tabular::DELETE_ROW:
4867 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4868 tabular.deleteRow(sel_row_start);
4869 if (sel_row_start >= tabular.nrows())
4871 cur.idx() = tabular.cellIndex(sel_row_start, column);
4874 cur.setSelection(false);
4877 case Tabular::DELETE_COLUMN:
4878 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4879 tabular.deleteColumn(sel_col_start);
4880 if (sel_col_start >= tabular.ncols())
4882 cur.idx() = tabular.cellIndex(row, sel_col_start);
4885 cur.setSelection(false);
4888 case Tabular::COPY_ROW:
4889 tabular.copyRow(row);
4892 case Tabular::COPY_COLUMN:
4893 tabular.copyColumn(column);
4894 cur.idx() = tabular.cellIndex(row, column);
4897 case Tabular::SET_LINE_TOP:
4898 case Tabular::TOGGLE_LINE_TOP: {
4899 bool lineSet = (feature == Tabular::SET_LINE_TOP)
4900 ? (value == "true") : !tabular.topLine(cur.idx());
4901 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4902 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4903 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
4907 case Tabular::SET_LINE_BOTTOM:
4908 case Tabular::TOGGLE_LINE_BOTTOM: {
4909 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
4910 ? (value == "true") : !tabular.bottomLine(cur.idx());
4911 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4912 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4913 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
4917 case Tabular::SET_LINE_LEFT:
4918 case Tabular::TOGGLE_LINE_LEFT: {
4919 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
4920 ? (value == "true") : !tabular.leftLine(cur.idx());
4921 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4922 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4923 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
4927 case Tabular::SET_LINE_RIGHT:
4928 case Tabular::TOGGLE_LINE_RIGHT: {
4929 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
4930 ? (value == "true") : !tabular.rightLine(cur.idx());
4931 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4932 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4933 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
4937 case Tabular::M_ALIGN_LEFT:
4938 case Tabular::M_ALIGN_RIGHT:
4939 case Tabular::M_ALIGN_CENTER:
4941 case Tabular::ALIGN_LEFT:
4942 case Tabular::ALIGN_RIGHT:
4943 case Tabular::ALIGN_CENTER:
4944 case Tabular::ALIGN_BLOCK:
4945 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4946 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4947 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
4950 case Tabular::M_VALIGN_TOP:
4951 case Tabular::M_VALIGN_BOTTOM:
4952 case Tabular::M_VALIGN_MIDDLE:
4954 case Tabular::VALIGN_TOP:
4955 case Tabular::VALIGN_BOTTOM:
4956 case Tabular::VALIGN_MIDDLE:
4957 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4958 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4959 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
4962 case Tabular::MULTICOLUMN: {
4963 if (!cur.selection()) {
4964 // just multicol for one single cell
4965 // check whether we are completely in a multicol
4966 if (tabular.isMultiColumn(cur.idx()))
4967 tabular.unsetMultiColumn(cur.idx());
4969 tabular.setMultiColumn(cur.idx(), 1);
4972 // we have a selection so this means we just add all this
4973 // cells to form a multicolumn cell
4974 idx_type const s_start = cur.selBegin().idx();
4975 row_type const col_start = tabular.cellColumn(s_start);
4976 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
4977 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
4980 cur.setSelection(false);
4984 case Tabular::MULTIROW: {
4985 if (!cur.selection()) {
4986 // just multirow for one single cell
4987 // check whether we are completely in a multirow
4988 if (tabular.isMultiRow(cur.idx()))
4989 tabular.unsetMultiRow(cur.idx());
4991 tabular.setMultiRow(cur.idx(), 1);
4994 // we have a selection so this means we just add all this
4995 // cells to form a multirow cell
4996 idx_type const s_start = cur.selBegin().idx();
4997 row_type const row_start = tabular.cellRow(s_start);
4998 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
4999 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5002 cur.setSelection(false);
5006 case Tabular::SET_ALL_LINES:
5008 case Tabular::UNSET_ALL_LINES:
5009 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5010 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5011 idx_type const cell = tabular.cellIndex(r, c);
5012 tabular.setTopLine(cell, setLines);
5013 tabular.setBottomLine(cell, setLines);
5014 tabular.setRightLine(cell, setLines);
5015 tabular.setLeftLine(cell, setLines);
5019 case Tabular::SET_BORDER_LINES:
5020 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5021 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5022 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5024 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5025 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5026 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5030 case Tabular::SET_LONGTABULAR:
5031 tabular.is_long_tabular = true;
5034 case Tabular::UNSET_LONGTABULAR:
5035 for (row_type r = 0; r < tabular.nrows(); ++r) {
5036 if (tabular.ltCaption(r)) {
5037 cur.idx() = tabular.cellIndex(r, 0);
5040 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5043 tabular.is_long_tabular = false;
5046 case Tabular::SET_ROTATE_TABULAR:
5047 tabular.rotate = true;
5050 case Tabular::UNSET_ROTATE_TABULAR:
5051 tabular.rotate = false;
5054 case Tabular::TOGGLE_ROTATE_TABULAR:
5055 tabular.rotate = !tabular.rotate;
5058 case Tabular::TABULAR_VALIGN_TOP:
5059 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5062 case Tabular::TABULAR_VALIGN_MIDDLE:
5063 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5066 case Tabular::TABULAR_VALIGN_BOTTOM:
5067 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5070 case Tabular::LONGTABULAR_ALIGN_LEFT:
5071 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5074 case Tabular::LONGTABULAR_ALIGN_CENTER:
5075 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5078 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5079 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5084 case Tabular::SET_ROTATE_CELL:
5085 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5086 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5087 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5090 case Tabular::UNSET_ROTATE_CELL:
5091 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5092 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5093 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5096 case Tabular::TOGGLE_ROTATE_CELL:
5098 bool oneNotRotated = oneCellHasRotationState(false,
5099 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5101 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5102 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5103 tabular.setRotateCell(tabular.cellIndex(r, c),
5108 case Tabular::SET_USEBOX: {
5109 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5110 if (val == tabular.getUsebox(cur.idx()))
5111 val = Tabular::BOX_NONE;
5112 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5113 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5114 tabular.setUsebox(tabular.cellIndex(r, c), val);
5118 case Tabular::UNSET_LTFIRSTHEAD:
5120 case Tabular::SET_LTFIRSTHEAD:
5121 tabular.getRowOfLTFirstHead(row, ltt);
5122 checkLongtableSpecial(ltt, value, flag);
5123 tabular.setLTHead(row, flag, ltt, true);
5126 case Tabular::UNSET_LTHEAD:
5128 case Tabular::SET_LTHEAD:
5129 tabular.getRowOfLTHead(row, ltt);
5130 checkLongtableSpecial(ltt, value, flag);
5131 tabular.setLTHead(row, flag, ltt, false);
5134 case Tabular::UNSET_LTFOOT:
5136 case Tabular::SET_LTFOOT:
5137 tabular.getRowOfLTFoot(row, ltt);
5138 checkLongtableSpecial(ltt, value, flag);
5139 tabular.setLTFoot(row, flag, ltt, false);
5142 case Tabular::UNSET_LTLASTFOOT:
5144 case Tabular::SET_LTLASTFOOT:
5145 tabular.getRowOfLTLastFoot(row, ltt);
5146 checkLongtableSpecial(ltt, value, flag);
5147 tabular.setLTFoot(row, flag, ltt, true);
5150 case Tabular::SET_LTNEWPAGE:
5151 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5154 case Tabular::TOGGLE_LTCAPTION: {
5155 bool const set = !tabular.ltCaption(row);
5156 cur.idx() = tabular.setLTCaption(row, set);
5159 cur.setSelection(false);
5162 // When a row is set as caption, then also insert
5163 // a caption. Otherwise the LaTeX output is broken.
5164 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5165 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5167 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5168 if (lyx::getStatus(fr).enabled())
5174 case Tabular::SET_BOOKTABS:
5175 tabular.use_booktabs = true;
5178 case Tabular::UNSET_BOOKTABS:
5179 tabular.use_booktabs = false;
5182 case Tabular::SET_TOP_SPACE: {
5184 if (value == "default")
5185 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5186 tabular.row_info[r].top_space_default = true;
5187 else if (value == "none")
5188 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5189 tabular.row_info[r].top_space_default = false;
5190 tabular.row_info[r].top_space = len;
5192 else if (isValidLength(value, &len))
5193 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5194 tabular.row_info[r].top_space_default = false;
5195 tabular.row_info[r].top_space = len;
5200 case Tabular::SET_BOTTOM_SPACE: {
5202 if (value == "default")
5203 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5204 tabular.row_info[r].bottom_space_default = true;
5205 else if (value == "none")
5206 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5207 tabular.row_info[r].bottom_space_default = false;
5208 tabular.row_info[r].bottom_space = len;
5210 else if (isValidLength(value, &len))
5211 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5212 tabular.row_info[r].bottom_space_default = false;
5213 tabular.row_info[r].bottom_space = len;
5218 case Tabular::SET_INTERLINE_SPACE: {
5220 if (value == "default")
5221 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5222 tabular.row_info[r].interline_space_default = true;
5223 else if (value == "none")
5224 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5225 tabular.row_info[r].interline_space_default = false;
5226 tabular.row_info[r].interline_space = len;
5228 else if (isValidLength(value, &len))
5229 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5230 tabular.row_info[r].interline_space_default = false;
5231 tabular.row_info[r].interline_space = len;
5236 // dummy stuff just to avoid warnings
5237 case Tabular::LAST_ACTION:
5243 bool InsetTabular::copySelection(Cursor & cur)
5245 if (!cur.selection())
5250 getSelection(cur, rs, re, cs, ce);
5252 paste_tabular.reset(new Tabular(tabular));
5254 for (row_type r = 0; r < rs; ++r)
5255 paste_tabular->deleteRow(0);
5257 row_type const rows = re - rs + 1;
5258 while (paste_tabular->nrows() > rows)
5259 paste_tabular->deleteRow(rows);
5261 for (col_type c = 0; c < cs; ++c)
5262 paste_tabular->deleteColumn(0);
5264 col_type const columns = ce - cs + 1;
5265 while (paste_tabular->ncols() > columns)
5266 paste_tabular->deleteColumn(columns);
5268 paste_tabular->setBuffer(tabular.buffer());
5270 odocstringstream os;
5271 OutputParams const runparams(0);
5272 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5273 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5274 cap::copySelection(cur, os.str());
5276 // mark tabular stack dirty
5277 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5278 // when we (hopefully) have a one-for-all paste mechanism.
5279 // This must be called after cap::copySelection.
5280 dirtyTabularStack(true);
5286 bool InsetTabular::pasteClipboard(Cursor & cur)
5290 col_type actcol = tabular.cellColumn(cur.idx());
5291 row_type actrow = tabular.cellRow(cur.idx());
5293 if (cur.selIsMultiCell()) {
5296 getSelection(cur, actrow, re, actcol, ce);
5299 for (row_type r1 = 0, r2 = actrow;
5300 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5302 for (col_type c1 = 0, c2 = actcol;
5303 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5305 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5306 tabular.isPartOfMultiColumn(r2, c2))
5308 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5312 if (tabular.isPartOfMultiColumn(r2, c2)) {
5316 shared_ptr<InsetTableCell> inset(
5317 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5318 tabular.setCellInset(r2, c2, inset);
5319 // FIXME?: why do we need to do this explicitly? (EL)
5320 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5322 // FIXME: change tracking (MG)
5323 inset->setChange(Change(buffer().params().trackChanges ?
5324 Change::INSERTED : Change::UNCHANGED));
5332 void InsetTabular::cutSelection(Cursor & cur)
5334 if (!cur.selection())
5339 getSelection(cur, rs, re, cs, ce);
5340 for (row_type r = rs; r <= re; ++r) {
5341 for (col_type c = cs; c <= ce; ++c) {
5342 shared_ptr<InsetTableCell> t
5343 = cell(tabular.cellIndex(r, c));
5344 if (buffer().params().trackChanges)
5345 // FIXME: Change tracking (MG)
5346 t->setChange(Change(Change::DELETED));
5352 // cursor position might be invalid now
5353 if (cur.pit() > cur.lastpit())
5354 cur.pit() = cur.lastpit();
5355 if (cur.pos() > cur.lastpos())
5356 cur.pos() = cur.lastpos();
5357 cur.clearSelection();
5361 bool InsetTabular::isRightToLeft(Cursor & cur) const
5363 LASSERT(cur.depth() > 1, /**/);
5364 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5365 pos_type const parentpos = cur[cur.depth() - 2].pos();
5366 return parentpar.getFontSettings(buffer().params(),
5367 parentpos).language()->rightToLeft();
5371 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5374 LASSERT(stidx <= enidx, return docstring());
5376 col_type const col1 = tabular.cellColumn(stidx);
5377 col_type const col2 = tabular.cellColumn(enidx);
5378 row_type const row1 = tabular.cellRow(stidx);
5379 row_type const row2 = tabular.cellRow(enidx);
5381 for (col_type col = col1; col <= col2; col++)
5382 for (row_type row = row1; row <= row2; row++) {
5387 retval += tabular.cellInset(row, col)->asString(intoInsets);
5393 void InsetTabular::getSelection(Cursor & cur,
5394 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5396 CursorSlice const & beg = cur.selBegin();
5397 CursorSlice const & end = cur.selEnd();
5398 cs = tabular.cellColumn(beg.idx());
5399 ce = tabular.cellColumn(end.idx());
5403 rs = tabular.cellRow(beg.idx());
5404 re = tabular.cellRow(end.idx());
5410 Text * InsetTabular::getText(int idx) const
5412 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5416 void InsetTabular::setChange(Change const & change)
5418 for (idx_type idx = 0; idx < nargs(); ++idx)
5419 cell(idx)->setChange(change);
5423 void InsetTabular::acceptChanges()
5425 for (idx_type idx = 0; idx < nargs(); ++idx)
5426 cell(idx)->acceptChanges();
5430 void InsetTabular::rejectChanges()
5432 for (idx_type idx = 0; idx < nargs(); ++idx)
5433 cell(idx)->rejectChanges();
5437 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5439 return tabular.getPWidth(cell).zero();
5443 bool InsetTabular::forcePlainLayout(idx_type cell) const
5445 return !tabular.getPWidth(cell).zero();
5449 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5452 if (buf.length() <= 0)
5457 col_type maxCols = 1;
5458 size_t const len = buf.length();
5462 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5470 maxCols = max(cols, maxCols);
5476 maxCols = max(cols, maxCols);
5482 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5483 loctab = paste_tabular.get();
5485 dirtyTabularStack(true);
5488 cell = bv.cursor().idx();
5489 ocol = tabular.cellColumn(cell);
5490 row = tabular.cellRow(cell);
5494 idx_type const cells = loctab->numberofcells;
5497 rows = loctab->nrows();
5498 col_type const columns = loctab->ncols();
5500 while (cell < cells && p < len && row < rows &&
5501 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5507 // we can only set this if we are not too far right
5508 if (cols < columns) {
5509 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5510 Font const font = bv.textMetrics(&inset->text()).
5512 inset->setText(buf.substr(op, p - op), font,
5513 buffer().params().trackChanges);
5519 // we can only set this if we are not too far right
5520 if (cols < columns) {
5521 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5522 Font const font = bv.textMetrics(&inset->text()).
5524 inset->setText(buf.substr(op, p - op), font,
5525 buffer().params().trackChanges);
5530 cell = loctab->cellIndex(row, cols);
5536 // check for the last cell if there is no trailing '\n'
5537 if (cell < cells && op < len) {
5538 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5539 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5540 inset->setText(buf.substr(op, len - op), font,
5541 buffer().params().trackChanges);
5547 void InsetTabular::addPreview(DocIterator const & inset_pos,
5548 PreviewLoader & loader) const
5550 DocIterator cell_pos = inset_pos;
5552 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5553 for (row_type r = 0; r < tabular.nrows(); ++r) {
5554 for (col_type c = 0; c < tabular.ncols(); ++c) {
5555 cell_pos.top().idx() = tabular.cellIndex(r, c);
5556 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5562 bool InsetTabular::completionSupported(Cursor const & cur) const
5564 Cursor const & bvCur = cur.bv().cursor();
5565 if (&bvCur.inset() != this)
5567 return cur.text()->completionSupported(cur);
5571 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5573 return completionSupported(cur);
5577 bool InsetTabular::automaticInlineCompletion() const
5579 return lyxrc.completion_inline_text;
5583 bool InsetTabular::automaticPopupCompletion() const
5585 return lyxrc.completion_popup_text;
5589 bool InsetTabular::showCompletionCursor() const
5591 return lyxrc.completion_cursor_text;
5595 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5597 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5601 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5603 if (!completionSupported(cur))
5605 return cur.text()->completionPrefix(cur);
5609 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5611 if (!completionSupported(cur))
5614 return cur.text()->insertCompletion(cur, s, finished);
5618 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5619 Dimension & dim) const
5621 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5622 tm.completionPosAndDim(cur, x, y, dim);
5626 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5628 istringstream data(in);
5630 lex.setStream(data);
5637 if (!lex || token != "tabular") {
5638 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5642 // This is part of the inset proper that is usually swallowed
5643 // by Buffer::readInset
5645 if (!lex || token != "Tabular") {
5646 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5654 string InsetTabular::params2string(InsetTabular const & inset)
5657 data << "tabular" << ' ';
5659 data << "\\end_inset\n";