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"
26 #include "buffer_funcs.h"
28 #include "BufferParams.h"
29 #include "BufferView.h"
30 #include "CoordCache.h"
33 #include "CutAndPaste.h"
34 #include "DispatchResult.h"
35 #include "FuncRequest.h"
36 #include "FuncStatus.h"
37 #include "InsetIterator.h"
38 #include "InsetList.h"
40 #include "LaTeXFeatures.h"
44 #include "MetricsInfo.h"
46 #include "output_xhtml.h"
47 #include "Paragraph.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 & change, BufferParams & bp)
456 // set the change to be Change() as default as this it should be if not
457 // in the file format.
460 if (getTokenValue(str, token, tmp)) {
461 vector<string> const changedata = getVectorFromString(tmp, " ");
462 if (changedata.size() != 3) {
463 Alert::warning(_("Change tracking data incomplete"),
464 _("Change tracking information for tabular row/column "
465 "is incomplete. I will ignore this."));
468 BufferParams::AuthorMap const & am = bp.author_map_;
469 int aid = convert<int>(changedata[1]);
470 if (am.find(aid) == am.end()) {
471 // FIXME Use ErrorList
472 Alert::warning(_("Change tracking author index missing"),
473 bformat(_("A change tracking author information for index "
474 "%1$d is missing. This can happen after a wrong "
475 "merge by a version control system. In this case, "
476 "either fix the merge, or have this information "
477 "missing until the corresponding tracked changes "
478 "are merged or this user edits the file again.\n"),
480 bp.addAuthor(Author(aid));
482 istringstream is(changedata[2]);
485 if (changedata[0] == "inserted") {
486 change = Change(Change::INSERTED, am.find(aid)->second, ct);
488 } else if (changedata[0] == "deleted") {
489 change = Change(Change::DELETED, am.find(aid)->second, ct);
497 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
502 if (!getTokenValue(str, token, tmp))
504 if (tmp == "default") {
508 return isValidLength(tmp, &len);
512 void l_getline(istream & is, string & str)
515 while (str.empty()) {
517 if (!str.empty() && str[str.length() - 1] == '\r')
518 str.erase(str.length() - 1);
523 string const write_attribute(string const & name, T const & t)
525 string const s = tostr(t);
526 return s.empty() ? s : " " + name + "=\"" + s + "\"";
530 string const write_attribute(string const & name, string const & t)
532 return t.empty() ? t : " " + name + "=\"" + t + "\"";
537 string const write_attribute(string const & name, docstring const & t)
539 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
544 string const write_attribute(string const & name, bool const & b)
546 // we write only true attribute values so we remove a bit of the
547 // file format bloat for tabulars.
548 return b ? write_attribute(name, convert<string>(b)) : string();
553 string const write_attribute(string const & name, int const & i)
555 // we write only true attribute values so we remove a bit of the
556 // file format bloat for tabulars.
557 return i ? write_attribute(name, convert<string>(i)) : string();
562 string const write_attribute(string const & name, idx_type const & i)
564 // we write only true attribute values so we remove a bit of the
565 // file format bloat for tabulars.
566 return i ? write_attribute(name, convert<string>(i)) : string();
571 string const write_attribute(string const & name, Length const & value)
573 // we write only the value if we really have one same reason as above.
574 return value.zero() ? string() : write_attribute(name, value.asString());
577 string const write_attribute(string const & name, Change const & change, BufferParams const & bp)
579 odocstringstream ods;
580 if (change.inserted())
581 ods << from_ascii("inserted");
582 else if (change.deleted())
583 ods << from_ascii("deleted");
584 if (change.changed()) {
585 ods << " " << bp.authors().get(change.author).bufferId()
586 << " " << change.changetime;
587 return write_attribute(name, ods.str());
595 string const featureAsString(Tabular::Feature action)
597 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
598 if (tabularFeature[i].action == action)
599 return tabularFeature[i].feature;
605 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
607 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
608 for (; dit; dit.forwardChar())
609 if (dit.inTexted() && dit.depth() == 1
610 && dit.paragraph().find(align_d, false, false, dit.pos()))
617 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
619 InsetTableCell tail = InsetTableCell(head);
620 DocIterator const dit = separatorPos(&head, align_d);
621 hassep = static_cast<bool>(dit);
623 pos_type const psize = head.paragraphs().front().size();
624 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
625 tail.paragraphs().front().eraseChars(0,
626 dit.pos() < psize ? dit.pos() + 1 : psize, false);
633 /////////////////////////////////////////////////////////////////////
637 /////////////////////////////////////////////////////////////////////
640 Tabular::CellData::CellData(Buffer * buf)
643 multicolumn(Tabular::CELL_NORMAL),
644 multirow(Tabular::CELL_NORMAL),
645 alignment(LYX_ALIGN_CENTER),
646 valignment(LYX_VALIGN_TOP),
654 top_line_rtrimmed(false),
655 top_line_ltrimmed(false),
656 bottom_line_rtrimmed(false),
657 bottom_line_ltrimmed(false),
660 inset(new InsetTableCell(buf))
662 inset->setBuffer(*buf);
666 Tabular::CellData::CellData(CellData const & cs)
669 multicolumn(cs.multicolumn),
670 multirow(cs.multirow),
671 mroffset(cs.mroffset),
672 alignment(cs.alignment),
673 valignment(cs.valignment),
674 decimal_hoffset(cs.decimal_hoffset),
675 decimal_width(cs.decimal_width),
677 top_line(cs.top_line),
678 bottom_line(cs.bottom_line),
679 left_line(cs.left_line),
680 right_line(cs.right_line),
681 top_line_rtrimmed(cs.top_line_rtrimmed),
682 top_line_ltrimmed(cs.top_line_ltrimmed),
683 bottom_line_rtrimmed(cs.bottom_line_rtrimmed),
684 bottom_line_ltrimmed(cs.bottom_line_ltrimmed),
687 align_special(cs.align_special),
689 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
693 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
699 multicolumn = cs.multicolumn;
700 multirow = cs.multirow;
701 mroffset = cs.mroffset;
702 alignment = cs.alignment;
703 valignment = cs.valignment;
704 decimal_hoffset = cs.decimal_hoffset;
705 decimal_width = cs.decimal_width;
706 voffset = cs.voffset;
707 top_line = cs.top_line;
708 bottom_line = cs.bottom_line;
709 left_line = cs.left_line;
710 right_line = cs.right_line;
711 top_line_rtrimmed = cs.top_line_rtrimmed;
712 top_line_ltrimmed = cs.top_line_ltrimmed;
713 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
714 bottom_line_ltrimmed = cs.bottom_line_ltrimmed;
717 align_special = cs.align_special;
718 p_width = cs.p_width;
719 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
723 Tabular::RowData::RowData()
726 top_space_default(false),
727 bottom_space_default(false),
728 interline_space_default(false),
739 Tabular::ColumnData::ColumnData()
740 : alignment(LYX_ALIGN_CENTER),
741 valignment(LYX_VALIGN_TOP),
749 Tabular::ltType::ltType()
750 : set(false), topDL(false),
756 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
758 init(buffer, rows_arg, columns_arg);
762 void Tabular::setBuffer(Buffer & buffer)
765 for (row_type i = 0; i < nrows(); ++i)
766 for (col_type j = 0; j < ncols(); ++j)
767 cell_info[i][j].inset->setBuffer(*buffer_);
771 // activates all lines and sets all widths to 0
772 void Tabular::init(Buffer * buf, row_type rows_arg,
773 col_type columns_arg)
776 row_info = row_vector(rows_arg);
777 column_info = column_vector(columns_arg);
778 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
779 row_info.reserve(10);
780 column_info.reserve(10);
781 cell_info.reserve(100);
783 is_long_tabular = false;
784 tabular_valignment = LYX_VALIGN_MIDDLE;
785 tabular_width = Length();
786 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
788 use_booktabs = false;
789 // set silly default lines
790 for (row_type r = 0; r < nrows(); ++r)
791 for (col_type c = 0; c < ncols(); ++c) {
792 cell_info[r][c].inset->setBuffer(*buffer_);
793 cell_info[r][c].top_line = true;
794 cell_info[r][c].left_line = true;
795 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
796 cell_info[r][c].right_line = c == ncols() - 1;
801 void Tabular::deleteRow(row_type const row, bool const force)
803 // Not allowed to delete last row
807 bool const ct = force ? false : buffer().params().track_changes;
809 for (col_type c = 0; c < ncols(); ++c) {
810 // Care about multirow cells
811 if (row + 1 < nrows() &&
812 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
813 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
814 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
818 row_info[row].change.setDeleted();
820 row_info.erase(row_info.begin() + row);
821 cell_info.erase(cell_info.begin() + row);
827 void Tabular::copyRow(row_type const row)
829 insertRow(row, true);
833 void Tabular::appendRow(row_type row)
835 insertRow(row, false);
839 void Tabular::insertRow(row_type const row, bool copy)
841 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
842 cell_info.insert(cell_info.begin() + row + 1,
843 cell_vector(0, CellData(buffer_)));
845 for (col_type c = 0; c < ncols(); ++c) {
846 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
847 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
848 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
849 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
853 for (col_type c = 0; c < ncols(); ++c) {
854 if (isPartOfMultiRow(row, c))
856 // inherit line settings
857 idx_type const i = cellIndex(row + 1, c);
858 idx_type const j = cellIndex(row, c);
859 setLeftLine(i, leftLine(j));
860 setRightLine(i, rightLine(j));
861 setTopLine(i, topLine(j));
862 if (topLine(j) && bottomLine(j)) {
863 setBottomLine(i, true);
864 setBottomLine(j, false);
867 if (buffer().params().track_changes) {
868 row_info[row + 1].change.setInserted();
874 void Tabular::moveColumn(col_type col, ColDirection direction)
876 if (direction == Tabular::LEFT)
879 std::swap(column_info[col], column_info[col + 1]);
881 for (row_type r = 0; r < nrows(); ++r) {
882 std::swap(cell_info[r][col], cell_info[r][col + 1]);
883 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
884 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
886 idx_type const i = cellIndex(r, col);
887 idx_type const j = cellIndex(r, col + 1);
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::moveRow(row_type row, RowDirection direction)
899 if (direction == Tabular::UP)
902 std::swap(row_info[row], row_info[row + 1]);
904 for (col_type c = 0; c < ncols(); ++c) {
905 std::swap(cell_info[row][c], cell_info[row + 1][c]);
906 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
907 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
909 idx_type const i = cellIndex(row, c);
910 idx_type const j = cellIndex(row + 1, c);
911 if (buffer().params().track_changes) {
912 cellInfo(i).inset->setChange(Change(Change::INSERTED));
913 cellInfo(j).inset->setChange(Change(Change::INSERTED));
920 void Tabular::deleteColumn(col_type const col, bool const force)
922 // Not allowed to delete last column
926 bool const ct = force ? false : buffer().params().track_changes;
928 for (row_type r = 0; r < nrows(); ++r) {
929 // Care about multicolumn cells
930 if (col + 1 < ncols() &&
931 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
932 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
933 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
936 cell_info[r].erase(cell_info[r].begin() + col);
939 column_info[col].change.setDeleted();
941 column_info.erase(column_info.begin() + col);
946 void Tabular::copyColumn(col_type const col)
948 insertColumn(col, true);
952 void Tabular::appendColumn(col_type col)
954 insertColumn(col, false);
958 void Tabular::insertColumn(col_type const col, bool copy)
960 bool const ct = buffer().params().track_changes;
961 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
963 for (row_type r = 0; r < nrows(); ++r) {
964 cell_info[r].insert(cell_info[r].begin() + col + 1,
965 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
966 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
967 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
970 for (row_type r = 0; r < nrows(); ++r) {
971 // inherit line settings
972 idx_type const i = cellIndex(r, col + 1);
973 idx_type const j = cellIndex(r, col);
974 setBottomLine(i, bottomLine(j));
975 setTopLine(i, topLine(j));
976 setLeftLine(i, leftLine(j));
977 setRightLine(i, rightLine(j));
978 if (rightLine(i) && rightLine(j)) {
979 setRightLine(j, false);
983 column_info[col + 1].change.setInserted();
989 void Tabular::updateIndexes()
994 for (row_type row = 0; row < nrows(); ++row)
995 for (col_type column = 0; column < ncols(); ++column) {
996 if (!isPartOfMultiColumn(row, column)
997 && !isPartOfMultiRow(row, column))
999 if (isPartOfMultiRow(row, column))
1000 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1002 cell_info[row][column].cellno = numberofcells - 1;
1005 rowofcell.resize(numberofcells);
1006 columnofcell.resize(numberofcells);
1008 // reset column and row of cells and update their width, alignment and ct status
1009 for (row_type row = 0; row < nrows(); ++row) {
1010 for (col_type column = 0; column < ncols(); ++column) {
1011 if (isPartOfMultiColumn(row, column)) {
1012 cell_info[row][column].inset->toggleMultiCol(true);
1015 cell_info[row][column].inset->toggleMultiCol(false);
1016 // columnofcell needs to be called before setting width and alignment
1017 // multirow cells inherit the width from the column width
1018 if (!isPartOfMultiRow(row, column)) {
1019 columnofcell[i] = column;
1022 setFixedWidth(row, column);
1023 if (isPartOfMultiRow(row, column)) {
1024 cell_info[row][column].inset->toggleMultiRow(true);
1027 cell_info[row][column].inset->toggleMultiRow(false);
1028 cell_info[row][column].inset->setContentAlignment(
1029 getAlignment(cellIndex(row, column)));
1030 if (buffer().params().track_changes) {
1031 if (row_info[row].change.changed())
1032 cell_info[row][column].inset->setChange(row_info[row].change);
1033 if (column_info[column].change.changed())
1034 cell_info[row][column].inset->setChange(column_info[column].change);
1042 idx_type Tabular::numberOfCellsInRow(row_type const row) const
1044 idx_type result = 0;
1045 for (col_type c = 0; c < ncols(); ++c)
1046 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1052 bool Tabular::topLine(idx_type const cell) const
1054 return cellInfo(cell).top_line;
1058 bool Tabular::bottomLine(idx_type const cell) const
1060 return cellInfo(cell).bottom_line;
1064 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1066 if (use_booktabs && !ignore_bt)
1068 return cellInfo(cell).left_line;
1072 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1074 if (use_booktabs && !ignore_bt)
1076 return cellInfo(cell).right_line;
1080 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1083 return make_pair(false, false);
1084 return make_pair(cellInfo(cell).top_line_ltrimmed,
1085 cellInfo(cell).top_line_rtrimmed);
1089 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1092 return make_pair(false, false);
1093 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1094 cellInfo(cell).bottom_line_rtrimmed);
1098 int Tabular::interRowSpace(row_type row) const
1100 if (!row || row >= nrows())
1103 int const interline_space = row_info[row - 1].interline_space_default ?
1104 default_line_space :
1105 row_info[row - 1].interline_space.inPixels(width());
1106 if (rowTopLine(row) && rowBottomLine(row - 1))
1107 return interline_space + WIDTH_OF_LINE;
1108 return interline_space;
1112 int Tabular::interColumnSpace(idx_type cell) const
1114 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1115 if (rightLine(cell) && nextcol < ncols()
1116 && leftLine(cellIndex(cellRow(cell), nextcol)))
1117 return WIDTH_OF_LINE;
1122 int Tabular::cellWidth(idx_type cell) const
1125 col_type const span = columnSpan(cell);
1126 col_type const col = cellColumn(cell);
1127 for(col_type c = col; c < col + span ; ++c)
1128 w += column_info[c].width;
1133 int Tabular::cellHeight(idx_type cell) const
1135 row_type const span = rowSpan(cell);
1136 row_type const row = cellRow(cell);
1138 for(row_type r = row; r < row + span ; ++r) {
1139 h += rowAscent(r) + rowDescent(r);
1140 if (r != row + span - 1)
1141 h += interRowSpace(r + 1);
1148 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1150 vector<int> max_dwidth(ncols(), 0);
1151 // collect max. fixed width of column
1152 map<col_type, int> max_pwidth;
1153 // collect max. variable width of column
1154 map<col_type, int> max_width;
1156 for(col_type c = 0; c < ncols(); ++c)
1157 for(row_type r = 0; r < nrows(); ++r) {
1158 idx_type const i = cellIndex(r, c);
1159 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1160 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1161 if (!getPWidth(i).zero())
1162 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1163 else if (!column_info[c].varwidth)
1164 max_width[c] = max(max_width[c], cell_info[r][c].width);
1167 // If we have a fixed tabular width, we take this into account
1168 Length tab_width = tabular_width;
1169 bool const tabularx = hasVarwidthColumn();
1170 if (tabularx && tab_width.zero())
1171 // If no tabular width is specified with X columns,
1172 // we use 100% colwidth
1173 tab_width = Length(100, Length::PCW);
1175 if (!tab_width.zero()) {
1176 restwidth = mi.base.inPixels(tab_width);
1177 // Subtract the fixed widths from the table width
1178 for (auto const & w : max_pwidth)
1179 restwidth -= w.second;
1182 // If we have a fixed width, distribute the available table width
1183 // (minus the fixed widths) to the variable-width columns
1185 int restcols = ncols() - max_pwidth.size();
1186 if ((restwidth > 0) && (restcols != 0))
1187 vcolwidth = restwidth / restcols;
1189 // Now consider that some variable width columns exceed the vcolwidth
1190 if (vcolwidth > 0) {
1191 bool changed = false;
1192 for (auto const & w : max_width) {
1193 if (tabularx || w.second > vcolwidth) {
1195 restwidth -= w.second;
1199 if (changed && restwidth > 0)
1200 vcolwidth = restwidth / restcols;
1203 bool update = false;
1204 // for each col get max of single col cells
1205 for(col_type c = 0; c < ncols(); ++c) {
1207 for(row_type r = 0; r < nrows(); ++r) {
1208 idx_type const i = cellIndex(r, c);
1209 if (columnSpan(i) == 1) {
1210 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1211 && cell_info[r][c].decimal_width != 0)
1212 new_width = max(new_width, cellInfo(i).width
1213 + max_dwidth[c] - cellInfo(i).decimal_width);
1214 else if (getPWidth(i).zero() && vcolwidth > 0) {
1215 if (tabularx && !column_info[c].varwidth)
1216 new_width = max(new_width, cellInfo(i).width);
1218 new_width = vcolwidth;
1220 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1222 new_width = max(new_width, cellInfo(i).width);
1226 if (column_info[c].width != new_width) {
1227 column_info[c].width = new_width;
1228 // Do not trigger update when no space is left for variable
1229 // columns, as this will loop
1230 update = tab_width.zero() || restwidth > 0;
1233 // update col widths to fit merged cells
1234 for(col_type c = 0; c < ncols(); ++c)
1235 for(row_type r = 0; r < nrows(); ++r) {
1236 idx_type const i = cellIndex(r, c);
1237 int const span = columnSpan(i);
1238 if (span == 1 || c > cellColumn(i))
1242 for(col_type j = c; j < c + span ; ++j)
1243 old_width += column_info[j].width;
1245 if (cellInfo(i).width > old_width) {
1246 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1247 // Do not trigger update when no space is left for variable
1248 // columns, as this will loop
1249 update = tab_width.zero() || restwidth > 0;
1257 int Tabular::width() const
1260 for (col_type c = 0; c < ncols(); ++c)
1261 width += column_info[c].width;
1266 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1269 col_type const col = cellColumn(cell);
1270 // set alignment for the whole row if we are not in a multicolumn cell,
1271 // exclude possible multicolumn cells in the row
1272 if (!isMultiColumn(cell)) {
1273 for (row_type r = 0; r < nrows(); ++r) {
1274 // only if the column has no width the multirow inherits the
1275 // alignment of the column, otherwise it is left aligned
1276 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1277 && !isMultiColumn(cellIndex(r, col))) {
1278 cell_info[r][col].alignment = align;
1279 cell_info[r][col].inset->setContentAlignment(align);
1281 if ((isMultiRow(cellIndex(r, col)) && has_width)
1282 && !isMultiColumn(cellIndex(r, col))) {
1283 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1284 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1287 column_info[col].alignment = align;
1288 docstring & dpoint = column_info[col].decimal_point;
1289 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1290 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1291 dpoint = tlang->decimalSeparator();
1294 cellInfo(cell).alignment = align;
1295 cellInset(cell)->setContentAlignment(align);
1300 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1303 if (!isMultiColumn(cell) || onlycolumn)
1304 column_info[cellColumn(cell)].valignment = align;
1306 cellInfo(cell).valignment = align;
1313 * Allow line and paragraph breaks for fixed width multirow cells
1314 * or disallow them, merge cell paragraphs and reset layout to standard
1315 * for variable width multicol cells.
1317 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1318 bool const fixedWidth, bool const multirow)
1320 inset->toggleFixedWidth(fixedWidth);
1324 // merge all paragraphs to one
1325 BufferParams const & bp = cur.bv().buffer().params();
1326 while (inset->paragraphs().size() > 1)
1327 mergeParagraph(bp, inset->paragraphs(), 0);
1329 // This is relevant for multirows
1334 ParagraphList::iterator pit = inset->paragraphs().begin();
1335 for (; pit != inset->paragraphs().end(); ++pit) {
1336 for (pos_type j = 0; j != pit->size(); ++j) {
1337 if (pit->isNewline(j))
1338 pit->eraseChar(j, bp.track_changes);
1344 // undo information has already been recorded
1345 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1346 bp.documentClass().plainLayoutName());
1353 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1354 Length const & width)
1356 col_type const c = cellColumn(cell);
1358 column_info[c].p_width = width;
1359 // reset the vertical alignment to top if the fixed width
1360 // is removed or zero because only fixed width columns can
1361 // have a vertical alignment
1362 if (column_info[c].p_width.zero())
1363 column_info[c].valignment = LYX_VALIGN_TOP;
1364 for (row_type r = 0; r < nrows(); ++r) {
1365 idx_type const cidx = cellIndex(r, c);
1366 // because of multicolumns
1367 toggleFixedWidth(cur, cellInset(cidx).get(),
1368 !getPWidth(cidx).zero(), isMultiRow(cidx));
1369 if (isMultiRow(cidx))
1370 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1372 // cur can become invalid after paragraphs were merged
1377 bool Tabular::setFixedWidth(row_type r, col_type c)
1379 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1380 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1381 || (multicol && !cell_info[r][c].p_width.zero());
1382 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1387 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1388 Length const & width)
1390 if (!isMultiColumn(cell))
1393 cellInfo(cell).p_width = width;
1394 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1396 // cur can become invalid after paragraphs were merged
1402 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1404 column_info[cellColumn(cell)].varwidth = varwidth;
1409 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1411 cellInfo(cell).mroffset = mroffset;
1416 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1417 Tabular::Feature what)
1419 if (what == SET_SPECIAL_MULTICOLUMN)
1420 cellInfo(cell).align_special = special;
1422 column_info[cellColumn(cell)].align_special = special;
1426 void Tabular::setTopLine(idx_type i, bool line)
1428 cellInfo(i).top_line = line;
1432 void Tabular::setBottomLine(idx_type i, bool line)
1434 cellInfo(i).bottom_line = line;
1438 void Tabular::setTopLineLTrim(idx_type i, bool val)
1440 cellInfo(i).top_line_ltrimmed = val;
1444 void Tabular::setTopLineRTrim(idx_type i, bool val)
1446 cellInfo(i).top_line_rtrimmed = val;
1450 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1452 cellInfo(i).bottom_line_ltrimmed = val;
1456 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1458 cellInfo(i).bottom_line_rtrimmed = val;
1462 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1464 setTopLineLTrim(i, trim.first);
1465 setTopLineRTrim(i, trim.second);
1468 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1470 setBottomLineLTrim(i, trim.first);
1471 setBottomLineRTrim(i, trim.second);
1474 void Tabular::setLeftLine(idx_type cell, bool line)
1476 cellInfo(cell).left_line = line;
1480 void Tabular::setRightLine(idx_type cell, bool line)
1482 cellInfo(cell).right_line = line;
1485 bool Tabular::rowTopLine(row_type r) const
1487 bool all_rows_set = true;
1488 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1489 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1490 return all_rows_set;
1494 bool Tabular::rowBottomLine(row_type r) const
1496 bool all_rows_set = true;
1497 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1498 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1499 return all_rows_set;
1503 bool Tabular::columnLeftLine(col_type c) const
1510 for (row_type r = 0; r < nrows(); ++r) {
1511 idx_type const i = cellIndex(r, c);
1512 if (c == cellColumn(i)) {
1514 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1515 if (cellInfo(i).left_line || right)
1519 return 2 * nrows_left >= total;
1523 bool Tabular::columnRightLine(col_type c) const
1528 int nrows_right = 0;
1530 for (row_type r = 0; r < nrows(); ++r) {
1531 idx_type i = cellIndex(r, c);
1532 if (c == cellColumn(i) + columnSpan(i) - 1) {
1534 bool left = (c + 1 < ncols()
1535 && cellInfo(cellIndex(r, c + 1)).left_line)
1536 || c + 1 == ncols();
1537 if (cellInfo(i).right_line && left)
1541 return 2 * nrows_right >= total;
1545 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1547 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1548 return cellInfo(cell).alignment;
1550 return column_info[cellColumn(cell)].alignment;
1555 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1557 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1558 return cellInfo(cell).valignment;
1559 return column_info[cellColumn(cell)].valignment;
1563 int Tabular::offsetVAlignment() const
1565 // for top-alignment the first horizontal table line must be exactly at
1566 // the position of the base line of the surrounding text line
1567 // for bottom alignment, the same is for the last table line
1568 int offset_valign = 0;
1569 switch (tabular_valignment) {
1570 case Tabular::LYX_VALIGN_BOTTOM:
1571 offset_valign = rowAscent(0) - height();
1573 case Tabular::LYX_VALIGN_MIDDLE:
1574 offset_valign = (- height()) / 2 + rowAscent(0);
1576 case Tabular::LYX_VALIGN_TOP:
1577 offset_valign = rowAscent(0);
1580 return offset_valign;
1584 Length const Tabular::getPWidth(idx_type cell) const
1586 if (isMultiColumn(cell))
1587 return cellInfo(cell).p_width;
1588 return column_info[cellColumn(cell)].p_width;
1592 Length const Tabular::getMROffset(idx_type cell) const
1594 return cellInfo(cell).mroffset;
1598 int Tabular::textHOffset(idx_type cell) const
1600 // the LaTeX Way :-(
1601 int x = WIDTH_OF_LINE;
1603 int const w = cellWidth(cell) - cellInfo(cell).width;
1605 switch (getAlignment(cell)) {
1606 case LYX_ALIGN_CENTER:
1609 case LYX_ALIGN_RIGHT:
1612 case LYX_ALIGN_DECIMAL: {
1613 // we center when no decimal point
1614 if (cellInfo(cell).decimal_width == 0) {
1618 col_type const c = cellColumn(cell);
1619 int max_dhoffset = 0;
1620 for(row_type r = 0; r < row_info.size() ; ++r) {
1621 idx_type const i = cellIndex(r, c);
1622 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1623 && cellInfo(i).decimal_width != 0)
1624 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1626 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1629 // LYX_ALIGN_LEFT: nothing :-)
1637 int Tabular::textVOffset(idx_type cell) const
1639 int voffset = cellInfo(cell).voffset;
1640 if (isMultiRow(cell)) {
1641 row_type const row = cellRow(cell);
1642 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1648 idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1651 idx_type const numcells = numberOfCellsInRow(row);
1652 // we check against numcells to make sure we do not crash if all the
1653 // cells are multirow (bug #7535), but in that case our return value
1654 // is really invalid, i.e., it is NOT the first cell in the row. but
1655 // i do not know what to do here. (rgh)
1656 while (c < numcells - 1
1657 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1658 || (ct && column_info[c].change.deleted())))
1660 return cell_info[row][c].cellno;
1664 idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1666 col_type c = ncols() - 1;
1667 // of course we check against 0 so we don't crash. but we have the same
1668 // problem as in the previous routine: if all the cells are part of a
1669 // multirow or part of a multi column, then our return value is invalid.
1671 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1672 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1673 || (ct && column_info[c].change.deleted())))
1675 return cell_info[row][c].cellno;
1679 row_type Tabular::getFirstRow(bool const ct) const
1684 // exclude deleted rows if ct == true
1685 while (r < nrows() && row_info[r].change.deleted())
1691 row_type Tabular::getLastRow(bool const ct) const
1693 row_type r = nrows() - 1;
1696 // exclude deleted rows if ct == true
1697 while (r > 0 && row_info[r].change.deleted())
1703 row_type Tabular::cellRow(idx_type cell) const
1705 if (cell >= numberofcells)
1709 return rowofcell[cell];
1713 col_type Tabular::cellColumn(idx_type cell) const
1715 if (cell >= numberofcells)
1719 return columnofcell[cell];
1723 void Tabular::write(ostream & os) const
1727 << write_attribute("version", 3)
1728 << write_attribute("rows", nrows())
1729 << write_attribute("columns", ncols())
1731 // global longtable options
1733 << write_attribute("rotate", rotate)
1734 << write_attribute("booktabs", use_booktabs)
1735 << write_attribute("islongtable", is_long_tabular)
1736 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1737 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1738 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1739 << write_attribute("headTopDL", endhead.topDL)
1740 << write_attribute("headBottomDL", endhead.bottomDL)
1741 << write_attribute("footTopDL", endfoot.topDL)
1742 << write_attribute("footBottomDL", endfoot.bottomDL)
1743 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1744 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1745 << write_attribute("lastFootEmpty", endlastfoot.empty);
1746 // longtables cannot be aligned vertically
1747 if (!is_long_tabular)
1748 os << write_attribute("tabularvalignment", tabular_valignment);
1749 os << write_attribute("tabularwidth", tabular_width);
1750 if (is_long_tabular)
1751 os << write_attribute("longtabularalignment", longtabular_alignment);
1753 for (col_type c = 0; c < ncols(); ++c) {
1755 << write_attribute("alignment", column_info[c].alignment);
1756 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1757 os << write_attribute("decimal_point", column_info[c].decimal_point);
1758 os << write_attribute("change", column_info[c].change, buffer().params())
1759 << write_attribute("valignment", column_info[c].valignment)
1760 << write_attribute("width", column_info[c].p_width.asString())
1761 << write_attribute("varwidth", column_info[c].varwidth)
1762 << write_attribute("special", column_info[c].align_special)
1765 for (row_type r = 0; r < nrows(); ++r) {
1766 static const string def("default");
1768 if (row_info[r].top_space_default)
1769 os << write_attribute("topspace", def);
1771 os << write_attribute("topspace", row_info[r].top_space);
1772 if (row_info[r].bottom_space_default)
1773 os << write_attribute("bottomspace", def);
1775 os << write_attribute("bottomspace", row_info[r].bottom_space);
1776 if (row_info[r].interline_space_default)
1777 os << write_attribute("interlinespace", def);
1779 os << write_attribute("interlinespace", row_info[r].interline_space);
1780 os << write_attribute("change", row_info[r].change, buffer().params())
1781 << write_attribute("endhead", row_info[r].endhead)
1782 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1783 << write_attribute("endfoot", row_info[r].endfoot)
1784 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1785 << write_attribute("newpage", row_info[r].newpage)
1786 << write_attribute("caption", row_info[r].caption)
1788 for (col_type c = 0; c < ncols(); ++c) {
1790 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1791 << write_attribute("multirow", cell_info[r][c].multirow)
1792 << write_attribute("mroffset", cell_info[r][c].mroffset)
1793 << write_attribute("alignment", cell_info[r][c].alignment)
1794 << write_attribute("valignment", cell_info[r][c].valignment)
1795 << write_attribute("topline", cell_info[r][c].top_line)
1796 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1797 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1798 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1799 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1800 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1801 << write_attribute("leftline", cell_info[r][c].left_line)
1802 << write_attribute("rightline", cell_info[r][c].right_line)
1803 << write_attribute("rotate", cell_info[r][c].rotate)
1804 << write_attribute("usebox", cell_info[r][c].usebox)
1805 << write_attribute("width", cell_info[r][c].p_width)
1806 << write_attribute("special", cell_info[r][c].align_special)
1808 os << "\\begin_inset ";
1809 cell_info[r][c].inset->write(os);
1810 os << "\n\\end_inset\n"
1812 // FIXME This can be removed again once the mystery
1813 // crash has been resolved.
1818 os << "</lyxtabular>\n";
1822 void Tabular::read(Lexer & lex)
1825 istream & is = lex.getStream();
1827 l_getline(is, line);
1828 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1829 LASSERT(false, return);
1833 if (!getTokenValue(line, "version", version))
1835 LATTEST(version >= 2);
1838 if (!getTokenValue(line, "rows", rows_arg))
1841 if (!getTokenValue(line, "columns", columns_arg))
1843 init(buffer_, rows_arg, columns_arg);
1844 l_getline(is, line);
1845 if (!prefixIs(line, "<features")) {
1846 lyxerr << "Wrong tabular format (expected <features ...> got"
1847 << line << ')' << endl;
1850 getTokenValue(line, "rotate", rotate);
1851 getTokenValue(line, "booktabs", use_booktabs);
1852 getTokenValue(line, "islongtable", is_long_tabular);
1853 getTokenValue(line, "tabularvalignment", tabular_valignment);
1854 getTokenValue(line, "tabularwidth", tabular_width);
1855 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1856 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1857 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1858 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1859 getTokenValue(line, "headTopDL", endhead.topDL);
1860 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1861 getTokenValue(line, "footTopDL", endfoot.topDL);
1862 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1863 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1864 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1865 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1867 for (col_type c = 0; c < ncols(); ++c) {
1869 if (!prefixIs(line,"<column")) {
1870 lyxerr << "Wrong tabular format (expected <column ...> got"
1871 << line << ')' << endl;
1874 getTokenValue(line, "alignment", column_info[c].alignment);
1875 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1876 getTokenValue(line, "valignment", column_info[c].valignment);
1877 getTokenValue(line, "width", column_info[c].p_width);
1878 getTokenValue(line, "special", column_info[c].align_special);
1879 getTokenValue(line, "varwidth", column_info[c].varwidth);
1880 getTokenValue(line, "change", column_info[c].change, buffer().params());
1883 for (row_type i = 0; i < nrows(); ++i) {
1884 l_getline(is, line);
1885 if (!prefixIs(line, "<row")) {
1886 lyxerr << "Wrong tabular format (expected <row ...> got"
1887 << line << ')' << endl;
1890 getTokenValue(line, "topspace", row_info[i].top_space,
1891 row_info[i].top_space_default);
1892 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1893 row_info[i].bottom_space_default);
1894 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1895 row_info[i].interline_space_default);
1896 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1897 getTokenValue(line, "endhead", row_info[i].endhead);
1898 getTokenValue(line, "endfoot", row_info[i].endfoot);
1899 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1900 getTokenValue(line, "newpage", row_info[i].newpage);
1901 getTokenValue(line, "caption", row_info[i].caption);
1902 getTokenValue(line, "change", row_info[i].change, buffer().params());
1903 for (col_type j = 0; j < ncols(); ++j) {
1904 l_getline(is, line);
1905 if (!prefixIs(line, "<cell")) {
1906 lyxerr << "Wrong tabular format (expected <cell ...> got"
1907 << line << ')' << endl;
1910 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1911 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1912 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1913 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1914 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1915 getTokenValue(line, "topline", cell_info[i][j].top_line);
1916 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1917 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1918 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1919 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1920 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1921 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1922 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1923 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1924 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1925 getTokenValue(line, "width", cell_info[i][j].p_width);
1927 getTokenValue(line, "special", cell_info[i][j].align_special);
1928 l_getline(is, line);
1929 if (prefixIs(line, "\\begin_inset")) {
1930 cell_info[i][j].inset->setBuffer(*buffer_);
1931 cell_info[i][j].inset->read(lex);
1932 l_getline(is, line);
1934 if (!prefixIs(line, "</cell>")) {
1935 lyxerr << "Wrong tabular format (expected </cell> got"
1936 << line << ')' << endl;
1940 l_getline(is, line);
1941 if (!prefixIs(line, "</row>")) {
1942 lyxerr << "Wrong tabular format (expected </row> got"
1943 << line << ')' << endl;
1947 while (!prefixIs(line, "</lyxtabular>")) {
1948 l_getline(is, line);
1954 bool Tabular::isMultiColumn(idx_type cell) const
1956 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1957 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1961 bool Tabular::hasMultiColumn(col_type c) const
1963 for (row_type r = 0; r < nrows(); ++r) {
1964 if (isMultiColumn(cellIndex(r, c)))
1971 bool Tabular::hasVarwidthColumn() const
1973 for (col_type c = 0; c < ncols(); ++c) {
1974 if (column_info[c].varwidth)
1981 bool Tabular::isVTypeColumn(col_type c) const
1983 for (row_type r = 0; r < nrows(); ++r) {
1984 idx_type idx = cellIndex(r, c);
1985 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH
1986 && getAlignment(idx) == LYX_ALIGN_LEFT
1987 && getVAlignment(idx) == LYX_VALIGN_TOP)
1994 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1996 return cell_info[cellRow(cell)][cellColumn(cell)];
2000 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2002 return cell_info[cellRow(cell)][cellColumn(cell)];
2006 idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2007 bool const right_border)
2009 idx_type const col = cellColumn(cell);
2010 idx_type const row = cellRow(cell);
2011 for (idx_type i = 0; i < number; ++i)
2012 unsetMultiRow(cellIndex(row, col + i));
2014 // unsetting of multirow may have invalidated cell index
2015 cell = cellIndex(row, col);
2016 CellData & cs = cellInfo(cell);
2017 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2018 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2019 cs.alignment = column_info[col].alignment;
2020 setRightLine(cell, right_border);
2021 // non-fixed width multicolumns cannot have multiple paragraphs
2022 if (getPWidth(cell).zero()) {
2023 toggleFixedWidth(cur, cellInset(cell).get(),
2024 !getPWidth(cell).zero(), isMultiRow(cell));
2025 // cur can become invalid after paragraphs were merged
2029 idx_type lastcell = cellIndex(row, col + number - 1);
2030 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2031 CellData & cs1 = cellInfo(cell + i);
2032 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2033 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2041 bool Tabular::isMultiRow(idx_type cell) const
2043 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2044 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2047 bool Tabular::hasMultiRow(row_type r) const
2049 for (col_type c = 0; c < ncols(); ++c) {
2050 if (isMultiRow(cellIndex(r, c)))
2056 idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2057 bool const bottom_border,
2058 LyXAlignment const halign)
2060 idx_type const col = cellColumn(cell);
2061 idx_type const row = cellRow(cell);
2062 for (idx_type i = 0; i < number; ++i)
2063 unsetMultiColumn(cellIndex(row + i, col));
2065 // unsetting of multirow may have invalidated cell index
2066 cell = cellIndex(row, col);
2067 CellData & cs = cellInfo(cell);
2068 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2069 cs.valignment = LYX_VALIGN_MIDDLE;
2070 // the horizontal alignment of multirow cells can only
2071 // be changed for the whole table row,
2072 // support changing this only for the multirow cell can be done via
2074 if (getPWidth(cell).zero())
2075 cs.alignment = halign;
2077 cs.alignment = LYX_ALIGN_LEFT;
2079 // Multirows cannot have multiple paragraphs
2080 if (getPWidth(cell).zero()) {
2081 toggleFixedWidth(cur, cellInset(cell).get(),
2082 !getPWidth(cell).zero(),
2084 // cur can become invalid after paragraphs were merged
2088 // set the bottom line of the last selected cell
2089 setBottomLine(cell, bottom_border);
2091 for (idx_type i = 1; i < number; ++i) {
2092 CellData & cs1 = cell_info[row + i][col];
2093 cs1.multirow = CELL_PART_OF_MULTIROW;
2094 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2102 idx_type Tabular::columnSpan(idx_type cell) const
2104 row_type const row = cellRow(cell);
2105 col_type const col = cellColumn(cell);
2107 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2114 idx_type Tabular::rowSpan(idx_type cell) const
2116 col_type const column = cellColumn(cell);
2117 col_type row = cellRow(cell) + 1;
2118 while (row < nrows() && isPartOfMultiRow(row, column))
2121 return row - cellRow(cell);
2125 void Tabular::unsetMultiColumn(idx_type cell)
2127 if (!isMultiColumn(cell))
2130 row_type const row = cellRow(cell);
2131 col_type const col = cellColumn(cell);
2132 row_type const span = columnSpan(cell);
2133 for (col_type c = 0; c < span; ++c) {
2134 // in the table dialog the lines are set in every case
2135 // when unsetting a multicolumn this leads to an additional right
2136 // line for every cell that was part of the former multicolumn cell,
2137 // except if the cell is in the last column
2138 // therefore remove this line
2139 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2140 && (col + c) < (col + span - 1))
2141 cell_info[row][col + c].right_line = false;
2142 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2148 void Tabular::unsetMultiRow(idx_type cell)
2150 if (!isMultiRow(cell))
2153 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2154 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2155 row_type const row = cellRow(cell);
2156 col_type const col = cellColumn(cell);
2157 row_type const span = rowSpan(cell);
2158 for (row_type r = 0; r < span; ++r)
2159 cell_info[row + r][col].multirow = CELL_NORMAL;
2164 void Tabular::setRotateCell(idx_type cell, int value)
2166 cellInfo(cell).rotate = value;
2170 int Tabular::getRotateCell(idx_type cell) const
2172 return cellInfo(cell).rotate;
2176 bool Tabular::needRotating() const
2178 if (rotate && !is_long_tabular)
2180 for (row_type r = 0; r < nrows(); ++r)
2181 for (col_type c = 0; c < ncols(); ++c)
2182 if (cell_info[r][c].rotate != 0)
2188 bool Tabular::isLastCell(idx_type cell) const
2190 return cell + 1 >= numberofcells;
2194 idx_type Tabular::cellAbove(idx_type cell) const
2196 if (cellRow(cell) == 0)
2199 col_type const col = cellColumn(cell);
2200 row_type r = cellRow(cell) - 1;
2201 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2204 return cell_info[r][col].cellno;
2208 idx_type Tabular::cellBelow(idx_type cell) const
2210 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2211 if (nextrow < nrows())
2212 return cell_info[nextrow][cellColumn(cell)].cellno;
2217 idx_type Tabular::cellIndex(row_type row, col_type column) const
2219 LASSERT(column != npos && column < ncols(), column = 0);
2220 LASSERT(row != npos && row < nrows(), row = 0);
2221 return cell_info[row][column].cellno;
2225 void Tabular::setUsebox(idx_type cell, BoxType type)
2227 cellInfo(cell).usebox = type;
2231 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2233 if (getRotateCell(cell) == 0
2234 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2235 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2237 if (cellInfo(cell).usebox > 1)
2238 return cellInfo(cell).usebox;
2239 return useBox(cell);
2244 // This are functions used for the longtable support
2246 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2252 row_info[row].endfirsthead = flag;
2256 row_info[row].endhead = flag;
2261 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2264 hd.set = haveLTHead();
2265 return row_info[row].endhead;
2269 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2272 hd.set = haveLTFirstHead();
2273 return row_info[row].endfirsthead;
2277 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2283 row_info[row].endlastfoot = flag;
2287 row_info[row].endfoot = flag;
2292 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2295 fd.set = haveLTFoot();
2296 return row_info[row].endfoot;
2300 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2303 fd.set = haveLTLastFoot();
2304 return row_info[row].endlastfoot;
2308 void Tabular::setLTNewPage(row_type row, bool what)
2310 row_info[row].newpage = what;
2314 bool Tabular::getLTNewPage(row_type row) const
2316 return row_info[row].newpage;
2320 bool Tabular::haveLTHead(bool withcaptions) const
2322 if (!is_long_tabular)
2324 for (row_type i = 0; i < nrows(); ++i)
2325 if (row_info[i].endhead &&
2326 (withcaptions || !row_info[i].caption))
2332 bool Tabular::haveLTFirstHead(bool withcaptions) const
2334 if (!is_long_tabular || endfirsthead.empty)
2336 for (row_type r = 0; r < nrows(); ++r)
2337 if (row_info[r].endfirsthead &&
2338 (withcaptions || !row_info[r].caption))
2344 bool Tabular::haveLTFoot(bool withcaptions) const
2346 if (!is_long_tabular)
2348 for (row_type r = 0; r < nrows(); ++r)
2349 if (row_info[r].endfoot &&
2350 (withcaptions || !row_info[r].caption))
2356 bool Tabular::haveLTLastFoot(bool withcaptions) const
2358 if (!is_long_tabular || endlastfoot.empty)
2360 for (row_type r = 0; r < nrows(); ++r)
2361 if (row_info[r].endlastfoot &&
2362 (withcaptions || !row_info[r].caption))
2368 idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2370 idx_type i = getFirstCellInRow(row);
2372 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2373 setTopLine(i, false);
2374 setBottomLine(i, false);
2375 setLeftLine(i, false);
2376 setRightLine(i, false);
2377 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2378 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2379 setLTHead(row, true, endfirsthead, true);
2380 row_info[row].endfirsthead = true;
2383 unsetMultiColumn(i);
2384 // When unsetting a caption row, also all existing
2385 // captions in this row must be dissolved.
2387 row_info[row].caption = what;
2392 bool Tabular::ltCaption(row_type row) const
2394 return row_info[row].caption;
2398 bool Tabular::haveLTCaption(CaptionType captiontype) const
2400 if (!is_long_tabular)
2402 for (row_type r = 0; r < nrows(); ++r) {
2403 if (row_info[r].caption) {
2404 switch (captiontype) {
2405 case CAPTION_FIRSTHEAD:
2406 if (row_info[r].endfirsthead)
2410 if (row_info[r].endhead)
2414 if (row_info[r].endfoot)
2417 case CAPTION_LASTFOOT:
2418 if (row_info[r].endlastfoot)
2430 // end longtable support functions
2432 void Tabular::setRowAscent(row_type row, int height)
2434 if (row >= nrows() || row_info[row].ascent == height)
2436 row_info[row].ascent = height;
2440 void Tabular::setRowDescent(row_type row, int height)
2442 if (row >= nrows() || row_info[row].descent == height)
2444 row_info[row].descent = height;
2448 int Tabular::rowAscent(row_type row) const
2450 LASSERT(row < nrows(), row = 0);
2451 return row_info[row].ascent;
2455 int Tabular::rowDescent(row_type row) const
2457 LASSERT(row < nrows(), row = 0);
2458 return row_info[row].descent;
2462 int Tabular::height() const
2465 for (row_type row = 0; row < nrows(); ++row)
2466 height += rowAscent(row) + rowDescent(row) +
2472 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2474 LASSERT(row < nrows(), return false);
2475 LASSERT(column < ncols(), return false);
2476 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2480 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2482 LASSERT(row < nrows(), return false);
2483 LASSERT(column < ncols(), return false);
2484 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2488 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns,
2489 list<col_type> logical_columns) const
2491 // we only output complete row lines and the 1st row here, the rest
2492 // is done in Tabular::TeXBottomHLine(...)
2494 // get for each column the topline (if any)
2495 map<col_type, bool> topline, topltrims, toprtrims;
2497 bool have_trims = false;
2498 for (auto const & c : columns) {
2499 topline[c] = topLine(cellIndex(row, c));
2500 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2501 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2502 // If cell is part of a multirow and not the first cell of the
2503 // multirow, no line must be drawn.
2505 if (isMultiRow(cellIndex(row, c))
2506 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2508 topltrims[c] = false;
2509 toprtrims[c] = false;
2511 // copy trimming to multicolumn parts
2512 if (isPartOfMultiColumn(row, c)) {
2513 topltrims[c] = topltrims[c-1];
2514 toprtrims[c] = toprtrims[c-1];
2516 if (topline.find(c) != topline.end() && topline.find(c)->second)
2518 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2519 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2523 // do nothing if empty first row, or incomplete row line after
2524 row_type first = getFirstRow(!buffer().params().output_changes);
2525 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2528 // Is this the actual first row (excluding longtable caption row)?
2529 bool const realfirstrow = (row == first
2530 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2532 // only output complete row lines and the 1st row's clines
2533 if (nset == columns.size() && !have_trims) {
2535 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2539 } else if (realfirstrow || have_trims) {
2540 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2542 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2543 std::list<col_type>::const_iterator it2 = columns.begin();
2544 // We need to iterate over the logical columns here, but take care for
2546 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2551 if (topline.find(c)->second) {
2552 col_type offset = 0;
2553 for (col_type j = 0 ; j < c; ++j)
2554 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2556 // If the two iterators differ, we are in bidi with swapped columns
2557 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2558 while (isPartOfMultiColumn(row, c))
2561 if (topltrims.find(c) != topltrims.end()
2562 && topltrims.find(c)->second)
2564 col_type cstart = c;
2565 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2566 if (isMultiColumn(cellIndex(row, c))
2567 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2569 if (c > cstart && topltrims.find(c) != topltrims.end()
2570 && topltrims.find(c)->second) {
2571 if (!isPartOfMultiColumn(row, c))
2574 } else if (toprtrims.find(c) != toprtrims.end()
2575 && toprtrims.find(c)->second)
2579 for (col_type j = cstart ; j < c ; ++j)
2580 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2582 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2583 if (toprtrims.find(c) != toprtrims.end()
2584 && toprtrims.find(c)->second)
2589 os << "(" << trim << ")";
2590 if (firstcol > lastcol)
2591 // This can happen with bidi (swapped columns)
2592 os << "{" << lastcol << '-' << firstcol << "}";
2594 os << "{" << firstcol << '-' << lastcol << "}";
2595 if (c == columns.size() - 1)
2605 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns,
2606 list<col_type> logical_columns) const
2608 // we output bottomlines of row r and the toplines of row r+1
2609 // if the latter do not span the whole tabular
2611 // get the bottomlines of row r, and toplines in next row
2612 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2613 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2614 bool nextrowset = true;
2615 for (auto const & c : columns) {
2616 idx_type const idx = cellIndex(row, c);
2617 bottomline[c] = bottomLine(cellIndex(row, c));
2618 bottomltrims[c] = bottomLineTrim(idx).first;
2619 bottomrtrims[c] = bottomLineTrim(idx).second;
2620 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2621 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2622 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2623 // If cell is part of a multirow and not the last cell of the
2624 // multirow, no line must be drawn.
2626 if (isMultiRow(cellIndex(row, c))
2627 && isMultiRow(cellIndex(row + 1, c))
2628 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2629 bottomline[c] = false;
2631 bottomltrims[c] = false;
2632 bottomrtrims[c] = false;
2633 topltrims[c] = false;
2634 toprtrims[c] = false;
2636 // copy trimming in multicolumn parts
2637 if (isPartOfMultiColumn(row, c)) {
2638 topltrims[c] = topltrims[c-1];
2639 toprtrims[c] = toprtrims[c-1];
2640 bottomltrims[c] = bottomltrims[c-1];
2641 bottomrtrims[c] = bottomrtrims[c-1];
2644 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2647 // combine this row's bottom lines and next row's toplines if necessary
2649 bool have_trims = false;
2650 for (auto const & c : columns) {
2652 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2653 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2654 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2655 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2656 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2657 if (bottomline.find(c)->second)
2659 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2660 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2664 // do nothing if empty, OR incomplete row line with a topline in next row
2665 if (nset == 0 || (nextrowset && nset != columns.size()))
2668 if (nset == columns.size() && !have_trims) {
2670 os << (lastrow ? "\\bottomrule" : "\\midrule");
2674 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2676 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2677 std::list<col_type>::const_iterator it2 = columns.begin();
2678 // We need to iterate over the logical columns here, but take care for
2680 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2685 if (bottomline.find(c)->second) {
2686 col_type offset = 0;
2687 for (col_type j = 0 ; j < c; ++j)
2688 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2690 // If the two iterators differ, we are in bidi with swapped columns
2691 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2692 while (isPartOfMultiColumn(row, c))
2695 if (bottomltrims.find(c) != bottomltrims.end()
2696 && bottomltrims.find(c)->second)
2698 col_type cstart = c;
2699 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2700 if (isMultiColumn(cellIndex(row, c))
2702 && isPartOfMultiColumn(row, c + 1))
2705 && bottomltrims.find(c) != bottomltrims.end()
2706 && bottomltrims.find(c)->second) {
2707 if (!isPartOfMultiColumn(row, c))
2710 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2711 && bottomrtrims.find(c)->second)
2715 for (col_type j = cstart ; j < c ; ++j)
2716 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2718 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2719 if (bottomrtrims.find(c) != bottomrtrims.end()
2720 && bottomrtrims.find(c)->second)
2725 os << "(" << trim << ")";
2726 if (firstcol > lastcol)
2727 // This can happen with bidi (swapped columns)
2728 os << "{" << lastcol << '-' << firstcol << "}";
2730 os << "{" << firstcol << '-' << lastcol << "}";
2731 if (c == columns.size() - 1)
2741 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2742 bool & ismulticol, bool & ismultirow,
2743 bool const bidi) const
2745 row_type const r = cellRow(cell);
2746 if (is_long_tabular && row_info[r].caption)
2749 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2750 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2751 // figure out how to set the lines
2752 // we always set double lines to the right of the cell
2753 // or left in bidi RTL, respectively.
2754 col_type const c = cellColumn(cell);
2755 col_type const nextcol = c + columnSpan(cell);
2756 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2757 bool colright = columnRightLine(c);
2758 bool colleft = columnLeftLine(c);
2759 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2760 bool nextcellleft = nextcol < ncols()
2761 && leftLine(cellIndex(r, nextcol));
2762 bool coldouble = colright && nextcolleft;
2763 bool celldouble = rightLine(cell) && nextcellleft;
2765 ismulticol = (isMultiColumn(cell)
2766 || (c == 0 && colleft != leftLine(cell))
2767 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2768 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2769 || (coldouble != celldouble))
2772 // we center in multicol when no decimal point
2774 docstring const align_d = column_info[c].decimal_point;
2775 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2776 bool const nosep = !dit;
2777 ismulticol |= nosep;
2778 celldouble &= nosep;
2781 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2782 int latexcolspan = columnSpan(cell);
2783 for (col_type col = c; col < c + columnSpan(cell); ++col)
2784 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2788 os << "\\multicolumn{" << latexcolspan << "}{";
2789 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2790 || (!bidi && c == 0 && leftLine(cell))))
2792 if (bidi && celldouble)
2793 // add extra vertical line if we want a double one
2795 if (!cellInfo(cell).align_special.empty()) {
2796 os << cellInfo(cell).align_special;
2798 if (!getPWidth(cell).zero()) {
2800 case LYX_ALIGN_LEFT:
2801 os << ">{\\raggedright}";
2803 case LYX_ALIGN_RIGHT:
2804 os << ">{\\raggedleft}";
2806 case LYX_ALIGN_CENTER:
2807 os << ">{\\centering}";
2813 case LYX_VALIGN_TOP:
2816 case LYX_VALIGN_MIDDLE:
2819 case LYX_VALIGN_BOTTOM:
2824 << from_ascii(getPWidth(cell).asLatexString())
2827 if ((getRotateCell(cell) == 0 && useBox(cell) == BOX_VARWIDTH
2828 && align == LYX_ALIGN_LEFT))
2829 os << "V{\\linewidth}";
2832 case LYX_ALIGN_LEFT:
2835 case LYX_ALIGN_RIGHT:
2843 } // end if else !getPWidth
2844 } // end if else !cellinfo_of_cell
2845 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2847 if (!bidi && celldouble)
2848 // add extra vertical line if we want a double one
2851 } // end if ismulticol
2853 // we only need code for the first multirow cell
2854 ismultirow = isMultiRow(cell);
2856 os << "\\multirow{" << rowSpan(cell) << "}{";
2857 if (!getPWidth(cell).zero())
2858 os << from_ascii(getPWidth(cell).asLatexString());
2860 // we need to set a default value
2863 if (!getMROffset(cell).zero())
2864 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2866 } // end if ismultirow
2868 if (getRotateCell(cell) != 0)
2869 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2871 if (getUsebox(cell) == BOX_PARBOX) {
2874 case LYX_VALIGN_TOP:
2877 case LYX_VALIGN_MIDDLE:
2880 case LYX_VALIGN_BOTTOM:
2884 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2886 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2887 os << "\\begin{minipage}[";
2889 case LYX_VALIGN_TOP:
2892 case LYX_VALIGN_MIDDLE:
2895 case LYX_VALIGN_BOTTOM:
2899 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2901 } else if (getUsebox(cell) == BOX_VARWIDTH
2902 && (getRotateCell(cell) != 0 || align != LYX_ALIGN_LEFT
2903 || valign != LYX_VALIGN_TOP)) {
2904 os << "\\begin{cellvarwidth}[";
2906 case LYX_VALIGN_TOP:
2909 case LYX_VALIGN_MIDDLE:
2912 case LYX_VALIGN_BOTTOM:
2918 case LYX_ALIGN_RIGHT:
2919 os << "\\raggedleft\n";
2921 case LYX_ALIGN_CENTER:
2922 os << "\\centering\n";
2924 case LYX_ALIGN_LEFT:
2925 //os << "\\narrowragged\n";
2927 case LYX_ALIGN_BLOCK:
2928 case LYX_ALIGN_DECIMAL:
2929 case LYX_ALIGN_NONE:
2930 case LYX_ALIGN_LAYOUT:
2931 case LYX_ALIGN_SPECIAL:
2938 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2939 bool ismulticol, bool ismultirow) const
2941 row_type const r = cellRow(cell);
2942 if (is_long_tabular && row_info[r].caption)
2946 if (getUsebox(cell) == BOX_PARBOX)
2948 else if (getUsebox(cell) == BOX_MINIPAGE)
2949 os << breakln << "\\end{minipage}";
2950 else if (getUsebox(cell) == BOX_VARWIDTH
2951 && (getRotateCell(cell) != 0 || getAlignment(cell) != LYX_ALIGN_LEFT
2952 || getVAlignment(cell) != LYX_VALIGN_TOP))
2953 os << breakln << "\\end{cellvarwidth}";
2954 if (getRotateCell(cell) != 0)
2955 os << breakln << "\\end{turn}";
2963 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2964 OutputParams const & runparams,
2965 list<col_type> columns,
2966 list<col_type> logical_columns) const
2968 if (!is_long_tabular)
2972 // output caption which is in no header or footer
2973 if (haveLTCaption()) {
2974 for (row_type r = 0; r < nrows(); ++r) {
2975 if (row_info[r].caption &&
2976 !row_info[r].endfirsthead && !row_info[r].endhead &&
2977 !row_info[r].endfoot && !row_info[r].endlastfoot)
2978 TeXRow(os, r, runparams, columns, logical_columns);
2981 // output first header info
2982 if (haveLTFirstHead()) {
2983 if (endfirsthead.topDL)
2985 for (row_type r = 0; r < nrows(); ++r) {
2986 if (row_info[r].endfirsthead)
2987 TeXRow(os, r, runparams, columns, logical_columns);
2989 if (endfirsthead.bottomDL)
2991 os << "\\endfirsthead\n";
2993 // output header info
2995 if (endfirsthead.empty && !haveLTFirstHead())
2996 os << "\\endfirsthead\n";
2999 for (row_type r = 0; r < nrows(); ++r) {
3000 if (row_info[r].endhead)
3001 TeXRow(os, r, runparams, columns, logical_columns);
3003 if (endhead.bottomDL)
3005 os << "\\endhead\n";
3007 // output footer info
3011 for (row_type r = 0; r < nrows(); ++r) {
3012 if (row_info[r].endfoot)
3013 TeXRow(os, r, runparams, columns, logical_columns);
3015 if (endfoot.bottomDL)
3017 os << "\\endfoot\n";
3018 if (endlastfoot.empty && !haveLTLastFoot())
3019 os << "\\endlastfoot\n";
3021 // output lastfooter info
3022 if (haveLTLastFoot()) {
3023 if (endlastfoot.topDL)
3025 for (row_type r = 0; r < nrows(); ++r) {
3026 if (row_info[r].endlastfoot)
3027 TeXRow(os, r, runparams, columns, logical_columns);
3029 if (endlastfoot.bottomDL)
3031 os << "\\endlastfoot\n";
3036 bool Tabular::isValidRow(row_type row) const
3038 if (!is_long_tabular)
3040 return !row_info[row].endhead && !row_info[row].endfirsthead
3041 && !row_info[row].endfoot && !row_info[row].endlastfoot
3042 && !row_info[row].caption;
3046 void Tabular::TeXRow(otexstream & os, row_type row,
3047 OutputParams const & runparams,
3048 list<col_type> columns, list<col_type> logical_columns) const
3050 //output the top line
3051 TeXTopHLine(os, row, columns, logical_columns);
3053 if (row_info[row].top_space_default) {
3055 os << "\\addlinespace\n";
3057 os << "\\noalign{\\vskip\\doublerulesep}\n";
3058 } else if(!row_info[row].top_space.zero()) {
3060 os << "\\addlinespace["
3061 << from_ascii(row_info[row].top_space.asLatexString())
3064 os << "\\noalign{\\vskip"
3065 << from_ascii(row_info[row].top_space.asLatexString())
3069 bool ismulticol = false;
3070 bool ismultirow = false;
3072 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3073 // Luabibdi (used by LuaTeX) behaves like classic
3074 bool const bidi_rtl =
3075 runparams.local_font->isRightToLeft()
3076 && runparams.useBidiPackage();
3077 bool const ct = !buffer().params().output_changes;
3079 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3081 for (auto const & c : columns) {
3082 if (isPartOfMultiColumn(row, c))
3085 idx_type cell = cellIndex(row, c);
3087 if (isPartOfMultiRow(row, c)
3088 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3089 if (cell != lastcell)
3094 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3095 InsetTableCell const * inset = cellInset(cell);
3097 Paragraph const & par = inset->paragraphs().front();
3099 os.texrow().forceStart(par.id(), 0);
3101 bool rtl = par.isRTL(buffer().params())
3103 && getPWidth(cell).zero()
3104 && !runparams.use_polyglossia;
3108 par.getParLanguage(buffer().params())->lang();
3109 if (lang == "farsi")
3111 else if (lang == "arabic_arabi")
3113 // currently, remaining RTL languages are
3114 // arabic_arabtex and hebrew
3118 // pass to the OutputParams that we are in a cell and
3119 // which alignment we have set.
3120 // InsetNewline needs this context information.
3121 OutputParams newrp(runparams);
3122 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3123 ? OutputParams::PLAIN
3124 : OutputParams::ALIGNED;
3126 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3127 // copy cell and split in 2
3128 InsetTableCell head = InsetTableCell(*cellInset(cell));
3129 head.setBuffer(const_cast<Buffer &>(buffer()));
3130 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3132 dit.push_back(CursorSlice(head));
3133 head.setMacrocontextPositionRecursive(dit);
3134 bool hassep = false;
3135 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3137 tail.setBuffer(head.buffer());
3139 dit.push_back(CursorSlice(tail));
3140 tail.setMacrocontextPositionRecursive(dit);
3144 tail.latex(os, newrp);
3147 head.latex(os, newrp);
3149 head.latex(os, newrp);
3152 tail.latex(os, newrp);
3155 } else if (ltCaption(row)) {
3156 // Inside longtable caption rows, we must only output the caption inset
3157 // with its content and omit anything outside of that (see #10791)
3158 InsetTableCell & tc_inset = *const_cast<InsetTableCell *>(inset);
3159 for (Inset const & it : tc_inset) {
3160 if (it.lyxCode() != CAPTION_CODE)
3162 it.latex(os, runparams);
3165 } else if (!isPartOfMultiRow(row, c)) {
3166 if (!runparams.nice)
3167 os.texrow().start(par.id(), 0);
3168 inset->latex(os, newrp);
3171 runparams.encoding = newrp.encoding;
3175 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3176 if (cell != lastcell) { // not last cell in row
3183 os << "\\tabularnewline";
3184 if (row_info[row].bottom_space_default) {
3186 os << "\\addlinespace";
3188 os << "[\\doublerulesep]";
3189 } else if (!row_info[row].bottom_space.zero()) {
3191 os << "\\addlinespace";
3193 << from_ascii(row_info[row].bottom_space.asLatexString())
3198 //output the bottom line
3199 TeXBottomHLine(os, row, columns, logical_columns);
3201 if (row_info[row].interline_space_default) {
3203 os << "\\addlinespace\n";
3205 os << "\\noalign{\\vskip\\doublerulesep}\n";
3206 } else if (!row_info[row].interline_space.zero()) {
3208 os << "\\addlinespace["
3209 << from_ascii(row_info[row].interline_space.asLatexString())
3212 os << "\\noalign{\\vskip"
3213 << from_ascii(row_info[row].interline_space.asLatexString())
3219 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3221 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3222 && !hasVarwidthColumn();
3223 bool const is_xltabular = is_long_tabular
3224 && (hasVarwidthColumn() || !tabular_width.zero());
3225 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3227 //+---------------------------------------------------------------------
3228 //+ first the opening preamble +
3229 //+---------------------------------------------------------------------
3232 if (!TexRow::isNone(pos))
3233 os.texrow().start(pos);
3236 if (is_long_tabular)
3237 os << "\\begin{landscape}\n";
3239 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3242 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3243 // order for RTL (#9686). Thus we use this list.
3244 bool const bidi_rtl =
3245 runparams.local_font->isRightToLeft()
3246 && runparams.useBidiPackage();
3247 list<col_type> columns;
3248 list<col_type> logical_columns;
3249 for (col_type cl = 0; cl < ncols(); ++cl) {
3250 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3253 columns.push_front(cl);
3255 columns.push_back(cl);
3256 // for some calculations, we need the logical (non-swapped)
3257 // columns also in bidi.
3258 logical_columns.push_back(cl);
3261 // If we use \cline or \cmidrule, we need to locally de-activate
3262 // the - character when using languages that activate it (e.g., Czech, Slovak).
3263 bool deactivate_chars = false;
3264 if ((runparams.use_babel || runparams.use_polyglossia)
3265 && contains(runparams.active_chars, '-')) {
3266 bool have_clines = false;
3267 // Check if we use \cline or \cmidrule
3268 for (row_type row = 0; row < nrows(); ++row) {
3269 col_type bset = 0, tset = 0;
3270 for (auto const & c : columns) {
3271 idx_type const idx = cellIndex(row, c);
3272 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3273 || topLineTrim(idx).first || topLineTrim(idx).second) {
3277 if (bottomLine(cellIndex(row, c)))
3279 if (topLine(cellIndex(row, c)))
3282 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3288 deactivate_chars = true;
3289 os << "\\begingroup\n"
3290 << "\\catcode`\\-=12\n";
3294 if (is_long_tabular) {
3296 os << "\\begin{xltabular}";
3298 os << "\\begin{longtable}";
3299 switch (longtabular_alignment) {
3300 case LYX_LONGTABULAR_ALIGN_LEFT:
3303 case LYX_LONGTABULAR_ALIGN_CENTER:
3306 case LYX_LONGTABULAR_ALIGN_RIGHT:
3311 if (tabular_width.zero())
3312 os << "{" << from_ascii("\\columnwidth") << "}";
3314 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3317 if (is_tabular_star)
3318 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3319 else if (hasVarwidthColumn()) {
3320 os << "\\begin{tabularx}{";
3321 if (tabular_width.zero())
3322 os << from_ascii("\\columnwidth") << "}";
3324 os << from_ascii(tabular_width.asLatexString()) << "}";
3326 os << "\\begin{tabular}";
3327 switch (tabular_valignment) {
3328 case LYX_VALIGN_TOP:
3331 case LYX_VALIGN_MIDDLE:
3333 case LYX_VALIGN_BOTTOM:
3341 if (is_tabular_star)
3342 os << "@{\\extracolsep{\\fill}}";
3344 for (auto const & c : columns) {
3345 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3347 if (!column_info[c].align_special.empty()) {
3348 os << column_info[c].align_special;
3350 if (!column_info[c].p_width.zero()) {
3351 bool decimal = false;
3352 switch (column_info[c].alignment) {
3353 case LYX_ALIGN_LEFT:
3354 os << ">{\\raggedright}";
3356 case LYX_ALIGN_RIGHT:
3357 os << ">{\\raggedleft}";
3359 case LYX_ALIGN_CENTER:
3360 os << ">{\\centering}";
3362 case LYX_ALIGN_NONE:
3363 case LYX_ALIGN_BLOCK:
3364 case LYX_ALIGN_LAYOUT:
3365 case LYX_ALIGN_SPECIAL:
3367 case LYX_ALIGN_DECIMAL: {
3369 os << ">{\\raggedright}";
3371 os << ">{\\raggedleft}";
3378 switch (column_info[c].valignment) {
3379 case LYX_VALIGN_TOP:
3380 // this is the default
3382 case LYX_VALIGN_MIDDLE:
3385 case LYX_VALIGN_BOTTOM:
3391 // Fixed-width cells with alignment at decimal separator
3392 // are output as two cells of half the width with the decimal
3393 // separator as column sep. This effectively puts the content
3394 // centered, which differs from the normal decimal sep alignment
3395 // and is not ideal, but we cannot do better ATM (see #9568).
3396 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3398 docstring const halffixedwith =
3399 from_ascii(Length(column_info[c].p_width.value() / 2,
3400 column_info[c].p_width.unit()).asLatexString());
3404 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3411 << from_ascii(column_info[c].p_width.asLatexString())
3413 } else if (column_info[c].varwidth) {
3414 switch (column_info[c].alignment) {
3415 case LYX_ALIGN_LEFT:
3416 os << ">{\\raggedright\\arraybackslash}";
3418 case LYX_ALIGN_RIGHT:
3419 os << ">{\\raggedleft\\arraybackslash}";
3421 case LYX_ALIGN_CENTER:
3422 os << ">{\\centering\\arraybackslash}";
3424 case LYX_ALIGN_NONE:
3425 case LYX_ALIGN_BLOCK:
3426 case LYX_ALIGN_LAYOUT:
3427 case LYX_ALIGN_SPECIAL:
3428 case LYX_ALIGN_DECIMAL:
3432 } else if (isVTypeColumn(c))
3433 os << "V{\\linewidth}";
3435 switch (column_info[c].alignment) {
3436 case LYX_ALIGN_LEFT:
3439 case LYX_ALIGN_RIGHT:
3442 case LYX_ALIGN_DECIMAL:
3443 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3449 } // end if else !column_info[i].p_width
3450 } // end if else !column_info[i].align_special
3451 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3456 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3458 //+---------------------------------------------------------------------
3459 //+ the single row and columns (cells) +
3460 //+---------------------------------------------------------------------
3462 for (row_type r = 0; r < nrows(); ++r) {
3463 if (!buffer().params().output_changes && row_info[r].change.deleted())
3465 if (isValidRow(r)) {
3466 TeXRow(os, r, runparams, columns, logical_columns);
3467 if (is_long_tabular && row_info[r].newpage)
3468 os << "\\newpage\n";
3472 //+---------------------------------------------------------------------
3473 //+ the closing of the tabular +
3474 //+---------------------------------------------------------------------
3476 if (is_long_tabular) {
3478 os << "\\end{xltabular}";
3480 os << "\\end{longtable}";
3482 if (is_tabular_star)
3483 os << "\\end{tabular*}";
3484 else if (hasVarwidthColumn())
3485 os << "\\end{tabularx}";
3487 os << "\\end{tabular}";
3490 if (deactivate_chars)
3492 os << "\n\\endgroup\n";
3495 if (is_long_tabular)
3496 os << breakln << "\\end{landscape}";
3498 os << breakln << "\\end{turn}";
3501 if (!TexRow::isNone(pos))
3502 os.texrow().start(pos);
3506 void Tabular::docbookRow(XMLStream & xs, row_type row,
3507 OutputParams const & runparams, bool header) const
3509 switch (buffer().params().docbook_table_output) {
3510 case BufferParams::HTMLTable:
3511 docbookRowAsHTML(xs, row, runparams, header);
3513 case BufferParams::CALSTable:
3514 docbookRowAsCALS(xs, row, runparams);
3520 void Tabular::docbookRowAsHTML(XMLStream & xs, row_type row,
3521 OutputParams const & runparams, bool header) const
3523 string const celltag = header ? "th" : "td";
3524 idx_type cell = getFirstCellInRow(row);
3526 xs << xml::StartTag("tr");
3528 for (col_type c = 0; c < ncols(); ++c) {
3529 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3534 Length const cwidth = column_info[c].p_width;
3535 if (!cwidth.zero()) {
3536 string const hwidth = cwidth.asHTMLString();
3537 attr << "style =\"width: " << hwidth << ";\" ";
3541 switch (getAlignment(cell)) {
3542 case LYX_ALIGN_BLOCK:
3545 case LYX_ALIGN_DECIMAL: {
3546 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3547 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3550 case LYX_ALIGN_LEFT:
3553 case LYX_ALIGN_RIGHT:
3561 attr << " valign='";
3562 switch (getVAlignment(cell)) {
3563 case LYX_VALIGN_TOP:
3566 case LYX_VALIGN_BOTTOM:
3569 case LYX_VALIGN_MIDDLE:
3574 if (isMultiColumn(cell))
3575 attr << " colspan='" << columnSpan(cell) << "'";
3576 else if (isMultiRow(cell))
3577 attr << " rowspan='" << rowSpan(cell) << "'";
3579 xs << xml::StartTag(celltag, attr.str(), true);
3580 cellInset(cell)->docbook(xs, runparams);
3581 xs << xml::EndTag(celltag);
3585 xs << xml::EndTag("tr");
3590 void Tabular::docbookRowAsCALS(XMLStream & xs, row_type row,
3591 OutputParams const & runparams) const
3593 idx_type cell = getFirstCellInRow(row);
3595 xs << xml::StartTag("row");
3597 for (col_type c = 0; c < ncols(); ++c) {
3598 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3604 switch (getAlignment(cell)) {
3605 case LYX_ALIGN_BLOCK:
3608 case LYX_ALIGN_DECIMAL: {
3609 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3610 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3613 case LYX_ALIGN_LEFT:
3616 case LYX_ALIGN_RIGHT:
3625 attr << " valign='";
3626 switch (getVAlignment(cell)) {
3627 case LYX_VALIGN_TOP:
3630 case LYX_VALIGN_BOTTOM:
3633 case LYX_VALIGN_MIDDLE:
3638 if (isMultiColumn(cell))
3639 attr << " colspan='" << columnSpan(cell) << "'";
3640 else if (isMultiRow(cell))
3641 attr << " rowspan='" << rowSpan(cell) << "'";
3643 attr << " colname='c" << (c + 1) << "'"; // Column numbering starts at 1.
3645 // All cases where there should be a line *below* this row.
3646 if (row_info[row].bottom_space_default)
3647 attr << " rowsep='1'";
3649 xs << xml::StartTag("entry", attr.str(), true);
3650 cellInset(cell)->docbook(xs, runparams);
3651 xs << xml::EndTag("entry");
3655 xs << xml::EndTag("row");
3660 void Tabular::docbook(XMLStream & xs, OutputParams const & runparams) const
3664 // Some tables are inline. Likely limitation: cannot output a table within a table; is that really a limitation?
3665 if (!runparams.docbook_in_table) { // Check on the *outer* set of parameters, so that the table can be closed
3666 // properly at the end of this function.
3667 xs << xml::StartTag("informaltable");
3671 // "Formal" tables have a title and use the tag <table>; the distinction with <informaltable> is done outside.
3672 // HTML has the caption first with titles forbidden, and CALS has a title first.
3673 if (haveLTCaption()) {
3674 std::string tag = ((buffer().params().docbook_table_output) == BufferParams::HTMLTable) ? "caption" : "title";
3676 xs << xml::StartTag(tag);
3677 for (row_type r = 0; r < nrows(); ++r)
3678 if (row_info[r].caption)
3679 docbookRow(xs, r, runparams);
3680 xs << xml::EndTag(tag);
3684 // CALS header: describe all columns in this table. For names, take 'c' then the ID of the column.
3685 // Start at one, as is customary with CALS!
3686 if (buffer().params().docbook_table_output == BufferParams::CALSTable) {
3687 for (col_type c = 0; c < ncols(); ++c) {
3688 std::stringstream attr;
3689 attr << "colnum='" << (c + 1) << "' ";
3690 attr << "colname='c" << (c + 1) << "' ";
3691 Length const cwidth = column_info[c].p_width;
3693 attr << "colwidth='" << cwidth.asHTMLString() << "' ";
3694 attr << "rowheader='norowheader'"; // Last attribute, hence no space at the end.
3696 xs << xml::CompTag("colspec", attr.str());
3701 // Output the header of the table. For both HTML and CALS, this is surrounded by a thead.
3702 bool const havefirsthead = haveLTFirstHead(false);
3703 // if we have a first head, then we are going to ignore the
3704 // headers for the additional pages, since there aren't any
3705 // in DocBook. this test accomplishes that.
3706 bool const havehead = !havefirsthead && haveLTHead(false);
3707 if (havehead || havefirsthead) {
3708 xs << xml::StartTag("thead") << xml::CR();
3709 for (row_type r = 0; r < nrows(); ++r) {
3710 if (((havefirsthead && row_info[r].endfirsthead) ||
3711 (havehead && row_info[r].endhead)) &&
3712 !row_info[r].caption) {
3713 docbookRow(xs, r, runparams, true); // TODO: HTML vs CALS
3716 xs << xml::EndTag("thead");
3720 // Output the footer of the table. For both HTML and CALS, this is surrounded by a tfoot and output just after
3721 // the header (and before the body).
3722 bool const havelastfoot = haveLTLastFoot(false);
3724 bool const havefoot = !havelastfoot && haveLTFoot(false);
3725 if (havefoot || havelastfoot) {
3726 xs << xml::StartTag("tfoot") << xml::CR();
3727 for (row_type r = 0; r < nrows(); ++r) {
3728 if (((havelastfoot && row_info[r].endlastfoot) ||
3729 (havefoot && row_info[r].endfoot)) &&
3730 !row_info[r].caption) {
3731 docbookRow(xs, r, runparams); // TODO: HTML vs CALS
3734 xs << xml::EndTag("tfoot");
3738 // Output the main part of the table. The tbody container is mandatory for CALS, but optional for HTML (only if
3739 // there is no header and no footer). It never hurts to have it, though.
3740 xs << xml::StartTag("tbody");
3742 for (row_type r = 0; r < nrows(); ++r)
3744 docbookRow(xs, r, runparams);
3745 xs << xml::EndTag("tbody");
3748 // If this method started the table tag, also make it close it.
3749 if (!runparams.docbook_in_table) {
3750 xs << xml::EndTag("informaltable");
3756 docstring Tabular::xhtmlRow(XMLStream & xs, row_type row,
3757 OutputParams const & runparams, bool header) const
3760 string const celltag = header ? "th" : "td";
3761 idx_type cell = getFirstCellInRow(row);
3763 xs << xml::StartTag("tr");
3764 for (col_type c = 0; c < ncols(); ++c) {
3765 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3770 Length const cwidth = column_info[c].p_width;
3771 if (!cwidth.zero()) {
3772 string const hwidth = cwidth.asHTMLString();
3773 attr << "style =\"width: " << hwidth << ";\" ";
3777 switch (getAlignment(cell)) {
3778 case LYX_ALIGN_LEFT:
3781 case LYX_ALIGN_RIGHT:
3789 attr << " valign='";
3790 switch (getVAlignment(cell)) {
3791 case LYX_VALIGN_TOP:
3794 case LYX_VALIGN_BOTTOM:
3797 case LYX_VALIGN_MIDDLE:
3802 if (isMultiColumn(cell))
3803 attr << " colspan='" << columnSpan(cell) << "'";
3804 else if (isMultiRow(cell))
3805 attr << " rowspan='" << rowSpan(cell) << "'";
3807 xs << xml::StartTag(celltag, attr.str(), true) << xml::CR();
3808 ret += cellInset(cell)->xhtml(xs, runparams);
3809 xs << xml::EndTag(celltag) << xml::CR();
3812 xs << xml::EndTag("tr");
3817 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3821 if (is_long_tabular) {
3822 // we'll wrap it in a div, so as to deal with alignment
3824 switch (longtabular_alignment) {
3825 case LYX_LONGTABULAR_ALIGN_LEFT:
3828 case LYX_LONGTABULAR_ALIGN_CENTER:
3831 case LYX_LONGTABULAR_ALIGN_RIGHT:
3835 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
3837 // The caption flag wins over head/foot
3838 if (haveLTCaption()) {
3839 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
3841 for (row_type r = 0; r < nrows(); ++r)
3842 if (row_info[r].caption)
3843 ret += xhtmlRow(xs, r, runparams);
3844 xs << xml::EndTag("div");
3849 xs << xml::StartTag("table");
3852 // output header info
3853 bool const havefirsthead = haveLTFirstHead(false);
3854 // if we have a first head, then we are going to ignore the
3855 // headers for the additional pages, since there aren't any
3856 // in XHTML. this test accomplishes that.
3857 bool const havehead = !havefirsthead && haveLTHead(false);
3858 if (havehead || havefirsthead) {
3859 xs << xml::StartTag("thead");
3861 for (row_type r = 0; r < nrows(); ++r) {
3862 if (((havefirsthead && row_info[r].endfirsthead) ||
3863 (havehead && row_info[r].endhead)) &&
3864 !row_info[r].caption) {
3865 ret += xhtmlRow(xs, r, runparams, true);
3868 xs << xml::EndTag("thead");
3871 // output footer info
3872 bool const havelastfoot = haveLTLastFoot(false);
3874 bool const havefoot = !havelastfoot && haveLTFoot(false);
3875 if (havefoot || havelastfoot) {
3876 xs << xml::StartTag("tfoot") << xml::CR();
3877 for (row_type r = 0; r < nrows(); ++r) {
3878 if (((havelastfoot && row_info[r].endlastfoot) ||
3879 (havefoot && row_info[r].endfoot)) &&
3880 !row_info[r].caption) {
3881 ret += xhtmlRow(xs, r, runparams);
3884 xs << xml::EndTag("tfoot");
3888 xs << xml::StartTag("tbody") << xml::CR();
3889 for (row_type r = 0; r < nrows(); ++r)
3891 ret += xhtmlRow(xs, r, runparams);
3892 xs << xml::EndTag("tbody");
3894 xs << xml::EndTag("table");
3896 if (is_long_tabular) {
3897 xs << xml::EndTag("div");
3904 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3905 vector<unsigned int> const & clen) const
3907 idx_type const fcell = getFirstCellInRow(row);
3908 idx_type const n = numberOfCellsInRow(row) + fcell;
3911 for (idx_type i = fcell; i < n; ++i) {
3921 for (idx_type i = fcell; i < n; ++i) {
3932 col_type column = cellColumn(i);
3933 int len = clen[column];
3934 while (column < ncols() - 1
3935 && isPartOfMultiColumn(row, ++column))
3936 len += clen[column] + 4;
3937 os << docstring(len, ch);
3952 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3953 vector<unsigned int> const & clen) const
3955 idx_type const fcell = getFirstCellInRow(row);
3956 idx_type const n = numberOfCellsInRow(row) + fcell;
3959 for (idx_type i = fcell; i < n; ++i) {
3960 if (bottomLine(i)) {
3969 for (idx_type i = fcell; i < n; ++i) {
3970 if (bottomLine(i)) {
3980 col_type column = cellColumn(i);
3981 int len = clen[column];
3982 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3983 len += clen[column] + 4;
3984 os << docstring(len, ch);
3985 if (bottomLine(i)) {
3999 void Tabular::plaintextPrintCell(odocstringstream & os,
4000 OutputParams const & runparams,
4001 idx_type cell, row_type row, col_type column,
4002 vector<unsigned int> const & clen,
4003 bool onlydata, size_t max_length) const
4005 odocstringstream sstr;
4006 cellInset(cell)->plaintext(sstr, runparams, max_length);
4018 unsigned int len1 = sstr.str().length();
4019 unsigned int len2 = clen[column];
4020 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4021 len2 += clen[column] + 4;
4024 switch (getAlignment(cell)) {
4026 case LYX_ALIGN_LEFT:
4029 case LYX_ALIGN_RIGHT:
4033 case LYX_ALIGN_CENTER:
4039 os << docstring(len1, ' ') << sstr.str()
4040 << docstring(len2, ' ');
4042 if (rightLine(cell))
4049 void Tabular::plaintext(odocstringstream & os,
4050 OutputParams const & runparams, int const depth,
4051 bool onlydata, char_type delim, size_t max_length) const
4053 // first calculate the width of the single columns
4054 vector<unsigned int> clen(ncols());
4057 // first all non multicolumn cells!
4058 for (col_type c = 0; c < ncols(); ++c) {
4060 for (row_type r = 0; r < nrows(); ++r) {
4061 idx_type cell = cellIndex(r, c);
4062 if (isMultiColumn(cell))
4064 odocstringstream sstr;
4065 cellInset(cell)->plaintext(sstr, runparams, max_length);
4066 if (clen[c] < sstr.str().length())
4067 clen[c] = sstr.str().length();
4070 // then all multicolumn cells!
4071 for (col_type c = 0; c < ncols(); ++c) {
4072 for (row_type r = 0; r < nrows(); ++r) {
4073 idx_type cell = cellIndex(r, c);
4074 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
4076 odocstringstream sstr;
4077 cellInset(cell)->plaintext(sstr, runparams, max_length);
4078 int len = int(sstr.str().length());
4079 idx_type const n = columnSpan(cell);
4080 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
4082 if (len > int(clen[c + n - 1]))
4083 clen[c + n - 1] = len;
4088 for (row_type r = 0; r < nrows(); ++r) {
4089 if (!onlydata && plaintextTopHLine(os, r, clen))
4090 os << docstring(depth * 2, ' ');
4091 for (col_type c = 0; c < ncols(); ++c) {
4092 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
4094 if (onlydata && c > 0)
4095 // we don't use operator<< for single UCS4 character.
4096 // see explanation in docstream.h
4098 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
4100 if (os.str().size() > max_length)
4105 os << docstring(depth * 2, ' ');
4106 if (plaintextBottomHLine(os, r, clen))
4107 os << docstring(depth * 2, ' ');
4109 if (os.str().size() > max_length)
4115 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4117 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4121 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4123 return cell_info[row][column].inset;
4127 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4129 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4133 void Tabular::setCellInset(row_type row, col_type column,
4134 shared_ptr<InsetTableCell> ins)
4136 CellData & cd = cell_info[row][column];
4141 void Tabular::validate(LaTeXFeatures & features) const
4143 features.require("NeedTabularnewline");
4145 features.require("booktabs");
4146 if (is_long_tabular && !hasVarwidthColumn()) {
4147 if (tabular_width.zero())
4148 features.require("longtable");
4150 features.require("xltabular");
4152 if (rotate && is_long_tabular)
4153 features.require("lscape");
4155 features.require("rotating");
4156 if (hasVarwidthColumn()) {
4157 if (is_long_tabular)
4158 features.require("xltabular");
4160 features.require("tabularx");
4162 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4163 if (isMultiRow(cell))
4164 features.require("multirow");
4165 if (getUsebox(cell) == BOX_VARWIDTH) {
4166 features.require("varwidth");
4167 features.require("cellvarwidth");
4169 if (getVAlignment(cell) != LYX_VALIGN_TOP
4170 || !getPWidth(cell).zero()
4171 || isVTypeColumn(cellColumn(cell)))
4172 features.require("array");
4173 // Tell footnote that we need a savenote
4174 // environment in non-long tables or
4175 // longtable headers/footers
4176 else if (!is_long_tabular && !features.inFloat())
4177 features.saveNoteEnv("tabular");
4178 else if (!isValidRow(cellRow(cell)))
4179 features.saveNoteEnv("longtable");
4181 cellInset(cell)->validate(features);
4182 features.saveNoteEnv(string());
4187 Tabular::BoxType Tabular::useBox(idx_type cell) const
4189 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4190 if (parlist.size() > 1)
4191 return BOX_VARWIDTH;
4193 ParagraphList::const_iterator cit = parlist.begin();
4194 ParagraphList::const_iterator end = parlist.end();
4196 for (; cit != end; ++cit)
4197 for (int i = 0; i < cit->size(); ++i)
4198 if (cit->isNewline(i) || cit->layout().isEnvironment())
4199 return BOX_VARWIDTH;
4205 /////////////////////////////////////////////////////////////////////
4209 /////////////////////////////////////////////////////////////////////
4211 InsetTableCell::InsetTableCell(Buffer * buf)
4212 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
4213 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4216 bool InsetTableCell::forcePlainLayout(idx_type) const
4222 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4224 return isFixedWidth;
4228 bool InsetTableCell::forceLocalFontSwitch() const
4234 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4235 FuncStatus & status) const
4237 bool enabled = true;
4238 switch (cmd.action()) {
4239 case LFUN_INSET_SPLIT:
4240 case LFUN_INSET_DISSOLVE:
4243 case LFUN_MATH_DISPLAY:
4244 if (!hasFixedWidth()) {
4249 return InsetText::getStatus(cur, cmd, status);
4251 status.setEnabled(enabled);
4255 docstring InsetTableCell::asString(bool intoInsets)
4258 if (paragraphs().empty())
4260 ParagraphList::const_iterator it = paragraphs().begin();
4261 ParagraphList::const_iterator en = paragraphs().end();
4263 for (; it != en; ++it) {
4268 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4274 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4275 UpdateType utype, TocBackend & backend) const
4277 InsetText::iterateForToc(di, output_active, utype, backend);
4281 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4284 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4285 return InsetText::xhtml(xs, rp);
4289 void InsetTableCell::docbook(XMLStream & xs, OutputParams const & runparams) const
4291 InsetText::docbook(xs, runparams);
4295 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4297 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4298 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4300 // Hand font through to contained lyxtext:
4301 tm.font_.fontInfo() = mi.base.font;
4302 mi.base.textwidth -= horiz_offset;
4304 // This can happen when a layout has a left and right margin,
4305 // and the view is made very narrow. We can't do better than
4306 // to draw it partly out of view (bug 5890).
4307 if (mi.base.textwidth < 1)
4308 mi.base.textwidth = 1;
4310 // We tell metrics here not to expand on multiple pars
4311 // This is the difference to InsetText::Metrics
4312 if (hasFixedWidth())
4313 tm.metrics(mi, dim, mi.base.textwidth, false);
4315 tm.metrics(mi, dim, 0, false);
4316 mi.base.textwidth += horiz_offset;
4317 dim.asc += topOffset(mi.base.bv);
4318 dim.des += bottomOffset(mi.base.bv);
4319 dim.wid += horiz_offset;
4323 /////////////////////////////////////////////////////////////////////
4327 /////////////////////////////////////////////////////////////////////
4329 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4331 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4332 rowselect_(false), colselect_(false)
4337 InsetTabular::InsetTabular(InsetTabular const & tab)
4338 : Inset(tab), tabular(tab.tabular),
4339 rowselect_(false), colselect_(false)
4344 InsetTabular::~InsetTabular()
4346 hideDialogs("tabular", this);
4350 void InsetTabular::setBuffer(Buffer & buf)
4352 tabular.setBuffer(buf);
4353 Inset::setBuffer(buf);
4357 bool InsetTabular::insetAllowed(InsetCode code) const
4362 case MATHMACRO_CODE:
4367 return tabular.is_long_tabular;
4375 bool InsetTabular::allowMultiPar() const
4377 for (col_type c = 0; c < tabular.ncols(); ++c) {
4378 for (row_type r = 0; r < tabular.nrows(); ++r) {
4379 if (tabular.cellInset(r,c)->allowMultiPar())
4387 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4389 return tabular.is_long_tabular &&
4390 (newtype == "Standard" || newtype == "Unnumbered");
4394 void InsetTabular::write(ostream & os) const
4396 os << "Tabular" << endl;
4401 string InsetTabular::contextMenu(BufferView const &, int, int) const
4403 // FIXME: depending on the selection state,
4404 // we could offer a different menu.
4405 return cell(0)->contextMenuName() + ";" + contextMenuName();
4409 string InsetTabular::contextMenuName() const
4411 return "context-tabular";
4415 void InsetTabular::read(Lexer & lex)
4417 //bool const old_format = (lex.getString() == "\\LyXTable");
4425 string token = lex.getString();
4426 while (lex && token != "\\end_inset") {
4428 token = lex.getString();
4431 lex.printError("Missing \\end_inset at this point. ");
4435 int InsetTabular::rowFromY(Cursor & cur, int y) const
4437 // top y coordinate of tabular
4438 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4440 for (; r < tabular.nrows() && y > h; ++r)
4441 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4442 + tabular.interRowSpace(r);
4448 int InsetTabular::columnFromX(Cursor & cur, int x) const
4450 // left x coordinate of tabular
4451 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4453 for (; c < tabular.ncols() && x > w; ++c)
4454 w += tabular.cellWidth(c);
4459 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4461 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4462 // mi.base.textwidth << "\n";
4463 LBUFERR(mi.base.bv);
4465 for (row_type r = 0; r < tabular.nrows(); ++r) {
4468 for (col_type c = 0; c < tabular.ncols(); ++c) {
4469 if (tabular.isPartOfMultiColumn(r, c)
4470 || tabular.isPartOfMultiRow(r, c))
4471 // multicolumn or multirow cell, but not first one
4473 idx_type const cell = tabular.cellIndex(r, c);
4476 Length const p_width = tabular.getPWidth(cell);
4477 if (!p_width.zero())
4478 m.base.textwidth = mi.base.inPixels(p_width);
4479 else if (tabular.column_info[c].varwidth)
4480 m.base.textwidth = tabular.column_info[c].width;
4481 tabular.cellInset(cell)->metrics(m, dim0);
4482 if (!p_width.zero() || tabular.column_info[c].varwidth)
4483 dim0.wid = m.base.textwidth;
4484 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4485 + tabular.interColumnSpace(cell);
4487 // FIXME(?): do we need a second metrics call?
4488 TextMetrics const & tm =
4489 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4491 // determine horizontal offset because of decimal align (if necessary)
4492 int decimal_width = 0;
4493 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4494 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4495 tail.setBuffer(tabular.buffer());
4496 // we need to set macrocontext position everywhere
4497 // otherwise we crash with nested insets (e.g. footnotes)
4498 // after decimal point
4499 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4501 dit.push_back(CursorSlice(tail));
4502 tail.setMacrocontextPositionRecursive(dit);
4504 // remove text leading decimal point
4505 docstring const align_d = tabular.column_info[c].decimal_point;
4506 dit = separatorPos(&tail, align_d);
4508 pos_type const psize = tail.paragraphs().front().size();
4510 tail.paragraphs().front().eraseChars(0,
4511 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4513 tail.metrics(m, dim1);
4514 decimal_width = dim1.width();
4517 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4518 tabular.cell_info[r][c].decimal_width = decimal_width;
4520 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4521 // = descent of text in last par + bottomOffset:
4522 int const lastpardes = tm.last().second->descent()
4523 + bottomOffset(mi.base.bv);
4525 switch (tabular.getVAlignment(cell)) {
4526 case Tabular::LYX_VALIGN_TOP:
4528 case Tabular::LYX_VALIGN_MIDDLE:
4529 offset = -(dim0.des - lastpardes)/2;
4531 case Tabular::LYX_VALIGN_BOTTOM:
4532 offset = -(dim0.des - lastpardes);
4535 tabular.cell_info[r][c].voffset = offset;
4536 maxasc = max(maxasc, dim0.asc - offset);
4537 maxdes = max(maxdes, dim0.des + offset);
4539 int const top_space = tabular.row_info[r].top_space_default ?
4540 default_line_space :
4541 mi.base.inPixels(tabular.row_info[r].top_space);
4542 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4543 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4544 default_line_space :
4545 mi.base.inPixels(tabular.row_info[r].bottom_space);
4546 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4549 // We need to recalculate the metrics after column width calculation
4550 // with xtabular (possibly multiple times, so the call is recursive).
4551 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4553 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4554 dim.des = tabular.height() - dim.asc;
4555 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4559 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4561 if (&cur.inset() == this && cur.selection()) {
4562 if (cur.selIsMultiCell()) {
4565 getSelection(cur, rs, re, cs, ce);
4567 idx_type const cell = tabular.cellIndex(row, col);
4568 col_type const cspan = tabular.columnSpan(cell);
4569 row_type const rspan = tabular.rowSpan(cell);
4570 if (col + cspan - 1 >= cs && col <= ce
4571 && row + rspan - 1 >= rs && row <= re)
4574 if (col == tabular.cellColumn(cur.idx())
4575 && row == tabular.cellRow(cur.idx())) {
4576 CursorSlice const & beg = cur.selBegin();
4577 CursorSlice const & end = cur.selEnd();
4579 if ((end.lastpos() > 0 || end.lastpit() > 0)
4580 && end.pos() == end.lastpos() && beg.pos() == 0
4581 && end.pit() == end.lastpit() && beg.pit() == 0)
4589 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4591 x += ADD_TO_TABULAR_WIDTH;
4593 BufferView * bv = pi.base.bv;
4594 Cursor & cur = pi.base.bv->cursor();
4596 // FIXME: As the full background is painted in drawBackground(),
4597 // we have no choice but to do a full repaint for the Text cells.
4598 pi.full_repaint = true;
4600 bool const original_selection_state = pi.selected;
4604 // Save tabular change status
4605 Change tab_change = pi.change;
4607 int yy = y + tabular.offsetVAlignment();
4608 for (row_type r = 0; r < tabular.nrows(); ++r) {
4610 for (col_type c = 0; c < tabular.ncols(); ++c) {
4611 if (tabular.isPartOfMultiColumn(r, c))
4614 idx = tabular.cellIndex(r, c);
4616 if (tabular.isPartOfMultiRow(r, c)) {
4617 nx += tabular.cellWidth(idx);
4621 pi.selected |= isCellSelected(cur, r, c);
4623 // Mark deleted rows/columns
4624 if (tabular.column_info[c].change.changed())
4625 pi.change = tabular.column_info[c].change;
4626 else if (tabular.row_info[r].change.changed())
4627 pi.change = tabular.row_info[r].change;
4629 pi.change = tab_change;
4631 int const cx = nx + tabular.textHOffset(idx);
4632 int const cy = yy + tabular.textVOffset(idx);
4633 // Cache the Inset position.
4634 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4635 cell(idx)->draw(pi, cx, cy);
4636 drawCellLines(pi, nx, yy, r, idx);
4637 nx += tabular.cellWidth(idx);
4638 pi.selected = original_selection_state;
4641 if (r + 1 < tabular.nrows())
4642 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4643 + tabular.interRowSpace(r + 1);
4648 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4650 x += ADD_TO_TABULAR_WIDTH;
4651 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4652 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4653 pi.backgroundColor(this));
4657 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4659 Cursor & cur = pi.base.bv->cursor();
4661 x += ADD_TO_TABULAR_WIDTH;
4663 if (!cur.selection())
4665 if (&cur.inset() != this)
4670 bool const full_cell_selected = isCellSelected(cur,
4671 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4673 if (cur.selIsMultiCell() || full_cell_selected) {
4674 for (row_type r = 0; r < tabular.nrows(); ++r) {
4676 for (col_type c = 0; c < tabular.ncols(); ++c) {
4677 if (tabular.isPartOfMultiColumn(r, c))
4680 idx_type const cell = tabular.cellIndex(r, c);
4682 if (tabular.isPartOfMultiRow(r, c)) {
4683 xx += tabular.cellWidth(cell);
4686 int const w = tabular.cellWidth(cell);
4687 int const h = tabular.cellHeight(cell);
4688 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4689 if (isCellSelected(cur, r, c))
4690 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4693 if (r + 1 < tabular.nrows())
4694 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4695 + tabular.interRowSpace(r + 1);
4699 // FIXME: This code has no effect because InsetTableCell does not handle
4700 // drawSelection other than the trivial implementation in Inset.
4702 // x += cellXPos(cur.idx());
4703 // x += tabular.textHOffset(cur.idx());
4704 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4711 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4712 Color const incol, bool drawline, bool heavy = false)
4714 Color const col = drawline ? incol : Color_tabularonoffline;
4715 if (drawline && lt > 0)
4716 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4717 Painter::line_onoffdash,
4718 Painter::thin_line);
4719 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4720 drawline ? Painter::line_solid : Painter::line_onoffdash,
4721 (heavy ? 2 : 1) * Painter::thin_line);
4722 if (drawline && rt > 0)
4723 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4724 Painter::line_onoffdash,
4725 Painter::thin_line);
4731 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4732 row_type row, idx_type cell) const
4734 y -= tabular.rowAscent(row);
4735 int const w = tabular.cellWidth(cell);
4736 int const h = tabular.cellHeight(cell);
4740 col_type const col = tabular.cellColumn(cell);
4742 // Colour the frame if rows/columns are added or deleted
4743 Color colour = Color_tabularline;
4744 if (tabular.column_info[col].change.changed()
4745 || tabular.row_info[row].change.changed())
4746 colour = InsetTableCell(*tabular.cellInset(cell)).paragraphs().front().lookupChange(0).color();
4749 bool drawline = tabular.topLine(cell)
4750 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4751 bool heavy = tabular.use_booktabs
4752 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4753 && tabular.rowTopLine(row);
4754 if (tabular.topLineTrim(cell).first
4755 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4757 if (tabular.topLineTrim(cell).second
4758 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4760 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4764 drawline = tabular.bottomLine(cell);
4765 row_type const lastrow = tabular.nrows() - 1;
4766 // Consider multi-rows
4768 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4769 && tabular.isPartOfMultiRow(r + 1, col))
4771 heavy = tabular.use_booktabs
4772 && ((row == lastrow && tabular.rowBottomLine(row))
4773 || (r == lastrow && tabular.rowBottomLine(r)));
4774 if (tabular.bottomLineTrim(cell).first)
4776 if (tabular.bottomLineTrim(cell).second)
4778 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4781 drawline = tabular.leftLine(cell)
4782 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4783 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4786 x -= tabular.interColumnSpace(cell);
4787 col_type next_cell_col = col + 1;
4788 while (next_cell_col < tabular.ncols()
4789 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4791 drawline = tabular.rightLine(cell)
4792 || (next_cell_col < tabular.ncols()
4793 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4794 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4798 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4800 //lyxerr << "InsetTabular::edit: " << this << endl;
4804 if (isRightToLeft(cur))
4805 cur.idx() = tabular.getLastCellInRow(0);
4811 if (isRightToLeft(cur))
4812 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4814 cur.idx() = tabular.numberofcells - 1;
4816 cur.pos() = cur.lastpos(); // FIXME crude guess
4818 cur.setCurrentFont();
4819 // FIXME: this accesses the position cache before it is initialized
4820 //cur.bv().fitCursor();
4824 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4826 // In a longtable, tell captions what the current float is
4827 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4828 string const saveflt = cnts.current_float();
4829 if (tabular.is_long_tabular) {
4830 cnts.current_float("table");
4831 // in longtables, we only step the counter once
4832 cnts.step(from_ascii("table"), utype);
4833 cnts.isLongtable(true);
4836 ParIterator it2 = it;
4838 size_t const end = it2.nargs();
4839 for ( ; it2.idx() < end; it2.top().forwardIdx())
4840 buffer().updateBuffer(it2, utype);
4843 if (tabular.is_long_tabular) {
4844 cnts.current_float(saveflt);
4845 cnts.isLongtable(false);
4850 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4851 UpdateType utype, TocBackend & backend) const
4853 DocIterator dit = cpit;
4855 size_t const end = dit.nargs();
4856 for ( ; dit.idx() < end; dit.top().forwardIdx())
4857 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4861 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4863 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4864 return x < x0 || x > x0 + tabular.width();
4868 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4870 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4871 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4872 // there is no margin added vertically to tabular insets.
4873 // However, it works for now.
4874 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4878 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4880 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4884 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4886 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4887 << "\n cur:" << cur);
4888 CursorSlice sl = cur.top();
4889 Cursor & bvcur = cur.bv().cursor();
4891 FuncCode const act = cmd.action();
4895 case LFUN_MOUSE_PRESS: {
4896 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4898 if (hitSelectRow(cur.bv(), cmd.x())) {
4899 row_type r = rowFromY(cur, cmd.y());
4900 cur.idx() = tabular.getFirstCellInRow(r);
4904 cur.idx() = tabular.getLastCellInRow(r);
4905 cur.pit() = cur.lastpit();
4906 cur.pos() = cur.lastpos();
4907 cur.selection(true);
4913 if (hitSelectColumn(cur.bv(), cmd.y())) {
4914 col_type c = columnFromX(cur, cmd.x());
4915 cur.idx() = tabular.cellIndex(0, c);
4919 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4920 cur.pit() = cur.lastpit();
4921 cur.pos() = cur.lastpos();
4922 cur.selection(true);
4927 // do not reset cursor/selection if we have selected
4928 // some cells (bug 2715).
4929 if (cmd.button() == mouse_button::button3
4930 && &bvcur.selBegin().inset() == this
4931 && bvcur.selIsMultiCell())
4934 // Let InsetTableCell do it
4935 cell(cur.idx())->dispatch(cur, cmd);
4938 case LFUN_MOUSE_MOTION:
4939 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4940 if (cmd.button() == mouse_button::button1) {
4941 // only accept motions to places not deeper nested than the real anchor
4942 if (!bvcur.realAnchor().hasPart(cur)) {
4946 // select (additional) row
4948 row_type r = rowFromY(cur, cmd.y());
4949 cur.idx() = tabular.getLastCellInRow(r);
4950 // we need to reset the cursor's pit and pos now, as the old ones
4951 // may no longer be valid.
4954 bvcur.setCursor(cur);
4955 bvcur.selection(true);
4958 // select (additional) column
4960 col_type c = columnFromX(cur, cmd.x());
4961 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4962 // we need to reset the cursor's pit and pos now, as the old ones
4963 // may no longer be valid.
4966 bvcur.setCursor(cur);
4967 bvcur.selection(true);
4970 // only update if selection changes
4971 if (bvcur.idx() == cur.idx() &&
4972 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4973 cur.noScreenUpdate();
4974 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4975 bvcur.setCursor(cur);
4976 bvcur.selection(true);
4977 // if this is a multicell selection, we just set the cursor to
4978 // the beginning of the cell's text.
4979 if (bvcur.selIsMultiCell()) {
4980 bvcur.pit() = bvcur.lastpit();
4981 bvcur.pos() = bvcur.lastpos();
4986 case LFUN_MOUSE_RELEASE:
4991 case LFUN_CELL_BACKWARD:
4993 cur.selection(false);
4996 case LFUN_CELL_FORWARD:
4998 cur.selection(false);
5001 case LFUN_CHAR_FORWARD_SELECT:
5002 case LFUN_CHAR_FORWARD:
5003 case LFUN_CHAR_BACKWARD_SELECT:
5004 case LFUN_CHAR_BACKWARD:
5005 case LFUN_CHAR_RIGHT_SELECT:
5006 case LFUN_CHAR_RIGHT:
5007 case LFUN_CHAR_LEFT_SELECT:
5008 case LFUN_CHAR_LEFT:
5009 case LFUN_WORD_FORWARD:
5010 case LFUN_WORD_FORWARD_SELECT:
5011 case LFUN_WORD_BACKWARD:
5012 case LFUN_WORD_BACKWARD_SELECT:
5013 case LFUN_WORD_RIGHT:
5014 case LFUN_WORD_RIGHT_SELECT:
5015 case LFUN_WORD_LEFT:
5016 case LFUN_WORD_LEFT_SELECT: {
5017 // determine whether we move to next or previous cell, where to enter
5018 // the new cell from, and which command to "finish" (i.e., exit the
5021 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
5022 FuncCode finish_lfun;
5024 if (act == LFUN_CHAR_FORWARD
5025 || act == LFUN_CHAR_FORWARD_SELECT
5026 || act == LFUN_WORD_FORWARD
5027 || act == LFUN_WORD_FORWARD_SELECT) {
5029 finish_lfun = LFUN_FINISHED_FORWARD;
5031 else if (act == LFUN_CHAR_BACKWARD
5032 || act == LFUN_CHAR_BACKWARD_SELECT
5033 || act == LFUN_WORD_BACKWARD
5034 || act == LFUN_WORD_BACKWARD_SELECT) {
5036 finish_lfun = LFUN_FINISHED_BACKWARD;
5038 // LEFT or RIGHT commands --- the interpretation will depend on the
5039 // table's direction.
5041 bool const right = act == LFUN_CHAR_RIGHT
5042 || act == LFUN_CHAR_RIGHT_SELECT
5043 || act == LFUN_WORD_RIGHT
5044 || act == LFUN_WORD_RIGHT_SELECT;
5045 next_cell = isRightToLeft(cur) != right;
5047 if (lyxrc.visual_cursor)
5048 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
5050 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
5053 bool const select = act == LFUN_CHAR_FORWARD_SELECT
5054 || act == LFUN_CHAR_BACKWARD_SELECT
5055 || act == LFUN_CHAR_RIGHT_SELECT
5056 || act == LFUN_CHAR_LEFT_SELECT
5057 || act == LFUN_WORD_FORWARD_SELECT
5058 || act == LFUN_WORD_RIGHT_SELECT
5059 || act == LFUN_WORD_BACKWARD_SELECT
5060 || act == LFUN_WORD_LEFT_SELECT;
5062 // If we have a multicell selection or we're
5063 // not doing some LFUN_*_SELECT thing anyway...
5064 if (!cur.selIsMultiCell() || !select) {
5065 col_type const c = tabular.cellColumn(cur.idx());
5066 row_type const r = tabular.cellRow(cur.idx());
5067 // Are we trying to select the whole cell and is the whole cell
5068 // not yet selected?
5069 bool const select_whole = select && !isCellSelected(cur, r, c) &&
5070 ((next_cell && cur.pit() == cur.lastpit()
5071 && cur.pos() == cur.lastpos())
5072 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
5074 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
5076 // ...try to dispatch to the cell's inset.
5077 cell(cur.idx())->dispatch(cur, cmd);
5079 // When we already have a selection we want to select the whole cell
5080 // before going to the next cell.
5081 if (select_whole && !empty_cell){
5082 getText(cur.idx())->selectAll(cur);
5084 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5088 // FIXME: When we support the selection of an empty cell, remove
5089 // the !empty_cell from this condition. For now we jump to the next
5090 // cell if the current cell is empty.
5091 if (cur.result().dispatched() && !empty_cell)
5095 // move to next/prev cell, as appropriate
5096 // note that we will always do this if we're selecting and we have
5097 // a multicell selection
5098 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
5099 << " cell from: " << int(entry_from));
5101 moveNextCell(cur, entry_from);
5103 movePrevCell(cur, entry_from);
5104 // if we're exiting the table, call the appropriate FINISHED lfun
5105 if (sl == cur.top()) {
5106 cmd = FuncRequest(finish_lfun);
5111 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5116 case LFUN_DOWN_SELECT:
5118 if (!(cur.selection() && cur.selIsMultiCell()))
5119 cell(cur.idx())->dispatch(cur, cmd);
5121 cur.dispatched(); // override the cell's decision
5122 if (sl == cur.top()) {
5123 // if our Text didn't do anything to the cursor
5124 // then we try to put the cursor into the cell below
5125 // setting also the right targetX.
5126 cur.selHandle(act == LFUN_DOWN_SELECT);
5127 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
5128 int const xtarget = cur.targetX();
5129 // WARNING: Once cur.idx() has been reset, the cursor is in
5130 // an inconsistent state until pos() has been set. Be careful
5131 // what you do with it!
5132 cur.idx() = tabular.cellBelow(cur.idx());
5134 TextMetrics const & tm =
5135 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5136 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5137 cur.setCurrentFont();
5140 if (sl == cur.top()) {
5141 // we trick it to go to forward after leaving the
5143 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5146 if (cur.selIsMultiCell()) {
5147 cur.pit() = cur.lastpit();
5148 cur.pos() = cur.lastpos();
5149 cur.setCurrentFont();
5150 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5153 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5156 case LFUN_UP_SELECT:
5158 if (!(cur.selection() && cur.selIsMultiCell()))
5159 cell(cur.idx())->dispatch(cur, cmd);
5160 cur.dispatched(); // override the cell's decision
5161 if (sl == cur.top()) {
5162 // if our Text didn't do anything to the cursor
5163 // then we try to put the cursor into the cell above
5164 // setting also the right targetX.
5165 cur.selHandle(act == LFUN_UP_SELECT);
5166 if (tabular.cellRow(cur.idx()) != 0) {
5167 int const xtarget = cur.targetX();
5168 // WARNING: Once cur.idx() has been reset, the cursor is in
5169 // an inconsistent state until pos() has been set. Be careful
5170 // what you do with it!
5171 cur.idx() = tabular.cellAbove(cur.idx());
5172 cur.pit() = cur.lastpit();
5173 Text const * text = cell(cur.idx())->getText(0);
5174 TextMetrics const & tm = cur.bv().textMetrics(text);
5175 ParagraphMetrics const & pm =
5176 tm.parMetrics(cur.lastpit());
5177 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5178 cur.setCurrentFont();
5181 if (sl == cur.top()) {
5182 cmd = FuncRequest(LFUN_UP);
5185 if (cur.selIsMultiCell()) {
5187 cur.pos() = cur.lastpos();
5188 cur.setCurrentFont();
5189 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5192 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5195 case LFUN_LAYOUT_TABULAR:
5196 cur.bv().showDialog("tabular");
5199 case LFUN_INSET_MODIFY:
5200 // we come from the dialog
5201 if (cmd.getArg(0) == "tabular")
5202 tabularFeatures(cur, cmd.getLongArg(1));
5207 case LFUN_TABULAR_FEATURE:
5208 tabularFeatures(cur, to_utf8(cmd.argument()));
5211 // insert file functions
5212 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5213 case LFUN_FILE_INSERT_PLAINTEXT:
5215 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5216 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5217 FileName(to_utf8(cmd.argument())));
5220 cur.recordUndoInset();
5221 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5222 // content has been replaced,
5223 // so cursor might be invalid
5224 cur.pos() = cur.lastpos();
5225 cur.pit() = cur.lastpit();
5226 bvcur.setCursor(cur);
5233 if (cur.selIsMultiCell()) {
5234 if (copySelection(cur)) {
5235 cur.recordUndoInset();
5239 cell(cur.idx())->dispatch(cur, cmd);
5242 case LFUN_SELF_INSERT:
5243 if (cur.selIsMultiCell()) {
5244 cur.recordUndoInset();
5246 BufferView * bv = &cur.bv();
5247 docstring::const_iterator cit = cmd.argument().begin();
5248 docstring::const_iterator const end = cmd.argument().end();
5249 for (; cit != end; ++cit)
5250 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5253 bv->bookmarkEditPosition();
5255 cell(cur.idx())->dispatch(cur, cmd);
5258 case LFUN_CHAR_DELETE_BACKWARD:
5259 case LFUN_CHAR_DELETE_FORWARD:
5260 if (cur.selIsMultiCell()) {
5261 cur.recordUndoInset();
5264 cell(cur.idx())->dispatch(cur, cmd);
5267 case LFUN_CLIPBOARD_PASTE:
5268 case LFUN_PRIMARY_SELECTION_PASTE: {
5269 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5270 theClipboard().getAsText(Clipboard::PlainTextType) :
5271 theSelection().get();
5274 // pass to InsertPlaintextString, but
5275 // only if we have multi-cell content
5276 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5277 cur.recordUndoInset();
5278 if (insertPlaintextString(cur.bv(), clip, false)) {
5279 // content has been replaced,
5280 // so cursor might be invalid
5281 cur.pos() = cur.lastpos();
5282 cur.pit() = cur.lastpit();
5283 bvcur.setCursor(cur);
5287 // Let the cell handle normal text
5288 cell(cur.idx())->dispatch(cur, cmd);
5293 if (!tabularStackDirty()) {
5294 // Check if we have plain text or HTML with rows/columns.
5295 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5296 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5297 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5298 docstring const clip =
5299 theClipboard().getAsText(Clipboard::AnyTextType);
5300 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5301 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5302 doDispatch(cur, ncmd);
5306 else if (theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5307 // This might be tabular data from another LyX instance. Check!
5308 docstring const clip =
5309 theClipboard().getAsText(Clipboard::PlainTextType);
5310 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5311 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5312 doDispatch(cur, ncmd);
5316 if (!cur.selIsMultiCell())
5317 cell(cur.idx())->dispatch(cur, cmd);
5320 if (theClipboard().isInternal()) {
5321 cur.recordUndoInset();
5322 pasteClipboard(cur);
5326 case LFUN_CHANGE_ACCEPT:
5327 case LFUN_CHANGE_REJECT:
5328 case LFUN_FONT_EMPH:
5329 case LFUN_FONT_BOLD:
5330 case LFUN_FONT_BOLDSYMBOL:
5331 case LFUN_FONT_ROMAN:
5332 case LFUN_FONT_NOUN:
5333 case LFUN_FONT_ITAL:
5334 case LFUN_FONT_FRAK:
5335 case LFUN_FONT_TYPEWRITER:
5336 case LFUN_FONT_SANS:
5337 case LFUN_TEXTSTYLE_APPLY:
5338 case LFUN_TEXTSTYLE_UPDATE:
5339 case LFUN_FONT_SIZE:
5340 case LFUN_FONT_UNDERLINE:
5341 case LFUN_FONT_STRIKEOUT:
5342 case LFUN_FONT_CROSSOUT:
5343 case LFUN_FONT_UNDERUNDERLINE:
5344 case LFUN_FONT_UNDERWAVE:
5346 case LFUN_PARAGRAPH_PARAMS_APPLY:
5347 case LFUN_PARAGRAPH_PARAMS:
5348 case LFUN_WORD_CAPITALIZE:
5349 case LFUN_WORD_UPCASE:
5350 case LFUN_WORD_LOWCASE:
5351 case LFUN_CHARS_TRANSPOSE: {
5352 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5353 if (cur.selIsMultiCell()) {
5356 getSelection(cur, rs, re, cs, ce);
5357 Cursor tmpcur = cur;
5358 for (row_type r = rs; r <= re; ++r) {
5359 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5360 // whole row selected
5361 if (act == LFUN_CHANGE_ACCEPT) {
5362 if (tabular.row_info[r].change.inserted())
5363 tabular.row_info[r].change.setUnchanged();
5364 else if (tabular.row_info[r].change.deleted()) {
5365 tabular.deleteRow(r, true);
5370 if (tabular.row_info[r].change.deleted())
5371 tabular.row_info[r].change.setUnchanged();
5372 else if (tabular.row_info[r].change.inserted()) {
5373 tabular.deleteRow(r, true);
5379 for (col_type c = cs; c <= ce; ++c) {
5380 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5381 // whole col selected
5382 if (act == LFUN_CHANGE_ACCEPT) {
5383 if (tabular.column_info[c].change.inserted())
5384 tabular.column_info[c].change.setUnchanged();
5385 else if (tabular.column_info[c].change.deleted()) {
5386 tabular.deleteColumn(c, true);
5391 if (tabular.column_info[c].change.deleted())
5392 tabular.column_info[c].change.setUnchanged();
5393 else if (tabular.column_info[c].change.inserted()) {
5394 tabular.deleteColumn(c, true);
5400 // cursor follows cell:
5401 tmpcur.idx() = tabular.cellIndex(r, c);
5402 // select this cell only:
5405 tmpcur.resetAnchor();
5406 tmpcur.pit() = tmpcur.lastpit();
5407 tmpcur.pos() = tmpcur.top().lastpos();
5408 tmpcur.setCursor(tmpcur);
5409 tmpcur.setSelection();
5410 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5414 tabular.updateIndexes();
5415 // cursor might be invalid
5417 // change bar might need to be redrawn
5418 cur.screenUpdateFlags(Update::Force);
5419 cur.forceBufferUpdate();
5423 cell(cur.idx())->dispatch(cur, cmd);
5428 case LFUN_CHANGE_NEXT:
5429 case LFUN_CHANGE_PREVIOUS: {
5430 // BufferView::dispatch has already moved the cursor, we just
5431 // need to select here if we have a changed row or column
5432 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5434 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5438 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5439 cur.pit() = cur.lastpit();
5440 cur.pos() = cur.lastpos();
5441 cur.selection(true);
5445 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5447 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5451 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5452 cur.pit() = cur.lastpit();
5453 cur.pos() = cur.lastpos();
5454 cur.selection(true);
5461 case LFUN_INSET_SETTINGS:
5462 // relay this lfun to Inset, not to the cell.
5463 Inset::doDispatch(cur, cmd);
5467 // we try to handle this event in the insets dispatch function.
5468 cell(cur.idx())->dispatch(cur, cmd);
5474 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5475 string const & argument, FuncStatus & status) const
5478 int action = Tabular::LAST_ACTION;
5480 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5481 if (tabularFeature[i].feature == s) {
5482 action = tabularFeature[i].action;
5486 if (action == Tabular::LAST_ACTION) {
5488 status.setUnknown(true);
5492 row_type sel_row_start = 0;
5493 row_type sel_row_end = 0;
5494 col_type sel_col_start = 0;
5495 col_type sel_col_end = 0;
5496 Tabular::ltType dummyltt;
5499 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5502 case Tabular::SET_PWIDTH:
5503 case Tabular::SET_MPWIDTH:
5504 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5505 case Tabular::SET_SPECIAL_COLUMN:
5506 case Tabular::SET_SPECIAL_MULTICOLUMN:
5507 case Tabular::APPEND_ROW:
5508 case Tabular::APPEND_COLUMN:
5509 case Tabular::DELETE_ROW:
5510 case Tabular::DELETE_COLUMN:
5511 case Tabular::COPY_ROW:
5512 case Tabular::COPY_COLUMN:
5513 case Tabular::SET_TOP_SPACE:
5514 case Tabular::SET_BOTTOM_SPACE:
5515 case Tabular::SET_INTERLINE_SPACE:
5519 case Tabular::SET_TABULAR_WIDTH:
5520 status.setEnabled(!tabular.rotate
5521 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5524 case Tabular::MOVE_COLUMN_RIGHT:
5525 case Tabular::MOVE_COLUMN_LEFT:
5526 case Tabular::MOVE_ROW_DOWN:
5527 case Tabular::MOVE_ROW_UP: {
5528 if (cur.selection()) {
5529 status.message(_("Selections not supported."));
5530 status.setEnabled(false);
5534 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
5535 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
5536 (action == Tabular::MOVE_COLUMN_LEFT &&
5537 tabular.cellColumn(cur.idx()) == 0) ||
5538 (action == Tabular::MOVE_ROW_DOWN &&
5539 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
5540 (action == Tabular::MOVE_ROW_UP &&
5541 tabular.cellRow(cur.idx()) == 0)) {
5542 status.setEnabled(false);
5546 if (action == Tabular::MOVE_COLUMN_RIGHT ||
5547 action == Tabular::MOVE_COLUMN_LEFT) {
5548 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
5549 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
5550 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
5551 status.message(_("Multi-column in current or"
5552 " destination column."));
5553 status.setEnabled(false);
5558 if (action == Tabular::MOVE_ROW_DOWN ||
5559 action == Tabular::MOVE_ROW_UP) {
5560 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
5561 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
5562 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
5563 status.message(_("Multi-row in current or"
5564 " destination row."));
5565 status.setEnabled(false);
5570 status.setEnabled(true);
5574 case Tabular::SET_DECIMAL_POINT:
5576 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5579 case Tabular::SET_MULTICOLUMN:
5580 case Tabular::UNSET_MULTICOLUMN:
5581 case Tabular::MULTICOLUMN:
5582 // If a row is set as longtable caption, it must not be allowed
5583 // to unset that this row is a multicolumn.
5584 status.setEnabled(sel_row_start == sel_row_end
5585 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5586 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5589 case Tabular::SET_MULTIROW:
5590 case Tabular::UNSET_MULTIROW:
5591 case Tabular::MULTIROW:
5592 // If a row is set as longtable caption, it must not be allowed
5593 // to unset that this row is a multirow.
5594 status.setEnabled(sel_col_start == sel_col_end
5595 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5596 status.setOnOff(tabular.isMultiRow(cur.idx()));
5599 case Tabular::SET_ALL_LINES:
5600 case Tabular::UNSET_ALL_LINES:
5601 case Tabular::SET_INNER_LINES:
5602 case Tabular::SET_BORDER_LINES:
5603 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5606 case Tabular::RESET_FORMAL_DEFAULT:
5607 status.setEnabled(tabular.use_booktabs);
5610 case Tabular::SET_LINE_TOP:
5611 case Tabular::SET_LINE_BOTTOM:
5612 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5615 case Tabular::SET_LINE_LEFT:
5616 case Tabular::SET_LINE_RIGHT:
5617 status.setEnabled(!tabular.use_booktabs
5618 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5621 case Tabular::SET_LTRIM_TOP:
5622 case Tabular::SET_RTRIM_TOP:
5623 status.setEnabled(tabular.use_booktabs
5624 && tabular.cellRow(cur.idx()) != 0
5625 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5628 case Tabular::SET_LTRIM_BOTTOM:
5629 case Tabular::SET_RTRIM_BOTTOM:
5630 status.setEnabled(tabular.use_booktabs
5631 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5632 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5635 case Tabular::TOGGLE_LINE_TOP:
5636 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5637 status.setOnOff(tabular.topLine(cur.idx()));
5640 case Tabular::TOGGLE_LINE_BOTTOM:
5641 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5642 status.setOnOff(tabular.bottomLine(cur.idx()));
5645 case Tabular::TOGGLE_LINE_LEFT:
5646 status.setEnabled(!tabular.use_booktabs
5647 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5648 status.setOnOff(tabular.leftLine(cur.idx()));
5651 case Tabular::TOGGLE_LINE_RIGHT:
5652 status.setEnabled(!tabular.use_booktabs
5653 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5654 status.setOnOff(tabular.rightLine(cur.idx()));
5657 case Tabular::TOGGLE_LTRIM_TOP:
5658 status.setEnabled(tabular.use_booktabs
5659 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5660 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5663 case Tabular::TOGGLE_RTRIM_TOP:
5664 status.setEnabled(tabular.use_booktabs
5665 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5666 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5669 case Tabular::TOGGLE_LTRIM_BOTTOM:
5670 status.setEnabled(tabular.use_booktabs
5671 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5672 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5675 case Tabular::TOGGLE_RTRIM_BOTTOM:
5676 status.setEnabled(tabular.use_booktabs
5677 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5678 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5681 // multirow cells only inherit the alignment of the column if the column has
5682 // no width, otherwise they are left-aligned
5683 // therefore allow always left but right and center only if there is no width
5684 case Tabular::M_ALIGN_LEFT:
5687 case Tabular::ALIGN_LEFT:
5688 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5691 case Tabular::M_ALIGN_RIGHT:
5694 case Tabular::ALIGN_RIGHT:
5695 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5696 && !tabular.getPWidth(cur.idx()).zero()));
5697 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5700 case Tabular::M_ALIGN_CENTER:
5703 case Tabular::ALIGN_CENTER:
5704 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5705 && !tabular.getPWidth(cur.idx()).zero()));
5706 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5709 case Tabular::ALIGN_BLOCK:
5710 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5711 && !tabular.isMultiRow(cur.idx()));
5712 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5715 case Tabular::ALIGN_DECIMAL:
5716 status.setEnabled(!tabular.isMultiRow(cur.idx())
5717 && !tabular.isMultiColumn(cur.idx()));
5718 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5721 case Tabular::M_VALIGN_TOP:
5724 case Tabular::VALIGN_TOP:
5725 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5726 && !tabular.isMultiRow(cur.idx()));
5728 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5731 case Tabular::M_VALIGN_BOTTOM:
5734 case Tabular::VALIGN_BOTTOM:
5735 status.setEnabled(!tabular.isMultiRow(cur.idx()));
5737 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5740 case Tabular::M_VALIGN_MIDDLE:
5743 case Tabular::VALIGN_MIDDLE:
5744 status.setEnabled(!tabular.isMultiRow(cur.idx()));
5746 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5749 case Tabular::SET_LONGTABULAR:
5750 case Tabular::TOGGLE_LONGTABULAR:
5751 // setting as longtable is not allowed when table is inside a float
5752 if (cur.innerInsetOfType(FLOAT_CODE) != nullptr
5753 || cur.innerInsetOfType(WRAP_CODE) != nullptr)
5754 status.setEnabled(false);
5756 status.setEnabled(true);
5757 status.setOnOff(tabular.is_long_tabular);
5760 case Tabular::UNSET_LONGTABULAR:
5761 status.setOnOff(!tabular.is_long_tabular);
5764 case Tabular::TOGGLE_ROTATE_TABULAR:
5765 case Tabular::SET_ROTATE_TABULAR:
5766 status.setOnOff(tabular.rotate != 0);
5769 case Tabular::TABULAR_VALIGN_TOP:
5770 status.setEnabled(tabular.tabular_width.zero());
5771 status.setOnOff(tabular.tabular_valignment
5772 == Tabular::LYX_VALIGN_TOP);
5774 case Tabular::TABULAR_VALIGN_MIDDLE:
5775 status.setEnabled(tabular.tabular_width.zero());
5776 status.setOnOff(tabular.tabular_valignment
5777 == Tabular::LYX_VALIGN_MIDDLE);
5779 case Tabular::TABULAR_VALIGN_BOTTOM:
5780 status.setEnabled(tabular.tabular_width.zero());
5781 status.setOnOff(tabular.tabular_valignment
5782 == Tabular::LYX_VALIGN_BOTTOM);
5785 case Tabular::LONGTABULAR_ALIGN_LEFT:
5786 status.setOnOff(tabular.longtabular_alignment
5787 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5789 case Tabular::LONGTABULAR_ALIGN_CENTER:
5790 status.setOnOff(tabular.longtabular_alignment
5791 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5793 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5794 status.setOnOff(tabular.longtabular_alignment
5795 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5798 case Tabular::UNSET_ROTATE_TABULAR:
5799 status.setOnOff(tabular.rotate == 0);
5802 case Tabular::TOGGLE_ROTATE_CELL:
5803 case Tabular::SET_ROTATE_CELL:
5804 status.setOnOff(!oneCellHasRotationState(false,
5805 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5808 case Tabular::UNSET_ROTATE_CELL:
5809 status.setOnOff(!oneCellHasRotationState(true,
5810 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5813 case Tabular::SET_USEBOX:
5814 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5817 // every row can only be one thing:
5818 // either a footer or header
5819 case Tabular::SET_LTFIRSTHEAD:
5820 status.setEnabled(sel_row_start == sel_row_end);
5821 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5824 case Tabular::UNSET_LTFIRSTHEAD:
5825 status.setEnabled(sel_row_start == sel_row_end);
5826 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5829 case Tabular::SET_LTHEAD:
5830 status.setEnabled(sel_row_start == sel_row_end);
5831 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5834 case Tabular::UNSET_LTHEAD:
5835 status.setEnabled(sel_row_start == sel_row_end);
5836 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5839 case Tabular::SET_LTFOOT:
5840 status.setEnabled(sel_row_start == sel_row_end);
5841 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5844 case Tabular::UNSET_LTFOOT:
5845 status.setEnabled(sel_row_start == sel_row_end);
5846 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5849 case Tabular::SET_LTLASTFOOT:
5850 status.setEnabled(sel_row_start == sel_row_end);
5851 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5854 case Tabular::UNSET_LTLASTFOOT:
5855 status.setEnabled(sel_row_start == sel_row_end);
5856 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5859 case Tabular::SET_LTNEWPAGE:
5860 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5862 case Tabular::UNSET_LTNEWPAGE:
5863 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5866 // only one row in head/firsthead/foot/lasthead can be the caption
5867 // and a multirow cannot be set as caption
5868 case Tabular::SET_LTCAPTION:
5869 status.setEnabled(sel_row_start == sel_row_end
5870 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5871 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5872 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5873 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5874 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5875 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5876 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5877 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5878 && !tabular.isMultiRow(sel_row_start));
5879 status.setOnOff(tabular.ltCaption(sel_row_start));
5882 case Tabular::UNSET_LTCAPTION:
5883 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5886 case Tabular::TOGGLE_LTCAPTION:
5887 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5888 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5889 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5890 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5891 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5892 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5893 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5894 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5895 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5896 status.setOnOff(tabular.ltCaption(sel_row_start));
5899 case Tabular::TOGGLE_BOOKTABS:
5900 case Tabular::SET_BOOKTABS:
5901 status.setOnOff(tabular.use_booktabs);
5904 case Tabular::UNSET_BOOKTABS:
5905 status.setOnOff(!tabular.use_booktabs);
5910 status.setEnabled(false);
5917 // function sets an object as defined in FuncStatus.h:
5918 // states OK, Unknown, Disabled, On, Off.
5919 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5920 FuncStatus & status) const
5922 switch (cmd.action()) {
5923 case LFUN_INSET_MODIFY:
5924 if (cmd.getArg(0) != "tabular")
5926 if (cmd.getArg(1) == "for-dialog") {
5927 // The dialog is asking the status of a command
5928 if (&cur.inset() != this)
5930 string action = cmd.getArg(2);
5931 string arg = cmd.getLongArg(3);
5932 return getFeatureStatus(cur, action, arg, status);
5934 // We always enable the lfun if it is coming from the dialog
5935 // because the dialog makes sure all the settings are valid,
5936 // even though the first argument might not be valid now.
5937 status.setEnabled(true);
5941 case LFUN_TABULAR_FEATURE: {
5942 if (&cur.inset() != this)
5944 string action = cmd.getArg(0);
5945 string arg = cmd.getLongArg(1);
5946 return getFeatureStatus(cur, action, arg, status);
5949 case LFUN_CAPTION_INSERT: {
5950 // caption is only allowed in caption cell of longtable
5951 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5952 status.setEnabled(false);
5955 // only standard caption is allowed
5956 string arg = cmd.getArg(0);
5957 if (!arg.empty() && arg != "Standard") {
5958 status.setEnabled(false);
5961 // check if there is already a caption
5962 bool have_caption = false;
5963 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5964 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5965 ParagraphList::const_iterator pend = itc.paragraphs().end();
5966 for (; pit != pend; ++pit) {
5967 InsetList::const_iterator it = pit->insetList().begin();
5968 InsetList::const_iterator end = pit->insetList().end();
5969 for (; it != end; ++it) {
5970 if (it->inset->lyxCode() == CAPTION_CODE) {
5971 have_caption = true;
5976 status.setEnabled(!have_caption);
5980 // These are only enabled inside tabular
5981 case LFUN_CELL_BACKWARD:
5982 case LFUN_CELL_FORWARD:
5983 status.setEnabled(true);
5986 // disable these with multiple cells selected
5987 case LFUN_INSET_INSERT:
5988 case LFUN_TABULAR_INSERT:
5989 case LFUN_TABULAR_STYLE_INSERT:
5990 case LFUN_FLEX_INSERT:
5991 case LFUN_FLOAT_INSERT:
5992 case LFUN_FLOAT_WIDE_INSERT:
5993 case LFUN_FOOTNOTE_INSERT:
5994 case LFUN_MARGINALNOTE_INSERT:
5995 case LFUN_MATH_INSERT:
5996 case LFUN_MATH_MODE:
5997 case LFUN_MATH_MUTATE:
5998 case LFUN_MATH_DISPLAY:
5999 case LFUN_NOTE_INSERT:
6000 case LFUN_ARGUMENT_INSERT:
6001 case LFUN_BOX_INSERT:
6002 case LFUN_BRANCH_INSERT:
6003 case LFUN_PHANTOM_INSERT:
6004 case LFUN_WRAP_INSERT:
6005 case LFUN_PREVIEW_INSERT:
6006 case LFUN_ERT_INSERT: {
6007 if (cur.selIsMultiCell()) {
6008 status.setEnabled(false);
6011 return cell(cur.idx())->getStatus(cur, cmd, status);
6014 case LFUN_CHANGE_ACCEPT:
6015 case LFUN_CHANGE_REJECT: {
6016 if (cur.selIsMultiCell()) {
6019 getSelection(cur, rs, re, cs, ce);
6020 for (row_type r = rs; r <= re; ++r) {
6021 if (tabular.row_info[r].change.changed()) {
6022 status.setEnabled(true);
6025 for (col_type c = cs; c <= ce; ++c) {
6026 if (tabular.column_info[c].change.changed()) {
6027 status.setEnabled(true);
6033 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
6034 status.setEnabled(true);
6037 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
6038 status.setEnabled(true);
6042 return cell(cur.idx())->getStatus(cur, cmd, status);
6045 // disable in non-fixed-width cells
6046 case LFUN_PARAGRAPH_BREAK:
6047 // multirow does not allow paragraph breaks
6048 if (tabular.isMultiRow(cur.idx())) {
6049 status.setEnabled(false);
6053 case LFUN_NEWLINE_INSERT:
6054 if (tabular.isMultiRow(cur.idx())
6055 && tabular.getPWidth(cur.idx()).zero()) {
6056 status.setEnabled(false);
6059 return cell(cur.idx())->getStatus(cur, cmd, status);
6061 case LFUN_NEWPAGE_INSERT:
6062 status.setEnabled(false);
6066 if (tabularStackDirty() && theClipboard().isInternal()) {
6067 if (cur.selIsMultiCell()) {
6070 getSelection(cur, rs, re, cs, ce);
6071 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
6072 && paste_tabular->nrows() == re - rs + 1)
6073 status.setEnabled(true);
6075 status.setEnabled(false);
6076 status.message(_("Selection size should match clipboard content."));
6079 status.setEnabled(true);
6082 return cell(cur.idx())->getStatus(cur, cmd, status);
6084 case LFUN_INSET_SETTINGS:
6085 // relay this lfun to Inset, not to the cell.
6086 return Inset::getStatus(cur, cmd, status);
6089 // we try to handle this event in the insets dispatch function.
6090 return cell(cur.idx())->getStatus(cur, cmd, status);
6096 Inset::RowFlags InsetTabular::rowFlags() const
6098 if (tabular.is_long_tabular) {
6099 switch (tabular.longtabular_alignment) {
6100 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
6101 return Display | AlignLeft;
6102 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
6104 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
6105 return Display | AlignRight;
6114 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
6116 tabular.latex(os, runparams);
6120 int InsetTabular::plaintext(odocstringstream & os,
6121 OutputParams const & runparams, size_t max_length) const
6123 os << '\n'; // output table on a new line
6124 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
6125 tabular.plaintext(os, runparams, dp, false, 0, max_length);
6126 return PLAINTEXT_NEWLINE;
6130 void InsetTabular::docbook(XMLStream & xs, OutputParams const & runparams) const
6132 tabular.docbook(xs, runparams);
6136 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6138 return tabular.xhtml(xs, rp);
6142 void InsetTabular::validate(LaTeXFeatures & features) const
6144 tabular.validate(features);
6145 features.useInsetLayout(getLayout());
6149 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6151 return tabular.cellInset(idx);
6155 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6157 return tabular.cellInset(idx);
6161 void InsetTabular::cursorPos(BufferView const & bv,
6162 CursorSlice const & sl, bool boundary, int & x, int & y) const
6164 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6166 // y offset correction
6167 y += cellYPos(sl.idx());
6168 y += tabular.textVOffset(sl.idx());
6169 y += tabular.offsetVAlignment();
6171 // x offset correction
6172 x += cellXPos(sl.idx());
6173 x += tabular.textHOffset(sl.idx());
6174 x += ADD_TO_TABULAR_WIDTH;
6178 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6182 Inset const & inset = *tabular.cellInset(cell);
6183 Point o = bv.coordCache().getInsets().xy(&inset);
6184 int const xbeg = o.x_ - tabular.textHOffset(cell);
6185 int const xend = xbeg + tabular.cellWidth(cell);
6186 row_type const row = tabular.cellRow(cell);
6187 int const ybeg = o.y_ - tabular.rowAscent(row)
6188 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6189 int const yend = ybeg + tabular.cellHeight(cell);
6201 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6202 // << " ybeg=" << ybeg << " yend=" << yend
6203 // << " xx=" << xx << " yy=" << yy
6204 // << " dist=" << xx + yy << endl;
6209 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6211 //lyxerr << "InsetTabular::editXY: " << this << endl;
6213 cur.idx() = getNearestCell(cur.bv(), x, y);
6214 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6218 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6220 cur.idx() = getNearestCell(cur.bv(), x, y);
6221 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6225 idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6227 idx_type idx_min = 0;
6228 int dist_min = numeric_limits<int>::max();
6229 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6230 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6231 int const d = dist(bv, i, x, y);
6242 int InsetTabular::cellYPos(idx_type const cell) const
6244 row_type row = tabular.cellRow(cell);
6246 for (row_type r = 0; r < row; ++r)
6247 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6248 + tabular.interRowSpace(r + 1);
6253 int InsetTabular::cellXPos(idx_type const cell) const
6255 col_type col = tabular.cellColumn(cell);
6257 for (col_type c = 0; c < col; ++c)
6258 lx += tabular.column_info[c].width;
6263 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6265 row_type const row = tabular.cellRow(cur.idx());
6266 col_type const col = tabular.cellColumn(cur.idx());
6268 if (isRightToLeft(cur)) {
6269 if (tabular.cellColumn(cur.idx()) == 0) {
6270 if (row == tabular.nrows() - 1)
6272 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6277 cur.idx() = tabular.getLastCellInRow(row - 1);
6279 cur.idx() = tabular.cellIndex(row, col - 1);
6282 if (tabular.isLastCell(cur.idx()))
6284 if (cur.idx() == tabular.getLastCellInRow(row))
6285 cur.idx() = tabular.cellIndex(row + 1, 0);
6287 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6288 cur.idx() = tabular.cellIndex(row, colnextcell);
6292 cur.boundary(false);
6294 if (cur.selIsMultiCell()) {
6295 cur.pit() = cur.lastpit();
6296 cur.pos() = cur.lastpos();
6303 // in visual mode, place cursor at extreme left or right
6305 switch(entry_from) {
6307 case ENTRY_DIRECTION_RIGHT:
6308 cur.posVisToRowExtremity(false /* !left */);
6310 case ENTRY_DIRECTION_LEFT:
6311 cur.posVisToRowExtremity(true /* left */);
6313 case ENTRY_DIRECTION_IGNORE:
6314 // nothing to do in this case
6318 cur.setCurrentFont();
6322 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6324 row_type const row = tabular.cellRow(cur.idx());
6325 col_type const col = tabular.cellColumn(cur.idx());
6327 if (isRightToLeft(cur)) {
6328 if (cur.idx() == tabular.getLastCellInRow(row)) {
6331 cur.idx() = tabular.getFirstCellInRow(row);
6332 cur.idx() = tabular.cellAbove(cur.idx());
6334 if (tabular.isLastCell(cur.idx()))
6336 if (cur.idx() == tabular.getLastCellInRow(row))
6337 cur.idx() = tabular.cellIndex(row + 1, 0);
6339 cur.idx() = tabular.cellIndex(row, col + 1);
6342 if (cur.idx() == 0) // first cell
6345 cur.idx() = tabular.getLastCellInRow(row - 1);
6347 cur.idx() = tabular.cellIndex(row, col - 1);
6350 if (cur.selIsMultiCell()) {
6351 cur.pit() = cur.lastpit();
6352 cur.pos() = cur.lastpos();
6356 cur.pit() = cur.lastpit();
6357 cur.pos() = cur.lastpos();
6359 // in visual mode, place cursor at extreme left or right
6361 switch(entry_from) {
6363 case ENTRY_DIRECTION_RIGHT:
6364 cur.posVisToRowExtremity(false /* !left */);
6366 case ENTRY_DIRECTION_LEFT:
6367 cur.posVisToRowExtremity(true /* left */);
6369 case ENTRY_DIRECTION_IGNORE:
6370 // nothing to do in this case
6374 cur.setCurrentFont();
6378 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6380 cur.recordUndoInset(this);
6382 istringstream is(argument);
6383 // limit the size of strings we read to avoid memory problems
6387 size_t safe_guard = 0;
6392 if (safe_guard > 1000) {
6393 LYXERR0("parameter max count reached!");
6397 Tabular::Feature action = Tabular::LAST_ACTION;
6400 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6401 if (s != tabularFeature[i].feature)
6403 action = tabularFeature[i].action;
6406 if (action == Tabular::LAST_ACTION) {
6407 LYXERR0("Feature not found " << s);
6411 if (tabularFeature[i].need_value)
6413 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6414 tabularFeatures(cur, action, val);
6419 static void checkLongtableSpecial(Tabular::ltType & ltt,
6420 string const & special, bool & flag)
6422 if (special == "dl_above") {
6425 } else if (special == "dl_below") {
6426 ltt.bottomDL = flag;
6428 } else if (special == "empty") {
6438 bool InsetTabular::oneCellHasRotationState(bool rotated,
6439 row_type row_start, row_type row_end,
6440 col_type col_start, col_type col_end) const
6442 for (row_type r = row_start; r <= row_end; ++r)
6443 for (col_type c = col_start; c <= col_end; ++c)
6445 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6448 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6455 void InsetTabular::tabularFeatures(Cursor & cur,
6456 Tabular::Feature feature, string const & value)
6458 col_type sel_col_start;
6459 col_type sel_col_end;
6460 row_type sel_row_start;
6461 row_type sel_row_end;
6462 bool setLines = false;
6463 bool setLinesInnerOnly = false;
6464 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6465 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6469 case Tabular::M_ALIGN_LEFT:
6470 case Tabular::ALIGN_LEFT:
6471 setAlign = LYX_ALIGN_LEFT;
6474 case Tabular::M_ALIGN_RIGHT:
6475 case Tabular::ALIGN_RIGHT:
6476 setAlign = LYX_ALIGN_RIGHT;
6479 case Tabular::M_ALIGN_CENTER:
6480 case Tabular::ALIGN_CENTER:
6481 setAlign = LYX_ALIGN_CENTER;
6484 case Tabular::ALIGN_BLOCK:
6485 setAlign = LYX_ALIGN_BLOCK;
6488 case Tabular::ALIGN_DECIMAL:
6489 setAlign = LYX_ALIGN_DECIMAL;
6492 case Tabular::M_VALIGN_TOP:
6493 case Tabular::VALIGN_TOP:
6494 setVAlign = Tabular::LYX_VALIGN_TOP;
6497 case Tabular::M_VALIGN_BOTTOM:
6498 case Tabular::VALIGN_BOTTOM:
6499 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6502 case Tabular::M_VALIGN_MIDDLE:
6503 case Tabular::VALIGN_MIDDLE:
6504 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6511 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6512 row_type const row = tabular.cellRow(cur.idx());
6513 col_type const column = tabular.cellColumn(cur.idx());
6515 Tabular::ltType ltt;
6519 case Tabular::SET_TABULAR_WIDTH:
6520 tabular.setTabularWidth(Length(value));
6523 case Tabular::SET_PWIDTH: {
6524 Length const len(value);
6525 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6526 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6528 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6529 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6534 case Tabular::SET_MPWIDTH:
6535 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6538 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6539 bool const varwidth = value == "on";
6540 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6541 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6545 case Tabular::SET_MROFFSET:
6546 tabular.setMROffset(cur, cur.idx(), Length(value));
6549 case Tabular::SET_SPECIAL_COLUMN:
6550 case Tabular::SET_SPECIAL_MULTICOLUMN:
6551 if (value == "none")
6552 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6554 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6557 case Tabular::APPEND_ROW:
6558 // append the row into the tabular
6559 tabular.appendRow(row);
6562 case Tabular::APPEND_COLUMN:
6563 // append the column into the tabular
6564 tabular.appendColumn(column);
6565 cur.idx() = tabular.cellIndex(row, column);
6568 case Tabular::DELETE_ROW:
6569 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6570 for (col_type c = 0; c < tabular.ncols(); c++) {
6571 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6572 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6573 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6574 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6578 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6579 tabular.deleteRow(sel_row_start);
6580 if (sel_row_start >= tabular.nrows())
6582 cur.idx() = tabular.cellIndex(sel_row_start, column);
6585 cur.selection(false);
6588 case Tabular::DELETE_COLUMN:
6589 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6590 for (row_type r = 0; r < tabular.nrows(); r++)
6591 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6592 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6595 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6596 for (row_type r = 0; r < tabular.nrows(); r++)
6597 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6598 tabular.leftLine(tabular.cellIndex(r, 0)));
6601 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6602 tabular.deleteColumn(sel_col_start);
6603 if (sel_col_start >= tabular.ncols())
6605 cur.idx() = tabular.cellIndex(row, sel_col_start);
6608 cur.selection(false);
6611 case Tabular::COPY_ROW:
6612 tabular.copyRow(row);
6615 case Tabular::COPY_COLUMN:
6616 tabular.copyColumn(column);
6617 cur.idx() = tabular.cellIndex(row, column);
6620 case Tabular::MOVE_COLUMN_RIGHT:
6621 tabular.moveColumn(column, Tabular::RIGHT);
6622 cur.idx() = tabular.cellIndex(row, column + 1);
6625 case Tabular::MOVE_COLUMN_LEFT:
6626 tabular.moveColumn(column, Tabular::LEFT);
6627 cur.idx() = tabular.cellIndex(row, column - 1);
6630 case Tabular::MOVE_ROW_DOWN:
6631 tabular.moveRow(row, Tabular::DOWN);
6632 cur.idx() = tabular.cellIndex(row + 1, column);
6635 case Tabular::MOVE_ROW_UP:
6636 tabular.moveRow(row, Tabular::UP);
6637 cur.idx() = tabular.cellIndex(row - 1, column);
6640 case Tabular::SET_LINE_TOP:
6641 case Tabular::TOGGLE_LINE_TOP: {
6642 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6643 ? (value == "true") : !tabular.topLine(cur.idx());
6644 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6645 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6646 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6650 case Tabular::SET_LINE_BOTTOM:
6651 case Tabular::TOGGLE_LINE_BOTTOM: {
6652 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6653 ? (value == "true") : !tabular.bottomLine(cur.idx());
6654 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6655 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6656 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6660 case Tabular::SET_LTRIM_TOP:
6661 case Tabular::TOGGLE_LTRIM_TOP: {
6662 bool l = (feature == Tabular::SET_LTRIM_TOP)
6663 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6664 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6665 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6666 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6670 case Tabular::SET_RTRIM_TOP:
6671 case Tabular::TOGGLE_RTRIM_TOP: {
6672 bool l = (feature == Tabular::SET_RTRIM_TOP)
6673 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6674 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6675 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6676 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6680 case Tabular::SET_LTRIM_BOTTOM:
6681 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6682 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6683 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6684 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6685 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6686 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6690 case Tabular::SET_RTRIM_BOTTOM:
6691 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6692 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6693 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6694 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6695 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6696 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6700 case Tabular::SET_LINE_LEFT:
6701 case Tabular::TOGGLE_LINE_LEFT: {
6702 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6703 ? (value == "true") : !tabular.leftLine(cur.idx());
6704 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6705 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6706 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6710 case Tabular::SET_LINE_RIGHT:
6711 case Tabular::TOGGLE_LINE_RIGHT: {
6712 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6713 ? (value == "true") : !tabular.rightLine(cur.idx());
6714 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6715 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6716 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6720 case Tabular::M_ALIGN_LEFT:
6721 case Tabular::M_ALIGN_RIGHT:
6722 case Tabular::M_ALIGN_CENTER:
6723 case Tabular::ALIGN_LEFT:
6724 case Tabular::ALIGN_RIGHT:
6725 case Tabular::ALIGN_CENTER:
6726 case Tabular::ALIGN_BLOCK:
6727 case Tabular::ALIGN_DECIMAL:
6728 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6729 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6730 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6731 !tabular.getPWidth(c).zero());
6734 case Tabular::M_VALIGN_TOP:
6735 case Tabular::M_VALIGN_BOTTOM:
6736 case Tabular::M_VALIGN_MIDDLE:
6739 case Tabular::VALIGN_TOP:
6740 case Tabular::VALIGN_BOTTOM:
6741 case Tabular::VALIGN_MIDDLE:
6742 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6743 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6744 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6747 case Tabular::SET_MULTICOLUMN: {
6748 if (!cur.selection()) {
6749 // just multicol for one single cell
6750 // check whether we are completely in a multicol
6751 if (!tabular.isMultiColumn(cur.idx()))
6752 tabular.setMultiColumn(cur, cur.idx(), 1,
6753 tabular.rightLine(cur.idx()));
6756 // we have a selection so this means we just add all these
6757 // cells to form a multicolumn cell
6758 idx_type const s_start = cur.selBegin().idx();
6759 row_type const col_start = tabular.cellColumn(s_start);
6760 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6761 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6762 tabular.rightLine(cur.selEnd().idx()));
6765 cur.selection(false);
6769 case Tabular::UNSET_MULTICOLUMN: {
6770 if (!cur.selection()) {
6771 if (tabular.isMultiColumn(cur.idx()))
6772 tabular.unsetMultiColumn(cur.idx());
6777 case Tabular::MULTICOLUMN: {
6778 if (!cur.selection()) {
6779 if (tabular.isMultiColumn(cur.idx()))
6780 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6782 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6786 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6787 row_type const r = sel_row_start;
6788 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6789 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6792 // If the selection contains at least one singlecol cell
6793 // or multiple multicol cells,
6794 // we assume the user will merge is to a single multicol
6796 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6798 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6802 case Tabular::SET_MULTIROW: {
6803 if (!cur.selection()) {
6804 // just multirow for one single cell
6805 // check whether we are completely in a multirow
6806 if (!tabular.isMultiRow(cur.idx()))
6807 tabular.setMultiRow(cur, cur.idx(), 1,
6808 tabular.bottomLine(cur.idx()),
6809 tabular.getAlignment(cur.idx()));
6812 // we have a selection so this means we just add all this
6813 // cells to form a multirow cell
6814 idx_type const s_start = cur.selBegin().idx();
6815 row_type const row_start = tabular.cellRow(s_start);
6816 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6817 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6818 tabular.bottomLine(cur.selEnd().idx()),
6819 tabular.getAlignment(cur.selEnd().idx()));
6822 cur.selection(false);
6826 case Tabular::UNSET_MULTIROW: {
6827 if (!cur.selection()) {
6828 if (tabular.isMultiRow(cur.idx()))
6829 tabular.unsetMultiRow(cur.idx());
6834 case Tabular::MULTIROW: {
6835 if (!cur.selection()) {
6836 if (tabular.isMultiRow(cur.idx()))
6837 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6839 tabularFeatures(cur, Tabular::SET_MULTIROW);
6843 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6844 col_type const c = sel_col_start;
6845 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6846 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6849 // If the selection contains at least one singlerow cell
6850 // or multiple multirow cells,
6851 // we assume the user will merge is to a single multirow
6853 tabularFeatures(cur, Tabular::SET_MULTIROW);
6855 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6859 case Tabular::SET_INNER_LINES:
6860 setLinesInnerOnly = true;
6862 case Tabular::SET_ALL_LINES:
6865 case Tabular::UNSET_ALL_LINES:
6866 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6867 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6868 idx_type const cell = tabular.cellIndex(r, c);
6869 if (!setLinesInnerOnly || r != sel_row_start)
6870 tabular.setTopLine(cell, setLines);
6871 if ((!setLinesInnerOnly || r != sel_row_end)
6872 && (!setLines || r == sel_row_end))
6873 tabular.setBottomLine(cell, setLines);
6874 if ((!setLinesInnerOnly || c != sel_col_end)
6875 && (!setLines || c == sel_col_end))
6876 tabular.setRightLine(cell, setLines);
6877 if ((!setLinesInnerOnly || c != sel_col_start))
6878 tabular.setLeftLine(cell, setLines);
6882 case Tabular::RESET_FORMAL_DEFAULT:
6883 for (row_type r = 0; r < tabular.nrows(); ++r) {
6884 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6885 for (col_type c = 0; c < tabular.ncols(); ++c) {
6886 idx_type const cell = tabular.cellIndex(r, c);
6887 tabular.setTopLine(cell, head_or_foot);
6888 tabular.setBottomLine(cell, head_or_foot);
6893 case Tabular::SET_BORDER_LINES:
6894 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6895 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6896 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6898 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6899 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6900 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6904 case Tabular::TOGGLE_LONGTABULAR:
6905 if (tabular.is_long_tabular)
6906 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6908 tabular.is_long_tabular = true;
6911 case Tabular::SET_LONGTABULAR:
6912 tabular.is_long_tabular = true;
6915 case Tabular::UNSET_LONGTABULAR:
6916 for (row_type r = 0; r < tabular.nrows(); ++r) {
6917 if (tabular.ltCaption(r)) {
6918 cur.idx() = tabular.cellIndex(r, 0);
6921 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6924 tabular.is_long_tabular = false;
6927 case Tabular::SET_ROTATE_TABULAR:
6928 tabular.rotate = convert<int>(value);
6931 case Tabular::UNSET_ROTATE_TABULAR:
6935 case Tabular::TOGGLE_ROTATE_TABULAR:
6936 // when pressing the rotate button we default to 90° rotation
6937 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6940 case Tabular::TABULAR_VALIGN_TOP:
6941 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6944 case Tabular::TABULAR_VALIGN_MIDDLE:
6945 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6948 case Tabular::TABULAR_VALIGN_BOTTOM:
6949 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6952 case Tabular::LONGTABULAR_ALIGN_LEFT:
6953 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6956 case Tabular::LONGTABULAR_ALIGN_CENTER:
6957 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6960 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6961 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6964 case Tabular::SET_ROTATE_CELL:
6965 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6966 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6967 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6970 case Tabular::UNSET_ROTATE_CELL:
6971 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6972 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6973 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6976 case Tabular::TOGGLE_ROTATE_CELL:
6978 bool oneNotRotated = oneCellHasRotationState(false,
6979 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6981 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6982 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6983 // when pressing the rotate cell button we default to 90° rotation
6985 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6987 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6992 case Tabular::SET_USEBOX: {
6993 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6994 if (val == tabular.getUsebox(cur.idx()))
6995 val = Tabular::BOX_NONE;
6996 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6997 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6998 tabular.setUsebox(tabular.cellIndex(r, c), val);
7002 case Tabular::UNSET_LTFIRSTHEAD:
7005 case Tabular::SET_LTFIRSTHEAD:
7006 tabular.getRowOfLTFirstHead(row, ltt);
7007 checkLongtableSpecial(ltt, value, flag);
7008 tabular.setLTHead(row, flag, ltt, true);
7011 case Tabular::UNSET_LTHEAD:
7014 case Tabular::SET_LTHEAD:
7015 tabular.getRowOfLTHead(row, ltt);
7016 checkLongtableSpecial(ltt, value, flag);
7017 tabular.setLTHead(row, flag, ltt, false);
7020 case Tabular::UNSET_LTFOOT:
7023 case Tabular::SET_LTFOOT:
7024 tabular.getRowOfLTFoot(row, ltt);
7025 checkLongtableSpecial(ltt, value, flag);
7026 tabular.setLTFoot(row, flag, ltt, false);
7029 case Tabular::UNSET_LTLASTFOOT:
7032 case Tabular::SET_LTLASTFOOT:
7033 tabular.getRowOfLTLastFoot(row, ltt);
7034 checkLongtableSpecial(ltt, value, flag);
7035 tabular.setLTFoot(row, flag, ltt, true);
7038 case Tabular::UNSET_LTNEWPAGE:
7041 case Tabular::SET_LTNEWPAGE:
7042 tabular.setLTNewPage(row, flag);
7045 case Tabular::SET_LTCAPTION: {
7046 if (tabular.ltCaption(row))
7048 cur.idx() = tabular.setLTCaption(cur, row, true);
7051 cur.selection(false);
7052 // If a row is set as caption, then also insert
7053 // a caption. Otherwise the LaTeX output is broken.
7054 // Select cell if it is non-empty
7055 if (cur.lastpos() > 0 || cur.lastpit() > 0)
7056 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
7057 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
7061 case Tabular::UNSET_LTCAPTION: {
7062 if (!tabular.ltCaption(row))
7064 cur.idx() = tabular.setLTCaption(cur, row, false);
7067 cur.selection(false);
7068 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
7069 if (lyx::getStatus(fr).enabled())
7074 case Tabular::TOGGLE_LTCAPTION: {
7075 if (tabular.ltCaption(row))
7076 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
7078 tabularFeatures(cur, Tabular::SET_LTCAPTION);
7082 case Tabular::TOGGLE_BOOKTABS:
7083 tabular.use_booktabs = !tabular.use_booktabs;
7086 case Tabular::SET_BOOKTABS:
7087 tabular.use_booktabs = true;
7090 case Tabular::UNSET_BOOKTABS:
7091 tabular.use_booktabs = false;
7094 case Tabular::SET_TOP_SPACE: {
7096 if (value == "default")
7097 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7098 tabular.row_info[r].top_space_default = true;
7099 else if (value == "none")
7100 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7101 tabular.row_info[r].top_space_default = false;
7102 tabular.row_info[r].top_space = len;
7104 else if (isValidLength(value, &len))
7105 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7106 tabular.row_info[r].top_space_default = false;
7107 tabular.row_info[r].top_space = len;
7112 case Tabular::SET_BOTTOM_SPACE: {
7114 if (value == "default")
7115 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7116 tabular.row_info[r].bottom_space_default = true;
7117 else if (value == "none")
7118 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7119 tabular.row_info[r].bottom_space_default = false;
7120 tabular.row_info[r].bottom_space = len;
7122 else if (isValidLength(value, &len))
7123 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7124 tabular.row_info[r].bottom_space_default = false;
7125 tabular.row_info[r].bottom_space = len;
7130 case Tabular::SET_INTERLINE_SPACE: {
7132 if (value == "default")
7133 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7134 tabular.row_info[r].interline_space_default = true;
7135 else if (value == "none")
7136 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7137 tabular.row_info[r].interline_space_default = false;
7138 tabular.row_info[r].interline_space = len;
7140 else if (isValidLength(value, &len))
7141 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7142 tabular.row_info[r].interline_space_default = false;
7143 tabular.row_info[r].interline_space = len;
7148 case Tabular::SET_DECIMAL_POINT:
7149 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7150 tabular.column_info[c].decimal_point = from_utf8(value);
7153 // dummy stuff just to avoid warnings
7154 case Tabular::LAST_ACTION:
7160 bool InsetTabular::copySelection(Cursor & cur)
7162 if (!cur.selection())
7167 getSelection(cur, rs, re, cs, ce);
7169 paste_tabular.reset(new Tabular(tabular));
7171 for (row_type r = 0; r < rs; ++r)
7172 paste_tabular->deleteRow(0, true);
7174 row_type const rows = re - rs + 1;
7175 while (paste_tabular->nrows() > rows)
7176 paste_tabular->deleteRow(rows, true);
7178 for (col_type c = 0; c < cs; ++c)
7179 paste_tabular->deleteColumn(0, true);
7181 col_type const columns = ce - cs + 1;
7182 while (paste_tabular->ncols() > columns)
7183 paste_tabular->deleteColumn(columns, true);
7185 paste_tabular->setBuffer(tabular.buffer());
7187 odocstringstream os;
7188 OutputParams const runparams(nullptr);
7189 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7190 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7191 cap::copySelection(cur, os.str());
7193 // mark tabular stack dirty
7194 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7195 // when we (hopefully) have a one-for-all paste mechanism.
7196 // This must be called after cap::copySelection.
7197 dirtyTabularStack(true);
7203 bool InsetTabular::pasteClipboard(Cursor & cur)
7207 col_type actcol = tabular.cellColumn(cur.idx());
7208 row_type actrow = tabular.cellRow(cur.idx());
7210 if (cur.selIsMultiCell()) {
7213 getSelection(cur, actrow, re, actcol, ce);
7216 col_type const oldncols = tabular.ncols();
7217 for (row_type r1 = 0, r2 = actrow; r1 < paste_tabular->nrows(); ++r1, ++r2) {
7218 // Append rows if needed
7219 if (r2 == tabular.nrows())
7220 tabular.insertRow(r2 - 1, false);
7221 for (col_type c1 = 0, c2 = actcol; c1 < paste_tabular->ncols(); ++c1, ++c2) {
7222 // Append columns if needed
7223 if (c2 == tabular.ncols())
7224 tabular.insertColumn(c2 - 1, false);
7225 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7226 tabular.isPartOfMultiColumn(r2, c2))
7228 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7232 if (tabular.isPartOfMultiColumn(r2, c2)) {
7236 shared_ptr<InsetTableCell> inset(
7237 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7238 tabular.setCellInset(r2, c2, inset);
7239 // FIXME?: why do we need to do this explicitly? (EL)
7240 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7242 if (!lyxrc.ct_markup_copied) {
7243 // do not paste deleted text
7244 inset->acceptChanges();
7245 inset->setChange(Change(buffer().params().track_changes ?
7246 Change::INSERTED : Change::UNCHANGED));
7252 // amend cursor position if cols have been appended
7253 cur.idx() += actrow * (tabular.ncols() - oldncols);
7258 void InsetTabular::cutSelection(Cursor & cur)
7260 if (!cur.selection())
7265 getSelection(cur, rs, re, cs, ce);
7266 for (row_type r = rs; r <= re; ++r) {
7267 for (col_type c = cs; c <= ce; ++c) {
7268 shared_ptr<InsetTableCell> t
7269 = cell(tabular.cellIndex(r, c));
7270 if (buffer().params().track_changes)
7271 // FIXME: Change tracking (MG)
7272 t->setChange(Change(Change::DELETED));
7278 // cursor position might be invalid now
7279 if (cur.pit() > cur.lastpit())
7280 cur.pit() = cur.lastpit();
7281 if (cur.pos() > cur.lastpos())
7282 cur.pos() = cur.lastpos();
7283 cur.clearSelection();
7287 bool InsetTabular::isRightToLeft(Cursor & cur) const
7289 // LASSERT: It might be better to abandon this Buffer.
7290 LASSERT(cur.depth() > 1, return false);
7291 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7292 pos_type const parentpos = cur[cur.depth() - 2].pos();
7293 return parentpar.getFontSettings(buffer().params(),
7294 parentpos).language()->rightToLeft();
7298 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7301 LASSERT(stidx <= enidx, return docstring());
7303 col_type const col1 = tabular.cellColumn(stidx);
7304 col_type const col2 = tabular.cellColumn(enidx);
7305 row_type const row1 = tabular.cellRow(stidx);
7306 row_type const row2 = tabular.cellRow(enidx);
7308 for (col_type col = col1; col <= col2; col++)
7309 for (row_type row = row1; row <= row2; row++) {
7314 retval += tabular.cellInset(row, col)->asString(intoInsets);
7320 ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
7322 LASSERT(stidx <= enidx, return ParagraphList());
7323 ParagraphList retval;
7324 col_type const col1 = tabular.cellColumn(stidx);
7325 col_type const col2 = tabular.cellColumn(enidx);
7326 row_type const row1 = tabular.cellRow(stidx);
7327 row_type const row2 = tabular.cellRow(enidx);
7328 for (col_type col = col1; col <= col2; col++)
7329 for (row_type row = row1; row <= row2; row++)
7330 for (auto const & par : tabular.cellInset(row, col)->paragraphs())
7331 retval.push_back(par);
7336 void InsetTabular::getSelection(Cursor & cur,
7337 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7339 CursorSlice const & beg = cur.selBegin();
7340 CursorSlice const & end = cur.selEnd();
7341 cs = tabular.cellColumn(beg.idx());
7342 ce = tabular.cellColumn(end.idx());
7346 rs = tabular.cellRow(beg.idx());
7347 re = tabular.cellRow(end.idx());
7353 Text * InsetTabular::getText(int idx) const
7355 return size_t(idx) < nargs() ? cell(idx)->getText(0) : nullptr;
7359 bool InsetTabular::isChanged() const
7361 for (idx_type idx = 0; idx < nargs(); ++idx) {
7362 if (cell(idx)->isChanged())
7364 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7366 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7373 void InsetTabular::setChange(Change const & change)
7375 for (idx_type idx = 0; idx < nargs(); ++idx)
7376 cell(idx)->setChange(change);
7380 void InsetTabular::acceptChanges()
7382 for (idx_type idx = 0; idx < nargs(); ++idx)
7383 cell(idx)->acceptChanges();
7384 for (row_type row = 0; row < tabular.nrows(); ++row) {
7385 if (tabular.row_info[row].change.inserted())
7386 tabular.row_info[row].change.setUnchanged();
7387 else if (tabular.row_info[row].change.deleted())
7388 tabular.deleteRow(row, true);
7390 for (col_type col = 0; col < tabular.ncols(); ++col) {
7391 if (tabular.column_info[col].change.inserted())
7392 tabular.column_info[col].change.setUnchanged();
7393 else if (tabular.column_info[col].change.deleted())
7394 tabular.deleteColumn(col, true);
7396 tabular.updateIndexes();
7400 void InsetTabular::rejectChanges()
7402 for (idx_type idx = 0; idx < nargs(); ++idx)
7403 cell(idx)->rejectChanges();
7404 for (row_type row = 0; row < tabular.nrows(); ++row) {
7405 if (tabular.row_info[row].change.deleted())
7406 tabular.row_info[row].change.setUnchanged();
7407 else if (tabular.row_info[row].change.inserted())
7408 tabular.deleteRow(row, true);
7410 for (col_type col = 0; col < tabular.ncols(); ++col) {
7411 if (tabular.column_info[col].change.deleted())
7412 tabular.column_info[col].change.setUnchanged();
7413 else if (tabular.column_info[col].change.inserted())
7414 tabular.deleteColumn(col, true);
7416 tabular.updateIndexes();
7420 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7422 return tabular.getPWidth(cell).zero();
7426 bool InsetTabular::forcePlainLayout(idx_type cell) const
7428 return tabular.isMultiRow(cell);
7432 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7435 if (buf.length() <= 0)
7440 col_type maxCols = 1;
7441 size_t const len = buf.length();
7445 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7453 maxCols = max(cols, maxCols);
7459 maxCols = max(cols, maxCols);
7465 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7466 loctab = paste_tabular.get();
7467 dirtyTabularStack(true);
7470 cell = bv.cursor().idx();
7471 ocol = tabular.cellColumn(cell);
7472 row = tabular.cellRow(cell);
7476 idx_type cells = loctab->numberofcells;
7479 rows = loctab->nrows();
7480 col_type columns = loctab->ncols();
7483 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7489 // append column if necessary
7490 if (cols == columns) {
7491 loctab->appendColumn(cols - 1);
7492 columns = loctab->ncols();
7493 cells = loctab->numberofcells;
7496 if (cols < columns) {
7497 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7498 Font const font = bv.textMetrics(&inset->text()).
7500 inset->setText(buf.substr(op, p - op), font,
7501 buffer().params().track_changes);
7507 // we can only set this if we are not too far right
7508 if (cols < columns) {
7509 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7510 Font const font = bv.textMetrics(&inset->text()).
7512 inset->setText(buf.substr(op, p - op), font,
7513 buffer().params().track_changes);
7517 // append row if necessary
7518 if (row == rows && p < len - 1) {
7519 loctab->appendRow(row - 1);
7520 rows = loctab->nrows();
7521 cells = loctab->numberofcells;
7524 cell = loctab->cellIndex(row, cols);
7530 // check for the last cell if there is no trailing '\n'
7531 if (cell < cells && op < len) {
7532 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7533 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7534 inset->setText(buf.substr(op, len - op), font,
7535 buffer().params().track_changes);
7541 void InsetTabular::addPreview(DocIterator const & inset_pos,
7542 PreviewLoader & loader) const
7544 DocIterator cell_pos = inset_pos;
7546 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7547 for (row_type r = 0; r < tabular.nrows(); ++r) {
7548 for (col_type c = 0; c < tabular.ncols(); ++c) {
7549 cell_pos.top().idx() = tabular.cellIndex(r, c);
7550 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7556 bool InsetTabular::completionSupported(Cursor const & cur) const
7558 Cursor const & bvCur = cur.bv().cursor();
7559 if (&bvCur.inset() != this)
7561 return cur.text()->completionSupported(cur);
7565 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7567 return completionSupported(cur);
7571 bool InsetTabular::automaticInlineCompletion() const
7573 return lyxrc.completion_inline_text;
7577 bool InsetTabular::automaticPopupCompletion() const
7579 return lyxrc.completion_popup_text;
7583 bool InsetTabular::showCompletionCursor() const
7585 return lyxrc.completion_cursor_text;
7589 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7591 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : nullptr;
7595 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7597 if (!completionSupported(cur))
7599 return cur.text()->completionPrefix(cur);
7603 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7605 if (!completionSupported(cur))
7608 return cur.text()->insertCompletion(cur, s, finished);
7612 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7613 Dimension & dim) const
7615 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7616 tm.completionPosAndDim(cur, x, y, dim);
7620 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7622 istringstream data(in);
7624 lex.setStream(data);
7631 if (!lex || token != "tabular") {
7632 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7636 // This is part of the inset proper that is usually swallowed
7637 // by Buffer::readInset
7639 if (!lex || token != "Tabular") {
7640 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7648 string InsetTabular::params2string(InsetTabular const & inset)
7651 data << "tabular" << ' ';
7653 data << "\\end_inset\n";
7658 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7660 for (col_type c = 0; c < tabular.ncols(); ++c) {
7661 for (row_type r = 0; r < tabular.nrows(); ++r) {
7662 if (!tabular.isPartOfMultiColumn(r,c) &&
7663 !tabular.isPartOfMultiRow(r,c))
7666 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7667 ParagraphList::iterator it = parlist.begin();
7668 ParagraphList::iterator const en = parlist.end();
7669 for (; it != en; ++it)
7670 it->setLayout(dc.plainLayout());