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_LTRIM_TOP, "set-ltrim-top", true },
131 { Tabular::SET_LTRIM_BOTTOM, "set-ltrim-bottom", true },
132 { Tabular::SET_RTRIM_TOP, "set-rtrim-top", true },
133 { Tabular::SET_RTRIM_BOTTOM, "set-rtrim-bottom", true },
134 { Tabular::SET_LINE_LEFT, "set-line-left", true },
135 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
136 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
137 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
138 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
139 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
140 { Tabular::TOGGLE_LTRIM_TOP, "toggle-ltrim-top", false },
141 { Tabular::TOGGLE_LTRIM_BOTTOM, "toggle-ltrim-bottom", false },
142 { Tabular::TOGGLE_RTRIM_TOP, "toggle-rtrim-top", false },
143 { Tabular::TOGGLE_RTRIM_BOTTOM, "toggle-rtrim-bottom", false },
144 { Tabular::ALIGN_LEFT, "align-left", false },
145 { Tabular::ALIGN_RIGHT, "align-right", false },
146 { Tabular::ALIGN_CENTER, "align-center", false },
147 { Tabular::ALIGN_BLOCK, "align-block", false },
148 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
149 { Tabular::VALIGN_TOP, "valign-top", false },
150 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
151 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
152 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
153 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
154 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
155 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
156 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
157 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
158 { Tabular::MULTICOLUMN, "multicolumn", false },
159 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
160 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
161 { Tabular::MULTIROW, "multirow", false },
162 { Tabular::SET_MULTIROW, "set-multirow", false },
163 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
164 { Tabular::SET_MROFFSET, "set-mroffset", true },
165 { Tabular::SET_ALL_LINES, "set-all-lines", false },
166 { Tabular::RESET_FORMAL_DEFAULT, "reset-formal-default", false },
167 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
168 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
169 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
170 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
171 { Tabular::SET_PWIDTH, "set-pwidth", true },
172 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
173 { Tabular::TOGGLE_VARWIDTH_COLUMN, "toggle-varwidth-column", true },
174 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
175 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
176 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
177 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
178 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
179 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
180 { Tabular::SET_USEBOX, "set-usebox", true },
181 { Tabular::SET_LTHEAD, "set-lthead", true },
182 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
183 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
184 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
185 { Tabular::SET_LTFOOT, "set-ltfoot", true },
186 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
187 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
188 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
189 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
190 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
191 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
192 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
193 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
194 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
195 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
196 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
197 { Tabular::SET_BOOKTABS, "set-booktabs", false },
198 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
199 { Tabular::SET_TOP_SPACE, "set-top-space", true },
200 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
201 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
202 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
203 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
204 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
205 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
206 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
207 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
208 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
209 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
210 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
211 { Tabular::SET_INNER_LINES, "set-inner-lines", false },
212 { Tabular::LAST_ACTION, "", false }
216 string const tostr(LyXAlignment const & num)
221 case LYX_ALIGN_BLOCK:
225 case LYX_ALIGN_CENTER:
227 case LYX_ALIGN_RIGHT:
229 case LYX_ALIGN_LAYOUT:
231 case LYX_ALIGN_SPECIAL:
233 case LYX_ALIGN_DECIMAL:
240 string const tostr(Tabular::HAlignment const & num)
243 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
245 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
247 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
254 string const tostr(Tabular::VAlignment const & num)
257 case Tabular::LYX_VALIGN_TOP:
259 case Tabular::LYX_VALIGN_MIDDLE:
261 case Tabular::LYX_VALIGN_BOTTOM:
268 string const tostr(Tabular::BoxType const & num)
271 case Tabular::BOX_NONE:
273 case Tabular::BOX_PARBOX:
275 case Tabular::BOX_MINIPAGE:
277 case Tabular::BOX_VARWIDTH:
284 // I would have liked a fromstr template a lot better. (Lgb)
285 bool string2type(string const & str, LyXAlignment & num)
288 num = LYX_ALIGN_NONE;
289 else if (str == "block")
290 num = LYX_ALIGN_BLOCK;
291 else if (str == "left")
292 num = LYX_ALIGN_LEFT;
293 else if (str == "center")
294 num = LYX_ALIGN_CENTER;
295 else if (str == "right")
296 num = LYX_ALIGN_RIGHT;
297 else if (str == "decimal")
298 num = LYX_ALIGN_DECIMAL;
305 bool string2type(string const & str, Tabular::HAlignment & num)
308 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
309 else if (str == "center" )
310 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
311 else if (str == "right")
312 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
319 bool string2type(string const & str, Tabular::VAlignment & num)
322 num = Tabular::LYX_VALIGN_TOP;
323 else if (str == "middle" )
324 num = Tabular::LYX_VALIGN_MIDDLE;
325 else if (str == "bottom")
326 num = Tabular::LYX_VALIGN_BOTTOM;
333 bool string2type(string const & str, Tabular::BoxType & num)
336 num = Tabular::BOX_NONE;
337 else if (str == "parbox")
338 num = Tabular::BOX_PARBOX;
339 else if (str == "minipage")
340 num = Tabular::BOX_MINIPAGE;
341 else if (str == "varwidth")
342 num = Tabular::BOX_VARWIDTH;
349 bool string2type(string const & str, bool & num)
353 else if (str == "false")
361 bool getTokenValue(string const & str, char const * token, string & ret)
364 size_t token_length = strlen(token);
365 size_t pos = str.find(token);
367 if (pos == string::npos || pos + token_length + 1 >= str.length()
368 || str[pos + token_length] != '=')
370 pos += token_length + 1;
372 if (ch != '"' && ch != '\'') { // only read till next space
376 while (pos < str.length() - 1 && str[++pos] != ch)
383 bool getTokenValue(string const & str, char const * token, docstring & ret)
386 bool const success = getTokenValue(str, token, tmp);
387 ret = from_utf8(tmp);
392 bool getTokenValue(string const & str, char const * token, int & num)
396 if (!getTokenValue(str, token, tmp))
398 num = convert<int>(tmp);
403 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
406 return getTokenValue(str, token, tmp) && string2type(tmp, num);
410 bool getTokenValue(string const & str, char const * token,
411 Tabular::HAlignment & num)
414 return getTokenValue(str, token, tmp) && string2type(tmp, num);
418 bool getTokenValue(string const & str, char const * token,
419 Tabular::VAlignment & num)
422 return getTokenValue(str, token, tmp) && string2type(tmp, num);
426 bool getTokenValue(string const & str, char const * token,
427 Tabular::BoxType & num)
430 return getTokenValue(str, token, tmp) && string2type(tmp, num);
434 bool getTokenValue(string const & str, char const * token, bool & flag)
436 // set the flag always to false as this should be the default for bools
437 // not in the file-format.
440 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
444 bool getTokenValue(string const & str, char const * token, Length & len)
446 // set the length to be zero() as default as this it should be if not
447 // in the file format.
450 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
454 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
459 if (!getTokenValue(str, token, tmp))
461 if (tmp == "default") {
465 return isValidLength(tmp, &len);
469 void l_getline(istream & is, string & str)
472 while (str.empty()) {
474 if (!str.empty() && str[str.length() - 1] == '\r')
475 str.erase(str.length() - 1);
480 string const write_attribute(string const & name, T const & t)
482 string const s = tostr(t);
483 return s.empty() ? s : " " + name + "=\"" + s + "\"";
487 string const write_attribute(string const & name, string const & t)
489 return t.empty() ? t : " " + name + "=\"" + t + "\"";
494 string const write_attribute(string const & name, docstring const & t)
496 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
501 string const write_attribute(string const & name, bool const & b)
503 // we write only true attribute values so we remove a bit of the
504 // file format bloat for tabulars.
505 return b ? write_attribute(name, convert<string>(b)) : string();
510 string const write_attribute(string const & name, int const & i)
512 // we write only true attribute values so we remove a bit of the
513 // file format bloat for tabulars.
514 return i ? write_attribute(name, convert<string>(i)) : string();
519 string const write_attribute(string const & name, Tabular::idx_type const & i)
521 // we write only true attribute values so we remove a bit of the
522 // file format bloat for tabulars.
523 return i ? write_attribute(name, convert<string>(i)) : string();
528 string const write_attribute(string const & name, Length const & value)
530 // we write only the value if we really have one same reson as above.
531 return value.zero() ? string() : write_attribute(name, value.asString());
537 string const featureAsString(Tabular::Feature action)
539 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
540 if (tabularFeature[i].action == action)
541 return tabularFeature[i].feature;
547 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
549 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
550 for (; dit; dit.forwardChar())
551 if (dit.inTexted() && dit.depth() == 1
552 && dit.paragraph().find(align_d, false, false, dit.pos()))
559 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
561 InsetTableCell tail = InsetTableCell(head);
562 DocIterator const dit = separatorPos(&head, align_d);
565 pit_type const psize = head.paragraphs().front().size();
566 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
567 tail.paragraphs().front().eraseChars(0,
568 dit.pos() < psize ? dit.pos() + 1 : psize, false);
575 /////////////////////////////////////////////////////////////////////
579 /////////////////////////////////////////////////////////////////////
582 Tabular::CellData::CellData(Buffer * buf)
585 multicolumn(Tabular::CELL_NORMAL),
586 multirow(Tabular::CELL_NORMAL),
587 alignment(LYX_ALIGN_CENTER),
588 valignment(LYX_VALIGN_TOP),
596 top_line_rtrimmed(false),
597 top_line_ltrimmed(false),
598 bottom_line_rtrimmed(false),
599 bottom_line_ltrimmed(false),
602 inset(new InsetTableCell(buf))
604 inset->setBuffer(*buf);
608 Tabular::CellData::CellData(CellData const & cs)
611 multicolumn(cs.multicolumn),
612 multirow(cs.multirow),
613 mroffset(cs.mroffset),
614 alignment(cs.alignment),
615 valignment(cs.valignment),
616 decimal_hoffset(cs.decimal_hoffset),
617 decimal_width(cs.decimal_width),
619 top_line(cs.top_line),
620 bottom_line(cs.bottom_line),
621 left_line(cs.left_line),
622 right_line(cs.right_line),
623 top_line_rtrimmed(cs.top_line_rtrimmed),
624 top_line_ltrimmed(cs.top_line_ltrimmed),
625 bottom_line_rtrimmed(cs.bottom_line_rtrimmed),
626 bottom_line_ltrimmed(cs.bottom_line_ltrimmed),
629 align_special(cs.align_special),
631 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
635 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
641 multicolumn = cs.multicolumn;
642 multirow = cs.multirow;
643 mroffset = cs.mroffset;
644 alignment = cs.alignment;
645 valignment = cs.valignment;
646 decimal_hoffset = cs.decimal_hoffset;
647 decimal_width = cs.decimal_width;
648 voffset = cs.voffset;
649 top_line = cs.top_line;
650 bottom_line = cs.bottom_line;
651 left_line = cs.left_line;
652 right_line = cs.right_line;
653 top_line_rtrimmed = cs.top_line_rtrimmed;
654 top_line_ltrimmed = cs.top_line_ltrimmed;
655 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
656 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
659 align_special = cs.align_special;
660 p_width = cs.p_width;
661 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
665 Tabular::RowData::RowData()
668 top_space_default(false),
669 bottom_space_default(false),
670 interline_space_default(false),
680 Tabular::ColumnData::ColumnData()
681 : alignment(LYX_ALIGN_CENTER),
682 valignment(LYX_VALIGN_TOP),
689 Tabular::ltType::ltType()
690 : set(false), topDL(false),
696 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
698 init(buffer, rows_arg, columns_arg);
702 void Tabular::setBuffer(Buffer & buffer)
705 for (row_type i = 0; i < nrows(); ++i)
706 for (col_type j = 0; j < ncols(); ++j)
707 cell_info[i][j].inset->setBuffer(*buffer_);
711 // activates all lines and sets all widths to 0
712 void Tabular::init(Buffer * buf, row_type rows_arg,
713 col_type columns_arg)
716 row_info = row_vector(rows_arg);
717 column_info = column_vector(columns_arg);
718 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
719 row_info.reserve(10);
720 column_info.reserve(10);
721 cell_info.reserve(100);
723 is_long_tabular = false;
724 tabular_valignment = LYX_VALIGN_MIDDLE;
725 tabular_width = Length();
726 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
728 use_booktabs = false;
729 // set silly default lines
730 for (row_type r = 0; r < nrows(); ++r)
731 for (col_type c = 0; c < ncols(); ++c) {
732 cell_info[r][c].inset->setBuffer(*buffer_);
733 cell_info[r][c].top_line = true;
734 cell_info[r][c].left_line = true;
735 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
736 cell_info[r][c].right_line = c == ncols() - 1;
741 void Tabular::deleteRow(row_type const row)
743 // Not allowed to delete last row
747 for (col_type c = 0; c < ncols(); ++c) {
748 // Care about multirow cells
749 if (row + 1 < nrows() &&
750 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
751 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
752 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
755 row_info.erase(row_info.begin() + row);
756 cell_info.erase(cell_info.begin() + row);
761 void Tabular::copyRow(row_type const row)
763 insertRow(row, true);
767 void Tabular::appendRow(row_type row)
769 insertRow(row, false);
773 void Tabular::insertRow(row_type const row, bool copy)
775 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
776 cell_info.insert(cell_info.begin() + row + 1,
777 cell_vector(0, CellData(buffer_)));
779 for (col_type c = 0; c < ncols(); ++c) {
780 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
781 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
782 if (buffer().params().track_changes)
783 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
784 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
785 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
789 for (col_type c = 0; c < ncols(); ++c) {
790 if (isPartOfMultiRow(row, c))
792 // inherit line settings
793 idx_type const i = cellIndex(row + 1, c);
794 idx_type const j = cellIndex(row, c);
795 setLeftLine(i, leftLine(j));
796 setRightLine(i, rightLine(j));
797 setTopLine(i, topLine(j));
798 if (topLine(j) && bottomLine(j)) {
799 setBottomLine(i, true);
800 setBottomLine(j, false);
802 // mark track changes
803 if (buffer().params().track_changes)
804 cellInfo(i).inset->setChange(Change(Change::INSERTED));
809 void Tabular::moveColumn(col_type col, ColDirection direction)
811 if (direction == Tabular::LEFT)
814 std::swap(column_info[col], column_info[col + 1]);
816 for (row_type r = 0; r < nrows(); ++r) {
817 std::swap(cell_info[r][col], cell_info[r][col + 1]);
818 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
819 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
821 // FIXME track changes is broken for tabular features (#8469)
822 idx_type const i = cellIndex(r, col);
823 idx_type const j = cellIndex(r, col + 1);
824 if (buffer().params().track_changes) {
825 cellInfo(i).inset->setChange(Change(Change::INSERTED));
826 cellInfo(j).inset->setChange(Change(Change::INSERTED));
833 void Tabular::moveRow(row_type row, RowDirection direction)
835 if (direction == Tabular::UP)
838 std::swap(row_info[row], row_info[row + 1]);
840 for (col_type c = 0; c < ncols(); ++c) {
841 std::swap(cell_info[row][c], cell_info[row + 1][c]);
842 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
843 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
845 // FIXME track changes is broken for tabular features (#8469)
846 idx_type const i = cellIndex(row, c);
847 idx_type const j = cellIndex(row + 1, c);
848 if (buffer().params().track_changes) {
849 cellInfo(i).inset->setChange(Change(Change::INSERTED));
850 cellInfo(j).inset->setChange(Change(Change::INSERTED));
857 void Tabular::deleteColumn(col_type const col)
859 // Not allowed to delete last column
863 for (row_type r = 0; r < nrows(); ++r) {
864 // Care about multicolumn cells
865 if (col + 1 < ncols() &&
866 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
867 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
868 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
870 cell_info[r].erase(cell_info[r].begin() + col);
872 column_info.erase(column_info.begin() + col);
877 void Tabular::copyColumn(col_type const col)
879 insertColumn(col, true);
883 void Tabular::appendColumn(col_type col)
885 insertColumn(col, false);
889 void Tabular::insertColumn(col_type const col, bool copy)
891 BufferParams const & bp = buffer().params();
892 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
894 for (row_type r = 0; r < nrows(); ++r) {
895 cell_info[r].insert(cell_info[r].begin() + col + 1,
896 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
897 if (bp.track_changes)
898 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
899 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
900 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
903 for (row_type r = 0; r < nrows(); ++r) {
904 // inherit line settings
905 idx_type const i = cellIndex(r, col + 1);
906 idx_type const j = cellIndex(r, col);
907 setBottomLine(i, bottomLine(j));
908 setTopLine(i, topLine(j));
909 setLeftLine(i, leftLine(j));
910 setRightLine(i, rightLine(j));
911 if (rightLine(i) && rightLine(j)) {
912 setRightLine(j, false);
914 if (buffer().params().track_changes)
915 cellInfo(i).inset->setChange(Change(Change::INSERTED));
920 void Tabular::updateIndexes()
925 for (row_type row = 0; row < nrows(); ++row)
926 for (col_type column = 0; column < ncols(); ++column) {
927 if (!isPartOfMultiColumn(row, column)
928 && !isPartOfMultiRow(row, column))
930 if (isPartOfMultiRow(row, column))
931 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
933 cell_info[row][column].cellno = numberofcells - 1;
936 rowofcell.resize(numberofcells);
937 columnofcell.resize(numberofcells);
939 // reset column and row of cells and update their width and alignment
940 for (row_type row = 0; row < nrows(); ++row)
941 for (col_type column = 0; column < ncols(); ++column) {
942 if (isPartOfMultiColumn(row, column)) {
943 cell_info[row][column].inset->toggleMultiCol(true);
946 cell_info[row][column].inset->toggleMultiCol(false);
947 // columnofcell needs to be called before setting width and aligment
948 // multirow cells inherit the width from the column width
949 if (!isPartOfMultiRow(row, column)) {
950 columnofcell[i] = column;
953 setFixedWidth(row, column);
954 if (isPartOfMultiRow(row, column)) {
955 cell_info[row][column].inset->toggleMultiRow(true);
958 cell_info[row][column].inset->toggleMultiRow(false);
959 cell_info[row][column].inset->setContentAlignment(
960 getAlignment(cellIndex(row, column)));
966 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
969 for (col_type c = 0; c < ncols(); ++c)
970 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
976 bool Tabular::topLine(idx_type const cell) const
978 return cellInfo(cell).top_line;
982 bool Tabular::bottomLine(idx_type const cell) const
984 return cellInfo(cell).bottom_line;
988 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
990 if (use_booktabs && !ignore_bt)
992 return cellInfo(cell).left_line;
996 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
998 if (use_booktabs && !ignore_bt)
1000 return cellInfo(cell).right_line;
1004 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1007 return make_pair(false, false);
1008 return make_pair(cellInfo(cell).top_line_ltrimmed,
1009 cellInfo(cell).top_line_rtrimmed);
1013 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1016 return make_pair(false, false);
1017 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1018 cellInfo(cell).bottom_line_rtrimmed);
1022 int Tabular::interRowSpace(row_type row) const
1024 if (!row || row >= nrows())
1027 int const interline_space = row_info[row - 1].interline_space_default ?
1028 default_line_space :
1029 row_info[row - 1].interline_space.inPixels(width());
1030 if (rowTopLine(row) && rowBottomLine(row - 1))
1031 return interline_space + WIDTH_OF_LINE;
1032 return interline_space;
1036 int Tabular::interColumnSpace(idx_type cell) const
1038 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1039 if (rightLine(cell) && nextcol < ncols()
1040 && leftLine(cellIndex(cellRow(cell), nextcol)))
1041 return WIDTH_OF_LINE;
1046 int Tabular::cellWidth(idx_type cell) const
1049 col_type const span = columnSpan(cell);
1050 col_type const col = cellColumn(cell);
1051 for(col_type c = col; c < col + span ; ++c)
1052 w += column_info[c].width;
1057 int Tabular::cellHeight(idx_type cell) const
1059 row_type const span = rowSpan(cell);
1060 row_type const row = cellRow(cell);
1062 for(row_type r = row; r < row + span ; ++r) {
1063 h += rowAscent(r) + rowDescent(r);
1064 if (r != row + span - 1)
1065 h += interRowSpace(r + 1);
1072 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1074 vector<int> max_dwidth(ncols(), 0);
1075 // collect max. fixed width of column
1076 map<col_type, int> max_pwidth;
1077 // collect max. variable width of column
1078 map<col_type, int> max_width;
1080 for(col_type c = 0; c < ncols(); ++c)
1081 for(row_type r = 0; r < nrows(); ++r) {
1082 idx_type const i = cellIndex(r, c);
1083 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1084 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1085 if (!getPWidth(i).zero())
1086 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1087 else if (!column_info[c].varwidth)
1088 max_width[c] = max(max_width[c], cell_info[r][c].width);
1091 // If we have a fixed tabular width, we take this into account
1092 Length tab_width = tabular_width;
1093 bool const tabularx = hasVarwidthColumn();
1094 if (tabularx && tab_width.zero())
1095 // If no tabular width is specified with X columns,
1096 // we use 100% colwidth
1097 tab_width = Length(100, Length::PCW);
1099 if (!tab_width.zero()) {
1100 restwidth = mi.base.inPixels(tab_width);
1101 // Substract the fixed widths from the table width
1102 for (auto const w : max_pwidth)
1103 restwidth -= w.second;
1106 // If we have a fixed width, distribute the available table width
1107 // (minus the fixed widths) to the variable-width columns
1109 int restcols = ncols() - max_pwidth.size();
1110 if ((restwidth > 0) && (restcols != 0))
1111 vcolwidth = restwidth / restcols;
1113 // Now consider that some variable width columns exceed the vcolwidth
1114 if (vcolwidth > 0) {
1115 bool changed = false;
1116 for (auto const w : max_width) {
1117 if (tabularx || w.second > vcolwidth) {
1119 restwidth -= w.second;
1123 if (changed && restwidth > 0)
1124 vcolwidth = restwidth / restcols;
1127 bool update = false;
1128 // for each col get max of single col cells
1129 for(col_type c = 0; c < ncols(); ++c) {
1131 for(row_type r = 0; r < nrows(); ++r) {
1132 idx_type const i = cellIndex(r, c);
1133 if (columnSpan(i) == 1) {
1134 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1135 && cell_info[r][c].decimal_width != 0)
1136 new_width = max(new_width, cellInfo(i).width
1137 + max_dwidth[c] - cellInfo(i).decimal_width);
1138 else if (getPWidth(i).zero() && vcolwidth > 0) {
1139 if (tabularx && !column_info[c].varwidth)
1140 new_width = max(new_width, cellInfo(i).width);
1142 new_width = vcolwidth;
1144 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1146 new_width = max(new_width, cellInfo(i).width);
1150 if (column_info[c].width != new_width) {
1151 column_info[c].width = new_width;
1152 // Do not trigger update when no space is left for variable
1153 // columns, as this will loop
1154 update = tab_width.zero() || restwidth > 0;
1157 // update col widths to fit merged cells
1158 for(col_type c = 0; c < ncols(); ++c)
1159 for(row_type r = 0; r < nrows(); ++r) {
1160 idx_type const i = cellIndex(r, c);
1161 int const span = columnSpan(i);
1162 if (span == 1 || c > cellColumn(i))
1166 for(col_type j = c; j < c + span ; ++j)
1167 old_width += column_info[j].width;
1169 if (cellInfo(i).width > old_width) {
1170 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1171 // Do not trigger update when no space is left for variable
1172 // columns, as this will loop
1173 update = tab_width.zero() || restwidth > 0;
1181 int Tabular::width() const
1184 for (col_type c = 0; c < ncols(); ++c)
1185 width += column_info[c].width;
1190 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1193 col_type const col = cellColumn(cell);
1194 // set alignment for the whole row if we are not in a multicolumn cell,
1195 // exclude possible multicolumn cells in the row
1196 if (!isMultiColumn(cell)) {
1197 for (row_type r = 0; r < nrows(); ++r) {
1198 // only if the column has no width the multirow inherits the
1199 // alignment of the column, otherwise it is left aligned
1200 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1201 && !isMultiColumn(cellIndex(r, col))) {
1202 cell_info[r][col].alignment = align;
1203 cell_info[r][col].inset->setContentAlignment(align);
1205 if ((isMultiRow(cellIndex(r, col)) && has_width)
1206 && !isMultiColumn(cellIndex(r, col))) {
1207 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1208 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1211 column_info[col].alignment = align;
1212 docstring & dpoint = column_info[col].decimal_point;
1213 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1214 dpoint = from_utf8(lyxrc.default_decimal_point);
1216 cellInfo(cell).alignment = align;
1217 cellInset(cell)->setContentAlignment(align);
1222 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1225 if (!isMultiColumn(cell) || onlycolumn)
1226 column_info[cellColumn(cell)].valignment = align;
1228 cellInfo(cell).valignment = align;
1235 * Allow line and paragraph breaks for fixed width multicol/multirow cells
1236 * or disallow them, merge cell paragraphs and reset layout to standard
1237 * for variable width multicol cells.
1239 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1240 bool const fixedWidth, bool const multicol,
1241 bool const multirow)
1243 inset->toggleFixedWidth(fixedWidth);
1244 if (!multirow && (fixedWidth || !multicol))
1247 // merge all paragraphs to one
1248 BufferParams const & bp = cur.bv().buffer().params();
1249 while (inset->paragraphs().size() > 1)
1250 mergeParagraph(bp, inset->paragraphs(), 0);
1252 // This is relevant for multirows
1257 ParagraphList::iterator pit = inset->paragraphs().begin();
1258 for (; pit != inset->paragraphs().end(); ++pit) {
1259 for (pos_type j = 0; j != pit->size(); ++j) {
1260 if (pit->isNewline(j))
1261 pit->eraseChar(j, bp.track_changes);
1267 // undo information has already been recorded
1268 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1269 bp.documentClass().plainLayoutName());
1276 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1277 Length const & width)
1279 col_type const c = cellColumn(cell);
1281 column_info[c].p_width = width;
1282 // reset the vertical alignment to top if the fixed width
1283 // is removed or zero because only fixed width columns can
1284 // have a vertical alignment
1285 if (column_info[c].p_width.zero())
1286 column_info[c].valignment = LYX_VALIGN_TOP;
1287 for (row_type r = 0; r < nrows(); ++r) {
1288 idx_type const cidx = cellIndex(r, c);
1289 // because of multicolumns
1290 toggleFixedWidth(cur, cellInset(cidx).get(),
1291 !getPWidth(cidx).zero(), isMultiColumn(cidx),
1293 if (isMultiRow(cidx))
1294 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1296 // cur can become invalid after paragraphs were merged
1301 bool Tabular::setFixedWidth(row_type r, col_type c)
1303 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1304 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1305 || (multicol && !cell_info[r][c].p_width.zero());
1306 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1311 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1312 Length const & width)
1314 if (!isMultiColumn(cell))
1317 cellInfo(cell).p_width = width;
1318 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1319 isMultiColumn(cell), isMultiRow(cell));
1320 // cur can become invalid after paragraphs were merged
1326 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1328 column_info[cellColumn(cell)].varwidth = varwidth;
1333 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1335 cellInfo(cell).mroffset = mroffset;
1340 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1341 Tabular::Feature what)
1343 if (what == SET_SPECIAL_MULTICOLUMN)
1344 cellInfo(cell).align_special = special;
1346 column_info[cellColumn(cell)].align_special = special;
1350 void Tabular::setTopLine(idx_type i, bool line)
1352 cellInfo(i).top_line = line;
1356 void Tabular::setBottomLine(idx_type i, bool line)
1358 cellInfo(i).bottom_line = line;
1362 void Tabular::setTopLineLTrim(idx_type i, bool val)
1364 cellInfo(i).top_line_ltrimmed = val;
1368 void Tabular::setTopLineRTrim(idx_type i, bool val)
1370 cellInfo(i).top_line_rtrimmed = val;
1374 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1376 cellInfo(i).bottom_line_ltrimmed = val;
1380 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1382 cellInfo(i).bottom_line_rtrimmed = val;
1386 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1388 setTopLineLTrim(i, trim.first);
1389 setTopLineRTrim(i, trim.second);
1392 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1394 setBottomLineLTrim(i, trim.first);
1395 setBottomLineRTrim(i, trim.second);
1398 void Tabular::setLeftLine(idx_type cell, bool line)
1400 cellInfo(cell).left_line = line;
1404 void Tabular::setRightLine(idx_type cell, bool line)
1406 cellInfo(cell).right_line = line;
1409 bool Tabular::rowTopLine(row_type r) const
1411 bool all_rows_set = true;
1412 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1413 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1414 return all_rows_set;
1418 bool Tabular::rowBottomLine(row_type r) const
1420 bool all_rows_set = true;
1421 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1422 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1423 return all_rows_set;
1427 bool Tabular::columnLeftLine(col_type c) const
1434 for (row_type r = 0; r < nrows(); ++r) {
1435 idx_type const i = cellIndex(r, c);
1436 if (c == cellColumn(i)) {
1438 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1439 if (cellInfo(i).left_line || right)
1443 return 2 * nrows_left >= total;
1447 bool Tabular::columnRightLine(col_type c) const
1452 int nrows_right = 0;
1454 for (row_type r = 0; r < nrows(); ++r) {
1455 idx_type i = cellIndex(r, c);
1456 if (c == cellColumn(i) + columnSpan(i) - 1) {
1458 bool left = (c + 1 < ncols()
1459 && cellInfo(cellIndex(r, c + 1)).left_line)
1460 || c + 1 == ncols();
1461 if (cellInfo(i).right_line && left)
1465 return 2 * nrows_right >= total;
1469 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1471 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1472 return cellInfo(cell).alignment;
1474 return column_info[cellColumn(cell)].alignment;
1479 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1481 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1482 return cellInfo(cell).valignment;
1483 return column_info[cellColumn(cell)].valignment;
1487 int Tabular::offsetVAlignment() const
1489 // for top-alignment the first horizontal table line must be exactly at
1490 // the position of the base line of the surrounding text line
1491 // for bottom alignment, the same is for the last table line
1492 int offset_valign = 0;
1493 switch (tabular_valignment) {
1494 case Tabular::LYX_VALIGN_BOTTOM:
1495 offset_valign = rowAscent(0) - height();
1497 case Tabular::LYX_VALIGN_MIDDLE:
1498 offset_valign = (- height()) / 2 + rowAscent(0);
1500 case Tabular::LYX_VALIGN_TOP:
1501 offset_valign = rowAscent(0);
1504 return offset_valign;
1508 Length const Tabular::getPWidth(idx_type cell) const
1510 if (isMultiColumn(cell))
1511 return cellInfo(cell).p_width;
1512 return column_info[cellColumn(cell)].p_width;
1516 Length const Tabular::getMROffset(idx_type cell) const
1518 return cellInfo(cell).mroffset;
1522 int Tabular::textHOffset(idx_type cell) const
1524 // the LaTeX Way :-(
1525 int x = WIDTH_OF_LINE;
1527 int const w = cellWidth(cell) - cellInfo(cell).width;
1529 switch (getAlignment(cell)) {
1530 case LYX_ALIGN_CENTER:
1533 case LYX_ALIGN_RIGHT:
1536 case LYX_ALIGN_DECIMAL: {
1537 // we center when no decimal point
1538 if (cellInfo(cell).decimal_width == 0) {
1542 col_type const c = cellColumn(cell);
1543 int max_dhoffset = 0;
1544 for(row_type r = 0; r < row_info.size() ; ++r) {
1545 idx_type const i = cellIndex(r, c);
1546 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1547 && cellInfo(i).decimal_width != 0)
1548 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1550 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1553 // LYX_ALIGN_LEFT: nothing :-)
1561 int Tabular::textVOffset(idx_type cell) const
1563 int voffset = cellInfo(cell).voffset;
1564 if (isMultiRow(cell)) {
1565 row_type const row = cellRow(cell);
1566 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1572 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1575 idx_type const numcells = numberOfCellsInRow(row);
1576 // we check against numcells to make sure we do not crash if all the
1577 // cells are multirow (bug #7535), but in that case our return value
1578 // is really invalid, i.e., it is NOT the first cell in the row. but
1579 // i do not know what to do here. (rgh)
1580 while (c < numcells - 1
1581 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1583 return cell_info[row][c].cellno;
1587 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1589 col_type c = ncols() - 1;
1590 // of course we check against 0 so we don't crash. but we have the same
1591 // problem as in the previous routine: if all the cells are part of a
1592 // multirow or part of a multi column, then our return value is invalid.
1594 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1595 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1597 return cell_info[row][c].cellno;
1601 Tabular::row_type Tabular::cellRow(idx_type cell) const
1603 if (cell >= numberofcells)
1607 return rowofcell[cell];
1611 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1613 if (cell >= numberofcells)
1617 return columnofcell[cell];
1621 void Tabular::write(ostream & os) const
1625 << write_attribute("version", 3)
1626 << write_attribute("rows", nrows())
1627 << write_attribute("columns", ncols())
1629 // global longtable options
1631 << write_attribute("rotate", rotate)
1632 << write_attribute("booktabs", use_booktabs)
1633 << write_attribute("islongtable", is_long_tabular)
1634 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1635 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1636 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1637 << write_attribute("headTopDL", endhead.topDL)
1638 << write_attribute("headBottomDL", endhead.bottomDL)
1639 << write_attribute("footTopDL", endfoot.topDL)
1640 << write_attribute("footBottomDL", endfoot.bottomDL)
1641 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1642 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1643 << write_attribute("lastFootEmpty", endlastfoot.empty);
1644 // longtables cannot be aligned vertically
1645 if (!is_long_tabular)
1646 os << write_attribute("tabularvalignment", tabular_valignment);
1647 os << write_attribute("tabularwidth", tabular_width);
1648 if (is_long_tabular)
1649 os << write_attribute("longtabularalignment", longtabular_alignment);
1651 for (col_type c = 0; c < ncols(); ++c) {
1653 << write_attribute("alignment", column_info[c].alignment);
1654 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1655 os << write_attribute("decimal_point", column_info[c].decimal_point);
1656 os << write_attribute("valignment", column_info[c].valignment)
1657 << write_attribute("width", column_info[c].p_width.asString())
1658 << write_attribute("varwidth", column_info[c].varwidth)
1659 << write_attribute("special", column_info[c].align_special)
1662 for (row_type r = 0; r < nrows(); ++r) {
1663 static const string def("default");
1665 if (row_info[r].top_space_default)
1666 os << write_attribute("topspace", def);
1668 os << write_attribute("topspace", row_info[r].top_space);
1669 if (row_info[r].bottom_space_default)
1670 os << write_attribute("bottomspace", def);
1672 os << write_attribute("bottomspace", row_info[r].bottom_space);
1673 if (row_info[r].interline_space_default)
1674 os << write_attribute("interlinespace", def);
1676 os << write_attribute("interlinespace", row_info[r].interline_space);
1677 os << write_attribute("endhead", row_info[r].endhead)
1678 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1679 << write_attribute("endfoot", row_info[r].endfoot)
1680 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1681 << write_attribute("newpage", row_info[r].newpage)
1682 << write_attribute("caption", row_info[r].caption)
1684 for (col_type c = 0; c < ncols(); ++c) {
1686 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1687 << write_attribute("multirow", cell_info[r][c].multirow)
1688 << write_attribute("mroffset", cell_info[r][c].mroffset)
1689 << write_attribute("alignment", cell_info[r][c].alignment)
1690 << write_attribute("valignment", cell_info[r][c].valignment)
1691 << write_attribute("topline", cell_info[r][c].top_line)
1692 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1693 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1694 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1695 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1696 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1697 << write_attribute("leftline", cell_info[r][c].left_line)
1698 << write_attribute("rightline", cell_info[r][c].right_line)
1699 << write_attribute("rotate", cell_info[r][c].rotate)
1700 << write_attribute("usebox", cell_info[r][c].usebox)
1701 << write_attribute("width", cell_info[r][c].p_width)
1702 << write_attribute("special", cell_info[r][c].align_special)
1704 os << "\\begin_inset ";
1705 cell_info[r][c].inset->write(os);
1706 os << "\n\\end_inset\n"
1708 // FIXME This can be removed again once the mystery
1709 // crash has been resolved.
1714 os << "</lyxtabular>\n";
1718 void Tabular::read(Lexer & lex)
1721 istream & is = lex.getStream();
1723 l_getline(is, line);
1724 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1725 LASSERT(false, return);
1729 if (!getTokenValue(line, "version", version))
1731 LATTEST(version >= 2);
1734 if (!getTokenValue(line, "rows", rows_arg))
1737 if (!getTokenValue(line, "columns", columns_arg))
1739 init(buffer_, rows_arg, columns_arg);
1740 l_getline(is, line);
1741 if (!prefixIs(line, "<features")) {
1742 lyxerr << "Wrong tabular format (expected <features ...> got"
1743 << line << ')' << endl;
1746 getTokenValue(line, "rotate", rotate);
1747 getTokenValue(line, "booktabs", use_booktabs);
1748 getTokenValue(line, "islongtable", is_long_tabular);
1749 getTokenValue(line, "tabularvalignment", tabular_valignment);
1750 getTokenValue(line, "tabularwidth", tabular_width);
1751 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1752 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1753 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1754 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1755 getTokenValue(line, "headTopDL", endhead.topDL);
1756 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1757 getTokenValue(line, "footTopDL", endfoot.topDL);
1758 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1759 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1760 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1761 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1763 for (col_type c = 0; c < ncols(); ++c) {
1765 if (!prefixIs(line,"<column")) {
1766 lyxerr << "Wrong tabular format (expected <column ...> got"
1767 << line << ')' << endl;
1770 getTokenValue(line, "alignment", column_info[c].alignment);
1771 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1772 getTokenValue(line, "valignment", column_info[c].valignment);
1773 getTokenValue(line, "width", column_info[c].p_width);
1774 getTokenValue(line, "special", column_info[c].align_special);
1775 getTokenValue(line, "varwidth", column_info[c].varwidth);
1778 for (row_type i = 0; i < nrows(); ++i) {
1779 l_getline(is, line);
1780 if (!prefixIs(line, "<row")) {
1781 lyxerr << "Wrong tabular format (expected <row ...> got"
1782 << line << ')' << endl;
1785 getTokenValue(line, "topspace", row_info[i].top_space,
1786 row_info[i].top_space_default);
1787 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1788 row_info[i].bottom_space_default);
1789 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1790 row_info[i].interline_space_default);
1791 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1792 getTokenValue(line, "endhead", row_info[i].endhead);
1793 getTokenValue(line, "endfoot", row_info[i].endfoot);
1794 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1795 getTokenValue(line, "newpage", row_info[i].newpage);
1796 getTokenValue(line, "caption", row_info[i].caption);
1797 for (col_type j = 0; j < ncols(); ++j) {
1798 l_getline(is, line);
1799 if (!prefixIs(line, "<cell")) {
1800 lyxerr << "Wrong tabular format (expected <cell ...> got"
1801 << line << ')' << endl;
1804 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1805 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1806 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1807 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1808 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1809 getTokenValue(line, "topline", cell_info[i][j].top_line);
1810 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1811 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1812 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1813 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1814 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1815 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1816 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1817 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1818 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1819 getTokenValue(line, "width", cell_info[i][j].p_width);
1821 getTokenValue(line, "special", cell_info[i][j].align_special);
1822 l_getline(is, line);
1823 if (prefixIs(line, "\\begin_inset")) {
1824 cell_info[i][j].inset->setBuffer(*buffer_);
1825 cell_info[i][j].inset->read(lex);
1826 l_getline(is, line);
1828 if (!prefixIs(line, "</cell>")) {
1829 lyxerr << "Wrong tabular format (expected </cell> got"
1830 << line << ')' << endl;
1834 l_getline(is, line);
1835 if (!prefixIs(line, "</row>")) {
1836 lyxerr << "Wrong tabular format (expected </row> got"
1837 << line << ')' << endl;
1841 while (!prefixIs(line, "</lyxtabular>")) {
1842 l_getline(is, line);
1848 bool Tabular::isMultiColumn(idx_type cell) const
1850 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1851 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1855 bool Tabular::hasMultiColumn(col_type c) const
1857 for (row_type r = 0; r < nrows(); ++r) {
1858 if (isMultiColumn(cellIndex(r, c)))
1865 bool Tabular::hasVarwidthColumn() const
1867 for (col_type c = 0; c < ncols(); ++c) {
1868 if (column_info[c].varwidth)
1875 bool Tabular::isVTypeColumn(col_type c) const
1877 for (row_type r = 0; r < nrows(); ++r) {
1878 idx_type idx = cellIndex(r, c);
1879 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1886 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1888 return cell_info[cellRow(cell)][cellColumn(cell)];
1892 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1894 return cell_info[cellRow(cell)][cellColumn(cell)];
1898 Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
1899 bool const right_border)
1901 idx_type const col = cellColumn(cell);
1902 idx_type const row = cellRow(cell);
1903 for (idx_type i = 0; i < number; ++i)
1904 unsetMultiRow(cellIndex(row, col + i));
1906 // unsetting of multirow may have invalidated cell index
1907 cell = cellIndex(row, col);
1908 CellData & cs = cellInfo(cell);
1909 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1910 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1911 cs.alignment = column_info[col].alignment;
1912 setRightLine(cell, right_border);
1913 // non-fixed width multicolumns cannot have multiple paragraphs
1914 if (getPWidth(cell).zero()) {
1915 toggleFixedWidth(cur, cellInset(cell).get(),
1916 !getPWidth(cell).zero(), isMultiColumn(cell),
1918 // cur can become invalid after paragraphs were merged
1922 idx_type lastcell = cellIndex(row, col + number - 1);
1923 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1924 CellData & cs1 = cellInfo(cell + i);
1925 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1926 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1934 bool Tabular::isMultiRow(idx_type cell) const
1936 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1937 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1940 bool Tabular::hasMultiRow(row_type r) const
1942 for (col_type c = 0; c < ncols(); ++c) {
1943 if (isMultiRow(cellIndex(r, c)))
1949 Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
1950 bool const bottom_border,
1951 LyXAlignment const halign)
1953 idx_type const col = cellColumn(cell);
1954 idx_type const row = cellRow(cell);
1955 for (idx_type i = 0; i < number; ++i)
1956 unsetMultiColumn(cellIndex(row + i, col));
1958 // unsetting of multirow may have invalidated cell index
1959 cell = cellIndex(row, col);
1960 CellData & cs = cellInfo(cell);
1961 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1962 cs.valignment = LYX_VALIGN_MIDDLE;
1963 // the horizontal alignment of multirow cells can only
1964 // be changed for the whole table row,
1965 // support changing this only for the multirow cell can be done via
1967 if (getPWidth(cell).zero())
1968 cs.alignment = halign;
1970 cs.alignment = LYX_ALIGN_LEFT;
1972 // Multirows cannot have multiple paragraphs
1973 if (getPWidth(cell).zero()) {
1974 toggleFixedWidth(cur, cellInset(cell).get(),
1975 !getPWidth(cell).zero(),
1976 isMultiColumn(cell), isMultiRow(cell));
1977 // cur can become invalid after paragraphs were merged
1981 // set the bottom line of the last selected cell
1982 setBottomLine(cell, bottom_border);
1984 for (idx_type i = 1; i < number; ++i) {
1985 CellData & cs1 = cell_info[row + i][col];
1986 cs1.multirow = CELL_PART_OF_MULTIROW;
1987 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1995 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1997 row_type const row = cellRow(cell);
1998 col_type const col = cellColumn(cell);
2000 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2007 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
2009 col_type const column = cellColumn(cell);
2010 col_type row = cellRow(cell) + 1;
2011 while (row < nrows() && isPartOfMultiRow(row, column))
2014 return row - cellRow(cell);
2018 void Tabular::unsetMultiColumn(idx_type cell)
2020 if (!isMultiColumn(cell))
2023 row_type const row = cellRow(cell);
2024 col_type const col = cellColumn(cell);
2025 row_type const span = columnSpan(cell);
2026 for (col_type c = 0; c < span; ++c) {
2027 // in the table dialog the lines are set in every case
2028 // when unsetting a multicolumn this leads to an additional right
2029 // line for every cell that was part of the former multicolumn cell,
2030 // except if the cell is in the last column
2031 // therefore remove this line
2032 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2033 && (col + c) < (col + span - 1))
2034 cell_info[row][col + c].right_line = false;
2035 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2041 void Tabular::unsetMultiRow(idx_type cell)
2043 if (!isMultiRow(cell))
2046 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2047 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2048 row_type const row = cellRow(cell);
2049 col_type const col = cellColumn(cell);
2050 row_type const span = rowSpan(cell);
2051 for (row_type r = 0; r < span; ++r)
2052 cell_info[row + r][col].multirow = CELL_NORMAL;
2057 void Tabular::setRotateCell(idx_type cell, int value)
2059 cellInfo(cell).rotate = value;
2063 int Tabular::getRotateCell(idx_type cell) const
2065 return cellInfo(cell).rotate;
2069 bool Tabular::needRotating() const
2071 if (rotate && !is_long_tabular)
2073 for (row_type r = 0; r < nrows(); ++r)
2074 for (col_type c = 0; c < ncols(); ++c)
2075 if (cell_info[r][c].rotate != 0)
2081 bool Tabular::isLastCell(idx_type cell) const
2083 if (cell + 1 < numberofcells)
2089 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2091 if (cellRow(cell) == 0)
2094 col_type const col = cellColumn(cell);
2095 row_type r = cellRow(cell) - 1;
2096 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2099 return cell_info[r][col].cellno;
2103 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2105 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2106 if (nextrow < nrows())
2107 return cell_info[nextrow][cellColumn(cell)].cellno;
2112 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2114 LASSERT(column != npos && column < ncols(), column = 0);
2115 LASSERT(row != npos && row < nrows(), row = 0);
2116 return cell_info[row][column].cellno;
2120 void Tabular::setUsebox(idx_type cell, BoxType type)
2122 cellInfo(cell).usebox = type;
2126 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2128 if (getRotateCell(cell) == 0
2129 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2130 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2132 if (cellInfo(cell).usebox > 1)
2133 return cellInfo(cell).usebox;
2134 return useBox(cell);
2139 // This are functions used for the longtable support
2141 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2147 row_info[row].endfirsthead = flag;
2151 row_info[row].endhead = flag;
2156 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2159 hd.set = haveLTHead();
2160 return row_info[row].endhead;
2164 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2167 hd.set = haveLTFirstHead();
2168 return row_info[row].endfirsthead;
2172 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2178 row_info[row].endlastfoot = flag;
2182 row_info[row].endfoot = flag;
2187 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2190 fd.set = haveLTFoot();
2191 return row_info[row].endfoot;
2195 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2198 fd.set = haveLTLastFoot();
2199 return row_info[row].endlastfoot;
2203 void Tabular::setLTNewPage(row_type row, bool what)
2205 row_info[row].newpage = what;
2209 bool Tabular::getLTNewPage(row_type row) const
2211 return row_info[row].newpage;
2215 bool Tabular::haveLTHead(bool withcaptions) const
2217 if (!is_long_tabular)
2219 for (row_type i = 0; i < nrows(); ++i)
2220 if (row_info[i].endhead &&
2221 (withcaptions || !row_info[i].caption))
2227 bool Tabular::haveLTFirstHead(bool withcaptions) const
2229 if (!is_long_tabular || endfirsthead.empty)
2231 for (row_type r = 0; r < nrows(); ++r)
2232 if (row_info[r].endfirsthead &&
2233 (withcaptions || !row_info[r].caption))
2239 bool Tabular::haveLTFoot(bool withcaptions) const
2241 if (!is_long_tabular)
2243 for (row_type r = 0; r < nrows(); ++r)
2244 if (row_info[r].endfoot &&
2245 (withcaptions || !row_info[r].caption))
2251 bool Tabular::haveLTLastFoot(bool withcaptions) const
2253 if (!is_long_tabular || endlastfoot.empty)
2255 for (row_type r = 0; r < nrows(); ++r)
2256 if (row_info[r].endlastfoot &&
2257 (withcaptions || !row_info[r].caption))
2263 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2265 idx_type i = getFirstCellInRow(row);
2267 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2268 setTopLine(i, false);
2269 setBottomLine(i, false);
2270 setLeftLine(i, false);
2271 setRightLine(i, false);
2272 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2273 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2274 setLTHead(row, true, endfirsthead, true);
2275 row_info[row].endfirsthead = true;
2278 unsetMultiColumn(i);
2279 // When unsetting a caption row, also all existing
2280 // captions in this row must be dissolved.
2282 row_info[row].caption = what;
2287 bool Tabular::ltCaption(row_type row) const
2289 return row_info[row].caption;
2293 bool Tabular::haveLTCaption(CaptionType captiontype) const
2295 if (!is_long_tabular)
2297 for (row_type r = 0; r < nrows(); ++r) {
2298 if (row_info[r].caption) {
2299 switch (captiontype) {
2300 case CAPTION_FIRSTHEAD:
2301 if (row_info[r].endfirsthead)
2305 if (row_info[r].endhead)
2309 if (row_info[r].endfoot)
2312 case CAPTION_LASTFOOT:
2313 if (row_info[r].endlastfoot)
2325 // end longtable support functions
2327 void Tabular::setRowAscent(row_type row, int height)
2329 if (row >= nrows() || row_info[row].ascent == height)
2331 row_info[row].ascent = height;
2335 void Tabular::setRowDescent(row_type row, int height)
2337 if (row >= nrows() || row_info[row].descent == height)
2339 row_info[row].descent = height;
2343 int Tabular::rowAscent(row_type row) const
2345 LASSERT(row < nrows(), row = 0);
2346 return row_info[row].ascent;
2350 int Tabular::rowDescent(row_type row) const
2352 LASSERT(row < nrows(), row = 0);
2353 return row_info[row].descent;
2357 int Tabular::height() const
2360 for (row_type row = 0; row < nrows(); ++row)
2361 height += rowAscent(row) + rowDescent(row) +
2367 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2369 LASSERT(row < nrows(), return false);
2370 LASSERT(column < ncols(), return false);
2371 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2375 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2377 LASSERT(row < nrows(), return false);
2378 LASSERT(column < ncols(), return false);
2379 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2383 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang,
2384 list<col_type> columns) const
2386 // we only output complete row lines and the 1st row here, the rest
2387 // is done in Tabular::TeXBottomHLine(...)
2389 // get for each column the topline (if any)
2390 map<col_type, bool> topline, topltrims, toprtrims;
2392 bool have_trims = false;
2393 for (auto const & c : columns) {
2394 topline[c] = topLine(cellIndex(row, c));
2395 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2396 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2397 // If cell is part of a multirow and not the first cell of the
2398 // multirow, no line must be drawn.
2400 if (isMultiRow(cellIndex(row, c))
2401 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2403 topltrims[c] = false;
2404 toprtrims[c] = false;
2406 // copy trimming to multicolumn parts
2407 if (isPartOfMultiColumn(row, c)) {
2408 topltrims[c] = topltrims[c-1];
2409 toprtrims[c] = toprtrims[c-1];
2411 if (topline.find(c) != topline.end() && topline.find(c)->second)
2413 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2414 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2418 // do nothing if empty first row, or incomplete row line after
2419 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2422 // Is this the actual first row (excluding longtable caption row)?
2423 bool const realfirstrow = (row == 0
2424 || (is_long_tabular && row == 1 && ltCaption(0)));
2426 // only output complete row lines and the 1st row's clines
2427 if (nset == ncols() && !have_trims) {
2429 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2433 } else if (realfirstrow || have_trims) {
2434 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2435 for (auto & c : columns) {
2436 if (topline.find(c)->second) {
2437 col_type offset = 0;
2438 for (col_type j = 0 ; j < c; ++j)
2439 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2441 while (isPartOfMultiColumn(row, c))
2444 if (topltrims.find(c) != topltrims.end()
2445 && topltrims.find(c)->second)
2447 string const firstcol = convert<string>(c + 1 + offset);
2448 col_type cstart = c;
2449 for ( ; c < ncols() - 1 && topline.find(c)->second ; ++c) {
2450 if (isMultiColumn(cellIndex(row, c))
2451 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2453 if (c > cstart && topltrims.find(c) != topltrims.end()
2454 && topltrims.find(c)->second) {
2455 if (!isPartOfMultiColumn(row, c))
2458 } else if (toprtrims.find(c) != toprtrims.end()
2459 && toprtrims.find(c)->second)
2463 for (col_type j = cstart ; j < c ; ++j)
2464 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2466 col_type const lastcol = c + 1 + offset;
2467 if (toprtrims.find(c) != toprtrims.end()
2468 && toprtrims.find(c)->second)
2471 //babel makes the "-" character an active one, so we have to suppress this here
2472 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2473 if (lang == "slovak" || lang == "czech") {
2474 os << "\\expandafter" << cline;
2476 os << "(" << trim << ")";
2477 os << "\\expandafter{\\expandafter" << firstcol << "\\string-";
2481 os << "(" << trim << ")";
2482 os << "{" << firstcol << '-';
2484 os << lastcol << "}";
2485 if (c == ncols() - 1)
2494 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang,
2495 list<col_type> columns) const
2497 // we output bottomlines of row r and the toplines of row r+1
2498 // if the latter do not span the whole tabular
2500 // get the bottomlines of row r, and toplines in next row
2501 bool lastrow = row == nrows() - 1;
2502 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2503 bool nextrowset = true;
2504 for (auto const & c : columns) {
2505 idx_type const idx = cellIndex(row, c);
2506 bottomline[c] = bottomLine(cellIndex(row, c));
2507 bottomltrims[c] = bottomLineTrim(idx).first;
2508 bottomrtrims[c] = bottomLineTrim(idx).second;
2509 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2510 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2511 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2512 // If cell is part of a multirow and not the last cell of the
2513 // multirow, no line must be drawn.
2515 if (isMultiRow(cellIndex(row, c))
2516 && isMultiRow(cellIndex(row + 1, c))
2517 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2518 bottomline[c] = false;
2520 bottomltrims[c] = false;
2521 bottomrtrims[c] = false;
2522 topltrims[c] = false;
2523 toprtrims[c] = false;
2525 // copy trimming in multicolumn parts
2526 if (isPartOfMultiColumn(row, c)) {
2527 topltrims[c] = topltrims[c-1];
2528 toprtrims[c] = toprtrims[c-1];
2529 bottomltrims[c] = bottomltrims[c-1];
2530 bottomrtrims[c] = bottomrtrims[c-1];
2533 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2536 // combine this row's bottom lines and next row's toplines if necessary
2538 bool have_trims = false;
2539 for (auto const & c : columns) {
2541 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2542 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2543 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2544 bottomrtrims[c] =(bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2545 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2546 if (bottomline.find(c)->second)
2548 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2549 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2553 // do nothing if empty, OR incomplete row line with a topline in next row
2554 if (nset == 0 || (nextrowset && nset != ncols()))
2557 if (nset == ncols() && !have_trims) {
2559 os << (lastrow ? "\\bottomrule" : "\\midrule");
2563 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2564 for (auto & c : columns) {
2565 if (bottomline.find(c)->second) {
2566 col_type offset = 0;
2567 for (col_type j = 0 ; j < c; ++j)
2568 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2570 while (isPartOfMultiColumn(row, c))
2573 if (bottomltrims.find(c) != bottomltrims.end()
2574 && bottomltrims.find(c)->second)
2576 string const firstcol = convert<string>(c + 1 + offset);
2577 col_type cstart = c;
2578 for ( ; c < ncols() - 1 && bottomline.find(c)->second ; ++c) {
2579 if (isMultiColumn(cellIndex(row, c))
2581 && isPartOfMultiColumn(row, c + 1))
2583 if (c > cstart && bottomltrims.find(c) != bottomltrims.end()
2584 && bottomltrims.find(c)->second) {
2585 if (!isPartOfMultiColumn(row, c))
2588 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2589 && bottomrtrims.find(c)->second)
2593 for (col_type j = cstart ; j < c ; ++j)
2594 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2596 col_type const lastcol = c + 1 + offset;
2597 if (bottomrtrims.find(c) != bottomrtrims.end()
2598 && bottomrtrims.find(c)->second)
2601 //babel makes the "-" character an active one, so we have to suppress this here
2602 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2603 if (lang == "slovak" || lang == "czech") {
2604 os << "\\expandafter" << cline;
2606 os << "(" << trim << ")";
2607 os << "\\expandafter{\\expandafter" << firstcol << "\\string-";
2611 os << "(" << trim << ")";
2612 os << "{" << firstcol << '-';
2614 os << lastcol << "}";
2615 if (c == ncols() - 1)
2624 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2625 bool & ismulticol, bool & ismultirow,
2626 bool const bidi) const
2628 row_type const r = cellRow(cell);
2629 if (is_long_tabular && row_info[r].caption)
2632 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2633 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2634 // figure out how to set the lines
2635 // we always set double lines to the right of the cell
2636 // or left in bidi RTL, respectively.
2637 col_type const c = cellColumn(cell);
2638 col_type const nextcol = c + columnSpan(cell);
2639 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2640 bool colright = columnRightLine(c);
2641 bool colleft = columnLeftLine(c);
2642 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2643 bool nextcellleft = nextcol < ncols()
2644 && leftLine(cellIndex(r, nextcol));
2645 bool coldouble = colright && nextcolleft;
2646 bool celldouble = rightLine(cell) && nextcellleft;
2648 ismulticol = (isMultiColumn(cell)
2649 || (c == 0 && colleft != leftLine(cell))
2650 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2651 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2652 || (coldouble != celldouble))
2655 // we center in multicol when no decimal point
2657 docstring const align_d = column_info[c].decimal_point;
2658 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2659 bool const nosep = !dit;
2660 ismulticol |= nosep;
2661 celldouble &= nosep;
2664 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2665 int latexcolspan = columnSpan(cell);
2666 for (col_type col = c; col < c + columnSpan(cell); ++col)
2667 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2671 os << "\\multicolumn{" << latexcolspan << "}{";
2672 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2673 || (!bidi && c == 0 && leftLine(cell))))
2675 if (bidi && celldouble)
2676 // add extra vertical line if we want a double one
2678 if (!cellInfo(cell).align_special.empty()) {
2679 os << cellInfo(cell).align_special;
2681 if (!getPWidth(cell).zero()) {
2683 case LYX_ALIGN_LEFT:
2684 os << ">{\\raggedright}";
2686 case LYX_ALIGN_RIGHT:
2687 os << ">{\\raggedleft}";
2689 case LYX_ALIGN_CENTER:
2690 os << ">{\\centering}";
2696 case LYX_VALIGN_TOP:
2699 case LYX_VALIGN_MIDDLE:
2702 case LYX_VALIGN_BOTTOM:
2707 << from_ascii(getPWidth(cell).asLatexString())
2711 case LYX_ALIGN_LEFT:
2714 case LYX_ALIGN_RIGHT:
2721 } // end if else !getPWidth
2722 } // end if else !cellinfo_of_cell
2723 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2725 if (!bidi && celldouble)
2726 // add extra vertical line if we want a double one
2729 } // end if ismulticol
2731 // we only need code for the first multirow cell
2732 ismultirow = isMultiRow(cell);
2734 os << "\\multirow{" << rowSpan(cell) << "}{";
2735 if (!getPWidth(cell).zero())
2736 os << from_ascii(getPWidth(cell).asLatexString());
2738 // we need to set a default value
2741 if (!getMROffset(cell).zero())
2742 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2744 } // end if ismultirow
2746 if (getRotateCell(cell) != 0)
2747 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2749 if (getUsebox(cell) == BOX_PARBOX) {
2752 case LYX_VALIGN_TOP:
2755 case LYX_VALIGN_MIDDLE:
2758 case LYX_VALIGN_BOTTOM:
2762 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2764 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2765 os << "\\begin{minipage}[";
2767 case LYX_VALIGN_TOP:
2770 case LYX_VALIGN_MIDDLE:
2773 case LYX_VALIGN_BOTTOM:
2777 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2779 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2780 os << "\\begin{varwidth}[";
2782 case LYX_VALIGN_TOP:
2785 case LYX_VALIGN_MIDDLE:
2788 case LYX_VALIGN_BOTTOM:
2792 os << "]{\\linewidth}\n";
2797 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2798 bool ismulticol, bool ismultirow) const
2800 row_type const r = cellRow(cell);
2801 if (is_long_tabular && row_info[r].caption)
2805 if (getUsebox(cell) == BOX_PARBOX)
2807 else if (getUsebox(cell) == BOX_MINIPAGE)
2808 os << breakln << "\\end{minipage}";
2809 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2810 os << breakln << "\\end{varwidth}";
2811 if (getRotateCell(cell) != 0)
2812 os << breakln << "\\end{turn}";
2820 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2821 OutputParams const & runparams,
2822 list<col_type> columns) const
2824 if (!is_long_tabular)
2828 // output caption which is in no header or footer
2829 if (haveLTCaption()) {
2830 for (row_type r = 0; r < nrows(); ++r) {
2831 if (row_info[r].caption &&
2832 !row_info[r].endfirsthead && !row_info[r].endhead &&
2833 !row_info[r].endfoot && !row_info[r].endlastfoot)
2834 TeXRow(os, r, runparams, columns);
2837 // output first header info
2838 if (haveLTFirstHead()) {
2839 if (endfirsthead.topDL)
2841 for (row_type r = 0; r < nrows(); ++r) {
2842 if (row_info[r].endfirsthead)
2843 TeXRow(os, r, runparams, columns);
2845 if (endfirsthead.bottomDL)
2847 os << "\\endfirsthead\n";
2849 // output header info
2851 if (endfirsthead.empty && !haveLTFirstHead())
2852 os << "\\endfirsthead\n";
2855 for (row_type r = 0; r < nrows(); ++r) {
2856 if (row_info[r].endhead)
2857 TeXRow(os, r, runparams, columns);
2859 if (endhead.bottomDL)
2861 os << "\\endhead\n";
2863 // output footer info
2867 for (row_type r = 0; r < nrows(); ++r) {
2868 if (row_info[r].endfoot)
2869 TeXRow(os, r, runparams, columns);
2871 if (endfoot.bottomDL)
2873 os << "\\endfoot\n";
2874 if (endlastfoot.empty && !haveLTLastFoot())
2875 os << "\\endlastfoot\n";
2877 // output lastfooter info
2878 if (haveLTLastFoot()) {
2879 if (endlastfoot.topDL)
2881 for (row_type r = 0; r < nrows(); ++r) {
2882 if (row_info[r].endlastfoot)
2883 TeXRow(os, r, runparams, columns);
2885 if (endlastfoot.bottomDL)
2887 os << "\\endlastfoot\n";
2892 bool Tabular::isValidRow(row_type row) const
2894 if (!is_long_tabular)
2896 return !row_info[row].endhead && !row_info[row].endfirsthead
2897 && !row_info[row].endfoot && !row_info[row].endlastfoot
2898 && !row_info[row].caption;
2902 void Tabular::TeXRow(otexstream & os, row_type row,
2903 OutputParams const & runparams,
2904 list<col_type> columns) const
2906 idx_type cell = cellIndex(row, 0);
2907 InsetTableCell const * cinset = cellInset(cell);
2908 Paragraph const & cpar = cinset->paragraphs().front();
2909 string const clang = cpar.getParLanguage(buffer().params())->lang();
2911 //output the top line
2912 TeXTopHLine(os, row, clang, columns);
2914 if (row_info[row].top_space_default) {
2916 os << "\\addlinespace\n";
2918 os << "\\noalign{\\vskip\\doublerulesep}\n";
2919 } else if(!row_info[row].top_space.zero()) {
2921 os << "\\addlinespace["
2922 << from_ascii(row_info[row].top_space.asLatexString())
2925 os << "\\noalign{\\vskip"
2926 << from_ascii(row_info[row].top_space.asLatexString())
2930 bool ismulticol = false;
2931 bool ismultirow = false;
2933 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
2934 // Luabibdi (used by LuaTeX) behaves like classic
2935 bool const bidi_rtl =
2936 runparams.local_font->isRightToLeft()
2937 && runparams.useBidiPackage();
2939 bidi_rtl ? getFirstCellInRow(row) : getLastCellInRow(row);
2941 for (auto const & c : columns) {
2942 if (isPartOfMultiColumn(row, c))
2945 cell = cellIndex(row, c);
2947 if (isPartOfMultiRow(row, c)
2948 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2949 if (cell != lastcell)
2954 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
2955 InsetTableCell const * inset = cellInset(cell);
2957 Paragraph const & par = inset->paragraphs().front();
2959 os.texrow().forceStart(par.id(), 0);
2961 bool rtl = par.isRTL(buffer().params())
2963 && getPWidth(cell).zero()
2964 && !runparams.use_polyglossia;
2968 par.getParLanguage(buffer().params())->lang();
2969 if (lang == "farsi")
2971 else if (lang == "arabic_arabi")
2973 // currently, remaining RTL languages are
2974 // arabic_arabtex and hebrew
2978 // pass to the OutputParams that we are in a cell and
2979 // which alignment we have set.
2980 // InsetNewline needs this context information.
2981 OutputParams newrp(runparams);
2982 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2983 ? OutputParams::PLAIN
2984 : OutputParams::ALIGNED;
2986 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2987 // copy cell and split in 2
2988 InsetTableCell head = InsetTableCell(*cellInset(cell));
2989 head.setBuffer(const_cast<Buffer &>(buffer()));
2990 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2992 dit.push_back(CursorSlice(head));
2993 head.setMacrocontextPositionRecursive(dit);
2994 bool hassep = false;
2995 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2997 tail.setBuffer(head.buffer());
2999 dit.push_back(CursorSlice(tail));
3000 tail.setMacrocontextPositionRecursive(dit);
3004 tail.latex(os, newrp);
3007 head.latex(os, newrp);
3009 head.latex(os, newrp);
3012 tail.latex(os, newrp);
3015 } else if (ltCaption(row)) {
3016 // Inside longtable caption rows, we must only output the caption inset
3017 // with its content and omit anything outside of that (see #10791)
3018 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
3019 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
3020 for (; it != i_end; ++it) {
3021 if (it->lyxCode() != CAPTION_CODE)
3023 it->latex(os, runparams);
3026 } else if (!isPartOfMultiRow(row, c)) {
3027 if (!runparams.nice)
3028 os.texrow().start(par.id(), 0);
3029 inset->latex(os, newrp);
3032 runparams.encoding = newrp.encoding;
3036 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3037 if (cell != lastcell) { // not last cell in row
3044 os << "\\tabularnewline";
3045 if (row_info[row].bottom_space_default) {
3047 os << "\\addlinespace";
3049 os << "[\\doublerulesep]";
3050 } else if (!row_info[row].bottom_space.zero()) {
3052 os << "\\addlinespace";
3054 << from_ascii(row_info[row].bottom_space.asLatexString())
3059 //output the bottom line
3060 TeXBottomHLine(os, row, clang, columns);
3062 if (row_info[row].interline_space_default) {
3064 os << "\\addlinespace\n";
3066 os << "\\noalign{\\vskip\\doublerulesep}\n";
3067 } else if (!row_info[row].interline_space.zero()) {
3069 os << "\\addlinespace["
3070 << from_ascii(row_info[row].interline_space.asLatexString())
3073 os << "\\noalign{\\vskip"
3074 << from_ascii(row_info[row].interline_space.asLatexString())
3080 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3082 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3083 && !hasVarwidthColumn();
3084 bool const is_xltabular = is_long_tabular
3085 && (hasVarwidthColumn() || !tabular_width.zero());
3086 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3088 //+---------------------------------------------------------------------
3089 //+ first the opening preamble +
3090 //+---------------------------------------------------------------------
3093 if (!TexRow::isNone(pos))
3094 os.texrow().start(pos);
3097 if (is_long_tabular)
3098 os << "\\begin{landscape}\n";
3100 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3103 if (is_long_tabular) {
3105 os << "\\begin{xltabular}";
3107 os << "\\begin{longtable}";
3108 switch (longtabular_alignment) {
3109 case LYX_LONGTABULAR_ALIGN_LEFT:
3112 case LYX_LONGTABULAR_ALIGN_CENTER:
3115 case LYX_LONGTABULAR_ALIGN_RIGHT:
3120 if (tabular_width.zero())
3121 os << "{" << from_ascii("\\columnwidth") << "}";
3123 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3126 if (is_tabular_star)
3127 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3128 else if (hasVarwidthColumn()) {
3129 os << "\\begin{tabularx}{";
3130 if (tabular_width.zero())
3131 os << from_ascii("\\columnwidth") << "}";
3133 os << from_ascii(tabular_width.asLatexString()) << "}";
3135 os << "\\begin{tabular}";
3136 switch (tabular_valignment) {
3137 case LYX_VALIGN_TOP:
3140 case LYX_VALIGN_MIDDLE:
3142 case LYX_VALIGN_BOTTOM:
3150 if (is_tabular_star)
3151 os << "@{\\extracolsep{\\fill}}";
3153 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3154 // order for RTL (#9686). Thus we use this list.
3155 bool const bidi_rtl =
3156 runparams.local_font->isRightToLeft()
3157 && runparams.useBidiPackage();
3158 list<col_type> columns;
3159 for (col_type cl = 0; cl < ncols(); ++cl) {
3161 columns.push_front(cl);
3163 columns.push_back(cl);
3166 for (auto const & c : columns) {
3167 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3169 if (!column_info[c].align_special.empty()) {
3170 os << column_info[c].align_special;
3172 if (!column_info[c].p_width.zero()) {
3173 bool decimal = false;
3174 switch (column_info[c].alignment) {
3175 case LYX_ALIGN_LEFT:
3176 os << ">{\\raggedright}";
3178 case LYX_ALIGN_RIGHT:
3179 os << ">{\\raggedleft}";
3181 case LYX_ALIGN_CENTER:
3182 os << ">{\\centering}";
3184 case LYX_ALIGN_NONE:
3185 case LYX_ALIGN_BLOCK:
3186 case LYX_ALIGN_LAYOUT:
3187 case LYX_ALIGN_SPECIAL:
3189 case LYX_ALIGN_DECIMAL: {
3191 os << ">{\\raggedright}";
3193 os << ">{\\raggedleft}";
3200 switch (column_info[c].valignment) {
3201 case LYX_VALIGN_TOP:
3202 // this is the default
3204 case LYX_VALIGN_MIDDLE:
3207 case LYX_VALIGN_BOTTOM:
3213 // Fixed-width cells with alignment at decimal separator
3214 // are output as two cells of half the width with the decimal
3215 // separator as column sep. This effectively puts the content
3216 // centered, which differs from the normal decimal sep alignment
3217 // and is not ideal, but we cannot do better ATM (see #9568).
3218 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3220 docstring const halffixedwith =
3221 from_ascii(Length(column_info[c].p_width.value() / 2,
3222 column_info[c].p_width.unit()).asLatexString());
3226 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3233 << from_ascii(column_info[c].p_width.asLatexString())
3235 } else if (column_info[c].varwidth) {
3236 switch (column_info[c].alignment) {
3237 case LYX_ALIGN_LEFT:
3238 os << ">{\\raggedright\\arraybackslash}";
3240 case LYX_ALIGN_RIGHT:
3241 os << ">{\\raggedleft\\arraybackslash}";
3243 case LYX_ALIGN_CENTER:
3244 os << ">{\\centering\\arraybackslash}";
3246 case LYX_ALIGN_NONE:
3247 case LYX_ALIGN_BLOCK:
3248 case LYX_ALIGN_LAYOUT:
3249 case LYX_ALIGN_SPECIAL:
3250 case LYX_ALIGN_DECIMAL:
3254 } else if (isVTypeColumn(c)) {
3255 switch (column_info[c].alignment) {
3256 case LYX_ALIGN_LEFT:
3257 os << ">{\\raggedright}";
3259 case LYX_ALIGN_RIGHT:
3260 os << ">{\\raggedleft}";
3262 case LYX_ALIGN_CENTER:
3263 os << ">{\\centering}";
3265 case LYX_ALIGN_NONE:
3266 case LYX_ALIGN_BLOCK:
3267 case LYX_ALIGN_LAYOUT:
3268 case LYX_ALIGN_SPECIAL:
3269 case LYX_ALIGN_DECIMAL:
3272 os << "V{\\linewidth}";
3274 switch (column_info[c].alignment) {
3275 case LYX_ALIGN_LEFT:
3278 case LYX_ALIGN_RIGHT:
3281 case LYX_ALIGN_DECIMAL:
3282 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3288 } // end if else !column_info[i].p_width
3289 } // end if else !column_info[i].align_special
3290 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3295 TeXLongtableHeaderFooter(os, runparams, columns);
3297 //+---------------------------------------------------------------------
3298 //+ the single row and columns (cells) +
3299 //+---------------------------------------------------------------------
3301 for (row_type r = 0; r < nrows(); ++r) {
3302 if (isValidRow(r)) {
3303 TeXRow(os, r, runparams, columns);
3304 if (is_long_tabular && row_info[r].newpage)
3305 os << "\\newpage\n";
3309 //+---------------------------------------------------------------------
3310 //+ the closing of the tabular +
3311 //+---------------------------------------------------------------------
3313 if (is_long_tabular) {
3315 os << "\\end{xltabular}";
3317 os << "\\end{longtable}";
3319 if (is_tabular_star)
3320 os << "\\end{tabular*}";
3321 else if (hasVarwidthColumn())
3322 os << "\\end{tabularx}";
3324 os << "\\end{tabular}";
3328 if (is_long_tabular)
3329 os << breakln << "\\end{landscape}";
3331 os << breakln << "\\end{turn}";
3334 if (!TexRow::isNone(pos))
3335 os.texrow().start(pos);
3339 int Tabular::docbookRow(odocstream & os, row_type row,
3340 OutputParams const & runparams) const
3343 idx_type cell = getFirstCellInRow(row);
3346 for (col_type c = 0; c < ncols(); ++c) {
3347 if (isPartOfMultiColumn(row, c))
3350 os << "<entry align=\"";
3351 switch (getAlignment(cell)) {
3352 case LYX_ALIGN_LEFT:
3355 case LYX_ALIGN_RIGHT:
3363 os << "\" valign=\"";
3364 switch (getVAlignment(cell)) {
3365 case LYX_VALIGN_TOP:
3368 case LYX_VALIGN_BOTTOM:
3371 case LYX_VALIGN_MIDDLE:
3376 if (isMultiColumn(cell)) {
3377 os << " namest=\"col" << c << "\" ";
3378 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3382 ret += cellInset(cell)->docbook(os, runparams);
3391 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3395 //+---------------------------------------------------------------------
3396 //+ first the opening preamble +
3397 //+---------------------------------------------------------------------
3399 os << "<tgroup cols=\"" << ncols()
3400 << "\" colsep=\"1\" rowsep=\"1\">\n";
3402 for (col_type c = 0; c < ncols(); ++c) {
3403 os << "<colspec colname=\"col" << c << "\" align=\"";
3404 switch (column_info[c].alignment) {
3405 case LYX_ALIGN_LEFT:
3408 case LYX_ALIGN_RIGHT:
3416 if (runparams.flavor == OutputParams::XML)
3422 //+---------------------------------------------------------------------
3423 //+ Long Tabular case +
3424 //+---------------------------------------------------------------------
3426 // output caption info
3427 // The caption flag wins over head/foot
3428 if (haveLTCaption()) {
3429 os << "<caption>\n";
3431 for (row_type r = 0; r < nrows(); ++r) {
3432 if (row_info[r].caption) {
3433 ret += docbookRow(os, r, runparams);
3436 os << "</caption>\n";
3439 // output header info
3440 if (haveLTHead(false) || haveLTFirstHead(false)) {
3443 for (row_type r = 0; r < nrows(); ++r) {
3444 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3445 !row_info[r].caption) {
3446 ret += docbookRow(os, r, runparams);
3452 // output footer info
3453 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3456 for (row_type r = 0; r < nrows(); ++r) {
3457 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3458 !row_info[r].caption) {
3459 ret += docbookRow(os, r, runparams);
3466 //+---------------------------------------------------------------------
3467 //+ the single row and columns (cells) +
3468 //+---------------------------------------------------------------------
3472 for (row_type r = 0; r < nrows(); ++r) {
3473 if (isValidRow(r)) {
3474 ret += docbookRow(os, r, runparams);
3479 //+---------------------------------------------------------------------
3480 //+ the closing of the tabular +
3481 //+---------------------------------------------------------------------
3490 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3491 OutputParams const & runparams, bool header) const
3494 string const celltag = header ? "th" : "td";
3495 idx_type cell = getFirstCellInRow(row);
3497 xs << html::StartTag("tr");
3498 for (col_type c = 0; c < ncols(); ++c) {
3499 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3504 Length const cwidth = column_info[c].p_width;
3505 if (!cwidth.zero()) {
3506 string const hwidth = cwidth.asHTMLString();
3507 attr << "style =\"width: " << hwidth << ";\" ";
3511 switch (getAlignment(cell)) {
3512 case LYX_ALIGN_LEFT:
3515 case LYX_ALIGN_RIGHT:
3523 attr << " valign='";
3524 switch (getVAlignment(cell)) {
3525 case LYX_VALIGN_TOP:
3528 case LYX_VALIGN_BOTTOM:
3531 case LYX_VALIGN_MIDDLE:
3536 if (isMultiColumn(cell))
3537 attr << " colspan='" << columnSpan(cell) << "'";
3538 else if (isMultiRow(cell))
3539 attr << " rowspan='" << rowSpan(cell) << "'";
3541 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3542 ret += cellInset(cell)->xhtml(xs, runparams);
3543 xs << html::EndTag(celltag) << html::CR();
3546 xs << html::EndTag("tr");
3551 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3555 if (is_long_tabular) {
3556 // we'll wrap it in a div, so as to deal with alignment
3558 switch (longtabular_alignment) {
3559 case LYX_LONGTABULAR_ALIGN_LEFT:
3562 case LYX_LONGTABULAR_ALIGN_CENTER:
3565 case LYX_LONGTABULAR_ALIGN_RIGHT:
3569 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3571 // The caption flag wins over head/foot
3572 if (haveLTCaption()) {
3573 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3575 for (row_type r = 0; r < nrows(); ++r)
3576 if (row_info[r].caption)
3577 ret += xhtmlRow(xs, r, runparams);
3578 xs << html::EndTag("div") << html::CR();
3582 xs << html::StartTag("table") << html::CR();
3584 // output header info
3585 bool const havefirsthead = haveLTFirstHead(false);
3586 // if we have a first head, then we are going to ignore the
3587 // headers for the additional pages, since there aren't any
3588 // in XHTML. this test accomplishes that.
3589 bool const havehead = !havefirsthead && haveLTHead(false);
3590 if (havehead || havefirsthead) {
3591 xs << html::StartTag("thead") << html::CR();
3592 for (row_type r = 0; r < nrows(); ++r) {
3593 if (((havefirsthead && row_info[r].endfirsthead) ||
3594 (havehead && row_info[r].endhead)) &&
3595 !row_info[r].caption) {
3596 ret += xhtmlRow(xs, r, runparams, true);
3599 xs << html::EndTag("thead") << html::CR();
3601 // output footer info
3602 bool const havelastfoot = haveLTLastFoot(false);
3604 bool const havefoot = !havelastfoot && haveLTFoot(false);
3605 if (havefoot || havelastfoot) {
3606 xs << html::StartTag("tfoot") << html::CR();
3607 for (row_type r = 0; r < nrows(); ++r) {
3608 if (((havelastfoot && row_info[r].endlastfoot) ||
3609 (havefoot && row_info[r].endfoot)) &&
3610 !row_info[r].caption) {
3611 ret += xhtmlRow(xs, r, runparams);
3614 xs << html::EndTag("tfoot") << html::CR();
3617 xs << html::StartTag("tbody") << html::CR();
3618 for (row_type r = 0; r < nrows(); ++r) {
3619 if (isValidRow(r)) {
3620 ret += xhtmlRow(xs, r, runparams);
3623 xs << html::EndTag("tbody")
3625 << html::EndTag("table")
3627 if (is_long_tabular)
3628 xs << html::EndTag("div") << html::CR();
3633 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3634 vector<unsigned int> const & clen) const
3636 idx_type const fcell = getFirstCellInRow(row);
3637 idx_type const n = numberOfCellsInRow(row) + fcell;
3640 for (idx_type i = fcell; i < n; ++i) {
3650 for (idx_type i = fcell; i < n; ++i) {
3661 col_type column = cellColumn(i);
3662 int len = clen[column];
3663 while (column < ncols() - 1
3664 && isPartOfMultiColumn(row, ++column))
3665 len += clen[column] + 4;
3666 os << docstring(len, ch);
3681 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3682 vector<unsigned int> const & clen) const
3684 idx_type const fcell = getFirstCellInRow(row);
3685 idx_type const n = numberOfCellsInRow(row) + fcell;
3688 for (idx_type i = fcell; i < n; ++i) {
3689 if (bottomLine(i)) {
3698 for (idx_type i = fcell; i < n; ++i) {
3699 if (bottomLine(i)) {
3709 col_type column = cellColumn(i);
3710 int len = clen[column];
3711 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3712 len += clen[column] + 4;
3713 os << docstring(len, ch);
3714 if (bottomLine(i)) {
3728 void Tabular::plaintextPrintCell(odocstringstream & os,
3729 OutputParams const & runparams,
3730 idx_type cell, row_type row, col_type column,
3731 vector<unsigned int> const & clen,
3732 bool onlydata, size_t max_length) const
3734 odocstringstream sstr;
3735 cellInset(cell)->plaintext(sstr, runparams, max_length);
3747 unsigned int len1 = sstr.str().length();
3748 unsigned int len2 = clen[column];
3749 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3750 len2 += clen[column] + 4;
3753 switch (getAlignment(cell)) {
3755 case LYX_ALIGN_LEFT:
3758 case LYX_ALIGN_RIGHT:
3762 case LYX_ALIGN_CENTER:
3768 os << docstring(len1, ' ') << sstr.str()
3769 << docstring(len2, ' ');
3771 if (rightLine(cell))
3778 void Tabular::plaintext(odocstringstream & os,
3779 OutputParams const & runparams, int const depth,
3780 bool onlydata, char_type delim, size_t max_length) const
3782 // first calculate the width of the single columns
3783 vector<unsigned int> clen(ncols());
3786 // first all non multicolumn cells!
3787 for (col_type c = 0; c < ncols(); ++c) {
3789 for (row_type r = 0; r < nrows(); ++r) {
3790 idx_type cell = cellIndex(r, c);
3791 if (isMultiColumn(cell))
3793 odocstringstream sstr;
3794 cellInset(cell)->plaintext(sstr, runparams, max_length);
3795 if (clen[c] < sstr.str().length())
3796 clen[c] = sstr.str().length();
3799 // then all multicolumn cells!
3800 for (col_type c = 0; c < ncols(); ++c) {
3801 for (row_type r = 0; r < nrows(); ++r) {
3802 idx_type cell = cellIndex(r, c);
3803 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3805 odocstringstream sstr;
3806 cellInset(cell)->plaintext(sstr, runparams, max_length);
3807 int len = int(sstr.str().length());
3808 idx_type const n = columnSpan(cell);
3809 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3811 if (len > int(clen[c + n - 1]))
3812 clen[c + n - 1] = len;
3817 for (row_type r = 0; r < nrows(); ++r) {
3818 if (!onlydata && plaintextTopHLine(os, r, clen))
3819 os << docstring(depth * 2, ' ');
3820 for (col_type c = 0; c < ncols(); ++c) {
3821 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3823 if (onlydata && c > 0)
3824 // we don't use operator<< for single UCS4 character.
3825 // see explanation in docstream.h
3827 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3829 if (os.str().size() > max_length)
3834 os << docstring(depth * 2, ' ');
3835 if (plaintextBottomHLine(os, r, clen))
3836 os << docstring(depth * 2, ' ');
3838 if (os.str().size() > max_length)
3844 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3846 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3850 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3852 return cell_info[row][column].inset;
3856 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3858 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3862 void Tabular::setCellInset(row_type row, col_type column,
3863 shared_ptr<InsetTableCell> ins)
3865 CellData & cd = cell_info[row][column];
3870 void Tabular::validate(LaTeXFeatures & features) const
3872 features.require("NeedTabularnewline");
3874 features.require("booktabs");
3875 if (is_long_tabular && !hasVarwidthColumn()) {
3876 if (tabular_width.zero())
3877 features.require("longtable");
3879 features.require("xltabular");
3881 if (rotate && is_long_tabular)
3882 features.require("lscape");
3884 features.require("rotating");
3885 if (hasVarwidthColumn()) {
3886 if (is_long_tabular)
3887 features.require("xltabular");
3889 features.require("tabularx");
3891 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3892 if (isMultiRow(cell))
3893 features.require("multirow");
3894 if (getUsebox(cell) == BOX_VARWIDTH)
3895 features.require("varwidth");
3896 if (getVAlignment(cell) != LYX_VALIGN_TOP
3897 || !getPWidth(cell).zero()
3898 || isVTypeColumn(cellColumn(cell)))
3899 features.require("array");
3900 // Tell footnote that we need a savenote
3901 // environment in non-long tables or
3902 // longtable headers/footers
3903 else if (!is_long_tabular && !features.inFloat())
3904 features.saveNoteEnv("tabular");
3905 else if (!isValidRow(cellRow(cell)))
3906 features.saveNoteEnv("longtable");
3908 cellInset(cell)->validate(features);
3909 features.saveNoteEnv(string());
3914 Tabular::BoxType Tabular::useBox(idx_type cell) const
3916 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3917 if (parlist.size() > 1)
3918 return BOX_VARWIDTH;
3920 ParagraphList::const_iterator cit = parlist.begin();
3921 ParagraphList::const_iterator end = parlist.end();
3923 for (; cit != end; ++cit)
3924 for (int i = 0; i < cit->size(); ++i)
3925 if (cit->isNewline(i) || cit->layout().isEnvironment())
3926 return BOX_VARWIDTH;
3932 /////////////////////////////////////////////////////////////////////
3936 /////////////////////////////////////////////////////////////////////
3938 InsetTableCell::InsetTableCell(Buffer * buf)
3939 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3940 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
3944 bool InsetTableCell::forcePlainLayout(idx_type) const
3946 return isMultiRow || (isMultiColumn && !isFixedWidth);
3950 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3952 return isFixedWidth;
3956 bool InsetTableCell::forceLocalFontSwitch() const
3958 return isFixedWidth;
3962 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3963 FuncStatus & status) const
3965 bool enabled = true;
3966 switch (cmd.action()) {
3967 case LFUN_MATH_DISPLAY:
3968 if (!hasFixedWidth()) {
3973 return InsetText::getStatus(cur, cmd, status);
3975 status.setEnabled(enabled);
3979 docstring InsetTableCell::asString(bool intoInsets)
3982 if (paragraphs().empty())
3984 ParagraphList::const_iterator it = paragraphs().begin();
3985 ParagraphList::const_iterator en = paragraphs().end();
3987 for (; it != en; ++it) {
3992 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3998 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3999 UpdateType utype, TocBackend & backend) const
4001 InsetText::iterateForToc(di, output_active, utype, backend);
4005 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4008 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4009 return InsetText::xhtml(xs, rp);
4013 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4015 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4017 // Hand font through to contained lyxtext:
4018 tm.font_.fontInfo() = mi.base.font;
4019 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
4021 // This can happen when a layout has a left and right margin,
4022 // and the view is made very narrow. We can't do better than
4023 // to draw it partly out of view (bug 5890).
4024 if (mi.base.textwidth < 1)
4025 mi.base.textwidth = 1;
4027 // We tell metrics here not to expand on multiple pars
4028 // This is the difference to InsetText::Metrics
4029 if (hasFixedWidth())
4030 tm.metrics(mi, dim, mi.base.textwidth, false);
4032 tm.metrics(mi, dim, 0, false);
4033 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
4034 dim.asc += TEXT_TO_INSET_OFFSET;
4035 dim.des += TEXT_TO_INSET_OFFSET;
4036 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
4041 /////////////////////////////////////////////////////////////////////
4045 /////////////////////////////////////////////////////////////////////
4047 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4049 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4050 rowselect_(false), colselect_(false)
4055 InsetTabular::InsetTabular(InsetTabular const & tab)
4056 : Inset(tab), tabular(tab.tabular),
4057 rowselect_(false), colselect_(false)
4062 InsetTabular::~InsetTabular()
4064 hideDialogs("tabular", this);
4068 void InsetTabular::setBuffer(Buffer & buf)
4070 tabular.setBuffer(buf);
4071 Inset::setBuffer(buf);
4075 bool InsetTabular::insetAllowed(InsetCode code) const
4080 case MATHMACRO_CODE:
4085 return tabular.is_long_tabular;
4093 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4095 return tabular.is_long_tabular &&
4096 (newtype == "Standard" || newtype == "Unnumbered");
4100 void InsetTabular::write(ostream & os) const
4102 os << "Tabular" << endl;
4107 string InsetTabular::contextMenu(BufferView const &, int, int) const
4109 // FIXME: depending on the selection state,
4110 // we could offer a different menu.
4111 return cell(0)->contextMenuName() + ";" + contextMenuName();
4115 string InsetTabular::contextMenuName() const
4117 return "context-tabular";
4121 void InsetTabular::read(Lexer & lex)
4123 //bool const old_format = (lex.getString() == "\\LyXTable");
4131 string token = lex.getString();
4132 while (lex && token != "\\end_inset") {
4134 token = lex.getString();
4137 lex.printError("Missing \\end_inset at this point. ");
4141 int InsetTabular::rowFromY(Cursor & cur, int y) const
4143 // top y coordinate of tabular
4144 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4146 for (; r < tabular.nrows() && y > h; ++r)
4147 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4148 + tabular.interRowSpace(r);
4154 int InsetTabular::columnFromX(Cursor & cur, int x) const
4156 // left x coordinate of tabular
4157 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4159 for (; c < tabular.ncols() && x > w; ++c)
4160 w += tabular.cellWidth(c);
4165 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4167 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4168 // mi.base.textwidth << "\n";
4169 LBUFERR(mi.base.bv);
4171 for (row_type r = 0; r < tabular.nrows(); ++r) {
4174 for (col_type c = 0; c < tabular.ncols(); ++c) {
4175 if (tabular.isPartOfMultiColumn(r, c)
4176 || tabular.isPartOfMultiRow(r, c))
4177 // multicolumn or multirow cell, but not first one
4179 idx_type const cell = tabular.cellIndex(r, c);
4182 Length const p_width = tabular.getPWidth(cell);
4183 if (!p_width.zero())
4184 m.base.textwidth = mi.base.inPixels(p_width);
4185 else if (tabular.column_info[c].varwidth)
4186 m.base.textwidth = tabular.column_info[c].width;
4187 tabular.cellInset(cell)->metrics(m, dim0);
4188 if (!p_width.zero() || tabular.column_info[c].varwidth)
4189 dim0.wid = m.base.textwidth;
4190 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4191 + tabular.interColumnSpace(cell);
4193 // FIXME(?): do we need a second metrics call?
4194 TextMetrics const & tm =
4195 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4197 // determine horizontal offset because of decimal align (if necessary)
4198 int decimal_width = 0;
4199 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4200 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4201 tail.setBuffer(tabular.buffer());
4202 // we need to set macrocontext position everywhere
4203 // otherwise we crash with nested insets (e.g. footnotes)
4204 // after decimal point
4205 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4207 dit.push_back(CursorSlice(tail));
4208 tail.setMacrocontextPositionRecursive(dit);
4210 // remove text leading decimal point
4211 docstring const align_d = tabular.column_info[c].decimal_point;
4212 dit = separatorPos(&tail, align_d);
4214 pit_type const psize = tail.paragraphs().front().size();
4216 tail.paragraphs().front().eraseChars(0,
4217 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4219 tail.metrics(m, dim1);
4220 decimal_width = dim1.width();
4223 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4224 tabular.cell_info[r][c].decimal_width = decimal_width;
4226 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4227 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
4228 int const lastpardes = tm.last().second->descent()
4229 + TEXT_TO_INSET_OFFSET;
4231 switch (tabular.getVAlignment(cell)) {
4232 case Tabular::LYX_VALIGN_TOP:
4234 case Tabular::LYX_VALIGN_MIDDLE:
4235 offset = -(dim0.des - lastpardes)/2;
4237 case Tabular::LYX_VALIGN_BOTTOM:
4238 offset = -(dim0.des - lastpardes);
4241 tabular.cell_info[r][c].voffset = offset;
4242 maxasc = max(maxasc, dim0.asc - offset);
4243 maxdes = max(maxdes, dim0.des + offset);
4245 int const top_space = tabular.row_info[r].top_space_default ?
4246 default_line_space :
4247 mi.base.inPixels(tabular.row_info[r].top_space);
4248 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4249 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4250 default_line_space :
4251 mi.base.inPixels(tabular.row_info[r].bottom_space);
4252 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4255 // We need to recalculate the metrics after column width calculation
4256 // with xtabular (possibly multiple times, so the call is recursive).
4257 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4259 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4260 dim.des = tabular.height() - dim.asc;
4261 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4265 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4267 if (&cur.inset() == this && cur.selection()) {
4268 if (cur.selIsMultiCell()) {
4271 getSelection(cur, rs, re, cs, ce);
4273 idx_type const cell = tabular.cellIndex(row, col);
4274 col_type const cspan = tabular.columnSpan(cell);
4275 row_type const rspan = tabular.rowSpan(cell);
4276 if (col + cspan - 1 >= cs && col <= ce
4277 && row + rspan - 1 >= rs && row <= re)
4280 if (col == tabular.cellColumn(cur.idx())
4281 && row == tabular.cellRow(cur.idx())) {
4282 CursorSlice const & beg = cur.selBegin();
4283 CursorSlice const & end = cur.selEnd();
4285 if ((end.lastpos() > 0 || end.lastpit() > 0)
4286 && end.pos() == end.lastpos() && beg.pos() == 0
4287 && end.pit() == end.lastpit() && beg.pit() == 0)
4295 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4297 x += ADD_TO_TABULAR_WIDTH;
4299 BufferView * bv = pi.base.bv;
4300 Cursor & cur = pi.base.bv->cursor();
4302 // FIXME: As the full background is painted in drawBackground(),
4303 // we have no choice but to do a full repaint for the Text cells.
4304 pi.full_repaint = true;
4306 bool const original_selection_state = pi.selected;
4310 int yy = y + tabular.offsetVAlignment();
4311 for (row_type r = 0; r < tabular.nrows(); ++r) {
4313 for (col_type c = 0; c < tabular.ncols(); ++c) {
4314 if (tabular.isPartOfMultiColumn(r, c))
4317 idx = tabular.cellIndex(r, c);
4319 if (tabular.isPartOfMultiRow(r, c)) {
4320 nx += tabular.cellWidth(idx);
4324 pi.selected |= isCellSelected(cur, r, c);
4325 int const cx = nx + tabular.textHOffset(idx);
4326 int const cy = yy + tabular.textVOffset(idx);
4327 // Cache the Inset position.
4328 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4329 cell(idx)->draw(pi, cx, cy);
4330 drawCellLines(pi, nx, yy, r, idx);
4331 nx += tabular.cellWidth(idx);
4332 pi.selected = original_selection_state;
4335 if (r + 1 < tabular.nrows())
4336 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4337 + tabular.interRowSpace(r + 1);
4342 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4344 x += ADD_TO_TABULAR_WIDTH;
4345 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4346 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4347 pi.backgroundColor(this));
4351 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4353 Cursor & cur = pi.base.bv->cursor();
4355 x += ADD_TO_TABULAR_WIDTH;
4357 if (!cur.selection())
4359 if (&cur.inset() != this)
4364 bool const full_cell_selected = isCellSelected(cur,
4365 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4367 if (cur.selIsMultiCell() || full_cell_selected) {
4368 for (row_type r = 0; r < tabular.nrows(); ++r) {
4370 for (col_type c = 0; c < tabular.ncols(); ++c) {
4371 if (tabular.isPartOfMultiColumn(r, c))
4374 idx_type const cell = tabular.cellIndex(r, c);
4376 if (tabular.isPartOfMultiRow(r, c)) {
4377 xx += tabular.cellWidth(cell);
4380 int const w = tabular.cellWidth(cell);
4381 int const h = tabular.cellHeight(cell);
4382 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4383 if (isCellSelected(cur, r, c))
4384 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4387 if (r + 1 < tabular.nrows())
4388 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4389 + tabular.interRowSpace(r + 1);
4393 // FIXME: This code has no effect because InsetTableCell does not handle
4394 // drawSelection other than the trivial implementation in Inset.
4396 // x += cellXPos(cur.idx());
4397 // x += tabular.textHOffset(cur.idx());
4398 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4405 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4406 bool drawline, bool heavy = false)
4408 ColorCode const col = drawline ? Color_tabularline : Color_tabularonoffline;
4409 if (drawline && lt > 0)
4410 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4411 Painter::line_onoffdash,
4412 Painter::thin_line);
4413 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4414 drawline ? Painter::line_solid : Painter::line_onoffdash,
4415 (heavy ? 2 : 1) * Painter::thin_line);
4416 if (drawline && rt > 0)
4417 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4418 Painter::line_onoffdash,
4419 Painter::thin_line);
4425 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4426 row_type row, idx_type cell) const
4428 y -= tabular.rowAscent(row);
4429 int const w = tabular.cellWidth(cell);
4430 int const h = tabular.cellHeight(cell);
4434 col_type const col = tabular.cellColumn(cell);
4437 bool drawline = tabular.topLine(cell)
4438 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4439 bool heavy = tabular.use_booktabs
4440 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4441 && tabular.rowTopLine(row);
4442 if (tabular.topLineTrim(cell).first
4443 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4445 if (tabular.topLineTrim(cell).second
4446 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4448 tabline(pi, x, y, x + w, y, lt, rt, drawline, heavy);
4452 drawline = tabular.bottomLine(cell);
4453 row_type const lastrow = tabular.nrows() - 1;
4454 // Consider multi-rows
4456 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4457 && tabular.isPartOfMultiRow(r + 1, col))
4459 heavy = tabular.use_booktabs
4460 && ((row == lastrow && tabular.rowBottomLine(row))
4461 || (r == lastrow && tabular.rowBottomLine(r)));
4462 if (tabular.bottomLineTrim(cell).first)
4464 if (tabular.bottomLineTrim(cell).second)
4466 tabline(pi, x, y + h, x + w, y + h, lt, rt, drawline, heavy);
4469 drawline = tabular.leftLine(cell)
4470 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4471 tabline(pi, x, y, x, y + h, 0, 0, drawline);
4474 x -= tabular.interColumnSpace(cell);
4475 col_type next_cell_col = col + 1;
4476 while (next_cell_col < tabular.ncols()
4477 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4479 drawline = tabular.rightLine(cell)
4480 || (next_cell_col < tabular.ncols()
4481 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4482 tabline(pi, x + w, y, x + w, y + h, 0, 0, drawline);
4486 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4488 //lyxerr << "InsetTabular::edit: " << this << endl;
4492 if (isRightToLeft(cur))
4493 cur.idx() = tabular.getLastCellInRow(0);
4499 if (isRightToLeft(cur))
4500 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4502 cur.idx() = tabular.numberofcells - 1;
4504 cur.pos() = cur.lastpos(); // FIXME crude guess
4506 cur.setCurrentFont();
4507 // FIXME: this accesses the position cache before it is initialized
4508 //cur.bv().fitCursor();
4512 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
4514 // In a longtable, tell captions what the current float is
4515 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4516 string const saveflt = cnts.current_float();
4517 if (tabular.is_long_tabular) {
4518 cnts.current_float("table");
4519 // in longtables, we only step the counter once
4520 cnts.step(from_ascii("table"), utype);
4521 cnts.isLongtable(true);
4524 ParIterator it2 = it;
4526 size_t const end = it2.nargs();
4527 for ( ; it2.idx() < end; it2.top().forwardIdx())
4528 buffer().updateBuffer(it2, utype);
4531 if (tabular.is_long_tabular) {
4532 cnts.current_float(saveflt);
4533 cnts.isLongtable(false);
4538 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4539 UpdateType utype, TocBackend & backend) const
4541 DocIterator dit = cpit;
4543 size_t const end = dit.nargs();
4544 for ( ; dit.idx() < end; dit.top().forwardIdx())
4545 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4549 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4551 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4552 return x < x0 || x > x0 + tabular.width();
4556 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4558 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4559 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4560 // there is no margin added vertically to tabular insets.
4561 // However, it works for now.
4562 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4566 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4568 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4572 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4574 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4575 << "\n cur:" << cur);
4576 CursorSlice sl = cur.top();
4577 Cursor & bvcur = cur.bv().cursor();
4579 FuncCode const act = cmd.action();
4583 case LFUN_MOUSE_PRESS: {
4584 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4586 if (hitSelectRow(cur.bv(), cmd.x())) {
4587 row_type r = rowFromY(cur, cmd.y());
4588 cur.idx() = tabular.getFirstCellInRow(r);
4592 cur.idx() = tabular.getLastCellInRow(r);
4593 cur.pit() = cur.lastpit();
4594 cur.pos() = cur.lastpos();
4595 cur.selection(true);
4601 if (hitSelectColumn(cur.bv(), cmd.y())) {
4602 col_type c = columnFromX(cur, cmd.x());
4603 cur.idx() = tabular.cellIndex(0, c);
4607 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4608 cur.pit() = cur.lastpit();
4609 cur.pos() = cur.lastpos();
4610 cur.selection(true);
4615 // do not reset cursor/selection if we have selected
4616 // some cells (bug 2715).
4617 if (cmd.button() == mouse_button::button3
4618 && &bvcur.selBegin().inset() == this
4619 && bvcur.selIsMultiCell())
4622 // Let InsetTableCell do it
4623 cell(cur.idx())->dispatch(cur, cmd);
4626 case LFUN_MOUSE_MOTION:
4627 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4628 if (cmd.button() == mouse_button::button1) {
4629 // only accept motions to places not deeper nested than the real anchor
4630 if (!bvcur.realAnchor().hasPart(cur)) {
4634 // select (additional) row
4636 row_type r = rowFromY(cur, cmd.y());
4637 cur.idx() = tabular.getLastCellInRow(r);
4638 // we need to reset the cursor's pit and pos now, as the old ones
4639 // may no longer be valid.
4642 bvcur.setCursor(cur);
4643 bvcur.selection(true);
4646 // select (additional) column
4648 col_type c = columnFromX(cur, cmd.x());
4649 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4650 // we need to reset the cursor's pit and pos now, as the old ones
4651 // may no longer be valid.
4654 bvcur.setCursor(cur);
4655 bvcur.selection(true);
4658 // only update if selection changes
4659 if (bvcur.idx() == cur.idx() &&
4660 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4661 cur.noScreenUpdate();
4662 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4663 bvcur.setCursor(cur);
4664 bvcur.selection(true);
4665 // if this is a multicell selection, we just set the cursor to
4666 // the beginning of the cell's text.
4667 if (bvcur.selIsMultiCell()) {
4668 bvcur.pit() = bvcur.lastpit();
4669 bvcur.pos() = bvcur.lastpos();
4674 case LFUN_MOUSE_RELEASE:
4679 case LFUN_CELL_BACKWARD:
4681 cur.selection(false);
4684 case LFUN_CELL_FORWARD:
4686 cur.selection(false);
4689 case LFUN_CHAR_FORWARD_SELECT:
4690 case LFUN_CHAR_FORWARD:
4691 case LFUN_CHAR_BACKWARD_SELECT:
4692 case LFUN_CHAR_BACKWARD:
4693 case LFUN_CHAR_RIGHT_SELECT:
4694 case LFUN_CHAR_RIGHT:
4695 case LFUN_CHAR_LEFT_SELECT:
4696 case LFUN_CHAR_LEFT:
4697 case LFUN_WORD_FORWARD:
4698 case LFUN_WORD_FORWARD_SELECT:
4699 case LFUN_WORD_BACKWARD:
4700 case LFUN_WORD_BACKWARD_SELECT:
4701 case LFUN_WORD_RIGHT:
4702 case LFUN_WORD_RIGHT_SELECT:
4703 case LFUN_WORD_LEFT:
4704 case LFUN_WORD_LEFT_SELECT: {
4705 // determine whether we move to next or previous cell, where to enter
4706 // the new cell from, and which command to "finish" (i.e., exit the
4709 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4710 FuncCode finish_lfun;
4712 if (act == LFUN_CHAR_FORWARD
4713 || act == LFUN_CHAR_FORWARD_SELECT
4714 || act == LFUN_WORD_FORWARD
4715 || act == LFUN_WORD_FORWARD_SELECT) {
4717 finish_lfun = LFUN_FINISHED_FORWARD;
4719 else if (act == LFUN_CHAR_BACKWARD
4720 || act == LFUN_CHAR_BACKWARD_SELECT
4721 || act == LFUN_WORD_BACKWARD
4722 || act == LFUN_WORD_BACKWARD_SELECT) {
4724 finish_lfun = LFUN_FINISHED_BACKWARD;
4726 // LEFT or RIGHT commands --- the interpretation will depend on the
4727 // table's direction.
4729 bool const right = act == LFUN_CHAR_RIGHT
4730 || act == LFUN_CHAR_RIGHT_SELECT
4731 || act == LFUN_WORD_RIGHT
4732 || act == LFUN_WORD_RIGHT_SELECT;
4733 next_cell = isRightToLeft(cur) != right;
4735 if (lyxrc.visual_cursor)
4736 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4738 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4741 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4742 || act == LFUN_CHAR_BACKWARD_SELECT
4743 || act == LFUN_CHAR_RIGHT_SELECT
4744 || act == LFUN_CHAR_LEFT_SELECT
4745 || act == LFUN_WORD_FORWARD_SELECT
4746 || act == LFUN_WORD_RIGHT_SELECT
4747 || act == LFUN_WORD_BACKWARD_SELECT
4748 || act == LFUN_WORD_LEFT_SELECT;
4750 // If we have a multicell selection or we're
4751 // not doing some LFUN_*_SELECT thing anyway...
4752 if (!cur.selIsMultiCell() || !select) {
4753 col_type const c = tabular.cellColumn(cur.idx());
4754 row_type const r = tabular.cellRow(cur.idx());
4755 // Are we trying to select the whole cell and is the whole cell
4756 // not yet selected?
4757 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4758 ((next_cell && cur.pit() == cur.lastpit()
4759 && cur.pos() == cur.lastpos())
4760 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4762 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4764 // ...try to dispatch to the cell's inset.
4765 cell(cur.idx())->dispatch(cur, cmd);
4767 // When we already have a selection we want to select the whole cell
4768 // before going to the next cell.
4769 if (select_whole && !empty_cell){
4770 getText(cur.idx())->selectAll(cur);
4772 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4776 // FIXME: When we support the selection of an empty cell, remove
4777 // the !empty_cell from this condition. For now we jump to the next
4778 // cell if the current cell is empty.
4779 if (cur.result().dispatched() && !empty_cell)
4783 // move to next/prev cell, as appropriate
4784 // note that we will always do this if we're selecting and we have
4785 // a multicell selection
4786 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4787 << " cell from: " << int(entry_from));
4789 moveNextCell(cur, entry_from);
4791 movePrevCell(cur, entry_from);
4792 // if we're exiting the table, call the appropriate FINISHED lfun
4793 if (sl == cur.top()) {
4794 cmd = FuncRequest(finish_lfun);
4799 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4804 case LFUN_DOWN_SELECT:
4806 if (!(cur.selection() && cur.selIsMultiCell()))
4807 cell(cur.idx())->dispatch(cur, cmd);
4809 cur.dispatched(); // override the cell's decision
4810 if (sl == cur.top()) {
4811 // if our Text didn't do anything to the cursor
4812 // then we try to put the cursor into the cell below
4813 // setting also the right targetX.
4814 cur.selHandle(act == LFUN_DOWN_SELECT);
4815 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4816 int const xtarget = cur.targetX();
4817 // WARNING: Once cur.idx() has been reset, the cursor is in
4818 // an inconsistent state until pos() has been set. Be careful
4819 // what you do with it!
4820 cur.idx() = tabular.cellBelow(cur.idx());
4822 TextMetrics const & tm =
4823 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4824 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4825 cur.setCurrentFont();
4828 if (sl == cur.top()) {
4829 // we trick it to go to forward after leaving the
4831 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4834 if (cur.selIsMultiCell()) {
4835 cur.pit() = cur.lastpit();
4836 cur.pos() = cur.lastpos();
4837 cur.setCurrentFont();
4838 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4841 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4844 case LFUN_UP_SELECT:
4846 if (!(cur.selection() && cur.selIsMultiCell()))
4847 cell(cur.idx())->dispatch(cur, cmd);
4848 cur.dispatched(); // override the cell's decision
4849 if (sl == cur.top()) {
4850 // if our Text didn't do anything to the cursor
4851 // then we try to put the cursor into the cell above
4852 // setting also the right targetX.
4853 cur.selHandle(act == LFUN_UP_SELECT);
4854 if (tabular.cellRow(cur.idx()) != 0) {
4855 int const xtarget = cur.targetX();
4856 // WARNING: Once cur.idx() has been reset, the cursor is in
4857 // an inconsistent state until pos() has been set. Be careful
4858 // what you do with it!
4859 cur.idx() = tabular.cellAbove(cur.idx());
4860 cur.pit() = cur.lastpit();
4861 Text const * text = cell(cur.idx())->getText(0);
4862 TextMetrics const & tm = cur.bv().textMetrics(text);
4863 ParagraphMetrics const & pm =
4864 tm.parMetrics(cur.lastpit());
4865 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4866 cur.setCurrentFont();
4869 if (sl == cur.top()) {
4870 cmd = FuncRequest(LFUN_UP);
4873 if (cur.selIsMultiCell()) {
4875 cur.pos() = cur.lastpos();
4876 cur.setCurrentFont();
4877 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4880 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4883 case LFUN_LAYOUT_TABULAR:
4884 cur.bv().showDialog("tabular");
4887 case LFUN_INSET_MODIFY:
4888 // we come from the dialog
4889 if (cmd.getArg(0) == "tabular")
4890 tabularFeatures(cur, cmd.getLongArg(1));
4895 case LFUN_TABULAR_FEATURE:
4896 tabularFeatures(cur, to_utf8(cmd.argument()));
4899 // insert file functions
4900 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4901 case LFUN_FILE_INSERT_PLAINTEXT:
4903 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4904 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4905 FileName(to_utf8(cmd.argument())));
4908 cur.recordUndoInset();
4909 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4910 // content has been replaced,
4911 // so cursor might be invalid
4912 cur.pos() = cur.lastpos();
4913 cur.pit() = cur.lastpit();
4914 bvcur.setCursor(cur);
4921 if (cur.selIsMultiCell()) {
4922 if (copySelection(cur)) {
4923 cur.recordUndoInset();
4927 cell(cur.idx())->dispatch(cur, cmd);
4930 case LFUN_SELF_INSERT:
4931 if (cur.selIsMultiCell()) {
4932 cur.recordUndoInset();
4934 BufferView * bv = &cur.bv();
4935 docstring::const_iterator cit = cmd.argument().begin();
4936 docstring::const_iterator const end = cmd.argument().end();
4937 for (; cit != end; ++cit)
4938 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4941 bv->bookmarkEditPosition();
4943 cell(cur.idx())->dispatch(cur, cmd);
4946 case LFUN_CHAR_DELETE_BACKWARD:
4947 case LFUN_CHAR_DELETE_FORWARD:
4948 if (cur.selIsMultiCell()) {
4949 cur.recordUndoInset();
4952 cell(cur.idx())->dispatch(cur, cmd);
4956 if (!cur.selection())
4958 if (cur.selIsMultiCell()) {
4962 cell(cur.idx())->dispatch(cur, cmd);
4965 case LFUN_CLIPBOARD_PASTE:
4966 case LFUN_PRIMARY_SELECTION_PASTE: {
4967 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4968 theClipboard().getAsText(Clipboard::PlainTextType) :
4969 theSelection().get();
4972 // pass to InsertPlaintextString, but
4973 // only if we have multi-cell content
4974 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4975 cur.recordUndoInset();
4976 if (insertPlaintextString(cur.bv(), clip, false)) {
4977 // content has been replaced,
4978 // so cursor might be invalid
4979 cur.pos() = cur.lastpos();
4980 cur.pit() = cur.lastpit();
4981 bvcur.setCursor(cur);
4985 // Let the cell handle normal text
4986 cell(cur.idx())->dispatch(cur, cmd);
4991 if (!tabularStackDirty()) {
4992 // Check if we have plain text or HTML with rows/columns.
4993 // and if so, pass over to LFUN_CLIPBOARD_PASTE
4994 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
4995 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
4996 docstring const clip =
4997 theClipboard().getAsText(Clipboard::AnyTextType);
4998 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4999 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5000 doDispatch(cur, ncmd);
5004 if (!cur.selIsMultiCell())
5005 cell(cur.idx())->dispatch(cur, cmd);
5008 if (theClipboard().isInternal()) {
5009 cur.recordUndoInset();
5010 pasteClipboard(cur);
5014 case LFUN_FONT_EMPH:
5015 case LFUN_FONT_BOLD:
5016 case LFUN_FONT_BOLDSYMBOL:
5017 case LFUN_FONT_ROMAN:
5018 case LFUN_FONT_NOUN:
5019 case LFUN_FONT_ITAL:
5020 case LFUN_FONT_FRAK:
5021 case LFUN_FONT_TYPEWRITER:
5022 case LFUN_FONT_SANS:
5023 case LFUN_TEXTSTYLE_APPLY:
5024 case LFUN_TEXTSTYLE_UPDATE:
5025 case LFUN_FONT_SIZE:
5026 case LFUN_FONT_UNDERLINE:
5027 case LFUN_FONT_STRIKEOUT:
5028 case LFUN_FONT_CROSSOUT:
5029 case LFUN_FONT_UNDERUNDERLINE:
5030 case LFUN_FONT_UNDERWAVE:
5032 case LFUN_PARAGRAPH_PARAMS_APPLY:
5033 case LFUN_PARAGRAPH_PARAMS:
5034 case LFUN_WORD_CAPITALIZE:
5035 case LFUN_WORD_UPCASE:
5036 case LFUN_WORD_LOWCASE:
5037 case LFUN_CHARS_TRANSPOSE:
5038 if (cur.selIsMultiCell()) {
5041 getSelection(cur, rs, re, cs, ce);
5042 Cursor tmpcur = cur;
5043 for (row_type r = rs; r <= re; ++r) {
5044 for (col_type c = cs; c <= ce; ++c) {
5045 // cursor follows cell:
5046 tmpcur.idx() = tabular.cellIndex(r, c);
5047 // select this cell only:
5050 tmpcur.resetAnchor();
5051 tmpcur.pit() = tmpcur.lastpit();
5052 tmpcur.pos() = tmpcur.top().lastpos();
5053 tmpcur.setCursor(tmpcur);
5054 tmpcur.setSelection();
5055 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5060 cell(cur.idx())->dispatch(cur, cmd);
5064 case LFUN_INSET_SETTINGS:
5065 // relay this lfun to Inset, not to the cell.
5066 Inset::doDispatch(cur, cmd);
5070 // we try to handle this event in the insets dispatch function.
5071 cell(cur.idx())->dispatch(cur, cmd);
5077 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5078 string const & argument, FuncStatus & status) const
5081 int action = Tabular::LAST_ACTION;
5083 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5084 if (tabularFeature[i].feature == s) {
5085 action = tabularFeature[i].action;
5089 if (action == Tabular::LAST_ACTION) {
5091 status.setUnknown(true);
5095 row_type sel_row_start = 0;
5096 row_type sel_row_end = 0;
5097 col_type sel_col_start = 0;
5098 col_type sel_col_end = 0;
5099 Tabular::ltType dummyltt;
5102 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5105 case Tabular::SET_PWIDTH:
5106 case Tabular::SET_MPWIDTH:
5107 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5108 case Tabular::SET_SPECIAL_COLUMN:
5109 case Tabular::SET_SPECIAL_MULTICOLUMN:
5110 case Tabular::APPEND_ROW:
5111 case Tabular::APPEND_COLUMN:
5112 case Tabular::DELETE_ROW:
5113 case Tabular::DELETE_COLUMN:
5114 case Tabular::COPY_ROW:
5115 case Tabular::COPY_COLUMN:
5116 case Tabular::SET_TOP_SPACE:
5117 case Tabular::SET_BOTTOM_SPACE:
5118 case Tabular::SET_INTERLINE_SPACE:
5122 case Tabular::SET_TABULAR_WIDTH:
5123 status.setEnabled(!tabular.rotate
5124 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5127 case Tabular::MOVE_COLUMN_RIGHT:
5128 case Tabular::MOVE_COLUMN_LEFT:
5129 case Tabular::MOVE_ROW_DOWN:
5130 case Tabular::MOVE_ROW_UP: {
5131 if (cur.selection()) {
5132 status.message(_("Selections not supported."));
5133 status.setEnabled(false);
5137 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
5138 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
5139 (action == Tabular::MOVE_COLUMN_LEFT &&
5140 tabular.cellColumn(cur.idx()) == 0) ||
5141 (action == Tabular::MOVE_ROW_DOWN &&
5142 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
5143 (action == Tabular::MOVE_ROW_UP &&
5144 tabular.cellRow(cur.idx()) == 0)) {
5145 status.setEnabled(false);
5149 if (action == Tabular::MOVE_COLUMN_RIGHT ||
5150 action == Tabular::MOVE_COLUMN_LEFT) {
5151 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
5152 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
5153 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
5154 status.message(_("Multi-column in current or"
5155 " destination column."));
5156 status.setEnabled(false);
5161 if (action == Tabular::MOVE_ROW_DOWN ||
5162 action == Tabular::MOVE_ROW_UP) {
5163 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
5164 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
5165 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
5166 status.message(_("Multi-row in current or"
5167 " destination row."));
5168 status.setEnabled(false);
5173 status.setEnabled(true);
5177 case Tabular::SET_DECIMAL_POINT:
5179 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5182 case Tabular::SET_MULTICOLUMN:
5183 case Tabular::UNSET_MULTICOLUMN:
5184 case Tabular::MULTICOLUMN:
5185 // If a row is set as longtable caption, it must not be allowed
5186 // to unset that this row is a multicolumn.
5187 status.setEnabled(sel_row_start == sel_row_end
5188 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5189 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5192 case Tabular::SET_MULTIROW:
5193 case Tabular::UNSET_MULTIROW:
5194 case Tabular::MULTIROW:
5195 // If a row is set as longtable caption, it must not be allowed
5196 // to unset that this row is a multirow.
5197 status.setEnabled(sel_col_start == sel_col_end
5198 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5199 status.setOnOff(tabular.isMultiRow(cur.idx()));
5202 case Tabular::SET_ALL_LINES:
5203 case Tabular::UNSET_ALL_LINES:
5204 case Tabular::SET_INNER_LINES:
5205 case Tabular::SET_BORDER_LINES:
5206 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5209 case Tabular::RESET_FORMAL_DEFAULT:
5210 status.setEnabled(tabular.use_booktabs);
5213 case Tabular::SET_LINE_TOP:
5214 case Tabular::SET_LINE_BOTTOM:
5215 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5218 case Tabular::SET_LINE_LEFT:
5219 case Tabular::SET_LINE_RIGHT:
5220 status.setEnabled(!tabular.use_booktabs
5221 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5224 case Tabular::SET_LTRIM_TOP:
5225 case Tabular::SET_RTRIM_TOP:
5226 status.setEnabled(tabular.use_booktabs
5227 && tabular.cellRow(cur.idx()) != 0
5228 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5231 case Tabular::SET_LTRIM_BOTTOM:
5232 case Tabular::SET_RTRIM_BOTTOM:
5233 status.setEnabled(tabular.use_booktabs
5234 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5235 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5238 case Tabular::TOGGLE_LINE_TOP:
5239 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5240 status.setOnOff(tabular.topLine(cur.idx()));
5243 case Tabular::TOGGLE_LINE_BOTTOM:
5244 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5245 status.setOnOff(tabular.bottomLine(cur.idx()));
5248 case Tabular::TOGGLE_LINE_LEFT:
5249 status.setEnabled(!tabular.use_booktabs
5250 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5251 status.setOnOff(tabular.leftLine(cur.idx()));
5254 case Tabular::TOGGLE_LINE_RIGHT:
5255 status.setEnabled(!tabular.use_booktabs
5256 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5257 status.setOnOff(tabular.rightLine(cur.idx()));
5260 case Tabular::TOGGLE_LTRIM_TOP:
5261 status.setEnabled(tabular.use_booktabs
5262 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5263 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5266 case Tabular::TOGGLE_RTRIM_TOP:
5267 status.setEnabled(tabular.use_booktabs
5268 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5269 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5272 case Tabular::TOGGLE_LTRIM_BOTTOM:
5273 status.setEnabled(tabular.use_booktabs
5274 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5275 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5278 case Tabular::TOGGLE_RTRIM_BOTTOM:
5279 status.setEnabled(tabular.use_booktabs
5280 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5281 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5284 // multirow cells only inherit the alignment of the column if the column has
5285 // no width, otherwise they are left-aligned
5286 // therefore allow always left but right and center only if there is no width
5287 case Tabular::M_ALIGN_LEFT:
5290 case Tabular::ALIGN_LEFT:
5291 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5294 case Tabular::M_ALIGN_RIGHT:
5297 case Tabular::ALIGN_RIGHT:
5298 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5299 && !tabular.getPWidth(cur.idx()).zero()));
5300 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5303 case Tabular::M_ALIGN_CENTER:
5306 case Tabular::ALIGN_CENTER:
5307 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5308 && !tabular.getPWidth(cur.idx()).zero()));
5309 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5312 case Tabular::ALIGN_BLOCK:
5313 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5314 && !tabular.isMultiRow(cur.idx()));
5315 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5318 case Tabular::ALIGN_DECIMAL:
5319 status.setEnabled(!tabular.isMultiRow(cur.idx())
5320 && !tabular.isMultiColumn(cur.idx()));
5321 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5324 case Tabular::M_VALIGN_TOP:
5327 case Tabular::VALIGN_TOP:
5328 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5329 && !tabular.isMultiRow(cur.idx()));
5331 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5334 case Tabular::M_VALIGN_BOTTOM:
5337 case Tabular::VALIGN_BOTTOM:
5338 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5339 && !tabular.isMultiRow(cur.idx()));
5341 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5344 case Tabular::M_VALIGN_MIDDLE:
5347 case Tabular::VALIGN_MIDDLE:
5348 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5349 && !tabular.isMultiRow(cur.idx()));
5351 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5354 case Tabular::SET_LONGTABULAR:
5355 case Tabular::TOGGLE_LONGTABULAR:
5356 // setting as longtable is not allowed when table is inside a float
5357 if (cur.innerInsetOfType(FLOAT_CODE) != 0
5358 || cur.innerInsetOfType(WRAP_CODE) != 0)
5359 status.setEnabled(false);
5361 status.setEnabled(true);
5362 status.setOnOff(tabular.is_long_tabular);
5365 case Tabular::UNSET_LONGTABULAR:
5366 status.setOnOff(!tabular.is_long_tabular);
5369 case Tabular::TOGGLE_ROTATE_TABULAR:
5370 case Tabular::SET_ROTATE_TABULAR:
5371 status.setOnOff(tabular.rotate != 0);
5374 case Tabular::TABULAR_VALIGN_TOP:
5375 status.setEnabled(tabular.tabular_width.zero());
5376 status.setOnOff(tabular.tabular_valignment
5377 == Tabular::LYX_VALIGN_TOP);
5379 case Tabular::TABULAR_VALIGN_MIDDLE:
5380 status.setEnabled(tabular.tabular_width.zero());
5381 status.setOnOff(tabular.tabular_valignment
5382 == Tabular::LYX_VALIGN_MIDDLE);
5384 case Tabular::TABULAR_VALIGN_BOTTOM:
5385 status.setEnabled(tabular.tabular_width.zero());
5386 status.setOnOff(tabular.tabular_valignment
5387 == Tabular::LYX_VALIGN_BOTTOM);
5390 case Tabular::LONGTABULAR_ALIGN_LEFT:
5391 status.setOnOff(tabular.longtabular_alignment
5392 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5394 case Tabular::LONGTABULAR_ALIGN_CENTER:
5395 status.setOnOff(tabular.longtabular_alignment
5396 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5398 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5399 status.setOnOff(tabular.longtabular_alignment
5400 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5403 case Tabular::UNSET_ROTATE_TABULAR:
5404 status.setOnOff(tabular.rotate == 0);
5407 case Tabular::TOGGLE_ROTATE_CELL:
5408 case Tabular::SET_ROTATE_CELL:
5409 status.setOnOff(!oneCellHasRotationState(false,
5410 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5413 case Tabular::UNSET_ROTATE_CELL:
5414 status.setOnOff(!oneCellHasRotationState(true,
5415 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5418 case Tabular::SET_USEBOX:
5419 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5422 // every row can only be one thing:
5423 // either a footer or header
5424 case Tabular::SET_LTFIRSTHEAD:
5425 status.setEnabled(sel_row_start == sel_row_end);
5426 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5429 case Tabular::UNSET_LTFIRSTHEAD:
5430 status.setEnabled(sel_row_start == sel_row_end);
5431 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5434 case Tabular::SET_LTHEAD:
5435 status.setEnabled(sel_row_start == sel_row_end);
5436 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5439 case Tabular::UNSET_LTHEAD:
5440 status.setEnabled(sel_row_start == sel_row_end);
5441 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5444 case Tabular::SET_LTFOOT:
5445 status.setEnabled(sel_row_start == sel_row_end);
5446 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5449 case Tabular::UNSET_LTFOOT:
5450 status.setEnabled(sel_row_start == sel_row_end);
5451 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5454 case Tabular::SET_LTLASTFOOT:
5455 status.setEnabled(sel_row_start == sel_row_end);
5456 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5459 case Tabular::UNSET_LTLASTFOOT:
5460 status.setEnabled(sel_row_start == sel_row_end);
5461 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5464 case Tabular::SET_LTNEWPAGE:
5465 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5467 case Tabular::UNSET_LTNEWPAGE:
5468 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5471 // only one row in head/firsthead/foot/lasthead can be the caption
5472 // and a multirow cannot be set as caption
5473 case Tabular::SET_LTCAPTION:
5474 status.setEnabled(sel_row_start == sel_row_end
5475 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5476 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5477 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5478 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5479 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5480 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5481 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5482 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5483 && !tabular.isMultiRow(sel_row_start));
5484 status.setOnOff(tabular.ltCaption(sel_row_start));
5487 case Tabular::UNSET_LTCAPTION:
5488 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5491 case Tabular::TOGGLE_LTCAPTION:
5492 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5493 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5494 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5495 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5496 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5497 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5498 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5499 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5500 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5501 status.setOnOff(tabular.ltCaption(sel_row_start));
5504 case Tabular::TOGGLE_BOOKTABS:
5505 case Tabular::SET_BOOKTABS:
5506 status.setOnOff(tabular.use_booktabs);
5509 case Tabular::UNSET_BOOKTABS:
5510 status.setOnOff(!tabular.use_booktabs);
5515 status.setEnabled(false);
5522 // function sets an object as defined in FuncStatus.h:
5523 // states OK, Unknown, Disabled, On, Off.
5524 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5525 FuncStatus & status) const
5527 switch (cmd.action()) {
5528 case LFUN_INSET_MODIFY:
5529 if (cmd.getArg(0) != "tabular")
5531 if (cmd.getArg(1) == "for-dialog") {
5532 // The dialog is asking the status of a command
5533 if (&cur.inset() != this)
5535 string action = cmd.getArg(2);
5536 string arg = cmd.getLongArg(3);
5537 return getFeatureStatus(cur, action, arg, status);
5539 // We always enable the lfun if it is coming from the dialog
5540 // because the dialog makes sure all the settings are valid,
5541 // even though the first argument might not be valid now.
5542 status.setEnabled(true);
5546 case LFUN_TABULAR_FEATURE: {
5547 if (&cur.inset() != this)
5549 string action = cmd.getArg(0);
5550 string arg = cmd.getLongArg(1);
5551 return getFeatureStatus(cur, action, arg, status);
5554 case LFUN_CAPTION_INSERT: {
5555 // caption is only allowed in caption cell of longtable
5556 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5557 status.setEnabled(false);
5560 // only standard caption is allowed
5561 string arg = cmd.getArg(0);
5562 if (!arg.empty() && arg != "Standard") {
5563 status.setEnabled(false);
5566 // check if there is already a caption
5567 bool have_caption = false;
5568 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5569 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5570 ParagraphList::const_iterator pend = itc.paragraphs().end();
5571 for (; pit != pend; ++pit) {
5572 InsetList::const_iterator it = pit->insetList().begin();
5573 InsetList::const_iterator end = pit->insetList().end();
5574 for (; it != end; ++it) {
5575 if (it->inset->lyxCode() == CAPTION_CODE) {
5576 have_caption = true;
5581 status.setEnabled(!have_caption);
5585 // These are only enabled inside tabular
5586 case LFUN_CELL_BACKWARD:
5587 case LFUN_CELL_FORWARD:
5588 status.setEnabled(true);
5591 // disable these with multiple cells selected
5592 case LFUN_INSET_INSERT:
5593 case LFUN_TABULAR_INSERT:
5594 case LFUN_TABULAR_STYLE_INSERT:
5595 case LFUN_FLEX_INSERT:
5596 case LFUN_FLOAT_INSERT:
5597 case LFUN_FLOAT_WIDE_INSERT:
5598 case LFUN_FOOTNOTE_INSERT:
5599 case LFUN_MARGINALNOTE_INSERT:
5600 case LFUN_MATH_INSERT:
5601 case LFUN_MATH_MODE:
5602 case LFUN_MATH_MUTATE:
5603 case LFUN_MATH_DISPLAY:
5604 case LFUN_NOTE_INSERT:
5605 case LFUN_ARGUMENT_INSERT:
5606 case LFUN_BOX_INSERT:
5607 case LFUN_BRANCH_INSERT:
5608 case LFUN_PHANTOM_INSERT:
5609 case LFUN_WRAP_INSERT:
5610 case LFUN_PREVIEW_INSERT:
5611 case LFUN_ERT_INSERT: {
5612 if (cur.selIsMultiCell()) {
5613 status.setEnabled(false);
5616 return cell(cur.idx())->getStatus(cur, cmd, status);
5619 // disable in non-fixed-width cells
5620 case LFUN_PARAGRAPH_BREAK:
5621 // multirow does not allow paragraph breaks
5622 if (tabular.isMultiRow(cur.idx())) {
5623 status.setEnabled(false);
5627 case LFUN_NEWLINE_INSERT:
5628 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
5629 && tabular.getPWidth(cur.idx()).zero()) {
5630 status.setEnabled(false);
5633 return cell(cur.idx())->getStatus(cur, cmd, status);
5635 case LFUN_NEWPAGE_INSERT:
5636 status.setEnabled(false);
5640 if (tabularStackDirty() && theClipboard().isInternal()) {
5641 if (cur.selIsMultiCell()) {
5644 getSelection(cur, rs, re, cs, ce);
5645 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5646 && paste_tabular->nrows() == re - rs + 1)
5647 status.setEnabled(true);
5649 status.setEnabled(false);
5650 status.message(_("Selection size should match clipboard content."));
5653 status.setEnabled(true);
5656 return cell(cur.idx())->getStatus(cur, cmd, status);
5658 case LFUN_INSET_SETTINGS:
5659 // relay this lfun to Inset, not to the cell.
5660 return Inset::getStatus(cur, cmd, status);
5663 // we try to handle this event in the insets dispatch function.
5664 return cell(cur.idx())->getStatus(cur, cmd, status);
5670 Inset::DisplayType InsetTabular::display() const
5672 if (tabular.is_long_tabular) {
5673 switch (tabular.longtabular_alignment) {
5674 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5676 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5678 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5688 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5690 tabular.latex(os, runparams);
5694 int InsetTabular::plaintext(odocstringstream & os,
5695 OutputParams const & runparams, size_t max_length) const
5697 os << '\n'; // output table on a new line
5698 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5699 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5700 return PLAINTEXT_NEWLINE;
5704 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5709 // FIXME: Why not pass a proper DocIterator here?
5711 // if the table is inside a float it doesn't need the informaltable
5712 // wrapper. Search for it.
5713 for (master = owner(); master; master = master->owner())
5714 if (master->lyxCode() == FLOAT_CODE)
5719 os << "<informaltable>";
5722 ret += tabular.docbook(os, runparams);
5724 os << "</informaltable>";
5731 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5733 return tabular.xhtml(xs, rp);
5737 void InsetTabular::validate(LaTeXFeatures & features) const
5739 tabular.validate(features);
5740 features.useInsetLayout(getLayout());
5744 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5746 return tabular.cellInset(idx);
5750 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5752 return tabular.cellInset(idx);
5756 void InsetTabular::cursorPos(BufferView const & bv,
5757 CursorSlice const & sl, bool boundary, int & x, int & y) const
5759 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5761 // y offset correction
5762 y += cellYPos(sl.idx());
5763 y += tabular.textVOffset(sl.idx());
5764 y += tabular.offsetVAlignment();
5766 // x offset correction
5767 x += cellXPos(sl.idx());
5768 x += tabular.textHOffset(sl.idx());
5769 x += ADD_TO_TABULAR_WIDTH;
5773 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5777 Inset const & inset = *tabular.cellInset(cell);
5778 Point o = bv.coordCache().getInsets().xy(&inset);
5779 int const xbeg = o.x_ - tabular.textHOffset(cell);
5780 int const xend = xbeg + tabular.cellWidth(cell);
5781 row_type const row = tabular.cellRow(cell);
5782 int const ybeg = o.y_ - tabular.rowAscent(row)
5783 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5784 int const yend = ybeg + tabular.cellHeight(cell);
5796 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5797 // << " ybeg=" << ybeg << " yend=" << yend
5798 // << " xx=" << xx << " yy=" << yy
5799 // << " dist=" << xx + yy << endl;
5804 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5806 //lyxerr << "InsetTabular::editXY: " << this << endl;
5808 cur.idx() = getNearestCell(cur.bv(), x, y);
5809 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5813 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5815 cur.idx() = getNearestCell(cur.bv(), x, y);
5816 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5820 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5822 idx_type idx_min = 0;
5823 int dist_min = numeric_limits<int>::max();
5824 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5825 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5826 int const d = dist(bv, i, x, y);
5837 int InsetTabular::cellYPos(idx_type const cell) const
5839 row_type row = tabular.cellRow(cell);
5841 for (row_type r = 0; r < row; ++r)
5842 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5843 + tabular.interRowSpace(r + 1);
5848 int InsetTabular::cellXPos(idx_type const cell) const
5850 col_type col = tabular.cellColumn(cell);
5852 for (col_type c = 0; c < col; ++c)
5853 lx += tabular.column_info[c].width;
5858 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5860 row_type const row = tabular.cellRow(cur.idx());
5861 col_type const col = tabular.cellColumn(cur.idx());
5863 if (isRightToLeft(cur)) {
5864 if (tabular.cellColumn(cur.idx()) == 0) {
5865 if (row == tabular.nrows() - 1)
5867 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5872 cur.idx() = tabular.getLastCellInRow(row - 1);
5874 cur.idx() = tabular.cellIndex(row, col - 1);
5877 if (tabular.isLastCell(cur.idx()))
5879 if (cur.idx() == tabular.getLastCellInRow(row))
5880 cur.idx() = tabular.cellIndex(row + 1, 0);
5882 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5883 cur.idx() = tabular.cellIndex(row, colnextcell);
5887 cur.boundary(false);
5889 if (cur.selIsMultiCell()) {
5890 cur.pit() = cur.lastpit();
5891 cur.pos() = cur.lastpos();
5898 // in visual mode, place cursor at extreme left or right
5900 switch(entry_from) {
5902 case ENTRY_DIRECTION_RIGHT:
5903 cur.posVisToRowExtremity(false /* !left */);
5905 case ENTRY_DIRECTION_LEFT:
5906 cur.posVisToRowExtremity(true /* left */);
5908 case ENTRY_DIRECTION_IGNORE:
5909 // nothing to do in this case
5913 cur.setCurrentFont();
5917 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5919 row_type const row = tabular.cellRow(cur.idx());
5920 col_type const col = tabular.cellColumn(cur.idx());
5922 if (isRightToLeft(cur)) {
5923 if (cur.idx() == tabular.getLastCellInRow(row)) {
5926 cur.idx() = tabular.getFirstCellInRow(row);
5927 cur.idx() = tabular.cellAbove(cur.idx());
5929 if (tabular.isLastCell(cur.idx()))
5931 if (cur.idx() == tabular.getLastCellInRow(row))
5932 cur.idx() = tabular.cellIndex(row + 1, 0);
5934 cur.idx() = tabular.cellIndex(row, col + 1);
5937 if (cur.idx() == 0) // first cell
5940 cur.idx() = tabular.getLastCellInRow(row - 1);
5942 cur.idx() = tabular.cellIndex(row, col - 1);
5945 if (cur.selIsMultiCell()) {
5946 cur.pit() = cur.lastpit();
5947 cur.pos() = cur.lastpos();
5951 cur.pit() = cur.lastpit();
5952 cur.pos() = cur.lastpos();
5954 // in visual mode, place cursor at extreme left or right
5956 switch(entry_from) {
5958 case ENTRY_DIRECTION_RIGHT:
5959 cur.posVisToRowExtremity(false /* !left */);
5961 case ENTRY_DIRECTION_LEFT:
5962 cur.posVisToRowExtremity(true /* left */);
5964 case ENTRY_DIRECTION_IGNORE:
5965 // nothing to do in this case
5969 cur.setCurrentFont();
5973 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5975 cur.recordUndoInset(this);
5977 istringstream is(argument);
5978 // limit the size of strings we read to avoid memory problems
5982 size_t safe_guard = 0;
5987 if (safe_guard > 1000) {
5988 LYXERR0("parameter max count reached!");
5992 Tabular::Feature action = Tabular::LAST_ACTION;
5995 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5996 if (s != tabularFeature[i].feature)
5998 action = tabularFeature[i].action;
6001 if (action == Tabular::LAST_ACTION) {
6002 LYXERR0("Feature not found " << s);
6006 if (tabularFeature[i].need_value)
6008 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6009 tabularFeatures(cur, action, val);
6014 static void checkLongtableSpecial(Tabular::ltType & ltt,
6015 string const & special, bool & flag)
6017 if (special == "dl_above") {
6020 } else if (special == "dl_below") {
6021 ltt.bottomDL = flag;
6023 } else if (special == "empty") {
6033 bool InsetTabular::oneCellHasRotationState(bool rotated,
6034 row_type row_start, row_type row_end,
6035 col_type col_start, col_type col_end) const
6037 for (row_type r = row_start; r <= row_end; ++r)
6038 for (col_type c = col_start; c <= col_end; ++c)
6040 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6043 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6050 void InsetTabular::tabularFeatures(Cursor & cur,
6051 Tabular::Feature feature, string const & value)
6053 col_type sel_col_start;
6054 col_type sel_col_end;
6055 row_type sel_row_start;
6056 row_type sel_row_end;
6057 bool setLines = false;
6058 bool setLinesInnerOnly = false;
6059 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6060 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6064 case Tabular::M_ALIGN_LEFT:
6065 case Tabular::ALIGN_LEFT:
6066 setAlign = LYX_ALIGN_LEFT;
6069 case Tabular::M_ALIGN_RIGHT:
6070 case Tabular::ALIGN_RIGHT:
6071 setAlign = LYX_ALIGN_RIGHT;
6074 case Tabular::M_ALIGN_CENTER:
6075 case Tabular::ALIGN_CENTER:
6076 setAlign = LYX_ALIGN_CENTER;
6079 case Tabular::ALIGN_BLOCK:
6080 setAlign = LYX_ALIGN_BLOCK;
6083 case Tabular::ALIGN_DECIMAL:
6084 setAlign = LYX_ALIGN_DECIMAL;
6087 case Tabular::M_VALIGN_TOP:
6088 case Tabular::VALIGN_TOP:
6089 setVAlign = Tabular::LYX_VALIGN_TOP;
6092 case Tabular::M_VALIGN_BOTTOM:
6093 case Tabular::VALIGN_BOTTOM:
6094 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6097 case Tabular::M_VALIGN_MIDDLE:
6098 case Tabular::VALIGN_MIDDLE:
6099 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6106 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6107 row_type const row = tabular.cellRow(cur.idx());
6108 col_type const column = tabular.cellColumn(cur.idx());
6110 Tabular::ltType ltt;
6114 case Tabular::SET_TABULAR_WIDTH:
6115 tabular.setTabularWidth(Length(value));
6118 case Tabular::SET_PWIDTH: {
6119 Length const len(value);
6120 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6121 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6123 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6124 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6129 case Tabular::SET_MPWIDTH:
6130 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6133 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6134 bool const varwidth = value == "on";
6135 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6136 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6140 case Tabular::SET_MROFFSET:
6141 tabular.setMROffset(cur, cur.idx(), Length(value));
6144 case Tabular::SET_SPECIAL_COLUMN:
6145 case Tabular::SET_SPECIAL_MULTICOLUMN:
6146 if (value == "none")
6147 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6149 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6152 case Tabular::APPEND_ROW:
6153 // append the row into the tabular
6154 tabular.appendRow(row);
6157 case Tabular::APPEND_COLUMN:
6158 // append the column into the tabular
6159 tabular.appendColumn(column);
6160 cur.idx() = tabular.cellIndex(row, column);
6163 case Tabular::DELETE_ROW:
6164 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6165 for (col_type c = 0; c < tabular.ncols(); c++) {
6166 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6167 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6168 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6169 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6173 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6174 tabular.deleteRow(sel_row_start);
6175 if (sel_row_start >= tabular.nrows())
6177 cur.idx() = tabular.cellIndex(sel_row_start, column);
6180 cur.selection(false);
6183 case Tabular::DELETE_COLUMN:
6184 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6185 for (row_type r = 0; r < tabular.nrows(); r++)
6186 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6187 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6190 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6191 for (row_type r = 0; r < tabular.nrows(); r++)
6192 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6193 tabular.leftLine(tabular.cellIndex(r, 0)));
6196 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6197 tabular.deleteColumn(sel_col_start);
6198 if (sel_col_start >= tabular.ncols())
6200 cur.idx() = tabular.cellIndex(row, sel_col_start);
6203 cur.selection(false);
6206 case Tabular::COPY_ROW:
6207 tabular.copyRow(row);
6210 case Tabular::COPY_COLUMN:
6211 tabular.copyColumn(column);
6212 cur.idx() = tabular.cellIndex(row, column);
6215 case Tabular::MOVE_COLUMN_RIGHT:
6216 tabular.moveColumn(column, Tabular::RIGHT);
6217 cur.idx() = tabular.cellIndex(row, column + 1);
6220 case Tabular::MOVE_COLUMN_LEFT:
6221 tabular.moveColumn(column, Tabular::LEFT);
6222 cur.idx() = tabular.cellIndex(row, column - 1);
6225 case Tabular::MOVE_ROW_DOWN:
6226 tabular.moveRow(row, Tabular::DOWN);
6227 cur.idx() = tabular.cellIndex(row + 1, column);
6230 case Tabular::MOVE_ROW_UP:
6231 tabular.moveRow(row, Tabular::UP);
6232 cur.idx() = tabular.cellIndex(row - 1, column);
6235 case Tabular::SET_LINE_TOP:
6236 case Tabular::TOGGLE_LINE_TOP: {
6237 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6238 ? (value == "true") : !tabular.topLine(cur.idx());
6239 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6240 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6241 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6245 case Tabular::SET_LINE_BOTTOM:
6246 case Tabular::TOGGLE_LINE_BOTTOM: {
6247 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6248 ? (value == "true") : !tabular.bottomLine(cur.idx());
6249 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6250 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6251 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6255 case Tabular::SET_LTRIM_TOP:
6256 case Tabular::TOGGLE_LTRIM_TOP: {
6257 bool l = (feature == Tabular::SET_LTRIM_TOP)
6258 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6259 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6260 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6261 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6265 case Tabular::SET_RTRIM_TOP:
6266 case Tabular::TOGGLE_RTRIM_TOP: {
6267 bool l = (feature == Tabular::SET_RTRIM_TOP)
6268 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6269 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6270 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6271 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6275 case Tabular::SET_LTRIM_BOTTOM:
6276 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6277 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6278 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6279 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6280 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6281 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6285 case Tabular::SET_RTRIM_BOTTOM:
6286 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6287 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6288 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6289 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6290 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6291 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6295 case Tabular::SET_LINE_LEFT:
6296 case Tabular::TOGGLE_LINE_LEFT: {
6297 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6298 ? (value == "true") : !tabular.leftLine(cur.idx());
6299 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6300 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6301 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6305 case Tabular::SET_LINE_RIGHT:
6306 case Tabular::TOGGLE_LINE_RIGHT: {
6307 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6308 ? (value == "true") : !tabular.rightLine(cur.idx());
6309 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6310 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6311 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6315 case Tabular::M_ALIGN_LEFT:
6316 case Tabular::M_ALIGN_RIGHT:
6317 case Tabular::M_ALIGN_CENTER:
6318 case Tabular::ALIGN_LEFT:
6319 case Tabular::ALIGN_RIGHT:
6320 case Tabular::ALIGN_CENTER:
6321 case Tabular::ALIGN_BLOCK:
6322 case Tabular::ALIGN_DECIMAL:
6323 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6324 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6325 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6326 !tabular.getPWidth(c).zero());
6329 case Tabular::M_VALIGN_TOP:
6330 case Tabular::M_VALIGN_BOTTOM:
6331 case Tabular::M_VALIGN_MIDDLE:
6334 case Tabular::VALIGN_TOP:
6335 case Tabular::VALIGN_BOTTOM:
6336 case Tabular::VALIGN_MIDDLE:
6337 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6338 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6339 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6342 case Tabular::SET_MULTICOLUMN: {
6343 if (!cur.selection()) {
6344 // just multicol for one single cell
6345 // check whether we are completely in a multicol
6346 if (!tabular.isMultiColumn(cur.idx()))
6347 tabular.setMultiColumn(cur, cur.idx(), 1,
6348 tabular.rightLine(cur.idx()));
6351 // we have a selection so this means we just add all these
6352 // cells to form a multicolumn cell
6353 idx_type const s_start = cur.selBegin().idx();
6354 row_type const col_start = tabular.cellColumn(s_start);
6355 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6356 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6357 tabular.rightLine(cur.selEnd().idx()));
6360 cur.selection(false);
6364 case Tabular::UNSET_MULTICOLUMN: {
6365 if (!cur.selection()) {
6366 if (tabular.isMultiColumn(cur.idx()))
6367 tabular.unsetMultiColumn(cur.idx());
6372 case Tabular::MULTICOLUMN: {
6373 if (!cur.selection()) {
6374 if (tabular.isMultiColumn(cur.idx()))
6375 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6377 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6381 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6382 row_type const r = sel_row_start;
6383 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6384 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6387 // If the selection contains at least one singlecol cell
6388 // or multiple multicol cells,
6389 // we assume the user will merge is to a single multicol
6391 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6393 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6397 case Tabular::SET_MULTIROW: {
6398 if (!cur.selection()) {
6399 // just multirow for one single cell
6400 // check whether we are completely in a multirow
6401 if (!tabular.isMultiRow(cur.idx()))
6402 tabular.setMultiRow(cur, cur.idx(), 1,
6403 tabular.bottomLine(cur.idx()),
6404 tabular.getAlignment(cur.idx()));
6407 // we have a selection so this means we just add all this
6408 // cells to form a multirow cell
6409 idx_type const s_start = cur.selBegin().idx();
6410 row_type const row_start = tabular.cellRow(s_start);
6411 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6412 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6413 tabular.bottomLine(cur.selEnd().idx()),
6414 tabular.getAlignment(cur.selEnd().idx()));
6417 cur.selection(false);
6421 case Tabular::UNSET_MULTIROW: {
6422 if (!cur.selection()) {
6423 if (tabular.isMultiRow(cur.idx()))
6424 tabular.unsetMultiRow(cur.idx());
6429 case Tabular::MULTIROW: {
6430 if (!cur.selection()) {
6431 if (tabular.isMultiRow(cur.idx()))
6432 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6434 tabularFeatures(cur, Tabular::SET_MULTIROW);
6438 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6439 col_type const c = sel_col_start;
6440 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6441 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6444 // If the selection contains at least one singlerow cell
6445 // or multiple multirow cells,
6446 // we assume the user will merge is to a single multirow
6448 tabularFeatures(cur, Tabular::SET_MULTIROW);
6450 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6454 case Tabular::SET_INNER_LINES:
6455 setLinesInnerOnly = true;
6457 case Tabular::SET_ALL_LINES:
6460 case Tabular::UNSET_ALL_LINES:
6461 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6462 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6463 idx_type const cell = tabular.cellIndex(r, c);
6464 if (!setLinesInnerOnly || r != sel_row_start)
6465 tabular.setTopLine(cell, setLines);
6466 if ((!setLinesInnerOnly || r != sel_row_end)
6467 && (!setLines || r == sel_row_end))
6468 tabular.setBottomLine(cell, setLines);
6469 if ((!setLinesInnerOnly || c != sel_col_end)
6470 && (!setLines || c == sel_col_end))
6471 tabular.setRightLine(cell, setLines);
6472 if ((!setLinesInnerOnly || c != sel_col_start))
6473 tabular.setLeftLine(cell, setLines);
6477 case Tabular::RESET_FORMAL_DEFAULT:
6478 for (row_type r = 0; r < tabular.nrows(); ++r) {
6479 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6480 for (col_type c = 0; c < tabular.ncols(); ++c) {
6481 idx_type const cell = tabular.cellIndex(r, c);
6482 tabular.setTopLine(cell, head_or_foot);
6483 tabular.setBottomLine(cell, head_or_foot);
6488 case Tabular::SET_BORDER_LINES:
6489 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6490 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6491 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6493 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6494 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6495 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6499 case Tabular::TOGGLE_LONGTABULAR:
6500 if (tabular.is_long_tabular)
6501 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6503 tabular.is_long_tabular = true;
6506 case Tabular::SET_LONGTABULAR:
6507 tabular.is_long_tabular = true;
6510 case Tabular::UNSET_LONGTABULAR:
6511 for (row_type r = 0; r < tabular.nrows(); ++r) {
6512 if (tabular.ltCaption(r)) {
6513 cur.idx() = tabular.cellIndex(r, 0);
6516 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6519 tabular.is_long_tabular = false;
6522 case Tabular::SET_ROTATE_TABULAR:
6523 tabular.rotate = convert<int>(value);
6526 case Tabular::UNSET_ROTATE_TABULAR:
6530 case Tabular::TOGGLE_ROTATE_TABULAR:
6531 // when pressing the rotate button we default to 90° rotation
6532 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6535 case Tabular::TABULAR_VALIGN_TOP:
6536 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6539 case Tabular::TABULAR_VALIGN_MIDDLE:
6540 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6543 case Tabular::TABULAR_VALIGN_BOTTOM:
6544 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6547 case Tabular::LONGTABULAR_ALIGN_LEFT:
6548 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6551 case Tabular::LONGTABULAR_ALIGN_CENTER:
6552 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6555 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6556 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6559 case Tabular::SET_ROTATE_CELL:
6560 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6561 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6562 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6565 case Tabular::UNSET_ROTATE_CELL:
6566 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6567 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6568 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6571 case Tabular::TOGGLE_ROTATE_CELL:
6573 bool oneNotRotated = oneCellHasRotationState(false,
6574 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6576 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6577 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6578 // when pressing the rotate cell button we default to 90° rotation
6580 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6582 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6587 case Tabular::SET_USEBOX: {
6588 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6589 if (val == tabular.getUsebox(cur.idx()))
6590 val = Tabular::BOX_NONE;
6591 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6592 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6593 tabular.setUsebox(tabular.cellIndex(r, c), val);
6597 case Tabular::UNSET_LTFIRSTHEAD:
6600 case Tabular::SET_LTFIRSTHEAD:
6601 tabular.getRowOfLTFirstHead(row, ltt);
6602 checkLongtableSpecial(ltt, value, flag);
6603 tabular.setLTHead(row, flag, ltt, true);
6606 case Tabular::UNSET_LTHEAD:
6609 case Tabular::SET_LTHEAD:
6610 tabular.getRowOfLTHead(row, ltt);
6611 checkLongtableSpecial(ltt, value, flag);
6612 tabular.setLTHead(row, flag, ltt, false);
6615 case Tabular::UNSET_LTFOOT:
6618 case Tabular::SET_LTFOOT:
6619 tabular.getRowOfLTFoot(row, ltt);
6620 checkLongtableSpecial(ltt, value, flag);
6621 tabular.setLTFoot(row, flag, ltt, false);
6624 case Tabular::UNSET_LTLASTFOOT:
6627 case Tabular::SET_LTLASTFOOT:
6628 tabular.getRowOfLTLastFoot(row, ltt);
6629 checkLongtableSpecial(ltt, value, flag);
6630 tabular.setLTFoot(row, flag, ltt, true);
6633 case Tabular::UNSET_LTNEWPAGE:
6636 case Tabular::SET_LTNEWPAGE:
6637 tabular.setLTNewPage(row, flag);
6640 case Tabular::SET_LTCAPTION: {
6641 if (tabular.ltCaption(row))
6643 cur.idx() = tabular.setLTCaption(cur, row, true);
6646 cur.selection(false);
6647 // If a row is set as caption, then also insert
6648 // a caption. Otherwise the LaTeX output is broken.
6649 // Select cell if it is non-empty
6650 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6651 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6652 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6656 case Tabular::UNSET_LTCAPTION: {
6657 if (!tabular.ltCaption(row))
6659 cur.idx() = tabular.setLTCaption(cur, row, false);
6662 cur.selection(false);
6663 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6664 if (lyx::getStatus(fr).enabled())
6669 case Tabular::TOGGLE_LTCAPTION: {
6670 if (tabular.ltCaption(row))
6671 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6673 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6677 case Tabular::TOGGLE_BOOKTABS:
6678 tabular.use_booktabs = !tabular.use_booktabs;
6681 case Tabular::SET_BOOKTABS:
6682 tabular.use_booktabs = true;
6685 case Tabular::UNSET_BOOKTABS:
6686 tabular.use_booktabs = false;
6689 case Tabular::SET_TOP_SPACE: {
6691 if (value == "default")
6692 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6693 tabular.row_info[r].top_space_default = true;
6694 else if (value == "none")
6695 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6696 tabular.row_info[r].top_space_default = false;
6697 tabular.row_info[r].top_space = len;
6699 else if (isValidLength(value, &len))
6700 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6701 tabular.row_info[r].top_space_default = false;
6702 tabular.row_info[r].top_space = len;
6707 case Tabular::SET_BOTTOM_SPACE: {
6709 if (value == "default")
6710 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6711 tabular.row_info[r].bottom_space_default = true;
6712 else if (value == "none")
6713 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6714 tabular.row_info[r].bottom_space_default = false;
6715 tabular.row_info[r].bottom_space = len;
6717 else if (isValidLength(value, &len))
6718 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6719 tabular.row_info[r].bottom_space_default = false;
6720 tabular.row_info[r].bottom_space = len;
6725 case Tabular::SET_INTERLINE_SPACE: {
6727 if (value == "default")
6728 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6729 tabular.row_info[r].interline_space_default = true;
6730 else if (value == "none")
6731 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6732 tabular.row_info[r].interline_space_default = false;
6733 tabular.row_info[r].interline_space = len;
6735 else if (isValidLength(value, &len))
6736 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6737 tabular.row_info[r].interline_space_default = false;
6738 tabular.row_info[r].interline_space = len;
6743 case Tabular::SET_DECIMAL_POINT:
6744 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6745 tabular.column_info[c].decimal_point = from_utf8(value);
6748 // dummy stuff just to avoid warnings
6749 case Tabular::LAST_ACTION:
6755 bool InsetTabular::copySelection(Cursor & cur)
6757 if (!cur.selection())
6762 getSelection(cur, rs, re, cs, ce);
6764 paste_tabular.reset(new Tabular(tabular));
6766 for (row_type r = 0; r < rs; ++r)
6767 paste_tabular->deleteRow(0);
6769 row_type const rows = re - rs + 1;
6770 while (paste_tabular->nrows() > rows)
6771 paste_tabular->deleteRow(rows);
6773 for (col_type c = 0; c < cs; ++c)
6774 paste_tabular->deleteColumn(0);
6776 col_type const columns = ce - cs + 1;
6777 while (paste_tabular->ncols() > columns)
6778 paste_tabular->deleteColumn(columns);
6780 paste_tabular->setBuffer(tabular.buffer());
6782 odocstringstream os;
6783 OutputParams const runparams(0);
6784 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6785 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6786 cap::copySelection(cur, os.str());
6788 // mark tabular stack dirty
6789 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6790 // when we (hopefully) have a one-for-all paste mechanism.
6791 // This must be called after cap::copySelection.
6792 dirtyTabularStack(true);
6798 bool InsetTabular::pasteClipboard(Cursor & cur)
6802 col_type actcol = tabular.cellColumn(cur.idx());
6803 row_type actrow = tabular.cellRow(cur.idx());
6805 if (cur.selIsMultiCell()) {
6808 getSelection(cur, actrow, re, actcol, ce);
6811 for (row_type r1 = 0, r2 = actrow;
6812 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6814 for (col_type c1 = 0, c2 = actcol;
6815 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6817 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6818 tabular.isPartOfMultiColumn(r2, c2))
6820 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6824 if (tabular.isPartOfMultiColumn(r2, c2)) {
6828 shared_ptr<InsetTableCell> inset(
6829 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6830 tabular.setCellInset(r2, c2, inset);
6831 // FIXME?: why do we need to do this explicitly? (EL)
6832 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6834 // FIXME: change tracking (MG)
6835 inset->setChange(Change(buffer().params().track_changes ?
6836 Change::INSERTED : Change::UNCHANGED));
6845 void InsetTabular::cutSelection(Cursor & cur)
6847 if (!cur.selection())
6852 getSelection(cur, rs, re, cs, ce);
6853 for (row_type r = rs; r <= re; ++r) {
6854 for (col_type c = cs; c <= ce; ++c) {
6855 shared_ptr<InsetTableCell> t
6856 = cell(tabular.cellIndex(r, c));
6857 if (buffer().params().track_changes)
6858 // FIXME: Change tracking (MG)
6859 t->setChange(Change(Change::DELETED));
6865 // cursor position might be invalid now
6866 if (cur.pit() > cur.lastpit())
6867 cur.pit() = cur.lastpit();
6868 if (cur.pos() > cur.lastpos())
6869 cur.pos() = cur.lastpos();
6870 cur.clearSelection();
6874 bool InsetTabular::isRightToLeft(Cursor & cur) const
6876 // LASSERT: It might be better to abandon this Buffer.
6877 LASSERT(cur.depth() > 1, return false);
6878 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6879 pos_type const parentpos = cur[cur.depth() - 2].pos();
6880 return parentpar.getFontSettings(buffer().params(),
6881 parentpos).language()->rightToLeft();
6885 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6888 LASSERT(stidx <= enidx, return docstring());
6890 col_type const col1 = tabular.cellColumn(stidx);
6891 col_type const col2 = tabular.cellColumn(enidx);
6892 row_type const row1 = tabular.cellRow(stidx);
6893 row_type const row2 = tabular.cellRow(enidx);
6895 for (col_type col = col1; col <= col2; col++)
6896 for (row_type row = row1; row <= row2; row++) {
6901 retval += tabular.cellInset(row, col)->asString(intoInsets);
6907 void InsetTabular::getSelection(Cursor & cur,
6908 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6910 CursorSlice const & beg = cur.selBegin();
6911 CursorSlice const & end = cur.selEnd();
6912 cs = tabular.cellColumn(beg.idx());
6913 ce = tabular.cellColumn(end.idx());
6917 rs = tabular.cellRow(beg.idx());
6918 re = tabular.cellRow(end.idx());
6924 Text * InsetTabular::getText(int idx) const
6926 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6930 void InsetTabular::setChange(Change const & change)
6932 for (idx_type idx = 0; idx < nargs(); ++idx)
6933 cell(idx)->setChange(change);
6937 void InsetTabular::acceptChanges()
6939 for (idx_type idx = 0; idx < nargs(); ++idx)
6940 cell(idx)->acceptChanges();
6944 void InsetTabular::rejectChanges()
6946 for (idx_type idx = 0; idx < nargs(); ++idx)
6947 cell(idx)->rejectChanges();
6951 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6953 return tabular.getPWidth(cell).zero();
6957 bool InsetTabular::forcePlainLayout(idx_type cell) const
6959 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
6963 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6966 if (buf.length() <= 0)
6971 col_type maxCols = 1;
6972 size_t const len = buf.length();
6976 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6984 maxCols = max(cols, maxCols);
6990 maxCols = max(cols, maxCols);
6996 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6997 loctab = paste_tabular.get();
6998 dirtyTabularStack(true);
7001 cell = bv.cursor().idx();
7002 ocol = tabular.cellColumn(cell);
7003 row = tabular.cellRow(cell);
7007 idx_type cells = loctab->numberofcells;
7010 rows = loctab->nrows();
7011 col_type columns = loctab->ncols();
7014 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7020 // append column if necessary
7021 if (cols == columns) {
7022 loctab->appendColumn(cols - 1);
7023 columns = loctab->ncols();
7024 cells = loctab->numberofcells;
7027 if (cols < columns) {
7028 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7029 Font const font = bv.textMetrics(&inset->text()).
7031 inset->setText(buf.substr(op, p - op), font,
7032 buffer().params().track_changes);
7038 // we can only set this if we are not too far right
7039 if (cols < columns) {
7040 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7041 Font const font = bv.textMetrics(&inset->text()).
7043 inset->setText(buf.substr(op, p - op), font,
7044 buffer().params().track_changes);
7048 // append row if necessary
7049 if (row == rows && p < len - 1) {
7050 loctab->appendRow(row - 1);
7051 rows = loctab->nrows();
7052 cells = loctab->numberofcells;
7055 cell = loctab->cellIndex(row, cols);
7061 // check for the last cell if there is no trailing '\n'
7062 if (cell < cells && op < len) {
7063 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7064 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7065 inset->setText(buf.substr(op, len - op), font,
7066 buffer().params().track_changes);
7072 void InsetTabular::addPreview(DocIterator const & inset_pos,
7073 PreviewLoader & loader) const
7075 DocIterator cell_pos = inset_pos;
7077 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7078 for (row_type r = 0; r < tabular.nrows(); ++r) {
7079 for (col_type c = 0; c < tabular.ncols(); ++c) {
7080 cell_pos.top().idx() = tabular.cellIndex(r, c);
7081 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7087 bool InsetTabular::completionSupported(Cursor const & cur) const
7089 Cursor const & bvCur = cur.bv().cursor();
7090 if (&bvCur.inset() != this)
7092 return cur.text()->completionSupported(cur);
7096 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7098 return completionSupported(cur);
7102 bool InsetTabular::automaticInlineCompletion() const
7104 return lyxrc.completion_inline_text;
7108 bool InsetTabular::automaticPopupCompletion() const
7110 return lyxrc.completion_popup_text;
7114 bool InsetTabular::showCompletionCursor() const
7116 return lyxrc.completion_cursor_text;
7120 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7122 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
7126 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7128 if (!completionSupported(cur))
7130 return cur.text()->completionPrefix(cur);
7134 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7136 if (!completionSupported(cur))
7139 return cur.text()->insertCompletion(cur, s, finished);
7143 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7144 Dimension & dim) const
7146 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7147 tm.completionPosAndDim(cur, x, y, dim);
7151 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7153 istringstream data(in);
7155 lex.setStream(data);
7162 if (!lex || token != "tabular") {
7163 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7167 // This is part of the inset proper that is usually swallowed
7168 // by Buffer::readInset
7170 if (!lex || token != "Tabular") {
7171 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7179 string InsetTabular::params2string(InsetTabular const & inset)
7182 data << "tabular" << ' ';
7184 data << "\\end_inset\n";
7189 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7191 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
7192 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
7193 if (!tabular.isPartOfMultiColumn(r,c) &&
7194 !tabular.isPartOfMultiRow(r,c))
7197 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7198 ParagraphList::iterator it = parlist.begin();
7199 ParagraphList::iterator const en = parlist.end();
7200 for (; it != en; ++it)
7201 it->setLayout(dc.plainLayout());