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
16 * \author Scott Kostyshak
18 * Full author contact details are available in file CREDITS.
23 #include "InsetTabular.h"
25 #include "buffer_funcs.h"
27 #include "BufferParams.h"
28 #include "BufferView.h"
29 #include "CoordCache.h"
32 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
36 #include "InsetIterator.h"
37 #include "InsetList.h"
39 #include "LaTeXFeatures.h"
43 #include "MetricsInfo.h"
44 #include "OutputParams.h"
45 #include "output_xhtml.h"
46 #include "Paragraph.h"
47 #include "ParagraphParameters.h"
48 #include "ParIterator.h"
50 #include "texstream.h"
51 #include "TextClass.h"
52 #include "TextMetrics.h"
54 #include "frontends/Application.h"
55 #include "frontends/alert.h"
56 #include "frontends/Clipboard.h"
57 #include "frontends/Painter.h"
58 #include "frontends/Selection.h"
60 #include "support/convert.h"
61 #include "support/debug.h"
62 #include "support/docstream.h"
63 #include "support/FileName.h"
64 #include "support/gettext.h"
65 #include "support/lassert.h"
66 #include "support/lstrings.h"
67 #include "support/unique_ptr.h"
76 using namespace lyx::support;
82 using cap::dirtyTabularStack;
83 using cap::tabularStackDirty;
85 using graphics::PreviewLoader;
87 using frontend::Painter;
88 using frontend::Clipboard;
90 namespace Alert = frontend::Alert;
95 int const ADD_TO_HEIGHT = 2; // in cell
96 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
97 int const default_line_space = 10; // ?
98 int const WIDTH_OF_LINE = 5; // space between double lines
102 unique_ptr<Tabular> paste_tabular;
105 struct TabularFeature {
106 Tabular::Feature action;
112 TabularFeature tabularFeature[] =
114 // the SET/UNSET actions are used by the table dialog,
115 // the TOGGLE actions by the table toolbar buttons
116 // FIXME: these values have been hardcoded in InsetMathGrid and other
118 { Tabular::APPEND_ROW, "append-row", false },
119 { Tabular::APPEND_COLUMN, "append-column", false },
120 { Tabular::DELETE_ROW, "delete-row", false },
121 { Tabular::DELETE_COLUMN, "delete-column", false },
122 { Tabular::COPY_ROW, "copy-row", false },
123 { Tabular::COPY_COLUMN, "copy-column", false },
124 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
125 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
126 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
127 { Tabular::MOVE_ROW_UP, "move-row-up", false },
128 { Tabular::SET_LINE_TOP, "set-line-top", true },
129 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
130 { Tabular::SET_LINE_LEFT, "set-line-left", true },
131 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
132 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
133 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
134 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
135 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
136 { Tabular::ALIGN_LEFT, "align-left", false },
137 { Tabular::ALIGN_RIGHT, "align-right", false },
138 { Tabular::ALIGN_CENTER, "align-center", false },
139 { Tabular::ALIGN_BLOCK, "align-block", false },
140 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
141 { Tabular::VALIGN_TOP, "valign-top", false },
142 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
143 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
144 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
145 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
146 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
147 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
148 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
149 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
150 { Tabular::MULTICOLUMN, "multicolumn", false },
151 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
152 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
153 { Tabular::MULTIROW, "multirow", false },
154 { Tabular::SET_MULTIROW, "set-multirow", false },
155 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
156 { Tabular::SET_MROFFSET, "set-mroffset", true },
157 { Tabular::SET_ALL_LINES, "set-all-lines", false },
158 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
159 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
160 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
161 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
162 { Tabular::SET_PWIDTH, "set-pwidth", true },
163 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
164 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
165 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
166 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
167 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
168 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
169 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
170 { Tabular::SET_USEBOX, "set-usebox", true },
171 { Tabular::SET_LTHEAD, "set-lthead", true },
172 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
173 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
174 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
175 { Tabular::SET_LTFOOT, "set-ltfoot", true },
176 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
177 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
178 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
179 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
180 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
181 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
182 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
183 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
184 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
185 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
186 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
187 { Tabular::SET_BOOKTABS, "set-booktabs", false },
188 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
189 { Tabular::SET_TOP_SPACE, "set-top-space", true },
190 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
191 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
192 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
193 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
194 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
195 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
196 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
197 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
198 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
199 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
200 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
201 { Tabular::SET_INNER_LINES, "set-inner-lines", false },
202 { Tabular::LAST_ACTION, "", false }
206 string const tostr(LyXAlignment const & num)
211 case LYX_ALIGN_BLOCK:
215 case LYX_ALIGN_CENTER:
217 case LYX_ALIGN_RIGHT:
219 case LYX_ALIGN_LAYOUT:
221 case LYX_ALIGN_SPECIAL:
223 case LYX_ALIGN_DECIMAL:
230 string const tostr(Tabular::HAlignment const & num)
233 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
235 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
237 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
244 string const tostr(Tabular::VAlignment const & num)
247 case Tabular::LYX_VALIGN_TOP:
249 case Tabular::LYX_VALIGN_MIDDLE:
251 case Tabular::LYX_VALIGN_BOTTOM:
258 string const tostr(Tabular::BoxType const & num)
261 case Tabular::BOX_NONE:
263 case Tabular::BOX_PARBOX:
265 case Tabular::BOX_MINIPAGE:
267 case Tabular::BOX_VARWIDTH:
274 // I would have liked a fromstr template a lot better. (Lgb)
275 bool string2type(string const & str, LyXAlignment & num)
278 num = LYX_ALIGN_NONE;
279 else if (str == "block")
280 num = LYX_ALIGN_BLOCK;
281 else if (str == "left")
282 num = LYX_ALIGN_LEFT;
283 else if (str == "center")
284 num = LYX_ALIGN_CENTER;
285 else if (str == "right")
286 num = LYX_ALIGN_RIGHT;
287 else if (str == "decimal")
288 num = LYX_ALIGN_DECIMAL;
295 bool string2type(string const & str, Tabular::HAlignment & num)
298 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
299 else if (str == "center" )
300 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
301 else if (str == "right")
302 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
309 bool string2type(string const & str, Tabular::VAlignment & num)
312 num = Tabular::LYX_VALIGN_TOP;
313 else if (str == "middle" )
314 num = Tabular::LYX_VALIGN_MIDDLE;
315 else if (str == "bottom")
316 num = Tabular::LYX_VALIGN_BOTTOM;
323 bool string2type(string const & str, Tabular::BoxType & num)
326 num = Tabular::BOX_NONE;
327 else if (str == "parbox")
328 num = Tabular::BOX_PARBOX;
329 else if (str == "minipage")
330 num = Tabular::BOX_MINIPAGE;
331 else if (str == "varwidth")
332 num = Tabular::BOX_VARWIDTH;
339 bool string2type(string const & str, bool & num)
343 else if (str == "false")
351 bool getTokenValue(string const & str, char const * token, string & ret)
354 size_t token_length = strlen(token);
355 size_t pos = str.find(token);
357 if (pos == string::npos || pos + token_length + 1 >= str.length()
358 || str[pos + token_length] != '=')
360 pos += token_length + 1;
362 if (ch != '"' && ch != '\'') { // only read till next space
366 while (pos < str.length() - 1 && str[++pos] != ch)
373 bool getTokenValue(string const & str, char const * token, docstring & ret)
376 bool const success = getTokenValue(str, token, tmp);
377 ret = from_utf8(tmp);
382 bool getTokenValue(string const & str, char const * token, int & num)
386 if (!getTokenValue(str, token, tmp))
388 num = convert<int>(tmp);
393 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
396 return getTokenValue(str, token, tmp) && string2type(tmp, num);
400 bool getTokenValue(string const & str, char const * token,
401 Tabular::HAlignment & num)
404 return getTokenValue(str, token, tmp) && string2type(tmp, num);
408 bool getTokenValue(string const & str, char const * token,
409 Tabular::VAlignment & num)
412 return getTokenValue(str, token, tmp) && string2type(tmp, num);
416 bool getTokenValue(string const & str, char const * token,
417 Tabular::BoxType & num)
420 return getTokenValue(str, token, tmp) && string2type(tmp, num);
424 bool getTokenValue(string const & str, char const * token, bool & flag)
426 // set the flag always to false as this should be the default for bools
427 // not in the file-format.
430 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
434 bool getTokenValue(string const & str, char const * token, Length & len)
436 // set the length to be zero() as default as this it should be if not
437 // in the file format.
440 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
444 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
449 if (!getTokenValue(str, token, tmp))
451 if (tmp == "default") {
455 return isValidLength(tmp, &len);
459 void l_getline(istream & is, string & str)
462 while (str.empty()) {
464 if (!str.empty() && str[str.length() - 1] == '\r')
465 str.erase(str.length() - 1);
470 string const write_attribute(string const & name, T const & t)
472 string const s = tostr(t);
473 return s.empty() ? s : " " + name + "=\"" + s + "\"";
477 string const write_attribute(string const & name, string const & t)
479 return t.empty() ? t : " " + name + "=\"" + t + "\"";
484 string const write_attribute(string const & name, docstring const & t)
486 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
491 string const write_attribute(string const & name, bool const & b)
493 // we write only true attribute values so we remove a bit of the
494 // file format bloat for tabulars.
495 return b ? write_attribute(name, convert<string>(b)) : string();
500 string const write_attribute(string const & name, int const & i)
502 // we write only true attribute values so we remove a bit of the
503 // file format bloat for tabulars.
504 return i ? write_attribute(name, convert<string>(i)) : string();
509 string const write_attribute(string const & name, Tabular::idx_type const & i)
511 // we write only true attribute values so we remove a bit of the
512 // file format bloat for tabulars.
513 return i ? write_attribute(name, convert<string>(i)) : string();
518 string const write_attribute(string const & name, Length const & value)
520 // we write only the value if we really have one same reson as above.
521 return value.zero() ? string() : write_attribute(name, value.asString());
527 string const featureAsString(Tabular::Feature action)
529 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
530 if (tabularFeature[i].action == action)
531 return tabularFeature[i].feature;
537 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
539 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
540 for (; dit; dit.forwardChar())
541 if (dit.inTexted() && dit.depth() == 1
542 && dit.paragraph().find(align_d, false, false, dit.pos()))
549 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
551 InsetTableCell tail = InsetTableCell(head);
552 DocIterator const dit = separatorPos(&head, align_d);
555 pit_type const psize = head.paragraphs().front().size();
556 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
557 tail.paragraphs().front().eraseChars(0,
558 dit.pos() < psize ? dit.pos() + 1 : psize, false);
565 /////////////////////////////////////////////////////////////////////
569 /////////////////////////////////////////////////////////////////////
572 Tabular::CellData::CellData(Buffer * buf)
575 multicolumn(Tabular::CELL_NORMAL),
576 multirow(Tabular::CELL_NORMAL),
577 alignment(LYX_ALIGN_CENTER),
578 valignment(LYX_VALIGN_TOP),
588 inset(new InsetTableCell(buf))
590 inset->setBuffer(*buf);
594 Tabular::CellData::CellData(CellData const & cs)
597 multicolumn(cs.multicolumn),
598 multirow(cs.multirow),
599 mroffset(cs.mroffset),
600 alignment(cs.alignment),
601 valignment(cs.valignment),
602 decimal_hoffset(cs.decimal_hoffset),
603 decimal_width(cs.decimal_width),
605 top_line(cs.top_line),
606 bottom_line(cs.bottom_line),
607 left_line(cs.left_line),
608 right_line(cs.right_line),
611 align_special(cs.align_special),
613 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
617 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
623 multicolumn = cs.multicolumn;
624 multirow = cs.multirow;
625 mroffset = cs.mroffset;
626 alignment = cs.alignment;
627 valignment = cs.valignment;
628 decimal_hoffset = cs.decimal_hoffset;
629 decimal_width = cs.decimal_width;
630 voffset = cs.voffset;
631 top_line = cs.top_line;
632 bottom_line = cs.bottom_line;
633 left_line = cs.left_line;
634 right_line = cs.right_line;
637 align_special = cs.align_special;
638 p_width = cs.p_width;
639 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
643 Tabular::RowData::RowData()
646 top_space_default(false),
647 bottom_space_default(false),
648 interline_space_default(false),
658 Tabular::ColumnData::ColumnData()
659 : alignment(LYX_ALIGN_CENTER),
660 valignment(LYX_VALIGN_TOP),
666 Tabular::ltType::ltType()
667 : set(false), topDL(false),
673 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
675 init(buffer, rows_arg, columns_arg);
679 void Tabular::setBuffer(Buffer & buffer)
682 for (row_type i = 0; i < nrows(); ++i)
683 for (col_type j = 0; j < ncols(); ++j)
684 cell_info[i][j].inset->setBuffer(*buffer_);
688 // activates all lines and sets all widths to 0
689 void Tabular::init(Buffer * buf, row_type rows_arg,
690 col_type columns_arg)
693 row_info = row_vector(rows_arg);
694 column_info = column_vector(columns_arg);
695 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
696 row_info.reserve(10);
697 column_info.reserve(10);
698 cell_info.reserve(100);
700 is_long_tabular = false;
701 tabular_valignment = LYX_VALIGN_MIDDLE;
702 tabular_width = Length();
703 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
705 use_booktabs = false;
706 // set silly default lines
707 for (row_type r = 0; r < nrows(); ++r)
708 for (col_type c = 0; c < ncols(); ++c) {
709 cell_info[r][c].inset->setBuffer(*buffer_);
710 cell_info[r][c].top_line = true;
711 cell_info[r][c].left_line = true;
712 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
713 cell_info[r][c].right_line = c == ncols() - 1;
718 void Tabular::deleteRow(row_type const row)
720 // Not allowed to delete last row
724 for (col_type c = 0; c < ncols(); ++c) {
725 // Care about multirow cells
726 if (row + 1 < nrows() &&
727 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
728 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
729 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
732 row_info.erase(row_info.begin() + row);
733 cell_info.erase(cell_info.begin() + row);
738 void Tabular::copyRow(row_type const row)
740 insertRow(row, true);
744 void Tabular::appendRow(row_type row)
746 insertRow(row, false);
750 void Tabular::insertRow(row_type const row, bool copy)
752 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
753 cell_info.insert(cell_info.begin() + row + 1,
754 cell_vector(0, CellData(buffer_)));
756 for (col_type c = 0; c < ncols(); ++c) {
757 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
758 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
759 if (buffer().params().track_changes)
760 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
761 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
762 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
766 for (col_type c = 0; c < ncols(); ++c) {
767 if (isPartOfMultiRow(row, c))
769 // inherit line settings
770 idx_type const i = cellIndex(row + 1, c);
771 idx_type const j = cellIndex(row, c);
772 setLeftLine(i, leftLine(j));
773 setRightLine(i, rightLine(j));
774 setTopLine(i, topLine(j));
775 if (topLine(j) && bottomLine(j)) {
776 setBottomLine(i, true);
777 setBottomLine(j, false);
779 // mark track changes
780 if (buffer().params().track_changes)
781 cellInfo(i).inset->setChange(Change(Change::INSERTED));
786 void Tabular::moveColumn(col_type col, ColDirection direction)
788 if (direction == Tabular::LEFT)
791 std::swap(column_info[col], column_info[col + 1]);
793 for (row_type r = 0; r < nrows(); ++r) {
794 std::swap(cell_info[r][col], cell_info[r][col + 1]);
795 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
796 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
798 // FIXME track changes is broken for tabular features (#8469)
799 idx_type const i = cellIndex(r, col);
800 idx_type const j = cellIndex(r, col + 1);
801 if (buffer().params().track_changes) {
802 cellInfo(i).inset->setChange(Change(Change::INSERTED));
803 cellInfo(j).inset->setChange(Change(Change::INSERTED));
810 void Tabular::moveRow(row_type row, RowDirection direction)
812 if (direction == Tabular::UP)
815 std::swap(row_info[row], row_info[row + 1]);
817 for (col_type c = 0; c < ncols(); ++c) {
818 std::swap(cell_info[row][c], cell_info[row + 1][c]);
819 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
820 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
822 // FIXME track changes is broken for tabular features (#8469)
823 idx_type const i = cellIndex(row, c);
824 idx_type const j = cellIndex(row + 1, c);
825 if (buffer().params().track_changes) {
826 cellInfo(i).inset->setChange(Change(Change::INSERTED));
827 cellInfo(j).inset->setChange(Change(Change::INSERTED));
834 void Tabular::deleteColumn(col_type const col)
836 // Not allowed to delete last column
840 for (row_type r = 0; r < nrows(); ++r) {
841 // Care about multicolumn cells
842 if (col + 1 < ncols() &&
843 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
844 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
845 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
847 cell_info[r].erase(cell_info[r].begin() + col);
849 column_info.erase(column_info.begin() + col);
854 void Tabular::copyColumn(col_type const col)
856 insertColumn(col, true);
860 void Tabular::appendColumn(col_type col)
862 insertColumn(col, false);
866 void Tabular::insertColumn(col_type const col, bool copy)
868 BufferParams const & bp = buffer().params();
869 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
871 for (row_type r = 0; r < nrows(); ++r) {
872 cell_info[r].insert(cell_info[r].begin() + col + 1,
873 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
874 if (bp.track_changes)
875 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
876 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
877 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
880 for (row_type r = 0; r < nrows(); ++r) {
881 // inherit line settings
882 idx_type const i = cellIndex(r, col + 1);
883 idx_type const j = cellIndex(r, col);
884 setBottomLine(i, bottomLine(j));
885 setTopLine(i, topLine(j));
886 setLeftLine(i, leftLine(j));
887 setRightLine(i, rightLine(j));
888 if (rightLine(i) && rightLine(j)) {
889 setRightLine(j, false);
891 if (buffer().params().track_changes)
892 cellInfo(i).inset->setChange(Change(Change::INSERTED));
897 void Tabular::updateIndexes()
902 for (row_type row = 0; row < nrows(); ++row)
903 for (col_type column = 0; column < ncols(); ++column) {
904 if (!isPartOfMultiColumn(row, column)
905 && !isPartOfMultiRow(row, column))
907 if (isPartOfMultiRow(row, column))
908 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
910 cell_info[row][column].cellno = numberofcells - 1;
913 rowofcell.resize(numberofcells);
914 columnofcell.resize(numberofcells);
916 // reset column and row of cells and update their width and alignment
917 for (row_type row = 0; row < nrows(); ++row)
918 for (col_type column = 0; column < ncols(); ++column) {
919 if (isPartOfMultiColumn(row, column))
921 // columnofcell needs to be called before setting width and aligment
922 // multirow cells inherit the width from the column width
923 if (!isPartOfMultiRow(row, column)) {
924 columnofcell[i] = column;
927 setFixedWidth(row, column);
928 if (isPartOfMultiRow(row, column))
930 cell_info[row][column].inset->setContentAlignment(
931 getAlignment(cellIndex(row, column)));
937 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
940 for (col_type c = 0; c < ncols(); ++c)
941 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
947 bool Tabular::topLine(idx_type const cell) const
949 return cellInfo(cell).top_line;
953 bool Tabular::bottomLine(idx_type const cell) const
955 return cellInfo(cell).bottom_line;
959 bool Tabular::leftLine(idx_type cell) const
963 return cellInfo(cell).left_line;
967 bool Tabular::rightLine(idx_type cell) const
971 return cellInfo(cell).right_line;
975 int Tabular::interRowSpace(row_type row) const
977 if (!row || row >= nrows())
980 int const interline_space = row_info[row - 1].interline_space_default ?
982 row_info[row - 1].interline_space.inPixels(width());
983 if (rowTopLine(row) && rowBottomLine(row - 1))
984 return interline_space + WIDTH_OF_LINE;
985 return interline_space;
989 int Tabular::interColumnSpace(idx_type cell) const
991 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
992 if (rightLine(cell) && nextcol < ncols()
993 && leftLine(cellIndex(cellRow(cell), nextcol)))
994 return WIDTH_OF_LINE;
999 int Tabular::cellWidth(idx_type cell) const
1002 col_type const span = columnSpan(cell);
1003 col_type const col = cellColumn(cell);
1004 for(col_type c = col; c < col + span ; ++c)
1005 w += column_info[c].width;
1010 int Tabular::cellHeight(idx_type cell) const
1012 row_type const span = rowSpan(cell);
1013 row_type const row = cellRow(cell);
1015 for(row_type r = row; r < row + span ; ++r) {
1016 h += rowAscent(r) + rowDescent(r);
1017 if (r != row + span - 1)
1018 h += interRowSpace(r + 1);
1025 bool Tabular::updateColumnWidths()
1027 vector<int> max_dwidth(ncols(), 0);
1028 for(col_type c = 0; c < ncols(); ++c)
1029 for(row_type r = 0; r < nrows(); ++r) {
1030 idx_type const i = cellIndex(r, c);
1031 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1032 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1035 bool update = false;
1036 // for each col get max of single col cells
1037 for(col_type c = 0; c < ncols(); ++c) {
1039 for(row_type r = 0; r < nrows(); ++r) {
1040 idx_type const i = cellIndex(r, c);
1041 if (columnSpan(i) == 1) {
1042 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1043 && cell_info[r][c].decimal_width != 0)
1044 new_width = max(new_width, cellInfo(i).width
1045 + max_dwidth[c] - cellInfo(i).decimal_width);
1047 new_width = max(new_width, cellInfo(i).width);
1051 if (column_info[c].width != new_width) {
1052 column_info[c].width = new_width;
1056 // update col widths to fit merged cells
1057 for(col_type c = 0; c < ncols(); ++c)
1058 for(row_type r = 0; r < nrows(); ++r) {
1059 idx_type const i = cellIndex(r, c);
1060 int const span = columnSpan(i);
1061 if (span == 1 || c > cellColumn(i))
1065 for(col_type j = c; j < c + span ; ++j)
1066 old_width += column_info[j].width;
1068 if (cellInfo(i).width > old_width) {
1069 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1078 int Tabular::width() const
1081 for (col_type c = 0; c < ncols(); ++c)
1082 width += column_info[c].width;
1087 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1090 col_type const col = cellColumn(cell);
1091 // set alignment for the whole row if we are not in a multicolumn cell,
1092 // exclude possible multicolumn cells in the row
1093 if (!isMultiColumn(cell)) {
1094 for (row_type r = 0; r < nrows(); ++r) {
1095 // only if the column has no width the multirow inherits the
1096 // alignment of the column, otherwise it is left aligned
1097 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1098 && !isMultiColumn(cellIndex(r, col))) {
1099 cell_info[r][col].alignment = align;
1100 cell_info[r][col].inset->setContentAlignment(align);
1102 if ((isMultiRow(cellIndex(r, col)) && has_width)
1103 && !isMultiColumn(cellIndex(r, col))) {
1104 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1105 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1108 column_info[col].alignment = align;
1109 docstring & dpoint = column_info[col].decimal_point;
1110 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1111 dpoint = from_utf8(lyxrc.default_decimal_point);
1113 cellInfo(cell).alignment = align;
1114 cellInset(cell)->setContentAlignment(align);
1119 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1122 if (!isMultiColumn(cell) || onlycolumn)
1123 column_info[cellColumn(cell)].valignment = align;
1125 cellInfo(cell).valignment = align;
1132 * Allow line and paragraph breaks for fixed width cells or disallow them,
1133 * merge cell paragraphs and reset layout to standard for variable width
1136 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1138 inset->toggleFixedWidth(fixedWidth);
1142 // merge all paragraphs to one
1143 BufferParams const & bp = cur.bv().buffer().params();
1144 while (inset->paragraphs().size() > 1)
1145 mergeParagraph(bp, inset->paragraphs(), 0);
1149 // undo information has already been recorded
1150 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1151 bp.documentClass().plainLayoutName());
1158 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1159 Length const & width)
1161 col_type const c = cellColumn(cell);
1163 column_info[c].p_width = width;
1164 // reset the vertical alignment to top if the fixed width
1165 // is removed or zero because only fixed width columns can
1166 // have a vertical alignment
1167 if (column_info[c].p_width.zero())
1168 column_info[c].valignment = LYX_VALIGN_TOP;
1169 for (row_type r = 0; r < nrows(); ++r) {
1170 idx_type const cell = cellIndex(r, c);
1171 // because of multicolumns
1172 toggleFixedWidth(cur, cellInset(cell).get(),
1173 !getPWidth(cell).zero());
1174 if (isMultiRow(cell))
1175 setAlignment(cell, LYX_ALIGN_LEFT, false);
1177 // cur paragraph can become invalid after paragraphs were merged
1178 if (cur.pit() > cur.lastpit())
1179 cur.pit() = cur.lastpit();
1180 // cur position can become invalid after newlines were removed
1181 if (cur.pos() > cur.lastpos())
1182 cur.pos() = cur.lastpos();
1186 bool Tabular::setFixedWidth(row_type r, col_type c)
1188 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1189 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1190 || (multicol && !cell_info[r][c].p_width.zero());
1191 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1196 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1197 Length const & width)
1199 if (!isMultiColumn(cell))
1202 cellInfo(cell).p_width = width;
1203 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1204 // cur paragraph can become invalid after paragraphs were merged
1205 if (cur.pit() > cur.lastpit())
1206 cur.pit() = cur.lastpit();
1207 // cur position can become invalid after newlines were removed
1208 if (cur.pos() > cur.lastpos())
1209 cur.pos() = cur.lastpos();
1214 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1216 cellInfo(cell).mroffset = mroffset;
1221 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1222 Tabular::Feature what)
1224 if (what == SET_SPECIAL_MULTICOLUMN)
1225 cellInfo(cell).align_special = special;
1227 column_info[cellColumn(cell)].align_special = special;
1231 void Tabular::setTopLine(idx_type i, bool line)
1233 cellInfo(i).top_line = line;
1237 void Tabular::setBottomLine(idx_type i, bool line)
1239 cellInfo(i).bottom_line = line;
1243 void Tabular::setLeftLine(idx_type cell, bool line)
1245 cellInfo(cell).left_line = line;
1249 void Tabular::setRightLine(idx_type cell, bool line)
1251 cellInfo(cell).right_line = line;
1254 bool Tabular::rowTopLine(row_type r) const
1256 bool all_rows_set = true;
1257 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1258 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1259 return all_rows_set;
1263 bool Tabular::rowBottomLine(row_type r) const
1265 bool all_rows_set = true;
1266 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1267 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1268 return all_rows_set;
1272 bool Tabular::columnLeftLine(col_type c) const
1279 for (row_type r = 0; r < nrows(); ++r) {
1280 idx_type const i = cellIndex(r, c);
1281 if (c == cellColumn(i)) {
1283 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1284 if (cellInfo(i).left_line || right)
1288 return 2 * nrows_left >= total;
1292 bool Tabular::columnRightLine(col_type c) const
1297 int nrows_right = 0;
1299 for (row_type r = 0; r < nrows(); ++r) {
1300 idx_type i = cellIndex(r, c);
1301 if (c == cellColumn(i) + columnSpan(i) - 1) {
1303 bool left = (c + 1 < ncols()
1304 && cellInfo(cellIndex(r, c + 1)).left_line)
1305 || c + 1 == ncols();
1306 if (cellInfo(i).right_line && left)
1310 return 2 * nrows_right >= total;
1314 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1316 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1317 return cellInfo(cell).alignment;
1319 return column_info[cellColumn(cell)].alignment;
1324 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1326 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1327 return cellInfo(cell).valignment;
1328 return column_info[cellColumn(cell)].valignment;
1332 int Tabular::offsetVAlignment() const
1334 // for top-alignment the first horizontal table line must be exactly at
1335 // the position of the base line of the surrounding text line
1336 // for bottom alignment, the same is for the last table line
1337 int offset_valign = 0;
1338 switch (tabular_valignment) {
1339 case Tabular::LYX_VALIGN_BOTTOM:
1340 offset_valign = rowAscent(0) - height();
1342 case Tabular::LYX_VALIGN_MIDDLE:
1343 offset_valign = (- height()) / 2 + rowAscent(0);
1345 case Tabular::LYX_VALIGN_TOP:
1346 offset_valign = rowAscent(0);
1349 return offset_valign;
1353 Length const Tabular::getPWidth(idx_type cell) const
1355 if (isMultiColumn(cell))
1356 return cellInfo(cell).p_width;
1357 return column_info[cellColumn(cell)].p_width;
1361 Length const Tabular::getMROffset(idx_type cell) const
1363 return cellInfo(cell).mroffset;
1367 int Tabular::textHOffset(idx_type cell) const
1369 // the LaTeX Way :-(
1370 int x = WIDTH_OF_LINE;
1372 int const w = cellWidth(cell) - cellInfo(cell).width;
1374 switch (getAlignment(cell)) {
1375 case LYX_ALIGN_CENTER:
1378 case LYX_ALIGN_RIGHT:
1381 case LYX_ALIGN_DECIMAL: {
1382 // we center when no decimal point
1383 if (cellInfo(cell).decimal_width == 0) {
1387 col_type const c = cellColumn(cell);
1388 int max_dhoffset = 0;
1389 for(row_type r = 0; r < row_info.size() ; ++r) {
1390 idx_type const i = cellIndex(r, c);
1391 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1392 && cellInfo(i).decimal_width != 0)
1393 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1395 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1398 // LYX_ALIGN_LEFT: nothing :-)
1406 int Tabular::textVOffset(idx_type cell) const
1408 int voffset = cellInfo(cell).voffset;
1409 if (isMultiRow(cell)) {
1410 row_type const row = cellRow(cell);
1411 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1417 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1420 idx_type const numcells = numberOfCellsInRow(row);
1421 // we check against numcells to make sure we do not crash if all the
1422 // cells are multirow (bug #7535), but in that case our return value
1423 // is really invalid, i.e., it is NOT the first cell in the row. but
1424 // i do not know what to do here. (rgh)
1425 while (c < numcells - 1
1426 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1428 return cell_info[row][c].cellno;
1432 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1434 col_type c = ncols() - 1;
1435 // of course we check against 0 so we don't crash. but we have the same
1436 // problem as in the previous routine: if all the cells are part of a
1437 // multirow or part of a multi column, then our return value is invalid.
1439 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1440 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1442 return cell_info[row][c].cellno;
1446 Tabular::row_type Tabular::cellRow(idx_type cell) const
1448 if (cell >= numberofcells)
1452 return rowofcell[cell];
1456 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1458 if (cell >= numberofcells)
1462 return columnofcell[cell];
1466 void Tabular::write(ostream & os) const
1470 << write_attribute("version", 3)
1471 << write_attribute("rows", nrows())
1472 << write_attribute("columns", ncols())
1474 // global longtable options
1476 << write_attribute("rotate", rotate)
1477 << write_attribute("booktabs", use_booktabs)
1478 << write_attribute("islongtable", is_long_tabular)
1479 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1480 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1481 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1482 << write_attribute("headTopDL", endhead.topDL)
1483 << write_attribute("headBottomDL", endhead.bottomDL)
1484 << write_attribute("footTopDL", endfoot.topDL)
1485 << write_attribute("footBottomDL", endfoot.bottomDL)
1486 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1487 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1488 << write_attribute("lastFootEmpty", endlastfoot.empty);
1489 // longtables cannot be aligned vertically
1490 if (!is_long_tabular) {
1491 os << write_attribute("tabularvalignment", tabular_valignment);
1492 os << write_attribute("tabularwidth", tabular_width);
1494 if (is_long_tabular)
1495 os << write_attribute("longtabularalignment", longtabular_alignment);
1497 for (col_type c = 0; c < ncols(); ++c) {
1499 << write_attribute("alignment", column_info[c].alignment);
1500 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1501 os << write_attribute("decimal_point", column_info[c].decimal_point);
1502 os << write_attribute("valignment", column_info[c].valignment)
1503 << write_attribute("width", column_info[c].p_width.asString())
1504 << write_attribute("special", column_info[c].align_special)
1507 for (row_type r = 0; r < nrows(); ++r) {
1508 static const string def("default");
1510 if (row_info[r].top_space_default)
1511 os << write_attribute("topspace", def);
1513 os << write_attribute("topspace", row_info[r].top_space);
1514 if (row_info[r].bottom_space_default)
1515 os << write_attribute("bottomspace", def);
1517 os << write_attribute("bottomspace", row_info[r].bottom_space);
1518 if (row_info[r].interline_space_default)
1519 os << write_attribute("interlinespace", def);
1521 os << write_attribute("interlinespace", row_info[r].interline_space);
1522 os << write_attribute("endhead", row_info[r].endhead)
1523 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1524 << write_attribute("endfoot", row_info[r].endfoot)
1525 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1526 << write_attribute("newpage", row_info[r].newpage)
1527 << write_attribute("caption", row_info[r].caption)
1529 for (col_type c = 0; c < ncols(); ++c) {
1531 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1532 << write_attribute("multirow", cell_info[r][c].multirow)
1533 << write_attribute("mroffset", cell_info[r][c].mroffset)
1534 << write_attribute("alignment", cell_info[r][c].alignment)
1535 << write_attribute("valignment", cell_info[r][c].valignment)
1536 << write_attribute("topline", cell_info[r][c].top_line)
1537 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1538 << write_attribute("leftline", cell_info[r][c].left_line)
1539 << write_attribute("rightline", cell_info[r][c].right_line)
1540 << write_attribute("rotate", cell_info[r][c].rotate)
1541 << write_attribute("usebox", cell_info[r][c].usebox)
1542 << write_attribute("width", cell_info[r][c].p_width)
1543 << write_attribute("special", cell_info[r][c].align_special)
1545 os << "\\begin_inset ";
1546 cell_info[r][c].inset->write(os);
1547 os << "\n\\end_inset\n"
1549 // FIXME This can be removed again once the mystery
1550 // crash has been resolved.
1555 os << "</lyxtabular>\n";
1559 void Tabular::read(Lexer & lex)
1562 istream & is = lex.getStream();
1564 l_getline(is, line);
1565 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1566 LASSERT(false, return);
1570 if (!getTokenValue(line, "version", version))
1572 LATTEST(version >= 2);
1575 if (!getTokenValue(line, "rows", rows_arg))
1578 if (!getTokenValue(line, "columns", columns_arg))
1580 init(buffer_, rows_arg, columns_arg);
1581 l_getline(is, line);
1582 if (!prefixIs(line, "<features")) {
1583 lyxerr << "Wrong tabular format (expected <features ...> got"
1584 << line << ')' << endl;
1587 getTokenValue(line, "rotate", rotate);
1588 getTokenValue(line, "booktabs", use_booktabs);
1589 getTokenValue(line, "islongtable", is_long_tabular);
1590 getTokenValue(line, "tabularvalignment", tabular_valignment);
1591 getTokenValue(line, "tabularwidth", tabular_width);
1592 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1593 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1594 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1595 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1596 getTokenValue(line, "headTopDL", endhead.topDL);
1597 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1598 getTokenValue(line, "footTopDL", endfoot.topDL);
1599 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1600 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1601 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1602 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1604 for (col_type c = 0; c < ncols(); ++c) {
1606 if (!prefixIs(line,"<column")) {
1607 lyxerr << "Wrong tabular format (expected <column ...> got"
1608 << line << ')' << endl;
1611 getTokenValue(line, "alignment", column_info[c].alignment);
1612 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1613 getTokenValue(line, "valignment", column_info[c].valignment);
1614 getTokenValue(line, "width", column_info[c].p_width);
1615 getTokenValue(line, "special", column_info[c].align_special);
1618 for (row_type i = 0; i < nrows(); ++i) {
1619 l_getline(is, line);
1620 if (!prefixIs(line, "<row")) {
1621 lyxerr << "Wrong tabular format (expected <row ...> got"
1622 << line << ')' << endl;
1625 getTokenValue(line, "topspace", row_info[i].top_space,
1626 row_info[i].top_space_default);
1627 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1628 row_info[i].bottom_space_default);
1629 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1630 row_info[i].interline_space_default);
1631 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1632 getTokenValue(line, "endhead", row_info[i].endhead);
1633 getTokenValue(line, "endfoot", row_info[i].endfoot);
1634 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1635 getTokenValue(line, "newpage", row_info[i].newpage);
1636 getTokenValue(line, "caption", row_info[i].caption);
1637 for (col_type j = 0; j < ncols(); ++j) {
1638 l_getline(is, line);
1639 if (!prefixIs(line, "<cell")) {
1640 lyxerr << "Wrong tabular format (expected <cell ...> got"
1641 << line << ')' << endl;
1644 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1645 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1646 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1647 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1648 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1649 getTokenValue(line, "topline", cell_info[i][j].top_line);
1650 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1651 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1652 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1653 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1654 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1655 getTokenValue(line, "width", cell_info[i][j].p_width);
1657 getTokenValue(line, "special", cell_info[i][j].align_special);
1658 l_getline(is, line);
1659 if (prefixIs(line, "\\begin_inset")) {
1660 cell_info[i][j].inset->setBuffer(*buffer_);
1661 cell_info[i][j].inset->read(lex);
1662 l_getline(is, line);
1664 if (!prefixIs(line, "</cell>")) {
1665 lyxerr << "Wrong tabular format (expected </cell> got"
1666 << line << ')' << endl;
1670 l_getline(is, line);
1671 if (!prefixIs(line, "</row>")) {
1672 lyxerr << "Wrong tabular format (expected </row> got"
1673 << line << ')' << endl;
1677 while (!prefixIs(line, "</lyxtabular>")) {
1678 l_getline(is, line);
1684 bool Tabular::isMultiColumn(idx_type cell) const
1686 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1687 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1691 bool Tabular::hasMultiColumn(col_type c) const
1693 for (row_type r = 0; r < nrows(); ++r) {
1694 if (isMultiColumn(cellIndex(r, c)))
1701 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1703 return cell_info[cellRow(cell)][cellColumn(cell)];
1707 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1709 return cell_info[cellRow(cell)][cellColumn(cell)];
1713 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1714 bool const right_border)
1716 idx_type const col = cellColumn(cell);
1717 idx_type const row = cellRow(cell);
1718 for (idx_type i = 0; i < number; ++i)
1719 unsetMultiRow(cellIndex(row, col + i));
1721 // unsetting of multirow may have invalidated cell index
1722 cell = cellIndex(row, col);
1723 CellData & cs = cellInfo(cell);
1724 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1725 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1726 cs.alignment = column_info[col].alignment;
1727 setRightLine(cell, right_border);
1729 idx_type lastcell = cellIndex(row, col + number - 1);
1730 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1731 CellData & cs1 = cellInfo(cell + i);
1732 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1733 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1741 bool Tabular::isMultiRow(idx_type cell) const
1743 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1744 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1747 bool Tabular::hasMultiRow(row_type r) const
1749 for (col_type c = 0; c < ncols(); ++c) {
1750 if (isMultiRow(cellIndex(r, c)))
1756 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1757 bool const bottom_border,
1758 LyXAlignment const halign)
1760 idx_type const col = cellColumn(cell);
1761 idx_type const row = cellRow(cell);
1762 for (idx_type i = 0; i < number; ++i)
1763 unsetMultiColumn(cellIndex(row + i, col));
1765 // unsetting of multirow may have invalidated cell index
1766 cell = cellIndex(row, col);
1767 CellData & cs = cellInfo(cell);
1768 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1769 cs.valignment = LYX_VALIGN_MIDDLE;
1770 // the horizontal alignment of multirow cells can only
1771 // be changed for the whole table row,
1772 // support changing this only for the multirow cell can be done via
1774 if (getPWidth(cell).zero())
1775 cs.alignment = halign;
1777 cs.alignment = LYX_ALIGN_LEFT;
1779 // set the bottom line of the last selected cell
1780 setBottomLine(cell, bottom_border);
1782 for (idx_type i = 1; i < number; ++i) {
1783 CellData & cs1 = cell_info[row + i][col];
1784 cs1.multirow = CELL_PART_OF_MULTIROW;
1785 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1793 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1795 row_type const row = cellRow(cell);
1796 col_type const col = cellColumn(cell);
1798 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1805 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1807 col_type const column = cellColumn(cell);
1808 col_type row = cellRow(cell) + 1;
1809 while (row < nrows() && isPartOfMultiRow(row, column))
1812 return row - cellRow(cell);
1816 void Tabular::unsetMultiColumn(idx_type cell)
1818 if (!isMultiColumn(cell))
1821 row_type const row = cellRow(cell);
1822 col_type const col = cellColumn(cell);
1823 row_type const span = columnSpan(cell);
1824 for (col_type c = 0; c < span; ++c) {
1825 // in the table dialog the lines are set in every case
1826 // when unsetting a multicolumn this leads to an additional right
1827 // line for every cell that was part of the former multicolumn cell,
1828 // except if the cell is in the last column
1829 // therefore remove this line
1830 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1831 && (col + c) < (col + span - 1))
1832 cell_info[row][col + c].right_line = false;
1833 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1839 void Tabular::unsetMultiRow(idx_type cell)
1841 if (!isMultiRow(cell))
1844 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1845 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1846 row_type const row = cellRow(cell);
1847 col_type const col = cellColumn(cell);
1848 row_type const span = rowSpan(cell);
1849 for (row_type r = 0; r < span; ++r)
1850 cell_info[row + r][col].multirow = CELL_NORMAL;
1855 void Tabular::setRotateCell(idx_type cell, int value)
1857 cellInfo(cell).rotate = value;
1861 int Tabular::getRotateCell(idx_type cell) const
1863 return cellInfo(cell).rotate;
1867 bool Tabular::needRotating() const
1869 if (rotate && !is_long_tabular)
1871 for (row_type r = 0; r < nrows(); ++r)
1872 for (col_type c = 0; c < ncols(); ++c)
1873 if (cell_info[r][c].rotate != 0)
1879 bool Tabular::isLastCell(idx_type cell) const
1881 if (cell + 1 < numberofcells)
1887 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1889 if (cellRow(cell) == 0)
1892 col_type const col = cellColumn(cell);
1893 row_type r = cellRow(cell) - 1;
1894 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1897 return cell_info[r][col].cellno;
1901 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1903 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1904 if (nextrow < nrows())
1905 return cell_info[nextrow][cellColumn(cell)].cellno;
1910 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1912 LASSERT(column != npos && column < ncols(), column = 0);
1913 LASSERT(row != npos && row < nrows(), row = 0);
1914 return cell_info[row][column].cellno;
1918 void Tabular::setUsebox(idx_type cell, BoxType type)
1920 cellInfo(cell).usebox = type;
1924 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1926 if (getRotateCell(cell) == 0
1927 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1928 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
1930 if (cellInfo(cell).usebox > 1)
1931 return cellInfo(cell).usebox;
1932 return useParbox(cell);
1937 // This are functions used for the longtable support
1939 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1945 row_info[row].endfirsthead = flag;
1949 row_info[row].endhead = flag;
1954 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1957 hd.set = haveLTHead();
1958 return row_info[row].endhead;
1962 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1965 hd.set = haveLTFirstHead();
1966 return row_info[row].endfirsthead;
1970 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1976 row_info[row].endlastfoot = flag;
1980 row_info[row].endfoot = flag;
1985 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1988 fd.set = haveLTFoot();
1989 return row_info[row].endfoot;
1993 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1996 fd.set = haveLTLastFoot();
1997 return row_info[row].endlastfoot;
2001 void Tabular::setLTNewPage(row_type row, bool what)
2003 row_info[row].newpage = what;
2007 bool Tabular::getLTNewPage(row_type row) const
2009 return row_info[row].newpage;
2013 bool Tabular::haveLTHead(bool withcaptions) const
2015 if (!is_long_tabular)
2017 for (row_type i = 0; i < nrows(); ++i)
2018 if (row_info[i].endhead &&
2019 (withcaptions || !row_info[i].caption))
2025 bool Tabular::haveLTFirstHead(bool withcaptions) const
2027 if (!is_long_tabular || endfirsthead.empty)
2029 for (row_type r = 0; r < nrows(); ++r)
2030 if (row_info[r].endfirsthead &&
2031 (withcaptions || !row_info[r].caption))
2037 bool Tabular::haveLTFoot(bool withcaptions) const
2039 if (!is_long_tabular)
2041 for (row_type r = 0; r < nrows(); ++r)
2042 if (row_info[r].endfoot &&
2043 (withcaptions || !row_info[r].caption))
2049 bool Tabular::haveLTLastFoot(bool withcaptions) const
2051 if (!is_long_tabular || endlastfoot.empty)
2053 for (row_type r = 0; r < nrows(); ++r)
2054 if (row_info[r].endlastfoot &&
2055 (withcaptions || !row_info[r].caption))
2061 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2063 idx_type i = getFirstCellInRow(row);
2065 setMultiColumn(i, numberOfCellsInRow(row), false);
2066 setTopLine(i, false);
2067 setBottomLine(i, false);
2068 setLeftLine(i, false);
2069 setRightLine(i, false);
2070 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2071 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2072 setLTHead(row, true, endfirsthead, true);
2073 row_info[row].endfirsthead = true;
2076 unsetMultiColumn(i);
2077 // When unsetting a caption row, also all existing
2078 // captions in this row must be dissolved.
2080 row_info[row].caption = what;
2085 bool Tabular::ltCaption(row_type row) const
2087 return row_info[row].caption;
2091 bool Tabular::haveLTCaption(CaptionType captiontype) const
2093 if (!is_long_tabular)
2095 for (row_type r = 0; r < nrows(); ++r) {
2096 if (row_info[r].caption) {
2097 switch (captiontype) {
2098 case CAPTION_FIRSTHEAD:
2099 if (row_info[r].endfirsthead)
2103 if (row_info[r].endhead)
2107 if (row_info[r].endfoot)
2110 case CAPTION_LASTFOOT:
2111 if (row_info[r].endlastfoot)
2123 // end longtable support functions
2125 void Tabular::setRowAscent(row_type row, int height)
2127 if (row >= nrows() || row_info[row].ascent == height)
2129 row_info[row].ascent = height;
2133 void Tabular::setRowDescent(row_type row, int height)
2135 if (row >= nrows() || row_info[row].descent == height)
2137 row_info[row].descent = height;
2141 int Tabular::rowAscent(row_type row) const
2143 LASSERT(row < nrows(), row = 0);
2144 return row_info[row].ascent;
2148 int Tabular::rowDescent(row_type row) const
2150 LASSERT(row < nrows(), row = 0);
2151 return row_info[row].descent;
2155 int Tabular::height() const
2158 for (row_type row = 0; row < nrows(); ++row)
2159 height += rowAscent(row) + rowDescent(row) +
2165 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2167 LASSERT(row < nrows(), return false);
2168 LASSERT(column < ncols(), return false);
2169 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2173 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2175 LASSERT(row < nrows(), return false);
2176 LASSERT(column < ncols(), return false);
2177 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2181 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang,
2182 list<col_type> columns) const
2184 // we only output complete row lines and the 1st row here, the rest
2185 // is done in Tabular::TeXBottomHLine(...)
2187 // get for each column the topline (if any)
2188 map<col_type, bool> topline;
2190 for (auto const & c : columns) {
2191 topline[c] = topLine(cellIndex(row, c));
2192 // If cell is part of a multirow and not the first cell of the
2193 // multirow, no line must be drawn.
2195 if (isMultiRow(cellIndex(row, c))
2196 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2198 if (topline.find(c) != topline.end() && topline.find(c)->second)
2202 // do nothing if empty first row, or incomplete row line after
2203 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2206 // only output complete row lines and the 1st row's clines
2207 if (nset == ncols()) {
2209 os << (row == 0 ? "\\toprule " : "\\midrule ");
2213 } else if (row == 0) {
2214 for (auto & c : columns) {
2215 if (topline.find(c)->second) {
2216 col_type offset = 0;
2217 for (col_type j = 0 ; j < c; ++j)
2218 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2221 //babel makes the "-" character an active one, so we have to suppress this here
2222 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2223 if (lang == "slovak" || lang == "czech")
2224 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2225 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2227 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2229 col_type cstart = c;
2230 for ( ; c < ncols() && topline.find(c)->second; ++c) {}
2232 for (col_type j = cstart ; j < c ; ++j)
2233 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2236 os << c + offset << "} ";
2244 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang,
2245 list<col_type> columns) const
2247 // we output bottomlines of row r and the toplines of row r+1
2248 // if the latter do not span the whole tabular
2250 // get the bottomlines of row r, and toplines in next row
2251 bool lastrow = row == nrows() - 1;
2252 map<col_type, bool> bottomline, topline;
2253 bool nextrowset = true;
2254 for (auto const & c : columns) {
2255 bottomline[c] = bottomLine(cellIndex(row, c));
2256 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2257 // If cell is part of a multirow and not the last cell of the
2258 // multirow, no line must be drawn.
2260 if (isMultiRow(cellIndex(row, c))
2261 && isMultiRow(cellIndex(row + 1, c))
2262 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2263 bottomline[c] = false;
2266 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2269 // combine this row's bottom lines and next row's toplines if necessary
2271 for (auto const & c : columns) {
2273 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2274 if (bottomline.find(c)->second)
2278 // do nothing if empty, OR incomplete row line with a topline in next row
2279 if (nset == 0 || (nextrowset && nset != ncols()))
2282 if (nset == ncols()) {
2284 os << (lastrow ? "\\bottomrule" : "\\midrule");
2288 for (auto & c : columns) {
2289 if (bottomline.find(c)->second) {
2290 col_type offset = 0;
2291 for (col_type j = 0 ; j < c; ++j)
2292 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2295 //babel makes the "-" character an active one, so we have to suppress this here
2296 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2297 if (lang == "slovak" || lang == "czech")
2298 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2299 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2301 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2303 col_type cstart = c;
2304 for ( ; c < ncols() && bottomline.find(c)->second; ++c) {}
2306 for (col_type j = cstart ; j < c ; ++j)
2307 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2310 os << c + offset << "} ";
2318 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2319 bool & ismulticol, bool & ismultirow,
2320 bool const bidi) const
2322 row_type const r = cellRow(cell);
2323 if (is_long_tabular && row_info[r].caption)
2326 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2327 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2328 // figure out how to set the lines
2329 // we always set double lines to the right of the cell
2330 // or left in bidi RTL, respectively.
2331 col_type const c = cellColumn(cell);
2332 col_type const nextcol = c + columnSpan(cell);
2333 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2334 bool colright = columnRightLine(c);
2335 bool colleft = columnLeftLine(c);
2336 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2337 bool nextcellleft = nextcol < ncols()
2338 && leftLine(cellIndex(r, nextcol));
2339 bool coldouble = colright && nextcolleft;
2340 bool celldouble = rightLine(cell) && nextcellleft;
2342 ismulticol = (isMultiColumn(cell)
2343 || (c == 0 && colleft != leftLine(cell))
2344 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2345 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2346 || (coldouble != celldouble))
2349 // we center in multicol when no decimal point
2351 docstring const align_d = column_info[c].decimal_point;
2352 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2353 bool const nosep = !dit;
2354 ismulticol |= nosep;
2355 celldouble &= nosep;
2358 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2359 int latexcolspan = columnSpan(cell);
2360 for (col_type col = c; col < c + columnSpan(cell); ++col)
2361 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2365 os << "\\multicolumn{" << latexcolspan << "}{";
2366 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2367 || (!bidi && c == 0 && leftLine(cell))))
2369 if (bidi && celldouble)
2370 // add extra vertical line if we want a double one
2372 if (!cellInfo(cell).align_special.empty()) {
2373 os << cellInfo(cell).align_special;
2375 if (!getPWidth(cell).zero()) {
2377 case LYX_ALIGN_LEFT:
2378 os << ">{\\raggedright}";
2380 case LYX_ALIGN_RIGHT:
2381 os << ">{\\raggedleft}";
2383 case LYX_ALIGN_CENTER:
2384 os << ">{\\centering}";
2390 case LYX_VALIGN_TOP:
2393 case LYX_VALIGN_MIDDLE:
2396 case LYX_VALIGN_BOTTOM:
2401 << from_ascii(getPWidth(cell).asLatexString())
2405 case LYX_ALIGN_LEFT:
2408 case LYX_ALIGN_RIGHT:
2415 } // end if else !getPWidth
2416 } // end if else !cellinfo_of_cell
2417 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2419 if (!bidi && celldouble)
2420 // add extra vertical line if we want a double one
2423 } // end if ismulticol
2425 // we only need code for the first multirow cell
2426 ismultirow = isMultiRow(cell);
2428 os << "\\multirow{" << rowSpan(cell) << "}{";
2429 if (!getPWidth(cell).zero())
2430 os << from_ascii(getPWidth(cell).asLatexString());
2432 // we need to set a default value
2435 if (!getMROffset(cell).zero())
2436 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2438 } // end if ismultirow
2440 if (getRotateCell(cell) != 0)
2441 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2443 if (getUsebox(cell) == BOX_PARBOX) {
2446 case LYX_VALIGN_TOP:
2449 case LYX_VALIGN_MIDDLE:
2452 case LYX_VALIGN_BOTTOM:
2456 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2458 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2459 os << "\\begin{minipage}[";
2461 case LYX_VALIGN_TOP:
2464 case LYX_VALIGN_MIDDLE:
2467 case LYX_VALIGN_BOTTOM:
2471 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2473 } else if (getUsebox(cell) == BOX_VARWIDTH) {
2474 os << "\\begin{varwidth}[";
2476 case LYX_VALIGN_TOP:
2479 case LYX_VALIGN_MIDDLE:
2482 case LYX_VALIGN_BOTTOM:
2486 os << "]{\\linewidth}\n";
2491 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2492 bool ismulticol, bool ismultirow) const
2494 row_type const r = cellRow(cell);
2495 if (is_long_tabular && row_info[r].caption)
2499 if (getUsebox(cell) == BOX_PARBOX)
2501 else if (getUsebox(cell) == BOX_MINIPAGE)
2502 os << breakln << "\\end{minipage}";
2503 else if (getUsebox(cell) == BOX_VARWIDTH)
2504 os << breakln << "\\end{varwidth}";
2505 if (getRotateCell(cell) != 0)
2506 os << breakln << "\\end{turn}";
2514 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2515 OutputParams const & runparams,
2516 list<col_type> columns) const
2518 if (!is_long_tabular)
2522 // output caption which is in no header or footer
2523 if (haveLTCaption()) {
2524 for (row_type r = 0; r < nrows(); ++r) {
2525 if (row_info[r].caption &&
2526 !row_info[r].endfirsthead && !row_info[r].endhead &&
2527 !row_info[r].endfoot && !row_info[r].endlastfoot)
2528 TeXRow(os, r, runparams, columns);
2531 // output first header info
2532 if (haveLTFirstHead()) {
2533 if (endfirsthead.topDL)
2535 for (row_type r = 0; r < nrows(); ++r) {
2536 if (row_info[r].endfirsthead)
2537 TeXRow(os, r, runparams, columns);
2539 if (endfirsthead.bottomDL)
2541 os << "\\endfirsthead\n";
2543 // output header info
2545 if (endfirsthead.empty && !haveLTFirstHead())
2546 os << "\\endfirsthead\n";
2549 for (row_type r = 0; r < nrows(); ++r) {
2550 if (row_info[r].endhead)
2551 TeXRow(os, r, runparams, columns);
2553 if (endhead.bottomDL)
2555 os << "\\endhead\n";
2557 // output footer info
2561 for (row_type r = 0; r < nrows(); ++r) {
2562 if (row_info[r].endfoot)
2563 TeXRow(os, r, runparams, columns);
2565 if (endfoot.bottomDL)
2567 os << "\\endfoot\n";
2568 if (endlastfoot.empty && !haveLTLastFoot())
2569 os << "\\endlastfoot\n";
2571 // output lastfooter info
2572 if (haveLTLastFoot()) {
2573 if (endlastfoot.topDL)
2575 for (row_type r = 0; r < nrows(); ++r) {
2576 if (row_info[r].endlastfoot)
2577 TeXRow(os, r, runparams, columns);
2579 if (endlastfoot.bottomDL)
2581 os << "\\endlastfoot\n";
2586 bool Tabular::isValidRow(row_type row) const
2588 if (!is_long_tabular)
2590 return !row_info[row].endhead && !row_info[row].endfirsthead
2591 && !row_info[row].endfoot && !row_info[row].endlastfoot
2592 && !row_info[row].caption;
2596 void Tabular::TeXRow(otexstream & os, row_type row,
2597 OutputParams const & runparams,
2598 list<col_type> columns) const
2600 idx_type cell = cellIndex(row, 0);
2601 InsetTableCell const * inset = cellInset(cell);
2602 Paragraph const & par = inset->paragraphs().front();
2603 string const lang = par.getParLanguage(buffer().params())->lang();
2605 //output the top line
2606 TeXTopHLine(os, row, lang, columns);
2608 if (row_info[row].top_space_default) {
2610 os << "\\addlinespace\n";
2612 os << "\\noalign{\\vskip\\doublerulesep}\n";
2613 } else if(!row_info[row].top_space.zero()) {
2615 os << "\\addlinespace["
2616 << from_ascii(row_info[row].top_space.asLatexString())
2619 os << "\\noalign{\\vskip"
2620 << from_ascii(row_info[row].top_space.asLatexString())
2624 bool ismulticol = false;
2625 bool ismultirow = false;
2627 // The bidi package (loaded by polyglossia) reverses RTL table columns
2628 bool const bidi_rtl =
2629 runparams.local_font->isRightToLeft()
2630 && runparams.use_polyglossia;
2632 bidi_rtl ? getFirstCellInRow(row) : getLastCellInRow(row);
2634 for (auto const & c : columns) {
2635 if (isPartOfMultiColumn(row, c))
2638 cell = cellIndex(row, c);
2640 if (isPartOfMultiRow(row, c)
2641 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2642 if (cell != lastcell)
2647 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
2648 InsetTableCell const * inset = cellInset(cell);
2650 Paragraph const & par = inset->paragraphs().front();
2652 os.texrow().forceStart(par.id(), 0);
2654 bool rtl = par.isRTL(buffer().params())
2656 && getPWidth(cell).zero()
2657 && !runparams.use_polyglossia;
2661 par.getParLanguage(buffer().params())->lang();
2662 if (lang == "farsi")
2664 else if (lang == "arabic_arabi")
2666 // currently, remaining RTL languages are
2667 // arabic_arabtex and hebrew
2671 // pass to the OutputParams that we are in a cell and
2672 // which alignment we have set.
2673 // InsetNewline needs this context information.
2674 OutputParams newrp(runparams);
2675 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2676 ? OutputParams::PLAIN
2677 : OutputParams::ALIGNED;
2679 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2680 // copy cell and split in 2
2681 InsetTableCell head = InsetTableCell(*cellInset(cell));
2682 head.setBuffer(const_cast<Buffer &>(buffer()));
2683 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2685 dit.push_back(CursorSlice(head));
2686 head.setMacrocontextPositionRecursive(dit);
2687 bool hassep = false;
2688 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2690 tail.setBuffer(head.buffer());
2692 dit.push_back(CursorSlice(tail));
2693 tail.setMacrocontextPositionRecursive(dit);
2697 tail.latex(os, newrp);
2700 head.latex(os, newrp);
2702 head.latex(os, newrp);
2705 tail.latex(os, newrp);
2708 } else if (ltCaption(row)) {
2709 // Inside longtable caption rows, we must only output the caption inset
2710 // with its content and omit anything outside of that (see #10791)
2711 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
2712 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
2713 for (; it != i_end; ++it) {
2714 if (it->lyxCode() != CAPTION_CODE)
2716 it->latex(os, runparams);
2719 } else if (!isPartOfMultiRow(row, c)) {
2720 if (!runparams.nice)
2721 os.texrow().start(par.id(), 0);
2722 inset->latex(os, newrp);
2725 runparams.encoding = newrp.encoding;
2729 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2730 if (cell != lastcell) { // not last cell in row
2737 os << "\\tabularnewline";
2738 if (row_info[row].bottom_space_default) {
2740 os << "\\addlinespace";
2742 os << "[\\doublerulesep]";
2743 } else if (!row_info[row].bottom_space.zero()) {
2745 os << "\\addlinespace";
2747 << from_ascii(row_info[row].bottom_space.asLatexString())
2752 //output the bottom line
2753 TeXBottomHLine(os, row, lang, columns);
2755 if (row_info[row].interline_space_default) {
2757 os << "\\addlinespace\n";
2759 os << "\\noalign{\\vskip\\doublerulesep}\n";
2760 } else if (!row_info[row].interline_space.zero()) {
2762 os << "\\addlinespace["
2763 << from_ascii(row_info[row].interline_space.asLatexString())
2766 os << "\\noalign{\\vskip"
2767 << from_ascii(row_info[row].interline_space.asLatexString())
2773 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2775 bool const is_tabular_star = !tabular_width.zero();
2776 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
2778 //+---------------------------------------------------------------------
2779 //+ first the opening preamble +
2780 //+---------------------------------------------------------------------
2783 if (!TexRow::isNone(pos))
2784 os.texrow().start(pos);
2787 if (is_long_tabular)
2788 os << "\\begin{landscape}\n";
2790 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2793 if (is_long_tabular) {
2794 os << "\\begin{longtable}";
2795 switch (longtabular_alignment) {
2796 case LYX_LONGTABULAR_ALIGN_LEFT:
2799 case LYX_LONGTABULAR_ALIGN_CENTER:
2802 case LYX_LONGTABULAR_ALIGN_RIGHT:
2807 if (is_tabular_star)
2808 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2810 os << "\\begin{tabular}";
2811 switch (tabular_valignment) {
2812 case LYX_VALIGN_TOP:
2815 case LYX_VALIGN_MIDDLE:
2817 case LYX_VALIGN_BOTTOM:
2825 if (is_tabular_star)
2826 os << "@{\\extracolsep{\\fill}}";
2828 // The bidi package (loaded by polyglossia) swaps the column
2829 // order for RTL (#9686). Thus we use this list.
2830 bool const bidi_rtl =
2831 runparams.local_font->isRightToLeft()
2832 && runparams.use_polyglossia;
2833 list<col_type> columns;
2834 for (col_type cl = 0; cl < ncols(); ++cl) {
2836 columns.push_front(cl);
2838 columns.push_back(cl);
2841 for (auto const & c : columns) {
2842 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
2844 if (!column_info[c].align_special.empty()) {
2845 os << column_info[c].align_special;
2847 if (!column_info[c].p_width.zero()) {
2848 bool decimal = false;
2849 switch (column_info[c].alignment) {
2850 case LYX_ALIGN_LEFT:
2851 os << ">{\\raggedright}";
2853 case LYX_ALIGN_RIGHT:
2854 os << ">{\\raggedleft}";
2856 case LYX_ALIGN_CENTER:
2857 os << ">{\\centering}";
2859 case LYX_ALIGN_NONE:
2860 case LYX_ALIGN_BLOCK:
2861 case LYX_ALIGN_LAYOUT:
2862 case LYX_ALIGN_SPECIAL:
2864 case LYX_ALIGN_DECIMAL: {
2866 os << ">{\\raggedright}";
2868 os << ">{\\raggedleft}";
2875 switch (column_info[c].valignment) {
2876 case LYX_VALIGN_TOP:
2877 // this is the default
2879 case LYX_VALIGN_MIDDLE:
2882 case LYX_VALIGN_BOTTOM:
2888 // Fixed-width cells with alignment at decimal separator
2889 // are output as two cells of half the width with the decimal
2890 // separator as column sep. This effectively puts the content
2891 // centered, which differs from the normal decimal sep alignment
2892 // and is not ideal, but we cannot do better ATM (see #9568).
2893 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
2895 docstring const halffixedwith =
2896 from_ascii(Length(column_info[c].p_width.value() / 2,
2897 column_info[c].p_width.unit()).asLatexString());
2901 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
2908 << from_ascii(column_info[c].p_width.asLatexString())
2911 switch (column_info[c].alignment) {
2912 case LYX_ALIGN_LEFT:
2915 case LYX_ALIGN_RIGHT:
2918 case LYX_ALIGN_DECIMAL:
2919 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2925 } // end if else !column_info[i].p_width
2926 } // end if else !column_info[i].align_special
2927 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
2932 TeXLongtableHeaderFooter(os, runparams, columns);
2934 //+---------------------------------------------------------------------
2935 //+ the single row and columns (cells) +
2936 //+---------------------------------------------------------------------
2938 for (row_type r = 0; r < nrows(); ++r) {
2939 if (isValidRow(r)) {
2940 TeXRow(os, r, runparams, columns);
2941 if (is_long_tabular && row_info[r].newpage)
2942 os << "\\newpage\n";
2946 //+---------------------------------------------------------------------
2947 //+ the closing of the tabular +
2948 //+---------------------------------------------------------------------
2950 if (is_long_tabular)
2951 os << "\\end{longtable}";
2953 if (is_tabular_star)
2954 os << "\\end{tabular*}";
2956 os << "\\end{tabular}";
2960 if (is_long_tabular)
2961 os << breakln << "\\end{landscape}";
2963 os << breakln << "\\end{turn}";
2966 if (!TexRow::isNone(pos))
2967 os.texrow().start(pos);
2971 int Tabular::docbookRow(odocstream & os, row_type row,
2972 OutputParams const & runparams) const
2975 idx_type cell = getFirstCellInRow(row);
2978 for (col_type c = 0; c < ncols(); ++c) {
2979 if (isPartOfMultiColumn(row, c))
2982 os << "<entry align=\"";
2983 switch (getAlignment(cell)) {
2984 case LYX_ALIGN_LEFT:
2987 case LYX_ALIGN_RIGHT:
2995 os << "\" valign=\"";
2996 switch (getVAlignment(cell)) {
2997 case LYX_VALIGN_TOP:
3000 case LYX_VALIGN_BOTTOM:
3003 case LYX_VALIGN_MIDDLE:
3008 if (isMultiColumn(cell)) {
3009 os << " namest=\"col" << c << "\" ";
3010 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3014 ret += cellInset(cell)->docbook(os, runparams);
3023 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3027 //+---------------------------------------------------------------------
3028 //+ first the opening preamble +
3029 //+---------------------------------------------------------------------
3031 os << "<tgroup cols=\"" << ncols()
3032 << "\" colsep=\"1\" rowsep=\"1\">\n";
3034 for (col_type c = 0; c < ncols(); ++c) {
3035 os << "<colspec colname=\"col" << c << "\" align=\"";
3036 switch (column_info[c].alignment) {
3037 case LYX_ALIGN_LEFT:
3040 case LYX_ALIGN_RIGHT:
3048 if (runparams.flavor == OutputParams::XML)
3054 //+---------------------------------------------------------------------
3055 //+ Long Tabular case +
3056 //+---------------------------------------------------------------------
3058 // output caption info
3059 // The caption flag wins over head/foot
3060 if (haveLTCaption()) {
3061 os << "<caption>\n";
3063 for (row_type r = 0; r < nrows(); ++r) {
3064 if (row_info[r].caption) {
3065 ret += docbookRow(os, r, runparams);
3068 os << "</caption>\n";
3071 // output header info
3072 if (haveLTHead(false) || haveLTFirstHead(false)) {
3075 for (row_type r = 0; r < nrows(); ++r) {
3076 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3077 !row_info[r].caption) {
3078 ret += docbookRow(os, r, runparams);
3084 // output footer info
3085 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3088 for (row_type r = 0; r < nrows(); ++r) {
3089 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3090 !row_info[r].caption) {
3091 ret += docbookRow(os, r, runparams);
3098 //+---------------------------------------------------------------------
3099 //+ the single row and columns (cells) +
3100 //+---------------------------------------------------------------------
3104 for (row_type r = 0; r < nrows(); ++r) {
3105 if (isValidRow(r)) {
3106 ret += docbookRow(os, r, runparams);
3111 //+---------------------------------------------------------------------
3112 //+ the closing of the tabular +
3113 //+---------------------------------------------------------------------
3122 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3123 OutputParams const & runparams, bool header) const
3126 string const celltag = header ? "th" : "td";
3127 idx_type cell = getFirstCellInRow(row);
3129 xs << html::StartTag("tr");
3130 for (col_type c = 0; c < ncols(); ++c) {
3131 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3136 Length const cwidth = column_info[c].p_width;
3137 if (!cwidth.zero()) {
3138 string const hwidth = cwidth.asHTMLString();
3139 attr << "style =\"width: " << hwidth << ";\" ";
3143 switch (getAlignment(cell)) {
3144 case LYX_ALIGN_LEFT:
3147 case LYX_ALIGN_RIGHT:
3155 attr << " valign='";
3156 switch (getVAlignment(cell)) {
3157 case LYX_VALIGN_TOP:
3160 case LYX_VALIGN_BOTTOM:
3163 case LYX_VALIGN_MIDDLE:
3168 if (isMultiColumn(cell))
3169 attr << " colspan='" << columnSpan(cell) << "'";
3170 else if (isMultiRow(cell))
3171 attr << " rowspan='" << rowSpan(cell) << "'";
3173 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3174 ret += cellInset(cell)->xhtml(xs, runparams);
3175 xs << html::EndTag(celltag) << html::CR();
3178 xs << html::EndTag("tr");
3183 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3187 if (is_long_tabular) {
3188 // we'll wrap it in a div, so as to deal with alignment
3190 switch (longtabular_alignment) {
3191 case LYX_LONGTABULAR_ALIGN_LEFT:
3194 case LYX_LONGTABULAR_ALIGN_CENTER:
3197 case LYX_LONGTABULAR_ALIGN_RIGHT:
3201 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3203 // The caption flag wins over head/foot
3204 if (haveLTCaption()) {
3205 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3207 for (row_type r = 0; r < nrows(); ++r)
3208 if (row_info[r].caption)
3209 ret += xhtmlRow(xs, r, runparams);
3210 xs << html::EndTag("div") << html::CR();
3214 xs << html::StartTag("table") << html::CR();
3216 // output header info
3217 bool const havefirsthead = haveLTFirstHead(false);
3218 // if we have a first head, then we are going to ignore the
3219 // headers for the additional pages, since there aren't any
3220 // in XHTML. this test accomplishes that.
3221 bool const havehead = !havefirsthead && haveLTHead(false);
3222 if (havehead || havefirsthead) {
3223 xs << html::StartTag("thead") << html::CR();
3224 for (row_type r = 0; r < nrows(); ++r) {
3225 if (((havefirsthead && row_info[r].endfirsthead) ||
3226 (havehead && row_info[r].endhead)) &&
3227 !row_info[r].caption) {
3228 ret += xhtmlRow(xs, r, runparams, true);
3231 xs << html::EndTag("thead") << html::CR();
3233 // output footer info
3234 bool const havelastfoot = haveLTLastFoot(false);
3236 bool const havefoot = !havelastfoot && haveLTFoot(false);
3237 if (havefoot || havelastfoot) {
3238 xs << html::StartTag("tfoot") << html::CR();
3239 for (row_type r = 0; r < nrows(); ++r) {
3240 if (((havelastfoot && row_info[r].endlastfoot) ||
3241 (havefoot && row_info[r].endfoot)) &&
3242 !row_info[r].caption) {
3243 ret += xhtmlRow(xs, r, runparams);
3246 xs << html::EndTag("tfoot") << html::CR();
3249 xs << html::StartTag("tbody") << html::CR();
3250 for (row_type r = 0; r < nrows(); ++r) {
3251 if (isValidRow(r)) {
3252 ret += xhtmlRow(xs, r, runparams);
3255 xs << html::EndTag("tbody")
3257 << html::EndTag("table")
3259 if (is_long_tabular)
3260 xs << html::EndTag("div") << html::CR();
3265 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3266 vector<unsigned int> const & clen) const
3268 idx_type const fcell = getFirstCellInRow(row);
3269 idx_type const n = numberOfCellsInRow(row) + fcell;
3272 for (idx_type i = fcell; i < n; ++i) {
3282 for (idx_type i = fcell; i < n; ++i) {
3293 col_type column = cellColumn(i);
3294 int len = clen[column];
3295 while (column < ncols() - 1
3296 && isPartOfMultiColumn(row, ++column))
3297 len += clen[column] + 4;
3298 os << docstring(len, ch);
3313 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3314 vector<unsigned int> const & clen) const
3316 idx_type const fcell = getFirstCellInRow(row);
3317 idx_type const n = numberOfCellsInRow(row) + fcell;
3320 for (idx_type i = fcell; i < n; ++i) {
3321 if (bottomLine(i)) {
3330 for (idx_type i = fcell; i < n; ++i) {
3331 if (bottomLine(i)) {
3341 col_type column = cellColumn(i);
3342 int len = clen[column];
3343 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3344 len += clen[column] + 4;
3345 os << docstring(len, ch);
3346 if (bottomLine(i)) {
3360 void Tabular::plaintextPrintCell(odocstringstream & os,
3361 OutputParams const & runparams,
3362 idx_type cell, row_type row, col_type column,
3363 vector<unsigned int> const & clen,
3364 bool onlydata, size_t max_length) const
3366 odocstringstream sstr;
3367 cellInset(cell)->plaintext(sstr, runparams, max_length);
3379 unsigned int len1 = sstr.str().length();
3380 unsigned int len2 = clen[column];
3381 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3382 len2 += clen[column] + 4;
3385 switch (getAlignment(cell)) {
3387 case LYX_ALIGN_LEFT:
3390 case LYX_ALIGN_RIGHT:
3394 case LYX_ALIGN_CENTER:
3400 os << docstring(len1, ' ') << sstr.str()
3401 << docstring(len2, ' ');
3403 if (rightLine(cell))
3410 void Tabular::plaintext(odocstringstream & os,
3411 OutputParams const & runparams, int const depth,
3412 bool onlydata, char_type delim, size_t max_length) const
3414 // first calculate the width of the single columns
3415 vector<unsigned int> clen(ncols());
3418 // first all non multicolumn cells!
3419 for (col_type c = 0; c < ncols(); ++c) {
3421 for (row_type r = 0; r < nrows(); ++r) {
3422 idx_type cell = cellIndex(r, c);
3423 if (isMultiColumn(cell))
3425 odocstringstream sstr;
3426 cellInset(cell)->plaintext(sstr, runparams, max_length);
3427 if (clen[c] < sstr.str().length())
3428 clen[c] = sstr.str().length();
3431 // then all multicolumn cells!
3432 for (col_type c = 0; c < ncols(); ++c) {
3433 for (row_type r = 0; r < nrows(); ++r) {
3434 idx_type cell = cellIndex(r, c);
3435 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3437 odocstringstream sstr;
3438 cellInset(cell)->plaintext(sstr, runparams, max_length);
3439 int len = int(sstr.str().length());
3440 idx_type const n = columnSpan(cell);
3441 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3443 if (len > int(clen[c + n - 1]))
3444 clen[c + n - 1] = len;
3449 for (row_type r = 0; r < nrows(); ++r) {
3450 if (!onlydata && plaintextTopHLine(os, r, clen))
3451 os << docstring(depth * 2, ' ');
3452 for (col_type c = 0; c < ncols(); ++c) {
3453 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3455 if (onlydata && c > 0)
3456 // we don't use operator<< for single UCS4 character.
3457 // see explanation in docstream.h
3459 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3461 if (os.str().size() > max_length)
3466 os << docstring(depth * 2, ' ');
3467 if (plaintextBottomHLine(os, r, clen))
3468 os << docstring(depth * 2, ' ');
3470 if (os.str().size() > max_length)
3476 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3478 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3482 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3484 return cell_info[row][column].inset;
3488 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3490 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3494 void Tabular::setCellInset(row_type row, col_type column,
3495 shared_ptr<InsetTableCell> ins)
3497 CellData & cd = cell_info[row][column];
3502 void Tabular::validate(LaTeXFeatures & features) const
3504 features.require("NeedTabularnewline");
3506 features.require("booktabs");
3507 if (is_long_tabular)
3508 features.require("longtable");
3509 if (rotate && is_long_tabular)
3510 features.require("lscape");
3512 features.require("rotating");
3513 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3514 if (isMultiRow(cell))
3515 features.require("multirow");
3516 if (getUsebox(cell) == BOX_VARWIDTH)
3517 features.require("varwidth");
3518 if (getVAlignment(cell) != LYX_VALIGN_TOP
3519 || !getPWidth(cell).zero())
3520 features.require("array");
3521 // Tell footnote that we need a savenote
3522 // environment in non-long tables or
3523 // longtable headers/footers
3524 else if (!is_long_tabular && !features.inFloat())
3525 features.saveNoteEnv("tabular");
3526 else if (!isValidRow(cellRow(cell)))
3527 features.saveNoteEnv("longtable");
3529 cellInset(cell)->validate(features);
3530 features.saveNoteEnv(string());
3535 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3537 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3538 ParagraphList::const_iterator cit = parlist.begin();
3539 ParagraphList::const_iterator end = parlist.end();
3540 bool const turned = getRotateCell(cell) != 0;
3542 for (; cit != end; ++cit)
3543 for (int i = 0; i < cit->size(); ++i)
3544 if (cit->isNewline(i))
3545 return turned ? BOX_VARWIDTH : BOX_PARBOX;
3551 /////////////////////////////////////////////////////////////////////
3555 /////////////////////////////////////////////////////////////////////
3557 InsetTableCell::InsetTableCell(Buffer * buf)
3558 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3559 contentAlign(LYX_ALIGN_CENTER)
3563 bool InsetTableCell::forcePlainLayout(idx_type) const
3565 return !isFixedWidth;
3569 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3571 return isFixedWidth;
3575 bool InsetTableCell::forceLocalFontSwitch() const
3577 return isFixedWidth;
3581 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3582 FuncStatus & status) const
3584 bool enabled = true;
3585 switch (cmd.action()) {
3586 case LFUN_MATH_DISPLAY:
3587 if (!hasFixedWidth()) {
3592 return InsetText::getStatus(cur, cmd, status);
3594 status.setEnabled(enabled);
3598 docstring InsetTableCell::asString(bool intoInsets)
3601 if (paragraphs().empty())
3603 ParagraphList::const_iterator it = paragraphs().begin();
3604 ParagraphList::const_iterator en = paragraphs().end();
3606 for (; it != en; ++it) {
3611 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3617 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3618 UpdateType utype, TocBackend & backend) const
3620 InsetText::iterateForToc(di, output_active, utype, backend);
3624 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3627 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3628 return InsetText::xhtml(xs, rp);
3633 /////////////////////////////////////////////////////////////////////
3637 /////////////////////////////////////////////////////////////////////
3639 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3641 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3642 rowselect_(false), colselect_(false)
3647 InsetTabular::InsetTabular(InsetTabular const & tab)
3648 : Inset(tab), tabular(tab.tabular),
3649 rowselect_(false), colselect_(false)
3654 InsetTabular::~InsetTabular()
3656 hideDialogs("tabular", this);
3660 void InsetTabular::setBuffer(Buffer & buf)
3662 tabular.setBuffer(buf);
3663 Inset::setBuffer(buf);
3667 bool InsetTabular::insetAllowed(InsetCode code) const
3672 case MATHMACRO_CODE:
3677 return tabular.is_long_tabular;
3685 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3687 return tabular.is_long_tabular &&
3688 (newtype == "Standard" || newtype == "Unnumbered");
3692 void InsetTabular::write(ostream & os) const
3694 os << "Tabular" << endl;
3699 string InsetTabular::contextMenu(BufferView const &, int, int) const
3701 // FIXME: depending on the selection state,
3702 // we could offer a different menu.
3703 return cell(0)->contextMenuName() + ";" + contextMenuName();
3707 string InsetTabular::contextMenuName() const
3709 return "context-tabular";
3713 void InsetTabular::read(Lexer & lex)
3715 //bool const old_format = (lex.getString() == "\\LyXTable");
3723 string token = lex.getString();
3724 while (lex && token != "\\end_inset") {
3726 token = lex.getString();
3729 lex.printError("Missing \\end_inset at this point. ");
3733 int InsetTabular::rowFromY(Cursor & cur, int y) const
3735 // top y coordinate of tabular
3736 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3738 for (; r < tabular.nrows() && y > h; ++r)
3739 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3740 + tabular.interRowSpace(r);
3746 int InsetTabular::columnFromX(Cursor & cur, int x) const
3748 // left x coordinate of tabular
3749 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3751 for (; c < tabular.ncols() && x > w; ++c)
3752 w += tabular.cellWidth(c);
3757 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3759 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3760 // mi.base.textwidth << "\n";
3761 LBUFERR(mi.base.bv);
3763 for (row_type r = 0; r < tabular.nrows(); ++r) {
3766 for (col_type c = 0; c < tabular.ncols(); ++c) {
3767 if (tabular.isPartOfMultiColumn(r, c)
3768 || tabular.isPartOfMultiRow(r, c))
3769 // multicolumn or multirow cell, but not first one
3771 idx_type const cell = tabular.cellIndex(r, c);
3774 Length const p_width = tabular.getPWidth(cell);
3775 if (!p_width.zero())
3776 m.base.textwidth = p_width.inPixels(mi.base);
3777 tabular.cellInset(cell)->metrics(m, dim);
3778 if (!p_width.zero())
3779 dim.wid = m.base.textwidth;
3780 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3781 + tabular.interColumnSpace(cell);
3783 // FIXME(?): do we need a second metrics call?
3784 TextMetrics const & tm =
3785 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3787 // determine horizontal offset because of decimal align (if necessary)
3788 int decimal_width = 0;
3789 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3790 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
3791 tail.setBuffer(tabular.buffer());
3792 // we need to set macrocontext position everywhere
3793 // otherwise we crash with nested insets (e.g. footnotes)
3794 // after decimal point
3795 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3797 dit.push_back(CursorSlice(tail));
3798 tail.setMacrocontextPositionRecursive(dit);
3800 // remove text leading decimal point
3801 docstring const align_d = tabular.column_info[c].decimal_point;
3802 dit = separatorPos(&tail, align_d);
3804 pit_type const psize = tail.paragraphs().front().size();
3806 tail.paragraphs().front().eraseChars(0,
3807 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3809 tail.metrics(m, dim1);
3810 decimal_width = dim1.width();
3813 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3814 tabular.cell_info[r][c].decimal_width = decimal_width;
3816 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3817 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3818 int const lastpardes = tm.last().second->descent()
3819 + TEXT_TO_INSET_OFFSET;
3821 switch (tabular.getVAlignment(cell)) {
3822 case Tabular::LYX_VALIGN_TOP:
3824 case Tabular::LYX_VALIGN_MIDDLE:
3825 offset = -(dim.des - lastpardes)/2;
3827 case Tabular::LYX_VALIGN_BOTTOM:
3828 offset = -(dim.des - lastpardes);
3831 tabular.cell_info[r][c].voffset = offset;
3832 maxasc = max(maxasc, dim.asc - offset);
3833 maxdes = max(maxdes, dim.des + offset);
3835 int const top_space = tabular.row_info[r].top_space_default ?
3836 default_line_space :
3837 tabular.row_info[r].top_space.inPixels(mi.base);
3838 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3839 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3840 default_line_space :
3841 tabular.row_info[r].bottom_space.inPixels(mi.base);
3842 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3845 tabular.updateColumnWidths();
3846 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
3847 dim.des = tabular.height() - dim.asc;
3848 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3852 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3855 if (&cur.inset() == this && cur.selection()) {
3856 if (cur.selIsMultiCell()) {
3859 getSelection(cur, rs, re, cs, ce);
3861 idx_type const cell = tabular.cellIndex(row, col);
3862 col_type const cspan = tabular.columnSpan(cell);
3863 row_type const rspan = tabular.rowSpan(cell);
3864 if (col + cspan - 1 >= cs && col <= ce
3865 && row + rspan - 1 >= rs && row <= re)
3868 if (col == tabular.cellColumn(cur.idx())
3869 && row == tabular.cellRow(cur.idx())) {
3870 CursorSlice const & beg = cur.selBegin();
3871 CursorSlice const & end = cur.selEnd();
3873 if ((end.lastpos() > 0 || end.lastpit() > 0)
3874 && end.pos() == end.lastpos() && beg.pos() == 0
3875 && end.pit() == end.lastpit() && beg.pit() == 0)
3883 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3885 x += ADD_TO_TABULAR_WIDTH;
3887 BufferView * bv = pi.base.bv;
3888 Cursor & cur = pi.base.bv->cursor();
3890 // FIXME: As the full background is painted in drawBackground(),
3891 // we have no choice but to do a full repaint for the Text cells.
3892 pi.full_repaint = true;
3894 bool const original_selection_state = pi.selected;
3898 int yy = y + tabular.offsetVAlignment();
3899 for (row_type r = 0; r < tabular.nrows(); ++r) {
3901 for (col_type c = 0; c < tabular.ncols(); ++c) {
3902 if (tabular.isPartOfMultiColumn(r, c))
3905 idx = tabular.cellIndex(r, c);
3907 if (tabular.isPartOfMultiRow(r, c)) {
3908 nx += tabular.cellWidth(idx);
3912 pi.selected |= isCellSelected(cur, r, c);
3913 int const cx = nx + tabular.textHOffset(idx);
3914 int const cy = yy + tabular.textVOffset(idx);
3915 // Cache the Inset position.
3916 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3917 cell(idx)->draw(pi, cx, cy);
3918 drawCellLines(pi, nx, yy, r, idx);
3919 nx += tabular.cellWidth(idx);
3920 pi.selected = original_selection_state;
3923 if (r + 1 < tabular.nrows())
3924 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3925 + tabular.interRowSpace(r + 1);
3930 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3932 x += ADD_TO_TABULAR_WIDTH;
3933 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
3934 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3935 pi.backgroundColor(this));
3939 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3941 Cursor & cur = pi.base.bv->cursor();
3943 x += ADD_TO_TABULAR_WIDTH;
3945 if (!cur.selection())
3947 if (&cur.inset() != this)
3952 bool const full_cell_selected = isCellSelected(cur,
3953 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3955 if (cur.selIsMultiCell() || full_cell_selected) {
3956 for (row_type r = 0; r < tabular.nrows(); ++r) {
3958 for (col_type c = 0; c < tabular.ncols(); ++c) {
3959 if (tabular.isPartOfMultiColumn(r, c))
3962 idx_type const cell = tabular.cellIndex(r, c);
3964 if (tabular.isPartOfMultiRow(r, c)) {
3965 xx += tabular.cellWidth(cell);
3968 int const w = tabular.cellWidth(cell);
3969 int const h = tabular.cellHeight(cell);
3970 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
3971 if (isCellSelected(cur, r, c))
3972 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3975 if (r + 1 < tabular.nrows())
3976 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3977 + tabular.interRowSpace(r + 1);
3981 // FIXME: This code has no effect because InsetTableCell does not handle
3982 // drawSelection other than the trivial implementation in Inset.
3984 // x += cellXPos(cur.idx());
3985 // x += tabular.textHOffset(cur.idx());
3986 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3993 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2,
3994 bool drawline, bool heavy = false)
3996 ColorCode const col = drawline ? Color_tabularline : Color_tabularonoffline;
3997 pi.pain.line(x1, y1, x2, y2, pi.textColor(col),
3998 drawline ? Painter::line_solid : Painter::line_onoffdash,
3999 (heavy ? 2 : 1) * Painter::thin_line);
4005 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4006 row_type row, idx_type cell) const
4008 y -= tabular.rowAscent(row);
4009 int const w = tabular.cellWidth(cell);
4010 int const h = tabular.cellHeight(cell);
4012 col_type const col = tabular.cellColumn(cell);
4015 bool drawline = tabular.topLine(cell)
4016 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4017 bool heavy = tabular.use_booktabs && row == 0 && tabular.rowTopLine(row);
4018 tabline(pi, x, y, x + w, y, drawline, heavy);
4021 drawline = tabular.bottomLine(cell);
4022 row_type const lastrow = tabular.nrows() - 1;
4023 // Consider multi-rows
4025 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4026 && tabular.isPartOfMultiRow(r + 1, col))
4028 heavy = tabular.use_booktabs
4029 && ((row == lastrow && tabular.rowBottomLine(row))
4030 || (r == lastrow && tabular.rowBottomLine(r)));
4031 tabline(pi, x, y + h, x + w, y + h, drawline, heavy);
4034 drawline = tabular.leftLine(cell)
4035 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4036 tabline(pi, x, y, x, y + h, drawline);
4039 x -= tabular.interColumnSpace(cell);
4040 col_type next_cell_col = col + 1;
4041 while (next_cell_col < tabular.ncols()
4042 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4044 drawline = tabular.rightLine(cell)
4045 || (next_cell_col < tabular.ncols()
4046 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4047 tabline(pi, x + w, y, x + w, y + h, drawline);
4051 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4053 //lyxerr << "InsetTabular::edit: " << this << endl;
4057 if (isRightToLeft(cur))
4058 cur.idx() = tabular.getLastCellInRow(0);
4064 if (isRightToLeft(cur))
4065 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4067 cur.idx() = tabular.numberofcells - 1;
4069 cur.pos() = cur.lastpos(); // FIXME crude guess
4071 cur.setCurrentFont();
4072 // FIXME: this accesses the position cache before it is initialized
4073 //cur.bv().fitCursor();
4077 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
4079 // In a longtable, tell captions what the current float is
4080 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4081 string const saveflt = cnts.current_float();
4082 if (tabular.is_long_tabular) {
4083 cnts.current_float("table");
4084 // in longtables, we only step the counter once
4085 cnts.step(from_ascii("table"), utype);
4086 cnts.isLongtable(true);
4089 ParIterator it2 = it;
4091 size_t const end = it2.nargs();
4092 for ( ; it2.idx() < end; it2.top().forwardIdx())
4093 buffer().updateBuffer(it2, utype);
4096 if (tabular.is_long_tabular) {
4097 cnts.current_float(saveflt);
4098 cnts.isLongtable(false);
4103 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4104 UpdateType utype, TocBackend & backend) const
4106 DocIterator dit = cpit;
4108 size_t const end = dit.nargs();
4109 for ( ; dit.idx() < end; dit.top().forwardIdx())
4110 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4114 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4116 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4117 return x < x0 || x > x0 + tabular.width();
4121 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4123 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4124 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4125 // there is no margin added vertically to tabular insets.
4126 // However, it works for now.
4127 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4131 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4133 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4137 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4139 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4140 << "\n cur:" << cur);
4141 CursorSlice sl = cur.top();
4142 Cursor & bvcur = cur.bv().cursor();
4144 FuncCode const act = cmd.action();
4148 case LFUN_MOUSE_PRESS: {
4149 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4151 if (hitSelectRow(cur.bv(), cmd.x())) {
4152 row_type r = rowFromY(cur, cmd.y());
4153 cur.idx() = tabular.getFirstCellInRow(r);
4157 cur.idx() = tabular.getLastCellInRow(r);
4158 cur.pit() = cur.lastpit();
4159 cur.pos() = cur.lastpos();
4160 cur.selection(true);
4166 if (hitSelectColumn(cur.bv(), cmd.y())) {
4167 col_type c = columnFromX(cur, cmd.x());
4168 cur.idx() = tabular.cellIndex(0, c);
4172 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4173 cur.pit() = cur.lastpit();
4174 cur.pos() = cur.lastpos();
4175 cur.selection(true);
4180 // do not reset cursor/selection if we have selected
4181 // some cells (bug 2715).
4182 if (cmd.button() == mouse_button::button3
4183 && &bvcur.selBegin().inset() == this
4184 && bvcur.selIsMultiCell())
4187 // Let InsetTableCell do it
4188 cell(cur.idx())->dispatch(cur, cmd);
4191 case LFUN_MOUSE_MOTION:
4192 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4193 if (cmd.button() == mouse_button::button1) {
4194 // only accept motions to places not deeper nested than the real anchor
4195 if (!bvcur.realAnchor().hasPart(cur)) {
4199 // select (additional) row
4201 row_type r = rowFromY(cur, cmd.y());
4202 cur.idx() = tabular.getLastCellInRow(r);
4203 // we need to reset the cursor's pit and pos now, as the old ones
4204 // may no longer be valid.
4207 bvcur.setCursor(cur);
4208 bvcur.selection(true);
4211 // select (additional) column
4213 col_type c = columnFromX(cur, cmd.x());
4214 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4215 // we need to reset the cursor's pit and pos now, as the old ones
4216 // may no longer be valid.
4219 bvcur.setCursor(cur);
4220 bvcur.selection(true);
4223 // only update if selection changes
4224 if (bvcur.idx() == cur.idx() &&
4225 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4226 cur.noScreenUpdate();
4227 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4228 bvcur.setCursor(cur);
4229 bvcur.selection(true);
4230 // if this is a multicell selection, we just set the cursor to
4231 // the beginning of the cell's text.
4232 if (bvcur.selIsMultiCell()) {
4233 bvcur.pit() = bvcur.lastpit();
4234 bvcur.pos() = bvcur.lastpos();
4239 case LFUN_MOUSE_RELEASE:
4244 case LFUN_CELL_BACKWARD:
4246 cur.selection(false);
4249 case LFUN_CELL_FORWARD:
4251 cur.selection(false);
4254 case LFUN_CHAR_FORWARD_SELECT:
4255 case LFUN_CHAR_FORWARD:
4256 case LFUN_CHAR_BACKWARD_SELECT:
4257 case LFUN_CHAR_BACKWARD:
4258 case LFUN_CHAR_RIGHT_SELECT:
4259 case LFUN_CHAR_RIGHT:
4260 case LFUN_CHAR_LEFT_SELECT:
4261 case LFUN_CHAR_LEFT:
4262 case LFUN_WORD_FORWARD:
4263 case LFUN_WORD_FORWARD_SELECT:
4264 case LFUN_WORD_BACKWARD:
4265 case LFUN_WORD_BACKWARD_SELECT:
4266 case LFUN_WORD_RIGHT:
4267 case LFUN_WORD_RIGHT_SELECT:
4268 case LFUN_WORD_LEFT:
4269 case LFUN_WORD_LEFT_SELECT: {
4270 // determine whether we move to next or previous cell, where to enter
4271 // the new cell from, and which command to "finish" (i.e., exit the
4274 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4275 FuncCode finish_lfun;
4277 if (act == LFUN_CHAR_FORWARD
4278 || act == LFUN_CHAR_FORWARD_SELECT
4279 || act == LFUN_WORD_FORWARD
4280 || act == LFUN_WORD_FORWARD_SELECT) {
4282 finish_lfun = LFUN_FINISHED_FORWARD;
4284 else if (act == LFUN_CHAR_BACKWARD
4285 || act == LFUN_CHAR_BACKWARD_SELECT
4286 || act == LFUN_WORD_BACKWARD
4287 || act == LFUN_WORD_BACKWARD_SELECT) {
4289 finish_lfun = LFUN_FINISHED_BACKWARD;
4291 // LEFT or RIGHT commands --- the interpretation will depend on the
4292 // table's direction.
4294 bool const right = act == LFUN_CHAR_RIGHT
4295 || act == LFUN_CHAR_RIGHT_SELECT
4296 || act == LFUN_WORD_RIGHT
4297 || act == LFUN_WORD_RIGHT_SELECT;
4298 next_cell = isRightToLeft(cur) != right;
4300 if (lyxrc.visual_cursor)
4301 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4303 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4306 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4307 || act == LFUN_CHAR_BACKWARD_SELECT
4308 || act == LFUN_CHAR_RIGHT_SELECT
4309 || act == LFUN_CHAR_LEFT_SELECT
4310 || act == LFUN_WORD_FORWARD_SELECT
4311 || act == LFUN_WORD_RIGHT_SELECT
4312 || act == LFUN_WORD_BACKWARD_SELECT
4313 || act == LFUN_WORD_LEFT_SELECT;
4315 // If we have a multicell selection or we're
4316 // not doing some LFUN_*_SELECT thing anyway...
4317 if (!cur.selIsMultiCell() || !select) {
4318 col_type const c = tabular.cellColumn(cur.idx());
4319 row_type const r = tabular.cellRow(cur.idx());
4320 // Are we trying to select the whole cell and is the whole cell
4321 // not yet selected?
4322 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4323 ((next_cell && cur.pit() == cur.lastpit()
4324 && cur.pos() == cur.lastpos())
4325 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4327 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4329 // ...try to dispatch to the cell's inset.
4330 cell(cur.idx())->dispatch(cur, cmd);
4332 // When we already have a selection we want to select the whole cell
4333 // before going to the next cell.
4334 if (select_whole && !empty_cell){
4335 getText(cur.idx())->selectAll(cur);
4337 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4341 // FIXME: When we support the selection of an empty cell, remove
4342 // the !empty_cell from this condition. For now we jump to the next
4343 // cell if the current cell is empty.
4344 if (cur.result().dispatched() && !empty_cell)
4348 // move to next/prev cell, as appropriate
4349 // note that we will always do this if we're selecting and we have
4350 // a multicell selection
4351 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4352 << " cell from: " << int(entry_from));
4354 moveNextCell(cur, entry_from);
4356 movePrevCell(cur, entry_from);
4357 // if we're exiting the table, call the appropriate FINISHED lfun
4358 if (sl == cur.top()) {
4359 cmd = FuncRequest(finish_lfun);
4364 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4369 case LFUN_DOWN_SELECT:
4371 if (!(cur.selection() && cur.selIsMultiCell()))
4372 cell(cur.idx())->dispatch(cur, cmd);
4374 cur.dispatched(); // override the cell's decision
4375 if (sl == cur.top()) {
4376 // if our Text didn't do anything to the cursor
4377 // then we try to put the cursor into the cell below
4378 // setting also the right targetX.
4379 cur.selHandle(act == LFUN_DOWN_SELECT);
4380 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4381 int const xtarget = cur.targetX();
4382 // WARNING: Once cur.idx() has been reset, the cursor is in
4383 // an inconsistent state until pos() has been set. Be careful
4384 // what you do with it!
4385 cur.idx() = tabular.cellBelow(cur.idx());
4387 TextMetrics const & tm =
4388 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4389 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4390 cur.setCurrentFont();
4393 if (sl == cur.top()) {
4394 // we trick it to go to forward after leaving the
4396 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4399 if (cur.selIsMultiCell()) {
4400 cur.pit() = cur.lastpit();
4401 cur.pos() = cur.lastpos();
4402 cur.setCurrentFont();
4403 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4406 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4409 case LFUN_UP_SELECT:
4411 if (!(cur.selection() && cur.selIsMultiCell()))
4412 cell(cur.idx())->dispatch(cur, cmd);
4413 cur.dispatched(); // override the cell's decision
4414 if (sl == cur.top()) {
4415 // if our Text didn't do anything to the cursor
4416 // then we try to put the cursor into the cell above
4417 // setting also the right targetX.
4418 cur.selHandle(act == LFUN_UP_SELECT);
4419 if (tabular.cellRow(cur.idx()) != 0) {
4420 int const xtarget = cur.targetX();
4421 // WARNING: Once cur.idx() has been reset, the cursor is in
4422 // an inconsistent state until pos() has been set. Be careful
4423 // what you do with it!
4424 cur.idx() = tabular.cellAbove(cur.idx());
4425 cur.pit() = cur.lastpit();
4426 Text const * text = cell(cur.idx())->getText(0);
4427 TextMetrics const & tm = cur.bv().textMetrics(text);
4428 ParagraphMetrics const & pm =
4429 tm.parMetrics(cur.lastpit());
4430 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4431 cur.setCurrentFont();
4434 if (sl == cur.top()) {
4435 cmd = FuncRequest(LFUN_UP);
4438 if (cur.selIsMultiCell()) {
4440 cur.pos() = cur.lastpos();
4441 cur.setCurrentFont();
4442 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4445 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4448 case LFUN_LAYOUT_TABULAR:
4449 cur.bv().showDialog("tabular");
4452 case LFUN_INSET_MODIFY:
4453 // we come from the dialog
4454 if (cmd.getArg(0) == "tabular")
4455 tabularFeatures(cur, cmd.getLongArg(1));
4460 case LFUN_TABULAR_FEATURE:
4461 tabularFeatures(cur, to_utf8(cmd.argument()));
4464 // insert file functions
4465 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4466 case LFUN_FILE_INSERT_PLAINTEXT:
4468 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4469 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4470 FileName(to_utf8(cmd.argument())));
4473 cur.recordUndoInset();
4474 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4475 // content has been replaced,
4476 // so cursor might be invalid
4477 cur.pos() = cur.lastpos();
4478 cur.pit() = cur.lastpit();
4479 bvcur.setCursor(cur);
4486 if (cur.selIsMultiCell()) {
4487 if (copySelection(cur)) {
4488 cur.recordUndoInset();
4492 cell(cur.idx())->dispatch(cur, cmd);
4495 case LFUN_SELF_INSERT:
4496 if (cur.selIsMultiCell()) {
4497 cur.recordUndoInset();
4499 BufferView * bv = &cur.bv();
4500 docstring::const_iterator cit = cmd.argument().begin();
4501 docstring::const_iterator const end = cmd.argument().end();
4502 for (; cit != end; ++cit)
4503 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4506 bv->bookmarkEditPosition();
4508 cell(cur.idx())->dispatch(cur, cmd);
4511 case LFUN_CHAR_DELETE_BACKWARD:
4512 case LFUN_CHAR_DELETE_FORWARD:
4513 if (cur.selIsMultiCell()) {
4514 cur.recordUndoInset();
4517 cell(cur.idx())->dispatch(cur, cmd);
4521 if (!cur.selection())
4523 if (cur.selIsMultiCell()) {
4527 cell(cur.idx())->dispatch(cur, cmd);
4530 case LFUN_CLIPBOARD_PASTE:
4531 case LFUN_PRIMARY_SELECTION_PASTE: {
4532 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4533 theClipboard().getAsText(Clipboard::PlainTextType) :
4534 theSelection().get();
4537 // pass to InsertPlaintextString, but
4538 // only if we have multi-cell content
4539 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4540 cur.recordUndoInset();
4541 if (insertPlaintextString(cur.bv(), clip, false)) {
4542 // content has been replaced,
4543 // so cursor might be invalid
4544 cur.pos() = cur.lastpos();
4545 cur.pit() = cur.lastpit();
4546 bvcur.setCursor(cur);
4550 // Let the cell handle normal text
4551 cell(cur.idx())->dispatch(cur, cmd);
4556 if (!tabularStackDirty()) {
4557 if (!cur.selIsMultiCell())
4558 cell(cur.idx())->dispatch(cur, cmd);
4561 if (theClipboard().isInternal()) {
4562 cur.recordUndoInset();
4563 pasteClipboard(cur);
4567 case LFUN_FONT_EMPH:
4568 case LFUN_FONT_BOLD:
4569 case LFUN_FONT_BOLDSYMBOL:
4570 case LFUN_FONT_ROMAN:
4571 case LFUN_FONT_NOUN:
4572 case LFUN_FONT_ITAL:
4573 case LFUN_FONT_FRAK:
4574 case LFUN_FONT_TYPEWRITER:
4575 case LFUN_FONT_SANS:
4576 case LFUN_TEXTSTYLE_APPLY:
4577 case LFUN_TEXTSTYLE_UPDATE:
4578 case LFUN_FONT_SIZE:
4579 case LFUN_FONT_UNDERLINE:
4580 case LFUN_FONT_STRIKEOUT:
4581 case LFUN_FONT_CROSSOUT:
4582 case LFUN_FONT_UNDERUNDERLINE:
4583 case LFUN_FONT_UNDERWAVE:
4585 case LFUN_PARAGRAPH_PARAMS_APPLY:
4586 case LFUN_PARAGRAPH_PARAMS:
4587 case LFUN_WORD_CAPITALIZE:
4588 case LFUN_WORD_UPCASE:
4589 case LFUN_WORD_LOWCASE:
4590 case LFUN_CHARS_TRANSPOSE:
4591 if (cur.selIsMultiCell()) {
4594 getSelection(cur, rs, re, cs, ce);
4595 Cursor tmpcur = cur;
4596 for (row_type r = rs; r <= re; ++r) {
4597 for (col_type c = cs; c <= ce; ++c) {
4598 // cursor follows cell:
4599 tmpcur.idx() = tabular.cellIndex(r, c);
4600 // select this cell only:
4603 tmpcur.resetAnchor();
4604 tmpcur.pit() = tmpcur.lastpit();
4605 tmpcur.pos() = tmpcur.top().lastpos();
4606 tmpcur.setCursor(tmpcur);
4607 tmpcur.setSelection();
4608 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4613 cell(cur.idx())->dispatch(cur, cmd);
4617 case LFUN_INSET_SETTINGS:
4618 // relay this lfun to Inset, not to the cell.
4619 Inset::doDispatch(cur, cmd);
4623 // we try to handle this event in the insets dispatch function.
4624 cell(cur.idx())->dispatch(cur, cmd);
4630 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4631 string const & argument, FuncStatus & status) const
4634 int action = Tabular::LAST_ACTION;
4636 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4637 if (tabularFeature[i].feature == s) {
4638 action = tabularFeature[i].action;
4642 if (action == Tabular::LAST_ACTION) {
4644 status.setUnknown(true);
4648 row_type sel_row_start = 0;
4649 row_type sel_row_end = 0;
4650 col_type sel_col_start = 0;
4651 col_type sel_col_end = 0;
4652 Tabular::ltType dummyltt;
4655 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4658 case Tabular::SET_PWIDTH:
4659 case Tabular::SET_MPWIDTH:
4660 case Tabular::SET_SPECIAL_COLUMN:
4661 case Tabular::SET_SPECIAL_MULTICOLUMN:
4662 case Tabular::APPEND_ROW:
4663 case Tabular::APPEND_COLUMN:
4664 case Tabular::DELETE_ROW:
4665 case Tabular::DELETE_COLUMN:
4666 case Tabular::COPY_ROW:
4667 case Tabular::COPY_COLUMN:
4668 case Tabular::SET_TOP_SPACE:
4669 case Tabular::SET_BOTTOM_SPACE:
4670 case Tabular::SET_INTERLINE_SPACE:
4674 case Tabular::SET_TABULAR_WIDTH:
4675 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4676 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4679 case Tabular::MOVE_COLUMN_RIGHT:
4680 case Tabular::MOVE_COLUMN_LEFT:
4681 case Tabular::MOVE_ROW_DOWN:
4682 case Tabular::MOVE_ROW_UP: {
4683 if (cur.selection()) {
4684 status.message(_("Selections not supported."));
4685 status.setEnabled(false);
4689 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4690 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4691 (action == Tabular::MOVE_COLUMN_LEFT &&
4692 tabular.cellColumn(cur.idx()) == 0) ||
4693 (action == Tabular::MOVE_ROW_DOWN &&
4694 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4695 (action == Tabular::MOVE_ROW_UP &&
4696 tabular.cellRow(cur.idx()) == 0)) {
4697 status.setEnabled(false);
4701 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4702 action == Tabular::MOVE_COLUMN_LEFT) {
4703 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4704 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4705 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4706 status.message(_("Multi-column in current or"
4707 " destination column."));
4708 status.setEnabled(false);
4713 if (action == Tabular::MOVE_ROW_DOWN ||
4714 action == Tabular::MOVE_ROW_UP) {
4715 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4716 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4717 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4718 status.message(_("Multi-row in current or"
4719 " destination row."));
4720 status.setEnabled(false);
4725 status.setEnabled(true);
4729 case Tabular::SET_DECIMAL_POINT:
4731 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4734 case Tabular::SET_MULTICOLUMN:
4735 case Tabular::UNSET_MULTICOLUMN:
4736 case Tabular::MULTICOLUMN:
4737 // If a row is set as longtable caption, it must not be allowed
4738 // to unset that this row is a multicolumn.
4739 status.setEnabled(sel_row_start == sel_row_end
4740 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4741 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4744 case Tabular::SET_MULTIROW:
4745 case Tabular::UNSET_MULTIROW:
4746 case Tabular::MULTIROW:
4747 // If a row is set as longtable caption, it must not be allowed
4748 // to unset that this row is a multirow.
4749 status.setEnabled(sel_col_start == sel_col_end
4750 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4751 status.setOnOff(tabular.isMultiRow(cur.idx()));
4754 case Tabular::SET_ALL_LINES:
4755 case Tabular::UNSET_ALL_LINES:
4756 case Tabular::SET_INNER_LINES:
4757 case Tabular::SET_BORDER_LINES:
4758 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4761 case Tabular::SET_LINE_TOP:
4762 case Tabular::SET_LINE_BOTTOM:
4763 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4766 case Tabular::SET_LINE_LEFT:
4767 case Tabular::SET_LINE_RIGHT:
4768 status.setEnabled(!tabular.use_booktabs
4769 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4772 case Tabular::TOGGLE_LINE_TOP:
4773 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4774 status.setOnOff(tabular.topLine(cur.idx()));
4777 case Tabular::TOGGLE_LINE_BOTTOM:
4778 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4779 status.setOnOff(tabular.bottomLine(cur.idx()));
4782 case Tabular::TOGGLE_LINE_LEFT:
4783 status.setEnabled(!tabular.use_booktabs
4784 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4785 status.setOnOff(tabular.leftLine(cur.idx()));
4788 case Tabular::TOGGLE_LINE_RIGHT:
4789 status.setEnabled(!tabular.use_booktabs
4790 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4791 status.setOnOff(tabular.rightLine(cur.idx()));
4794 // multirow cells only inherit the alignment of the column if the column has
4795 // no width, otherwise they are left-aligned
4796 // therefore allow always left but right and center only if there is no width
4797 case Tabular::M_ALIGN_LEFT:
4800 case Tabular::ALIGN_LEFT:
4801 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4804 case Tabular::M_ALIGN_RIGHT:
4807 case Tabular::ALIGN_RIGHT:
4808 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4809 && !tabular.getPWidth(cur.idx()).zero()));
4810 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4813 case Tabular::M_ALIGN_CENTER:
4816 case Tabular::ALIGN_CENTER:
4817 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4818 && !tabular.getPWidth(cur.idx()).zero()));
4819 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4822 case Tabular::ALIGN_BLOCK:
4823 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4824 && !tabular.isMultiRow(cur.idx()));
4825 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4828 case Tabular::ALIGN_DECIMAL:
4829 status.setEnabled(!tabular.isMultiRow(cur.idx())
4830 && !tabular.isMultiColumn(cur.idx()));
4831 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4834 case Tabular::M_VALIGN_TOP:
4837 case Tabular::VALIGN_TOP:
4838 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4839 && !tabular.isMultiRow(cur.idx()));
4841 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4844 case Tabular::M_VALIGN_BOTTOM:
4847 case Tabular::VALIGN_BOTTOM:
4848 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4849 && !tabular.isMultiRow(cur.idx()));
4851 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4854 case Tabular::M_VALIGN_MIDDLE:
4857 case Tabular::VALIGN_MIDDLE:
4858 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4859 && !tabular.isMultiRow(cur.idx()));
4861 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4864 case Tabular::SET_LONGTABULAR:
4865 case Tabular::TOGGLE_LONGTABULAR:
4866 // setting as longtable is not allowed when table is inside a float
4867 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4868 || cur.innerInsetOfType(WRAP_CODE) != 0)
4869 status.setEnabled(false);
4871 status.setEnabled(true);
4872 status.setOnOff(tabular.is_long_tabular);
4875 case Tabular::UNSET_LONGTABULAR:
4876 status.setOnOff(!tabular.is_long_tabular);
4879 case Tabular::TOGGLE_ROTATE_TABULAR:
4880 case Tabular::SET_ROTATE_TABULAR:
4881 status.setOnOff(tabular.rotate != 0);
4884 case Tabular::TABULAR_VALIGN_TOP:
4885 status.setEnabled(tabular.tabular_width.zero());
4886 status.setOnOff(tabular.tabular_valignment
4887 == Tabular::LYX_VALIGN_TOP);
4889 case Tabular::TABULAR_VALIGN_MIDDLE:
4890 status.setEnabled(tabular.tabular_width.zero());
4891 status.setOnOff(tabular.tabular_valignment
4892 == Tabular::LYX_VALIGN_MIDDLE);
4894 case Tabular::TABULAR_VALIGN_BOTTOM:
4895 status.setEnabled(tabular.tabular_width.zero());
4896 status.setOnOff(tabular.tabular_valignment
4897 == Tabular::LYX_VALIGN_BOTTOM);
4900 case Tabular::LONGTABULAR_ALIGN_LEFT:
4901 status.setOnOff(tabular.longtabular_alignment
4902 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4904 case Tabular::LONGTABULAR_ALIGN_CENTER:
4905 status.setOnOff(tabular.longtabular_alignment
4906 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4908 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4909 status.setOnOff(tabular.longtabular_alignment
4910 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4913 case Tabular::UNSET_ROTATE_TABULAR:
4914 status.setOnOff(tabular.rotate == 0);
4917 case Tabular::TOGGLE_ROTATE_CELL:
4918 case Tabular::SET_ROTATE_CELL:
4919 status.setOnOff(!oneCellHasRotationState(false,
4920 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4923 case Tabular::UNSET_ROTATE_CELL:
4924 status.setOnOff(!oneCellHasRotationState(true,
4925 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4928 case Tabular::SET_USEBOX:
4929 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4932 // every row can only be one thing:
4933 // either a footer or header
4934 case Tabular::SET_LTFIRSTHEAD:
4935 status.setEnabled(sel_row_start == sel_row_end);
4936 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4939 case Tabular::UNSET_LTFIRSTHEAD:
4940 status.setEnabled(sel_row_start == sel_row_end);
4941 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4944 case Tabular::SET_LTHEAD:
4945 status.setEnabled(sel_row_start == sel_row_end);
4946 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4949 case Tabular::UNSET_LTHEAD:
4950 status.setEnabled(sel_row_start == sel_row_end);
4951 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4954 case Tabular::SET_LTFOOT:
4955 status.setEnabled(sel_row_start == sel_row_end);
4956 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4959 case Tabular::UNSET_LTFOOT:
4960 status.setEnabled(sel_row_start == sel_row_end);
4961 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4964 case Tabular::SET_LTLASTFOOT:
4965 status.setEnabled(sel_row_start == sel_row_end);
4966 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4969 case Tabular::UNSET_LTLASTFOOT:
4970 status.setEnabled(sel_row_start == sel_row_end);
4971 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4974 case Tabular::SET_LTNEWPAGE:
4975 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4977 case Tabular::UNSET_LTNEWPAGE:
4978 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4981 // only one row in head/firsthead/foot/lasthead can be the caption
4982 // and a multirow cannot be set as caption
4983 case Tabular::SET_LTCAPTION:
4984 status.setEnabled(sel_row_start == sel_row_end
4985 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4986 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4987 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4988 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4989 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4990 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4991 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4992 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4993 && !tabular.isMultiRow(sel_row_start));
4994 status.setOnOff(tabular.ltCaption(sel_row_start));
4997 case Tabular::UNSET_LTCAPTION:
4998 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5001 case Tabular::TOGGLE_LTCAPTION:
5002 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5003 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5004 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5005 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5006 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5007 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5008 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5009 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5010 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5011 status.setOnOff(tabular.ltCaption(sel_row_start));
5014 case Tabular::TOGGLE_BOOKTABS:
5015 case Tabular::SET_BOOKTABS:
5016 status.setOnOff(tabular.use_booktabs);
5019 case Tabular::UNSET_BOOKTABS:
5020 status.setOnOff(!tabular.use_booktabs);
5025 status.setEnabled(false);
5032 // function sets an object as defined in FuncStatus.h:
5033 // states OK, Unknown, Disabled, On, Off.
5034 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5035 FuncStatus & status) const
5037 switch (cmd.action()) {
5038 case LFUN_INSET_MODIFY:
5039 if (cmd.getArg(0) != "tabular")
5041 if (cmd.getArg(1) == "for-dialog") {
5042 // The dialog is asking the status of a command
5043 if (&cur.inset() != this)
5045 string action = cmd.getArg(2);
5046 string arg = cmd.getLongArg(3);
5047 return getFeatureStatus(cur, action, arg, status);
5049 // We always enable the lfun if it is coming from the dialog
5050 // because the dialog makes sure all the settings are valid,
5051 // even though the first argument might not be valid now.
5052 status.setEnabled(true);
5056 case LFUN_TABULAR_FEATURE: {
5057 if (&cur.inset() != this)
5059 string action = cmd.getArg(0);
5060 string arg = cmd.getLongArg(1);
5061 return getFeatureStatus(cur, action, arg, status);
5064 case LFUN_CAPTION_INSERT: {
5065 // caption is only allowed in caption cell of longtable
5066 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5067 status.setEnabled(false);
5070 // only standard caption is allowed
5071 string arg = cmd.getArg(0);
5072 if (!arg.empty() && arg != "Standard") {
5073 status.setEnabled(false);
5076 // check if there is already a caption
5077 bool have_caption = false;
5078 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5079 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5080 ParagraphList::const_iterator pend = itc.paragraphs().end();
5081 for (; pit != pend; ++pit) {
5082 InsetList::const_iterator it = pit->insetList().begin();
5083 InsetList::const_iterator end = pit->insetList().end();
5084 for (; it != end; ++it) {
5085 if (it->inset->lyxCode() == CAPTION_CODE) {
5086 have_caption = true;
5091 status.setEnabled(!have_caption);
5095 // These are only enabled inside tabular
5096 case LFUN_CELL_BACKWARD:
5097 case LFUN_CELL_FORWARD:
5098 status.setEnabled(true);
5101 // disable these with multiple cells selected
5102 case LFUN_INSET_INSERT:
5103 case LFUN_TABULAR_INSERT:
5104 case LFUN_FLEX_INSERT:
5105 case LFUN_FLOAT_INSERT:
5106 case LFUN_FLOAT_WIDE_INSERT:
5107 case LFUN_FOOTNOTE_INSERT:
5108 case LFUN_MARGINALNOTE_INSERT:
5109 case LFUN_MATH_INSERT:
5110 case LFUN_MATH_MODE:
5111 case LFUN_MATH_MUTATE:
5112 case LFUN_MATH_DISPLAY:
5113 case LFUN_NOTE_INSERT:
5114 case LFUN_ARGUMENT_INSERT:
5115 case LFUN_BOX_INSERT:
5116 case LFUN_BRANCH_INSERT:
5117 case LFUN_PHANTOM_INSERT:
5118 case LFUN_WRAP_INSERT:
5119 case LFUN_PREVIEW_INSERT:
5120 case LFUN_ERT_INSERT: {
5121 if (cur.selIsMultiCell()) {
5122 status.setEnabled(false);
5125 return cell(cur.idx())->getStatus(cur, cmd, status);
5128 // disable in non-fixed-width cells
5129 case LFUN_PARAGRAPH_BREAK:
5130 // multirow does not allow paragraph breaks
5131 if (tabular.isMultiRow(cur.idx())) {
5132 status.setEnabled(false);
5136 case LFUN_NEWLINE_INSERT: {
5137 if (tabular.getPWidth(cur.idx()).zero()) {
5138 status.setEnabled(false);
5141 return cell(cur.idx())->getStatus(cur, cmd, status);
5144 case LFUN_NEWPAGE_INSERT:
5145 status.setEnabled(false);
5149 if (tabularStackDirty() && theClipboard().isInternal()) {
5150 if (cur.selIsMultiCell()) {
5153 getSelection(cur, rs, re, cs, ce);
5154 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5155 && paste_tabular->nrows() == re - rs + 1)
5156 status.setEnabled(true);
5158 status.setEnabled(false);
5159 status.message(_("Selection size should match clipboard content."));
5162 status.setEnabled(true);
5165 return cell(cur.idx())->getStatus(cur, cmd, status);
5167 case LFUN_INSET_SETTINGS:
5168 // relay this lfun to Inset, not to the cell.
5169 return Inset::getStatus(cur, cmd, status);
5172 // we try to handle this event in the insets dispatch function.
5173 return cell(cur.idx())->getStatus(cur, cmd, status);
5179 Inset::DisplayType InsetTabular::display() const
5181 if (tabular.is_long_tabular) {
5182 switch (tabular.longtabular_alignment) {
5183 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5185 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5187 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5197 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5199 tabular.latex(os, runparams);
5203 int InsetTabular::plaintext(odocstringstream & os,
5204 OutputParams const & runparams, size_t max_length) const
5206 os << '\n'; // output table on a new line
5207 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5208 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5209 return PLAINTEXT_NEWLINE;
5213 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5218 // FIXME: Why not pass a proper DocIterator here?
5220 // if the table is inside a float it doesn't need the informaltable
5221 // wrapper. Search for it.
5222 for (master = owner(); master; master = master->owner())
5223 if (master->lyxCode() == FLOAT_CODE)
5228 os << "<informaltable>";
5231 ret += tabular.docbook(os, runparams);
5233 os << "</informaltable>";
5240 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5242 return tabular.xhtml(xs, rp);
5246 void InsetTabular::validate(LaTeXFeatures & features) const
5248 tabular.validate(features);
5249 features.useInsetLayout(getLayout());
5253 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5255 return tabular.cellInset(idx);
5259 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5261 return tabular.cellInset(idx);
5265 void InsetTabular::cursorPos(BufferView const & bv,
5266 CursorSlice const & sl, bool boundary, int & x, int & y) const
5268 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5270 // y offset correction
5271 y += cellYPos(sl.idx());
5272 y += tabular.textVOffset(sl.idx());
5273 y += tabular.offsetVAlignment();
5275 // x offset correction
5276 x += cellXPos(sl.idx());
5277 x += tabular.textHOffset(sl.idx());
5278 x += ADD_TO_TABULAR_WIDTH;
5282 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5286 Inset const & inset = *tabular.cellInset(cell);
5287 Point o = bv.coordCache().getInsets().xy(&inset);
5288 int const xbeg = o.x_ - tabular.textHOffset(cell);
5289 int const xend = xbeg + tabular.cellWidth(cell);
5290 row_type const row = tabular.cellRow(cell);
5291 int const ybeg = o.y_ - tabular.rowAscent(row)
5292 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5293 int const yend = ybeg + tabular.cellHeight(cell);
5305 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5306 // << " ybeg=" << ybeg << " yend=" << yend
5307 // << " xx=" << xx << " yy=" << yy
5308 // << " dist=" << xx + yy << endl;
5313 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5315 //lyxerr << "InsetTabular::editXY: " << this << endl;
5317 cur.idx() = getNearestCell(cur.bv(), x, y);
5318 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5322 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5324 cur.idx() = getNearestCell(cur.bv(), x, y);
5325 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5329 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5331 idx_type idx_min = 0;
5332 int dist_min = numeric_limits<int>::max();
5333 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5334 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5335 int const d = dist(bv, i, x, y);
5346 int InsetTabular::cellYPos(idx_type const cell) const
5348 row_type row = tabular.cellRow(cell);
5350 for (row_type r = 0; r < row; ++r)
5351 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5352 + tabular.interRowSpace(r + 1);
5357 int InsetTabular::cellXPos(idx_type const cell) const
5359 col_type col = tabular.cellColumn(cell);
5361 for (col_type c = 0; c < col; ++c)
5362 lx += tabular.column_info[c].width;
5367 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5369 row_type const row = tabular.cellRow(cur.idx());
5370 col_type const col = tabular.cellColumn(cur.idx());
5372 if (isRightToLeft(cur)) {
5373 if (tabular.cellColumn(cur.idx()) == 0) {
5374 if (row == tabular.nrows() - 1)
5376 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5381 cur.idx() = tabular.getLastCellInRow(row - 1);
5383 cur.idx() = tabular.cellIndex(row, col - 1);
5386 if (tabular.isLastCell(cur.idx()))
5388 if (cur.idx() == tabular.getLastCellInRow(row))
5389 cur.idx() = tabular.cellIndex(row + 1, 0);
5391 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5392 cur.idx() = tabular.cellIndex(row, colnextcell);
5396 cur.boundary(false);
5398 if (cur.selIsMultiCell()) {
5399 cur.pit() = cur.lastpit();
5400 cur.pos() = cur.lastpos();
5407 // in visual mode, place cursor at extreme left or right
5409 switch(entry_from) {
5411 case ENTRY_DIRECTION_RIGHT:
5412 cur.posVisToRowExtremity(false /* !left */);
5414 case ENTRY_DIRECTION_LEFT:
5415 cur.posVisToRowExtremity(true /* left */);
5417 case ENTRY_DIRECTION_IGNORE:
5418 // nothing to do in this case
5422 cur.setCurrentFont();
5426 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5428 row_type const row = tabular.cellRow(cur.idx());
5429 col_type const col = tabular.cellColumn(cur.idx());
5431 if (isRightToLeft(cur)) {
5432 if (cur.idx() == tabular.getLastCellInRow(row)) {
5435 cur.idx() = tabular.getFirstCellInRow(row);
5436 cur.idx() = tabular.cellAbove(cur.idx());
5438 if (tabular.isLastCell(cur.idx()))
5440 if (cur.idx() == tabular.getLastCellInRow(row))
5441 cur.idx() = tabular.cellIndex(row + 1, 0);
5443 cur.idx() = tabular.cellIndex(row, col + 1);
5446 if (cur.idx() == 0) // first cell
5449 cur.idx() = tabular.getLastCellInRow(row - 1);
5451 cur.idx() = tabular.cellIndex(row, col - 1);
5454 if (cur.selIsMultiCell()) {
5455 cur.pit() = cur.lastpit();
5456 cur.pos() = cur.lastpos();
5460 cur.pit() = cur.lastpit();
5461 cur.pos() = cur.lastpos();
5463 // in visual mode, place cursor at extreme left or right
5465 switch(entry_from) {
5467 case ENTRY_DIRECTION_RIGHT:
5468 cur.posVisToRowExtremity(false /* !left */);
5470 case ENTRY_DIRECTION_LEFT:
5471 cur.posVisToRowExtremity(true /* left */);
5473 case ENTRY_DIRECTION_IGNORE:
5474 // nothing to do in this case
5478 cur.setCurrentFont();
5482 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5484 cur.recordUndoInset(this);
5486 istringstream is(argument);
5487 // limit the size of strings we read to avoid memory problems
5491 size_t safe_guard = 0;
5496 if (safe_guard > 1000) {
5497 LYXERR0("parameter max count reached!");
5501 Tabular::Feature action = Tabular::LAST_ACTION;
5504 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5505 if (s != tabularFeature[i].feature)
5507 action = tabularFeature[i].action;
5510 if (action == Tabular::LAST_ACTION) {
5511 LYXERR0("Feature not found " << s);
5515 if (tabularFeature[i].need_value)
5517 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5518 tabularFeatures(cur, action, val);
5523 static void checkLongtableSpecial(Tabular::ltType & ltt,
5524 string const & special, bool & flag)
5526 if (special == "dl_above") {
5529 } else if (special == "dl_below") {
5530 ltt.bottomDL = flag;
5532 } else if (special == "empty") {
5542 bool InsetTabular::oneCellHasRotationState(bool rotated,
5543 row_type row_start, row_type row_end,
5544 col_type col_start, col_type col_end) const
5546 for (row_type r = row_start; r <= row_end; ++r)
5547 for (col_type c = col_start; c <= col_end; ++c)
5549 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5552 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5559 void InsetTabular::tabularFeatures(Cursor & cur,
5560 Tabular::Feature feature, string const & value)
5562 col_type sel_col_start;
5563 col_type sel_col_end;
5564 row_type sel_row_start;
5565 row_type sel_row_end;
5566 bool setLines = false;
5567 bool setLinesInnerOnly = false;
5568 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5569 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5573 case Tabular::M_ALIGN_LEFT:
5574 case Tabular::ALIGN_LEFT:
5575 setAlign = LYX_ALIGN_LEFT;
5578 case Tabular::M_ALIGN_RIGHT:
5579 case Tabular::ALIGN_RIGHT:
5580 setAlign = LYX_ALIGN_RIGHT;
5583 case Tabular::M_ALIGN_CENTER:
5584 case Tabular::ALIGN_CENTER:
5585 setAlign = LYX_ALIGN_CENTER;
5588 case Tabular::ALIGN_BLOCK:
5589 setAlign = LYX_ALIGN_BLOCK;
5592 case Tabular::ALIGN_DECIMAL:
5593 setAlign = LYX_ALIGN_DECIMAL;
5596 case Tabular::M_VALIGN_TOP:
5597 case Tabular::VALIGN_TOP:
5598 setVAlign = Tabular::LYX_VALIGN_TOP;
5601 case Tabular::M_VALIGN_BOTTOM:
5602 case Tabular::VALIGN_BOTTOM:
5603 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5606 case Tabular::M_VALIGN_MIDDLE:
5607 case Tabular::VALIGN_MIDDLE:
5608 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5615 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5616 row_type const row = tabular.cellRow(cur.idx());
5617 col_type const column = tabular.cellColumn(cur.idx());
5619 Tabular::ltType ltt;
5623 case Tabular::SET_TABULAR_WIDTH:
5624 tabular.setTabularWidth(Length(value));
5627 case Tabular::SET_PWIDTH: {
5628 Length const len(value);
5629 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5630 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
5632 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
5633 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5638 case Tabular::SET_MPWIDTH:
5639 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5642 case Tabular::SET_MROFFSET:
5643 tabular.setMROffset(cur, cur.idx(), Length(value));
5646 case Tabular::SET_SPECIAL_COLUMN:
5647 case Tabular::SET_SPECIAL_MULTICOLUMN:
5648 if (value == "none")
5649 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5651 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5654 case Tabular::APPEND_ROW:
5655 // append the row into the tabular
5656 tabular.appendRow(row);
5659 case Tabular::APPEND_COLUMN:
5660 // append the column into the tabular
5661 tabular.appendColumn(column);
5662 cur.idx() = tabular.cellIndex(row, column);
5665 case Tabular::DELETE_ROW:
5666 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5667 for (col_type c = 0; c < tabular.ncols(); c++)
5668 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5669 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5672 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5673 tabular.deleteRow(sel_row_start);
5674 if (sel_row_start >= tabular.nrows())
5676 cur.idx() = tabular.cellIndex(sel_row_start, column);
5679 cur.selection(false);
5682 case Tabular::DELETE_COLUMN:
5683 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5684 for (row_type r = 0; r < tabular.nrows(); r++)
5685 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5686 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5689 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5690 for (row_type r = 0; r < tabular.nrows(); r++)
5691 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5692 tabular.leftLine(tabular.cellIndex(r, 0)));
5695 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5696 tabular.deleteColumn(sel_col_start);
5697 if (sel_col_start >= tabular.ncols())
5699 cur.idx() = tabular.cellIndex(row, sel_col_start);
5702 cur.selection(false);
5705 case Tabular::COPY_ROW:
5706 tabular.copyRow(row);
5709 case Tabular::COPY_COLUMN:
5710 tabular.copyColumn(column);
5711 cur.idx() = tabular.cellIndex(row, column);
5714 case Tabular::MOVE_COLUMN_RIGHT:
5715 tabular.moveColumn(column, Tabular::RIGHT);
5716 cur.idx() = tabular.cellIndex(row, column + 1);
5719 case Tabular::MOVE_COLUMN_LEFT:
5720 tabular.moveColumn(column, Tabular::LEFT);
5721 cur.idx() = tabular.cellIndex(row, column - 1);
5724 case Tabular::MOVE_ROW_DOWN:
5725 tabular.moveRow(row, Tabular::DOWN);
5726 cur.idx() = tabular.cellIndex(row + 1, column);
5729 case Tabular::MOVE_ROW_UP:
5730 tabular.moveRow(row, Tabular::UP);
5731 cur.idx() = tabular.cellIndex(row - 1, column);
5734 case Tabular::SET_LINE_TOP:
5735 case Tabular::TOGGLE_LINE_TOP: {
5736 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5737 ? (value == "true") : !tabular.topLine(cur.idx());
5738 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5739 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5740 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5744 case Tabular::SET_LINE_BOTTOM:
5745 case Tabular::TOGGLE_LINE_BOTTOM: {
5746 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5747 ? (value == "true") : !tabular.bottomLine(cur.idx());
5748 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5749 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5750 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5754 case Tabular::SET_LINE_LEFT:
5755 case Tabular::TOGGLE_LINE_LEFT: {
5756 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5757 ? (value == "true") : !tabular.leftLine(cur.idx());
5758 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5759 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5760 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5764 case Tabular::SET_LINE_RIGHT:
5765 case Tabular::TOGGLE_LINE_RIGHT: {
5766 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5767 ? (value == "true") : !tabular.rightLine(cur.idx());
5768 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5769 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5770 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5774 case Tabular::M_ALIGN_LEFT:
5775 case Tabular::M_ALIGN_RIGHT:
5776 case Tabular::M_ALIGN_CENTER:
5777 case Tabular::ALIGN_LEFT:
5778 case Tabular::ALIGN_RIGHT:
5779 case Tabular::ALIGN_CENTER:
5780 case Tabular::ALIGN_BLOCK:
5781 case Tabular::ALIGN_DECIMAL:
5782 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5783 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5784 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5785 !tabular.getPWidth(c).zero());
5788 case Tabular::M_VALIGN_TOP:
5789 case Tabular::M_VALIGN_BOTTOM:
5790 case Tabular::M_VALIGN_MIDDLE:
5793 case Tabular::VALIGN_TOP:
5794 case Tabular::VALIGN_BOTTOM:
5795 case Tabular::VALIGN_MIDDLE:
5796 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5797 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5798 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5801 case Tabular::SET_MULTICOLUMN: {
5802 if (!cur.selection()) {
5803 // just multicol for one single cell
5804 // check whether we are completely in a multicol
5805 if (!tabular.isMultiColumn(cur.idx()))
5806 tabular.setMultiColumn(cur.idx(), 1,
5807 tabular.rightLine(cur.idx()));
5810 // we have a selection so this means we just add all these
5811 // cells to form a multicolumn cell
5812 idx_type const s_start = cur.selBegin().idx();
5813 row_type const col_start = tabular.cellColumn(s_start);
5814 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5815 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5816 tabular.rightLine(cur.selEnd().idx()));
5819 cur.selection(false);
5823 case Tabular::UNSET_MULTICOLUMN: {
5824 if (!cur.selection()) {
5825 if (tabular.isMultiColumn(cur.idx()))
5826 tabular.unsetMultiColumn(cur.idx());
5831 case Tabular::MULTICOLUMN: {
5832 if (!cur.selection()) {
5833 if (tabular.isMultiColumn(cur.idx()))
5834 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5836 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5840 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5841 row_type const r = sel_row_start;
5842 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5843 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5846 // If the selection contains at least one singlecol cell
5847 // or multiple multicol cells,
5848 // we assume the user will merge is to a single multicol
5850 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5852 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5856 case Tabular::SET_MULTIROW: {
5857 if (!cur.selection()) {
5858 // just multirow for one single cell
5859 // check whether we are completely in a multirow
5860 if (!tabular.isMultiRow(cur.idx()))
5861 tabular.setMultiRow(cur.idx(), 1,
5862 tabular.bottomLine(cur.idx()),
5863 tabular.getAlignment(cur.idx()));
5866 // we have a selection so this means we just add all this
5867 // cells to form a multirow cell
5868 idx_type const s_start = cur.selBegin().idx();
5869 row_type const row_start = tabular.cellRow(s_start);
5870 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5871 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5872 tabular.bottomLine(cur.selEnd().idx()),
5873 tabular.getAlignment(cur.selEnd().idx()));
5876 cur.selection(false);
5880 case Tabular::UNSET_MULTIROW: {
5881 if (!cur.selection()) {
5882 if (tabular.isMultiRow(cur.idx()))
5883 tabular.unsetMultiRow(cur.idx());
5888 case Tabular::MULTIROW: {
5889 if (!cur.selection()) {
5890 if (tabular.isMultiRow(cur.idx()))
5891 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5893 tabularFeatures(cur, Tabular::SET_MULTIROW);
5897 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5898 col_type const c = sel_col_start;
5899 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5900 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5903 // If the selection contains at least one singlerow cell
5904 // or multiple multirow cells,
5905 // we assume the user will merge is to a single multirow
5907 tabularFeatures(cur, Tabular::SET_MULTIROW);
5909 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5913 case Tabular::SET_INNER_LINES:
5914 setLinesInnerOnly = true;
5916 case Tabular::SET_ALL_LINES:
5919 case Tabular::UNSET_ALL_LINES:
5920 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5921 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5922 idx_type const cell = tabular.cellIndex(r, c);
5923 if (!setLinesInnerOnly || r != sel_row_start)
5924 tabular.setTopLine(cell, setLines);
5925 if ((!setLinesInnerOnly || r != sel_row_end)
5926 && (!setLines || r == sel_row_end))
5927 tabular.setBottomLine(cell, setLines);
5928 if ((!setLinesInnerOnly || c != sel_col_end)
5929 && (!setLines || c == sel_col_end))
5930 tabular.setRightLine(cell, setLines);
5931 if ((!setLinesInnerOnly || c != sel_col_start))
5932 tabular.setLeftLine(cell, setLines);
5936 case Tabular::SET_BORDER_LINES:
5937 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5938 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5939 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5941 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5942 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5943 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5947 case Tabular::TOGGLE_LONGTABULAR:
5948 if (tabular.is_long_tabular)
5949 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
5951 tabular.is_long_tabular = true;
5954 case Tabular::SET_LONGTABULAR:
5955 tabular.is_long_tabular = true;
5958 case Tabular::UNSET_LONGTABULAR:
5959 for (row_type r = 0; r < tabular.nrows(); ++r) {
5960 if (tabular.ltCaption(r)) {
5961 cur.idx() = tabular.cellIndex(r, 0);
5964 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5967 tabular.is_long_tabular = false;
5970 case Tabular::SET_ROTATE_TABULAR:
5971 tabular.rotate = convert<int>(value);
5974 case Tabular::UNSET_ROTATE_TABULAR:
5978 case Tabular::TOGGLE_ROTATE_TABULAR:
5979 // when pressing the rotate button we default to 90° rotation
5980 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5983 case Tabular::TABULAR_VALIGN_TOP:
5984 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5987 case Tabular::TABULAR_VALIGN_MIDDLE:
5988 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5991 case Tabular::TABULAR_VALIGN_BOTTOM:
5992 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5995 case Tabular::LONGTABULAR_ALIGN_LEFT:
5996 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5999 case Tabular::LONGTABULAR_ALIGN_CENTER:
6000 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6003 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6004 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6007 case Tabular::SET_ROTATE_CELL:
6008 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6009 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6010 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6013 case Tabular::UNSET_ROTATE_CELL:
6014 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6015 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6016 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6019 case Tabular::TOGGLE_ROTATE_CELL:
6021 bool oneNotRotated = oneCellHasRotationState(false,
6022 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6024 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6025 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6026 // when pressing the rotate cell button we default to 90° rotation
6028 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6030 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6035 case Tabular::SET_USEBOX: {
6036 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6037 if (val == tabular.getUsebox(cur.idx()))
6038 val = Tabular::BOX_NONE;
6039 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6040 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6041 tabular.setUsebox(tabular.cellIndex(r, c), val);
6045 case Tabular::UNSET_LTFIRSTHEAD:
6048 case Tabular::SET_LTFIRSTHEAD:
6049 tabular.getRowOfLTFirstHead(row, ltt);
6050 checkLongtableSpecial(ltt, value, flag);
6051 tabular.setLTHead(row, flag, ltt, true);
6054 case Tabular::UNSET_LTHEAD:
6057 case Tabular::SET_LTHEAD:
6058 tabular.getRowOfLTHead(row, ltt);
6059 checkLongtableSpecial(ltt, value, flag);
6060 tabular.setLTHead(row, flag, ltt, false);
6063 case Tabular::UNSET_LTFOOT:
6066 case Tabular::SET_LTFOOT:
6067 tabular.getRowOfLTFoot(row, ltt);
6068 checkLongtableSpecial(ltt, value, flag);
6069 tabular.setLTFoot(row, flag, ltt, false);
6072 case Tabular::UNSET_LTLASTFOOT:
6075 case Tabular::SET_LTLASTFOOT:
6076 tabular.getRowOfLTLastFoot(row, ltt);
6077 checkLongtableSpecial(ltt, value, flag);
6078 tabular.setLTFoot(row, flag, ltt, true);
6081 case Tabular::UNSET_LTNEWPAGE:
6084 case Tabular::SET_LTNEWPAGE:
6085 tabular.setLTNewPage(row, flag);
6088 case Tabular::SET_LTCAPTION: {
6089 if (tabular.ltCaption(row))
6091 cur.idx() = tabular.setLTCaption(row, true);
6094 cur.selection(false);
6095 // If a row is set as caption, then also insert
6096 // a caption. Otherwise the LaTeX output is broken.
6097 // Select cell if it is non-empty
6098 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6099 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6100 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6104 case Tabular::UNSET_LTCAPTION: {
6105 if (!tabular.ltCaption(row))
6107 cur.idx() = tabular.setLTCaption(row, false);
6110 cur.selection(false);
6111 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6112 if (lyx::getStatus(fr).enabled())
6117 case Tabular::TOGGLE_LTCAPTION: {
6118 if (tabular.ltCaption(row))
6119 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6121 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6125 case Tabular::TOGGLE_BOOKTABS:
6126 tabular.use_booktabs = !tabular.use_booktabs;
6129 case Tabular::SET_BOOKTABS:
6130 tabular.use_booktabs = true;
6133 case Tabular::UNSET_BOOKTABS:
6134 tabular.use_booktabs = false;
6137 case Tabular::SET_TOP_SPACE: {
6139 if (value == "default")
6140 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6141 tabular.row_info[r].top_space_default = true;
6142 else if (value == "none")
6143 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6144 tabular.row_info[r].top_space_default = false;
6145 tabular.row_info[r].top_space = len;
6147 else if (isValidLength(value, &len))
6148 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6149 tabular.row_info[r].top_space_default = false;
6150 tabular.row_info[r].top_space = len;
6155 case Tabular::SET_BOTTOM_SPACE: {
6157 if (value == "default")
6158 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6159 tabular.row_info[r].bottom_space_default = true;
6160 else if (value == "none")
6161 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6162 tabular.row_info[r].bottom_space_default = false;
6163 tabular.row_info[r].bottom_space = len;
6165 else if (isValidLength(value, &len))
6166 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6167 tabular.row_info[r].bottom_space_default = false;
6168 tabular.row_info[r].bottom_space = len;
6173 case Tabular::SET_INTERLINE_SPACE: {
6175 if (value == "default")
6176 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6177 tabular.row_info[r].interline_space_default = true;
6178 else if (value == "none")
6179 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6180 tabular.row_info[r].interline_space_default = false;
6181 tabular.row_info[r].interline_space = len;
6183 else if (isValidLength(value, &len))
6184 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6185 tabular.row_info[r].interline_space_default = false;
6186 tabular.row_info[r].interline_space = len;
6191 case Tabular::SET_DECIMAL_POINT:
6192 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6193 tabular.column_info[c].decimal_point = from_utf8(value);
6196 // dummy stuff just to avoid warnings
6197 case Tabular::LAST_ACTION:
6203 bool InsetTabular::copySelection(Cursor & cur)
6205 if (!cur.selection())
6210 getSelection(cur, rs, re, cs, ce);
6212 paste_tabular.reset(new Tabular(tabular));
6214 for (row_type r = 0; r < rs; ++r)
6215 paste_tabular->deleteRow(0);
6217 row_type const rows = re - rs + 1;
6218 while (paste_tabular->nrows() > rows)
6219 paste_tabular->deleteRow(rows);
6221 for (col_type c = 0; c < cs; ++c)
6222 paste_tabular->deleteColumn(0);
6224 col_type const columns = ce - cs + 1;
6225 while (paste_tabular->ncols() > columns)
6226 paste_tabular->deleteColumn(columns);
6228 paste_tabular->setBuffer(tabular.buffer());
6230 odocstringstream os;
6231 OutputParams const runparams(0);
6232 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6233 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6234 cap::copySelection(cur, os.str());
6236 // mark tabular stack dirty
6237 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6238 // when we (hopefully) have a one-for-all paste mechanism.
6239 // This must be called after cap::copySelection.
6240 dirtyTabularStack(true);
6246 bool InsetTabular::pasteClipboard(Cursor & cur)
6250 col_type actcol = tabular.cellColumn(cur.idx());
6251 row_type actrow = tabular.cellRow(cur.idx());
6253 if (cur.selIsMultiCell()) {
6256 getSelection(cur, actrow, re, actcol, ce);
6259 for (row_type r1 = 0, r2 = actrow;
6260 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6262 for (col_type c1 = 0, c2 = actcol;
6263 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6265 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6266 tabular.isPartOfMultiColumn(r2, c2))
6268 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6272 if (tabular.isPartOfMultiColumn(r2, c2)) {
6276 shared_ptr<InsetTableCell> inset(
6277 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6278 tabular.setCellInset(r2, c2, inset);
6279 // FIXME?: why do we need to do this explicitly? (EL)
6280 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6282 // FIXME: change tracking (MG)
6283 inset->setChange(Change(buffer().params().track_changes ?
6284 Change::INSERTED : Change::UNCHANGED));
6293 void InsetTabular::cutSelection(Cursor & cur)
6295 if (!cur.selection())
6300 getSelection(cur, rs, re, cs, ce);
6301 for (row_type r = rs; r <= re; ++r) {
6302 for (col_type c = cs; c <= ce; ++c) {
6303 shared_ptr<InsetTableCell> t
6304 = cell(tabular.cellIndex(r, c));
6305 if (buffer().params().track_changes)
6306 // FIXME: Change tracking (MG)
6307 t->setChange(Change(Change::DELETED));
6313 // cursor position might be invalid now
6314 if (cur.pit() > cur.lastpit())
6315 cur.pit() = cur.lastpit();
6316 if (cur.pos() > cur.lastpos())
6317 cur.pos() = cur.lastpos();
6318 cur.clearSelection();
6322 bool InsetTabular::isRightToLeft(Cursor & cur) const
6324 // LASSERT: It might be better to abandon this Buffer.
6325 LASSERT(cur.depth() > 1, return false);
6326 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6327 pos_type const parentpos = cur[cur.depth() - 2].pos();
6328 return parentpar.getFontSettings(buffer().params(),
6329 parentpos).language()->rightToLeft();
6333 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6336 LASSERT(stidx <= enidx, return docstring());
6338 col_type const col1 = tabular.cellColumn(stidx);
6339 col_type const col2 = tabular.cellColumn(enidx);
6340 row_type const row1 = tabular.cellRow(stidx);
6341 row_type const row2 = tabular.cellRow(enidx);
6343 for (col_type col = col1; col <= col2; col++)
6344 for (row_type row = row1; row <= row2; row++) {
6349 retval += tabular.cellInset(row, col)->asString(intoInsets);
6355 void InsetTabular::getSelection(Cursor & cur,
6356 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6358 CursorSlice const & beg = cur.selBegin();
6359 CursorSlice const & end = cur.selEnd();
6360 cs = tabular.cellColumn(beg.idx());
6361 ce = tabular.cellColumn(end.idx());
6365 rs = tabular.cellRow(beg.idx());
6366 re = tabular.cellRow(end.idx());
6372 Text * InsetTabular::getText(int idx) const
6374 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6378 void InsetTabular::setChange(Change const & change)
6380 for (idx_type idx = 0; idx < nargs(); ++idx)
6381 cell(idx)->setChange(change);
6385 void InsetTabular::acceptChanges()
6387 for (idx_type idx = 0; idx < nargs(); ++idx)
6388 cell(idx)->acceptChanges();
6392 void InsetTabular::rejectChanges()
6394 for (idx_type idx = 0; idx < nargs(); ++idx)
6395 cell(idx)->rejectChanges();
6399 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6401 return tabular.getPWidth(cell).zero();
6405 bool InsetTabular::forcePlainLayout(idx_type cell) const
6407 return !tabular.getPWidth(cell).zero();
6411 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6414 if (buf.length() <= 0)
6419 col_type maxCols = 1;
6420 size_t const len = buf.length();
6424 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6432 maxCols = max(cols, maxCols);
6438 maxCols = max(cols, maxCols);
6444 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6445 loctab = paste_tabular.get();
6446 dirtyTabularStack(true);
6449 cell = bv.cursor().idx();
6450 ocol = tabular.cellColumn(cell);
6451 row = tabular.cellRow(cell);
6455 idx_type const cells = loctab->numberofcells;
6458 rows = loctab->nrows();
6459 col_type const columns = loctab->ncols();
6461 while (cell < cells && p < len && row < rows &&
6462 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6468 // we can only set this if we are not too far right
6469 if (cols < columns) {
6470 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6471 Font const font = bv.textMetrics(&inset->text()).
6473 inset->setText(buf.substr(op, p - op), font,
6474 buffer().params().track_changes);
6480 // we can only set this if we are not too far right
6481 if (cols < columns) {
6482 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6483 Font const font = bv.textMetrics(&inset->text()).
6485 inset->setText(buf.substr(op, p - op), font,
6486 buffer().params().track_changes);
6491 cell = loctab->cellIndex(row, cols);
6497 // check for the last cell if there is no trailing '\n'
6498 if (cell < cells && op < len) {
6499 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6500 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6501 inset->setText(buf.substr(op, len - op), font,
6502 buffer().params().track_changes);
6508 void InsetTabular::addPreview(DocIterator const & inset_pos,
6509 PreviewLoader & loader) const
6511 DocIterator cell_pos = inset_pos;
6513 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6514 for (row_type r = 0; r < tabular.nrows(); ++r) {
6515 for (col_type c = 0; c < tabular.ncols(); ++c) {
6516 cell_pos.top().idx() = tabular.cellIndex(r, c);
6517 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6523 bool InsetTabular::completionSupported(Cursor const & cur) const
6525 Cursor const & bvCur = cur.bv().cursor();
6526 if (&bvCur.inset() != this)
6528 return cur.text()->completionSupported(cur);
6532 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6534 return completionSupported(cur);
6538 bool InsetTabular::automaticInlineCompletion() const
6540 return lyxrc.completion_inline_text;
6544 bool InsetTabular::automaticPopupCompletion() const
6546 return lyxrc.completion_popup_text;
6550 bool InsetTabular::showCompletionCursor() const
6552 return lyxrc.completion_cursor_text;
6556 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6558 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6562 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6564 if (!completionSupported(cur))
6566 return cur.text()->completionPrefix(cur);
6570 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6572 if (!completionSupported(cur))
6575 return cur.text()->insertCompletion(cur, s, finished);
6579 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6580 Dimension & dim) const
6582 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6583 tm.completionPosAndDim(cur, x, y, dim);
6587 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6589 istringstream data(in);
6591 lex.setStream(data);
6598 if (!lex || token != "tabular") {
6599 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6603 // This is part of the inset proper that is usually swallowed
6604 // by Buffer::readInset
6606 if (!lex || token != "Tabular") {
6607 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6615 string InsetTabular::params2string(InsetTabular const & inset)
6618 data << "tabular" << ' ';
6620 data << "\\end_inset\n";
6625 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6627 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6628 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6629 if (!tabular.isPartOfMultiColumn(r,c) &&
6630 !tabular.isPartOfMultiRow(r,c))
6633 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6634 ParagraphList::iterator it = parlist.begin();
6635 ParagraphList::iterator const en = parlist.end();
6636 for (; it != en; ++it)
6637 it->setLayout(dc.plainLayout());