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] = cell_info[row][c];
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_start, col_type col_end,
875 ColDirection direction)
877 if (direction == Tabular::LEFT) {
878 for (col_type col = col_start; col <= col_end; ++col) {
879 std::swap(column_info[col - 1], column_info[col]);
880 for (row_type r = 0; r < nrows(); ++r) {
881 std::swap(cell_info[r][col - 1], cell_info[r][col]);
882 std::swap(cell_info[r][col - 1].left_line, cell_info[r][col].left_line);
883 std::swap(cell_info[r][col - 1].right_line, cell_info[r][col].right_line);
885 if (buffer().params().track_changes) {
886 idx_type const i = cellIndex(r, col - 1);
887 idx_type const j = cellIndex(r, col);
888 cellInfo(i).inset->setChange(Change(Change::INSERTED));
889 cellInfo(j).inset->setChange(Change(Change::INSERTED));
897 for (col_type col = col_end; col >= col_start; --col) {
898 std::swap(column_info[col], column_info[col + 1]);
899 for (row_type r = 0; r < nrows(); ++r) {
900 std::swap(cell_info[r][col], cell_info[r][col + 1]);
901 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
902 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
904 if (buffer().params().track_changes) {
905 idx_type const i = cellIndex(r, col);
906 idx_type const j = cellIndex(r, col + 1);
907 cellInfo(i).inset->setChange(Change(Change::INSERTED));
908 cellInfo(j).inset->setChange(Change(Change::INSERTED));
919 void Tabular::moveRow(row_type row_start, row_type row_end, RowDirection direction)
921 if (direction == Tabular::UP) {
922 for (row_type row = row_start; row <= row_end; ++row) {
923 std::swap(row_info[row - 1], row_info[row]);
924 for (col_type c = 0; c < ncols(); ++c) {
925 std::swap(cell_info[row - 1][c], cell_info[row][c]);
926 std::swap(cell_info[row - 1][c].top_line, cell_info[row][c].top_line);
927 std::swap(cell_info[row - 1][c].bottom_line, cell_info[row][c].bottom_line);
929 idx_type const i = cellIndex(row - 1, c);
930 idx_type const j = cellIndex(row, c);
931 if (buffer().params().track_changes) {
932 cellInfo(i).inset->setChange(Change(Change::INSERTED));
933 cellInfo(j).inset->setChange(Change(Change::INSERTED));
941 for (row_type row = row_end; row >= row_start; --row) {
942 std::swap(row_info[row], row_info[row + 1]);
943 for (col_type c = 0; c < ncols(); ++c) {
944 std::swap(cell_info[row][c], cell_info[row + 1][c]);
945 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
946 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
948 idx_type const i = cellIndex(row, c);
949 idx_type const j = cellIndex(row + 1, c);
950 if (buffer().params().track_changes) {
951 cellInfo(i).inset->setChange(Change(Change::INSERTED));
952 cellInfo(j).inset->setChange(Change(Change::INSERTED));
963 void Tabular::deleteColumn(col_type const col, bool const force)
965 // Not allowed to delete last column
969 bool const ct = force ? false : buffer().params().track_changes;
971 for (row_type r = 0; r < nrows(); ++r) {
972 // Care about multicolumn cells
973 if (col + 1 < ncols() &&
974 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
975 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
976 cell_info[r][col + 1] = cell_info[r][col];
979 cell_info[r].erase(cell_info[r].begin() + col);
982 column_info[col].change.setDeleted();
984 column_info.erase(column_info.begin() + col);
989 void Tabular::copyColumn(col_type const col)
991 insertColumn(col, true);
995 void Tabular::appendColumn(col_type col)
997 insertColumn(col, false);
1001 void Tabular::insertColumn(col_type const col, bool copy)
1003 bool const ct = buffer().params().track_changes;
1004 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
1006 for (row_type r = 0; r < nrows(); ++r) {
1007 cell_info[r].insert(cell_info[r].begin() + col + 1,
1008 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
1009 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
1010 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
1013 for (row_type r = 0; r < nrows(); ++r) {
1014 // inherit line settings
1015 idx_type const i = cellIndex(r, col + 1);
1016 idx_type const j = cellIndex(r, col);
1017 setBottomLine(i, bottomLine(j));
1018 setTopLine(i, topLine(j));
1019 setLeftLine(i, leftLine(j));
1020 setRightLine(i, rightLine(j));
1021 if (rightLine(i) && rightLine(j)) {
1022 setRightLine(j, false);
1026 column_info[col + 1].change.setInserted();
1032 void Tabular::updateIndexes()
1034 setBuffer(buffer());
1036 // reset cell number
1037 for (row_type row = 0; row < nrows(); ++row)
1038 for (col_type column = 0; column < ncols(); ++column) {
1039 if (!isPartOfMultiColumn(row, column)
1040 && !isPartOfMultiRow(row, column))
1042 if (isPartOfMultiRow(row, column))
1043 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1045 cell_info[row][column].cellno = numberofcells - 1;
1048 rowofcell.resize(numberofcells);
1049 columnofcell.resize(numberofcells);
1051 // reset column and row of cells and update their width, alignment and ct status
1052 for (row_type row = 0; row < nrows(); ++row) {
1053 for (col_type column = 0; column < ncols(); ++column) {
1054 if (isPartOfMultiColumn(row, column)) {
1055 cell_info[row][column].inset->toggleMultiCol(true);
1058 cell_info[row][column].inset->toggleMultiCol(false);
1059 // columnofcell needs to be called before setting width and alignment
1060 // multirow cells inherit the width from the column width
1061 if (!isPartOfMultiRow(row, column)) {
1062 columnofcell[i] = column;
1065 setFixedWidth(row, column);
1066 if (isPartOfMultiRow(row, column)) {
1067 cell_info[row][column].inset->toggleMultiRow(true);
1070 cell_info[row][column].inset->toggleMultiRow(false);
1071 cell_info[row][column].inset->setContentAlignment(
1072 getAlignment(cellIndex(row, column)));
1073 if (buffer().params().track_changes) {
1074 if (row_info[row].change.changed())
1075 cell_info[row][column].inset->setChange(row_info[row].change);
1076 if (column_info[column].change.changed())
1077 cell_info[row][column].inset->setChange(column_info[column].change);
1085 idx_type Tabular::numberOfCellsInRow(row_type const row) const
1087 idx_type result = 0;
1088 for (col_type c = 0; c < ncols(); ++c)
1089 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1095 bool Tabular::topLine(idx_type const cell) const
1097 return cellInfo(cell).top_line;
1101 bool Tabular::bottomLine(idx_type const cell) const
1103 return cellInfo(cell).bottom_line;
1107 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1109 if (use_booktabs && !ignore_bt)
1111 return cellInfo(cell).left_line;
1115 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1117 if (use_booktabs && !ignore_bt)
1119 return cellInfo(cell).right_line;
1123 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1126 return make_pair(false, false);
1127 return make_pair(cellInfo(cell).top_line_ltrimmed,
1128 cellInfo(cell).top_line_rtrimmed);
1132 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1135 return make_pair(false, false);
1136 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1137 cellInfo(cell).bottom_line_rtrimmed);
1141 int Tabular::interRowSpace(row_type row) const
1143 if (!row || row >= nrows())
1146 int const interline_space = row_info[row - 1].interline_space_default ?
1147 default_line_space :
1148 row_info[row - 1].interline_space.inPixels(width());
1149 if (rowTopLine(row) && rowBottomLine(row - 1))
1150 return interline_space + WIDTH_OF_LINE;
1151 return interline_space;
1155 int Tabular::interColumnSpace(idx_type cell) const
1157 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1158 if (rightLine(cell) && nextcol < ncols()
1159 && leftLine(cellIndex(cellRow(cell), nextcol)))
1160 return WIDTH_OF_LINE;
1165 int Tabular::cellWidth(idx_type cell) const
1168 col_type const span = columnSpan(cell);
1169 col_type const col = cellColumn(cell);
1170 for(col_type c = col; c < col + span ; ++c)
1171 w += column_info[c].width;
1176 int Tabular::cellHeight(idx_type cell) const
1178 row_type const span = rowSpan(cell);
1179 row_type const row = cellRow(cell);
1181 for(row_type r = row; r < row + span ; ++r) {
1182 h += rowAscent(r) + rowDescent(r);
1183 if (r != row + span - 1)
1184 h += interRowSpace(r + 1);
1191 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1193 vector<int> max_dwidth(ncols(), 0);
1194 // collect max. fixed width of column
1195 map<col_type, int> max_pwidth;
1196 // collect max. variable width of column
1197 map<col_type, int> max_width;
1199 for(col_type c = 0; c < ncols(); ++c)
1200 for(row_type r = 0; r < nrows(); ++r) {
1201 idx_type const i = cellIndex(r, c);
1202 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1203 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1204 if (!getPWidth(i).zero())
1205 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1206 else if (!column_info[c].varwidth)
1207 max_width[c] = max(max_width[c], cell_info[r][c].width);
1210 // If we have a fixed tabular width, we take this into account
1211 Length tab_width = tabular_width;
1212 bool const tabularx = hasVarwidthColumn();
1213 if (tabularx && tab_width.zero())
1214 // If no tabular width is specified with X columns,
1215 // we use 100% colwidth
1216 tab_width = Length(100, Length::PCW);
1218 if (!tab_width.zero()) {
1219 restwidth = mi.base.inPixels(tab_width);
1220 // Subtract the fixed widths from the table width
1221 for (auto const & w : max_pwidth)
1222 restwidth -= w.second;
1225 // If we have a fixed width, distribute the available table width
1226 // (minus the fixed widths) to the variable-width columns
1228 int restcols = ncols() - max_pwidth.size();
1229 if ((restwidth > 0) && (restcols != 0))
1230 vcolwidth = restwidth / restcols;
1232 // Now consider that some variable width columns exceed the vcolwidth
1233 if (vcolwidth > 0) {
1234 bool changed = false;
1235 for (auto const & w : max_width) {
1236 if (tabularx || w.second > vcolwidth) {
1238 restwidth -= w.second;
1242 if (changed && restwidth > 0)
1243 vcolwidth = restwidth / restcols;
1246 bool update = false;
1247 // for each col get max of single col cells
1248 for(col_type c = 0; c < ncols(); ++c) {
1250 for(row_type r = 0; r < nrows(); ++r) {
1251 idx_type const i = cellIndex(r, c);
1252 if (columnSpan(i) == 1) {
1253 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1254 && cell_info[r][c].decimal_width != 0)
1255 new_width = max(new_width, cellInfo(i).width
1256 + max_dwidth[c] - cellInfo(i).decimal_width);
1257 else if (getPWidth(i).zero() && vcolwidth > 0) {
1258 if (tabularx && !column_info[c].varwidth)
1259 new_width = max(new_width, cellInfo(i).width);
1261 new_width = vcolwidth;
1263 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1265 new_width = max(new_width, cellInfo(i).width);
1269 if (column_info[c].width != new_width) {
1270 column_info[c].width = new_width;
1271 // Do not trigger update when no space is left for variable
1272 // columns, as this will loop
1273 update = tab_width.zero() || restwidth > 0;
1276 // update col widths to fit merged cells
1277 for(col_type c = 0; c < ncols(); ++c)
1278 for(row_type r = 0; r < nrows(); ++r) {
1279 idx_type const i = cellIndex(r, c);
1280 int const span = columnSpan(i);
1281 if (span == 1 || c > cellColumn(i))
1285 for(col_type j = c; j < c + span ; ++j)
1286 old_width += column_info[j].width;
1288 if (cellInfo(i).width > old_width) {
1289 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1290 // Do not trigger update when no space is left for variable
1291 // columns, as this will loop
1292 update = tab_width.zero() || restwidth > 0;
1300 int Tabular::width() const
1303 for (col_type c = 0; c < ncols(); ++c)
1304 width += column_info[c].width;
1309 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1312 col_type const col = cellColumn(cell);
1313 // set alignment for the whole row if we are not in a multicolumn cell,
1314 // exclude possible multicolumn cells in the row
1315 if (!isMultiColumn(cell)) {
1316 for (row_type r = 0; r < nrows(); ++r) {
1317 // only if the column has no width the multirow inherits the
1318 // alignment of the column, otherwise it is left aligned
1319 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1320 && !isMultiColumn(cellIndex(r, col))) {
1321 cell_info[r][col].alignment = align;
1322 cell_info[r][col].inset->setContentAlignment(align);
1324 if ((isMultiRow(cellIndex(r, col)) && has_width)
1325 && !isMultiColumn(cellIndex(r, col))) {
1326 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1327 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1330 column_info[col].alignment = align;
1331 docstring & dpoint = column_info[col].decimal_point;
1332 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1333 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1334 dpoint = tlang->decimalSeparator();
1337 cellInfo(cell).alignment = align;
1338 cellInset(cell)->setContentAlignment(align);
1343 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1346 if (!isMultiColumn(cell) || onlycolumn)
1347 column_info[cellColumn(cell)].valignment = align;
1349 cellInfo(cell).valignment = align;
1356 * Allow line and paragraph breaks for fixed width multirow cells
1357 * or disallow them, merge cell paragraphs and reset layout to standard
1358 * for variable width multicol cells.
1360 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1361 bool const fixedWidth, bool const multirow)
1363 inset->toggleFixedWidth(fixedWidth);
1367 BufferParams const & bp = cur.bv().buffer().params();
1370 // undo information has already been recorded
1371 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1372 bp.documentClass().plainLayoutName());
1379 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1380 Length const & width)
1382 col_type const c = cellColumn(cell);
1384 column_info[c].p_width = width;
1385 // reset the vertical alignment to top if the fixed width
1386 // is removed or zero because only fixed width columns can
1387 // have a vertical alignment
1388 if (column_info[c].p_width.zero())
1389 column_info[c].valignment = LYX_VALIGN_TOP;
1390 for (row_type r = 0; r < nrows(); ++r) {
1391 idx_type const cidx = cellIndex(r, c);
1392 // because of multicolumns
1393 toggleFixedWidth(cur, cellInset(cidx).get(),
1394 !getPWidth(cidx).zero(), isMultiRow(cidx));
1395 if (isMultiRow(cidx))
1396 setAlignment(cidx, column_info[c].alignment,
1397 !column_info[c].p_width.zero());
1399 // cur can become invalid after paragraphs were merged
1404 bool Tabular::setFixedWidth(row_type r, col_type c)
1406 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1407 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1408 || (multicol && !cell_info[r][c].p_width.zero());
1409 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1414 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1415 Length const & width)
1417 if (!isMultiColumn(cell))
1420 cellInfo(cell).p_width = width;
1421 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1423 // cur can become invalid after paragraphs were merged
1429 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1431 column_info[cellColumn(cell)].varwidth = varwidth;
1432 cellInset(cell).get()->toggleVarWidth(varwidth);
1437 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1439 cellInfo(cell).mroffset = mroffset;
1444 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1445 Tabular::Feature what)
1447 if (what == SET_SPECIAL_MULTICOLUMN)
1448 cellInfo(cell).align_special = special;
1450 column_info[cellColumn(cell)].align_special = special;
1454 void Tabular::setTopLine(idx_type i, bool line)
1456 cellInfo(i).top_line = line;
1460 void Tabular::setBottomLine(idx_type i, bool line)
1462 cellInfo(i).bottom_line = line;
1466 void Tabular::setTopLineLTrim(idx_type i, bool val)
1468 cellInfo(i).top_line_ltrimmed = val;
1472 void Tabular::setTopLineRTrim(idx_type i, bool val)
1474 cellInfo(i).top_line_rtrimmed = val;
1478 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1480 cellInfo(i).bottom_line_ltrimmed = val;
1484 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1486 cellInfo(i).bottom_line_rtrimmed = val;
1490 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1492 setTopLineLTrim(i, trim.first);
1493 setTopLineRTrim(i, trim.second);
1496 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1498 setBottomLineLTrim(i, trim.first);
1499 setBottomLineRTrim(i, trim.second);
1502 void Tabular::setLeftLine(idx_type cell, bool line)
1504 cellInfo(cell).left_line = line;
1508 void Tabular::setRightLine(idx_type cell, bool line)
1510 cellInfo(cell).right_line = line;
1513 bool Tabular::rowTopLine(row_type r) const
1515 bool all_rows_set = true;
1516 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1517 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1518 return all_rows_set;
1522 bool Tabular::rowBottomLine(row_type r) const
1524 bool all_rows_set = true;
1525 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1526 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1527 return all_rows_set;
1531 bool Tabular::columnLeftLine(col_type c) const
1538 for (row_type r = 0; r < nrows(); ++r) {
1539 idx_type const i = cellIndex(r, c);
1540 if (c == cellColumn(i)) {
1542 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1543 if (cellInfo(i).left_line || right)
1547 return 2 * nrows_left >= total;
1551 bool Tabular::columnRightLine(col_type c) const
1556 int nrows_right = 0;
1558 for (row_type r = 0; r < nrows(); ++r) {
1559 idx_type i = cellIndex(r, c);
1560 if (c == cellColumn(i) + columnSpan(i) - 1) {
1562 bool left = (c + 1 < ncols()
1563 && cellInfo(cellIndex(r, c + 1)).left_line)
1564 || c + 1 == ncols();
1565 if (cellInfo(i).right_line && left)
1569 return 2 * nrows_right >= total;
1573 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1575 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1576 return cellInfo(cell).alignment;
1578 return column_info[cellColumn(cell)].alignment;
1583 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1585 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1586 return cellInfo(cell).valignment;
1587 return column_info[cellColumn(cell)].valignment;
1591 int Tabular::offsetVAlignment() const
1593 // for top-alignment the first horizontal table line must be exactly at
1594 // the position of the base line of the surrounding text line
1595 // for bottom alignment, the same is for the last table line
1596 int offset_valign = 0;
1597 switch (tabular_valignment) {
1598 case Tabular::LYX_VALIGN_BOTTOM:
1599 offset_valign = rowAscent(0) - height();
1601 case Tabular::LYX_VALIGN_MIDDLE:
1602 offset_valign = (- height()) / 2 + rowAscent(0);
1604 case Tabular::LYX_VALIGN_TOP:
1605 offset_valign = rowAscent(0);
1608 return offset_valign;
1612 Length const Tabular::getPWidth(idx_type cell) const
1614 if (isMultiColumn(cell))
1615 return cellInfo(cell).p_width;
1616 return column_info[cellColumn(cell)].p_width;
1620 Length const Tabular::getMROffset(idx_type cell) const
1622 return cellInfo(cell).mroffset;
1626 int Tabular::textHOffset(idx_type cell) const
1628 // the LaTeX Way :-(
1629 int x = WIDTH_OF_LINE;
1631 int const w = cellWidth(cell) - cellInfo(cell).width;
1633 switch (getAlignment(cell)) {
1634 case LYX_ALIGN_CENTER:
1637 case LYX_ALIGN_RIGHT:
1640 case LYX_ALIGN_DECIMAL: {
1641 // we center when no decimal point
1642 if (cellInfo(cell).decimal_width == 0) {
1646 col_type const c = cellColumn(cell);
1647 int max_dhoffset = 0;
1648 for(row_type r = 0; r < row_info.size() ; ++r) {
1649 idx_type const i = cellIndex(r, c);
1650 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1651 && cellInfo(i).decimal_width != 0)
1652 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1654 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1657 // LYX_ALIGN_LEFT: nothing :-)
1665 int Tabular::textVOffset(idx_type cell) const
1667 int voffset = cellInfo(cell).voffset;
1668 if (isMultiRow(cell)) {
1669 row_type const row = cellRow(cell);
1670 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1676 idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1679 idx_type const numcells = numberOfCellsInRow(row);
1680 // we check against numcells to make sure we do not crash if all the
1681 // cells are multirow (bug #7535), but in that case our return value
1682 // is really invalid, i.e., it is NOT the first cell in the row. but
1683 // i do not know what to do here. (rgh)
1684 while (c < numcells - 1
1685 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1686 || (ct && column_info[c].change.deleted())))
1688 return cell_info[row][c].cellno;
1692 idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1694 col_type c = ncols() - 1;
1695 // of course we check against 0 so we don't crash. but we have the same
1696 // problem as in the previous routine: if all the cells are part of a
1697 // multirow or part of a multi column, then our return value is invalid.
1699 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1700 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1701 || (ct && column_info[c].change.deleted())))
1703 return cell_info[row][c].cellno;
1707 row_type Tabular::getFirstRow(bool const ct) const
1712 // exclude deleted rows if ct == true
1713 while (r < nrows() && row_info[r].change.deleted())
1719 row_type Tabular::getLastRow(bool const ct) const
1721 row_type r = nrows() - 1;
1724 // exclude deleted rows if ct == true
1725 while (r > 0 && row_info[r].change.deleted())
1731 row_type Tabular::cellRow(idx_type cell) const
1733 if (cell >= numberofcells)
1737 return rowofcell[cell];
1741 col_type Tabular::cellColumn(idx_type cell) const
1743 if (cell >= numberofcells)
1747 return columnofcell[cell];
1751 void Tabular::write(ostream & os) const
1755 << write_attribute("version", 3)
1756 << write_attribute("rows", nrows())
1757 << write_attribute("columns", ncols())
1759 // global longtable options
1761 << write_attribute("rotate", rotate)
1762 << write_attribute("booktabs", use_booktabs)
1763 << write_attribute("islongtable", is_long_tabular)
1764 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1765 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1766 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1767 << write_attribute("headTopDL", endhead.topDL)
1768 << write_attribute("headBottomDL", endhead.bottomDL)
1769 << write_attribute("footTopDL", endfoot.topDL)
1770 << write_attribute("footBottomDL", endfoot.bottomDL)
1771 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1772 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1773 << write_attribute("lastFootEmpty", endlastfoot.empty);
1774 // longtables cannot be aligned vertically
1775 if (!is_long_tabular)
1776 os << write_attribute("tabularvalignment", tabular_valignment);
1777 os << write_attribute("tabularwidth", tabular_width);
1778 if (is_long_tabular)
1779 os << write_attribute("longtabularalignment", longtabular_alignment);
1781 for (col_type c = 0; c < ncols(); ++c) {
1783 << write_attribute("alignment", column_info[c].alignment);
1784 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1785 os << write_attribute("decimal_point", column_info[c].decimal_point);
1786 os << write_attribute("change", column_info[c].change, buffer().params())
1787 << write_attribute("valignment", column_info[c].valignment)
1788 << write_attribute("width", column_info[c].p_width.asString())
1789 << write_attribute("varwidth", column_info[c].varwidth)
1790 << write_attribute("special", column_info[c].align_special)
1793 for (row_type r = 0; r < nrows(); ++r) {
1794 static const string def("default");
1796 if (row_info[r].top_space_default)
1797 os << write_attribute("topspace", def);
1799 os << write_attribute("topspace", row_info[r].top_space);
1800 if (row_info[r].bottom_space_default)
1801 os << write_attribute("bottomspace", def);
1803 os << write_attribute("bottomspace", row_info[r].bottom_space);
1804 if (row_info[r].interline_space_default)
1805 os << write_attribute("interlinespace", def);
1807 os << write_attribute("interlinespace", row_info[r].interline_space);
1808 os << write_attribute("change", row_info[r].change, buffer().params())
1809 << write_attribute("endhead", row_info[r].endhead)
1810 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1811 << write_attribute("endfoot", row_info[r].endfoot)
1812 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1813 << write_attribute("newpage", row_info[r].newpage)
1814 << write_attribute("caption", row_info[r].caption)
1816 for (col_type c = 0; c < ncols(); ++c) {
1818 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1819 << write_attribute("multirow", cell_info[r][c].multirow)
1820 << write_attribute("mroffset", cell_info[r][c].mroffset)
1821 << write_attribute("alignment", cell_info[r][c].alignment)
1822 << write_attribute("valignment", cell_info[r][c].valignment)
1823 << write_attribute("topline", cell_info[r][c].top_line)
1824 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1825 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1826 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1827 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1828 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1829 << write_attribute("leftline", cell_info[r][c].left_line)
1830 << write_attribute("rightline", cell_info[r][c].right_line)
1831 << write_attribute("rotate", cell_info[r][c].rotate)
1832 << write_attribute("usebox", cell_info[r][c].usebox)
1833 << write_attribute("width", cell_info[r][c].p_width)
1834 << write_attribute("special", cell_info[r][c].align_special)
1836 os << "\\begin_inset ";
1837 cell_info[r][c].inset->write(os);
1838 os << "\n\\end_inset\n"
1840 // FIXME This can be removed again once the mystery
1841 // crash has been resolved.
1846 os << "</lyxtabular>\n";
1850 void Tabular::read(Lexer & lex)
1853 istream & is = lex.getStream();
1855 l_getline(is, line);
1856 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1857 LASSERT(false, return);
1861 if (!getTokenValue(line, "version", version))
1863 LATTEST(version >= 2);
1866 if (!getTokenValue(line, "rows", rows_arg))
1869 if (!getTokenValue(line, "columns", columns_arg))
1871 init(buffer_, rows_arg, columns_arg);
1872 l_getline(is, line);
1873 if (!prefixIs(line, "<features")) {
1874 lyxerr << "Wrong tabular format (expected <features ...> got"
1875 << line << ')' << endl;
1878 getTokenValue(line, "rotate", rotate);
1879 getTokenValue(line, "booktabs", use_booktabs);
1880 getTokenValue(line, "islongtable", is_long_tabular);
1881 getTokenValue(line, "tabularvalignment", tabular_valignment);
1882 getTokenValue(line, "tabularwidth", tabular_width);
1883 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1884 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1885 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1886 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1887 getTokenValue(line, "headTopDL", endhead.topDL);
1888 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1889 getTokenValue(line, "footTopDL", endfoot.topDL);
1890 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1891 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1892 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1893 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1895 for (col_type c = 0; c < ncols(); ++c) {
1897 if (!prefixIs(line,"<column")) {
1898 lyxerr << "Wrong tabular format (expected <column ...> got"
1899 << line << ')' << endl;
1902 getTokenValue(line, "alignment", column_info[c].alignment);
1903 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1904 getTokenValue(line, "valignment", column_info[c].valignment);
1905 getTokenValue(line, "width", column_info[c].p_width);
1906 getTokenValue(line, "special", column_info[c].align_special);
1907 getTokenValue(line, "varwidth", column_info[c].varwidth);
1908 getTokenValue(line, "change", column_info[c].change, buffer().params());
1911 for (row_type i = 0; i < nrows(); ++i) {
1912 l_getline(is, line);
1913 if (!prefixIs(line, "<row")) {
1914 lyxerr << "Wrong tabular format (expected <row ...> got"
1915 << line << ')' << endl;
1918 getTokenValue(line, "topspace", row_info[i].top_space,
1919 row_info[i].top_space_default);
1920 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1921 row_info[i].bottom_space_default);
1922 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1923 row_info[i].interline_space_default);
1924 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1925 getTokenValue(line, "endhead", row_info[i].endhead);
1926 getTokenValue(line, "endfoot", row_info[i].endfoot);
1927 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1928 getTokenValue(line, "newpage", row_info[i].newpage);
1929 getTokenValue(line, "caption", row_info[i].caption);
1930 getTokenValue(line, "change", row_info[i].change, buffer().params());
1931 for (col_type j = 0; j < ncols(); ++j) {
1932 l_getline(is, line);
1933 if (!prefixIs(line, "<cell")) {
1934 lyxerr << "Wrong tabular format (expected <cell ...> got"
1935 << line << ')' << endl;
1938 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1939 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1940 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1941 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1942 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1943 getTokenValue(line, "topline", cell_info[i][j].top_line);
1944 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1945 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1946 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1947 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1948 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1949 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1950 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1951 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1952 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1953 getTokenValue(line, "width", cell_info[i][j].p_width);
1955 getTokenValue(line, "special", cell_info[i][j].align_special);
1956 l_getline(is, line);
1957 if (prefixIs(line, "\\begin_inset")) {
1958 cell_info[i][j].inset->setBuffer(*buffer_);
1959 cell_info[i][j].inset->read(lex);
1960 l_getline(is, line);
1962 if (!prefixIs(line, "</cell>")) {
1963 lyxerr << "Wrong tabular format (expected </cell> got"
1964 << line << ')' << endl;
1968 l_getline(is, line);
1969 if (!prefixIs(line, "</row>")) {
1970 lyxerr << "Wrong tabular format (expected </row> got"
1971 << line << ')' << endl;
1975 while (!prefixIs(line, "</lyxtabular>")) {
1976 l_getline(is, line);
1982 bool Tabular::isMultiColumn(idx_type cell) const
1984 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1985 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1989 bool Tabular::hasMultiColumn(col_type c) const
1991 for (row_type r = 0; r < nrows(); ++r) {
1992 if (isMultiColumn(cellIndex(r, c)))
1999 bool Tabular::hasVarwidthColumn() const
2001 for (col_type c = 0; c < ncols(); ++c) {
2002 if (column_info[c].varwidth)
2009 bool Tabular::isVTypeColumn(col_type c) const
2011 for (row_type r = 0; r < nrows(); ++r) {
2012 idx_type idx = cellIndex(r, c);
2013 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH
2014 && getAlignment(idx) == LYX_ALIGN_LEFT
2015 && getVAlignment(idx) == LYX_VALIGN_TOP)
2022 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
2024 return cell_info[cellRow(cell)][cellColumn(cell)];
2028 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2030 return cell_info[cellRow(cell)][cellColumn(cell)];
2034 idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2035 bool const right_border)
2037 idx_type const col = cellColumn(cell);
2038 idx_type const row = cellRow(cell);
2039 for (idx_type i = 0; i < number; ++i)
2040 unsetMultiRow(cellIndex(row, col + i));
2042 // unsetting of multirow may have invalidated cell index
2043 cell = cellIndex(row, col);
2044 CellData & cs = cellInfo(cell);
2045 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2046 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2047 cs.alignment = column_info[col].alignment;
2048 setRightLine(cell, right_border);
2049 // non-fixed width multicolumns cannot have multiple paragraphs
2050 if (getPWidth(cell).zero()) {
2051 toggleFixedWidth(cur, cellInset(cell).get(),
2052 !getPWidth(cell).zero(), isMultiRow(cell));
2053 // cur can become invalid after paragraphs were merged
2057 idx_type lastcell = cellIndex(row, col + number - 1);
2058 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2059 CellData & cs1 = cellInfo(cell + i);
2060 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2061 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2069 bool Tabular::isMultiRow(idx_type cell) const
2071 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2072 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2075 bool Tabular::hasMultiRow(row_type r) const
2077 for (col_type c = 0; c < ncols(); ++c) {
2078 if (isMultiRow(cellIndex(r, c)))
2084 idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2085 bool const bottom_border,
2086 LyXAlignment const halign)
2088 idx_type const col = cellColumn(cell);
2089 idx_type const row = cellRow(cell);
2090 for (idx_type i = 0; i < number; ++i)
2091 unsetMultiColumn(cellIndex(row + i, col));
2093 // unsetting of multirow may have invalidated cell index
2094 cell = cellIndex(row, col);
2095 CellData & cs = cellInfo(cell);
2096 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2097 cs.valignment = LYX_VALIGN_MIDDLE;
2098 // the horizontal alignment of multirow cells can only
2099 // be changed for the whole table row,
2100 // support changing this only for the multirow cell can be done via
2102 if (getPWidth(cell).zero())
2103 cs.alignment = halign;
2105 cs.alignment = LYX_ALIGN_LEFT;
2107 // Multirows cannot have multiple paragraphs
2108 if (getPWidth(cell).zero()) {
2109 toggleFixedWidth(cur, cellInset(cell).get(),
2110 !getPWidth(cell).zero(),
2112 // cur can become invalid after paragraphs were merged
2116 // set the bottom line of the last selected cell
2117 setBottomLine(cell, bottom_border);
2119 for (idx_type i = 1; i < number; ++i) {
2120 CellData & cs1 = cell_info[row + i][col];
2121 cs1.multirow = CELL_PART_OF_MULTIROW;
2122 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2130 idx_type Tabular::columnSpan(idx_type cell) const
2132 row_type const row = cellRow(cell);
2133 col_type const col = cellColumn(cell);
2135 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2142 idx_type Tabular::rowSpan(idx_type cell) const
2144 col_type const column = cellColumn(cell);
2145 col_type row = cellRow(cell) + 1;
2146 while (row < nrows() && isPartOfMultiRow(row, column))
2149 return row - cellRow(cell);
2153 void Tabular::unsetMultiColumn(idx_type cell)
2155 if (!isMultiColumn(cell))
2158 row_type const row = cellRow(cell);
2159 col_type const col = cellColumn(cell);
2160 row_type const span = columnSpan(cell);
2161 for (col_type c = 0; c < span; ++c) {
2162 // in the table dialog the lines are set in every case
2163 // when unsetting a multicolumn this leads to an additional right
2164 // line for every cell that was part of the former multicolumn cell,
2165 // except if the cell is in the last column
2166 // therefore remove this line
2167 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2168 && (col + c) < (col + span - 1))
2169 cell_info[row][col + c].right_line = false;
2170 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2176 void Tabular::unsetMultiRow(idx_type cell)
2178 if (!isMultiRow(cell))
2181 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2182 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2183 row_type const row = cellRow(cell);
2184 col_type const col = cellColumn(cell);
2185 row_type const span = rowSpan(cell);
2186 for (row_type r = 0; r < span; ++r)
2187 cell_info[row + r][col].multirow = CELL_NORMAL;
2192 void Tabular::setRotateCell(idx_type cell, int value)
2194 cellInfo(cell).rotate = value;
2198 int Tabular::getRotateCell(idx_type cell) const
2200 return cellInfo(cell).rotate;
2204 bool Tabular::needRotating() const
2206 if (rotate && !is_long_tabular)
2208 for (row_type r = 0; r < nrows(); ++r)
2209 for (col_type c = 0; c < ncols(); ++c)
2210 if (cell_info[r][c].rotate != 0)
2216 bool Tabular::isLastCell(idx_type cell) const
2218 return cell + 1 >= numberofcells;
2222 idx_type Tabular::cellAbove(idx_type cell) const
2224 if (cellRow(cell) == 0)
2227 col_type const col = cellColumn(cell);
2228 row_type r = cellRow(cell) - 1;
2229 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2232 return cell_info[r][col].cellno;
2236 idx_type Tabular::cellBelow(idx_type cell) const
2238 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2239 if (nextrow < nrows())
2240 return cell_info[nextrow][cellColumn(cell)].cellno;
2245 idx_type Tabular::cellIndex(row_type row, col_type column) const
2247 LASSERT(column != npos && column < ncols(), column = 0);
2248 LASSERT(row != npos && row < nrows(), row = 0);
2249 return cell_info[row][column].cellno;
2253 void Tabular::setUsebox(idx_type cell, BoxType type)
2255 cellInfo(cell).usebox = type;
2259 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2261 if (getRotateCell(cell) == 0
2262 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2263 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2265 if (cellInfo(cell).usebox > 1)
2266 return cellInfo(cell).usebox;
2267 return useBox(cell);
2272 // This are functions used for the longtable support
2274 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2280 row_info[row].endfirsthead = flag;
2284 row_info[row].endhead = flag;
2289 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2292 hd.set = haveLTHead();
2293 return row_info[row].endhead;
2297 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2300 hd.set = haveLTFirstHead();
2301 return row_info[row].endfirsthead;
2305 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2311 row_info[row].endlastfoot = flag;
2315 row_info[row].endfoot = flag;
2320 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2323 fd.set = haveLTFoot();
2324 return row_info[row].endfoot;
2328 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2331 fd.set = haveLTLastFoot();
2332 return row_info[row].endlastfoot;
2336 void Tabular::setLTNewPage(row_type row, bool what)
2338 row_info[row].newpage = what;
2342 bool Tabular::getLTNewPage(row_type row) const
2344 return row_info[row].newpage;
2348 bool Tabular::haveLTHead(bool withcaptions) const
2350 if (!is_long_tabular)
2352 for (row_type i = 0; i < nrows(); ++i)
2353 if (row_info[i].endhead &&
2354 (withcaptions || !row_info[i].caption))
2360 bool Tabular::haveLTFirstHead(bool withcaptions) const
2362 if (!is_long_tabular || endfirsthead.empty)
2364 for (row_type r = 0; r < nrows(); ++r)
2365 if (row_info[r].endfirsthead &&
2366 (withcaptions || !row_info[r].caption))
2372 bool Tabular::haveLTFoot(bool withcaptions) const
2374 if (!is_long_tabular)
2376 for (row_type r = 0; r < nrows(); ++r)
2377 if (row_info[r].endfoot &&
2378 (withcaptions || !row_info[r].caption))
2384 bool Tabular::haveLTLastFoot(bool withcaptions) const
2386 if (!is_long_tabular || endlastfoot.empty)
2388 for (row_type r = 0; r < nrows(); ++r)
2389 if (row_info[r].endlastfoot &&
2390 (withcaptions || !row_info[r].caption))
2396 idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2398 idx_type i = getFirstCellInRow(row);
2400 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2401 setTopLine(i, false);
2402 setBottomLine(i, false);
2403 setLeftLine(i, false);
2404 setRightLine(i, false);
2405 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2406 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2407 setLTHead(row, true, endfirsthead, true);
2408 row_info[row].endfirsthead = true;
2411 unsetMultiColumn(i);
2412 // When unsetting a caption row, also all existing
2413 // captions in this row must be dissolved.
2415 row_info[row].caption = what;
2420 bool Tabular::ltCaption(row_type row) const
2422 return row_info[row].caption;
2426 bool Tabular::haveLTCaption(CaptionType captiontype) const
2428 if (!is_long_tabular)
2430 for (row_type r = 0; r < nrows(); ++r) {
2431 if (row_info[r].caption) {
2432 switch (captiontype) {
2433 case CAPTION_FIRSTHEAD:
2434 if (row_info[r].endfirsthead)
2438 if (row_info[r].endhead)
2442 if (row_info[r].endfoot)
2445 case CAPTION_LASTFOOT:
2446 if (row_info[r].endlastfoot)
2458 // end longtable support functions
2460 void Tabular::setRowAscent(row_type row, int height)
2462 if (row >= nrows() || row_info[row].ascent == height)
2464 row_info[row].ascent = height;
2468 void Tabular::setRowDescent(row_type row, int height)
2470 if (row >= nrows() || row_info[row].descent == height)
2472 row_info[row].descent = height;
2476 int Tabular::rowAscent(row_type row) const
2478 LASSERT(row < nrows(), row = 0);
2479 return row_info[row].ascent;
2483 int Tabular::rowDescent(row_type row) const
2485 LASSERT(row < nrows(), row = 0);
2486 return row_info[row].descent;
2490 int Tabular::height() const
2493 for (row_type row = 0; row < nrows(); ++row)
2494 height += rowAscent(row) + rowDescent(row) +
2500 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2502 LASSERT(row < nrows(), return false);
2503 LASSERT(column < ncols(), return false);
2504 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2508 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2510 LASSERT(row < nrows(), return false);
2511 LASSERT(column < ncols(), return false);
2512 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2516 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns,
2517 list<col_type> logical_columns) const
2519 // we only output complete row lines and the 1st row here, the rest
2520 // is done in Tabular::TeXBottomHLine(...)
2522 // get for each column the topline (if any)
2523 map<col_type, bool> topline, topltrims, toprtrims;
2525 bool have_trims = false;
2526 for (auto const & c : columns) {
2527 topline[c] = topLine(cellIndex(row, c));
2528 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2529 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2530 // If cell is part of a multirow and not the first cell of the
2531 // multirow, no line must be drawn.
2533 if (isMultiRow(cellIndex(row, c))
2534 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2536 topltrims[c] = false;
2537 toprtrims[c] = false;
2539 // copy trimming to multicolumn parts
2540 if (isPartOfMultiColumn(row, c)) {
2541 topltrims[c] = topltrims[c-1];
2542 toprtrims[c] = toprtrims[c-1];
2544 if (topline.find(c) != topline.end() && topline.find(c)->second)
2546 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2547 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2551 // do nothing if empty first row, or incomplete row line after
2552 row_type first = getFirstRow(!buffer().params().output_changes);
2553 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2556 // Is this the actual first row (excluding longtable caption row)?
2557 bool const realfirstrow = (row == first
2558 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2560 // only output complete row lines and the 1st row's clines
2561 if (nset == columns.size() && !have_trims) {
2563 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2567 } else if (realfirstrow || have_trims) {
2568 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2570 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2571 std::list<col_type>::const_iterator it2 = columns.begin();
2572 // We need to iterate over the logical columns here, but take care for
2574 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2579 if (topline.find(c)->second) {
2580 col_type offset = 0;
2581 for (col_type j = 0 ; j < c; ++j)
2582 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2584 // If the two iterators differ, we are in bidi with swapped columns
2585 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2586 while (isPartOfMultiColumn(row, c))
2589 if (topltrims.find(c) != topltrims.end()
2590 && topltrims.find(c)->second)
2592 col_type cstart = c;
2593 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2594 if (isMultiColumn(cellIndex(row, c))
2595 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2597 if (c > cstart && topltrims.find(c) != topltrims.end()
2598 && topltrims.find(c)->second) {
2599 if (!isPartOfMultiColumn(row, c))
2602 } else if (toprtrims.find(c) != toprtrims.end()
2603 && toprtrims.find(c)->second)
2607 for (col_type j = cstart ; j < c ; ++j)
2608 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2610 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2611 if (toprtrims.find(c) != toprtrims.end()
2612 && toprtrims.find(c)->second)
2617 os << "(" << trim << ")";
2618 if (firstcol > lastcol)
2619 // This can happen with bidi (swapped columns)
2620 os << "{" << lastcol << '-' << firstcol << "}";
2622 os << "{" << firstcol << '-' << lastcol << "}";
2623 if (c == columns.size() - 1)
2633 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns,
2634 list<col_type> logical_columns) const
2636 // we output bottomlines of row r and the toplines of row r+1
2637 // if the latter do not span the whole tabular
2639 // get the bottomlines of row r, and toplines in next row
2640 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2641 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2642 bool nextrowset = true;
2643 for (auto const & c : columns) {
2644 idx_type const idx = cellIndex(row, c);
2645 bottomline[c] = bottomLine(cellIndex(row, c));
2646 bottomltrims[c] = bottomLineTrim(idx).first;
2647 bottomrtrims[c] = bottomLineTrim(idx).second;
2648 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2649 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2650 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2651 // If cell is part of a multirow and not the last cell of the
2652 // multirow, no line must be drawn.
2654 if (isMultiRow(cellIndex(row, c))
2655 && isMultiRow(cellIndex(row + 1, c))
2656 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2657 bottomline[c] = false;
2659 bottomltrims[c] = false;
2660 bottomrtrims[c] = false;
2661 topltrims[c] = false;
2662 toprtrims[c] = false;
2664 // copy trimming in multicolumn parts
2665 if (isPartOfMultiColumn(row, c)) {
2666 topltrims[c] = topltrims[c-1];
2667 toprtrims[c] = toprtrims[c-1];
2668 bottomltrims[c] = bottomltrims[c-1];
2669 bottomrtrims[c] = bottomrtrims[c-1];
2672 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2675 // combine this row's bottom lines and next row's toplines if necessary
2677 bool have_trims = false;
2678 for (auto const & c : columns) {
2680 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2681 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2682 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2683 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2684 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2685 if (bottomline.find(c)->second)
2687 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2688 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2692 // do nothing if empty, OR incomplete row line with a topline in next row
2693 if (nset == 0 || (nextrowset && nset != columns.size()))
2696 if (nset == columns.size() && !have_trims) {
2698 os << (lastrow ? "\\bottomrule" : "\\midrule");
2702 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2704 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2705 std::list<col_type>::const_iterator it2 = columns.begin();
2706 // We need to iterate over the logical columns here, but take care for
2708 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2713 if (bottomline.find(c)->second) {
2714 col_type offset = 0;
2715 for (col_type j = 0 ; j < c; ++j)
2716 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2718 // If the two iterators differ, we are in bidi with swapped columns
2719 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2720 while (isPartOfMultiColumn(row, c))
2723 if (bottomltrims.find(c) != bottomltrims.end()
2724 && bottomltrims.find(c)->second)
2726 col_type cstart = c;
2727 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2728 if (isMultiColumn(cellIndex(row, c))
2730 && isPartOfMultiColumn(row, c + 1))
2733 && bottomltrims.find(c) != bottomltrims.end()
2734 && bottomltrims.find(c)->second) {
2735 if (!isPartOfMultiColumn(row, c))
2738 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2739 && bottomrtrims.find(c)->second)
2743 for (col_type j = cstart ; j < c ; ++j)
2744 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2746 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2747 if (bottomrtrims.find(c) != bottomrtrims.end()
2748 && bottomrtrims.find(c)->second)
2753 os << "(" << trim << ")";
2754 if (firstcol > lastcol)
2755 // This can happen with bidi (swapped columns)
2756 os << "{" << lastcol << '-' << firstcol << "}";
2758 os << "{" << firstcol << '-' << lastcol << "}";
2759 if (c == columns.size() - 1)
2769 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2770 bool & ismulticol, bool & ismultirow,
2771 bool const bidi) const
2773 row_type const r = cellRow(cell);
2774 if (is_long_tabular && row_info[r].caption)
2777 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2778 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2779 // figure out how to set the lines
2780 // we always set double lines to the right of the cell
2781 // or left in bidi RTL, respectively.
2782 col_type const c = cellColumn(cell);
2783 col_type const nextcol = c + columnSpan(cell);
2784 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2785 bool colright = columnRightLine(c);
2786 bool colleft = columnLeftLine(c);
2787 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2788 bool nextcellleft = nextcol < ncols()
2789 && leftLine(cellIndex(r, nextcol));
2790 bool coldouble = colright && nextcolleft;
2791 bool celldouble = rightLine(cell) && nextcellleft;
2793 ismulticol = (isMultiColumn(cell)
2794 || (c == 0 && colleft != leftLine(cell))
2795 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2796 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2797 || (coldouble != celldouble))
2800 // we center in multicol when no decimal point
2802 docstring const align_d = column_info[c].decimal_point;
2803 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2804 bool const nosep = !dit;
2805 ismulticol |= nosep;
2806 celldouble &= nosep;
2809 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2810 int latexcolspan = columnSpan(cell);
2811 for (col_type col = c; col < c + columnSpan(cell); ++col)
2812 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2816 os << "\\multicolumn{" << latexcolspan << "}{";
2817 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2818 || (!bidi && c == 0 && leftLine(cell))))
2820 if (bidi && celldouble)
2821 // add extra vertical line if we want a double one
2823 if (!cellInfo(cell).align_special.empty()) {
2824 os << cellInfo(cell).align_special;
2826 if (!getPWidth(cell).zero()) {
2828 case LYX_ALIGN_LEFT:
2829 os << ">{\\raggedright}";
2831 case LYX_ALIGN_RIGHT:
2832 os << ">{\\raggedleft}";
2834 case LYX_ALIGN_CENTER:
2835 os << ">{\\centering}";
2841 case LYX_VALIGN_TOP:
2844 case LYX_VALIGN_MIDDLE:
2847 case LYX_VALIGN_BOTTOM:
2852 << from_ascii(getPWidth(cell).asLatexString())
2855 if ((getRotateCell(cell) == 0 && useBox(cell) == BOX_VARWIDTH
2856 && align == LYX_ALIGN_LEFT))
2857 os << "V{\\linewidth}";
2860 case LYX_ALIGN_LEFT:
2863 case LYX_ALIGN_RIGHT:
2871 } // end if else !getPWidth
2872 } // end if else !cellinfo_of_cell
2873 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2875 if (!bidi && celldouble)
2876 // add extra vertical line if we want a double one
2879 } // end if ismulticol
2881 // we only need code for the first multirow cell
2882 ismultirow = isMultiRow(cell);
2884 os << "\\multirow{" << rowSpan(cell) << "}{";
2885 if (!getPWidth(cell).zero())
2886 os << from_ascii(getPWidth(cell).asLatexString());
2888 if (column_info[c].varwidth)
2889 // this inherits varwidth size
2892 // we need to set a default value
2896 if (!getMROffset(cell).zero())
2897 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2899 } // end if ismultirow
2901 if (getRotateCell(cell) != 0)
2902 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2904 if (getUsebox(cell) == BOX_PARBOX) {
2907 case LYX_VALIGN_TOP:
2910 case LYX_VALIGN_MIDDLE:
2913 case LYX_VALIGN_BOTTOM:
2917 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2919 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2920 os << "\\begin{minipage}[";
2922 case LYX_VALIGN_TOP:
2925 case LYX_VALIGN_MIDDLE:
2928 case LYX_VALIGN_BOTTOM:
2932 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2934 } else if (getUsebox(cell) == BOX_VARWIDTH
2935 && (getRotateCell(cell) != 0 || align != LYX_ALIGN_LEFT
2936 || valign != LYX_VALIGN_TOP)) {
2937 os << "\\begin{cellvarwidth}[";
2939 case LYX_VALIGN_TOP:
2942 case LYX_VALIGN_MIDDLE:
2945 case LYX_VALIGN_BOTTOM:
2951 case LYX_ALIGN_RIGHT:
2952 os << "\\raggedleft\n";
2954 case LYX_ALIGN_CENTER:
2955 os << "\\centering\n";
2957 case LYX_ALIGN_LEFT:
2958 //os << "\\narrowragged\n";
2960 case LYX_ALIGN_BLOCK:
2961 case LYX_ALIGN_DECIMAL:
2962 case LYX_ALIGN_NONE:
2963 case LYX_ALIGN_LAYOUT:
2964 case LYX_ALIGN_SPECIAL:
2971 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2972 bool ismulticol, bool ismultirow) const
2974 row_type const r = cellRow(cell);
2975 if (is_long_tabular && row_info[r].caption)
2979 if (getUsebox(cell) == BOX_PARBOX)
2981 else if (getUsebox(cell) == BOX_MINIPAGE)
2982 os << breakln << "\\end{minipage}";
2983 else if (getUsebox(cell) == BOX_VARWIDTH
2984 && (getRotateCell(cell) != 0 || getAlignment(cell) != LYX_ALIGN_LEFT
2985 || getVAlignment(cell) != LYX_VALIGN_TOP))
2986 os << breakln << "\\end{cellvarwidth}";
2987 if (getRotateCell(cell) != 0)
2988 os << breakln << "\\end{turn}";
2996 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2997 OutputParams const & runparams,
2998 list<col_type> columns,
2999 list<col_type> logical_columns) const
3001 if (!is_long_tabular)
3005 // output caption which is in no header or footer
3006 if (haveLTCaption()) {
3007 for (row_type r = 0; r < nrows(); ++r) {
3008 if (row_info[r].caption &&
3009 !row_info[r].endfirsthead && !row_info[r].endhead &&
3010 !row_info[r].endfoot && !row_info[r].endlastfoot)
3011 TeXRow(os, r, runparams, columns, logical_columns);
3014 // output first header info
3015 if (haveLTFirstHead()) {
3016 if (endfirsthead.topDL)
3018 for (row_type r = 0; r < nrows(); ++r) {
3019 if (row_info[r].endfirsthead)
3020 TeXRow(os, r, runparams, columns, logical_columns);
3022 if (endfirsthead.bottomDL)
3024 os << "\\endfirsthead\n";
3026 // output header info
3028 if (endfirsthead.empty && !haveLTFirstHead())
3029 os << "\\endfirsthead\n";
3032 for (row_type r = 0; r < nrows(); ++r) {
3033 if (row_info[r].endhead)
3034 TeXRow(os, r, runparams, columns, logical_columns);
3036 if (endhead.bottomDL)
3038 os << "\\endhead\n";
3040 // output footer info
3044 for (row_type r = 0; r < nrows(); ++r) {
3045 if (row_info[r].endfoot)
3046 TeXRow(os, r, runparams, columns, logical_columns);
3048 if (endfoot.bottomDL)
3050 os << "\\endfoot\n";
3051 if (endlastfoot.empty && !haveLTLastFoot())
3052 os << "\\endlastfoot\n";
3054 // output lastfooter info
3055 if (haveLTLastFoot()) {
3056 if (endlastfoot.topDL)
3058 for (row_type r = 0; r < nrows(); ++r) {
3059 if (row_info[r].endlastfoot)
3060 TeXRow(os, r, runparams, columns, logical_columns);
3062 if (endlastfoot.bottomDL)
3064 os << "\\endlastfoot\n";
3069 bool Tabular::isValidRow(row_type row) const
3071 if (!is_long_tabular)
3073 return !row_info[row].endhead && !row_info[row].endfirsthead
3074 && !row_info[row].endfoot && !row_info[row].endlastfoot
3075 && !row_info[row].caption;
3079 void Tabular::TeXRow(otexstream & os, row_type row,
3080 OutputParams const & runparams,
3081 list<col_type> columns, list<col_type> logical_columns) const
3083 //output the top line
3084 TeXTopHLine(os, row, columns, logical_columns);
3086 if (row_info[row].top_space_default) {
3088 os << "\\addlinespace\n";
3090 os << "\\noalign{\\vskip\\doublerulesep}\n";
3091 } else if(!row_info[row].top_space.zero()) {
3093 os << "\\addlinespace["
3094 << from_ascii(row_info[row].top_space.asLatexString())
3097 os << "\\noalign{\\vskip"
3098 << from_ascii(row_info[row].top_space.asLatexString())
3102 bool ismulticol = false;
3103 bool ismultirow = false;
3105 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3106 // Luabibdi (used by LuaTeX) behaves like classic
3107 bool const bidi_rtl =
3108 runparams.local_font->isRightToLeft()
3109 && runparams.useBidiPackage();
3110 bool const ct = !buffer().params().output_changes;
3112 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3114 for (auto const & c : columns) {
3115 if (isPartOfMultiColumn(row, c))
3118 idx_type cell = cellIndex(row, c);
3120 if (isPartOfMultiRow(row, c)
3121 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3122 if (cell != lastcell)
3127 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3128 InsetTableCell const * inset = cellInset(cell);
3130 Paragraph const & par = inset->paragraphs().front();
3132 os.texrow().forceStart(par.id(), 0);
3134 bool rtl = par.isRTL(buffer().params())
3136 && getPWidth(cell).zero()
3137 && !runparams.use_polyglossia;
3141 par.getParLanguage(buffer().params())->lang();
3142 if (lang == "farsi")
3144 else if (lang == "arabic_arabi")
3146 // currently, remaining RTL languages are
3147 // arabic_arabtex and hebrew
3151 // pass to the OutputParams that we are in a cell and
3152 // which alignment we have set.
3153 // InsetNewline needs this context information.
3154 OutputParams newrp(runparams);
3155 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3156 ? OutputParams::PLAIN
3157 : OutputParams::ALIGNED;
3159 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3160 // copy cell and split in 2
3161 InsetTableCell head = InsetTableCell(*cellInset(cell));
3162 head.setBuffer(const_cast<Buffer &>(buffer()));
3163 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3165 dit.push_back(CursorSlice(head));
3166 head.setMacrocontextPositionRecursive(dit);
3167 bool hassep = false;
3168 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3170 tail.setBuffer(head.buffer());
3172 dit.push_back(CursorSlice(tail));
3173 tail.setMacrocontextPositionRecursive(dit);
3177 tail.latex(os, newrp);
3180 head.latex(os, newrp);
3182 head.latex(os, newrp);
3185 tail.latex(os, newrp);
3188 } else if (ltCaption(row)) {
3189 // Inside longtable caption rows, we must only output the caption inset
3190 // with its content and omit anything outside of that (see #10791)
3191 InsetTableCell & tc_inset = *const_cast<InsetTableCell *>(inset);
3192 for (Inset const & it : tc_inset) {
3193 if (it.lyxCode() != CAPTION_CODE)
3195 it.latex(os, runparams);
3198 } else if (!isPartOfMultiRow(row, c)) {
3199 if (!runparams.nice)
3200 os.texrow().start(par.id(), 0);
3201 if (isMultiRow(cell) && !LaTeXFeatures::isAvailable("multirow-2021/01/29"))
3202 newrp.isNonLong = true;
3203 inset->latex(os, newrp);
3206 runparams.encoding = newrp.encoding;
3210 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3211 if (cell != lastcell) { // not last cell in row
3218 os << "\\tabularnewline";
3219 if (row_info[row].bottom_space_default) {
3221 os << "\\addlinespace";
3223 os << "[\\doublerulesep]";
3224 } else if (!row_info[row].bottom_space.zero()) {
3226 os << "\\addlinespace";
3228 << from_ascii(row_info[row].bottom_space.asLatexString())
3233 //output the bottom line
3234 TeXBottomHLine(os, row, columns, logical_columns);
3236 if (row_info[row].interline_space_default) {
3238 os << "\\addlinespace\n";
3240 os << "\\noalign{\\vskip\\doublerulesep}\n";
3241 } else if (!row_info[row].interline_space.zero()) {
3243 os << "\\addlinespace["
3244 << from_ascii(row_info[row].interline_space.asLatexString())
3247 os << "\\noalign{\\vskip"
3248 << from_ascii(row_info[row].interline_space.asLatexString())
3254 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3256 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3257 && !hasVarwidthColumn();
3258 bool const is_xltabular = is_long_tabular
3259 && (hasVarwidthColumn() || !tabular_width.zero());
3260 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3262 //+---------------------------------------------------------------------
3263 //+ first the opening preamble +
3264 //+---------------------------------------------------------------------
3267 if (!TexRow::isNone(pos))
3268 os.texrow().start(pos);
3271 if (is_long_tabular)
3272 os << "\\begin{landscape}\n";
3274 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3277 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3278 // order for RTL (#9686). Thus we use this list.
3279 bool const bidi_rtl =
3280 runparams.local_font->isRightToLeft()
3281 && runparams.useBidiPackage();
3282 list<col_type> columns;
3283 list<col_type> logical_columns;
3284 for (col_type cl = 0; cl < ncols(); ++cl) {
3285 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3288 columns.push_front(cl);
3290 columns.push_back(cl);
3291 // for some calculations, we need the logical (non-swapped)
3292 // columns also in bidi.
3293 logical_columns.push_back(cl);
3296 // If we use \cline or \cmidrule, we need to locally de-activate
3297 // the - character when using languages that activate it (e.g., Czech, Slovak).
3298 bool deactivate_chars = false;
3299 if ((runparams.use_babel || runparams.use_polyglossia)
3300 && contains(runparams.active_chars, '-')) {
3301 bool have_clines = false;
3302 // Check if we use \cline or \cmidrule
3303 for (row_type row = 0; row < nrows(); ++row) {
3304 col_type bset = 0, tset = 0;
3305 for (auto const & c : columns) {
3306 idx_type const idx = cellIndex(row, c);
3307 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3308 || topLineTrim(idx).first || topLineTrim(idx).second) {
3312 if (bottomLine(cellIndex(row, c)))
3314 if (topLine(cellIndex(row, c)))
3317 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3323 deactivate_chars = true;
3324 os << "\\begingroup\n"
3325 << "\\catcode`\\-=12\n";
3329 if (is_long_tabular) {
3331 os << "\\begin{xltabular}";
3333 os << "\\begin{longtable}";
3334 switch (longtabular_alignment) {
3335 case LYX_LONGTABULAR_ALIGN_LEFT:
3338 case LYX_LONGTABULAR_ALIGN_CENTER:
3341 case LYX_LONGTABULAR_ALIGN_RIGHT:
3346 if (tabular_width.zero())
3347 os << "{" << from_ascii("\\columnwidth") << "}";
3349 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3352 if (is_tabular_star)
3353 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3354 else if (hasVarwidthColumn()) {
3355 os << "\\begin{tabularx}{";
3356 if (tabular_width.zero())
3357 os << from_ascii("\\columnwidth") << "}";
3359 os << from_ascii(tabular_width.asLatexString()) << "}";
3361 os << "\\begin{tabular}";
3362 switch (tabular_valignment) {
3363 case LYX_VALIGN_TOP:
3366 case LYX_VALIGN_MIDDLE:
3368 case LYX_VALIGN_BOTTOM:
3376 if (is_tabular_star)
3377 os << "@{\\extracolsep{\\fill}}";
3379 for (auto const & c : columns) {
3380 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3382 if (!column_info[c].align_special.empty()) {
3383 os << column_info[c].align_special;
3385 if (!column_info[c].p_width.zero()) {
3386 bool decimal = false;
3387 switch (column_info[c].alignment) {
3388 case LYX_ALIGN_LEFT:
3389 os << ">{\\raggedright}";
3391 case LYX_ALIGN_RIGHT:
3392 os << ">{\\raggedleft}";
3394 case LYX_ALIGN_CENTER:
3395 os << ">{\\centering}";
3397 case LYX_ALIGN_NONE:
3398 case LYX_ALIGN_BLOCK:
3399 case LYX_ALIGN_LAYOUT:
3400 case LYX_ALIGN_SPECIAL:
3402 case LYX_ALIGN_DECIMAL: {
3404 os << ">{\\raggedright}";
3406 os << ">{\\raggedleft}";
3413 switch (column_info[c].valignment) {
3414 case LYX_VALIGN_TOP:
3415 // this is the default
3417 case LYX_VALIGN_MIDDLE:
3420 case LYX_VALIGN_BOTTOM:
3426 // Fixed-width cells with alignment at decimal separator
3427 // are output as two cells of half the width with the decimal
3428 // separator as column sep. This effectively puts the content
3429 // centered, which differs from the normal decimal sep alignment
3430 // and is not ideal, but we cannot do better ATM (see #9568).
3431 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3433 docstring const halffixedwith =
3434 from_ascii(Length(column_info[c].p_width.value() / 2,
3435 column_info[c].p_width.unit()).asLatexString());
3439 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3446 << from_ascii(column_info[c].p_width.asLatexString())
3448 } else if (column_info[c].varwidth) {
3449 switch (column_info[c].alignment) {
3450 case LYX_ALIGN_LEFT:
3451 os << ">{\\raggedright\\arraybackslash}";
3453 case LYX_ALIGN_RIGHT:
3454 os << ">{\\raggedleft\\arraybackslash}";
3456 case LYX_ALIGN_CENTER:
3457 os << ">{\\centering\\arraybackslash}";
3459 case LYX_ALIGN_NONE:
3460 case LYX_ALIGN_BLOCK:
3461 case LYX_ALIGN_LAYOUT:
3462 case LYX_ALIGN_SPECIAL:
3463 case LYX_ALIGN_DECIMAL:
3467 } else if (isVTypeColumn(c))
3468 os << "V{\\linewidth}";
3470 switch (column_info[c].alignment) {
3471 case LYX_ALIGN_LEFT:
3474 case LYX_ALIGN_RIGHT:
3477 case LYX_ALIGN_DECIMAL:
3478 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3484 } // end if else !column_info[i].p_width
3485 } // end if else !column_info[i].align_special
3486 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3491 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3493 //+---------------------------------------------------------------------
3494 //+ the single row and columns (cells) +
3495 //+---------------------------------------------------------------------
3497 for (row_type r = 0; r < nrows(); ++r) {
3498 if (!buffer().params().output_changes && row_info[r].change.deleted())
3500 if (isValidRow(r)) {
3501 TeXRow(os, r, runparams, columns, logical_columns);
3502 if (is_long_tabular && row_info[r].newpage)
3503 os << "\\newpage\n";
3507 //+---------------------------------------------------------------------
3508 //+ the closing of the tabular +
3509 //+---------------------------------------------------------------------
3511 if (is_long_tabular) {
3513 os << "\\end{xltabular}";
3515 os << "\\end{longtable}";
3517 if (is_tabular_star)
3518 os << "\\end{tabular*}";
3519 else if (hasVarwidthColumn())
3520 os << "\\end{tabularx}";
3522 os << "\\end{tabular}";
3525 if (deactivate_chars)
3527 os << "\n\\endgroup\n";
3530 if (is_long_tabular)
3531 os << breakln << "\\end{landscape}";
3533 os << breakln << "\\end{turn}";
3536 if (!TexRow::isNone(pos))
3537 os.texrow().start(pos);
3541 void Tabular::docbookRow(XMLStream & xs, row_type row,
3542 OutputParams const & runparams, bool header) const
3544 switch (buffer().params().docbook_table_output) {
3545 case BufferParams::HTMLTable:
3546 docbookRowAsHTML(xs, row, runparams, header);
3548 case BufferParams::CALSTable:
3549 docbookRowAsCALS(xs, row, runparams);
3555 void Tabular::docbookRowAsHTML(XMLStream & xs, row_type row,
3556 OutputParams const & runparams, bool header) const
3558 string const celltag = header ? "th" : "td";
3559 idx_type cell = getFirstCellInRow(row);
3561 xs << xml::StartTag("tr");
3563 for (col_type c = 0; c < ncols(); ++c) {
3564 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3569 Length const cwidth = column_info[c].p_width;
3570 if (!cwidth.zero()) {
3571 string const hwidth = cwidth.asHTMLString();
3572 attr << "style =\"width: " << hwidth << ";\" ";
3576 switch (getAlignment(cell)) {
3577 case LYX_ALIGN_BLOCK:
3580 case LYX_ALIGN_DECIMAL: {
3581 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3582 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3585 case LYX_ALIGN_LEFT:
3588 case LYX_ALIGN_RIGHT:
3596 attr << " valign='";
3597 switch (getVAlignment(cell)) {
3598 case LYX_VALIGN_TOP:
3601 case LYX_VALIGN_BOTTOM:
3604 case LYX_VALIGN_MIDDLE:
3609 if (isMultiColumn(cell))
3610 attr << " colspan='" << columnSpan(cell) << "'";
3611 else if (isMultiRow(cell))
3612 attr << " rowspan='" << rowSpan(cell) << "'";
3614 xs << xml::StartTag(celltag, attr.str(), true);
3615 cellInset(cell)->docbook(xs, runparams);
3616 xs << xml::EndTag(celltag);
3620 xs << xml::EndTag("tr");
3625 void Tabular::docbookRowAsCALS(XMLStream & xs, row_type row,
3626 OutputParams const & runparams) const
3628 idx_type cell = getFirstCellInRow(row);
3630 xs << xml::StartTag("row");
3632 for (col_type c = 0; c < ncols(); ++c) {
3633 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3639 switch (getAlignment(cell)) {
3640 case LYX_ALIGN_BLOCK:
3643 case LYX_ALIGN_DECIMAL: {
3644 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3645 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3648 case LYX_ALIGN_LEFT:
3651 case LYX_ALIGN_RIGHT:
3660 attr << " valign='";
3661 switch (getVAlignment(cell)) {
3662 case LYX_VALIGN_TOP:
3665 case LYX_VALIGN_BOTTOM:
3668 case LYX_VALIGN_MIDDLE:
3673 if (isMultiColumn(cell))
3674 attr << " colspan='" << columnSpan(cell) << "'";
3675 else if (isMultiRow(cell))
3676 attr << " rowspan='" << rowSpan(cell) << "'";
3678 attr << " colname='c" << (c + 1) << "'"; // Column numbering starts at 1.
3680 // All cases where there should be a line *below* this row.
3681 if (row_info[row].bottom_space_default)
3682 attr << " rowsep='1'";
3684 xs << xml::StartTag("entry", attr.str(), true);
3685 cellInset(cell)->docbook(xs, runparams);
3686 xs << xml::EndTag("entry");
3690 xs << xml::EndTag("row");
3695 void Tabular::docbook(XMLStream & xs, OutputParams const & runparams) const
3699 // Some tables are inline. Likely limitation: cannot output a table within a table; is that really a limitation?
3700 if (!runparams.docbook_in_table) { // Check on the *outer* set of parameters, so that the table can be closed
3701 // properly at the end of this function.
3702 xs << xml::StartTag("informaltable");
3706 // "Formal" tables have a title and use the tag <table>; the distinction with <informaltable> is done outside.
3707 // HTML has the caption first with titles forbidden, and CALS has a title first.
3708 if (haveLTCaption()) {
3709 std::string tag = ((buffer().params().docbook_table_output) == BufferParams::HTMLTable) ? "caption" : "title";
3711 xs << xml::StartTag(tag);
3712 for (row_type r = 0; r < nrows(); ++r)
3713 if (row_info[r].caption)
3714 docbookRow(xs, r, runparams);
3715 xs << xml::EndTag(tag);
3719 // CALS header: describe all columns in this table. For names, take 'c' then the ID of the column.
3720 // Start at one, as is customary with CALS!
3721 if (buffer().params().docbook_table_output == BufferParams::CALSTable) {
3722 for (col_type c = 0; c < ncols(); ++c) {
3723 std::stringstream attr;
3724 attr << "colnum='" << (c + 1) << "' ";
3725 attr << "colname='c" << (c + 1) << "' ";
3726 Length const cwidth = column_info[c].p_width;
3728 attr << "colwidth='" << cwidth.asHTMLString() << "' ";
3729 attr << "rowheader='norowheader'"; // Last attribute, hence no space at the end.
3731 xs << xml::CompTag("colspec", attr.str());
3736 // Output the header of the table. For both HTML and CALS, this is surrounded by a thead.
3737 bool const havefirsthead = haveLTFirstHead(false);
3738 // if we have a first head, then we are going to ignore the
3739 // headers for the additional pages, since there aren't any
3740 // in DocBook. this test accomplishes that.
3741 bool const havehead = !havefirsthead && haveLTHead(false);
3742 if (havehead || havefirsthead) {
3743 xs << xml::StartTag("thead") << xml::CR();
3744 for (row_type r = 0; r < nrows(); ++r) {
3745 if (((havefirsthead && row_info[r].endfirsthead) ||
3746 (havehead && row_info[r].endhead)) &&
3747 !row_info[r].caption) {
3748 docbookRow(xs, r, runparams, true); // TODO: HTML vs CALS
3751 xs << xml::EndTag("thead");
3755 // Output the footer of the table. For both HTML and CALS, this is surrounded by a tfoot and output just after
3756 // the header (and before the body).
3757 bool const havelastfoot = haveLTLastFoot(false);
3759 bool const havefoot = !havelastfoot && haveLTFoot(false);
3760 if (havefoot || havelastfoot) {
3761 xs << xml::StartTag("tfoot") << xml::CR();
3762 for (row_type r = 0; r < nrows(); ++r) {
3763 if (((havelastfoot && row_info[r].endlastfoot) ||
3764 (havefoot && row_info[r].endfoot)) &&
3765 !row_info[r].caption) {
3766 docbookRow(xs, r, runparams); // TODO: HTML vs CALS
3769 xs << xml::EndTag("tfoot");
3773 // Output the main part of the table. The tbody container is mandatory for CALS, but optional for HTML (only if
3774 // there is no header and no footer). It never hurts to have it, though.
3775 xs << xml::StartTag("tbody");
3777 for (row_type r = 0; r < nrows(); ++r)
3779 docbookRow(xs, r, runparams);
3780 xs << xml::EndTag("tbody");
3783 // If this method started the table tag, also make it close it.
3784 if (!runparams.docbook_in_table) {
3785 xs << xml::EndTag("informaltable");
3791 docstring Tabular::xhtmlRow(XMLStream & xs, row_type row,
3792 OutputParams const & runparams, bool header) const
3795 string const celltag = header ? "th" : "td";
3796 idx_type cell = getFirstCellInRow(row);
3798 xs << xml::StartTag("tr");
3799 for (col_type c = 0; c < ncols(); ++c) {
3800 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3805 Length const cwidth = column_info[c].p_width;
3806 if (!cwidth.zero()) {
3807 string const hwidth = cwidth.asHTMLString();
3808 attr << "style =\"width: " << hwidth << ";\" ";
3812 switch (getAlignment(cell)) {
3813 case LYX_ALIGN_LEFT:
3816 case LYX_ALIGN_RIGHT:
3824 attr << " valign='";
3825 switch (getVAlignment(cell)) {
3826 case LYX_VALIGN_TOP:
3829 case LYX_VALIGN_BOTTOM:
3832 case LYX_VALIGN_MIDDLE:
3837 if (isMultiColumn(cell))
3838 attr << " colspan='" << columnSpan(cell) << "'";
3839 else if (isMultiRow(cell))
3840 attr << " rowspan='" << rowSpan(cell) << "'";
3842 xs << xml::StartTag(celltag, attr.str(), true) << xml::CR();
3843 ret += cellInset(cell)->xhtml(xs, runparams);
3844 xs << xml::EndTag(celltag) << xml::CR();
3847 xs << xml::EndTag("tr");
3852 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3856 if (is_long_tabular) {
3857 // we'll wrap it in a div, so as to deal with alignment
3859 switch (longtabular_alignment) {
3860 case LYX_LONGTABULAR_ALIGN_LEFT:
3863 case LYX_LONGTABULAR_ALIGN_CENTER:
3866 case LYX_LONGTABULAR_ALIGN_RIGHT:
3870 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
3872 // The caption flag wins over head/foot
3873 if (haveLTCaption()) {
3874 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
3876 for (row_type r = 0; r < nrows(); ++r)
3877 if (row_info[r].caption)
3878 ret += xhtmlRow(xs, r, runparams);
3879 xs << xml::EndTag("div");
3884 xs << xml::StartTag("table");
3887 // output header info
3888 bool const havefirsthead = haveLTFirstHead(false);
3889 // if we have a first head, then we are going to ignore the
3890 // headers for the additional pages, since there aren't any
3891 // in XHTML. this test accomplishes that.
3892 bool const havehead = !havefirsthead && haveLTHead(false);
3893 if (havehead || havefirsthead) {
3894 xs << xml::StartTag("thead");
3896 for (row_type r = 0; r < nrows(); ++r) {
3897 if (((havefirsthead && row_info[r].endfirsthead) ||
3898 (havehead && row_info[r].endhead)) &&
3899 !row_info[r].caption) {
3900 ret += xhtmlRow(xs, r, runparams, true);
3903 xs << xml::EndTag("thead");
3906 // output footer info
3907 bool const havelastfoot = haveLTLastFoot(false);
3909 bool const havefoot = !havelastfoot && haveLTFoot(false);
3910 if (havefoot || havelastfoot) {
3911 xs << xml::StartTag("tfoot") << xml::CR();
3912 for (row_type r = 0; r < nrows(); ++r) {
3913 if (((havelastfoot && row_info[r].endlastfoot) ||
3914 (havefoot && row_info[r].endfoot)) &&
3915 !row_info[r].caption) {
3916 ret += xhtmlRow(xs, r, runparams);
3919 xs << xml::EndTag("tfoot");
3923 xs << xml::StartTag("tbody") << xml::CR();
3924 for (row_type r = 0; r < nrows(); ++r)
3926 ret += xhtmlRow(xs, r, runparams);
3927 xs << xml::EndTag("tbody");
3929 xs << xml::EndTag("table");
3931 if (is_long_tabular) {
3932 xs << xml::EndTag("div");
3939 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3940 vector<unsigned int> const & clen) const
3942 idx_type const fcell = getFirstCellInRow(row);
3943 idx_type const n = numberOfCellsInRow(row) + fcell;
3946 for (idx_type i = fcell; i < n; ++i) {
3956 for (idx_type i = fcell; i < n; ++i) {
3967 col_type column = cellColumn(i);
3968 int len = clen[column];
3969 while (column < ncols() - 1
3970 && isPartOfMultiColumn(row, ++column))
3971 len += clen[column] + 4;
3972 os << docstring(len, ch);
3987 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3988 vector<unsigned int> const & clen) const
3990 idx_type const fcell = getFirstCellInRow(row);
3991 idx_type const n = numberOfCellsInRow(row) + fcell;
3994 for (idx_type i = fcell; i < n; ++i) {
3995 if (bottomLine(i)) {
4004 for (idx_type i = fcell; i < n; ++i) {
4005 if (bottomLine(i)) {
4015 col_type column = cellColumn(i);
4016 int len = clen[column];
4017 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4018 len += clen[column] + 4;
4019 os << docstring(len, ch);
4020 if (bottomLine(i)) {
4034 void Tabular::plaintextPrintCell(odocstringstream & os,
4035 OutputParams const & runparams,
4036 idx_type cell, row_type row, col_type column,
4037 vector<unsigned int> const & clen,
4038 bool onlydata, size_t max_length) const
4040 odocstringstream sstr;
4041 cellInset(cell)->plaintext(sstr, runparams, max_length);
4053 unsigned int len1 = sstr.str().length();
4054 unsigned int len2 = clen[column];
4055 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4056 len2 += clen[column] + 4;
4059 switch (getAlignment(cell)) {
4061 case LYX_ALIGN_LEFT:
4064 case LYX_ALIGN_RIGHT:
4068 case LYX_ALIGN_CENTER:
4074 os << docstring(len1, ' ') << sstr.str()
4075 << docstring(len2, ' ');
4077 if (rightLine(cell))
4084 void Tabular::plaintext(odocstringstream & os,
4085 OutputParams const & runparams, int const depth,
4086 bool onlydata, char_type delim, size_t max_length) const
4088 // first calculate the width of the single columns
4089 vector<unsigned int> clen(ncols());
4092 // first all non multicolumn cells!
4093 for (col_type c = 0; c < ncols(); ++c) {
4095 for (row_type r = 0; r < nrows(); ++r) {
4096 idx_type cell = cellIndex(r, c);
4097 if (isMultiColumn(cell))
4099 odocstringstream sstr;
4100 cellInset(cell)->plaintext(sstr, runparams, max_length);
4101 if (clen[c] < sstr.str().length())
4102 clen[c] = sstr.str().length();
4105 // then all multicolumn cells!
4106 for (col_type c = 0; c < ncols(); ++c) {
4107 for (row_type r = 0; r < nrows(); ++r) {
4108 idx_type cell = cellIndex(r, c);
4109 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
4111 odocstringstream sstr;
4112 cellInset(cell)->plaintext(sstr, runparams, max_length);
4113 int len = int(sstr.str().length());
4114 idx_type const n = columnSpan(cell);
4115 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
4117 if (len > int(clen[c + n - 1]))
4118 clen[c + n - 1] = len;
4123 for (row_type r = 0; r < nrows(); ++r) {
4124 if (!onlydata && plaintextTopHLine(os, r, clen))
4125 os << docstring(depth * 2, ' ');
4126 for (col_type c = 0; c < ncols(); ++c) {
4127 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
4129 if (onlydata && c > 0)
4130 // we don't use operator<< for single UCS4 character.
4131 // see explanation in docstream.h
4133 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
4135 if (os.str().size() > max_length)
4140 os << docstring(depth * 2, ' ');
4141 if (plaintextBottomHLine(os, r, clen))
4142 os << docstring(depth * 2, ' ');
4144 if (os.str().size() > max_length)
4150 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4152 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4156 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4158 return cell_info[row][column].inset;
4162 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4164 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4168 void Tabular::setCellInset(row_type row, col_type column,
4169 shared_ptr<InsetTableCell> ins)
4171 CellData & cd = cell_info[row][column];
4176 void Tabular::validate(LaTeXFeatures & features) const
4178 features.require("NeedTabularnewline");
4180 features.require("booktabs");
4181 if (is_long_tabular && !hasVarwidthColumn()) {
4182 if (tabular_width.zero())
4183 features.require("longtable");
4185 features.require("xltabular");
4187 if (rotate && is_long_tabular)
4188 features.require("lscape");
4190 features.require("rotating");
4191 if (hasVarwidthColumn()) {
4192 if (is_long_tabular)
4193 features.require("xltabular");
4195 features.require("tabularx");
4197 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4198 if (isMultiRow(cell))
4199 features.require("multirow");
4200 if (getUsebox(cell) == BOX_VARWIDTH) {
4201 features.require("varwidth");
4202 features.require("cellvarwidth");
4204 if (getVAlignment(cell) != LYX_VALIGN_TOP
4205 || !getPWidth(cell).zero()
4206 || isVTypeColumn(cellColumn(cell)))
4207 features.require("array");
4208 // Tell footnote that we need a savenote
4209 // environment in non-long tables or
4210 // longtable headers/footers
4211 else if (!is_long_tabular && !features.inFloat())
4212 features.saveNoteEnv("tabular");
4213 else if (!isValidRow(cellRow(cell)))
4214 features.saveNoteEnv("longtable");
4216 cellInset(cell)->validate(features);
4217 features.saveNoteEnv(string());
4222 Tabular::BoxType Tabular::useBox(idx_type cell) const
4224 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4225 if (parlist.size() > 1)
4226 return BOX_VARWIDTH;
4228 ParagraphList::const_iterator cit = parlist.begin();
4229 ParagraphList::const_iterator end = parlist.end();
4231 for (; cit != end; ++cit)
4232 for (int i = 0; i < cit->size(); ++i)
4233 if (cit->isNewline(i) || cit->layout().isEnvironment())
4234 return BOX_VARWIDTH;
4240 /////////////////////////////////////////////////////////////////////
4244 /////////////////////////////////////////////////////////////////////
4246 InsetTableCell::InsetTableCell(Buffer * buf)
4247 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false), isVarwidth(false),
4248 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4251 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4253 return isFixedWidth;
4257 bool InsetTableCell::forceLocalFontSwitch() const
4263 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4264 FuncStatus & status) const
4266 bool enabled = true;
4267 switch (cmd.action()) {
4268 case LFUN_INSET_SPLIT:
4269 case LFUN_INSET_DISSOLVE:
4272 case LFUN_MATH_DISPLAY:
4273 if (!hasFixedWidth()) {
4278 return InsetText::getStatus(cur, cmd, status);
4280 status.setEnabled(enabled);
4284 docstring InsetTableCell::asString(bool intoInsets)
4287 if (paragraphs().empty())
4289 ParagraphList::const_iterator it = paragraphs().begin();
4290 ParagraphList::const_iterator en = paragraphs().end();
4292 for (; it != en; ++it) {
4297 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4303 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4304 UpdateType utype, TocBackend & backend) const
4306 InsetText::iterateForToc(di, output_active, utype, backend);
4310 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4313 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4314 return InsetText::xhtml(xs, rp);
4318 void InsetTableCell::docbook(XMLStream & xs, OutputParams const & runparams) const
4320 InsetText::docbook(xs, runparams);
4324 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4326 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4327 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4329 // Hand font through to contained lyxtext:
4330 tm.font_.fontInfo() = mi.base.font;
4331 mi.base.textwidth -= horiz_offset;
4333 // This can happen when a layout has a left and right margin,
4334 // and the view is made very narrow. We can't do better than
4335 // to draw it partly out of view (bug 5890).
4336 if (mi.base.textwidth < 1)
4337 mi.base.textwidth = 1;
4339 // We tell metrics here not to expand on multiple pars
4340 // This is the difference to InsetText::Metrics
4341 if (hasFixedWidth() || isVarwidth)
4342 tm.metrics(mi, dim, mi.base.textwidth, false);
4344 tm.metrics(mi, dim, 0, false);
4345 mi.base.textwidth += horiz_offset;
4346 dim.asc += topOffset(mi.base.bv);
4347 dim.des += bottomOffset(mi.base.bv);
4348 dim.wid += horiz_offset;
4352 /////////////////////////////////////////////////////////////////////
4356 /////////////////////////////////////////////////////////////////////
4358 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4360 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4361 rowselect_(false), colselect_(false)
4366 InsetTabular::InsetTabular(InsetTabular const & tab)
4367 : Inset(tab), tabular(tab.tabular),
4368 rowselect_(false), colselect_(false)
4373 InsetTabular::~InsetTabular()
4375 hideDialogs("tabular", this);
4379 void InsetTabular::setBuffer(Buffer & buf)
4381 tabular.setBuffer(buf);
4382 Inset::setBuffer(buf);
4386 bool InsetTabular::insetAllowed(InsetCode code) const
4391 case MATHMACRO_CODE:
4396 return tabular.is_long_tabular;
4404 bool InsetTabular::allowMultiPar() const
4406 for (col_type c = 0; c < tabular.ncols(); ++c) {
4407 for (row_type r = 0; r < tabular.nrows(); ++r) {
4408 if (tabular.cellInset(r,c)->allowMultiPar())
4416 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4418 return tabular.is_long_tabular &&
4419 (newtype == "Standard" || newtype == "Unnumbered");
4423 void InsetTabular::write(ostream & os) const
4425 os << "Tabular" << endl;
4430 string InsetTabular::contextMenu(BufferView const &, int, int) const
4432 // FIXME: depending on the selection state,
4433 // we could offer a different menu.
4434 return cell(0)->contextMenuName() + ";" + contextMenuName();
4438 string InsetTabular::contextMenuName() const
4440 return "context-tabular";
4444 void InsetTabular::read(Lexer & lex)
4446 //bool const old_format = (lex.getString() == "\\LyXTable");
4454 string token = lex.getString();
4455 while (lex && token != "\\end_inset") {
4457 token = lex.getString();
4460 lex.printError("Missing \\end_inset at this point. ");
4464 int InsetTabular::rowFromY(Cursor & cur, int y) const
4466 // top y coordinate of tabular
4467 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4469 for (; r < tabular.nrows() && y > h; ++r)
4470 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4471 + tabular.interRowSpace(r);
4477 int InsetTabular::columnFromX(Cursor & cur, int x) const
4479 // left x coordinate of tabular
4480 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4482 for (; c < tabular.ncols() && x > w; ++c)
4483 w += tabular.cellWidth(c);
4488 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4490 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4491 // mi.base.textwidth << "\n";
4492 LBUFERR(mi.base.bv);
4494 for (row_type r = 0; r < tabular.nrows(); ++r) {
4497 for (col_type c = 0; c < tabular.ncols(); ++c) {
4498 if (tabular.isPartOfMultiColumn(r, c)
4499 || tabular.isPartOfMultiRow(r, c))
4500 // multicolumn or multirow cell, but not first one
4502 idx_type const cell = tabular.cellIndex(r, c);
4505 Length const p_width = tabular.getPWidth(cell);
4506 if (!p_width.zero())
4507 m.base.textwidth = mi.base.inPixels(p_width);
4508 else if (tabular.column_info[c].varwidth)
4509 m.base.textwidth = tabular.column_info[c].width;
4510 tabular.cellInset(cell)->metrics(m, dim0);
4511 if (!p_width.zero() || tabular.column_info[c].varwidth)
4512 dim0.wid = m.base.textwidth;
4513 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4514 + tabular.interColumnSpace(cell);
4516 // FIXME(?): do we need a second metrics call?
4517 TextMetrics const & tm =
4518 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4520 // determine horizontal offset because of decimal align (if necessary)
4521 int decimal_width = 0;
4522 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4523 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4524 tail.setBuffer(tabular.buffer());
4525 // we need to set macrocontext position everywhere
4526 // otherwise we crash with nested insets (e.g. footnotes)
4527 // after decimal point
4528 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4530 dit.push_back(CursorSlice(tail));
4531 tail.setMacrocontextPositionRecursive(dit);
4533 // remove text leading decimal point
4534 docstring const align_d = tabular.column_info[c].decimal_point;
4535 dit = separatorPos(&tail, align_d);
4537 pos_type const psize = tail.paragraphs().front().size();
4539 tail.paragraphs().front().eraseChars(0,
4540 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4542 tail.metrics(m, dim1);
4543 decimal_width = dim1.width();
4546 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4547 tabular.cell_info[r][c].decimal_width = decimal_width;
4549 // with LYX_VALIGN_BOTTOM the descent is relative to the last
4550 // row of the last par (note that the par might have multile rows!)
4551 // = descent of text in last row + bottomOffset:
4552 int const lastpardes = tm.last().second->rows().back().descent()
4553 + bottomOffset(mi.base.bv);
4555 switch (tabular.getVAlignment(cell)) {
4556 case Tabular::LYX_VALIGN_TOP:
4558 case Tabular::LYX_VALIGN_MIDDLE:
4559 offset = -(dim0.des - lastpardes)/2;
4561 case Tabular::LYX_VALIGN_BOTTOM:
4562 offset = -(dim0.des - lastpardes);
4565 tabular.cell_info[r][c].voffset = offset;
4566 maxasc = max(maxasc, dim0.asc - offset);
4567 maxdes = max(maxdes, dim0.des + offset);
4569 int const top_space = tabular.row_info[r].top_space_default ?
4570 default_line_space :
4571 mi.base.inPixels(tabular.row_info[r].top_space);
4572 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4573 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4574 default_line_space :
4575 mi.base.inPixels(tabular.row_info[r].bottom_space);
4576 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4579 // We need to recalculate the metrics after column width calculation
4580 // with xtabular (possibly multiple times, so the call is recursive).
4581 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4583 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4584 dim.des = tabular.height() - dim.asc;
4585 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4589 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4591 if (&cur.inset() == this && cur.selection()) {
4592 if (cur.selIsMultiCell()) {
4595 getSelection(cur, rs, re, cs, ce);
4597 idx_type const cell = tabular.cellIndex(row, col);
4598 col_type const cspan = tabular.columnSpan(cell);
4599 row_type const rspan = tabular.rowSpan(cell);
4600 if (col + cspan - 1 >= cs && col <= ce
4601 && row + rspan - 1 >= rs && row <= re)
4604 if (col == tabular.cellColumn(cur.idx())
4605 && row == tabular.cellRow(cur.idx())) {
4606 CursorSlice const & beg = cur.selBegin();
4607 CursorSlice const & end = cur.selEnd();
4609 if ((end.lastpos() > 0 || end.lastpit() > 0)
4610 && end.pos() == end.lastpos() && beg.pos() == 0
4611 && end.pit() == end.lastpit() && beg.pit() == 0)
4619 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4621 x += ADD_TO_TABULAR_WIDTH;
4623 BufferView * bv = pi.base.bv;
4624 Cursor & cur = pi.base.bv->cursor();
4626 // FIXME: As the full background is painted in drawBackground(),
4627 // we have no choice but to do a full repaint for the Text cells.
4628 pi.full_repaint = true;
4630 bool const original_selection_state = pi.selected;
4634 // Save tabular change status
4635 Change tab_change = pi.change;
4637 int yy = y + tabular.offsetVAlignment();
4638 for (row_type r = 0; r < tabular.nrows(); ++r) {
4640 for (col_type c = 0; c < tabular.ncols(); ++c) {
4641 if (tabular.isPartOfMultiColumn(r, c))
4644 idx = tabular.cellIndex(r, c);
4646 if (tabular.isPartOfMultiRow(r, c)) {
4647 nx += tabular.cellWidth(idx);
4651 pi.selected |= isCellSelected(cur, r, c);
4653 // Mark deleted rows/columns
4654 if (tabular.column_info[c].change.changed())
4655 pi.change = tabular.column_info[c].change;
4656 else if (tabular.row_info[r].change.changed())
4657 pi.change = tabular.row_info[r].change;
4659 pi.change = tab_change;
4661 int const cx = nx + tabular.textHOffset(idx);
4662 int const cy = yy + tabular.textVOffset(idx);
4663 // Cache the Inset position.
4664 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4665 cell(idx)->draw(pi, cx, cy);
4666 drawCellLines(pi, nx, yy, r, idx);
4667 nx += tabular.cellWidth(idx);
4668 pi.selected = original_selection_state;
4671 if (r + 1 < tabular.nrows())
4672 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4673 + tabular.interRowSpace(r + 1);
4678 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4680 x += ADD_TO_TABULAR_WIDTH;
4681 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4682 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4683 pi.backgroundColor(this));
4687 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4689 Cursor & cur = pi.base.bv->cursor();
4691 x += ADD_TO_TABULAR_WIDTH;
4693 if (!cur.selection())
4695 if (&cur.inset() != this)
4700 bool const full_cell_selected = isCellSelected(cur,
4701 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4703 if (cur.selIsMultiCell() || full_cell_selected) {
4704 for (row_type r = 0; r < tabular.nrows(); ++r) {
4706 for (col_type c = 0; c < tabular.ncols(); ++c) {
4707 if (tabular.isPartOfMultiColumn(r, c))
4710 idx_type const cell = tabular.cellIndex(r, c);
4712 if (tabular.isPartOfMultiRow(r, c)) {
4713 xx += tabular.cellWidth(cell);
4716 int const w = tabular.cellWidth(cell);
4717 int const h = tabular.cellHeight(cell);
4718 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4719 if (isCellSelected(cur, r, c))
4720 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4723 if (r + 1 < tabular.nrows())
4724 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4725 + tabular.interRowSpace(r + 1);
4729 // FIXME: This code has no effect because InsetTableCell does not handle
4730 // drawSelection other than the trivial implementation in Inset.
4732 // x += cellXPos(cur.idx());
4733 // x += tabular.textHOffset(cur.idx());
4734 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4741 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4742 Color const incol, bool drawline, bool heavy = false)
4744 Color const col = drawline ? incol : Color_tabularonoffline;
4745 if (drawline && lt > 0)
4746 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4747 Painter::line_onoffdash,
4748 Painter::thin_line);
4749 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4750 drawline ? Painter::line_solid : Painter::line_onoffdash,
4751 (heavy ? 2 : 1) * Painter::thin_line);
4752 if (drawline && rt > 0)
4753 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4754 Painter::line_onoffdash,
4755 Painter::thin_line);
4761 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4762 row_type row, idx_type cell) const
4764 y -= tabular.rowAscent(row);
4765 int const w = tabular.cellWidth(cell);
4766 int const h = tabular.cellHeight(cell);
4770 col_type const col = tabular.cellColumn(cell);
4772 // Colour the frame if rows/columns are added or deleted
4773 Color colour = Color_tabularline;
4774 if (tabular.column_info[col].change.changed()
4775 || tabular.row_info[row].change.changed())
4776 colour = InsetTableCell(*tabular.cellInset(cell)).paragraphs().front().lookupChange(0).color();
4779 bool drawline = tabular.topLine(cell)
4780 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4781 bool heavy = tabular.use_booktabs
4782 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4783 && tabular.rowTopLine(row);
4784 if (tabular.topLineTrim(cell).first
4785 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4787 if (tabular.topLineTrim(cell).second
4788 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4790 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4794 drawline = tabular.bottomLine(cell);
4795 row_type const lastrow = tabular.nrows() - 1;
4796 // Consider multi-rows
4798 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4799 && tabular.isPartOfMultiRow(r + 1, col))
4801 heavy = tabular.use_booktabs
4802 && ((row == lastrow && tabular.rowBottomLine(row))
4803 || (r == lastrow && tabular.rowBottomLine(r)));
4804 if (tabular.bottomLineTrim(cell).first)
4806 if (tabular.bottomLineTrim(cell).second)
4808 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4811 drawline = tabular.leftLine(cell)
4812 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4813 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4816 x -= tabular.interColumnSpace(cell);
4817 col_type next_cell_col = col + 1;
4818 while (next_cell_col < tabular.ncols()
4819 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4821 drawline = tabular.rightLine(cell)
4822 || (next_cell_col < tabular.ncols()
4823 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4824 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4828 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4830 //lyxerr << "InsetTabular::edit: " << this << endl;
4834 if (isRightToLeft(cur))
4835 cur.idx() = tabular.getLastCellInRow(0);
4841 if (isRightToLeft(cur))
4842 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4844 cur.idx() = tabular.numberofcells - 1;
4846 cur.pos() = cur.lastpos(); // FIXME crude guess
4848 cur.setCurrentFont();
4849 // FIXME: this accesses the position cache before it is initialized
4850 //cur.bv().fitCursor();
4854 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4856 // In a longtable, tell captions what the current float is
4857 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4858 string const saveflt = cnts.current_float();
4859 if (tabular.is_long_tabular) {
4860 cnts.current_float("table");
4861 // in longtables, we only step the counter once
4862 cnts.step(from_ascii("table"), utype);
4863 cnts.isLongtable(true);
4866 ParIterator it2 = it;
4868 size_t const end = it2.nargs();
4869 for ( ; it2.idx() < end; it2.top().forwardIdx())
4870 buffer().updateBuffer(it2, utype);
4873 if (tabular.is_long_tabular) {
4874 cnts.current_float(saveflt);
4875 cnts.isLongtable(false);
4880 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4881 UpdateType utype, TocBackend & backend) const
4883 DocIterator dit = cpit;
4885 size_t const end = dit.nargs();
4886 for ( ; dit.idx() < end; dit.top().forwardIdx())
4887 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4891 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4893 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4894 return x < x0 || x > x0 + tabular.width();
4898 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4900 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4901 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4902 // there is no margin added vertically to tabular insets.
4903 // However, it works for now.
4904 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4908 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4910 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4914 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4916 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4917 << "\n cur:" << cur);
4918 CursorSlice sl = cur.top();
4919 Cursor & bvcur = cur.bv().cursor();
4921 FuncCode const act = cmd.action();
4925 case LFUN_MOUSE_PRESS: {
4926 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4928 if (hitSelectRow(cur.bv(), cmd.x())) {
4929 row_type r = rowFromY(cur, cmd.y());
4930 cur.idx() = tabular.getFirstCellInRow(r);
4934 cur.idx() = tabular.getLastCellInRow(r);
4935 cur.pit() = cur.lastpit();
4936 cur.pos() = cur.lastpos();
4937 cur.selection(true);
4943 if (hitSelectColumn(cur.bv(), cmd.y())) {
4944 col_type c = columnFromX(cur, cmd.x());
4945 cur.idx() = tabular.cellIndex(0, c);
4949 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4950 cur.pit() = cur.lastpit();
4951 cur.pos() = cur.lastpos();
4952 cur.selection(true);
4957 // do not reset cursor/selection if we have selected
4958 // some cells (bug 2715).
4959 if (cmd.button() == mouse_button::button3
4960 && &bvcur.selBegin().inset() == this
4961 && bvcur.selIsMultiCell())
4964 // Let InsetTableCell do it
4965 cell(cur.idx())->dispatch(cur, cmd);
4968 case LFUN_MOUSE_MOTION:
4969 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4970 if (cmd.button() == mouse_button::button1) {
4971 // only accept motions to places not deeper nested than the real anchor
4972 if (!bvcur.realAnchor().hasPart(cur)) {
4976 // select (additional) row
4978 row_type r = rowFromY(cur, cmd.y());
4979 cur.idx() = tabular.getLastCellInRow(r);
4980 // we need to reset the cursor's pit and pos now, as the old ones
4981 // may no longer be valid.
4984 bvcur.setCursor(cur);
4985 bvcur.selection(true);
4988 // select (additional) column
4990 col_type c = columnFromX(cur, cmd.x());
4991 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4992 // we need to reset the cursor's pit and pos now, as the old ones
4993 // may no longer be valid.
4996 bvcur.setCursor(cur);
4997 bvcur.selection(true);
5000 // only update if selection changes
5001 if (bvcur.idx() == cur.idx() &&
5002 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
5003 cur.noScreenUpdate();
5004 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
5005 bvcur.setCursor(cur);
5006 bvcur.selection(true);
5007 // if this is a multicell selection, we just set the cursor to
5008 // the beginning of the cell's text.
5009 if (bvcur.selIsMultiCell()) {
5010 bvcur.pit() = bvcur.lastpit();
5011 bvcur.pos() = bvcur.lastpos();
5016 case LFUN_MOUSE_RELEASE:
5021 case LFUN_CELL_BACKWARD:
5023 cur.selection(false);
5026 case LFUN_CELL_FORWARD:
5028 cur.selection(false);
5031 case LFUN_CHAR_FORWARD_SELECT:
5032 case LFUN_CHAR_FORWARD:
5033 case LFUN_CHAR_BACKWARD_SELECT:
5034 case LFUN_CHAR_BACKWARD:
5035 case LFUN_CHAR_RIGHT_SELECT:
5036 case LFUN_CHAR_RIGHT:
5037 case LFUN_CHAR_LEFT_SELECT:
5038 case LFUN_CHAR_LEFT:
5039 case LFUN_WORD_FORWARD:
5040 case LFUN_WORD_FORWARD_SELECT:
5041 case LFUN_WORD_BACKWARD:
5042 case LFUN_WORD_BACKWARD_SELECT:
5043 case LFUN_WORD_RIGHT:
5044 case LFUN_WORD_RIGHT_SELECT:
5045 case LFUN_WORD_LEFT:
5046 case LFUN_WORD_LEFT_SELECT: {
5047 // determine whether we move to next or previous cell, where to enter
5048 // the new cell from, and which command to "finish" (i.e., exit the
5051 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
5052 FuncCode finish_lfun;
5054 if (act == LFUN_CHAR_FORWARD
5055 || act == LFUN_CHAR_FORWARD_SELECT
5056 || act == LFUN_WORD_FORWARD
5057 || act == LFUN_WORD_FORWARD_SELECT) {
5059 finish_lfun = LFUN_FINISHED_FORWARD;
5061 else if (act == LFUN_CHAR_BACKWARD
5062 || act == LFUN_CHAR_BACKWARD_SELECT
5063 || act == LFUN_WORD_BACKWARD
5064 || act == LFUN_WORD_BACKWARD_SELECT) {
5066 finish_lfun = LFUN_FINISHED_BACKWARD;
5068 // LEFT or RIGHT commands --- the interpretation will depend on the
5069 // table's direction.
5071 bool const right = act == LFUN_CHAR_RIGHT
5072 || act == LFUN_CHAR_RIGHT_SELECT
5073 || act == LFUN_WORD_RIGHT
5074 || act == LFUN_WORD_RIGHT_SELECT;
5075 next_cell = isRightToLeft(cur) != right;
5077 if (lyxrc.visual_cursor)
5078 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
5080 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
5083 bool const select = act == LFUN_CHAR_FORWARD_SELECT
5084 || act == LFUN_CHAR_BACKWARD_SELECT
5085 || act == LFUN_CHAR_RIGHT_SELECT
5086 || act == LFUN_CHAR_LEFT_SELECT
5087 || act == LFUN_WORD_FORWARD_SELECT
5088 || act == LFUN_WORD_RIGHT_SELECT
5089 || act == LFUN_WORD_BACKWARD_SELECT
5090 || act == LFUN_WORD_LEFT_SELECT;
5092 // If we have a multicell selection or we're
5093 // not doing some LFUN_*_SELECT thing anyway...
5094 if (!cur.selIsMultiCell() || !select) {
5095 col_type const c = tabular.cellColumn(cur.idx());
5096 row_type const r = tabular.cellRow(cur.idx());
5097 // Are we trying to select the whole cell and is the whole cell
5098 // not yet selected?
5099 bool const select_whole = select && !isCellSelected(cur, r, c) &&
5100 ((next_cell && cur.pit() == cur.lastpit()
5101 && cur.pos() == cur.lastpos())
5102 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
5104 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
5106 // ...try to dispatch to the cell's inset.
5107 cell(cur.idx())->dispatch(cur, cmd);
5109 // When we already have a selection we want to select the whole cell
5110 // before going to the next cell.
5111 if (select_whole && !empty_cell){
5112 getText(cur.idx())->selectAll(cur);
5114 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5118 // FIXME: When we support the selection of an empty cell, remove
5119 // the !empty_cell from this condition. For now we jump to the next
5120 // cell if the current cell is empty.
5121 if (cur.result().dispatched() && !empty_cell)
5125 // move to next/prev cell, as appropriate
5126 // note that we will always do this if we're selecting and we have
5127 // a multicell selection
5128 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
5129 << " cell from: " << int(entry_from));
5131 moveNextCell(cur, entry_from);
5133 movePrevCell(cur, entry_from);
5134 // if we're exiting the table, call the appropriate FINISHED lfun
5135 if (sl == cur.top()) {
5136 cmd = FuncRequest(finish_lfun);
5141 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5146 case LFUN_DOWN_SELECT:
5148 if (!(cur.selection() && cur.selIsMultiCell()))
5149 cell(cur.idx())->dispatch(cur, cmd);
5151 cur.dispatched(); // override the cell's decision
5152 if (sl == cur.top()) {
5153 // if our Text didn't do anything to the cursor
5154 // then we try to put the cursor into the cell below
5155 // setting also the right targetX.
5156 cur.selHandle(act == LFUN_DOWN_SELECT);
5157 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
5158 int const xtarget = cur.targetX();
5159 // WARNING: Once cur.idx() has been reset, the cursor is in
5160 // an inconsistent state until pos() has been set. Be careful
5161 // what you do with it!
5162 cur.idx() = tabular.cellBelow(cur.idx());
5164 TextMetrics const & tm =
5165 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5166 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5167 cur.setCurrentFont();
5170 if (sl == cur.top()) {
5171 // we trick it to go to forward after leaving the
5173 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5176 if (cur.selIsMultiCell()) {
5177 cur.pit() = cur.lastpit();
5178 cur.pos() = cur.lastpos();
5179 cur.setCurrentFont();
5180 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5183 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5186 case LFUN_UP_SELECT:
5188 if (!(cur.selection() && cur.selIsMultiCell()))
5189 cell(cur.idx())->dispatch(cur, cmd);
5190 cur.dispatched(); // override the cell's decision
5191 if (sl == cur.top()) {
5192 // if our Text didn't do anything to the cursor
5193 // then we try to put the cursor into the cell above
5194 // setting also the right targetX.
5195 cur.selHandle(act == LFUN_UP_SELECT);
5196 if (tabular.cellRow(cur.idx()) != 0) {
5197 int const xtarget = cur.targetX();
5198 // WARNING: Once cur.idx() has been reset, the cursor is in
5199 // an inconsistent state until pos() has been set. Be careful
5200 // what you do with it!
5201 cur.idx() = tabular.cellAbove(cur.idx());
5202 cur.pit() = cur.lastpit();
5203 Text const * text = cell(cur.idx())->getText(0);
5204 TextMetrics const & tm = cur.bv().textMetrics(text);
5205 ParagraphMetrics const & pm =
5206 tm.parMetrics(cur.lastpit());
5207 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5208 cur.setCurrentFont();
5211 if (sl == cur.top()) {
5212 cmd = FuncRequest(LFUN_UP);
5215 if (cur.selIsMultiCell()) {
5217 cur.pos() = cur.lastpos();
5218 cur.setCurrentFont();
5219 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5222 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5225 case LFUN_LAYOUT_TABULAR:
5226 cur.bv().showDialog("tabular");
5229 case LFUN_INSET_MODIFY:
5230 // we come from the dialog
5231 if (cmd.getArg(0) == "tabular")
5232 tabularFeatures(cur, cmd.getLongArg(1));
5237 case LFUN_TABULAR_FEATURE:
5238 tabularFeatures(cur, to_utf8(cmd.argument()));
5241 // insert file functions
5242 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5243 case LFUN_FILE_INSERT_PLAINTEXT:
5245 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5246 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5247 FileName(to_utf8(cmd.argument())));
5250 cur.recordUndoInset();
5251 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5252 // content has been replaced,
5253 // so cursor might be invalid
5254 cur.pos() = cur.lastpos();
5255 cur.pit() = cur.lastpit();
5256 bvcur.setCursor(cur);
5263 if (cur.selIsMultiCell()) {
5264 if (copySelection(cur)) {
5265 cur.recordUndoInset();
5269 cell(cur.idx())->dispatch(cur, cmd);
5272 case LFUN_SELF_INSERT:
5273 if (cur.selIsMultiCell()) {
5274 cur.recordUndoInset();
5276 BufferView * bv = &cur.bv();
5277 docstring::const_iterator cit = cmd.argument().begin();
5278 docstring::const_iterator const end = cmd.argument().end();
5279 for (; cit != end; ++cit)
5280 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5283 bv->bookmarkEditPosition();
5285 cell(cur.idx())->dispatch(cur, cmd);
5288 case LFUN_CHAR_DELETE_BACKWARD:
5289 case LFUN_CHAR_DELETE_FORWARD:
5290 if (cur.selIsMultiCell()) {
5291 cur.recordUndoInset();
5294 cell(cur.idx())->dispatch(cur, cmd);
5297 case LFUN_CLIPBOARD_PASTE:
5298 case LFUN_PRIMARY_SELECTION_PASTE: {
5299 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5300 theClipboard().getAsText(Clipboard::PlainTextType) :
5301 theSelection().get();
5304 // pass to InsertPlaintextString, but
5305 // only if we have multi-cell content
5306 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5307 cur.recordUndoInset();
5308 if (insertPlaintextString(cur.bv(), clip, false)) {
5309 // content has been replaced,
5310 // so cursor might be invalid
5311 cur.pos() = cur.lastpos();
5312 cur.pit() = cur.lastpit();
5313 bvcur.setCursor(cur);
5317 // Let the cell handle normal text
5318 cell(cur.idx())->dispatch(cur, cmd);
5323 if (!tabularStackDirty()) {
5324 // Check if we have plain text or HTML with rows/columns.
5325 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5326 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5327 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5328 docstring const clip =
5329 theClipboard().getAsText(Clipboard::AnyTextType);
5330 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5331 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5332 doDispatch(cur, ncmd);
5336 else if (theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5337 // This might be tabular data from another LyX instance. Check!
5338 docstring const clip =
5339 theClipboard().getAsText(Clipboard::PlainTextType);
5340 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5341 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5342 doDispatch(cur, ncmd);
5346 if (!cur.selIsMultiCell())
5347 cell(cur.idx())->dispatch(cur, cmd);
5350 if (theClipboard().isInternal()) {
5351 cur.recordUndoInset();
5352 pasteClipboard(cur);
5356 case LFUN_CHANGE_ACCEPT:
5357 case LFUN_CHANGE_REJECT:
5358 case LFUN_FONT_EMPH:
5359 case LFUN_FONT_BOLD:
5360 case LFUN_FONT_BOLDSYMBOL:
5361 case LFUN_FONT_ROMAN:
5362 case LFUN_FONT_NOUN:
5363 case LFUN_FONT_ITAL:
5364 case LFUN_FONT_FRAK:
5365 case LFUN_FONT_TYPEWRITER:
5366 case LFUN_FONT_SANS:
5367 case LFUN_TEXTSTYLE_APPLY:
5368 case LFUN_TEXTSTYLE_UPDATE:
5369 case LFUN_FONT_SIZE:
5370 case LFUN_FONT_UNDERLINE:
5371 case LFUN_FONT_STRIKEOUT:
5372 case LFUN_FONT_CROSSOUT:
5373 case LFUN_FONT_UNDERUNDERLINE:
5374 case LFUN_FONT_UNDERWAVE:
5376 case LFUN_PARAGRAPH_PARAMS_APPLY:
5377 case LFUN_PARAGRAPH_PARAMS:
5378 case LFUN_WORD_CAPITALIZE:
5379 case LFUN_WORD_UPCASE:
5380 case LFUN_WORD_LOWCASE:
5381 case LFUN_CHARS_TRANSPOSE: {
5382 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5383 if (cur.selIsMultiCell()) {
5386 getSelection(cur, rs, re, cs, ce);
5387 Cursor tmpcur = cur;
5388 for (row_type r = rs; r <= re; ++r) {
5389 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5390 // whole row selected
5391 if (act == LFUN_CHANGE_ACCEPT) {
5392 if (tabular.row_info[r].change.inserted())
5393 tabular.row_info[r].change.setUnchanged();
5394 else if (tabular.row_info[r].change.deleted()) {
5395 tabular.deleteRow(r, true);
5400 if (tabular.row_info[r].change.deleted())
5401 tabular.row_info[r].change.setUnchanged();
5402 else if (tabular.row_info[r].change.inserted()) {
5403 tabular.deleteRow(r, true);
5409 for (col_type c = cs; c <= ce; ++c) {
5410 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5411 // whole col selected
5412 if (act == LFUN_CHANGE_ACCEPT) {
5413 if (tabular.column_info[c].change.inserted())
5414 tabular.column_info[c].change.setUnchanged();
5415 else if (tabular.column_info[c].change.deleted()) {
5416 tabular.deleteColumn(c, true);
5421 if (tabular.column_info[c].change.deleted())
5422 tabular.column_info[c].change.setUnchanged();
5423 else if (tabular.column_info[c].change.inserted()) {
5424 tabular.deleteColumn(c, true);
5430 // cursor follows cell:
5431 tmpcur.idx() = tabular.cellIndex(r, c);
5432 // select this cell only:
5435 tmpcur.resetAnchor();
5436 tmpcur.pit() = tmpcur.lastpit();
5437 tmpcur.pos() = tmpcur.top().lastpos();
5438 tmpcur.setCursor(tmpcur);
5439 tmpcur.setSelection();
5440 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5444 tabular.updateIndexes();
5445 // cursor might be invalid
5447 // change bar might need to be redrawn
5448 cur.screenUpdateFlags(Update::Force);
5449 cur.forceBufferUpdate();
5453 cell(cur.idx())->dispatch(cur, cmd);
5458 case LFUN_CHANGE_NEXT:
5459 case LFUN_CHANGE_PREVIOUS: {
5460 // BufferView::dispatch has already moved the cursor, we just
5461 // need to select here if we have a changed row or column
5462 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5464 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5468 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5469 cur.pit() = cur.lastpit();
5470 cur.pos() = cur.lastpos();
5471 cur.selection(true);
5475 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5477 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5481 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5482 cur.pit() = cur.lastpit();
5483 cur.pos() = cur.lastpos();
5484 cur.selection(true);
5491 case LFUN_INSET_SETTINGS:
5492 // relay this lfun to Inset, not to the cell.
5493 Inset::doDispatch(cur, cmd);
5497 // we try to handle this event in the insets dispatch function.
5498 cell(cur.idx())->dispatch(cur, cmd);
5504 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5505 string const & argument, FuncStatus & status) const
5508 int action = Tabular::LAST_ACTION;
5510 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5511 if (tabularFeature[i].feature == s) {
5512 action = tabularFeature[i].action;
5516 if (action == Tabular::LAST_ACTION) {
5518 status.setUnknown(true);
5522 row_type sel_row_start = 0;
5523 row_type sel_row_end = 0;
5524 col_type sel_col_start = 0;
5525 col_type sel_col_end = 0;
5526 Tabular::ltType dummyltt;
5529 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5532 case Tabular::SET_PWIDTH:
5533 case Tabular::SET_MPWIDTH:
5534 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5535 case Tabular::SET_SPECIAL_COLUMN:
5536 case Tabular::SET_SPECIAL_MULTICOLUMN:
5537 case Tabular::APPEND_ROW:
5538 case Tabular::APPEND_COLUMN:
5539 case Tabular::DELETE_ROW:
5540 case Tabular::DELETE_COLUMN:
5541 case Tabular::COPY_ROW:
5542 case Tabular::COPY_COLUMN:
5543 case Tabular::SET_TOP_SPACE:
5544 case Tabular::SET_BOTTOM_SPACE:
5545 case Tabular::SET_INTERLINE_SPACE:
5549 case Tabular::SET_TABULAR_WIDTH:
5550 status.setEnabled(!tabular.rotate
5551 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5554 case Tabular::MOVE_COLUMN_RIGHT:
5555 case Tabular::MOVE_COLUMN_LEFT:
5556 case Tabular::MOVE_ROW_DOWN:
5557 case Tabular::MOVE_ROW_UP: {
5560 if (cur.selIsMultiCell())
5561 getSelection(cur, rs, re, cs, ce);
5563 rs = tabular.cellRow(cur.idx());
5565 cs = tabular.cellColumn(cur.idx());
5568 if ((action == Tabular::MOVE_COLUMN_RIGHT
5569 && tabular.ncols() == ce + 1)
5570 || (action == Tabular::MOVE_COLUMN_LEFT && cs == 0)
5571 || (action == Tabular::MOVE_ROW_DOWN
5572 && tabular.nrows() == re + 1)
5573 || (action == Tabular::MOVE_ROW_UP && rs == 0)) {
5574 status.setEnabled(false);
5578 if (action == Tabular::MOVE_COLUMN_RIGHT
5579 || action == Tabular::MOVE_COLUMN_LEFT) {
5580 bool has_multicol = (action == Tabular::MOVE_COLUMN_RIGHT)
5581 ? tabular.hasMultiColumn(ce + 1)
5582 : tabular.hasMultiColumn(cs - 1);
5583 for (col_type c = cs; c <= ce; ++c) {
5584 if (tabular.hasMultiColumn(c)) {
5585 has_multicol = true;
5590 status.message(_("Column movement not supported with multi-columns."));
5591 status.setEnabled(false);
5596 if (action == Tabular::MOVE_ROW_DOWN
5597 || action == Tabular::MOVE_ROW_UP) {
5598 bool has_multirow = (action == Tabular::MOVE_ROW_DOWN)
5599 ? tabular.hasMultiRow(re + 1)
5600 : tabular.hasMultiRow(rs - 1);
5601 for (row_type r = rs; r <= re; ++r) {
5602 if (tabular.hasMultiRow(r)) {
5603 has_multirow = true;
5608 status.message(_("Row movement not supported with multi-rows."));
5609 status.setEnabled(false);
5614 status.setEnabled(true);
5618 case Tabular::SET_DECIMAL_POINT:
5620 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5623 case Tabular::SET_MULTICOLUMN:
5624 case Tabular::UNSET_MULTICOLUMN:
5625 case Tabular::MULTICOLUMN:
5626 // If a row is set as longtable caption, it must not be allowed
5627 // to unset that this row is a multicolumn.
5628 status.setEnabled(sel_row_start == sel_row_end
5629 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5630 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5633 case Tabular::SET_MULTIROW:
5634 case Tabular::UNSET_MULTIROW:
5635 case Tabular::MULTIROW:
5636 // If a row is set as longtable caption, it must not be allowed
5637 // to unset that this row is a multirow.
5638 status.setEnabled(sel_col_start == sel_col_end
5639 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5640 status.setOnOff(tabular.isMultiRow(cur.idx()));
5643 case Tabular::SET_ALL_LINES:
5644 case Tabular::UNSET_ALL_LINES:
5645 case Tabular::SET_INNER_LINES:
5646 case Tabular::SET_BORDER_LINES:
5647 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5650 case Tabular::RESET_FORMAL_DEFAULT:
5651 status.setEnabled(tabular.use_booktabs);
5654 case Tabular::SET_LINE_TOP:
5655 case Tabular::SET_LINE_BOTTOM:
5656 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5659 case Tabular::SET_LINE_LEFT:
5660 case Tabular::SET_LINE_RIGHT:
5661 status.setEnabled(!tabular.use_booktabs
5662 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5665 case Tabular::SET_LTRIM_TOP:
5666 case Tabular::SET_RTRIM_TOP:
5667 status.setEnabled(tabular.use_booktabs
5668 && tabular.cellRow(cur.idx()) != 0
5669 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5672 case Tabular::SET_LTRIM_BOTTOM:
5673 case Tabular::SET_RTRIM_BOTTOM:
5674 status.setEnabled(tabular.use_booktabs
5675 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5676 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5679 case Tabular::TOGGLE_LINE_TOP:
5680 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5681 status.setOnOff(tabular.topLine(cur.idx()));
5684 case Tabular::TOGGLE_LINE_BOTTOM:
5685 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5686 status.setOnOff(tabular.bottomLine(cur.idx()));
5689 case Tabular::TOGGLE_LINE_LEFT:
5690 status.setEnabled(!tabular.use_booktabs
5691 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5692 status.setOnOff(tabular.leftLine(cur.idx()));
5695 case Tabular::TOGGLE_LINE_RIGHT:
5696 status.setEnabled(!tabular.use_booktabs
5697 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5698 status.setOnOff(tabular.rightLine(cur.idx()));
5701 case Tabular::TOGGLE_LTRIM_TOP:
5702 status.setEnabled(tabular.use_booktabs
5703 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5704 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5707 case Tabular::TOGGLE_RTRIM_TOP:
5708 status.setEnabled(tabular.use_booktabs
5709 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5710 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5713 case Tabular::TOGGLE_LTRIM_BOTTOM:
5714 status.setEnabled(tabular.use_booktabs
5715 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5716 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5719 case Tabular::TOGGLE_RTRIM_BOTTOM:
5720 status.setEnabled(tabular.use_booktabs
5721 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5722 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5725 // multirow cells only inherit the alignment of the column if the column has
5726 // no width, otherwise they are left-aligned
5727 // therefore allow always left but right and center only if there is no width
5728 case Tabular::M_ALIGN_LEFT:
5731 case Tabular::ALIGN_LEFT:
5732 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5735 case Tabular::M_ALIGN_RIGHT:
5738 case Tabular::ALIGN_RIGHT:
5739 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5740 && !tabular.getPWidth(cur.idx()).zero()));
5741 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5744 case Tabular::M_ALIGN_CENTER:
5747 case Tabular::ALIGN_CENTER:
5748 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5749 && !tabular.getPWidth(cur.idx()).zero()));
5750 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5753 case Tabular::ALIGN_BLOCK:
5754 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5755 && !tabular.isMultiRow(cur.idx()));
5756 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5759 case Tabular::ALIGN_DECIMAL:
5760 status.setEnabled(!tabular.isMultiRow(cur.idx())
5761 && !tabular.isMultiColumn(cur.idx()));
5762 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5765 case Tabular::M_VALIGN_TOP:
5768 case Tabular::VALIGN_TOP:
5769 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5770 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5772 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5775 case Tabular::M_VALIGN_BOTTOM:
5778 case Tabular::VALIGN_BOTTOM:
5779 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5780 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5782 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5785 case Tabular::M_VALIGN_MIDDLE:
5788 case Tabular::VALIGN_MIDDLE:
5789 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5790 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5792 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5795 case Tabular::SET_LONGTABULAR:
5796 case Tabular::TOGGLE_LONGTABULAR:
5797 // setting as longtable is not allowed when table is inside a float
5798 if (cur.innerInsetOfType(FLOAT_CODE) != nullptr
5799 || cur.innerInsetOfType(WRAP_CODE) != nullptr)
5800 status.setEnabled(false);
5802 status.setEnabled(true);
5803 status.setOnOff(tabular.is_long_tabular);
5806 case Tabular::UNSET_LONGTABULAR:
5807 status.setOnOff(!tabular.is_long_tabular);
5810 case Tabular::TOGGLE_ROTATE_TABULAR:
5811 case Tabular::SET_ROTATE_TABULAR:
5812 status.setOnOff(tabular.rotate != 0);
5815 case Tabular::TABULAR_VALIGN_TOP:
5816 status.setEnabled(tabular.tabular_width.zero());
5817 status.setOnOff(tabular.tabular_valignment
5818 == Tabular::LYX_VALIGN_TOP);
5820 case Tabular::TABULAR_VALIGN_MIDDLE:
5821 status.setEnabled(tabular.tabular_width.zero());
5822 status.setOnOff(tabular.tabular_valignment
5823 == Tabular::LYX_VALIGN_MIDDLE);
5825 case Tabular::TABULAR_VALIGN_BOTTOM:
5826 status.setEnabled(tabular.tabular_width.zero());
5827 status.setOnOff(tabular.tabular_valignment
5828 == Tabular::LYX_VALIGN_BOTTOM);
5831 case Tabular::LONGTABULAR_ALIGN_LEFT:
5832 status.setOnOff(tabular.longtabular_alignment
5833 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5835 case Tabular::LONGTABULAR_ALIGN_CENTER:
5836 status.setOnOff(tabular.longtabular_alignment
5837 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5839 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5840 status.setOnOff(tabular.longtabular_alignment
5841 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5844 case Tabular::UNSET_ROTATE_TABULAR:
5845 status.setOnOff(tabular.rotate == 0);
5848 case Tabular::TOGGLE_ROTATE_CELL:
5849 case Tabular::SET_ROTATE_CELL:
5850 status.setOnOff(!oneCellHasRotationState(false,
5851 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5854 case Tabular::UNSET_ROTATE_CELL:
5855 status.setOnOff(!oneCellHasRotationState(true,
5856 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5859 case Tabular::SET_USEBOX:
5860 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5863 // every row can only be one thing:
5864 // either a footer or header
5865 case Tabular::SET_LTFIRSTHEAD:
5866 status.setEnabled(sel_row_start == sel_row_end);
5867 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5870 case Tabular::UNSET_LTFIRSTHEAD:
5871 status.setEnabled(sel_row_start == sel_row_end);
5872 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5875 case Tabular::SET_LTHEAD:
5876 status.setEnabled(sel_row_start == sel_row_end);
5877 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5880 case Tabular::UNSET_LTHEAD:
5881 status.setEnabled(sel_row_start == sel_row_end);
5882 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5885 case Tabular::SET_LTFOOT:
5886 status.setEnabled(sel_row_start == sel_row_end);
5887 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5890 case Tabular::UNSET_LTFOOT:
5891 status.setEnabled(sel_row_start == sel_row_end);
5892 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5895 case Tabular::SET_LTLASTFOOT:
5896 status.setEnabled(sel_row_start == sel_row_end);
5897 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5900 case Tabular::UNSET_LTLASTFOOT:
5901 status.setEnabled(sel_row_start == sel_row_end);
5902 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5905 case Tabular::SET_LTNEWPAGE:
5906 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5908 case Tabular::UNSET_LTNEWPAGE:
5909 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5912 // only one row in head/firsthead/foot/lasthead can be the caption
5913 // and a multirow cannot be set as caption
5914 case Tabular::SET_LTCAPTION:
5915 status.setEnabled(sel_row_start == sel_row_end
5916 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5917 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5918 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5919 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5920 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5921 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5922 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5923 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5924 && !tabular.isMultiRow(sel_row_start));
5925 status.setOnOff(tabular.ltCaption(sel_row_start));
5928 case Tabular::UNSET_LTCAPTION:
5929 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5932 case Tabular::TOGGLE_LTCAPTION:
5933 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5934 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5935 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5936 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5937 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5938 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5939 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5940 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5941 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5942 status.setOnOff(tabular.ltCaption(sel_row_start));
5945 case Tabular::TOGGLE_BOOKTABS:
5946 case Tabular::SET_BOOKTABS:
5947 status.setOnOff(tabular.use_booktabs);
5950 case Tabular::UNSET_BOOKTABS:
5951 status.setOnOff(!tabular.use_booktabs);
5956 status.setEnabled(false);
5963 // function sets an object as defined in FuncStatus.h:
5964 // states OK, Unknown, Disabled, On, Off.
5965 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5966 FuncStatus & status) const
5968 switch (cmd.action()) {
5969 case LFUN_INSET_MODIFY:
5970 if (cmd.getArg(0) != "tabular")
5972 if (cmd.getArg(1) == "for-dialog") {
5973 // The dialog is asking the status of a command
5974 if (&cur.inset() != this)
5976 string action = cmd.getArg(2);
5977 string arg = cmd.getLongArg(3);
5978 return getFeatureStatus(cur, action, arg, status);
5980 // We always enable the lfun if it is coming from the dialog
5981 // because the dialog makes sure all the settings are valid,
5982 // even though the first argument might not be valid now.
5983 status.setEnabled(true);
5987 case LFUN_TABULAR_FEATURE: {
5988 if (&cur.inset() != this)
5990 string action = cmd.getArg(0);
5991 string arg = cmd.getLongArg(1);
5992 return getFeatureStatus(cur, action, arg, status);
5995 case LFUN_CAPTION_INSERT: {
5996 // caption is only allowed in caption cell of longtable
5997 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5998 status.setEnabled(false);
6001 // only standard caption is allowed
6002 string arg = cmd.getArg(0);
6003 if (!arg.empty() && arg != "Standard") {
6004 status.setEnabled(false);
6007 // check if there is already a caption
6008 bool have_caption = false;
6009 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
6010 ParagraphList::const_iterator pit = itc.paragraphs().begin();
6011 ParagraphList::const_iterator pend = itc.paragraphs().end();
6012 for (; pit != pend; ++pit) {
6013 InsetList::const_iterator it = pit->insetList().begin();
6014 InsetList::const_iterator end = pit->insetList().end();
6015 for (; it != end; ++it) {
6016 if (it->inset->lyxCode() == CAPTION_CODE) {
6017 have_caption = true;
6022 status.setEnabled(!have_caption);
6026 // These are only enabled inside tabular
6027 case LFUN_CELL_BACKWARD:
6028 case LFUN_CELL_FORWARD:
6029 status.setEnabled(true);
6032 // disable these with multiple cells selected
6033 case LFUN_INSET_INSERT:
6034 case LFUN_TABULAR_INSERT:
6035 case LFUN_TABULAR_STYLE_INSERT:
6036 case LFUN_FLEX_INSERT:
6037 case LFUN_FLOAT_INSERT:
6038 case LFUN_FLOAT_WIDE_INSERT:
6039 case LFUN_FOOTNOTE_INSERT:
6040 case LFUN_MARGINALNOTE_INSERT:
6041 case LFUN_MATH_INSERT:
6042 case LFUN_MATH_MODE:
6043 case LFUN_MATH_MUTATE:
6044 case LFUN_MATH_DISPLAY:
6045 case LFUN_NOTE_INSERT:
6046 case LFUN_ARGUMENT_INSERT:
6047 case LFUN_BOX_INSERT:
6048 case LFUN_BRANCH_INSERT:
6049 case LFUN_PHANTOM_INSERT:
6050 case LFUN_WRAP_INSERT:
6051 case LFUN_PREVIEW_INSERT:
6052 case LFUN_ERT_INSERT: {
6053 if (cur.selIsMultiCell()) {
6054 status.setEnabled(false);
6057 return cell(cur.idx())->getStatus(cur, cmd, status);
6060 case LFUN_CHANGE_ACCEPT:
6061 case LFUN_CHANGE_REJECT: {
6062 if (cur.selIsMultiCell()) {
6065 getSelection(cur, rs, re, cs, ce);
6066 for (row_type r = rs; r <= re; ++r) {
6067 if (tabular.row_info[r].change.changed()) {
6068 status.setEnabled(true);
6071 for (col_type c = cs; c <= ce; ++c) {
6072 if (tabular.column_info[c].change.changed()) {
6073 status.setEnabled(true);
6079 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
6080 status.setEnabled(true);
6083 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
6084 status.setEnabled(true);
6088 return cell(cur.idx())->getStatus(cur, cmd, status);
6091 case LFUN_PARAGRAPH_BREAK:
6092 case LFUN_NEWLINE_INSERT:
6093 return cell(cur.idx())->getStatus(cur, cmd, status);
6095 case LFUN_NEWPAGE_INSERT:
6096 status.setEnabled(false);
6100 if (tabularStackDirty() && theClipboard().isInternal()) {
6101 if (cur.selIsMultiCell()) {
6104 getSelection(cur, rs, re, cs, ce);
6105 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
6106 && paste_tabular->nrows() == re - rs + 1)
6107 status.setEnabled(true);
6109 status.setEnabled(false);
6110 status.message(_("Selection size should match clipboard content."));
6113 status.setEnabled(true);
6116 return cell(cur.idx())->getStatus(cur, cmd, status);
6118 case LFUN_INSET_SETTINGS:
6119 // relay this lfun to Inset, not to the cell.
6120 return Inset::getStatus(cur, cmd, status);
6123 // we try to handle this event in the insets dispatch function.
6124 return cell(cur.idx())->getStatus(cur, cmd, status);
6130 Inset::RowFlags InsetTabular::rowFlags() const
6132 if (tabular.is_long_tabular) {
6133 switch (tabular.longtabular_alignment) {
6134 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
6135 return Display | AlignLeft;
6136 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
6138 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
6139 return Display | AlignRight;
6148 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
6150 tabular.latex(os, runparams);
6154 int InsetTabular::plaintext(odocstringstream & os,
6155 OutputParams const & runparams, size_t max_length) const
6157 os << '\n'; // output table on a new line
6158 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
6159 tabular.plaintext(os, runparams, dp, false, 0, max_length);
6160 return PLAINTEXT_NEWLINE;
6164 void InsetTabular::docbook(XMLStream & xs, OutputParams const & runparams) const
6166 tabular.docbook(xs, runparams);
6170 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6172 return tabular.xhtml(xs, rp);
6176 void InsetTabular::validate(LaTeXFeatures & features) const
6178 tabular.validate(features);
6179 features.useInsetLayout(getLayout());
6183 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6185 return tabular.cellInset(idx);
6189 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6191 return tabular.cellInset(idx);
6195 void InsetTabular::cursorPos(BufferView const & bv,
6196 CursorSlice const & sl, bool boundary, int & x, int & y) const
6198 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6200 // y offset correction
6201 y += cellYPos(sl.idx());
6202 y += tabular.textVOffset(sl.idx());
6203 y += tabular.offsetVAlignment();
6205 // x offset correction
6206 x += cellXPos(sl.idx());
6207 x += tabular.textHOffset(sl.idx());
6208 x += ADD_TO_TABULAR_WIDTH;
6212 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6216 Inset const & inset = *tabular.cellInset(cell);
6217 Point o = bv.coordCache().getInsets().xy(&inset);
6218 int const xbeg = o.x_ - tabular.textHOffset(cell);
6219 int const xend = xbeg + tabular.cellWidth(cell);
6220 row_type const row = tabular.cellRow(cell);
6221 int const ybeg = o.y_ - tabular.rowAscent(row)
6222 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6223 int const yend = ybeg + tabular.cellHeight(cell);
6235 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6236 // << " ybeg=" << ybeg << " yend=" << yend
6237 // << " xx=" << xx << " yy=" << yy
6238 // << " dist=" << xx + yy << endl;
6243 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6245 //lyxerr << "InsetTabular::editXY: " << this << endl;
6247 cur.idx() = getNearestCell(cur.bv(), x, y);
6248 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6252 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6254 cur.idx() = getNearestCell(cur.bv(), x, y);
6255 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6259 idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6261 idx_type idx_min = 0;
6262 int dist_min = numeric_limits<int>::max();
6263 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6264 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6265 int const d = dist(bv, i, x, y);
6276 int InsetTabular::cellYPos(idx_type const cell) const
6278 row_type row = tabular.cellRow(cell);
6280 for (row_type r = 0; r < row; ++r)
6281 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6282 + tabular.interRowSpace(r + 1);
6287 int InsetTabular::cellXPos(idx_type const cell) const
6289 col_type col = tabular.cellColumn(cell);
6291 for (col_type c = 0; c < col; ++c)
6292 lx += tabular.column_info[c].width;
6297 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6299 row_type const row = tabular.cellRow(cur.idx());
6300 col_type const col = tabular.cellColumn(cur.idx());
6302 if (isRightToLeft(cur)) {
6303 if (tabular.cellColumn(cur.idx()) == 0) {
6304 if (row == tabular.nrows() - 1)
6306 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6311 cur.idx() = tabular.getLastCellInRow(row - 1);
6313 cur.idx() = tabular.cellIndex(row, col - 1);
6316 if (tabular.isLastCell(cur.idx()))
6318 if (cur.idx() == tabular.getLastCellInRow(row))
6319 cur.idx() = tabular.cellIndex(row + 1, 0);
6321 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6322 cur.idx() = tabular.cellIndex(row, colnextcell);
6326 cur.boundary(false);
6328 if (cur.selIsMultiCell()) {
6329 cur.pit() = cur.lastpit();
6330 cur.pos() = cur.lastpos();
6337 // in visual mode, place cursor at extreme left or right
6339 switch(entry_from) {
6341 case ENTRY_DIRECTION_RIGHT:
6342 cur.posVisToRowExtremity(false /* !left */);
6344 case ENTRY_DIRECTION_LEFT:
6345 cur.posVisToRowExtremity(true /* left */);
6347 case ENTRY_DIRECTION_IGNORE:
6348 // nothing to do in this case
6352 cur.setCurrentFont();
6356 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6358 row_type const row = tabular.cellRow(cur.idx());
6359 col_type const col = tabular.cellColumn(cur.idx());
6361 if (isRightToLeft(cur)) {
6362 if (cur.idx() == tabular.getLastCellInRow(row)) {
6365 cur.idx() = tabular.getFirstCellInRow(row);
6366 cur.idx() = tabular.cellAbove(cur.idx());
6368 if (tabular.isLastCell(cur.idx()))
6370 if (cur.idx() == tabular.getLastCellInRow(row))
6371 cur.idx() = tabular.cellIndex(row + 1, 0);
6373 cur.idx() = tabular.cellIndex(row, col + 1);
6376 if (cur.idx() == 0) // first cell
6379 cur.idx() = tabular.getLastCellInRow(row - 1);
6381 cur.idx() = tabular.cellIndex(row, col - 1);
6384 if (cur.selIsMultiCell()) {
6385 cur.pit() = cur.lastpit();
6386 cur.pos() = cur.lastpos();
6390 cur.pit() = cur.lastpit();
6391 cur.pos() = cur.lastpos();
6393 // in visual mode, place cursor at extreme left or right
6395 switch(entry_from) {
6397 case ENTRY_DIRECTION_RIGHT:
6398 cur.posVisToRowExtremity(false /* !left */);
6400 case ENTRY_DIRECTION_LEFT:
6401 cur.posVisToRowExtremity(true /* left */);
6403 case ENTRY_DIRECTION_IGNORE:
6404 // nothing to do in this case
6408 cur.setCurrentFont();
6412 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6414 cur.recordUndoInset(this);
6416 istringstream is(argument);
6417 // limit the size of strings we read to avoid memory problems
6421 size_t safe_guard = 0;
6426 if (safe_guard > 1000) {
6427 LYXERR0("parameter max count reached!");
6431 Tabular::Feature action = Tabular::LAST_ACTION;
6434 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6435 if (s != tabularFeature[i].feature)
6437 action = tabularFeature[i].action;
6440 if (action == Tabular::LAST_ACTION) {
6441 LYXERR0("Feature not found " << s);
6445 if (tabularFeature[i].need_value)
6447 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6448 tabularFeatures(cur, action, val);
6453 static void checkLongtableSpecial(Tabular::ltType & ltt,
6454 string const & special, bool & flag)
6456 if (special == "dl_above") {
6459 } else if (special == "dl_below") {
6460 ltt.bottomDL = flag;
6462 } else if (special == "empty") {
6472 bool InsetTabular::oneCellHasRotationState(bool rotated,
6473 row_type row_start, row_type row_end,
6474 col_type col_start, col_type col_end) const
6476 for (row_type r = row_start; r <= row_end; ++r)
6477 for (col_type c = col_start; c <= col_end; ++c)
6479 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6482 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6489 void InsetTabular::tabularFeatures(Cursor & cur,
6490 Tabular::Feature feature, string const & value)
6492 col_type sel_col_start;
6493 col_type sel_col_end;
6494 row_type sel_row_start;
6495 row_type sel_row_end;
6496 bool setLines = false;
6497 bool setLinesInnerOnly = false;
6498 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6499 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6503 case Tabular::M_ALIGN_LEFT:
6504 case Tabular::ALIGN_LEFT:
6505 setAlign = LYX_ALIGN_LEFT;
6508 case Tabular::M_ALIGN_RIGHT:
6509 case Tabular::ALIGN_RIGHT:
6510 setAlign = LYX_ALIGN_RIGHT;
6513 case Tabular::M_ALIGN_CENTER:
6514 case Tabular::ALIGN_CENTER:
6515 setAlign = LYX_ALIGN_CENTER;
6518 case Tabular::ALIGN_BLOCK:
6519 setAlign = LYX_ALIGN_BLOCK;
6522 case Tabular::ALIGN_DECIMAL:
6523 setAlign = LYX_ALIGN_DECIMAL;
6526 case Tabular::M_VALIGN_TOP:
6527 case Tabular::VALIGN_TOP:
6528 setVAlign = Tabular::LYX_VALIGN_TOP;
6531 case Tabular::M_VALIGN_BOTTOM:
6532 case Tabular::VALIGN_BOTTOM:
6533 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6536 case Tabular::M_VALIGN_MIDDLE:
6537 case Tabular::VALIGN_MIDDLE:
6538 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6545 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6546 row_type const row = tabular.cellRow(cur.idx());
6547 col_type const column = tabular.cellColumn(cur.idx());
6549 Tabular::ltType ltt;
6553 case Tabular::SET_TABULAR_WIDTH:
6554 tabular.setTabularWidth(Length(value));
6557 case Tabular::SET_PWIDTH: {
6558 Length const len(value);
6559 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6560 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6562 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6563 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6568 case Tabular::SET_MPWIDTH:
6569 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6572 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6573 bool const varwidth = value == "on";
6574 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6575 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6579 case Tabular::SET_MROFFSET:
6580 tabular.setMROffset(cur, cur.idx(), Length(value));
6583 case Tabular::SET_SPECIAL_COLUMN:
6584 case Tabular::SET_SPECIAL_MULTICOLUMN:
6585 if (value == "none")
6586 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6588 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6591 case Tabular::APPEND_ROW:
6592 // append the row into the tabular
6593 tabular.appendRow(row);
6596 case Tabular::APPEND_COLUMN:
6597 // append the column into the tabular
6598 tabular.appendColumn(column);
6599 cur.idx() = tabular.cellIndex(row, column);
6602 case Tabular::DELETE_ROW:
6603 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6604 for (col_type c = 0; c < tabular.ncols(); c++) {
6605 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6606 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6607 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6608 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6612 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6613 tabular.deleteRow(sel_row_start);
6614 if (sel_row_start >= tabular.nrows())
6616 cur.idx() = tabular.cellIndex(sel_row_start, column);
6619 cur.selection(false);
6622 case Tabular::DELETE_COLUMN:
6623 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6624 for (row_type r = 0; r < tabular.nrows(); r++)
6625 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6626 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6629 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6630 for (row_type r = 0; r < tabular.nrows(); r++)
6631 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6632 tabular.leftLine(tabular.cellIndex(r, 0)));
6635 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6636 tabular.deleteColumn(sel_col_start);
6637 if (sel_col_start >= tabular.ncols())
6639 cur.idx() = tabular.cellIndex(row, sel_col_start);
6642 cur.selection(false);
6645 case Tabular::COPY_ROW:
6646 tabular.copyRow(row);
6649 case Tabular::COPY_COLUMN:
6650 tabular.copyColumn(column);
6651 cur.idx() = tabular.cellIndex(row, column);
6654 case Tabular::MOVE_COLUMN_RIGHT:
6655 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::RIGHT);
6656 if (cur.selection()) {
6657 cur.selection(false);
6658 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start + 1);
6660 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end + 1);
6663 cur.idx() = tabular.cellIndex(row, column + 1);
6666 case Tabular::MOVE_COLUMN_LEFT:
6667 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::LEFT);
6668 if (cur.selection()) {
6669 cur.selection(false);
6670 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start - 1);
6672 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end - 1);
6675 cur.idx() = tabular.cellIndex(row, column - 1);
6678 case Tabular::MOVE_ROW_DOWN:
6679 tabular.moveRow(sel_row_start, sel_row_end, Tabular::DOWN);
6680 if (cur.selection()) {
6681 cur.selection(false);
6682 cur.idx() = tabular.cellIndex(sel_row_start + 1, sel_col_start);
6684 cur.idx() = tabular.cellIndex(sel_row_end + 1, sel_col_end);
6687 cur.idx() = tabular.cellIndex(row + 1, column);
6690 case Tabular::MOVE_ROW_UP:
6691 tabular.moveRow(sel_row_start, sel_row_end, Tabular::UP);
6692 if (cur.selection()) {
6693 cur.selection(false);
6694 cur.idx() = tabular.cellIndex(sel_row_start - 1, sel_col_start);
6696 cur.idx() = tabular.cellIndex(sel_row_end - 1, sel_col_end);
6699 cur.idx() = tabular.cellIndex(row - 1, column);
6702 case Tabular::SET_LINE_TOP:
6703 case Tabular::TOGGLE_LINE_TOP: {
6704 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6705 ? (value == "true") : !tabular.topLine(cur.idx());
6706 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6707 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6708 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6712 case Tabular::SET_LINE_BOTTOM:
6713 case Tabular::TOGGLE_LINE_BOTTOM: {
6714 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6715 ? (value == "true") : !tabular.bottomLine(cur.idx());
6716 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6717 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6718 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6722 case Tabular::SET_LTRIM_TOP:
6723 case Tabular::TOGGLE_LTRIM_TOP: {
6724 bool l = (feature == Tabular::SET_LTRIM_TOP)
6725 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6726 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6727 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6728 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6732 case Tabular::SET_RTRIM_TOP:
6733 case Tabular::TOGGLE_RTRIM_TOP: {
6734 bool l = (feature == Tabular::SET_RTRIM_TOP)
6735 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6736 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6737 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6738 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6742 case Tabular::SET_LTRIM_BOTTOM:
6743 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6744 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6745 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6746 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6747 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6748 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6752 case Tabular::SET_RTRIM_BOTTOM:
6753 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6754 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6755 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6756 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6757 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6758 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6762 case Tabular::SET_LINE_LEFT:
6763 case Tabular::TOGGLE_LINE_LEFT: {
6764 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6765 ? (value == "true") : !tabular.leftLine(cur.idx());
6766 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6767 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6768 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6772 case Tabular::SET_LINE_RIGHT:
6773 case Tabular::TOGGLE_LINE_RIGHT: {
6774 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6775 ? (value == "true") : !tabular.rightLine(cur.idx());
6776 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6777 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6778 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6782 case Tabular::M_ALIGN_LEFT:
6783 case Tabular::M_ALIGN_RIGHT:
6784 case Tabular::M_ALIGN_CENTER:
6785 case Tabular::ALIGN_LEFT:
6786 case Tabular::ALIGN_RIGHT:
6787 case Tabular::ALIGN_CENTER:
6788 case Tabular::ALIGN_BLOCK:
6789 case Tabular::ALIGN_DECIMAL:
6790 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6791 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6792 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6793 !tabular.getPWidth(c).zero());
6796 case Tabular::M_VALIGN_TOP:
6797 case Tabular::M_VALIGN_BOTTOM:
6798 case Tabular::M_VALIGN_MIDDLE:
6801 case Tabular::VALIGN_TOP:
6802 case Tabular::VALIGN_BOTTOM:
6803 case Tabular::VALIGN_MIDDLE:
6804 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6805 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6806 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6809 case Tabular::SET_MULTICOLUMN: {
6810 if (!cur.selection()) {
6811 // just multicol for one single cell
6812 // check whether we are completely in a multicol
6813 if (!tabular.isMultiColumn(cur.idx()))
6814 tabular.setMultiColumn(cur, cur.idx(), 1,
6815 tabular.rightLine(cur.idx()));
6818 // we have a selection so this means we just add all these
6819 // cells to form a multicolumn cell
6820 idx_type const s_start = cur.selBegin().idx();
6821 row_type const col_start = tabular.cellColumn(s_start);
6822 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6823 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6824 tabular.rightLine(cur.selEnd().idx()));
6827 cur.selection(false);
6831 case Tabular::UNSET_MULTICOLUMN: {
6832 if (!cur.selection()) {
6833 if (tabular.isMultiColumn(cur.idx()))
6834 tabular.unsetMultiColumn(cur.idx());
6839 case Tabular::MULTICOLUMN: {
6840 if (!cur.selection()) {
6841 if (tabular.isMultiColumn(cur.idx()))
6842 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6844 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6848 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6849 row_type const r = sel_row_start;
6850 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6851 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6854 // If the selection contains at least one singlecol cell
6855 // or multiple multicol cells,
6856 // we assume the user will merge is to a single multicol
6858 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6860 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6864 case Tabular::SET_MULTIROW: {
6865 if (!cur.selection()) {
6866 // just multirow for one single cell
6867 // check whether we are completely in a multirow
6868 if (!tabular.isMultiRow(cur.idx()))
6869 tabular.setMultiRow(cur, cur.idx(), 1,
6870 tabular.bottomLine(cur.idx()),
6871 tabular.getAlignment(cur.idx()));
6874 // we have a selection so this means we just add all this
6875 // cells to form a multirow cell
6876 idx_type const s_start = cur.selBegin().idx();
6877 row_type const row_start = tabular.cellRow(s_start);
6878 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6879 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6880 tabular.bottomLine(cur.selEnd().idx()),
6881 tabular.getAlignment(cur.selEnd().idx()));
6884 cur.selection(false);
6888 case Tabular::UNSET_MULTIROW: {
6889 if (!cur.selection()) {
6890 if (tabular.isMultiRow(cur.idx()))
6891 tabular.unsetMultiRow(cur.idx());
6896 case Tabular::MULTIROW: {
6897 if (!cur.selection()) {
6898 if (tabular.isMultiRow(cur.idx()))
6899 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6901 tabularFeatures(cur, Tabular::SET_MULTIROW);
6905 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6906 col_type const c = sel_col_start;
6907 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6908 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6911 // If the selection contains at least one singlerow cell
6912 // or multiple multirow cells,
6913 // we assume the user will merge is to a single multirow
6915 tabularFeatures(cur, Tabular::SET_MULTIROW);
6917 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6921 case Tabular::SET_INNER_LINES:
6922 setLinesInnerOnly = true;
6924 case Tabular::SET_ALL_LINES:
6927 case Tabular::UNSET_ALL_LINES:
6928 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6929 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6930 idx_type const cell = tabular.cellIndex(r, c);
6931 if (!setLinesInnerOnly || r != sel_row_start)
6932 tabular.setTopLine(cell, setLines);
6933 if ((!setLinesInnerOnly || r != sel_row_end)
6934 && (!setLines || r == sel_row_end))
6935 tabular.setBottomLine(cell, setLines);
6936 if ((!setLinesInnerOnly || c != sel_col_end)
6937 && (!setLines || c == sel_col_end))
6938 tabular.setRightLine(cell, setLines);
6939 if ((!setLinesInnerOnly || c != sel_col_start))
6940 tabular.setLeftLine(cell, setLines);
6944 case Tabular::RESET_FORMAL_DEFAULT:
6945 for (row_type r = 0; r < tabular.nrows(); ++r) {
6946 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6947 for (col_type c = 0; c < tabular.ncols(); ++c) {
6948 idx_type const cell = tabular.cellIndex(r, c);
6949 tabular.setTopLine(cell, head_or_foot);
6950 tabular.setBottomLine(cell, head_or_foot);
6955 case Tabular::SET_BORDER_LINES:
6956 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6957 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6958 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6960 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6961 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6962 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6966 case Tabular::TOGGLE_LONGTABULAR:
6967 if (tabular.is_long_tabular)
6968 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6970 tabular.is_long_tabular = true;
6973 case Tabular::SET_LONGTABULAR:
6974 tabular.is_long_tabular = true;
6977 case Tabular::UNSET_LONGTABULAR:
6978 for (row_type r = 0; r < tabular.nrows(); ++r) {
6979 if (tabular.ltCaption(r)) {
6980 cur.idx() = tabular.cellIndex(r, 0);
6983 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6986 tabular.is_long_tabular = false;
6989 case Tabular::SET_ROTATE_TABULAR:
6990 tabular.rotate = convert<int>(value);
6993 case Tabular::UNSET_ROTATE_TABULAR:
6997 case Tabular::TOGGLE_ROTATE_TABULAR:
6998 // when pressing the rotate button we default to 90° rotation
6999 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
7002 case Tabular::TABULAR_VALIGN_TOP:
7003 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
7006 case Tabular::TABULAR_VALIGN_MIDDLE:
7007 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
7010 case Tabular::TABULAR_VALIGN_BOTTOM:
7011 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
7014 case Tabular::LONGTABULAR_ALIGN_LEFT:
7015 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
7018 case Tabular::LONGTABULAR_ALIGN_CENTER:
7019 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
7022 case Tabular::LONGTABULAR_ALIGN_RIGHT:
7023 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
7026 case Tabular::SET_ROTATE_CELL:
7027 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7028 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7029 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
7032 case Tabular::UNSET_ROTATE_CELL:
7033 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7034 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7035 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7038 case Tabular::TOGGLE_ROTATE_CELL:
7040 bool oneNotRotated = oneCellHasRotationState(false,
7041 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
7043 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7044 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
7045 // when pressing the rotate cell button we default to 90° rotation
7047 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
7049 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7054 case Tabular::SET_USEBOX: {
7055 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
7056 if (val == tabular.getUsebox(cur.idx()))
7057 val = Tabular::BOX_NONE;
7058 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7059 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7060 tabular.setUsebox(tabular.cellIndex(r, c), val);
7064 case Tabular::UNSET_LTFIRSTHEAD:
7067 case Tabular::SET_LTFIRSTHEAD:
7068 tabular.getRowOfLTFirstHead(row, ltt);
7069 checkLongtableSpecial(ltt, value, flag);
7070 tabular.setLTHead(row, flag, ltt, true);
7073 case Tabular::UNSET_LTHEAD:
7076 case Tabular::SET_LTHEAD:
7077 tabular.getRowOfLTHead(row, ltt);
7078 checkLongtableSpecial(ltt, value, flag);
7079 tabular.setLTHead(row, flag, ltt, false);
7082 case Tabular::UNSET_LTFOOT:
7085 case Tabular::SET_LTFOOT:
7086 tabular.getRowOfLTFoot(row, ltt);
7087 checkLongtableSpecial(ltt, value, flag);
7088 tabular.setLTFoot(row, flag, ltt, false);
7091 case Tabular::UNSET_LTLASTFOOT:
7094 case Tabular::SET_LTLASTFOOT:
7095 tabular.getRowOfLTLastFoot(row, ltt);
7096 checkLongtableSpecial(ltt, value, flag);
7097 tabular.setLTFoot(row, flag, ltt, true);
7100 case Tabular::UNSET_LTNEWPAGE:
7103 case Tabular::SET_LTNEWPAGE:
7104 tabular.setLTNewPage(row, flag);
7107 case Tabular::SET_LTCAPTION: {
7108 if (tabular.ltCaption(row))
7110 cur.idx() = tabular.setLTCaption(cur, row, true);
7113 cur.selection(false);
7114 // If a row is set as caption, then also insert
7115 // a caption. Otherwise the LaTeX output is broken.
7116 // Select cell if it is non-empty
7117 if (cur.lastpos() > 0 || cur.lastpit() > 0)
7118 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
7119 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
7123 case Tabular::UNSET_LTCAPTION: {
7124 if (!tabular.ltCaption(row))
7126 cur.idx() = tabular.setLTCaption(cur, row, false);
7129 cur.selection(false);
7130 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
7131 if (lyx::getStatus(fr).enabled())
7136 case Tabular::TOGGLE_LTCAPTION: {
7137 if (tabular.ltCaption(row))
7138 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
7140 tabularFeatures(cur, Tabular::SET_LTCAPTION);
7144 case Tabular::TOGGLE_BOOKTABS:
7145 tabular.use_booktabs = !tabular.use_booktabs;
7148 case Tabular::SET_BOOKTABS:
7149 tabular.use_booktabs = true;
7152 case Tabular::UNSET_BOOKTABS:
7153 tabular.use_booktabs = false;
7156 case Tabular::SET_TOP_SPACE: {
7158 if (value == "default")
7159 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7160 tabular.row_info[r].top_space_default = true;
7161 else if (value == "none")
7162 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7163 tabular.row_info[r].top_space_default = false;
7164 tabular.row_info[r].top_space = len;
7166 else if (isValidLength(value, &len))
7167 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7168 tabular.row_info[r].top_space_default = false;
7169 tabular.row_info[r].top_space = len;
7174 case Tabular::SET_BOTTOM_SPACE: {
7176 if (value == "default")
7177 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7178 tabular.row_info[r].bottom_space_default = true;
7179 else if (value == "none")
7180 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7181 tabular.row_info[r].bottom_space_default = false;
7182 tabular.row_info[r].bottom_space = len;
7184 else if (isValidLength(value, &len))
7185 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7186 tabular.row_info[r].bottom_space_default = false;
7187 tabular.row_info[r].bottom_space = len;
7192 case Tabular::SET_INTERLINE_SPACE: {
7194 if (value == "default")
7195 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7196 tabular.row_info[r].interline_space_default = true;
7197 else if (value == "none")
7198 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7199 tabular.row_info[r].interline_space_default = false;
7200 tabular.row_info[r].interline_space = len;
7202 else if (isValidLength(value, &len))
7203 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7204 tabular.row_info[r].interline_space_default = false;
7205 tabular.row_info[r].interline_space = len;
7210 case Tabular::SET_DECIMAL_POINT:
7211 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7212 tabular.column_info[c].decimal_point = from_utf8(value);
7215 // dummy stuff just to avoid warnings
7216 case Tabular::LAST_ACTION:
7222 bool InsetTabular::copySelection(Cursor & cur)
7224 if (!cur.selection())
7229 getSelection(cur, rs, re, cs, ce);
7231 paste_tabular.reset(new Tabular(tabular));
7233 for (row_type r = 0; r < rs; ++r)
7234 paste_tabular->deleteRow(0, true);
7236 row_type const rows = re - rs + 1;
7237 while (paste_tabular->nrows() > rows)
7238 paste_tabular->deleteRow(rows, true);
7240 for (col_type c = 0; c < cs; ++c)
7241 paste_tabular->deleteColumn(0, true);
7243 col_type const columns = ce - cs + 1;
7244 while (paste_tabular->ncols() > columns)
7245 paste_tabular->deleteColumn(columns, true);
7247 paste_tabular->setBuffer(tabular.buffer());
7249 odocstringstream os;
7250 OutputParams const runparams(nullptr);
7251 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7252 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7253 cap::copySelection(cur, os.str());
7255 // mark tabular stack dirty
7256 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7257 // when we (hopefully) have a one-for-all paste mechanism.
7258 // This must be called after cap::copySelection.
7259 dirtyTabularStack(true);
7265 bool InsetTabular::pasteClipboard(Cursor & cur)
7269 col_type actcol = tabular.cellColumn(cur.idx());
7270 row_type actrow = tabular.cellRow(cur.idx());
7272 if (cur.selIsMultiCell()) {
7275 getSelection(cur, actrow, re, actcol, ce);
7278 col_type const oldncols = tabular.ncols();
7279 for (row_type r1 = 0, r2 = actrow; r1 < paste_tabular->nrows(); ++r1, ++r2) {
7280 // Append rows if needed
7281 if (r2 == tabular.nrows())
7282 tabular.insertRow(r2 - 1, false);
7283 for (col_type c1 = 0, c2 = actcol; c1 < paste_tabular->ncols(); ++c1, ++c2) {
7284 // Append columns if needed
7285 if (c2 == tabular.ncols())
7286 tabular.insertColumn(c2 - 1, false);
7287 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7288 tabular.isPartOfMultiColumn(r2, c2))
7290 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7294 if (tabular.isPartOfMultiColumn(r2, c2)) {
7298 shared_ptr<InsetTableCell> inset(
7299 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7300 tabular.setCellInset(r2, c2, inset);
7301 // FIXME?: why do we need to do this explicitly? (EL)
7302 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7304 if (!lyxrc.ct_markup_copied) {
7305 // do not paste deleted text
7306 inset->acceptChanges();
7307 inset->setChange(Change(buffer().params().track_changes ?
7308 Change::INSERTED : Change::UNCHANGED));
7314 // amend cursor position if cols have been appended
7315 cur.idx() += actrow * (tabular.ncols() - oldncols);
7320 void InsetTabular::cutSelection(Cursor & cur)
7322 if (!cur.selection())
7327 getSelection(cur, rs, re, cs, ce);
7328 for (row_type r = rs; r <= re; ++r) {
7329 for (col_type c = cs; c <= ce; ++c) {
7330 shared_ptr<InsetTableCell> t
7331 = cell(tabular.cellIndex(r, c));
7332 if (buffer().params().track_changes)
7333 // FIXME: Change tracking (MG)
7334 t->setChange(Change(Change::DELETED));
7340 // cursor position might be invalid now
7341 if (cur.pit() > cur.lastpit())
7342 cur.pit() = cur.lastpit();
7343 if (cur.pos() > cur.lastpos())
7344 cur.pos() = cur.lastpos();
7345 cur.clearSelection();
7349 bool InsetTabular::isRightToLeft(Cursor & cur) const
7351 // LASSERT: It might be better to abandon this Buffer.
7352 LASSERT(cur.depth() > 1, return false);
7353 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7354 pos_type const parentpos = cur[cur.depth() - 2].pos();
7355 return parentpar.getFontSettings(buffer().params(),
7356 parentpos).language()->rightToLeft();
7360 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7363 LASSERT(stidx <= enidx, return docstring());
7365 col_type const col1 = tabular.cellColumn(stidx);
7366 col_type const col2 = tabular.cellColumn(enidx);
7367 row_type const row1 = tabular.cellRow(stidx);
7368 row_type const row2 = tabular.cellRow(enidx);
7370 for (col_type col = col1; col <= col2; col++)
7371 for (row_type row = row1; row <= row2; row++) {
7376 retval += tabular.cellInset(row, col)->asString(intoInsets);
7382 ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
7384 LASSERT(stidx <= enidx, return ParagraphList());
7385 ParagraphList retval;
7386 col_type const col1 = tabular.cellColumn(stidx);
7387 col_type const col2 = tabular.cellColumn(enidx);
7388 row_type const row1 = tabular.cellRow(stidx);
7389 row_type const row2 = tabular.cellRow(enidx);
7390 for (col_type col = col1; col <= col2; col++)
7391 for (row_type row = row1; row <= row2; row++)
7392 for (auto const & par : tabular.cellInset(row, col)->paragraphs())
7393 retval.push_back(par);
7398 void InsetTabular::getSelection(Cursor & cur,
7399 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7401 CursorSlice const & beg = cur.selBegin();
7402 CursorSlice const & end = cur.selEnd();
7403 cs = tabular.cellColumn(beg.idx());
7404 ce = tabular.cellColumn(end.idx());
7408 rs = tabular.cellRow(beg.idx());
7409 re = tabular.cellRow(end.idx());
7415 Text * InsetTabular::getText(int idx) const
7417 return size_t(idx) < nargs() ? cell(idx)->getText(0) : nullptr;
7421 bool InsetTabular::isChanged() const
7423 for (idx_type idx = 0; idx < nargs(); ++idx) {
7424 if (cell(idx)->isChanged())
7426 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7428 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7435 void InsetTabular::setChange(Change const & change)
7437 for (idx_type idx = 0; idx < nargs(); ++idx)
7438 cell(idx)->setChange(change);
7442 void InsetTabular::acceptChanges()
7444 for (idx_type idx = 0; idx < nargs(); ++idx)
7445 cell(idx)->acceptChanges();
7446 for (row_type row = 0; row < tabular.nrows(); ++row) {
7447 if (tabular.row_info[row].change.inserted())
7448 tabular.row_info[row].change.setUnchanged();
7449 else if (tabular.row_info[row].change.deleted())
7450 tabular.deleteRow(row, true);
7452 for (col_type col = 0; col < tabular.ncols(); ++col) {
7453 if (tabular.column_info[col].change.inserted())
7454 tabular.column_info[col].change.setUnchanged();
7455 else if (tabular.column_info[col].change.deleted())
7456 tabular.deleteColumn(col, true);
7458 tabular.updateIndexes();
7462 void InsetTabular::rejectChanges()
7464 for (idx_type idx = 0; idx < nargs(); ++idx)
7465 cell(idx)->rejectChanges();
7466 for (row_type row = 0; row < tabular.nrows(); ++row) {
7467 if (tabular.row_info[row].change.deleted())
7468 tabular.row_info[row].change.setUnchanged();
7469 else if (tabular.row_info[row].change.inserted())
7470 tabular.deleteRow(row, true);
7472 for (col_type col = 0; col < tabular.ncols(); ++col) {
7473 if (tabular.column_info[col].change.deleted())
7474 tabular.column_info[col].change.setUnchanged();
7475 else if (tabular.column_info[col].change.inserted())
7476 tabular.deleteColumn(col, true);
7478 tabular.updateIndexes();
7482 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7484 return tabular.getPWidth(cell).zero();
7488 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7491 if (buf.length() <= 0)
7496 col_type maxCols = 1;
7497 size_t const len = buf.length();
7501 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7509 maxCols = max(cols, maxCols);
7515 maxCols = max(cols, maxCols);
7521 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7522 loctab = paste_tabular.get();
7523 dirtyTabularStack(true);
7526 cell = bv.cursor().idx();
7527 ocol = tabular.cellColumn(cell);
7528 row = tabular.cellRow(cell);
7532 idx_type cells = loctab->numberofcells;
7535 rows = loctab->nrows();
7536 col_type columns = loctab->ncols();
7539 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7545 // append column if necessary
7546 if (cols == columns) {
7547 loctab->appendColumn(cols - 1);
7548 columns = loctab->ncols();
7549 cells = loctab->numberofcells;
7552 if (cols < columns) {
7553 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7554 Font const font = bv.textMetrics(&inset->text()).
7556 inset->setText(buf.substr(op, p - op), font,
7557 buffer().params().track_changes);
7563 // we can only set this if we are not too far right
7564 if (cols < columns) {
7565 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7566 Font const font = bv.textMetrics(&inset->text()).
7568 inset->setText(buf.substr(op, p - op), font,
7569 buffer().params().track_changes);
7573 // append row if necessary
7574 if (row == rows && p < len - 1) {
7575 loctab->appendRow(row - 1);
7576 rows = loctab->nrows();
7577 cells = loctab->numberofcells;
7580 cell = loctab->cellIndex(row, cols);
7586 // check for the last cell if there is no trailing '\n'
7587 if (cell < cells && op < len) {
7588 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7589 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7590 inset->setText(buf.substr(op, len - op), font,
7591 buffer().params().track_changes);
7597 void InsetTabular::addPreview(DocIterator const & inset_pos,
7598 PreviewLoader & loader) const
7600 DocIterator cell_pos = inset_pos;
7602 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7603 for (row_type r = 0; r < tabular.nrows(); ++r) {
7604 for (col_type c = 0; c < tabular.ncols(); ++c) {
7605 cell_pos.top().idx() = tabular.cellIndex(r, c);
7606 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7612 bool InsetTabular::completionSupported(Cursor const & cur) const
7614 Cursor const & bvCur = cur.bv().cursor();
7615 if (&bvCur.inset() != this)
7617 return cur.text()->completionSupported(cur);
7621 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7623 return completionSupported(cur);
7627 bool InsetTabular::automaticInlineCompletion() const
7629 return lyxrc.completion_inline_text;
7633 bool InsetTabular::automaticPopupCompletion() const
7635 return lyxrc.completion_popup_text;
7639 bool InsetTabular::showCompletionCursor() const
7641 return lyxrc.completion_cursor_text &&
7642 (lyxrc.completion_inline_text || lyxrc.completion_popup_text);
7646 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7648 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : nullptr;
7652 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7654 if (!completionSupported(cur))
7656 return cur.text()->completionPrefix(cur);
7660 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7662 if (!completionSupported(cur))
7665 return cur.text()->insertCompletion(cur, s, finished);
7669 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7670 Dimension & dim) const
7672 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7673 tm.completionPosAndDim(cur, x, y, dim);
7677 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7679 istringstream data(in);
7681 lex.setStream(data);
7688 if (!lex || token != "tabular") {
7689 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7693 // This is part of the inset proper that is usually swallowed
7694 // by Buffer::readInset
7696 if (!lex || token != "Tabular") {
7697 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7705 string InsetTabular::params2string(InsetTabular const & inset)
7708 data << "tabular" << ' ';
7710 data << "\\end_inset\n";
7715 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7717 for (col_type c = 0; c < tabular.ncols(); ++c) {
7718 for (row_type r = 0; r < tabular.nrows(); ++r) {
7719 if (!tabular.isPartOfMultiColumn(r,c) &&
7720 !tabular.isPartOfMultiRow(r,c))
7723 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7724 ParagraphList::iterator it = parlist.begin();
7725 ParagraphList::iterator const en = parlist.end();
7726 for (; it != en; ++it)
7727 it->setLayout(dc.plainLayout());