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, Change::Type & change)
456 // set the length to be zero() as default as this it should be if not
457 // in the file format.
458 change = Change::UNCHANGED;
460 if (getTokenValue(str, token, tmp)) {
461 if (tmp == "inserted") {
462 change = Change::INSERTED;
464 } else if (tmp == "deleted") {
465 change = Change::DELETED;
473 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
478 if (!getTokenValue(str, token, tmp))
480 if (tmp == "default") {
484 return isValidLength(tmp, &len);
488 void l_getline(istream & is, string & str)
491 while (str.empty()) {
493 if (!str.empty() && str[str.length() - 1] == '\r')
494 str.erase(str.length() - 1);
499 string const write_attribute(string const & name, T const & t)
501 string const s = tostr(t);
502 return s.empty() ? s : " " + name + "=\"" + s + "\"";
506 string const write_attribute(string const & name, string const & t)
508 return t.empty() ? t : " " + name + "=\"" + t + "\"";
513 string const write_attribute(string const & name, docstring const & t)
515 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
520 string const write_attribute(string const & name, bool const & b)
522 // we write only true attribute values so we remove a bit of the
523 // file format bloat for tabulars.
524 return b ? write_attribute(name, convert<string>(b)) : string();
529 string const write_attribute(string const & name, int const & i)
531 // we write only true attribute values so we remove a bit of the
532 // file format bloat for tabulars.
533 return i ? write_attribute(name, convert<string>(i)) : string();
538 string const write_attribute(string const & name, Tabular::idx_type const & i)
540 // we write only true attribute values so we remove a bit of the
541 // file format bloat for tabulars.
542 return i ? write_attribute(name, convert<string>(i)) : string();
547 string const write_attribute(string const & name, Length const & value)
549 // we write only the value if we really have one same reson as above.
550 return value.zero() ? string() : write_attribute(name, value.asString());
554 string const write_attribute(string const & name, Change::Type const & type)
556 if (type == Change::INSERTED)
557 return write_attribute(name, from_ascii("inserted"));
558 else if (type == Change::DELETED)
559 return write_attribute(name, from_ascii("deleted"));
566 string const featureAsString(Tabular::Feature action)
568 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
569 if (tabularFeature[i].action == action)
570 return tabularFeature[i].feature;
576 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
578 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
579 for (; dit; dit.forwardChar())
580 if (dit.inTexted() && dit.depth() == 1
581 && dit.paragraph().find(align_d, false, false, dit.pos()))
588 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
590 InsetTableCell tail = InsetTableCell(head);
591 DocIterator const dit = separatorPos(&head, align_d);
594 pit_type const psize = head.paragraphs().front().size();
595 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
596 tail.paragraphs().front().eraseChars(0,
597 dit.pos() < psize ? dit.pos() + 1 : psize, false);
604 /////////////////////////////////////////////////////////////////////
608 /////////////////////////////////////////////////////////////////////
611 Tabular::CellData::CellData(Buffer * buf)
614 multicolumn(Tabular::CELL_NORMAL),
615 multirow(Tabular::CELL_NORMAL),
616 alignment(LYX_ALIGN_CENTER),
617 valignment(LYX_VALIGN_TOP),
625 top_line_rtrimmed(false),
626 top_line_ltrimmed(false),
627 bottom_line_rtrimmed(false),
628 bottom_line_ltrimmed(false),
631 inset(new InsetTableCell(buf))
633 inset->setBuffer(*buf);
637 Tabular::CellData::CellData(CellData const & cs)
640 multicolumn(cs.multicolumn),
641 multirow(cs.multirow),
642 mroffset(cs.mroffset),
643 alignment(cs.alignment),
644 valignment(cs.valignment),
645 decimal_hoffset(cs.decimal_hoffset),
646 decimal_width(cs.decimal_width),
648 top_line(cs.top_line),
649 bottom_line(cs.bottom_line),
650 left_line(cs.left_line),
651 right_line(cs.right_line),
652 top_line_rtrimmed(cs.top_line_rtrimmed),
653 top_line_ltrimmed(cs.top_line_ltrimmed),
654 bottom_line_rtrimmed(cs.bottom_line_rtrimmed),
655 bottom_line_ltrimmed(cs.bottom_line_ltrimmed),
658 align_special(cs.align_special),
660 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
664 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
670 multicolumn = cs.multicolumn;
671 multirow = cs.multirow;
672 mroffset = cs.mroffset;
673 alignment = cs.alignment;
674 valignment = cs.valignment;
675 decimal_hoffset = cs.decimal_hoffset;
676 decimal_width = cs.decimal_width;
677 voffset = cs.voffset;
678 top_line = cs.top_line;
679 bottom_line = cs.bottom_line;
680 left_line = cs.left_line;
681 right_line = cs.right_line;
682 top_line_rtrimmed = cs.top_line_rtrimmed;
683 top_line_ltrimmed = cs.top_line_ltrimmed;
684 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
685 bottom_line_ltrimmed = cs.bottom_line_ltrimmed;
688 align_special = cs.align_special;
689 p_width = cs.p_width;
690 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
694 Tabular::RowData::RowData()
697 top_space_default(false),
698 bottom_space_default(false),
699 interline_space_default(false),
706 change(Change::UNCHANGED)
710 Tabular::ColumnData::ColumnData()
711 : alignment(LYX_ALIGN_CENTER),
712 valignment(LYX_VALIGN_TOP),
715 change(Change::UNCHANGED)
720 Tabular::ltType::ltType()
721 : set(false), topDL(false),
727 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
729 init(buffer, rows_arg, columns_arg);
733 void Tabular::setBuffer(Buffer & buffer)
736 for (row_type i = 0; i < nrows(); ++i)
737 for (col_type j = 0; j < ncols(); ++j)
738 cell_info[i][j].inset->setBuffer(*buffer_);
742 // activates all lines and sets all widths to 0
743 void Tabular::init(Buffer * buf, row_type rows_arg,
744 col_type columns_arg)
747 row_info = row_vector(rows_arg);
748 column_info = column_vector(columns_arg);
749 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
750 row_info.reserve(10);
751 column_info.reserve(10);
752 cell_info.reserve(100);
754 is_long_tabular = false;
755 tabular_valignment = LYX_VALIGN_MIDDLE;
756 tabular_width = Length();
757 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
759 use_booktabs = false;
760 // set silly default lines
761 for (row_type r = 0; r < nrows(); ++r)
762 for (col_type c = 0; c < ncols(); ++c) {
763 cell_info[r][c].inset->setBuffer(*buffer_);
764 cell_info[r][c].top_line = true;
765 cell_info[r][c].left_line = true;
766 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
767 cell_info[r][c].right_line = c == ncols() - 1;
772 void Tabular::deleteRow(row_type const row, bool const force)
774 // Not allowed to delete last row
778 bool const ct = force ? false : buffer().params().track_changes;
780 for (col_type c = 0; c < ncols(); ++c) {
781 // mark track changes
783 cell_info[row][c].inset->setChange(Change(Change::DELETED));
784 // Care about multirow cells
785 if (row + 1 < nrows() &&
786 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
787 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
788 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
792 row_info[row].change = Change::DELETED;
794 row_info.erase(row_info.begin() + row);
795 cell_info.erase(cell_info.begin() + row);
801 void Tabular::copyRow(row_type const row)
803 insertRow(row, true);
807 void Tabular::appendRow(row_type row)
809 insertRow(row, false);
813 void Tabular::insertRow(row_type const row, bool copy)
815 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
816 cell_info.insert(cell_info.begin() + row + 1,
817 cell_vector(0, CellData(buffer_)));
819 for (col_type c = 0; c < ncols(); ++c) {
820 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
821 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
822 if (buffer().params().track_changes)
823 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
824 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
825 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
829 for (col_type c = 0; c < ncols(); ++c) {
830 if (isPartOfMultiRow(row, c))
832 // inherit line settings
833 idx_type const i = cellIndex(row + 1, c);
834 idx_type const j = cellIndex(row, c);
835 setLeftLine(i, leftLine(j));
836 setRightLine(i, rightLine(j));
837 setTopLine(i, topLine(j));
838 if (topLine(j) && bottomLine(j)) {
839 setBottomLine(i, true);
840 setBottomLine(j, false);
842 // mark track changes
843 if (buffer().params().track_changes)
844 cellInfo(i).inset->setChange(Change(Change::INSERTED));
846 if (buffer().params().track_changes)
847 row_info[row + 1].change = Change::INSERTED;
851 void Tabular::moveColumn(col_type col, ColDirection direction)
853 if (direction == Tabular::LEFT)
856 std::swap(column_info[col], column_info[col + 1]);
858 for (row_type r = 0; r < nrows(); ++r) {
859 std::swap(cell_info[r][col], cell_info[r][col + 1]);
860 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
861 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
863 idx_type const i = cellIndex(r, col);
864 idx_type const j = cellIndex(r, col + 1);
865 if (buffer().params().track_changes) {
866 cellInfo(i).inset->setChange(Change(Change::INSERTED));
867 cellInfo(j).inset->setChange(Change(Change::INSERTED));
874 void Tabular::moveRow(row_type row, RowDirection direction)
876 if (direction == Tabular::UP)
879 std::swap(row_info[row], row_info[row + 1]);
881 for (col_type c = 0; c < ncols(); ++c) {
882 std::swap(cell_info[row][c], cell_info[row + 1][c]);
883 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
884 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
886 idx_type const i = cellIndex(row, c);
887 idx_type const j = cellIndex(row + 1, c);
888 if (buffer().params().track_changes) {
889 cellInfo(i).inset->setChange(Change(Change::INSERTED));
890 cellInfo(j).inset->setChange(Change(Change::INSERTED));
897 void Tabular::deleteColumn(col_type const col, bool const force)
899 // Not allowed to delete last column
903 bool const ct = force ? false : buffer().params().track_changes;
905 for (row_type r = 0; r < nrows(); ++r) {
906 // mark track changes
908 cell_info[r][col].inset->setChange(Change(Change::DELETED));
909 // Care about multicolumn cells
910 if (col + 1 < ncols() &&
911 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
912 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
913 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
916 cell_info[r].erase(cell_info[r].begin() + col);
919 column_info[col].change = Change::DELETED;
921 column_info.erase(column_info.begin() + col);
926 void Tabular::copyColumn(col_type const col)
928 insertColumn(col, true);
932 void Tabular::appendColumn(col_type col)
934 insertColumn(col, false);
938 void Tabular::insertColumn(col_type const col, bool copy)
940 BufferParams const & bp = buffer().params();
941 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
943 for (row_type r = 0; r < nrows(); ++r) {
944 cell_info[r].insert(cell_info[r].begin() + col + 1,
945 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
946 if (bp.track_changes)
947 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
948 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
949 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
952 for (row_type r = 0; r < nrows(); ++r) {
953 // inherit line settings
954 idx_type const i = cellIndex(r, col + 1);
955 idx_type const j = cellIndex(r, col);
956 setBottomLine(i, bottomLine(j));
957 setTopLine(i, topLine(j));
958 setLeftLine(i, leftLine(j));
959 setRightLine(i, rightLine(j));
960 if (rightLine(i) && rightLine(j)) {
961 setRightLine(j, false);
963 if (buffer().params().track_changes)
964 cellInfo(i).inset->setChange(Change(Change::INSERTED));
966 if (buffer().params().track_changes)
967 column_info[col + 1].change = Change::INSERTED;
971 void Tabular::updateIndexes()
976 for (row_type row = 0; row < nrows(); ++row)
977 for (col_type column = 0; column < ncols(); ++column) {
978 if (!isPartOfMultiColumn(row, column)
979 && !isPartOfMultiRow(row, column))
981 if (isPartOfMultiRow(row, column))
982 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
984 cell_info[row][column].cellno = numberofcells - 1;
987 rowofcell.resize(numberofcells);
988 columnofcell.resize(numberofcells);
990 // reset column and row of cells and update their width and alignment
991 for (row_type row = 0; row < nrows(); ++row)
992 for (col_type column = 0; column < ncols(); ++column) {
993 if (isPartOfMultiColumn(row, column)) {
994 cell_info[row][column].inset->toggleMultiCol(true);
997 cell_info[row][column].inset->toggleMultiCol(false);
998 // columnofcell needs to be called before setting width and aligment
999 // multirow cells inherit the width from the column width
1000 if (!isPartOfMultiRow(row, column)) {
1001 columnofcell[i] = column;
1004 setFixedWidth(row, column);
1005 if (isPartOfMultiRow(row, column)) {
1006 cell_info[row][column].inset->toggleMultiRow(true);
1009 cell_info[row][column].inset->toggleMultiRow(false);
1010 cell_info[row][column].inset->setContentAlignment(
1011 getAlignment(cellIndex(row, column)));
1017 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
1019 idx_type result = 0;
1020 for (col_type c = 0; c < ncols(); ++c)
1021 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1027 bool Tabular::topLine(idx_type const cell) const
1029 return cellInfo(cell).top_line;
1033 bool Tabular::bottomLine(idx_type const cell) const
1035 return cellInfo(cell).bottom_line;
1039 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1041 if (use_booktabs && !ignore_bt)
1043 return cellInfo(cell).left_line;
1047 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1049 if (use_booktabs && !ignore_bt)
1051 return cellInfo(cell).right_line;
1055 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1058 return make_pair(false, false);
1059 return make_pair(cellInfo(cell).top_line_ltrimmed,
1060 cellInfo(cell).top_line_rtrimmed);
1064 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1067 return make_pair(false, false);
1068 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1069 cellInfo(cell).bottom_line_rtrimmed);
1073 int Tabular::interRowSpace(row_type row) const
1075 if (!row || row >= nrows())
1078 int const interline_space = row_info[row - 1].interline_space_default ?
1079 default_line_space :
1080 row_info[row - 1].interline_space.inPixels(width());
1081 if (rowTopLine(row) && rowBottomLine(row - 1))
1082 return interline_space + WIDTH_OF_LINE;
1083 return interline_space;
1087 int Tabular::interColumnSpace(idx_type cell) const
1089 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1090 if (rightLine(cell) && nextcol < ncols()
1091 && leftLine(cellIndex(cellRow(cell), nextcol)))
1092 return WIDTH_OF_LINE;
1097 int Tabular::cellWidth(idx_type cell) const
1100 col_type const span = columnSpan(cell);
1101 col_type const col = cellColumn(cell);
1102 for(col_type c = col; c < col + span ; ++c)
1103 w += column_info[c].width;
1108 int Tabular::cellHeight(idx_type cell) const
1110 row_type const span = rowSpan(cell);
1111 row_type const row = cellRow(cell);
1113 for(row_type r = row; r < row + span ; ++r) {
1114 h += rowAscent(r) + rowDescent(r);
1115 if (r != row + span - 1)
1116 h += interRowSpace(r + 1);
1123 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1125 vector<int> max_dwidth(ncols(), 0);
1126 // collect max. fixed width of column
1127 map<col_type, int> max_pwidth;
1128 // collect max. variable width of column
1129 map<col_type, int> max_width;
1131 for(col_type c = 0; c < ncols(); ++c)
1132 for(row_type r = 0; r < nrows(); ++r) {
1133 idx_type const i = cellIndex(r, c);
1134 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1135 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1136 if (!getPWidth(i).zero())
1137 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1138 else if (!column_info[c].varwidth)
1139 max_width[c] = max(max_width[c], cell_info[r][c].width);
1142 // If we have a fixed tabular width, we take this into account
1143 Length tab_width = tabular_width;
1144 bool const tabularx = hasVarwidthColumn();
1145 if (tabularx && tab_width.zero())
1146 // If no tabular width is specified with X columns,
1147 // we use 100% colwidth
1148 tab_width = Length(100, Length::PCW);
1150 if (!tab_width.zero()) {
1151 restwidth = mi.base.inPixels(tab_width);
1152 // Substract the fixed widths from the table width
1153 for (auto const w : max_pwidth)
1154 restwidth -= w.second;
1157 // If we have a fixed width, distribute the available table width
1158 // (minus the fixed widths) to the variable-width columns
1160 int restcols = ncols() - max_pwidth.size();
1161 if ((restwidth > 0) && (restcols != 0))
1162 vcolwidth = restwidth / restcols;
1164 // Now consider that some variable width columns exceed the vcolwidth
1165 if (vcolwidth > 0) {
1166 bool changed = false;
1167 for (auto const w : max_width) {
1168 if (tabularx || w.second > vcolwidth) {
1170 restwidth -= w.second;
1174 if (changed && restwidth > 0)
1175 vcolwidth = restwidth / restcols;
1178 bool update = false;
1179 // for each col get max of single col cells
1180 for(col_type c = 0; c < ncols(); ++c) {
1182 for(row_type r = 0; r < nrows(); ++r) {
1183 idx_type const i = cellIndex(r, c);
1184 if (columnSpan(i) == 1) {
1185 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1186 && cell_info[r][c].decimal_width != 0)
1187 new_width = max(new_width, cellInfo(i).width
1188 + max_dwidth[c] - cellInfo(i).decimal_width);
1189 else if (getPWidth(i).zero() && vcolwidth > 0) {
1190 if (tabularx && !column_info[c].varwidth)
1191 new_width = max(new_width, cellInfo(i).width);
1193 new_width = vcolwidth;
1195 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1197 new_width = max(new_width, cellInfo(i).width);
1201 if (column_info[c].width != new_width) {
1202 column_info[c].width = new_width;
1203 // Do not trigger update when no space is left for variable
1204 // columns, as this will loop
1205 update = tab_width.zero() || restwidth > 0;
1208 // update col widths to fit merged cells
1209 for(col_type c = 0; c < ncols(); ++c)
1210 for(row_type r = 0; r < nrows(); ++r) {
1211 idx_type const i = cellIndex(r, c);
1212 int const span = columnSpan(i);
1213 if (span == 1 || c > cellColumn(i))
1217 for(col_type j = c; j < c + span ; ++j)
1218 old_width += column_info[j].width;
1220 if (cellInfo(i).width > old_width) {
1221 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1222 // Do not trigger update when no space is left for variable
1223 // columns, as this will loop
1224 update = tab_width.zero() || restwidth > 0;
1232 int Tabular::width() const
1235 for (col_type c = 0; c < ncols(); ++c)
1236 width += column_info[c].width;
1241 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1244 col_type const col = cellColumn(cell);
1245 // set alignment for the whole row if we are not in a multicolumn cell,
1246 // exclude possible multicolumn cells in the row
1247 if (!isMultiColumn(cell)) {
1248 for (row_type r = 0; r < nrows(); ++r) {
1249 // only if the column has no width the multirow inherits the
1250 // alignment of the column, otherwise it is left aligned
1251 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1252 && !isMultiColumn(cellIndex(r, col))) {
1253 cell_info[r][col].alignment = align;
1254 cell_info[r][col].inset->setContentAlignment(align);
1256 if ((isMultiRow(cellIndex(r, col)) && has_width)
1257 && !isMultiColumn(cellIndex(r, col))) {
1258 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1259 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1262 column_info[col].alignment = align;
1263 docstring & dpoint = column_info[col].decimal_point;
1264 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1265 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1266 dpoint = tlang->decimalSeparator();
1269 cellInfo(cell).alignment = align;
1270 cellInset(cell)->setContentAlignment(align);
1275 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1278 if (!isMultiColumn(cell) || onlycolumn)
1279 column_info[cellColumn(cell)].valignment = align;
1281 cellInfo(cell).valignment = align;
1288 * Allow line and paragraph breaks for fixed width multicol/multirow cells
1289 * or disallow them, merge cell paragraphs and reset layout to standard
1290 * for variable width multicol cells.
1292 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1293 bool const fixedWidth, bool const multicol,
1294 bool const multirow)
1296 inset->toggleFixedWidth(fixedWidth);
1297 if (!multirow && (fixedWidth || !multicol))
1300 // merge all paragraphs to one
1301 BufferParams const & bp = cur.bv().buffer().params();
1302 while (inset->paragraphs().size() > 1)
1303 mergeParagraph(bp, inset->paragraphs(), 0);
1305 // This is relevant for multirows
1310 ParagraphList::iterator pit = inset->paragraphs().begin();
1311 for (; pit != inset->paragraphs().end(); ++pit) {
1312 for (pos_type j = 0; j != pit->size(); ++j) {
1313 if (pit->isNewline(j))
1314 pit->eraseChar(j, bp.track_changes);
1320 // undo information has already been recorded
1321 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1322 bp.documentClass().plainLayoutName());
1329 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1330 Length const & width)
1332 col_type const c = cellColumn(cell);
1334 column_info[c].p_width = width;
1335 // reset the vertical alignment to top if the fixed width
1336 // is removed or zero because only fixed width columns can
1337 // have a vertical alignment
1338 if (column_info[c].p_width.zero())
1339 column_info[c].valignment = LYX_VALIGN_TOP;
1340 for (row_type r = 0; r < nrows(); ++r) {
1341 idx_type const cidx = cellIndex(r, c);
1342 // because of multicolumns
1343 toggleFixedWidth(cur, cellInset(cidx).get(),
1344 !getPWidth(cidx).zero(), isMultiColumn(cidx),
1346 if (isMultiRow(cidx))
1347 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1349 // cur can become invalid after paragraphs were merged
1354 bool Tabular::setFixedWidth(row_type r, col_type c)
1356 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1357 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1358 || (multicol && !cell_info[r][c].p_width.zero());
1359 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1364 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1365 Length const & width)
1367 if (!isMultiColumn(cell))
1370 cellInfo(cell).p_width = width;
1371 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1372 isMultiColumn(cell), isMultiRow(cell));
1373 // cur can become invalid after paragraphs were merged
1379 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1381 column_info[cellColumn(cell)].varwidth = varwidth;
1386 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1388 cellInfo(cell).mroffset = mroffset;
1393 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1394 Tabular::Feature what)
1396 if (what == SET_SPECIAL_MULTICOLUMN)
1397 cellInfo(cell).align_special = special;
1399 column_info[cellColumn(cell)].align_special = special;
1403 void Tabular::setTopLine(idx_type i, bool line)
1405 cellInfo(i).top_line = line;
1409 void Tabular::setBottomLine(idx_type i, bool line)
1411 cellInfo(i).bottom_line = line;
1415 void Tabular::setTopLineLTrim(idx_type i, bool val)
1417 cellInfo(i).top_line_ltrimmed = val;
1421 void Tabular::setTopLineRTrim(idx_type i, bool val)
1423 cellInfo(i).top_line_rtrimmed = val;
1427 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1429 cellInfo(i).bottom_line_ltrimmed = val;
1433 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1435 cellInfo(i).bottom_line_rtrimmed = val;
1439 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1441 setTopLineLTrim(i, trim.first);
1442 setTopLineRTrim(i, trim.second);
1445 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1447 setBottomLineLTrim(i, trim.first);
1448 setBottomLineRTrim(i, trim.second);
1451 void Tabular::setLeftLine(idx_type cell, bool line)
1453 cellInfo(cell).left_line = line;
1457 void Tabular::setRightLine(idx_type cell, bool line)
1459 cellInfo(cell).right_line = line;
1462 bool Tabular::rowTopLine(row_type r) const
1464 bool all_rows_set = true;
1465 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1466 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1467 return all_rows_set;
1471 bool Tabular::rowBottomLine(row_type r) const
1473 bool all_rows_set = true;
1474 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1475 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1476 return all_rows_set;
1480 bool Tabular::columnLeftLine(col_type c) const
1487 for (row_type r = 0; r < nrows(); ++r) {
1488 idx_type const i = cellIndex(r, c);
1489 if (c == cellColumn(i)) {
1491 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1492 if (cellInfo(i).left_line || right)
1496 return 2 * nrows_left >= total;
1500 bool Tabular::columnRightLine(col_type c) const
1505 int nrows_right = 0;
1507 for (row_type r = 0; r < nrows(); ++r) {
1508 idx_type i = cellIndex(r, c);
1509 if (c == cellColumn(i) + columnSpan(i) - 1) {
1511 bool left = (c + 1 < ncols()
1512 && cellInfo(cellIndex(r, c + 1)).left_line)
1513 || c + 1 == ncols();
1514 if (cellInfo(i).right_line && left)
1518 return 2 * nrows_right >= total;
1522 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1524 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1525 return cellInfo(cell).alignment;
1527 return column_info[cellColumn(cell)].alignment;
1532 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1534 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1535 return cellInfo(cell).valignment;
1536 return column_info[cellColumn(cell)].valignment;
1540 int Tabular::offsetVAlignment() const
1542 // for top-alignment the first horizontal table line must be exactly at
1543 // the position of the base line of the surrounding text line
1544 // for bottom alignment, the same is for the last table line
1545 int offset_valign = 0;
1546 switch (tabular_valignment) {
1547 case Tabular::LYX_VALIGN_BOTTOM:
1548 offset_valign = rowAscent(0) - height();
1550 case Tabular::LYX_VALIGN_MIDDLE:
1551 offset_valign = (- height()) / 2 + rowAscent(0);
1553 case Tabular::LYX_VALIGN_TOP:
1554 offset_valign = rowAscent(0);
1557 return offset_valign;
1561 Length const Tabular::getPWidth(idx_type cell) const
1563 if (isMultiColumn(cell))
1564 return cellInfo(cell).p_width;
1565 return column_info[cellColumn(cell)].p_width;
1569 Length const Tabular::getMROffset(idx_type cell) const
1571 return cellInfo(cell).mroffset;
1575 int Tabular::textHOffset(idx_type cell) const
1577 // the LaTeX Way :-(
1578 int x = WIDTH_OF_LINE;
1580 int const w = cellWidth(cell) - cellInfo(cell).width;
1582 switch (getAlignment(cell)) {
1583 case LYX_ALIGN_CENTER:
1586 case LYX_ALIGN_RIGHT:
1589 case LYX_ALIGN_DECIMAL: {
1590 // we center when no decimal point
1591 if (cellInfo(cell).decimal_width == 0) {
1595 col_type const c = cellColumn(cell);
1596 int max_dhoffset = 0;
1597 for(row_type r = 0; r < row_info.size() ; ++r) {
1598 idx_type const i = cellIndex(r, c);
1599 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1600 && cellInfo(i).decimal_width != 0)
1601 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1603 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1606 // LYX_ALIGN_LEFT: nothing :-)
1614 int Tabular::textVOffset(idx_type cell) const
1616 int voffset = cellInfo(cell).voffset;
1617 if (isMultiRow(cell)) {
1618 row_type const row = cellRow(cell);
1619 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1625 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1628 idx_type const numcells = numberOfCellsInRow(row);
1629 // we check against numcells to make sure we do not crash if all the
1630 // cells are multirow (bug #7535), but in that case our return value
1631 // is really invalid, i.e., it is NOT the first cell in the row. but
1632 // i do not know what to do here. (rgh)
1633 while (c < numcells - 1
1634 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1636 return cell_info[row][c].cellno;
1640 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1642 col_type c = ncols() - 1;
1643 // of course we check against 0 so we don't crash. but we have the same
1644 // problem as in the previous routine: if all the cells are part of a
1645 // multirow or part of a multi column, then our return value is invalid.
1647 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1648 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1650 return cell_info[row][c].cellno;
1654 Tabular::row_type Tabular::cellRow(idx_type cell) const
1656 if (cell >= numberofcells)
1660 return rowofcell[cell];
1664 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1666 if (cell >= numberofcells)
1670 return columnofcell[cell];
1674 void Tabular::write(ostream & os) const
1678 << write_attribute("version", 3)
1679 << write_attribute("rows", nrows())
1680 << write_attribute("columns", ncols())
1682 // global longtable options
1684 << write_attribute("rotate", rotate)
1685 << write_attribute("booktabs", use_booktabs)
1686 << write_attribute("islongtable", is_long_tabular)
1687 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1688 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1689 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1690 << write_attribute("headTopDL", endhead.topDL)
1691 << write_attribute("headBottomDL", endhead.bottomDL)
1692 << write_attribute("footTopDL", endfoot.topDL)
1693 << write_attribute("footBottomDL", endfoot.bottomDL)
1694 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1695 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1696 << write_attribute("lastFootEmpty", endlastfoot.empty);
1697 // longtables cannot be aligned vertically
1698 if (!is_long_tabular)
1699 os << write_attribute("tabularvalignment", tabular_valignment);
1700 os << write_attribute("tabularwidth", tabular_width);
1701 if (is_long_tabular)
1702 os << write_attribute("longtabularalignment", longtabular_alignment);
1704 for (col_type c = 0; c < ncols(); ++c) {
1706 << write_attribute("alignment", column_info[c].alignment);
1707 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1708 os << write_attribute("decimal_point", column_info[c].decimal_point);
1709 os << write_attribute("change", column_info[c].change)
1710 << write_attribute("valignment", column_info[c].valignment)
1711 << write_attribute("width", column_info[c].p_width.asString())
1712 << write_attribute("varwidth", column_info[c].varwidth)
1713 << write_attribute("special", column_info[c].align_special)
1716 for (row_type r = 0; r < nrows(); ++r) {
1717 static const string def("default");
1719 if (row_info[r].top_space_default)
1720 os << write_attribute("topspace", def);
1722 os << write_attribute("topspace", row_info[r].top_space);
1723 if (row_info[r].bottom_space_default)
1724 os << write_attribute("bottomspace", def);
1726 os << write_attribute("bottomspace", row_info[r].bottom_space);
1727 if (row_info[r].interline_space_default)
1728 os << write_attribute("interlinespace", def);
1730 os << write_attribute("interlinespace", row_info[r].interline_space);
1731 os << write_attribute("change", row_info[r].change)
1732 << write_attribute("endhead", row_info[r].endhead)
1733 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1734 << write_attribute("endfoot", row_info[r].endfoot)
1735 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1736 << write_attribute("newpage", row_info[r].newpage)
1737 << write_attribute("caption", row_info[r].caption)
1739 for (col_type c = 0; c < ncols(); ++c) {
1741 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1742 << write_attribute("multirow", cell_info[r][c].multirow)
1743 << write_attribute("mroffset", cell_info[r][c].mroffset)
1744 << write_attribute("alignment", cell_info[r][c].alignment)
1745 << write_attribute("valignment", cell_info[r][c].valignment)
1746 << write_attribute("topline", cell_info[r][c].top_line)
1747 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1748 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1749 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1750 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1751 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1752 << write_attribute("leftline", cell_info[r][c].left_line)
1753 << write_attribute("rightline", cell_info[r][c].right_line)
1754 << write_attribute("rotate", cell_info[r][c].rotate)
1755 << write_attribute("usebox", cell_info[r][c].usebox)
1756 << write_attribute("width", cell_info[r][c].p_width)
1757 << write_attribute("special", cell_info[r][c].align_special)
1759 os << "\\begin_inset ";
1760 cell_info[r][c].inset->write(os);
1761 os << "\n\\end_inset\n"
1763 // FIXME This can be removed again once the mystery
1764 // crash has been resolved.
1769 os << "</lyxtabular>\n";
1773 void Tabular::read(Lexer & lex)
1776 istream & is = lex.getStream();
1778 l_getline(is, line);
1779 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1780 LASSERT(false, return);
1784 if (!getTokenValue(line, "version", version))
1786 LATTEST(version >= 2);
1789 if (!getTokenValue(line, "rows", rows_arg))
1792 if (!getTokenValue(line, "columns", columns_arg))
1794 init(buffer_, rows_arg, columns_arg);
1795 l_getline(is, line);
1796 if (!prefixIs(line, "<features")) {
1797 lyxerr << "Wrong tabular format (expected <features ...> got"
1798 << line << ')' << endl;
1801 getTokenValue(line, "rotate", rotate);
1802 getTokenValue(line, "booktabs", use_booktabs);
1803 getTokenValue(line, "islongtable", is_long_tabular);
1804 getTokenValue(line, "tabularvalignment", tabular_valignment);
1805 getTokenValue(line, "tabularwidth", tabular_width);
1806 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1807 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1808 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1809 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1810 getTokenValue(line, "headTopDL", endhead.topDL);
1811 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1812 getTokenValue(line, "footTopDL", endfoot.topDL);
1813 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1814 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1815 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1816 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1818 for (col_type c = 0; c < ncols(); ++c) {
1820 if (!prefixIs(line,"<column")) {
1821 lyxerr << "Wrong tabular format (expected <column ...> got"
1822 << line << ')' << endl;
1825 getTokenValue(line, "alignment", column_info[c].alignment);
1826 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1827 getTokenValue(line, "valignment", column_info[c].valignment);
1828 getTokenValue(line, "width", column_info[c].p_width);
1829 getTokenValue(line, "special", column_info[c].align_special);
1830 getTokenValue(line, "varwidth", column_info[c].varwidth);
1831 getTokenValue(line, "change", column_info[c].change);
1834 for (row_type i = 0; i < nrows(); ++i) {
1835 l_getline(is, line);
1836 if (!prefixIs(line, "<row")) {
1837 lyxerr << "Wrong tabular format (expected <row ...> got"
1838 << line << ')' << endl;
1841 getTokenValue(line, "topspace", row_info[i].top_space,
1842 row_info[i].top_space_default);
1843 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1844 row_info[i].bottom_space_default);
1845 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1846 row_info[i].interline_space_default);
1847 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1848 getTokenValue(line, "endhead", row_info[i].endhead);
1849 getTokenValue(line, "endfoot", row_info[i].endfoot);
1850 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1851 getTokenValue(line, "newpage", row_info[i].newpage);
1852 getTokenValue(line, "caption", row_info[i].caption);
1853 getTokenValue(line, "change", row_info[i].change);
1854 for (col_type j = 0; j < ncols(); ++j) {
1855 l_getline(is, line);
1856 if (!prefixIs(line, "<cell")) {
1857 lyxerr << "Wrong tabular format (expected <cell ...> got"
1858 << line << ')' << endl;
1861 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1862 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1863 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1864 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1865 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1866 getTokenValue(line, "topline", cell_info[i][j].top_line);
1867 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1868 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1869 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1870 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1871 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1872 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1873 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1874 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1875 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1876 getTokenValue(line, "width", cell_info[i][j].p_width);
1878 getTokenValue(line, "special", cell_info[i][j].align_special);
1879 l_getline(is, line);
1880 if (prefixIs(line, "\\begin_inset")) {
1881 cell_info[i][j].inset->setBuffer(*buffer_);
1882 cell_info[i][j].inset->read(lex);
1883 l_getline(is, line);
1885 if (!prefixIs(line, "</cell>")) {
1886 lyxerr << "Wrong tabular format (expected </cell> got"
1887 << line << ')' << endl;
1891 l_getline(is, line);
1892 if (!prefixIs(line, "</row>")) {
1893 lyxerr << "Wrong tabular format (expected </row> got"
1894 << line << ')' << endl;
1898 while (!prefixIs(line, "</lyxtabular>")) {
1899 l_getline(is, line);
1905 bool Tabular::isMultiColumn(idx_type cell) const
1907 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1908 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1912 bool Tabular::hasMultiColumn(col_type c) const
1914 for (row_type r = 0; r < nrows(); ++r) {
1915 if (isMultiColumn(cellIndex(r, c)))
1922 bool Tabular::hasVarwidthColumn() const
1924 for (col_type c = 0; c < ncols(); ++c) {
1925 if (column_info[c].varwidth)
1932 bool Tabular::isVTypeColumn(col_type c) const
1934 for (row_type r = 0; r < nrows(); ++r) {
1935 idx_type idx = cellIndex(r, c);
1936 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1943 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1945 return cell_info[cellRow(cell)][cellColumn(cell)];
1949 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1951 return cell_info[cellRow(cell)][cellColumn(cell)];
1955 Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
1956 bool const right_border)
1958 idx_type const col = cellColumn(cell);
1959 idx_type const row = cellRow(cell);
1960 for (idx_type i = 0; i < number; ++i)
1961 unsetMultiRow(cellIndex(row, col + i));
1963 // unsetting of multirow may have invalidated cell index
1964 cell = cellIndex(row, col);
1965 CellData & cs = cellInfo(cell);
1966 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1967 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1968 cs.alignment = column_info[col].alignment;
1969 setRightLine(cell, right_border);
1970 // non-fixed width multicolumns cannot have multiple paragraphs
1971 if (getPWidth(cell).zero()) {
1972 toggleFixedWidth(cur, cellInset(cell).get(),
1973 !getPWidth(cell).zero(), isMultiColumn(cell),
1975 // cur can become invalid after paragraphs were merged
1979 idx_type lastcell = cellIndex(row, col + number - 1);
1980 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1981 CellData & cs1 = cellInfo(cell + i);
1982 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1983 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1991 bool Tabular::isMultiRow(idx_type cell) const
1993 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1994 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1997 bool Tabular::hasMultiRow(row_type r) const
1999 for (col_type c = 0; c < ncols(); ++c) {
2000 if (isMultiRow(cellIndex(r, c)))
2006 Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2007 bool const bottom_border,
2008 LyXAlignment const halign)
2010 idx_type const col = cellColumn(cell);
2011 idx_type const row = cellRow(cell);
2012 for (idx_type i = 0; i < number; ++i)
2013 unsetMultiColumn(cellIndex(row + i, col));
2015 // unsetting of multirow may have invalidated cell index
2016 cell = cellIndex(row, col);
2017 CellData & cs = cellInfo(cell);
2018 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2019 cs.valignment = LYX_VALIGN_MIDDLE;
2020 // the horizontal alignment of multirow cells can only
2021 // be changed for the whole table row,
2022 // support changing this only for the multirow cell can be done via
2024 if (getPWidth(cell).zero())
2025 cs.alignment = halign;
2027 cs.alignment = LYX_ALIGN_LEFT;
2029 // Multirows cannot have multiple paragraphs
2030 if (getPWidth(cell).zero()) {
2031 toggleFixedWidth(cur, cellInset(cell).get(),
2032 !getPWidth(cell).zero(),
2033 isMultiColumn(cell), isMultiRow(cell));
2034 // cur can become invalid after paragraphs were merged
2038 // set the bottom line of the last selected cell
2039 setBottomLine(cell, bottom_border);
2041 for (idx_type i = 1; i < number; ++i) {
2042 CellData & cs1 = cell_info[row + i][col];
2043 cs1.multirow = CELL_PART_OF_MULTIROW;
2044 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2052 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
2054 row_type const row = cellRow(cell);
2055 col_type const col = cellColumn(cell);
2057 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2064 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
2066 col_type const column = cellColumn(cell);
2067 col_type row = cellRow(cell) + 1;
2068 while (row < nrows() && isPartOfMultiRow(row, column))
2071 return row - cellRow(cell);
2075 void Tabular::unsetMultiColumn(idx_type cell)
2077 if (!isMultiColumn(cell))
2080 row_type const row = cellRow(cell);
2081 col_type const col = cellColumn(cell);
2082 row_type const span = columnSpan(cell);
2083 for (col_type c = 0; c < span; ++c) {
2084 // in the table dialog the lines are set in every case
2085 // when unsetting a multicolumn this leads to an additional right
2086 // line for every cell that was part of the former multicolumn cell,
2087 // except if the cell is in the last column
2088 // therefore remove this line
2089 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2090 && (col + c) < (col + span - 1))
2091 cell_info[row][col + c].right_line = false;
2092 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2098 void Tabular::unsetMultiRow(idx_type cell)
2100 if (!isMultiRow(cell))
2103 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2104 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2105 row_type const row = cellRow(cell);
2106 col_type const col = cellColumn(cell);
2107 row_type const span = rowSpan(cell);
2108 for (row_type r = 0; r < span; ++r)
2109 cell_info[row + r][col].multirow = CELL_NORMAL;
2114 void Tabular::setRotateCell(idx_type cell, int value)
2116 cellInfo(cell).rotate = value;
2120 int Tabular::getRotateCell(idx_type cell) const
2122 return cellInfo(cell).rotate;
2126 bool Tabular::needRotating() const
2128 if (rotate && !is_long_tabular)
2130 for (row_type r = 0; r < nrows(); ++r)
2131 for (col_type c = 0; c < ncols(); ++c)
2132 if (cell_info[r][c].rotate != 0)
2138 bool Tabular::isLastCell(idx_type cell) const
2140 if (cell + 1 < numberofcells)
2146 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2148 if (cellRow(cell) == 0)
2151 col_type const col = cellColumn(cell);
2152 row_type r = cellRow(cell) - 1;
2153 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2156 return cell_info[r][col].cellno;
2160 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2162 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2163 if (nextrow < nrows())
2164 return cell_info[nextrow][cellColumn(cell)].cellno;
2169 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2171 LASSERT(column != npos && column < ncols(), column = 0);
2172 LASSERT(row != npos && row < nrows(), row = 0);
2173 return cell_info[row][column].cellno;
2177 void Tabular::setUsebox(idx_type cell, BoxType type)
2179 cellInfo(cell).usebox = type;
2183 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2185 if (getRotateCell(cell) == 0
2186 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2187 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2189 if (cellInfo(cell).usebox > 1)
2190 return cellInfo(cell).usebox;
2191 return useBox(cell);
2196 // This are functions used for the longtable support
2198 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2204 row_info[row].endfirsthead = flag;
2208 row_info[row].endhead = flag;
2213 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2216 hd.set = haveLTHead();
2217 return row_info[row].endhead;
2221 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2224 hd.set = haveLTFirstHead();
2225 return row_info[row].endfirsthead;
2229 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2235 row_info[row].endlastfoot = flag;
2239 row_info[row].endfoot = flag;
2244 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2247 fd.set = haveLTFoot();
2248 return row_info[row].endfoot;
2252 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2255 fd.set = haveLTLastFoot();
2256 return row_info[row].endlastfoot;
2260 void Tabular::setLTNewPage(row_type row, bool what)
2262 row_info[row].newpage = what;
2266 bool Tabular::getLTNewPage(row_type row) const
2268 return row_info[row].newpage;
2272 bool Tabular::haveLTHead(bool withcaptions) const
2274 if (!is_long_tabular)
2276 for (row_type i = 0; i < nrows(); ++i)
2277 if (row_info[i].endhead &&
2278 (withcaptions || !row_info[i].caption))
2284 bool Tabular::haveLTFirstHead(bool withcaptions) const
2286 if (!is_long_tabular || endfirsthead.empty)
2288 for (row_type r = 0; r < nrows(); ++r)
2289 if (row_info[r].endfirsthead &&
2290 (withcaptions || !row_info[r].caption))
2296 bool Tabular::haveLTFoot(bool withcaptions) const
2298 if (!is_long_tabular)
2300 for (row_type r = 0; r < nrows(); ++r)
2301 if (row_info[r].endfoot &&
2302 (withcaptions || !row_info[r].caption))
2308 bool Tabular::haveLTLastFoot(bool withcaptions) const
2310 if (!is_long_tabular || endlastfoot.empty)
2312 for (row_type r = 0; r < nrows(); ++r)
2313 if (row_info[r].endlastfoot &&
2314 (withcaptions || !row_info[r].caption))
2320 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2322 idx_type i = getFirstCellInRow(row);
2324 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2325 setTopLine(i, false);
2326 setBottomLine(i, false);
2327 setLeftLine(i, false);
2328 setRightLine(i, false);
2329 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2330 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2331 setLTHead(row, true, endfirsthead, true);
2332 row_info[row].endfirsthead = true;
2335 unsetMultiColumn(i);
2336 // When unsetting a caption row, also all existing
2337 // captions in this row must be dissolved.
2339 row_info[row].caption = what;
2344 bool Tabular::ltCaption(row_type row) const
2346 return row_info[row].caption;
2350 bool Tabular::haveLTCaption(CaptionType captiontype) const
2352 if (!is_long_tabular)
2354 for (row_type r = 0; r < nrows(); ++r) {
2355 if (row_info[r].caption) {
2356 switch (captiontype) {
2357 case CAPTION_FIRSTHEAD:
2358 if (row_info[r].endfirsthead)
2362 if (row_info[r].endhead)
2366 if (row_info[r].endfoot)
2369 case CAPTION_LASTFOOT:
2370 if (row_info[r].endlastfoot)
2382 // end longtable support functions
2384 void Tabular::setRowAscent(row_type row, int height)
2386 if (row >= nrows() || row_info[row].ascent == height)
2388 row_info[row].ascent = height;
2392 void Tabular::setRowDescent(row_type row, int height)
2394 if (row >= nrows() || row_info[row].descent == height)
2396 row_info[row].descent = height;
2400 int Tabular::rowAscent(row_type row) const
2402 LASSERT(row < nrows(), row = 0);
2403 return row_info[row].ascent;
2407 int Tabular::rowDescent(row_type row) const
2409 LASSERT(row < nrows(), row = 0);
2410 return row_info[row].descent;
2414 int Tabular::height() const
2417 for (row_type row = 0; row < nrows(); ++row)
2418 height += rowAscent(row) + rowDescent(row) +
2424 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2426 LASSERT(row < nrows(), return false);
2427 LASSERT(column < ncols(), return false);
2428 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2432 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2434 LASSERT(row < nrows(), return false);
2435 LASSERT(column < ncols(), return false);
2436 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2440 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns) const
2442 // we only output complete row lines and the 1st row here, the rest
2443 // is done in Tabular::TeXBottomHLine(...)
2445 // get for each column the topline (if any)
2446 map<col_type, bool> topline, topltrims, toprtrims;
2448 bool have_trims = false;
2449 for (auto const & c : columns) {
2450 topline[c] = topLine(cellIndex(row, c));
2451 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2452 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2453 // If cell is part of a multirow and not the first cell of the
2454 // multirow, no line must be drawn.
2456 if (isMultiRow(cellIndex(row, c))
2457 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2459 topltrims[c] = false;
2460 toprtrims[c] = false;
2462 // copy trimming to multicolumn parts
2463 if (isPartOfMultiColumn(row, c)) {
2464 topltrims[c] = topltrims[c-1];
2465 toprtrims[c] = toprtrims[c-1];
2467 if (topline.find(c) != topline.end() && topline.find(c)->second)
2469 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2470 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2474 // do nothing if empty first row, or incomplete row line after
2475 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2478 // Is this the actual first row (excluding longtable caption row)?
2479 bool const realfirstrow = (row == 0
2480 || (is_long_tabular && row == 1 && ltCaption(0)));
2482 // only output complete row lines and the 1st row's clines
2483 if (nset == ncols() && !have_trims) {
2485 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2489 } else if (realfirstrow || have_trims) {
2490 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2492 for (auto & cl : columns) {
2496 if (topline.find(c)->second) {
2497 col_type offset = 0;
2498 for (col_type j = 0 ; j < c; ++j)
2499 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2501 string const firstcol = convert<string>(c + 1 + offset);
2502 while (isPartOfMultiColumn(row, c))
2505 if (topltrims.find(c) != topltrims.end()
2506 && topltrims.find(c)->second)
2508 col_type cstart = c;
2509 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2510 if (isMultiColumn(cellIndex(row, c))
2511 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2513 if (c > cstart && topltrims.find(c) != topltrims.end()
2514 && topltrims.find(c)->second) {
2515 if (!isPartOfMultiColumn(row, c))
2518 } else if (toprtrims.find(c) != toprtrims.end()
2519 && toprtrims.find(c)->second)
2523 for (col_type j = cstart ; j < c ; ++j)
2524 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2526 col_type const lastcol = c + 1 + offset;
2527 if (toprtrims.find(c) != toprtrims.end()
2528 && toprtrims.find(c)->second)
2533 os << "(" << trim << ")";
2534 os << "{" << firstcol << '-' << lastcol << "}";
2535 if (c == ncols() - 1)
2545 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns) const
2547 // we output bottomlines of row r and the toplines of row r+1
2548 // if the latter do not span the whole tabular
2550 // get the bottomlines of row r, and toplines in next row
2551 bool lastrow = row == nrows() - 1;
2552 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2553 bool nextrowset = true;
2554 for (auto const & c : columns) {
2555 idx_type const idx = cellIndex(row, c);
2556 bottomline[c] = bottomLine(cellIndex(row, c));
2557 bottomltrims[c] = bottomLineTrim(idx).first;
2558 bottomrtrims[c] = bottomLineTrim(idx).second;
2559 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2560 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2561 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2562 // If cell is part of a multirow and not the last cell of the
2563 // multirow, no line must be drawn.
2565 if (isMultiRow(cellIndex(row, c))
2566 && isMultiRow(cellIndex(row + 1, c))
2567 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2568 bottomline[c] = false;
2570 bottomltrims[c] = false;
2571 bottomrtrims[c] = false;
2572 topltrims[c] = false;
2573 toprtrims[c] = false;
2575 // copy trimming in multicolumn parts
2576 if (isPartOfMultiColumn(row, c)) {
2577 topltrims[c] = topltrims[c-1];
2578 toprtrims[c] = toprtrims[c-1];
2579 bottomltrims[c] = bottomltrims[c-1];
2580 bottomrtrims[c] = bottomrtrims[c-1];
2583 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2586 // combine this row's bottom lines and next row's toplines if necessary
2588 bool have_trims = false;
2589 for (auto const & c : columns) {
2591 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2592 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2593 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2594 bottomrtrims[c] =(bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2595 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2596 if (bottomline.find(c)->second)
2598 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2599 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2603 // do nothing if empty, OR incomplete row line with a topline in next row
2604 if (nset == 0 || (nextrowset && nset != ncols()))
2607 if (nset == ncols() && !have_trims) {
2609 os << (lastrow ? "\\bottomrule" : "\\midrule");
2613 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2615 for (auto & cl : columns) {
2619 if (bottomline.find(c)->second) {
2620 col_type offset = 0;
2621 for (col_type j = 0 ; j < c; ++j)
2622 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2624 string const firstcol = convert<string>(c + 1 + offset);
2625 while (isPartOfMultiColumn(row, c))
2628 if (bottomltrims.find(c) != bottomltrims.end()
2629 && bottomltrims.find(c)->second)
2631 col_type cstart = c;
2632 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2633 if (isMultiColumn(cellIndex(row, c))
2635 && isPartOfMultiColumn(row, c + 1))
2638 && bottomltrims.find(c) != bottomltrims.end()
2639 && bottomltrims.find(c)->second) {
2640 if (!isPartOfMultiColumn(row, c))
2643 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2644 && bottomrtrims.find(c)->second)
2648 for (col_type j = cstart ; j < c ; ++j)
2649 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2651 col_type const lastcol = c + 1 + offset;
2652 if (bottomrtrims.find(c) != bottomrtrims.end()
2653 && bottomrtrims.find(c)->second)
2658 os << "(" << trim << ")";
2659 os << "{" << firstcol << '-' << lastcol << "}";
2660 if (c == ncols() - 1)
2670 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2671 bool & ismulticol, bool & ismultirow,
2672 bool const bidi) const
2674 row_type const r = cellRow(cell);
2675 if (is_long_tabular && row_info[r].caption)
2678 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2679 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2680 // figure out how to set the lines
2681 // we always set double lines to the right of the cell
2682 // or left in bidi RTL, respectively.
2683 col_type const c = cellColumn(cell);
2684 col_type const nextcol = c + columnSpan(cell);
2685 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2686 bool colright = columnRightLine(c);
2687 bool colleft = columnLeftLine(c);
2688 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2689 bool nextcellleft = nextcol < ncols()
2690 && leftLine(cellIndex(r, nextcol));
2691 bool coldouble = colright && nextcolleft;
2692 bool celldouble = rightLine(cell) && nextcellleft;
2694 ismulticol = (isMultiColumn(cell)
2695 || (c == 0 && colleft != leftLine(cell))
2696 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2697 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2698 || (coldouble != celldouble))
2701 // we center in multicol when no decimal point
2703 docstring const align_d = column_info[c].decimal_point;
2704 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2705 bool const nosep = !dit;
2706 ismulticol |= nosep;
2707 celldouble &= nosep;
2710 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2711 int latexcolspan = columnSpan(cell);
2712 for (col_type col = c; col < c + columnSpan(cell); ++col)
2713 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2717 os << "\\multicolumn{" << latexcolspan << "}{";
2718 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2719 || (!bidi && c == 0 && leftLine(cell))))
2721 if (bidi && celldouble)
2722 // add extra vertical line if we want a double one
2724 if (!cellInfo(cell).align_special.empty()) {
2725 os << cellInfo(cell).align_special;
2727 if (!getPWidth(cell).zero()) {
2729 case LYX_ALIGN_LEFT:
2730 os << ">{\\raggedright}";
2732 case LYX_ALIGN_RIGHT:
2733 os << ">{\\raggedleft}";
2735 case LYX_ALIGN_CENTER:
2736 os << ">{\\centering}";
2742 case LYX_VALIGN_TOP:
2745 case LYX_VALIGN_MIDDLE:
2748 case LYX_VALIGN_BOTTOM:
2753 << from_ascii(getPWidth(cell).asLatexString())
2757 case LYX_ALIGN_LEFT:
2760 case LYX_ALIGN_RIGHT:
2767 } // end if else !getPWidth
2768 } // end if else !cellinfo_of_cell
2769 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2771 if (!bidi && celldouble)
2772 // add extra vertical line if we want a double one
2775 } // end if ismulticol
2777 // we only need code for the first multirow cell
2778 ismultirow = isMultiRow(cell);
2780 os << "\\multirow{" << rowSpan(cell) << "}{";
2781 if (!getPWidth(cell).zero())
2782 os << from_ascii(getPWidth(cell).asLatexString());
2784 // we need to set a default value
2787 if (!getMROffset(cell).zero())
2788 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2790 } // end if ismultirow
2792 if (getRotateCell(cell) != 0)
2793 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2795 if (getUsebox(cell) == BOX_PARBOX) {
2798 case LYX_VALIGN_TOP:
2801 case LYX_VALIGN_MIDDLE:
2804 case LYX_VALIGN_BOTTOM:
2808 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2810 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2811 os << "\\begin{minipage}[";
2813 case LYX_VALIGN_TOP:
2816 case LYX_VALIGN_MIDDLE:
2819 case LYX_VALIGN_BOTTOM:
2823 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2825 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2826 os << "\\begin{varwidth}[";
2828 case LYX_VALIGN_TOP:
2831 case LYX_VALIGN_MIDDLE:
2834 case LYX_VALIGN_BOTTOM:
2838 os << "]{\\linewidth}\n";
2843 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2844 bool ismulticol, bool ismultirow) const
2846 row_type const r = cellRow(cell);
2847 if (is_long_tabular && row_info[r].caption)
2851 if (getUsebox(cell) == BOX_PARBOX)
2853 else if (getUsebox(cell) == BOX_MINIPAGE)
2854 os << breakln << "\\end{minipage}";
2855 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2856 os << breakln << "\\end{varwidth}";
2857 if (getRotateCell(cell) != 0)
2858 os << breakln << "\\end{turn}";
2866 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2867 OutputParams const & runparams,
2868 list<col_type> columns) const
2870 if (!is_long_tabular)
2874 // output caption which is in no header or footer
2875 if (haveLTCaption()) {
2876 for (row_type r = 0; r < nrows(); ++r) {
2877 if (row_info[r].caption &&
2878 !row_info[r].endfirsthead && !row_info[r].endhead &&
2879 !row_info[r].endfoot && !row_info[r].endlastfoot)
2880 TeXRow(os, r, runparams, columns);
2883 // output first header info
2884 if (haveLTFirstHead()) {
2885 if (endfirsthead.topDL)
2887 for (row_type r = 0; r < nrows(); ++r) {
2888 if (row_info[r].endfirsthead)
2889 TeXRow(os, r, runparams, columns);
2891 if (endfirsthead.bottomDL)
2893 os << "\\endfirsthead\n";
2895 // output header info
2897 if (endfirsthead.empty && !haveLTFirstHead())
2898 os << "\\endfirsthead\n";
2901 for (row_type r = 0; r < nrows(); ++r) {
2902 if (row_info[r].endhead)
2903 TeXRow(os, r, runparams, columns);
2905 if (endhead.bottomDL)
2907 os << "\\endhead\n";
2909 // output footer info
2913 for (row_type r = 0; r < nrows(); ++r) {
2914 if (row_info[r].endfoot)
2915 TeXRow(os, r, runparams, columns);
2917 if (endfoot.bottomDL)
2919 os << "\\endfoot\n";
2920 if (endlastfoot.empty && !haveLTLastFoot())
2921 os << "\\endlastfoot\n";
2923 // output lastfooter info
2924 if (haveLTLastFoot()) {
2925 if (endlastfoot.topDL)
2927 for (row_type r = 0; r < nrows(); ++r) {
2928 if (row_info[r].endlastfoot)
2929 TeXRow(os, r, runparams, columns);
2931 if (endlastfoot.bottomDL)
2933 os << "\\endlastfoot\n";
2938 bool Tabular::isValidRow(row_type row) const
2940 if (!is_long_tabular)
2942 return !row_info[row].endhead && !row_info[row].endfirsthead
2943 && !row_info[row].endfoot && !row_info[row].endlastfoot
2944 && !row_info[row].caption;
2948 void Tabular::TeXRow(otexstream & os, row_type row,
2949 OutputParams const & runparams,
2950 list<col_type> columns) const
2952 idx_type cell = cellIndex(row, 0);
2954 //output the top line
2955 TeXTopHLine(os, row, columns);
2957 if (row_info[row].top_space_default) {
2959 os << "\\addlinespace\n";
2961 os << "\\noalign{\\vskip\\doublerulesep}\n";
2962 } else if(!row_info[row].top_space.zero()) {
2964 os << "\\addlinespace["
2965 << from_ascii(row_info[row].top_space.asLatexString())
2968 os << "\\noalign{\\vskip"
2969 << from_ascii(row_info[row].top_space.asLatexString())
2973 bool ismulticol = false;
2974 bool ismultirow = false;
2976 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
2977 // Luabibdi (used by LuaTeX) behaves like classic
2978 bool const bidi_rtl =
2979 runparams.local_font->isRightToLeft()
2980 && runparams.useBidiPackage();
2982 bidi_rtl ? getFirstCellInRow(row) : getLastCellInRow(row);
2984 for (auto const & c : columns) {
2985 if (isPartOfMultiColumn(row, c))
2988 cell = cellIndex(row, c);
2990 if (isPartOfMultiRow(row, c)
2991 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2992 if (cell != lastcell)
2997 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
2998 InsetTableCell const * inset = cellInset(cell);
3000 Paragraph const & par = inset->paragraphs().front();
3002 os.texrow().forceStart(par.id(), 0);
3004 bool rtl = par.isRTL(buffer().params())
3006 && getPWidth(cell).zero()
3007 && !runparams.use_polyglossia;
3011 par.getParLanguage(buffer().params())->lang();
3012 if (lang == "farsi")
3014 else if (lang == "arabic_arabi")
3016 // currently, remaining RTL languages are
3017 // arabic_arabtex and hebrew
3021 // pass to the OutputParams that we are in a cell and
3022 // which alignment we have set.
3023 // InsetNewline needs this context information.
3024 OutputParams newrp(runparams);
3025 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3026 ? OutputParams::PLAIN
3027 : OutputParams::ALIGNED;
3029 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3030 // copy cell and split in 2
3031 InsetTableCell head = InsetTableCell(*cellInset(cell));
3032 head.setBuffer(const_cast<Buffer &>(buffer()));
3033 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3035 dit.push_back(CursorSlice(head));
3036 head.setMacrocontextPositionRecursive(dit);
3037 bool hassep = false;
3038 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3040 tail.setBuffer(head.buffer());
3042 dit.push_back(CursorSlice(tail));
3043 tail.setMacrocontextPositionRecursive(dit);
3047 tail.latex(os, newrp);
3050 head.latex(os, newrp);
3052 head.latex(os, newrp);
3055 tail.latex(os, newrp);
3058 } else if (ltCaption(row)) {
3059 // Inside longtable caption rows, we must only output the caption inset
3060 // with its content and omit anything outside of that (see #10791)
3061 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
3062 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
3063 for (; it != i_end; ++it) {
3064 if (it->lyxCode() != CAPTION_CODE)
3066 it->latex(os, runparams);
3069 } else if (!isPartOfMultiRow(row, c)) {
3070 if (!runparams.nice)
3071 os.texrow().start(par.id(), 0);
3072 inset->latex(os, newrp);
3075 runparams.encoding = newrp.encoding;
3079 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3080 if (cell != lastcell) { // not last cell in row
3087 os << "\\tabularnewline";
3088 if (row_info[row].bottom_space_default) {
3090 os << "\\addlinespace";
3092 os << "[\\doublerulesep]";
3093 } else if (!row_info[row].bottom_space.zero()) {
3095 os << "\\addlinespace";
3097 << from_ascii(row_info[row].bottom_space.asLatexString())
3102 //output the bottom line
3103 TeXBottomHLine(os, row, columns);
3105 if (row_info[row].interline_space_default) {
3107 os << "\\addlinespace\n";
3109 os << "\\noalign{\\vskip\\doublerulesep}\n";
3110 } else if (!row_info[row].interline_space.zero()) {
3112 os << "\\addlinespace["
3113 << from_ascii(row_info[row].interline_space.asLatexString())
3116 os << "\\noalign{\\vskip"
3117 << from_ascii(row_info[row].interline_space.asLatexString())
3123 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3125 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3126 && !hasVarwidthColumn();
3127 bool const is_xltabular = is_long_tabular
3128 && (hasVarwidthColumn() || !tabular_width.zero());
3129 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3131 //+---------------------------------------------------------------------
3132 //+ first the opening preamble +
3133 //+---------------------------------------------------------------------
3136 if (!TexRow::isNone(pos))
3137 os.texrow().start(pos);
3140 if (is_long_tabular)
3141 os << "\\begin{landscape}\n";
3143 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3146 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3147 // order for RTL (#9686). Thus we use this list.
3148 bool const bidi_rtl =
3149 runparams.local_font->isRightToLeft()
3150 && runparams.useBidiPackage();
3151 list<col_type> columns;
3152 for (col_type cl = 0; cl < ncols(); ++cl) {
3154 columns.push_front(cl);
3156 columns.push_back(cl);
3159 // If we use \cline or \cmidrule, we need to locally de-activate
3160 // the - character when using languages that activate it (e.g., Czech, Slovak).
3161 bool deactivate_chars = false;
3162 if ((runparams.use_babel || runparams.use_polyglossia)
3163 && contains(runparams.active_chars, '-')) {
3164 bool have_clines = false;
3165 // Check if we use \cline or \cmidrule
3166 for (row_type row = 0; row < nrows(); ++row) {
3167 col_type bset = 0, tset = 0;
3168 for (auto const & c : columns) {
3169 idx_type const idx = cellIndex(row, c);
3170 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3171 || topLineTrim(idx).first || topLineTrim(idx).second) {
3175 if (bottomLine(cellIndex(row, c)))
3177 if (topLine(cellIndex(row, c)))
3180 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3186 deactivate_chars = true;
3187 os << "\\begingroup\n"
3188 << "\\catcode`\\-=12\n";
3192 if (is_long_tabular) {
3194 os << "\\begin{xltabular}";
3196 os << "\\begin{longtable}";
3197 switch (longtabular_alignment) {
3198 case LYX_LONGTABULAR_ALIGN_LEFT:
3201 case LYX_LONGTABULAR_ALIGN_CENTER:
3204 case LYX_LONGTABULAR_ALIGN_RIGHT:
3209 if (tabular_width.zero())
3210 os << "{" << from_ascii("\\columnwidth") << "}";
3212 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3215 if (is_tabular_star)
3216 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3217 else if (hasVarwidthColumn()) {
3218 os << "\\begin{tabularx}{";
3219 if (tabular_width.zero())
3220 os << from_ascii("\\columnwidth") << "}";
3222 os << from_ascii(tabular_width.asLatexString()) << "}";
3224 os << "\\begin{tabular}";
3225 switch (tabular_valignment) {
3226 case LYX_VALIGN_TOP:
3229 case LYX_VALIGN_MIDDLE:
3231 case LYX_VALIGN_BOTTOM:
3239 if (is_tabular_star)
3240 os << "@{\\extracolsep{\\fill}}";
3242 for (auto const & c : columns) {
3243 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3245 if (!column_info[c].align_special.empty()) {
3246 os << column_info[c].align_special;
3248 if (!column_info[c].p_width.zero()) {
3249 bool decimal = false;
3250 switch (column_info[c].alignment) {
3251 case LYX_ALIGN_LEFT:
3252 os << ">{\\raggedright}";
3254 case LYX_ALIGN_RIGHT:
3255 os << ">{\\raggedleft}";
3257 case LYX_ALIGN_CENTER:
3258 os << ">{\\centering}";
3260 case LYX_ALIGN_NONE:
3261 case LYX_ALIGN_BLOCK:
3262 case LYX_ALIGN_LAYOUT:
3263 case LYX_ALIGN_SPECIAL:
3265 case LYX_ALIGN_DECIMAL: {
3267 os << ">{\\raggedright}";
3269 os << ">{\\raggedleft}";
3276 switch (column_info[c].valignment) {
3277 case LYX_VALIGN_TOP:
3278 // this is the default
3280 case LYX_VALIGN_MIDDLE:
3283 case LYX_VALIGN_BOTTOM:
3289 // Fixed-width cells with alignment at decimal separator
3290 // are output as two cells of half the width with the decimal
3291 // separator as column sep. This effectively puts the content
3292 // centered, which differs from the normal decimal sep alignment
3293 // and is not ideal, but we cannot do better ATM (see #9568).
3294 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3296 docstring const halffixedwith =
3297 from_ascii(Length(column_info[c].p_width.value() / 2,
3298 column_info[c].p_width.unit()).asLatexString());
3302 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3309 << from_ascii(column_info[c].p_width.asLatexString())
3311 } else if (column_info[c].varwidth) {
3312 switch (column_info[c].alignment) {
3313 case LYX_ALIGN_LEFT:
3314 os << ">{\\raggedright\\arraybackslash}";
3316 case LYX_ALIGN_RIGHT:
3317 os << ">{\\raggedleft\\arraybackslash}";
3319 case LYX_ALIGN_CENTER:
3320 os << ">{\\centering\\arraybackslash}";
3322 case LYX_ALIGN_NONE:
3323 case LYX_ALIGN_BLOCK:
3324 case LYX_ALIGN_LAYOUT:
3325 case LYX_ALIGN_SPECIAL:
3326 case LYX_ALIGN_DECIMAL:
3330 } else if (isVTypeColumn(c)) {
3331 switch (column_info[c].alignment) {
3332 case LYX_ALIGN_LEFT:
3333 os << ">{\\raggedright}";
3335 case LYX_ALIGN_RIGHT:
3336 os << ">{\\raggedleft}";
3338 case LYX_ALIGN_CENTER:
3339 os << ">{\\centering}";
3341 case LYX_ALIGN_NONE:
3342 case LYX_ALIGN_BLOCK:
3343 case LYX_ALIGN_LAYOUT:
3344 case LYX_ALIGN_SPECIAL:
3345 case LYX_ALIGN_DECIMAL:
3348 os << "V{\\linewidth}";
3350 switch (column_info[c].alignment) {
3351 case LYX_ALIGN_LEFT:
3354 case LYX_ALIGN_RIGHT:
3357 case LYX_ALIGN_DECIMAL:
3358 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3364 } // end if else !column_info[i].p_width
3365 } // end if else !column_info[i].align_special
3366 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3371 TeXLongtableHeaderFooter(os, runparams, columns);
3373 //+---------------------------------------------------------------------
3374 //+ the single row and columns (cells) +
3375 //+---------------------------------------------------------------------
3377 for (row_type r = 0; r < nrows(); ++r) {
3378 if (isValidRow(r)) {
3379 TeXRow(os, r, runparams, columns);
3380 if (is_long_tabular && row_info[r].newpage)
3381 os << "\\newpage\n";
3385 //+---------------------------------------------------------------------
3386 //+ the closing of the tabular +
3387 //+---------------------------------------------------------------------
3389 if (is_long_tabular) {
3391 os << "\\end{xltabular}";
3393 os << "\\end{longtable}";
3395 if (is_tabular_star)
3396 os << "\\end{tabular*}";
3397 else if (hasVarwidthColumn())
3398 os << "\\end{tabularx}";
3400 os << "\\end{tabular}";
3403 if (deactivate_chars)
3405 os << "\n\\endgroup\n";
3408 if (is_long_tabular)
3409 os << breakln << "\\end{landscape}";
3411 os << breakln << "\\end{turn}";
3414 if (!TexRow::isNone(pos))
3415 os.texrow().start(pos);
3419 int Tabular::docbookRow(odocstream & os, row_type row,
3420 OutputParams const & runparams) const
3423 idx_type cell = getFirstCellInRow(row);
3426 for (col_type c = 0; c < ncols(); ++c) {
3427 if (isPartOfMultiColumn(row, c))
3430 os << "<entry align=\"";
3431 switch (getAlignment(cell)) {
3432 case LYX_ALIGN_LEFT:
3435 case LYX_ALIGN_RIGHT:
3443 os << "\" valign=\"";
3444 switch (getVAlignment(cell)) {
3445 case LYX_VALIGN_TOP:
3448 case LYX_VALIGN_BOTTOM:
3451 case LYX_VALIGN_MIDDLE:
3456 if (isMultiColumn(cell)) {
3457 os << " namest=\"col" << c << "\" ";
3458 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3462 ret += cellInset(cell)->docbook(os, runparams);
3471 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3475 //+---------------------------------------------------------------------
3476 //+ first the opening preamble +
3477 //+---------------------------------------------------------------------
3479 os << "<tgroup cols=\"" << ncols()
3480 << "\" colsep=\"1\" rowsep=\"1\">\n";
3482 for (col_type c = 0; c < ncols(); ++c) {
3483 os << "<colspec colname=\"col" << c << "\" align=\"";
3484 switch (column_info[c].alignment) {
3485 case LYX_ALIGN_LEFT:
3488 case LYX_ALIGN_RIGHT:
3496 if (runparams.flavor == OutputParams::XML)
3502 //+---------------------------------------------------------------------
3503 //+ Long Tabular case +
3504 //+---------------------------------------------------------------------
3506 // output caption info
3507 // The caption flag wins over head/foot
3508 if (haveLTCaption()) {
3509 os << "<caption>\n";
3511 for (row_type r = 0; r < nrows(); ++r) {
3512 if (row_info[r].caption) {
3513 ret += docbookRow(os, r, runparams);
3516 os << "</caption>\n";
3519 // output header info
3520 if (haveLTHead(false) || haveLTFirstHead(false)) {
3523 for (row_type r = 0; r < nrows(); ++r) {
3524 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3525 !row_info[r].caption) {
3526 ret += docbookRow(os, r, runparams);
3532 // output footer info
3533 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3536 for (row_type r = 0; r < nrows(); ++r) {
3537 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3538 !row_info[r].caption) {
3539 ret += docbookRow(os, r, runparams);
3546 //+---------------------------------------------------------------------
3547 //+ the single row and columns (cells) +
3548 //+---------------------------------------------------------------------
3552 for (row_type r = 0; r < nrows(); ++r) {
3553 if (isValidRow(r)) {
3554 ret += docbookRow(os, r, runparams);
3559 //+---------------------------------------------------------------------
3560 //+ the closing of the tabular +
3561 //+---------------------------------------------------------------------
3570 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3571 OutputParams const & runparams, bool header) const
3574 string const celltag = header ? "th" : "td";
3575 idx_type cell = getFirstCellInRow(row);
3577 xs << html::StartTag("tr");
3578 for (col_type c = 0; c < ncols(); ++c) {
3579 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3584 Length const cwidth = column_info[c].p_width;
3585 if (!cwidth.zero()) {
3586 string const hwidth = cwidth.asHTMLString();
3587 attr << "style =\"width: " << hwidth << ";\" ";
3591 switch (getAlignment(cell)) {
3592 case LYX_ALIGN_LEFT:
3595 case LYX_ALIGN_RIGHT:
3603 attr << " valign='";
3604 switch (getVAlignment(cell)) {
3605 case LYX_VALIGN_TOP:
3608 case LYX_VALIGN_BOTTOM:
3611 case LYX_VALIGN_MIDDLE:
3616 if (isMultiColumn(cell))
3617 attr << " colspan='" << columnSpan(cell) << "'";
3618 else if (isMultiRow(cell))
3619 attr << " rowspan='" << rowSpan(cell) << "'";
3621 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3622 ret += cellInset(cell)->xhtml(xs, runparams);
3623 xs << html::EndTag(celltag) << html::CR();
3626 xs << html::EndTag("tr");
3631 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3635 if (is_long_tabular) {
3636 // we'll wrap it in a div, so as to deal with alignment
3638 switch (longtabular_alignment) {
3639 case LYX_LONGTABULAR_ALIGN_LEFT:
3642 case LYX_LONGTABULAR_ALIGN_CENTER:
3645 case LYX_LONGTABULAR_ALIGN_RIGHT:
3649 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3651 // The caption flag wins over head/foot
3652 if (haveLTCaption()) {
3653 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3655 for (row_type r = 0; r < nrows(); ++r)
3656 if (row_info[r].caption)
3657 ret += xhtmlRow(xs, r, runparams);
3658 xs << html::EndTag("div") << html::CR();
3662 xs << html::StartTag("table") << html::CR();
3664 // output header info
3665 bool const havefirsthead = haveLTFirstHead(false);
3666 // if we have a first head, then we are going to ignore the
3667 // headers for the additional pages, since there aren't any
3668 // in XHTML. this test accomplishes that.
3669 bool const havehead = !havefirsthead && haveLTHead(false);
3670 if (havehead || havefirsthead) {
3671 xs << html::StartTag("thead") << html::CR();
3672 for (row_type r = 0; r < nrows(); ++r) {
3673 if (((havefirsthead && row_info[r].endfirsthead) ||
3674 (havehead && row_info[r].endhead)) &&
3675 !row_info[r].caption) {
3676 ret += xhtmlRow(xs, r, runparams, true);
3679 xs << html::EndTag("thead") << html::CR();
3681 // output footer info
3682 bool const havelastfoot = haveLTLastFoot(false);
3684 bool const havefoot = !havelastfoot && haveLTFoot(false);
3685 if (havefoot || havelastfoot) {
3686 xs << html::StartTag("tfoot") << html::CR();
3687 for (row_type r = 0; r < nrows(); ++r) {
3688 if (((havelastfoot && row_info[r].endlastfoot) ||
3689 (havefoot && row_info[r].endfoot)) &&
3690 !row_info[r].caption) {
3691 ret += xhtmlRow(xs, r, runparams);
3694 xs << html::EndTag("tfoot") << html::CR();
3697 xs << html::StartTag("tbody") << html::CR();
3698 for (row_type r = 0; r < nrows(); ++r) {
3699 if (isValidRow(r)) {
3700 ret += xhtmlRow(xs, r, runparams);
3703 xs << html::EndTag("tbody")
3705 << html::EndTag("table")
3707 if (is_long_tabular)
3708 xs << html::EndTag("div") << html::CR();
3713 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3714 vector<unsigned int> const & clen) const
3716 idx_type const fcell = getFirstCellInRow(row);
3717 idx_type const n = numberOfCellsInRow(row) + fcell;
3720 for (idx_type i = fcell; i < n; ++i) {
3730 for (idx_type i = fcell; i < n; ++i) {
3741 col_type column = cellColumn(i);
3742 int len = clen[column];
3743 while (column < ncols() - 1
3744 && isPartOfMultiColumn(row, ++column))
3745 len += clen[column] + 4;
3746 os << docstring(len, ch);
3761 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3762 vector<unsigned int> const & clen) const
3764 idx_type const fcell = getFirstCellInRow(row);
3765 idx_type const n = numberOfCellsInRow(row) + fcell;
3768 for (idx_type i = fcell; i < n; ++i) {
3769 if (bottomLine(i)) {
3778 for (idx_type i = fcell; i < n; ++i) {
3779 if (bottomLine(i)) {
3789 col_type column = cellColumn(i);
3790 int len = clen[column];
3791 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3792 len += clen[column] + 4;
3793 os << docstring(len, ch);
3794 if (bottomLine(i)) {
3808 void Tabular::plaintextPrintCell(odocstringstream & os,
3809 OutputParams const & runparams,
3810 idx_type cell, row_type row, col_type column,
3811 vector<unsigned int> const & clen,
3812 bool onlydata, size_t max_length) const
3814 odocstringstream sstr;
3815 cellInset(cell)->plaintext(sstr, runparams, max_length);
3827 unsigned int len1 = sstr.str().length();
3828 unsigned int len2 = clen[column];
3829 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3830 len2 += clen[column] + 4;
3833 switch (getAlignment(cell)) {
3835 case LYX_ALIGN_LEFT:
3838 case LYX_ALIGN_RIGHT:
3842 case LYX_ALIGN_CENTER:
3848 os << docstring(len1, ' ') << sstr.str()
3849 << docstring(len2, ' ');
3851 if (rightLine(cell))
3858 void Tabular::plaintext(odocstringstream & os,
3859 OutputParams const & runparams, int const depth,
3860 bool onlydata, char_type delim, size_t max_length) const
3862 // first calculate the width of the single columns
3863 vector<unsigned int> clen(ncols());
3866 // first all non multicolumn cells!
3867 for (col_type c = 0; c < ncols(); ++c) {
3869 for (row_type r = 0; r < nrows(); ++r) {
3870 idx_type cell = cellIndex(r, c);
3871 if (isMultiColumn(cell))
3873 odocstringstream sstr;
3874 cellInset(cell)->plaintext(sstr, runparams, max_length);
3875 if (clen[c] < sstr.str().length())
3876 clen[c] = sstr.str().length();
3879 // then all multicolumn cells!
3880 for (col_type c = 0; c < ncols(); ++c) {
3881 for (row_type r = 0; r < nrows(); ++r) {
3882 idx_type cell = cellIndex(r, c);
3883 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3885 odocstringstream sstr;
3886 cellInset(cell)->plaintext(sstr, runparams, max_length);
3887 int len = int(sstr.str().length());
3888 idx_type const n = columnSpan(cell);
3889 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3891 if (len > int(clen[c + n - 1]))
3892 clen[c + n - 1] = len;
3897 for (row_type r = 0; r < nrows(); ++r) {
3898 if (!onlydata && plaintextTopHLine(os, r, clen))
3899 os << docstring(depth * 2, ' ');
3900 for (col_type c = 0; c < ncols(); ++c) {
3901 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3903 if (onlydata && c > 0)
3904 // we don't use operator<< for single UCS4 character.
3905 // see explanation in docstream.h
3907 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3909 if (os.str().size() > max_length)
3914 os << docstring(depth * 2, ' ');
3915 if (plaintextBottomHLine(os, r, clen))
3916 os << docstring(depth * 2, ' ');
3918 if (os.str().size() > max_length)
3924 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3926 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3930 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3932 return cell_info[row][column].inset;
3936 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3938 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3942 void Tabular::setCellInset(row_type row, col_type column,
3943 shared_ptr<InsetTableCell> ins)
3945 CellData & cd = cell_info[row][column];
3950 void Tabular::validate(LaTeXFeatures & features) const
3952 features.require("NeedTabularnewline");
3954 features.require("booktabs");
3955 if (is_long_tabular && !hasVarwidthColumn()) {
3956 if (tabular_width.zero())
3957 features.require("longtable");
3959 features.require("xltabular");
3961 if (rotate && is_long_tabular)
3962 features.require("lscape");
3964 features.require("rotating");
3965 if (hasVarwidthColumn()) {
3966 if (is_long_tabular)
3967 features.require("xltabular");
3969 features.require("tabularx");
3971 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3972 if (isMultiRow(cell))
3973 features.require("multirow");
3974 if (getUsebox(cell) == BOX_VARWIDTH)
3975 features.require("varwidth");
3976 if (getVAlignment(cell) != LYX_VALIGN_TOP
3977 || !getPWidth(cell).zero()
3978 || isVTypeColumn(cellColumn(cell)))
3979 features.require("array");
3980 // Tell footnote that we need a savenote
3981 // environment in non-long tables or
3982 // longtable headers/footers
3983 else if (!is_long_tabular && !features.inFloat())
3984 features.saveNoteEnv("tabular");
3985 else if (!isValidRow(cellRow(cell)))
3986 features.saveNoteEnv("longtable");
3988 cellInset(cell)->validate(features);
3989 features.saveNoteEnv(string());
3994 Tabular::BoxType Tabular::useBox(idx_type cell) const
3996 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3997 if (parlist.size() > 1)
3998 return BOX_VARWIDTH;
4000 ParagraphList::const_iterator cit = parlist.begin();
4001 ParagraphList::const_iterator end = parlist.end();
4003 for (; cit != end; ++cit)
4004 for (int i = 0; i < cit->size(); ++i)
4005 if (cit->isNewline(i) || cit->layout().isEnvironment())
4006 return BOX_VARWIDTH;
4012 /////////////////////////////////////////////////////////////////////
4016 /////////////////////////////////////////////////////////////////////
4018 InsetTableCell::InsetTableCell(Buffer * buf)
4019 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
4020 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4024 bool InsetTableCell::forcePlainLayout(idx_type) const
4026 return isMultiRow || (isMultiColumn && !isFixedWidth);
4030 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4032 return isFixedWidth;
4036 bool InsetTableCell::forceLocalFontSwitch() const
4042 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4043 FuncStatus & status) const
4045 bool enabled = true;
4046 switch (cmd.action()) {
4047 case LFUN_INSET_DISSOLVE:
4050 case LFUN_MATH_DISPLAY:
4051 if (!hasFixedWidth()) {
4056 return InsetText::getStatus(cur, cmd, status);
4058 status.setEnabled(enabled);
4062 docstring InsetTableCell::asString(bool intoInsets)
4065 if (paragraphs().empty())
4067 ParagraphList::const_iterator it = paragraphs().begin();
4068 ParagraphList::const_iterator en = paragraphs().end();
4070 for (; it != en; ++it) {
4075 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4081 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4082 UpdateType utype, TocBackend & backend) const
4084 InsetText::iterateForToc(di, output_active, utype, backend);
4088 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4091 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4092 return InsetText::xhtml(xs, rp);
4096 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4098 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4100 // Hand font through to contained lyxtext:
4101 tm.font_.fontInfo() = mi.base.font;
4102 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
4104 // This can happen when a layout has a left and right margin,
4105 // and the view is made very narrow. We can't do better than
4106 // to draw it partly out of view (bug 5890).
4107 if (mi.base.textwidth < 1)
4108 mi.base.textwidth = 1;
4110 // We tell metrics here not to expand on multiple pars
4111 // This is the difference to InsetText::Metrics
4112 if (hasFixedWidth())
4113 tm.metrics(mi, dim, mi.base.textwidth, false);
4115 tm.metrics(mi, dim, 0, false);
4116 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
4117 dim.asc += TEXT_TO_INSET_OFFSET;
4118 dim.des += TEXT_TO_INSET_OFFSET;
4119 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
4124 /////////////////////////////////////////////////////////////////////
4128 /////////////////////////////////////////////////////////////////////
4130 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4132 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4133 rowselect_(false), colselect_(false)
4138 InsetTabular::InsetTabular(InsetTabular const & tab)
4139 : Inset(tab), tabular(tab.tabular),
4140 rowselect_(false), colselect_(false)
4145 InsetTabular::~InsetTabular()
4147 hideDialogs("tabular", this);
4151 void InsetTabular::setBuffer(Buffer & buf)
4153 tabular.setBuffer(buf);
4154 Inset::setBuffer(buf);
4158 bool InsetTabular::insetAllowed(InsetCode code) const
4163 case MATHMACRO_CODE:
4168 return tabular.is_long_tabular;
4176 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4178 return tabular.is_long_tabular &&
4179 (newtype == "Standard" || newtype == "Unnumbered");
4183 void InsetTabular::write(ostream & os) const
4185 os << "Tabular" << endl;
4190 string InsetTabular::contextMenu(BufferView const &, int, int) const
4192 // FIXME: depending on the selection state,
4193 // we could offer a different menu.
4194 return cell(0)->contextMenuName() + ";" + contextMenuName();
4198 string InsetTabular::contextMenuName() const
4200 return "context-tabular";
4204 void InsetTabular::read(Lexer & lex)
4206 //bool const old_format = (lex.getString() == "\\LyXTable");
4214 string token = lex.getString();
4215 while (lex && token != "\\end_inset") {
4217 token = lex.getString();
4220 lex.printError("Missing \\end_inset at this point. ");
4224 int InsetTabular::rowFromY(Cursor & cur, int y) const
4226 // top y coordinate of tabular
4227 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4229 for (; r < tabular.nrows() && y > h; ++r)
4230 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4231 + tabular.interRowSpace(r);
4237 int InsetTabular::columnFromX(Cursor & cur, int x) const
4239 // left x coordinate of tabular
4240 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4242 for (; c < tabular.ncols() && x > w; ++c)
4243 w += tabular.cellWidth(c);
4248 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4250 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4251 // mi.base.textwidth << "\n";
4252 LBUFERR(mi.base.bv);
4254 for (row_type r = 0; r < tabular.nrows(); ++r) {
4257 for (col_type c = 0; c < tabular.ncols(); ++c) {
4258 if (tabular.isPartOfMultiColumn(r, c)
4259 || tabular.isPartOfMultiRow(r, c))
4260 // multicolumn or multirow cell, but not first one
4262 idx_type const cell = tabular.cellIndex(r, c);
4265 Length const p_width = tabular.getPWidth(cell);
4266 if (!p_width.zero())
4267 m.base.textwidth = mi.base.inPixels(p_width);
4268 else if (tabular.column_info[c].varwidth)
4269 m.base.textwidth = tabular.column_info[c].width;
4270 tabular.cellInset(cell)->metrics(m, dim0);
4271 if (!p_width.zero() || tabular.column_info[c].varwidth)
4272 dim0.wid = m.base.textwidth;
4273 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4274 + tabular.interColumnSpace(cell);
4276 // FIXME(?): do we need a second metrics call?
4277 TextMetrics const & tm =
4278 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4280 // determine horizontal offset because of decimal align (if necessary)
4281 int decimal_width = 0;
4282 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4283 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4284 tail.setBuffer(tabular.buffer());
4285 // we need to set macrocontext position everywhere
4286 // otherwise we crash with nested insets (e.g. footnotes)
4287 // after decimal point
4288 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4290 dit.push_back(CursorSlice(tail));
4291 tail.setMacrocontextPositionRecursive(dit);
4293 // remove text leading decimal point
4294 docstring const align_d = tabular.column_info[c].decimal_point;
4295 dit = separatorPos(&tail, align_d);
4297 pit_type const psize = tail.paragraphs().front().size();
4299 tail.paragraphs().front().eraseChars(0,
4300 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4302 tail.metrics(m, dim1);
4303 decimal_width = dim1.width();
4306 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4307 tabular.cell_info[r][c].decimal_width = decimal_width;
4309 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4310 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
4311 int const lastpardes = tm.last().second->descent()
4312 + TEXT_TO_INSET_OFFSET;
4314 switch (tabular.getVAlignment(cell)) {
4315 case Tabular::LYX_VALIGN_TOP:
4317 case Tabular::LYX_VALIGN_MIDDLE:
4318 offset = -(dim0.des - lastpardes)/2;
4320 case Tabular::LYX_VALIGN_BOTTOM:
4321 offset = -(dim0.des - lastpardes);
4324 tabular.cell_info[r][c].voffset = offset;
4325 maxasc = max(maxasc, dim0.asc - offset);
4326 maxdes = max(maxdes, dim0.des + offset);
4328 int const top_space = tabular.row_info[r].top_space_default ?
4329 default_line_space :
4330 mi.base.inPixels(tabular.row_info[r].top_space);
4331 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4332 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4333 default_line_space :
4334 mi.base.inPixels(tabular.row_info[r].bottom_space);
4335 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4338 // We need to recalculate the metrics after column width calculation
4339 // with xtabular (possibly multiple times, so the call is recursive).
4340 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4342 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4343 dim.des = tabular.height() - dim.asc;
4344 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4348 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4350 if (&cur.inset() == this && cur.selection()) {
4351 if (cur.selIsMultiCell()) {
4354 getSelection(cur, rs, re, cs, ce);
4356 idx_type const cell = tabular.cellIndex(row, col);
4357 col_type const cspan = tabular.columnSpan(cell);
4358 row_type const rspan = tabular.rowSpan(cell);
4359 if (col + cspan - 1 >= cs && col <= ce
4360 && row + rspan - 1 >= rs && row <= re)
4363 if (col == tabular.cellColumn(cur.idx())
4364 && row == tabular.cellRow(cur.idx())) {
4365 CursorSlice const & beg = cur.selBegin();
4366 CursorSlice const & end = cur.selEnd();
4368 if ((end.lastpos() > 0 || end.lastpit() > 0)
4369 && end.pos() == end.lastpos() && beg.pos() == 0
4370 && end.pit() == end.lastpit() && beg.pit() == 0)
4378 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4380 x += ADD_TO_TABULAR_WIDTH;
4382 BufferView * bv = pi.base.bv;
4383 Cursor & cur = pi.base.bv->cursor();
4385 // FIXME: As the full background is painted in drawBackground(),
4386 // we have no choice but to do a full repaint for the Text cells.
4387 pi.full_repaint = true;
4389 bool const original_selection_state = pi.selected;
4393 int yy = y + tabular.offsetVAlignment();
4394 for (row_type r = 0; r < tabular.nrows(); ++r) {
4396 for (col_type c = 0; c < tabular.ncols(); ++c) {
4397 if (tabular.isPartOfMultiColumn(r, c))
4400 idx = tabular.cellIndex(r, c);
4402 if (tabular.isPartOfMultiRow(r, c)) {
4403 nx += tabular.cellWidth(idx);
4407 pi.selected |= isCellSelected(cur, r, c);
4408 int const cx = nx + tabular.textHOffset(idx);
4409 int const cy = yy + tabular.textVOffset(idx);
4410 // Cache the Inset position.
4411 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4412 cell(idx)->draw(pi, cx, cy);
4413 drawCellLines(pi, nx, yy, r, idx);
4414 nx += tabular.cellWidth(idx);
4415 pi.selected = original_selection_state;
4418 if (r + 1 < tabular.nrows())
4419 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4420 + tabular.interRowSpace(r + 1);
4425 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4427 x += ADD_TO_TABULAR_WIDTH;
4428 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4429 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4430 pi.backgroundColor(this));
4434 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4436 Cursor & cur = pi.base.bv->cursor();
4438 x += ADD_TO_TABULAR_WIDTH;
4440 if (!cur.selection())
4442 if (&cur.inset() != this)
4447 bool const full_cell_selected = isCellSelected(cur,
4448 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4450 if (cur.selIsMultiCell() || full_cell_selected) {
4451 for (row_type r = 0; r < tabular.nrows(); ++r) {
4453 for (col_type c = 0; c < tabular.ncols(); ++c) {
4454 if (tabular.isPartOfMultiColumn(r, c))
4457 idx_type const cell = tabular.cellIndex(r, c);
4459 if (tabular.isPartOfMultiRow(r, c)) {
4460 xx += tabular.cellWidth(cell);
4463 int const w = tabular.cellWidth(cell);
4464 int const h = tabular.cellHeight(cell);
4465 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4466 if (isCellSelected(cur, r, c))
4467 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4470 if (r + 1 < tabular.nrows())
4471 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4472 + tabular.interRowSpace(r + 1);
4476 // FIXME: This code has no effect because InsetTableCell does not handle
4477 // drawSelection other than the trivial implementation in Inset.
4479 // x += cellXPos(cur.idx());
4480 // x += tabular.textHOffset(cur.idx());
4481 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4488 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4489 Color const incol, bool drawline, bool heavy = false)
4491 Color const col = drawline ? incol : Color_tabularonoffline;
4492 if (drawline && lt > 0)
4493 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4494 Painter::line_onoffdash,
4495 Painter::thin_line);
4496 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4497 drawline ? Painter::line_solid : Painter::line_onoffdash,
4498 (heavy ? 2 : 1) * Painter::thin_line);
4499 if (drawline && rt > 0)
4500 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4501 Painter::line_onoffdash,
4502 Painter::thin_line);
4508 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4509 row_type row, idx_type cell) const
4511 y -= tabular.rowAscent(row);
4512 int const w = tabular.cellWidth(cell);
4513 int const h = tabular.cellHeight(cell);
4517 col_type const col = tabular.cellColumn(cell);
4519 // Colour the frame if rows/columns are added or deleted
4520 Color colour = Color_tabularline;
4521 if (tabular.column_info[col].change != Change::UNCHANGED
4522 || tabular.row_info[row].change != Change::UNCHANGED)
4523 colour = InsetTableCell(*tabular.cellInset(cell)).paragraphs().front().lookupChange(0).color();
4526 bool drawline = tabular.topLine(cell)
4527 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4528 bool heavy = tabular.use_booktabs
4529 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4530 && tabular.rowTopLine(row);
4531 if (tabular.topLineTrim(cell).first
4532 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4534 if (tabular.topLineTrim(cell).second
4535 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4537 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4541 drawline = tabular.bottomLine(cell);
4542 row_type const lastrow = tabular.nrows() - 1;
4543 // Consider multi-rows
4545 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4546 && tabular.isPartOfMultiRow(r + 1, col))
4548 heavy = tabular.use_booktabs
4549 && ((row == lastrow && tabular.rowBottomLine(row))
4550 || (r == lastrow && tabular.rowBottomLine(r)));
4551 if (tabular.bottomLineTrim(cell).first)
4553 if (tabular.bottomLineTrim(cell).second)
4555 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4558 drawline = tabular.leftLine(cell)
4559 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4560 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4563 x -= tabular.interColumnSpace(cell);
4564 col_type next_cell_col = col + 1;
4565 while (next_cell_col < tabular.ncols()
4566 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4568 drawline = tabular.rightLine(cell)
4569 || (next_cell_col < tabular.ncols()
4570 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4571 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4575 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4577 //lyxerr << "InsetTabular::edit: " << this << endl;
4581 if (isRightToLeft(cur))
4582 cur.idx() = tabular.getLastCellInRow(0);
4588 if (isRightToLeft(cur))
4589 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4591 cur.idx() = tabular.numberofcells - 1;
4593 cur.pos() = cur.lastpos(); // FIXME crude guess
4595 cur.setCurrentFont();
4596 // FIXME: this accesses the position cache before it is initialized
4597 //cur.bv().fitCursor();
4601 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
4603 // In a longtable, tell captions what the current float is
4604 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4605 string const saveflt = cnts.current_float();
4606 if (tabular.is_long_tabular) {
4607 cnts.current_float("table");
4608 // in longtables, we only step the counter once
4609 cnts.step(from_ascii("table"), utype);
4610 cnts.isLongtable(true);
4613 ParIterator it2 = it;
4615 size_t const end = it2.nargs();
4616 for ( ; it2.idx() < end; it2.top().forwardIdx())
4617 buffer().updateBuffer(it2, utype);
4620 if (tabular.is_long_tabular) {
4621 cnts.current_float(saveflt);
4622 cnts.isLongtable(false);
4627 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4628 UpdateType utype, TocBackend & backend) const
4630 DocIterator dit = cpit;
4632 size_t const end = dit.nargs();
4633 for ( ; dit.idx() < end; dit.top().forwardIdx())
4634 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4638 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4640 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4641 return x < x0 || x > x0 + tabular.width();
4645 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4647 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4648 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4649 // there is no margin added vertically to tabular insets.
4650 // However, it works for now.
4651 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4655 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4657 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4661 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4663 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4664 << "\n cur:" << cur);
4665 CursorSlice sl = cur.top();
4666 Cursor & bvcur = cur.bv().cursor();
4668 FuncCode const act = cmd.action();
4672 case LFUN_MOUSE_PRESS: {
4673 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4675 if (hitSelectRow(cur.bv(), cmd.x())) {
4676 row_type r = rowFromY(cur, cmd.y());
4677 cur.idx() = tabular.getFirstCellInRow(r);
4681 cur.idx() = tabular.getLastCellInRow(r);
4682 cur.pit() = cur.lastpit();
4683 cur.pos() = cur.lastpos();
4684 cur.selection(true);
4690 if (hitSelectColumn(cur.bv(), cmd.y())) {
4691 col_type c = columnFromX(cur, cmd.x());
4692 cur.idx() = tabular.cellIndex(0, c);
4696 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4697 cur.pit() = cur.lastpit();
4698 cur.pos() = cur.lastpos();
4699 cur.selection(true);
4704 // do not reset cursor/selection if we have selected
4705 // some cells (bug 2715).
4706 if (cmd.button() == mouse_button::button3
4707 && &bvcur.selBegin().inset() == this
4708 && bvcur.selIsMultiCell())
4711 // Let InsetTableCell do it
4712 cell(cur.idx())->dispatch(cur, cmd);
4715 case LFUN_MOUSE_MOTION:
4716 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4717 if (cmd.button() == mouse_button::button1) {
4718 // only accept motions to places not deeper nested than the real anchor
4719 if (!bvcur.realAnchor().hasPart(cur)) {
4723 // select (additional) row
4725 row_type r = rowFromY(cur, cmd.y());
4726 cur.idx() = tabular.getLastCellInRow(r);
4727 // we need to reset the cursor's pit and pos now, as the old ones
4728 // may no longer be valid.
4731 bvcur.setCursor(cur);
4732 bvcur.selection(true);
4735 // select (additional) column
4737 col_type c = columnFromX(cur, cmd.x());
4738 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4739 // we need to reset the cursor's pit and pos now, as the old ones
4740 // may no longer be valid.
4743 bvcur.setCursor(cur);
4744 bvcur.selection(true);
4747 // only update if selection changes
4748 if (bvcur.idx() == cur.idx() &&
4749 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4750 cur.noScreenUpdate();
4751 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4752 bvcur.setCursor(cur);
4753 bvcur.selection(true);
4754 // if this is a multicell selection, we just set the cursor to
4755 // the beginning of the cell's text.
4756 if (bvcur.selIsMultiCell()) {
4757 bvcur.pit() = bvcur.lastpit();
4758 bvcur.pos() = bvcur.lastpos();
4763 case LFUN_MOUSE_RELEASE:
4768 case LFUN_CELL_BACKWARD:
4770 cur.selection(false);
4773 case LFUN_CELL_FORWARD:
4775 cur.selection(false);
4778 case LFUN_CHAR_FORWARD_SELECT:
4779 case LFUN_CHAR_FORWARD:
4780 case LFUN_CHAR_BACKWARD_SELECT:
4781 case LFUN_CHAR_BACKWARD:
4782 case LFUN_CHAR_RIGHT_SELECT:
4783 case LFUN_CHAR_RIGHT:
4784 case LFUN_CHAR_LEFT_SELECT:
4785 case LFUN_CHAR_LEFT:
4786 case LFUN_WORD_FORWARD:
4787 case LFUN_WORD_FORWARD_SELECT:
4788 case LFUN_WORD_BACKWARD:
4789 case LFUN_WORD_BACKWARD_SELECT:
4790 case LFUN_WORD_RIGHT:
4791 case LFUN_WORD_RIGHT_SELECT:
4792 case LFUN_WORD_LEFT:
4793 case LFUN_WORD_LEFT_SELECT: {
4794 // determine whether we move to next or previous cell, where to enter
4795 // the new cell from, and which command to "finish" (i.e., exit the
4798 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4799 FuncCode finish_lfun;
4801 if (act == LFUN_CHAR_FORWARD
4802 || act == LFUN_CHAR_FORWARD_SELECT
4803 || act == LFUN_WORD_FORWARD
4804 || act == LFUN_WORD_FORWARD_SELECT) {
4806 finish_lfun = LFUN_FINISHED_FORWARD;
4808 else if (act == LFUN_CHAR_BACKWARD
4809 || act == LFUN_CHAR_BACKWARD_SELECT
4810 || act == LFUN_WORD_BACKWARD
4811 || act == LFUN_WORD_BACKWARD_SELECT) {
4813 finish_lfun = LFUN_FINISHED_BACKWARD;
4815 // LEFT or RIGHT commands --- the interpretation will depend on the
4816 // table's direction.
4818 bool const right = act == LFUN_CHAR_RIGHT
4819 || act == LFUN_CHAR_RIGHT_SELECT
4820 || act == LFUN_WORD_RIGHT
4821 || act == LFUN_WORD_RIGHT_SELECT;
4822 next_cell = isRightToLeft(cur) != right;
4824 if (lyxrc.visual_cursor)
4825 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4827 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4830 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4831 || act == LFUN_CHAR_BACKWARD_SELECT
4832 || act == LFUN_CHAR_RIGHT_SELECT
4833 || act == LFUN_CHAR_LEFT_SELECT
4834 || act == LFUN_WORD_FORWARD_SELECT
4835 || act == LFUN_WORD_RIGHT_SELECT
4836 || act == LFUN_WORD_BACKWARD_SELECT
4837 || act == LFUN_WORD_LEFT_SELECT;
4839 // If we have a multicell selection or we're
4840 // not doing some LFUN_*_SELECT thing anyway...
4841 if (!cur.selIsMultiCell() || !select) {
4842 col_type const c = tabular.cellColumn(cur.idx());
4843 row_type const r = tabular.cellRow(cur.idx());
4844 // Are we trying to select the whole cell and is the whole cell
4845 // not yet selected?
4846 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4847 ((next_cell && cur.pit() == cur.lastpit()
4848 && cur.pos() == cur.lastpos())
4849 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4851 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4853 // ...try to dispatch to the cell's inset.
4854 cell(cur.idx())->dispatch(cur, cmd);
4856 // When we already have a selection we want to select the whole cell
4857 // before going to the next cell.
4858 if (select_whole && !empty_cell){
4859 getText(cur.idx())->selectAll(cur);
4861 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4865 // FIXME: When we support the selection of an empty cell, remove
4866 // the !empty_cell from this condition. For now we jump to the next
4867 // cell if the current cell is empty.
4868 if (cur.result().dispatched() && !empty_cell)
4872 // move to next/prev cell, as appropriate
4873 // note that we will always do this if we're selecting and we have
4874 // a multicell selection
4875 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4876 << " cell from: " << int(entry_from));
4878 moveNextCell(cur, entry_from);
4880 movePrevCell(cur, entry_from);
4881 // if we're exiting the table, call the appropriate FINISHED lfun
4882 if (sl == cur.top()) {
4883 cmd = FuncRequest(finish_lfun);
4888 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4893 case LFUN_DOWN_SELECT:
4895 if (!(cur.selection() && cur.selIsMultiCell()))
4896 cell(cur.idx())->dispatch(cur, cmd);
4898 cur.dispatched(); // override the cell's decision
4899 if (sl == cur.top()) {
4900 // if our Text didn't do anything to the cursor
4901 // then we try to put the cursor into the cell below
4902 // setting also the right targetX.
4903 cur.selHandle(act == LFUN_DOWN_SELECT);
4904 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4905 int const xtarget = cur.targetX();
4906 // WARNING: Once cur.idx() has been reset, the cursor is in
4907 // an inconsistent state until pos() has been set. Be careful
4908 // what you do with it!
4909 cur.idx() = tabular.cellBelow(cur.idx());
4911 TextMetrics const & tm =
4912 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4913 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4914 cur.setCurrentFont();
4917 if (sl == cur.top()) {
4918 // we trick it to go to forward after leaving the
4920 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4923 if (cur.selIsMultiCell()) {
4924 cur.pit() = cur.lastpit();
4925 cur.pos() = cur.lastpos();
4926 cur.setCurrentFont();
4927 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4930 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4933 case LFUN_UP_SELECT:
4935 if (!(cur.selection() && cur.selIsMultiCell()))
4936 cell(cur.idx())->dispatch(cur, cmd);
4937 cur.dispatched(); // override the cell's decision
4938 if (sl == cur.top()) {
4939 // if our Text didn't do anything to the cursor
4940 // then we try to put the cursor into the cell above
4941 // setting also the right targetX.
4942 cur.selHandle(act == LFUN_UP_SELECT);
4943 if (tabular.cellRow(cur.idx()) != 0) {
4944 int const xtarget = cur.targetX();
4945 // WARNING: Once cur.idx() has been reset, the cursor is in
4946 // an inconsistent state until pos() has been set. Be careful
4947 // what you do with it!
4948 cur.idx() = tabular.cellAbove(cur.idx());
4949 cur.pit() = cur.lastpit();
4950 Text const * text = cell(cur.idx())->getText(0);
4951 TextMetrics const & tm = cur.bv().textMetrics(text);
4952 ParagraphMetrics const & pm =
4953 tm.parMetrics(cur.lastpit());
4954 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4955 cur.setCurrentFont();
4958 if (sl == cur.top()) {
4959 cmd = FuncRequest(LFUN_UP);
4962 if (cur.selIsMultiCell()) {
4964 cur.pos() = cur.lastpos();
4965 cur.setCurrentFont();
4966 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4969 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4972 case LFUN_LAYOUT_TABULAR:
4973 cur.bv().showDialog("tabular");
4976 case LFUN_INSET_MODIFY:
4977 // we come from the dialog
4978 if (cmd.getArg(0) == "tabular")
4979 tabularFeatures(cur, cmd.getLongArg(1));
4984 case LFUN_TABULAR_FEATURE:
4985 tabularFeatures(cur, to_utf8(cmd.argument()));
4988 // insert file functions
4989 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4990 case LFUN_FILE_INSERT_PLAINTEXT:
4992 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4993 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4994 FileName(to_utf8(cmd.argument())));
4997 cur.recordUndoInset();
4998 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4999 // content has been replaced,
5000 // so cursor might be invalid
5001 cur.pos() = cur.lastpos();
5002 cur.pit() = cur.lastpit();
5003 bvcur.setCursor(cur);
5010 if (cur.selIsMultiCell()) {
5011 if (copySelection(cur)) {
5012 cur.recordUndoInset();
5016 cell(cur.idx())->dispatch(cur, cmd);
5019 case LFUN_SELF_INSERT:
5020 if (cur.selIsMultiCell()) {
5021 cur.recordUndoInset();
5023 BufferView * bv = &cur.bv();
5024 docstring::const_iterator cit = cmd.argument().begin();
5025 docstring::const_iterator const end = cmd.argument().end();
5026 for (; cit != end; ++cit)
5027 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5030 bv->bookmarkEditPosition();
5032 cell(cur.idx())->dispatch(cur, cmd);
5035 case LFUN_CHAR_DELETE_BACKWARD:
5036 case LFUN_CHAR_DELETE_FORWARD:
5037 if (cur.selIsMultiCell()) {
5038 cur.recordUndoInset();
5041 cell(cur.idx())->dispatch(cur, cmd);
5045 if (!cur.selection())
5047 if (cur.selIsMultiCell()) {
5051 cell(cur.idx())->dispatch(cur, cmd);
5054 case LFUN_CLIPBOARD_PASTE:
5055 case LFUN_PRIMARY_SELECTION_PASTE: {
5056 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5057 theClipboard().getAsText(Clipboard::PlainTextType) :
5058 theSelection().get();
5061 // pass to InsertPlaintextString, but
5062 // only if we have multi-cell content
5063 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5064 cur.recordUndoInset();
5065 if (insertPlaintextString(cur.bv(), clip, false)) {
5066 // content has been replaced,
5067 // so cursor might be invalid
5068 cur.pos() = cur.lastpos();
5069 cur.pit() = cur.lastpit();
5070 bvcur.setCursor(cur);
5074 // Let the cell handle normal text
5075 cell(cur.idx())->dispatch(cur, cmd);
5080 if (!tabularStackDirty()) {
5081 // Check if we have plain text or HTML with rows/columns.
5082 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5083 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5084 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5085 docstring const clip =
5086 theClipboard().getAsText(Clipboard::AnyTextType);
5087 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5088 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5089 doDispatch(cur, ncmd);
5093 if (!cur.selIsMultiCell())
5094 cell(cur.idx())->dispatch(cur, cmd);
5097 if (theClipboard().isInternal()) {
5098 cur.recordUndoInset();
5099 pasteClipboard(cur);
5103 case LFUN_CHANGE_ACCEPT:
5104 case LFUN_CHANGE_REJECT:
5105 case LFUN_FONT_EMPH:
5106 case LFUN_FONT_BOLD:
5107 case LFUN_FONT_BOLDSYMBOL:
5108 case LFUN_FONT_ROMAN:
5109 case LFUN_FONT_NOUN:
5110 case LFUN_FONT_ITAL:
5111 case LFUN_FONT_FRAK:
5112 case LFUN_FONT_TYPEWRITER:
5113 case LFUN_FONT_SANS:
5114 case LFUN_TEXTSTYLE_APPLY:
5115 case LFUN_TEXTSTYLE_UPDATE:
5116 case LFUN_FONT_SIZE:
5117 case LFUN_FONT_UNDERLINE:
5118 case LFUN_FONT_STRIKEOUT:
5119 case LFUN_FONT_CROSSOUT:
5120 case LFUN_FONT_UNDERUNDERLINE:
5121 case LFUN_FONT_UNDERWAVE:
5123 case LFUN_PARAGRAPH_PARAMS_APPLY:
5124 case LFUN_PARAGRAPH_PARAMS:
5125 case LFUN_WORD_CAPITALIZE:
5126 case LFUN_WORD_UPCASE:
5127 case LFUN_WORD_LOWCASE:
5128 case LFUN_CHARS_TRANSPOSE: {
5129 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5130 if (cur.selIsMultiCell()) {
5133 getSelection(cur, rs, re, cs, ce);
5134 Cursor tmpcur = cur;
5135 for (row_type r = rs; r <= re; ++r) {
5136 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5137 // whole row selected
5138 if (act == LFUN_CHANGE_ACCEPT) {
5139 if (tabular.row_info[r].change == Change::INSERTED)
5140 tabular.row_info[r].change = Change::UNCHANGED;
5141 else if (tabular.row_info[r].change == Change::DELETED) {
5142 tabular.deleteRow(r, true);
5147 if (tabular.row_info[r].change == Change::DELETED)
5148 tabular.row_info[r].change = Change::UNCHANGED;
5149 else if (tabular.row_info[r].change == Change::INSERTED) {
5150 tabular.deleteRow(r, true);
5156 for (col_type c = cs; c <= ce; ++c) {
5157 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5158 // whole col selected
5159 if (act == LFUN_CHANGE_ACCEPT) {
5160 if (tabular.column_info[c].change == Change::INSERTED)
5161 tabular.column_info[c].change = Change::UNCHANGED;
5162 else if (tabular.column_info[c].change == Change::DELETED) {
5163 tabular.deleteColumn(c, true);
5168 if (tabular.column_info[c].change == Change::DELETED)
5169 tabular.column_info[c].change = Change::UNCHANGED;
5170 else if (tabular.column_info[c].change == Change::INSERTED) {
5171 tabular.deleteColumn(c, true);
5177 // cursor follows cell:
5178 tmpcur.idx() = tabular.cellIndex(r, c);
5179 // select this cell only:
5182 tmpcur.resetAnchor();
5183 tmpcur.pit() = tmpcur.lastpit();
5184 tmpcur.pos() = tmpcur.top().lastpos();
5185 tmpcur.setCursor(tmpcur);
5186 tmpcur.setSelection();
5187 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5191 // cursor might be invalid
5196 cell(cur.idx())->dispatch(cur, cmd);
5201 case LFUN_CHANGE_NEXT:
5202 case LFUN_CHANGE_PREVIOUS: {
5203 // BufferView::dispatch has already moved the cursor, we just
5204 // need to select here if we have a changed row or column
5205 if (tabular.row_info[tabular.cellRow(cur.idx())].change != Change::UNCHANGED) {
5207 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5211 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5212 cur.pit() = cur.lastpit();
5213 cur.pos() = cur.lastpos();
5214 cur.selection(true);
5218 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change != Change::UNCHANGED) {
5220 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5224 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5225 cur.pit() = cur.lastpit();
5226 cur.pos() = cur.lastpos();
5227 cur.selection(true);
5234 case LFUN_INSET_SETTINGS:
5235 // relay this lfun to Inset, not to the cell.
5236 Inset::doDispatch(cur, cmd);
5240 // we try to handle this event in the insets dispatch function.
5241 cell(cur.idx())->dispatch(cur, cmd);
5247 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5248 string const & argument, FuncStatus & status) const
5251 int action = Tabular::LAST_ACTION;
5253 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5254 if (tabularFeature[i].feature == s) {
5255 action = tabularFeature[i].action;
5259 if (action == Tabular::LAST_ACTION) {
5261 status.setUnknown(true);
5265 row_type sel_row_start = 0;
5266 row_type sel_row_end = 0;
5267 col_type sel_col_start = 0;
5268 col_type sel_col_end = 0;
5269 Tabular::ltType dummyltt;
5272 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5275 case Tabular::SET_PWIDTH:
5276 case Tabular::SET_MPWIDTH:
5277 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5278 case Tabular::SET_SPECIAL_COLUMN:
5279 case Tabular::SET_SPECIAL_MULTICOLUMN:
5280 case Tabular::APPEND_ROW:
5281 case Tabular::APPEND_COLUMN:
5282 case Tabular::DELETE_ROW:
5283 case Tabular::DELETE_COLUMN:
5284 case Tabular::COPY_ROW:
5285 case Tabular::COPY_COLUMN:
5286 case Tabular::SET_TOP_SPACE:
5287 case Tabular::SET_BOTTOM_SPACE:
5288 case Tabular::SET_INTERLINE_SPACE:
5292 case Tabular::SET_TABULAR_WIDTH:
5293 status.setEnabled(!tabular.rotate
5294 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5297 case Tabular::MOVE_COLUMN_RIGHT:
5298 case Tabular::MOVE_COLUMN_LEFT:
5299 case Tabular::MOVE_ROW_DOWN:
5300 case Tabular::MOVE_ROW_UP: {
5301 if (cur.selection()) {
5302 status.message(_("Selections not supported."));
5303 status.setEnabled(false);
5307 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
5308 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
5309 (action == Tabular::MOVE_COLUMN_LEFT &&
5310 tabular.cellColumn(cur.idx()) == 0) ||
5311 (action == Tabular::MOVE_ROW_DOWN &&
5312 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
5313 (action == Tabular::MOVE_ROW_UP &&
5314 tabular.cellRow(cur.idx()) == 0)) {
5315 status.setEnabled(false);
5319 if (action == Tabular::MOVE_COLUMN_RIGHT ||
5320 action == Tabular::MOVE_COLUMN_LEFT) {
5321 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
5322 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
5323 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
5324 status.message(_("Multi-column in current or"
5325 " destination column."));
5326 status.setEnabled(false);
5331 if (action == Tabular::MOVE_ROW_DOWN ||
5332 action == Tabular::MOVE_ROW_UP) {
5333 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
5334 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
5335 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
5336 status.message(_("Multi-row in current or"
5337 " destination row."));
5338 status.setEnabled(false);
5343 status.setEnabled(true);
5347 case Tabular::SET_DECIMAL_POINT:
5349 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5352 case Tabular::SET_MULTICOLUMN:
5353 case Tabular::UNSET_MULTICOLUMN:
5354 case Tabular::MULTICOLUMN:
5355 // If a row is set as longtable caption, it must not be allowed
5356 // to unset that this row is a multicolumn.
5357 status.setEnabled(sel_row_start == sel_row_end
5358 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5359 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5362 case Tabular::SET_MULTIROW:
5363 case Tabular::UNSET_MULTIROW:
5364 case Tabular::MULTIROW:
5365 // If a row is set as longtable caption, it must not be allowed
5366 // to unset that this row is a multirow.
5367 status.setEnabled(sel_col_start == sel_col_end
5368 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5369 status.setOnOff(tabular.isMultiRow(cur.idx()));
5372 case Tabular::SET_ALL_LINES:
5373 case Tabular::UNSET_ALL_LINES:
5374 case Tabular::SET_INNER_LINES:
5375 case Tabular::SET_BORDER_LINES:
5376 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5379 case Tabular::RESET_FORMAL_DEFAULT:
5380 status.setEnabled(tabular.use_booktabs);
5383 case Tabular::SET_LINE_TOP:
5384 case Tabular::SET_LINE_BOTTOM:
5385 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5388 case Tabular::SET_LINE_LEFT:
5389 case Tabular::SET_LINE_RIGHT:
5390 status.setEnabled(!tabular.use_booktabs
5391 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5394 case Tabular::SET_LTRIM_TOP:
5395 case Tabular::SET_RTRIM_TOP:
5396 status.setEnabled(tabular.use_booktabs
5397 && tabular.cellRow(cur.idx()) != 0
5398 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5401 case Tabular::SET_LTRIM_BOTTOM:
5402 case Tabular::SET_RTRIM_BOTTOM:
5403 status.setEnabled(tabular.use_booktabs
5404 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5405 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5408 case Tabular::TOGGLE_LINE_TOP:
5409 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5410 status.setOnOff(tabular.topLine(cur.idx()));
5413 case Tabular::TOGGLE_LINE_BOTTOM:
5414 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5415 status.setOnOff(tabular.bottomLine(cur.idx()));
5418 case Tabular::TOGGLE_LINE_LEFT:
5419 status.setEnabled(!tabular.use_booktabs
5420 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5421 status.setOnOff(tabular.leftLine(cur.idx()));
5424 case Tabular::TOGGLE_LINE_RIGHT:
5425 status.setEnabled(!tabular.use_booktabs
5426 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5427 status.setOnOff(tabular.rightLine(cur.idx()));
5430 case Tabular::TOGGLE_LTRIM_TOP:
5431 status.setEnabled(tabular.use_booktabs
5432 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5433 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5436 case Tabular::TOGGLE_RTRIM_TOP:
5437 status.setEnabled(tabular.use_booktabs
5438 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5439 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5442 case Tabular::TOGGLE_LTRIM_BOTTOM:
5443 status.setEnabled(tabular.use_booktabs
5444 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5445 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5448 case Tabular::TOGGLE_RTRIM_BOTTOM:
5449 status.setEnabled(tabular.use_booktabs
5450 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5451 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5454 // multirow cells only inherit the alignment of the column if the column has
5455 // no width, otherwise they are left-aligned
5456 // therefore allow always left but right and center only if there is no width
5457 case Tabular::M_ALIGN_LEFT:
5460 case Tabular::ALIGN_LEFT:
5461 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5464 case Tabular::M_ALIGN_RIGHT:
5467 case Tabular::ALIGN_RIGHT:
5468 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5469 && !tabular.getPWidth(cur.idx()).zero()));
5470 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5473 case Tabular::M_ALIGN_CENTER:
5476 case Tabular::ALIGN_CENTER:
5477 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5478 && !tabular.getPWidth(cur.idx()).zero()));
5479 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5482 case Tabular::ALIGN_BLOCK:
5483 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5484 && !tabular.isMultiRow(cur.idx()));
5485 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5488 case Tabular::ALIGN_DECIMAL:
5489 status.setEnabled(!tabular.isMultiRow(cur.idx())
5490 && !tabular.isMultiColumn(cur.idx()));
5491 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5494 case Tabular::M_VALIGN_TOP:
5497 case Tabular::VALIGN_TOP:
5498 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5499 && !tabular.isMultiRow(cur.idx()));
5501 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5504 case Tabular::M_VALIGN_BOTTOM:
5507 case Tabular::VALIGN_BOTTOM:
5508 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5509 && !tabular.isMultiRow(cur.idx()));
5511 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5514 case Tabular::M_VALIGN_MIDDLE:
5517 case Tabular::VALIGN_MIDDLE:
5518 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5519 && !tabular.isMultiRow(cur.idx()));
5521 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5524 case Tabular::SET_LONGTABULAR:
5525 case Tabular::TOGGLE_LONGTABULAR:
5526 // setting as longtable is not allowed when table is inside a float
5527 if (cur.innerInsetOfType(FLOAT_CODE) != 0
5528 || cur.innerInsetOfType(WRAP_CODE) != 0)
5529 status.setEnabled(false);
5531 status.setEnabled(true);
5532 status.setOnOff(tabular.is_long_tabular);
5535 case Tabular::UNSET_LONGTABULAR:
5536 status.setOnOff(!tabular.is_long_tabular);
5539 case Tabular::TOGGLE_ROTATE_TABULAR:
5540 case Tabular::SET_ROTATE_TABULAR:
5541 status.setOnOff(tabular.rotate != 0);
5544 case Tabular::TABULAR_VALIGN_TOP:
5545 status.setEnabled(tabular.tabular_width.zero());
5546 status.setOnOff(tabular.tabular_valignment
5547 == Tabular::LYX_VALIGN_TOP);
5549 case Tabular::TABULAR_VALIGN_MIDDLE:
5550 status.setEnabled(tabular.tabular_width.zero());
5551 status.setOnOff(tabular.tabular_valignment
5552 == Tabular::LYX_VALIGN_MIDDLE);
5554 case Tabular::TABULAR_VALIGN_BOTTOM:
5555 status.setEnabled(tabular.tabular_width.zero());
5556 status.setOnOff(tabular.tabular_valignment
5557 == Tabular::LYX_VALIGN_BOTTOM);
5560 case Tabular::LONGTABULAR_ALIGN_LEFT:
5561 status.setOnOff(tabular.longtabular_alignment
5562 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5564 case Tabular::LONGTABULAR_ALIGN_CENTER:
5565 status.setOnOff(tabular.longtabular_alignment
5566 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5568 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5569 status.setOnOff(tabular.longtabular_alignment
5570 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5573 case Tabular::UNSET_ROTATE_TABULAR:
5574 status.setOnOff(tabular.rotate == 0);
5577 case Tabular::TOGGLE_ROTATE_CELL:
5578 case Tabular::SET_ROTATE_CELL:
5579 status.setOnOff(!oneCellHasRotationState(false,
5580 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5583 case Tabular::UNSET_ROTATE_CELL:
5584 status.setOnOff(!oneCellHasRotationState(true,
5585 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5588 case Tabular::SET_USEBOX:
5589 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5592 // every row can only be one thing:
5593 // either a footer or header
5594 case Tabular::SET_LTFIRSTHEAD:
5595 status.setEnabled(sel_row_start == sel_row_end);
5596 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5599 case Tabular::UNSET_LTFIRSTHEAD:
5600 status.setEnabled(sel_row_start == sel_row_end);
5601 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5604 case Tabular::SET_LTHEAD:
5605 status.setEnabled(sel_row_start == sel_row_end);
5606 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5609 case Tabular::UNSET_LTHEAD:
5610 status.setEnabled(sel_row_start == sel_row_end);
5611 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5614 case Tabular::SET_LTFOOT:
5615 status.setEnabled(sel_row_start == sel_row_end);
5616 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5619 case Tabular::UNSET_LTFOOT:
5620 status.setEnabled(sel_row_start == sel_row_end);
5621 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5624 case Tabular::SET_LTLASTFOOT:
5625 status.setEnabled(sel_row_start == sel_row_end);
5626 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5629 case Tabular::UNSET_LTLASTFOOT:
5630 status.setEnabled(sel_row_start == sel_row_end);
5631 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5634 case Tabular::SET_LTNEWPAGE:
5635 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5637 case Tabular::UNSET_LTNEWPAGE:
5638 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5641 // only one row in head/firsthead/foot/lasthead can be the caption
5642 // and a multirow cannot be set as caption
5643 case Tabular::SET_LTCAPTION:
5644 status.setEnabled(sel_row_start == sel_row_end
5645 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5646 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5647 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5648 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5649 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5650 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5651 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5652 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5653 && !tabular.isMultiRow(sel_row_start));
5654 status.setOnOff(tabular.ltCaption(sel_row_start));
5657 case Tabular::UNSET_LTCAPTION:
5658 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5661 case Tabular::TOGGLE_LTCAPTION:
5662 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5663 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5664 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5665 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5666 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5667 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5668 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5669 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5670 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5671 status.setOnOff(tabular.ltCaption(sel_row_start));
5674 case Tabular::TOGGLE_BOOKTABS:
5675 case Tabular::SET_BOOKTABS:
5676 status.setOnOff(tabular.use_booktabs);
5679 case Tabular::UNSET_BOOKTABS:
5680 status.setOnOff(!tabular.use_booktabs);
5685 status.setEnabled(false);
5692 // function sets an object as defined in FuncStatus.h:
5693 // states OK, Unknown, Disabled, On, Off.
5694 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5695 FuncStatus & status) const
5697 switch (cmd.action()) {
5698 case LFUN_INSET_MODIFY:
5699 if (cmd.getArg(0) != "tabular")
5701 if (cmd.getArg(1) == "for-dialog") {
5702 // The dialog is asking the status of a command
5703 if (&cur.inset() != this)
5705 string action = cmd.getArg(2);
5706 string arg = cmd.getLongArg(3);
5707 return getFeatureStatus(cur, action, arg, status);
5709 // We always enable the lfun if it is coming from the dialog
5710 // because the dialog makes sure all the settings are valid,
5711 // even though the first argument might not be valid now.
5712 status.setEnabled(true);
5716 case LFUN_TABULAR_FEATURE: {
5717 if (&cur.inset() != this)
5719 string action = cmd.getArg(0);
5720 string arg = cmd.getLongArg(1);
5721 return getFeatureStatus(cur, action, arg, status);
5724 case LFUN_CAPTION_INSERT: {
5725 // caption is only allowed in caption cell of longtable
5726 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5727 status.setEnabled(false);
5730 // only standard caption is allowed
5731 string arg = cmd.getArg(0);
5732 if (!arg.empty() && arg != "Standard") {
5733 status.setEnabled(false);
5736 // check if there is already a caption
5737 bool have_caption = false;
5738 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5739 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5740 ParagraphList::const_iterator pend = itc.paragraphs().end();
5741 for (; pit != pend; ++pit) {
5742 InsetList::const_iterator it = pit->insetList().begin();
5743 InsetList::const_iterator end = pit->insetList().end();
5744 for (; it != end; ++it) {
5745 if (it->inset->lyxCode() == CAPTION_CODE) {
5746 have_caption = true;
5751 status.setEnabled(!have_caption);
5755 // These are only enabled inside tabular
5756 case LFUN_CELL_BACKWARD:
5757 case LFUN_CELL_FORWARD:
5758 status.setEnabled(true);
5761 // disable these with multiple cells selected
5762 case LFUN_INSET_INSERT:
5763 case LFUN_TABULAR_INSERT:
5764 case LFUN_TABULAR_STYLE_INSERT:
5765 case LFUN_FLEX_INSERT:
5766 case LFUN_FLOAT_INSERT:
5767 case LFUN_FLOAT_WIDE_INSERT:
5768 case LFUN_FOOTNOTE_INSERT:
5769 case LFUN_MARGINALNOTE_INSERT:
5770 case LFUN_MATH_INSERT:
5771 case LFUN_MATH_MODE:
5772 case LFUN_MATH_MUTATE:
5773 case LFUN_MATH_DISPLAY:
5774 case LFUN_NOTE_INSERT:
5775 case LFUN_ARGUMENT_INSERT:
5776 case LFUN_BOX_INSERT:
5777 case LFUN_BRANCH_INSERT:
5778 case LFUN_PHANTOM_INSERT:
5779 case LFUN_WRAP_INSERT:
5780 case LFUN_PREVIEW_INSERT:
5781 case LFUN_ERT_INSERT: {
5782 if (cur.selIsMultiCell()) {
5783 status.setEnabled(false);
5786 return cell(cur.idx())->getStatus(cur, cmd, status);
5789 case LFUN_CHANGE_ACCEPT:
5790 case LFUN_CHANGE_REJECT: {
5791 if (cur.selIsMultiCell()) {
5794 getSelection(cur, rs, re, cs, ce);
5795 for (row_type r = rs; r <= re; ++r) {
5796 if (tabular.row_info[r].change != Change::UNCHANGED) {
5797 status.setEnabled(true);
5800 for (col_type c = cs; c <= ce; ++c) {
5801 if (tabular.column_info[c].change != Change::UNCHANGED) {
5802 status.setEnabled(true);
5808 if (tabular.row_info[tabular.cellRow(cur.idx())].change != Change::UNCHANGED) {
5809 status.setEnabled(true);
5812 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change != Change::UNCHANGED) {
5813 status.setEnabled(true);
5817 return cell(cur.idx())->getStatus(cur, cmd, status);
5820 // disable in non-fixed-width cells
5821 case LFUN_PARAGRAPH_BREAK:
5822 // multirow does not allow paragraph breaks
5823 if (tabular.isMultiRow(cur.idx())) {
5824 status.setEnabled(false);
5828 case LFUN_NEWLINE_INSERT:
5829 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
5830 && tabular.getPWidth(cur.idx()).zero()) {
5831 status.setEnabled(false);
5834 return cell(cur.idx())->getStatus(cur, cmd, status);
5836 case LFUN_NEWPAGE_INSERT:
5837 status.setEnabled(false);
5841 if (tabularStackDirty() && theClipboard().isInternal()) {
5842 if (cur.selIsMultiCell()) {
5845 getSelection(cur, rs, re, cs, ce);
5846 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5847 && paste_tabular->nrows() == re - rs + 1)
5848 status.setEnabled(true);
5850 status.setEnabled(false);
5851 status.message(_("Selection size should match clipboard content."));
5854 status.setEnabled(true);
5857 return cell(cur.idx())->getStatus(cur, cmd, status);
5859 case LFUN_INSET_SETTINGS:
5860 // relay this lfun to Inset, not to the cell.
5861 return Inset::getStatus(cur, cmd, status);
5864 // we try to handle this event in the insets dispatch function.
5865 return cell(cur.idx())->getStatus(cur, cmd, status);
5871 Inset::DisplayType InsetTabular::display() const
5873 if (tabular.is_long_tabular) {
5874 switch (tabular.longtabular_alignment) {
5875 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5877 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5879 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5889 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5891 tabular.latex(os, runparams);
5895 int InsetTabular::plaintext(odocstringstream & os,
5896 OutputParams const & runparams, size_t max_length) const
5898 os << '\n'; // output table on a new line
5899 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5900 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5901 return PLAINTEXT_NEWLINE;
5905 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5910 // FIXME: Why not pass a proper DocIterator here?
5912 // if the table is inside a float it doesn't need the informaltable
5913 // wrapper. Search for it.
5914 for (master = owner(); master; master = master->owner())
5915 if (master->lyxCode() == FLOAT_CODE)
5920 os << "<informaltable>";
5923 ret += tabular.docbook(os, runparams);
5925 os << "</informaltable>";
5932 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5934 return tabular.xhtml(xs, rp);
5938 void InsetTabular::validate(LaTeXFeatures & features) const
5940 tabular.validate(features);
5941 features.useInsetLayout(getLayout());
5945 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5947 return tabular.cellInset(idx);
5951 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5953 return tabular.cellInset(idx);
5957 void InsetTabular::cursorPos(BufferView const & bv,
5958 CursorSlice const & sl, bool boundary, int & x, int & y) const
5960 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5962 // y offset correction
5963 y += cellYPos(sl.idx());
5964 y += tabular.textVOffset(sl.idx());
5965 y += tabular.offsetVAlignment();
5967 // x offset correction
5968 x += cellXPos(sl.idx());
5969 x += tabular.textHOffset(sl.idx());
5970 x += ADD_TO_TABULAR_WIDTH;
5974 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5978 Inset const & inset = *tabular.cellInset(cell);
5979 Point o = bv.coordCache().getInsets().xy(&inset);
5980 int const xbeg = o.x_ - tabular.textHOffset(cell);
5981 int const xend = xbeg + tabular.cellWidth(cell);
5982 row_type const row = tabular.cellRow(cell);
5983 int const ybeg = o.y_ - tabular.rowAscent(row)
5984 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5985 int const yend = ybeg + tabular.cellHeight(cell);
5997 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5998 // << " ybeg=" << ybeg << " yend=" << yend
5999 // << " xx=" << xx << " yy=" << yy
6000 // << " dist=" << xx + yy << endl;
6005 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6007 //lyxerr << "InsetTabular::editXY: " << this << endl;
6009 cur.idx() = getNearestCell(cur.bv(), x, y);
6010 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6014 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6016 cur.idx() = getNearestCell(cur.bv(), x, y);
6017 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6021 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6023 idx_type idx_min = 0;
6024 int dist_min = numeric_limits<int>::max();
6025 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6026 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6027 int const d = dist(bv, i, x, y);
6038 int InsetTabular::cellYPos(idx_type const cell) const
6040 row_type row = tabular.cellRow(cell);
6042 for (row_type r = 0; r < row; ++r)
6043 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6044 + tabular.interRowSpace(r + 1);
6049 int InsetTabular::cellXPos(idx_type const cell) const
6051 col_type col = tabular.cellColumn(cell);
6053 for (col_type c = 0; c < col; ++c)
6054 lx += tabular.column_info[c].width;
6059 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6061 row_type const row = tabular.cellRow(cur.idx());
6062 col_type const col = tabular.cellColumn(cur.idx());
6064 if (isRightToLeft(cur)) {
6065 if (tabular.cellColumn(cur.idx()) == 0) {
6066 if (row == tabular.nrows() - 1)
6068 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6073 cur.idx() = tabular.getLastCellInRow(row - 1);
6075 cur.idx() = tabular.cellIndex(row, col - 1);
6078 if (tabular.isLastCell(cur.idx()))
6080 if (cur.idx() == tabular.getLastCellInRow(row))
6081 cur.idx() = tabular.cellIndex(row + 1, 0);
6083 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6084 cur.idx() = tabular.cellIndex(row, colnextcell);
6088 cur.boundary(false);
6090 if (cur.selIsMultiCell()) {
6091 cur.pit() = cur.lastpit();
6092 cur.pos() = cur.lastpos();
6099 // in visual mode, place cursor at extreme left or right
6101 switch(entry_from) {
6103 case ENTRY_DIRECTION_RIGHT:
6104 cur.posVisToRowExtremity(false /* !left */);
6106 case ENTRY_DIRECTION_LEFT:
6107 cur.posVisToRowExtremity(true /* left */);
6109 case ENTRY_DIRECTION_IGNORE:
6110 // nothing to do in this case
6114 cur.setCurrentFont();
6118 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6120 row_type const row = tabular.cellRow(cur.idx());
6121 col_type const col = tabular.cellColumn(cur.idx());
6123 if (isRightToLeft(cur)) {
6124 if (cur.idx() == tabular.getLastCellInRow(row)) {
6127 cur.idx() = tabular.getFirstCellInRow(row);
6128 cur.idx() = tabular.cellAbove(cur.idx());
6130 if (tabular.isLastCell(cur.idx()))
6132 if (cur.idx() == tabular.getLastCellInRow(row))
6133 cur.idx() = tabular.cellIndex(row + 1, 0);
6135 cur.idx() = tabular.cellIndex(row, col + 1);
6138 if (cur.idx() == 0) // first cell
6141 cur.idx() = tabular.getLastCellInRow(row - 1);
6143 cur.idx() = tabular.cellIndex(row, col - 1);
6146 if (cur.selIsMultiCell()) {
6147 cur.pit() = cur.lastpit();
6148 cur.pos() = cur.lastpos();
6152 cur.pit() = cur.lastpit();
6153 cur.pos() = cur.lastpos();
6155 // in visual mode, place cursor at extreme left or right
6157 switch(entry_from) {
6159 case ENTRY_DIRECTION_RIGHT:
6160 cur.posVisToRowExtremity(false /* !left */);
6162 case ENTRY_DIRECTION_LEFT:
6163 cur.posVisToRowExtremity(true /* left */);
6165 case ENTRY_DIRECTION_IGNORE:
6166 // nothing to do in this case
6170 cur.setCurrentFont();
6174 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6176 cur.recordUndoInset(this);
6178 istringstream is(argument);
6179 // limit the size of strings we read to avoid memory problems
6183 size_t safe_guard = 0;
6188 if (safe_guard > 1000) {
6189 LYXERR0("parameter max count reached!");
6193 Tabular::Feature action = Tabular::LAST_ACTION;
6196 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6197 if (s != tabularFeature[i].feature)
6199 action = tabularFeature[i].action;
6202 if (action == Tabular::LAST_ACTION) {
6203 LYXERR0("Feature not found " << s);
6207 if (tabularFeature[i].need_value)
6209 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6210 tabularFeatures(cur, action, val);
6215 static void checkLongtableSpecial(Tabular::ltType & ltt,
6216 string const & special, bool & flag)
6218 if (special == "dl_above") {
6221 } else if (special == "dl_below") {
6222 ltt.bottomDL = flag;
6224 } else if (special == "empty") {
6234 bool InsetTabular::oneCellHasRotationState(bool rotated,
6235 row_type row_start, row_type row_end,
6236 col_type col_start, col_type col_end) const
6238 for (row_type r = row_start; r <= row_end; ++r)
6239 for (col_type c = col_start; c <= col_end; ++c)
6241 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6244 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6251 void InsetTabular::tabularFeatures(Cursor & cur,
6252 Tabular::Feature feature, string const & value)
6254 col_type sel_col_start;
6255 col_type sel_col_end;
6256 row_type sel_row_start;
6257 row_type sel_row_end;
6258 bool setLines = false;
6259 bool setLinesInnerOnly = false;
6260 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6261 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6265 case Tabular::M_ALIGN_LEFT:
6266 case Tabular::ALIGN_LEFT:
6267 setAlign = LYX_ALIGN_LEFT;
6270 case Tabular::M_ALIGN_RIGHT:
6271 case Tabular::ALIGN_RIGHT:
6272 setAlign = LYX_ALIGN_RIGHT;
6275 case Tabular::M_ALIGN_CENTER:
6276 case Tabular::ALIGN_CENTER:
6277 setAlign = LYX_ALIGN_CENTER;
6280 case Tabular::ALIGN_BLOCK:
6281 setAlign = LYX_ALIGN_BLOCK;
6284 case Tabular::ALIGN_DECIMAL:
6285 setAlign = LYX_ALIGN_DECIMAL;
6288 case Tabular::M_VALIGN_TOP:
6289 case Tabular::VALIGN_TOP:
6290 setVAlign = Tabular::LYX_VALIGN_TOP;
6293 case Tabular::M_VALIGN_BOTTOM:
6294 case Tabular::VALIGN_BOTTOM:
6295 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6298 case Tabular::M_VALIGN_MIDDLE:
6299 case Tabular::VALIGN_MIDDLE:
6300 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6307 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6308 row_type const row = tabular.cellRow(cur.idx());
6309 col_type const column = tabular.cellColumn(cur.idx());
6311 Tabular::ltType ltt;
6315 case Tabular::SET_TABULAR_WIDTH:
6316 tabular.setTabularWidth(Length(value));
6319 case Tabular::SET_PWIDTH: {
6320 Length const len(value);
6321 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6322 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6324 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6325 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6330 case Tabular::SET_MPWIDTH:
6331 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6334 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6335 bool const varwidth = value == "on";
6336 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6337 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6341 case Tabular::SET_MROFFSET:
6342 tabular.setMROffset(cur, cur.idx(), Length(value));
6345 case Tabular::SET_SPECIAL_COLUMN:
6346 case Tabular::SET_SPECIAL_MULTICOLUMN:
6347 if (value == "none")
6348 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6350 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6353 case Tabular::APPEND_ROW:
6354 // append the row into the tabular
6355 tabular.appendRow(row);
6358 case Tabular::APPEND_COLUMN:
6359 // append the column into the tabular
6360 tabular.appendColumn(column);
6361 cur.idx() = tabular.cellIndex(row, column);
6364 case Tabular::DELETE_ROW:
6365 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6366 for (col_type c = 0; c < tabular.ncols(); c++) {
6367 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6368 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6369 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6370 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6374 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6375 tabular.deleteRow(sel_row_start);
6376 if (sel_row_start >= tabular.nrows())
6378 cur.idx() = tabular.cellIndex(sel_row_start, column);
6381 cur.selection(false);
6384 case Tabular::DELETE_COLUMN:
6385 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6386 for (row_type r = 0; r < tabular.nrows(); r++)
6387 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6388 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6391 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6392 for (row_type r = 0; r < tabular.nrows(); r++)
6393 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6394 tabular.leftLine(tabular.cellIndex(r, 0)));
6397 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6398 tabular.deleteColumn(sel_col_start);
6399 if (sel_col_start >= tabular.ncols())
6401 cur.idx() = tabular.cellIndex(row, sel_col_start);
6404 cur.selection(false);
6407 case Tabular::COPY_ROW:
6408 tabular.copyRow(row);
6411 case Tabular::COPY_COLUMN:
6412 tabular.copyColumn(column);
6413 cur.idx() = tabular.cellIndex(row, column);
6416 case Tabular::MOVE_COLUMN_RIGHT:
6417 tabular.moveColumn(column, Tabular::RIGHT);
6418 cur.idx() = tabular.cellIndex(row, column + 1);
6421 case Tabular::MOVE_COLUMN_LEFT:
6422 tabular.moveColumn(column, Tabular::LEFT);
6423 cur.idx() = tabular.cellIndex(row, column - 1);
6426 case Tabular::MOVE_ROW_DOWN:
6427 tabular.moveRow(row, Tabular::DOWN);
6428 cur.idx() = tabular.cellIndex(row + 1, column);
6431 case Tabular::MOVE_ROW_UP:
6432 tabular.moveRow(row, Tabular::UP);
6433 cur.idx() = tabular.cellIndex(row - 1, column);
6436 case Tabular::SET_LINE_TOP:
6437 case Tabular::TOGGLE_LINE_TOP: {
6438 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6439 ? (value == "true") : !tabular.topLine(cur.idx());
6440 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6441 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6442 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6446 case Tabular::SET_LINE_BOTTOM:
6447 case Tabular::TOGGLE_LINE_BOTTOM: {
6448 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6449 ? (value == "true") : !tabular.bottomLine(cur.idx());
6450 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6451 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6452 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6456 case Tabular::SET_LTRIM_TOP:
6457 case Tabular::TOGGLE_LTRIM_TOP: {
6458 bool l = (feature == Tabular::SET_LTRIM_TOP)
6459 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6460 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6461 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6462 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6466 case Tabular::SET_RTRIM_TOP:
6467 case Tabular::TOGGLE_RTRIM_TOP: {
6468 bool l = (feature == Tabular::SET_RTRIM_TOP)
6469 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6470 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6471 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6472 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6476 case Tabular::SET_LTRIM_BOTTOM:
6477 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6478 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6479 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6480 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6481 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6482 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6486 case Tabular::SET_RTRIM_BOTTOM:
6487 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6488 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6489 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6490 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6491 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6492 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6496 case Tabular::SET_LINE_LEFT:
6497 case Tabular::TOGGLE_LINE_LEFT: {
6498 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6499 ? (value == "true") : !tabular.leftLine(cur.idx());
6500 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6501 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6502 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6506 case Tabular::SET_LINE_RIGHT:
6507 case Tabular::TOGGLE_LINE_RIGHT: {
6508 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6509 ? (value == "true") : !tabular.rightLine(cur.idx());
6510 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6511 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6512 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6516 case Tabular::M_ALIGN_LEFT:
6517 case Tabular::M_ALIGN_RIGHT:
6518 case Tabular::M_ALIGN_CENTER:
6519 case Tabular::ALIGN_LEFT:
6520 case Tabular::ALIGN_RIGHT:
6521 case Tabular::ALIGN_CENTER:
6522 case Tabular::ALIGN_BLOCK:
6523 case Tabular::ALIGN_DECIMAL:
6524 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6525 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6526 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6527 !tabular.getPWidth(c).zero());
6530 case Tabular::M_VALIGN_TOP:
6531 case Tabular::M_VALIGN_BOTTOM:
6532 case Tabular::M_VALIGN_MIDDLE:
6535 case Tabular::VALIGN_TOP:
6536 case Tabular::VALIGN_BOTTOM:
6537 case Tabular::VALIGN_MIDDLE:
6538 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6539 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6540 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6543 case Tabular::SET_MULTICOLUMN: {
6544 if (!cur.selection()) {
6545 // just multicol for one single cell
6546 // check whether we are completely in a multicol
6547 if (!tabular.isMultiColumn(cur.idx()))
6548 tabular.setMultiColumn(cur, cur.idx(), 1,
6549 tabular.rightLine(cur.idx()));
6552 // we have a selection so this means we just add all these
6553 // cells to form a multicolumn cell
6554 idx_type const s_start = cur.selBegin().idx();
6555 row_type const col_start = tabular.cellColumn(s_start);
6556 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6557 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6558 tabular.rightLine(cur.selEnd().idx()));
6561 cur.selection(false);
6565 case Tabular::UNSET_MULTICOLUMN: {
6566 if (!cur.selection()) {
6567 if (tabular.isMultiColumn(cur.idx()))
6568 tabular.unsetMultiColumn(cur.idx());
6573 case Tabular::MULTICOLUMN: {
6574 if (!cur.selection()) {
6575 if (tabular.isMultiColumn(cur.idx()))
6576 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6578 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6582 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6583 row_type const r = sel_row_start;
6584 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6585 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6588 // If the selection contains at least one singlecol cell
6589 // or multiple multicol cells,
6590 // we assume the user will merge is to a single multicol
6592 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6594 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6598 case Tabular::SET_MULTIROW: {
6599 if (!cur.selection()) {
6600 // just multirow for one single cell
6601 // check whether we are completely in a multirow
6602 if (!tabular.isMultiRow(cur.idx()))
6603 tabular.setMultiRow(cur, cur.idx(), 1,
6604 tabular.bottomLine(cur.idx()),
6605 tabular.getAlignment(cur.idx()));
6608 // we have a selection so this means we just add all this
6609 // cells to form a multirow cell
6610 idx_type const s_start = cur.selBegin().idx();
6611 row_type const row_start = tabular.cellRow(s_start);
6612 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6613 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6614 tabular.bottomLine(cur.selEnd().idx()),
6615 tabular.getAlignment(cur.selEnd().idx()));
6618 cur.selection(false);
6622 case Tabular::UNSET_MULTIROW: {
6623 if (!cur.selection()) {
6624 if (tabular.isMultiRow(cur.idx()))
6625 tabular.unsetMultiRow(cur.idx());
6630 case Tabular::MULTIROW: {
6631 if (!cur.selection()) {
6632 if (tabular.isMultiRow(cur.idx()))
6633 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6635 tabularFeatures(cur, Tabular::SET_MULTIROW);
6639 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6640 col_type const c = sel_col_start;
6641 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6642 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6645 // If the selection contains at least one singlerow cell
6646 // or multiple multirow cells,
6647 // we assume the user will merge is to a single multirow
6649 tabularFeatures(cur, Tabular::SET_MULTIROW);
6651 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6655 case Tabular::SET_INNER_LINES:
6656 setLinesInnerOnly = true;
6658 case Tabular::SET_ALL_LINES:
6661 case Tabular::UNSET_ALL_LINES:
6662 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6663 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6664 idx_type const cell = tabular.cellIndex(r, c);
6665 if (!setLinesInnerOnly || r != sel_row_start)
6666 tabular.setTopLine(cell, setLines);
6667 if ((!setLinesInnerOnly || r != sel_row_end)
6668 && (!setLines || r == sel_row_end))
6669 tabular.setBottomLine(cell, setLines);
6670 if ((!setLinesInnerOnly || c != sel_col_end)
6671 && (!setLines || c == sel_col_end))
6672 tabular.setRightLine(cell, setLines);
6673 if ((!setLinesInnerOnly || c != sel_col_start))
6674 tabular.setLeftLine(cell, setLines);
6678 case Tabular::RESET_FORMAL_DEFAULT:
6679 for (row_type r = 0; r < tabular.nrows(); ++r) {
6680 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6681 for (col_type c = 0; c < tabular.ncols(); ++c) {
6682 idx_type const cell = tabular.cellIndex(r, c);
6683 tabular.setTopLine(cell, head_or_foot);
6684 tabular.setBottomLine(cell, head_or_foot);
6689 case Tabular::SET_BORDER_LINES:
6690 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6691 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6692 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6694 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6695 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6696 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6700 case Tabular::TOGGLE_LONGTABULAR:
6701 if (tabular.is_long_tabular)
6702 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6704 tabular.is_long_tabular = true;
6707 case Tabular::SET_LONGTABULAR:
6708 tabular.is_long_tabular = true;
6711 case Tabular::UNSET_LONGTABULAR:
6712 for (row_type r = 0; r < tabular.nrows(); ++r) {
6713 if (tabular.ltCaption(r)) {
6714 cur.idx() = tabular.cellIndex(r, 0);
6717 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6720 tabular.is_long_tabular = false;
6723 case Tabular::SET_ROTATE_TABULAR:
6724 tabular.rotate = convert<int>(value);
6727 case Tabular::UNSET_ROTATE_TABULAR:
6731 case Tabular::TOGGLE_ROTATE_TABULAR:
6732 // when pressing the rotate button we default to 90° rotation
6733 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6736 case Tabular::TABULAR_VALIGN_TOP:
6737 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6740 case Tabular::TABULAR_VALIGN_MIDDLE:
6741 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6744 case Tabular::TABULAR_VALIGN_BOTTOM:
6745 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6748 case Tabular::LONGTABULAR_ALIGN_LEFT:
6749 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6752 case Tabular::LONGTABULAR_ALIGN_CENTER:
6753 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6756 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6757 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6760 case Tabular::SET_ROTATE_CELL:
6761 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6762 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6763 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6766 case Tabular::UNSET_ROTATE_CELL:
6767 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6768 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6769 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6772 case Tabular::TOGGLE_ROTATE_CELL:
6774 bool oneNotRotated = oneCellHasRotationState(false,
6775 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6777 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6778 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6779 // when pressing the rotate cell button we default to 90° rotation
6781 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6783 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6788 case Tabular::SET_USEBOX: {
6789 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6790 if (val == tabular.getUsebox(cur.idx()))
6791 val = Tabular::BOX_NONE;
6792 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6793 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6794 tabular.setUsebox(tabular.cellIndex(r, c), val);
6798 case Tabular::UNSET_LTFIRSTHEAD:
6801 case Tabular::SET_LTFIRSTHEAD:
6802 tabular.getRowOfLTFirstHead(row, ltt);
6803 checkLongtableSpecial(ltt, value, flag);
6804 tabular.setLTHead(row, flag, ltt, true);
6807 case Tabular::UNSET_LTHEAD:
6810 case Tabular::SET_LTHEAD:
6811 tabular.getRowOfLTHead(row, ltt);
6812 checkLongtableSpecial(ltt, value, flag);
6813 tabular.setLTHead(row, flag, ltt, false);
6816 case Tabular::UNSET_LTFOOT:
6819 case Tabular::SET_LTFOOT:
6820 tabular.getRowOfLTFoot(row, ltt);
6821 checkLongtableSpecial(ltt, value, flag);
6822 tabular.setLTFoot(row, flag, ltt, false);
6825 case Tabular::UNSET_LTLASTFOOT:
6828 case Tabular::SET_LTLASTFOOT:
6829 tabular.getRowOfLTLastFoot(row, ltt);
6830 checkLongtableSpecial(ltt, value, flag);
6831 tabular.setLTFoot(row, flag, ltt, true);
6834 case Tabular::UNSET_LTNEWPAGE:
6837 case Tabular::SET_LTNEWPAGE:
6838 tabular.setLTNewPage(row, flag);
6841 case Tabular::SET_LTCAPTION: {
6842 if (tabular.ltCaption(row))
6844 cur.idx() = tabular.setLTCaption(cur, row, true);
6847 cur.selection(false);
6848 // If a row is set as caption, then also insert
6849 // a caption. Otherwise the LaTeX output is broken.
6850 // Select cell if it is non-empty
6851 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6852 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6853 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6857 case Tabular::UNSET_LTCAPTION: {
6858 if (!tabular.ltCaption(row))
6860 cur.idx() = tabular.setLTCaption(cur, row, false);
6863 cur.selection(false);
6864 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6865 if (lyx::getStatus(fr).enabled())
6870 case Tabular::TOGGLE_LTCAPTION: {
6871 if (tabular.ltCaption(row))
6872 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6874 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6878 case Tabular::TOGGLE_BOOKTABS:
6879 tabular.use_booktabs = !tabular.use_booktabs;
6882 case Tabular::SET_BOOKTABS:
6883 tabular.use_booktabs = true;
6886 case Tabular::UNSET_BOOKTABS:
6887 tabular.use_booktabs = false;
6890 case Tabular::SET_TOP_SPACE: {
6892 if (value == "default")
6893 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6894 tabular.row_info[r].top_space_default = true;
6895 else if (value == "none")
6896 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6897 tabular.row_info[r].top_space_default = false;
6898 tabular.row_info[r].top_space = len;
6900 else if (isValidLength(value, &len))
6901 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6902 tabular.row_info[r].top_space_default = false;
6903 tabular.row_info[r].top_space = len;
6908 case Tabular::SET_BOTTOM_SPACE: {
6910 if (value == "default")
6911 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6912 tabular.row_info[r].bottom_space_default = true;
6913 else if (value == "none")
6914 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6915 tabular.row_info[r].bottom_space_default = false;
6916 tabular.row_info[r].bottom_space = len;
6918 else if (isValidLength(value, &len))
6919 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6920 tabular.row_info[r].bottom_space_default = false;
6921 tabular.row_info[r].bottom_space = len;
6926 case Tabular::SET_INTERLINE_SPACE: {
6928 if (value == "default")
6929 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6930 tabular.row_info[r].interline_space_default = true;
6931 else if (value == "none")
6932 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6933 tabular.row_info[r].interline_space_default = false;
6934 tabular.row_info[r].interline_space = len;
6936 else if (isValidLength(value, &len))
6937 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6938 tabular.row_info[r].interline_space_default = false;
6939 tabular.row_info[r].interline_space = len;
6944 case Tabular::SET_DECIMAL_POINT:
6945 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6946 tabular.column_info[c].decimal_point = from_utf8(value);
6949 // dummy stuff just to avoid warnings
6950 case Tabular::LAST_ACTION:
6956 bool InsetTabular::copySelection(Cursor & cur)
6958 if (!cur.selection())
6963 getSelection(cur, rs, re, cs, ce);
6965 paste_tabular.reset(new Tabular(tabular));
6967 for (row_type r = 0; r < rs; ++r)
6968 paste_tabular->deleteRow(0);
6970 row_type const rows = re - rs + 1;
6971 while (paste_tabular->nrows() > rows)
6972 paste_tabular->deleteRow(rows);
6974 for (col_type c = 0; c < cs; ++c)
6975 paste_tabular->deleteColumn(0);
6977 col_type const columns = ce - cs + 1;
6978 while (paste_tabular->ncols() > columns)
6979 paste_tabular->deleteColumn(columns);
6981 paste_tabular->setBuffer(tabular.buffer());
6983 odocstringstream os;
6984 OutputParams const runparams(0);
6985 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6986 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6987 cap::copySelection(cur, os.str());
6989 // mark tabular stack dirty
6990 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6991 // when we (hopefully) have a one-for-all paste mechanism.
6992 // This must be called after cap::copySelection.
6993 dirtyTabularStack(true);
6999 bool InsetTabular::pasteClipboard(Cursor & cur)
7003 col_type actcol = tabular.cellColumn(cur.idx());
7004 row_type actrow = tabular.cellRow(cur.idx());
7006 if (cur.selIsMultiCell()) {
7009 getSelection(cur, actrow, re, actcol, ce);
7012 for (row_type r1 = 0, r2 = actrow;
7013 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
7015 for (col_type c1 = 0, c2 = actcol;
7016 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
7018 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7019 tabular.isPartOfMultiColumn(r2, c2))
7021 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7025 if (tabular.isPartOfMultiColumn(r2, c2)) {
7029 shared_ptr<InsetTableCell> inset(
7030 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7031 tabular.setCellInset(r2, c2, inset);
7032 // FIXME?: why do we need to do this explicitly? (EL)
7033 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7035 // FIXME: change tracking (MG)
7036 inset->setChange(Change(buffer().params().track_changes ?
7037 Change::INSERTED : Change::UNCHANGED));
7046 void InsetTabular::cutSelection(Cursor & cur)
7048 if (!cur.selection())
7053 getSelection(cur, rs, re, cs, ce);
7054 for (row_type r = rs; r <= re; ++r) {
7055 for (col_type c = cs; c <= ce; ++c) {
7056 shared_ptr<InsetTableCell> t
7057 = cell(tabular.cellIndex(r, c));
7058 if (buffer().params().track_changes)
7059 // FIXME: Change tracking (MG)
7060 t->setChange(Change(Change::DELETED));
7066 // cursor position might be invalid now
7067 if (cur.pit() > cur.lastpit())
7068 cur.pit() = cur.lastpit();
7069 if (cur.pos() > cur.lastpos())
7070 cur.pos() = cur.lastpos();
7071 cur.clearSelection();
7075 bool InsetTabular::isRightToLeft(Cursor & cur) const
7077 // LASSERT: It might be better to abandon this Buffer.
7078 LASSERT(cur.depth() > 1, return false);
7079 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7080 pos_type const parentpos = cur[cur.depth() - 2].pos();
7081 return parentpar.getFontSettings(buffer().params(),
7082 parentpos).language()->rightToLeft();
7086 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7089 LASSERT(stidx <= enidx, return docstring());
7091 col_type const col1 = tabular.cellColumn(stidx);
7092 col_type const col2 = tabular.cellColumn(enidx);
7093 row_type const row1 = tabular.cellRow(stidx);
7094 row_type const row2 = tabular.cellRow(enidx);
7096 for (col_type col = col1; col <= col2; col++)
7097 for (row_type row = row1; row <= row2; row++) {
7102 retval += tabular.cellInset(row, col)->asString(intoInsets);
7108 void InsetTabular::getSelection(Cursor & cur,
7109 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7111 CursorSlice const & beg = cur.selBegin();
7112 CursorSlice const & end = cur.selEnd();
7113 cs = tabular.cellColumn(beg.idx());
7114 ce = tabular.cellColumn(end.idx());
7118 rs = tabular.cellRow(beg.idx());
7119 re = tabular.cellRow(end.idx());
7125 Text * InsetTabular::getText(int idx) const
7127 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
7131 bool InsetTabular::isChanged() const
7133 for (idx_type idx = 0; idx < nargs(); ++idx)
7134 if (cell(idx)->isChanged())
7136 // FIXME: shall we look at row/columns changed status?
7141 void InsetTabular::setChange(Change const & change)
7143 for (idx_type idx = 0; idx < nargs(); ++idx)
7144 cell(idx)->setChange(change);
7148 void InsetTabular::acceptChanges()
7150 for (idx_type idx = 0; idx < nargs(); ++idx)
7151 cell(idx)->acceptChanges();
7152 for (row_type row = 0; row < tabular.nrows(); ++row) {
7153 if (tabular.row_info[row].change == Change::INSERTED)
7154 tabular.row_info[row].change = Change::UNCHANGED;
7155 else if (tabular.row_info[row].change == Change::DELETED)
7156 tabular.deleteRow(row, true);
7158 for (col_type col = 0; col < tabular.ncols(); ++col) {
7159 if (tabular.column_info[col].change == Change::INSERTED)
7160 tabular.column_info[col].change = Change::UNCHANGED;
7161 else if (tabular.column_info[col].change == Change::DELETED)
7162 tabular.deleteColumn(col, true);
7167 void InsetTabular::rejectChanges()
7169 for (idx_type idx = 0; idx < nargs(); ++idx)
7170 cell(idx)->rejectChanges();
7171 for (row_type row = 0; row < tabular.nrows(); ++row) {
7172 if (tabular.row_info[row].change == Change::DELETED)
7173 tabular.row_info[row].change = Change::UNCHANGED;
7174 else if (tabular.row_info[row].change == Change::INSERTED)
7175 tabular.deleteRow(row, true);
7177 for (col_type col = 0; col < tabular.ncols(); ++col) {
7178 if (tabular.column_info[col].change == Change::DELETED)
7179 tabular.column_info[col].change = Change::UNCHANGED;
7180 else if (tabular.column_info[col].change == Change::INSERTED)
7181 tabular.deleteColumn(col, true);
7186 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7188 return tabular.getPWidth(cell).zero();
7192 bool InsetTabular::forcePlainLayout(idx_type cell) const
7194 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
7198 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7201 if (buf.length() <= 0)
7206 col_type maxCols = 1;
7207 size_t const len = buf.length();
7211 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7219 maxCols = max(cols, maxCols);
7225 maxCols = max(cols, maxCols);
7231 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7232 loctab = paste_tabular.get();
7233 dirtyTabularStack(true);
7236 cell = bv.cursor().idx();
7237 ocol = tabular.cellColumn(cell);
7238 row = tabular.cellRow(cell);
7242 idx_type cells = loctab->numberofcells;
7245 rows = loctab->nrows();
7246 col_type columns = loctab->ncols();
7249 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7255 // append column if necessary
7256 if (cols == columns) {
7257 loctab->appendColumn(cols - 1);
7258 columns = loctab->ncols();
7259 cells = loctab->numberofcells;
7262 if (cols < columns) {
7263 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7264 Font const font = bv.textMetrics(&inset->text()).
7266 inset->setText(buf.substr(op, p - op), font,
7267 buffer().params().track_changes);
7273 // we can only set this if we are not too far right
7274 if (cols < columns) {
7275 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7276 Font const font = bv.textMetrics(&inset->text()).
7278 inset->setText(buf.substr(op, p - op), font,
7279 buffer().params().track_changes);
7283 // append row if necessary
7284 if (row == rows && p < len - 1) {
7285 loctab->appendRow(row - 1);
7286 rows = loctab->nrows();
7287 cells = loctab->numberofcells;
7290 cell = loctab->cellIndex(row, cols);
7296 // check for the last cell if there is no trailing '\n'
7297 if (cell < cells && op < len) {
7298 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7299 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7300 inset->setText(buf.substr(op, len - op), font,
7301 buffer().params().track_changes);
7307 void InsetTabular::addPreview(DocIterator const & inset_pos,
7308 PreviewLoader & loader) const
7310 DocIterator cell_pos = inset_pos;
7312 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7313 for (row_type r = 0; r < tabular.nrows(); ++r) {
7314 for (col_type c = 0; c < tabular.ncols(); ++c) {
7315 cell_pos.top().idx() = tabular.cellIndex(r, c);
7316 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7322 bool InsetTabular::completionSupported(Cursor const & cur) const
7324 Cursor const & bvCur = cur.bv().cursor();
7325 if (&bvCur.inset() != this)
7327 return cur.text()->completionSupported(cur);
7331 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7333 return completionSupported(cur);
7337 bool InsetTabular::automaticInlineCompletion() const
7339 return lyxrc.completion_inline_text;
7343 bool InsetTabular::automaticPopupCompletion() const
7345 return lyxrc.completion_popup_text;
7349 bool InsetTabular::showCompletionCursor() const
7351 return lyxrc.completion_cursor_text;
7355 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7357 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
7361 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7363 if (!completionSupported(cur))
7365 return cur.text()->completionPrefix(cur);
7369 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7371 if (!completionSupported(cur))
7374 return cur.text()->insertCompletion(cur, s, finished);
7378 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7379 Dimension & dim) const
7381 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7382 tm.completionPosAndDim(cur, x, y, dim);
7386 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7388 istringstream data(in);
7390 lex.setStream(data);
7397 if (!lex || token != "tabular") {
7398 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7402 // This is part of the inset proper that is usually swallowed
7403 // by Buffer::readInset
7405 if (!lex || token != "Tabular") {
7406 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7414 string InsetTabular::params2string(InsetTabular const & inset)
7417 data << "tabular" << ' ';
7419 data << "\\end_inset\n";
7424 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7426 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
7427 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
7428 if (!tabular.isPartOfMultiColumn(r,c) &&
7429 !tabular.isPartOfMultiRow(r,c))
7432 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7433 ParagraphList::iterator it = parlist.begin();
7434 ParagraphList::iterator const en = parlist.end();
7435 for (; it != en; ++it)
7436 it->setLayout(dc.plainLayout());