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"
45 #include "OutputParams.h"
47 #include "output_xhtml.h"
48 #include "Paragraph.h"
49 #include "ParagraphParameters.h"
50 #include "ParIterator.h"
52 #include "texstream.h"
53 #include "TextClass.h"
54 #include "TextMetrics.h"
56 #include "frontends/Application.h"
57 #include "frontends/alert.h"
58 #include "frontends/Clipboard.h"
59 #include "frontends/Painter.h"
60 #include "frontends/Selection.h"
62 #include "support/convert.h"
63 #include "support/debug.h"
64 #include "support/docstream.h"
65 #include "support/FileName.h"
66 #include "support/gettext.h"
67 #include "support/lassert.h"
68 #include "support/lstrings.h"
69 #include "support/unique_ptr.h"
78 using namespace lyx::support;
84 using cap::dirtyTabularStack;
85 using cap::tabularStackDirty;
87 using graphics::PreviewLoader;
89 using frontend::Painter;
90 using frontend::Clipboard;
92 namespace Alert = frontend::Alert;
97 int const ADD_TO_HEIGHT = 2; // in cell
98 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
99 int const default_line_space = 10; // ?
100 int const WIDTH_OF_LINE = 5; // space between double lines
104 unique_ptr<Tabular> paste_tabular;
107 struct TabularFeature {
108 Tabular::Feature action;
114 TabularFeature tabularFeature[] =
116 // the SET/UNSET actions are used by the table dialog,
117 // the TOGGLE actions by the table toolbar buttons
118 // FIXME: these values have been hardcoded in InsetMathGrid and other
120 { Tabular::APPEND_ROW, "append-row", false },
121 { Tabular::APPEND_COLUMN, "append-column", false },
122 { Tabular::DELETE_ROW, "delete-row", false },
123 { Tabular::DELETE_COLUMN, "delete-column", false },
124 { Tabular::COPY_ROW, "copy-row", false },
125 { Tabular::COPY_COLUMN, "copy-column", false },
126 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
127 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
128 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
129 { Tabular::MOVE_ROW_UP, "move-row-up", false },
130 { Tabular::SET_LINE_TOP, "set-line-top", true },
131 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
132 { Tabular::SET_LTRIM_TOP, "set-ltrim-top", true },
133 { Tabular::SET_LTRIM_BOTTOM, "set-ltrim-bottom", true },
134 { Tabular::SET_RTRIM_TOP, "set-rtrim-top", true },
135 { Tabular::SET_RTRIM_BOTTOM, "set-rtrim-bottom", true },
136 { Tabular::SET_LINE_LEFT, "set-line-left", true },
137 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
138 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
139 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
140 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
141 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
142 { Tabular::TOGGLE_LTRIM_TOP, "toggle-ltrim-top", false },
143 { Tabular::TOGGLE_LTRIM_BOTTOM, "toggle-ltrim-bottom", false },
144 { Tabular::TOGGLE_RTRIM_TOP, "toggle-rtrim-top", false },
145 { Tabular::TOGGLE_RTRIM_BOTTOM, "toggle-rtrim-bottom", false },
146 { Tabular::ALIGN_LEFT, "align-left", false },
147 { Tabular::ALIGN_RIGHT, "align-right", false },
148 { Tabular::ALIGN_CENTER, "align-center", false },
149 { Tabular::ALIGN_BLOCK, "align-block", false },
150 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
151 { Tabular::VALIGN_TOP, "valign-top", false },
152 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
153 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
154 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
155 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
156 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
157 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
158 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
159 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
160 { Tabular::MULTICOLUMN, "multicolumn", false },
161 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
162 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
163 { Tabular::MULTIROW, "multirow", false },
164 { Tabular::SET_MULTIROW, "set-multirow", false },
165 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
166 { Tabular::SET_MROFFSET, "set-mroffset", true },
167 { Tabular::SET_ALL_LINES, "set-all-lines", false },
168 { Tabular::RESET_FORMAL_DEFAULT, "reset-formal-default", false },
169 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
170 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
171 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
172 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
173 { Tabular::SET_PWIDTH, "set-pwidth", true },
174 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
175 { Tabular::TOGGLE_VARWIDTH_COLUMN, "toggle-varwidth-column", true },
176 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
177 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
178 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
179 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
180 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
181 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
182 { Tabular::SET_USEBOX, "set-usebox", true },
183 { Tabular::SET_LTHEAD, "set-lthead", true },
184 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
185 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
186 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
187 { Tabular::SET_LTFOOT, "set-ltfoot", true },
188 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
189 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
190 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
191 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
192 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
193 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
194 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
195 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
196 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
197 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
198 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
199 { Tabular::SET_BOOKTABS, "set-booktabs", false },
200 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
201 { Tabular::SET_TOP_SPACE, "set-top-space", true },
202 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
203 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
204 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
205 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
206 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
207 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
208 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
209 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
210 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
211 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
212 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
213 { Tabular::SET_INNER_LINES, "set-inner-lines", false },
214 { Tabular::LAST_ACTION, "", false }
218 string const tostr(LyXAlignment const & num)
223 case LYX_ALIGN_BLOCK:
227 case LYX_ALIGN_CENTER:
229 case LYX_ALIGN_RIGHT:
231 case LYX_ALIGN_LAYOUT:
233 case LYX_ALIGN_SPECIAL:
235 case LYX_ALIGN_DECIMAL:
242 string const tostr(Tabular::HAlignment const & num)
245 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
247 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
249 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
256 string const tostr(Tabular::VAlignment const & num)
259 case Tabular::LYX_VALIGN_TOP:
261 case Tabular::LYX_VALIGN_MIDDLE:
263 case Tabular::LYX_VALIGN_BOTTOM:
270 string const tostr(Tabular::BoxType const & num)
273 case Tabular::BOX_NONE:
275 case Tabular::BOX_PARBOX:
277 case Tabular::BOX_MINIPAGE:
279 case Tabular::BOX_VARWIDTH:
286 // I would have liked a fromstr template a lot better. (Lgb)
287 bool string2type(string const & str, LyXAlignment & num)
290 num = LYX_ALIGN_NONE;
291 else if (str == "block")
292 num = LYX_ALIGN_BLOCK;
293 else if (str == "left")
294 num = LYX_ALIGN_LEFT;
295 else if (str == "center")
296 num = LYX_ALIGN_CENTER;
297 else if (str == "right")
298 num = LYX_ALIGN_RIGHT;
299 else if (str == "decimal")
300 num = LYX_ALIGN_DECIMAL;
307 bool string2type(string const & str, Tabular::HAlignment & num)
310 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
311 else if (str == "center" )
312 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
313 else if (str == "right")
314 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
321 bool string2type(string const & str, Tabular::VAlignment & num)
324 num = Tabular::LYX_VALIGN_TOP;
325 else if (str == "middle" )
326 num = Tabular::LYX_VALIGN_MIDDLE;
327 else if (str == "bottom")
328 num = Tabular::LYX_VALIGN_BOTTOM;
335 bool string2type(string const & str, Tabular::BoxType & num)
338 num = Tabular::BOX_NONE;
339 else if (str == "parbox")
340 num = Tabular::BOX_PARBOX;
341 else if (str == "minipage")
342 num = Tabular::BOX_MINIPAGE;
343 else if (str == "varwidth")
344 num = Tabular::BOX_VARWIDTH;
351 bool string2type(string const & str, bool & num)
355 else if (str == "false")
363 bool getTokenValue(string const & str, char const * token, string & ret)
366 size_t token_length = strlen(token);
367 size_t pos = str.find(token);
369 if (pos == string::npos || pos + token_length + 1 >= str.length()
370 || str[pos + token_length] != '=')
372 pos += token_length + 1;
374 if (ch != '"' && ch != '\'') { // only read till next space
378 while (pos < str.length() - 1 && str[++pos] != ch)
385 bool getTokenValue(string const & str, char const * token, docstring & ret)
388 bool const success = getTokenValue(str, token, tmp);
389 ret = from_utf8(tmp);
394 bool getTokenValue(string const & str, char const * token, int & num)
398 if (!getTokenValue(str, token, tmp))
400 num = convert<int>(tmp);
405 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token,
413 Tabular::HAlignment & num)
416 return getTokenValue(str, token, tmp) && string2type(tmp, num);
420 bool getTokenValue(string const & str, char const * token,
421 Tabular::VAlignment & num)
424 return getTokenValue(str, token, tmp) && string2type(tmp, num);
428 bool getTokenValue(string const & str, char const * token,
429 Tabular::BoxType & num)
432 return getTokenValue(str, token, tmp) && string2type(tmp, num);
436 bool getTokenValue(string const & str, char const * token, bool & flag)
438 // set the flag always to false as this should be the default for bools
439 // not in the file-format.
442 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
446 bool getTokenValue(string const & str, char const * token, Length & len)
448 // set the length to be zero() as default as this it should be if not
449 // in the file format.
452 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
456 bool getTokenValue(string const & str, char const * token, Change & change, BufferParams & bp)
458 // set the change to be Change() as default as this it should be if not
459 // in the file format.
462 if (getTokenValue(str, token, tmp)) {
463 vector<string> const changedata = getVectorFromString(tmp, " ");
464 if (changedata.size() != 3) {
465 Alert::warning(_("Change tracking data incomplete"),
466 _("Change tracking information for tabular row/column "
467 "is incomplete. I will ignore this."));
470 BufferParams::AuthorMap const & am = bp.author_map_;
471 int aid = convert<int>(changedata[1]);
472 if (am.find(aid) == am.end()) {
473 // FIXME Use ErrorList
474 Alert::warning(_("Change tracking author index missing"),
475 bformat(_("A change tracking author information for index "
476 "%1$d is missing. This can happen after a wrong "
477 "merge by a version control system. In this case, "
478 "either fix the merge, or have this information "
479 "missing until the corresponding tracked changes "
480 "are merged or this user edits the file again.\n"),
482 bp.addAuthor(Author(aid));
484 istringstream is(changedata[2]);
487 if (changedata[0] == "inserted") {
488 change = Change(Change::INSERTED, am.find(aid)->second, ct);
490 } else if (changedata[0] == "deleted") {
491 change = Change(Change::DELETED, am.find(aid)->second, ct);
499 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
504 if (!getTokenValue(str, token, tmp))
506 if (tmp == "default") {
510 return isValidLength(tmp, &len);
514 void l_getline(istream & is, string & str)
517 while (str.empty()) {
519 if (!str.empty() && str[str.length() - 1] == '\r')
520 str.erase(str.length() - 1);
525 string const write_attribute(string const & name, T const & t)
527 string const s = tostr(t);
528 return s.empty() ? s : " " + name + "=\"" + s + "\"";
532 string const write_attribute(string const & name, string const & t)
534 return t.empty() ? t : " " + name + "=\"" + t + "\"";
539 string const write_attribute(string const & name, docstring const & t)
541 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
546 string const write_attribute(string const & name, bool const & b)
548 // we write only true attribute values so we remove a bit of the
549 // file format bloat for tabulars.
550 return b ? write_attribute(name, convert<string>(b)) : string();
555 string const write_attribute(string const & name, int const & i)
557 // we write only true attribute values so we remove a bit of the
558 // file format bloat for tabulars.
559 return i ? write_attribute(name, convert<string>(i)) : string();
564 string const write_attribute(string const & name, Tabular::idx_type const & i)
566 // we write only true attribute values so we remove a bit of the
567 // file format bloat for tabulars.
568 return i ? write_attribute(name, convert<string>(i)) : string();
573 string const write_attribute(string const & name, Length const & value)
575 // we write only the value if we really have one same reason as above.
576 return value.zero() ? string() : write_attribute(name, value.asString());
579 string const write_attribute(string const & name, Change const & change, BufferParams const & bp)
581 odocstringstream ods;
582 if (change.inserted())
583 ods << from_ascii("inserted");
584 else if (change.deleted())
585 ods << from_ascii("deleted");
586 if (change.changed()) {
587 ods << " " << bp.authors().get(change.author).bufferId()
588 << " " << change.changetime;
589 return write_attribute(name, ods.str());
597 string const featureAsString(Tabular::Feature action)
599 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
600 if (tabularFeature[i].action == action)
601 return tabularFeature[i].feature;
607 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
609 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
610 for (; dit; dit.forwardChar())
611 if (dit.inTexted() && dit.depth() == 1
612 && dit.paragraph().find(align_d, false, false, dit.pos()))
619 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
621 InsetTableCell tail = InsetTableCell(head);
622 DocIterator const dit = separatorPos(&head, align_d);
623 hassep = static_cast<bool>(dit);
625 pos_type const psize = head.paragraphs().front().size();
626 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
627 tail.paragraphs().front().eraseChars(0,
628 dit.pos() < psize ? dit.pos() + 1 : psize, false);
635 /////////////////////////////////////////////////////////////////////
639 /////////////////////////////////////////////////////////////////////
642 Tabular::CellData::CellData(Buffer * buf)
645 multicolumn(Tabular::CELL_NORMAL),
646 multirow(Tabular::CELL_NORMAL),
647 alignment(LYX_ALIGN_CENTER),
648 valignment(LYX_VALIGN_TOP),
656 top_line_rtrimmed(false),
657 top_line_ltrimmed(false),
658 bottom_line_rtrimmed(false),
659 bottom_line_ltrimmed(false),
662 inset(new InsetTableCell(buf))
664 inset->setBuffer(*buf);
668 Tabular::CellData::CellData(CellData const & cs)
671 multicolumn(cs.multicolumn),
672 multirow(cs.multirow),
673 mroffset(cs.mroffset),
674 alignment(cs.alignment),
675 valignment(cs.valignment),
676 decimal_hoffset(cs.decimal_hoffset),
677 decimal_width(cs.decimal_width),
679 top_line(cs.top_line),
680 bottom_line(cs.bottom_line),
681 left_line(cs.left_line),
682 right_line(cs.right_line),
683 top_line_rtrimmed(cs.top_line_rtrimmed),
684 top_line_ltrimmed(cs.top_line_ltrimmed),
685 bottom_line_rtrimmed(cs.bottom_line_rtrimmed),
686 bottom_line_ltrimmed(cs.bottom_line_ltrimmed),
689 align_special(cs.align_special),
691 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
695 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
701 multicolumn = cs.multicolumn;
702 multirow = cs.multirow;
703 mroffset = cs.mroffset;
704 alignment = cs.alignment;
705 valignment = cs.valignment;
706 decimal_hoffset = cs.decimal_hoffset;
707 decimal_width = cs.decimal_width;
708 voffset = cs.voffset;
709 top_line = cs.top_line;
710 bottom_line = cs.bottom_line;
711 left_line = cs.left_line;
712 right_line = cs.right_line;
713 top_line_rtrimmed = cs.top_line_rtrimmed;
714 top_line_ltrimmed = cs.top_line_ltrimmed;
715 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
716 bottom_line_ltrimmed = cs.bottom_line_ltrimmed;
719 align_special = cs.align_special;
720 p_width = cs.p_width;
721 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
725 Tabular::RowData::RowData()
728 top_space_default(false),
729 bottom_space_default(false),
730 interline_space_default(false),
741 Tabular::ColumnData::ColumnData()
742 : alignment(LYX_ALIGN_CENTER),
743 valignment(LYX_VALIGN_TOP),
751 Tabular::ltType::ltType()
752 : set(false), topDL(false),
758 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
760 init(buffer, rows_arg, columns_arg);
764 void Tabular::setBuffer(Buffer & buffer)
767 for (row_type i = 0; i < nrows(); ++i)
768 for (col_type j = 0; j < ncols(); ++j)
769 cell_info[i][j].inset->setBuffer(*buffer_);
773 // activates all lines and sets all widths to 0
774 void Tabular::init(Buffer * buf, row_type rows_arg,
775 col_type columns_arg)
778 row_info = row_vector(rows_arg);
779 column_info = column_vector(columns_arg);
780 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
781 row_info.reserve(10);
782 column_info.reserve(10);
783 cell_info.reserve(100);
785 is_long_tabular = false;
786 tabular_valignment = LYX_VALIGN_MIDDLE;
787 tabular_width = Length();
788 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
790 use_booktabs = false;
791 // set silly default lines
792 for (row_type r = 0; r < nrows(); ++r)
793 for (col_type c = 0; c < ncols(); ++c) {
794 cell_info[r][c].inset->setBuffer(*buffer_);
795 cell_info[r][c].top_line = true;
796 cell_info[r][c].left_line = true;
797 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
798 cell_info[r][c].right_line = c == ncols() - 1;
803 void Tabular::deleteRow(row_type const row, bool const force)
805 // Not allowed to delete last row
809 bool const ct = force ? false : buffer().params().track_changes;
811 for (col_type c = 0; c < ncols(); ++c) {
812 // Care about multirow cells
813 if (row + 1 < nrows() &&
814 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
815 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
816 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
820 row_info[row].change.setDeleted();
822 row_info.erase(row_info.begin() + row);
823 cell_info.erase(cell_info.begin() + row);
829 void Tabular::copyRow(row_type const row)
831 insertRow(row, true);
835 void Tabular::appendRow(row_type row)
837 insertRow(row, false);
841 void Tabular::insertRow(row_type const row, bool copy)
843 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
844 cell_info.insert(cell_info.begin() + row + 1,
845 cell_vector(0, CellData(buffer_)));
847 for (col_type c = 0; c < ncols(); ++c) {
848 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
849 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
850 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
851 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
855 for (col_type c = 0; c < ncols(); ++c) {
856 if (isPartOfMultiRow(row, c))
858 // inherit line settings
859 idx_type const i = cellIndex(row + 1, c);
860 idx_type const j = cellIndex(row, c);
861 setLeftLine(i, leftLine(j));
862 setRightLine(i, rightLine(j));
863 setTopLine(i, topLine(j));
864 if (topLine(j) && bottomLine(j)) {
865 setBottomLine(i, true);
866 setBottomLine(j, false);
869 if (buffer().params().track_changes) {
870 row_info[row + 1].change.setInserted();
876 void Tabular::moveColumn(col_type col, ColDirection direction)
878 if (direction == Tabular::LEFT)
881 std::swap(column_info[col], column_info[col + 1]);
883 for (row_type r = 0; r < nrows(); ++r) {
884 std::swap(cell_info[r][col], cell_info[r][col + 1]);
885 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
886 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
888 idx_type const i = cellIndex(r, col);
889 idx_type const j = cellIndex(r, col + 1);
890 if (buffer().params().track_changes) {
891 cellInfo(i).inset->setChange(Change(Change::INSERTED));
892 cellInfo(j).inset->setChange(Change(Change::INSERTED));
899 void Tabular::moveRow(row_type row, RowDirection direction)
901 if (direction == Tabular::UP)
904 std::swap(row_info[row], row_info[row + 1]);
906 for (col_type c = 0; c < ncols(); ++c) {
907 std::swap(cell_info[row][c], cell_info[row + 1][c]);
908 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
909 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
911 idx_type const i = cellIndex(row, c);
912 idx_type const j = cellIndex(row + 1, c);
913 if (buffer().params().track_changes) {
914 cellInfo(i).inset->setChange(Change(Change::INSERTED));
915 cellInfo(j).inset->setChange(Change(Change::INSERTED));
922 void Tabular::deleteColumn(col_type const col, bool const force)
924 // Not allowed to delete last column
928 bool const ct = force ? false : buffer().params().track_changes;
930 for (row_type r = 0; r < nrows(); ++r) {
931 // Care about multicolumn cells
932 if (col + 1 < ncols() &&
933 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
934 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
935 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
938 cell_info[r].erase(cell_info[r].begin() + col);
941 column_info[col].change.setDeleted();
943 column_info.erase(column_info.begin() + col);
948 void Tabular::copyColumn(col_type const col)
950 insertColumn(col, true);
954 void Tabular::appendColumn(col_type col)
956 insertColumn(col, false);
960 void Tabular::insertColumn(col_type const col, bool copy)
962 bool const ct = buffer().params().track_changes;
963 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
965 for (row_type r = 0; r < nrows(); ++r) {
966 cell_info[r].insert(cell_info[r].begin() + col + 1,
967 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
968 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
969 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
972 for (row_type r = 0; r < nrows(); ++r) {
973 // inherit line settings
974 idx_type const i = cellIndex(r, col + 1);
975 idx_type const j = cellIndex(r, col);
976 setBottomLine(i, bottomLine(j));
977 setTopLine(i, topLine(j));
978 setLeftLine(i, leftLine(j));
979 setRightLine(i, rightLine(j));
980 if (rightLine(i) && rightLine(j)) {
981 setRightLine(j, false);
985 column_info[col + 1].change.setInserted();
991 void Tabular::updateIndexes()
996 for (row_type row = 0; row < nrows(); ++row)
997 for (col_type column = 0; column < ncols(); ++column) {
998 if (!isPartOfMultiColumn(row, column)
999 && !isPartOfMultiRow(row, column))
1001 if (isPartOfMultiRow(row, column))
1002 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1004 cell_info[row][column].cellno = numberofcells - 1;
1007 rowofcell.resize(numberofcells);
1008 columnofcell.resize(numberofcells);
1010 // reset column and row of cells and update their width, alignment and ct status
1011 for (row_type row = 0; row < nrows(); ++row) {
1012 for (col_type column = 0; column < ncols(); ++column) {
1013 if (isPartOfMultiColumn(row, column)) {
1014 cell_info[row][column].inset->toggleMultiCol(true);
1017 cell_info[row][column].inset->toggleMultiCol(false);
1018 // columnofcell needs to be called before setting width and alignment
1019 // multirow cells inherit the width from the column width
1020 if (!isPartOfMultiRow(row, column)) {
1021 columnofcell[i] = column;
1024 setFixedWidth(row, column);
1025 if (isPartOfMultiRow(row, column)) {
1026 cell_info[row][column].inset->toggleMultiRow(true);
1029 cell_info[row][column].inset->toggleMultiRow(false);
1030 cell_info[row][column].inset->setContentAlignment(
1031 getAlignment(cellIndex(row, column)));
1032 if (buffer().params().track_changes) {
1033 if (row_info[row].change.changed())
1034 cell_info[row][column].inset->setChange(row_info[row].change);
1035 if (column_info[column].change.changed())
1036 cell_info[row][column].inset->setChange(column_info[column].change);
1044 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
1046 idx_type result = 0;
1047 for (col_type c = 0; c < ncols(); ++c)
1048 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1054 bool Tabular::topLine(idx_type const cell) const
1056 return cellInfo(cell).top_line;
1060 bool Tabular::bottomLine(idx_type const cell) const
1062 return cellInfo(cell).bottom_line;
1066 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1068 if (use_booktabs && !ignore_bt)
1070 return cellInfo(cell).left_line;
1074 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1076 if (use_booktabs && !ignore_bt)
1078 return cellInfo(cell).right_line;
1082 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1085 return make_pair(false, false);
1086 return make_pair(cellInfo(cell).top_line_ltrimmed,
1087 cellInfo(cell).top_line_rtrimmed);
1091 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1094 return make_pair(false, false);
1095 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1096 cellInfo(cell).bottom_line_rtrimmed);
1100 int Tabular::interRowSpace(row_type row) const
1102 if (!row || row >= nrows())
1105 int const interline_space = row_info[row - 1].interline_space_default ?
1106 default_line_space :
1107 row_info[row - 1].interline_space.inPixels(width());
1108 if (rowTopLine(row) && rowBottomLine(row - 1))
1109 return interline_space + WIDTH_OF_LINE;
1110 return interline_space;
1114 int Tabular::interColumnSpace(idx_type cell) const
1116 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1117 if (rightLine(cell) && nextcol < ncols()
1118 && leftLine(cellIndex(cellRow(cell), nextcol)))
1119 return WIDTH_OF_LINE;
1124 int Tabular::cellWidth(idx_type cell) const
1127 col_type const span = columnSpan(cell);
1128 col_type const col = cellColumn(cell);
1129 for(col_type c = col; c < col + span ; ++c)
1130 w += column_info[c].width;
1135 int Tabular::cellHeight(idx_type cell) const
1137 row_type const span = rowSpan(cell);
1138 row_type const row = cellRow(cell);
1140 for(row_type r = row; r < row + span ; ++r) {
1141 h += rowAscent(r) + rowDescent(r);
1142 if (r != row + span - 1)
1143 h += interRowSpace(r + 1);
1150 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1152 vector<int> max_dwidth(ncols(), 0);
1153 // collect max. fixed width of column
1154 map<col_type, int> max_pwidth;
1155 // collect max. variable width of column
1156 map<col_type, int> max_width;
1158 for(col_type c = 0; c < ncols(); ++c)
1159 for(row_type r = 0; r < nrows(); ++r) {
1160 idx_type const i = cellIndex(r, c);
1161 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1162 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1163 if (!getPWidth(i).zero())
1164 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1165 else if (!column_info[c].varwidth)
1166 max_width[c] = max(max_width[c], cell_info[r][c].width);
1169 // If we have a fixed tabular width, we take this into account
1170 Length tab_width = tabular_width;
1171 bool const tabularx = hasVarwidthColumn();
1172 if (tabularx && tab_width.zero())
1173 // If no tabular width is specified with X columns,
1174 // we use 100% colwidth
1175 tab_width = Length(100, Length::PCW);
1177 if (!tab_width.zero()) {
1178 restwidth = mi.base.inPixels(tab_width);
1179 // Subtract the fixed widths from the table width
1180 for (auto const & w : max_pwidth)
1181 restwidth -= w.second;
1184 // If we have a fixed width, distribute the available table width
1185 // (minus the fixed widths) to the variable-width columns
1187 int restcols = ncols() - max_pwidth.size();
1188 if ((restwidth > 0) && (restcols != 0))
1189 vcolwidth = restwidth / restcols;
1191 // Now consider that some variable width columns exceed the vcolwidth
1192 if (vcolwidth > 0) {
1193 bool changed = false;
1194 for (auto const & w : max_width) {
1195 if (tabularx || w.second > vcolwidth) {
1197 restwidth -= w.second;
1201 if (changed && restwidth > 0)
1202 vcolwidth = restwidth / restcols;
1205 bool update = false;
1206 // for each col get max of single col cells
1207 for(col_type c = 0; c < ncols(); ++c) {
1209 for(row_type r = 0; r < nrows(); ++r) {
1210 idx_type const i = cellIndex(r, c);
1211 if (columnSpan(i) == 1) {
1212 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1213 && cell_info[r][c].decimal_width != 0)
1214 new_width = max(new_width, cellInfo(i).width
1215 + max_dwidth[c] - cellInfo(i).decimal_width);
1216 else if (getPWidth(i).zero() && vcolwidth > 0) {
1217 if (tabularx && !column_info[c].varwidth)
1218 new_width = max(new_width, cellInfo(i).width);
1220 new_width = vcolwidth;
1222 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1224 new_width = max(new_width, cellInfo(i).width);
1228 if (column_info[c].width != new_width) {
1229 column_info[c].width = new_width;
1230 // Do not trigger update when no space is left for variable
1231 // columns, as this will loop
1232 update = tab_width.zero() || restwidth > 0;
1235 // update col widths to fit merged cells
1236 for(col_type c = 0; c < ncols(); ++c)
1237 for(row_type r = 0; r < nrows(); ++r) {
1238 idx_type const i = cellIndex(r, c);
1239 int const span = columnSpan(i);
1240 if (span == 1 || c > cellColumn(i))
1244 for(col_type j = c; j < c + span ; ++j)
1245 old_width += column_info[j].width;
1247 if (cellInfo(i).width > old_width) {
1248 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1249 // Do not trigger update when no space is left for variable
1250 // columns, as this will loop
1251 update = tab_width.zero() || restwidth > 0;
1259 int Tabular::width() const
1262 for (col_type c = 0; c < ncols(); ++c)
1263 width += column_info[c].width;
1268 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1271 col_type const col = cellColumn(cell);
1272 // set alignment for the whole row if we are not in a multicolumn cell,
1273 // exclude possible multicolumn cells in the row
1274 if (!isMultiColumn(cell)) {
1275 for (row_type r = 0; r < nrows(); ++r) {
1276 // only if the column has no width the multirow inherits the
1277 // alignment of the column, otherwise it is left aligned
1278 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1279 && !isMultiColumn(cellIndex(r, col))) {
1280 cell_info[r][col].alignment = align;
1281 cell_info[r][col].inset->setContentAlignment(align);
1283 if ((isMultiRow(cellIndex(r, col)) && has_width)
1284 && !isMultiColumn(cellIndex(r, col))) {
1285 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1286 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1289 column_info[col].alignment = align;
1290 docstring & dpoint = column_info[col].decimal_point;
1291 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1292 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1293 dpoint = tlang->decimalSeparator();
1296 cellInfo(cell).alignment = align;
1297 cellInset(cell)->setContentAlignment(align);
1302 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1305 if (!isMultiColumn(cell) || onlycolumn)
1306 column_info[cellColumn(cell)].valignment = align;
1308 cellInfo(cell).valignment = align;
1315 * Allow line and paragraph breaks for fixed width multicol/multirow cells
1316 * or disallow them, merge cell paragraphs and reset layout to standard
1317 * for variable width multicol cells.
1319 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1320 bool const fixedWidth, bool const multicol,
1321 bool const multirow)
1323 inset->toggleFixedWidth(fixedWidth);
1324 if (!multirow && (fixedWidth || !multicol))
1327 // merge all paragraphs to one
1328 BufferParams const & bp = cur.bv().buffer().params();
1329 while (inset->paragraphs().size() > 1)
1330 mergeParagraph(bp, inset->paragraphs(), 0);
1332 // This is relevant for multirows
1337 ParagraphList::iterator pit = inset->paragraphs().begin();
1338 for (; pit != inset->paragraphs().end(); ++pit) {
1339 for (pos_type j = 0; j != pit->size(); ++j) {
1340 if (pit->isNewline(j))
1341 pit->eraseChar(j, bp.track_changes);
1347 // undo information has already been recorded
1348 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1349 bp.documentClass().plainLayoutName());
1356 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1357 Length const & width)
1359 col_type const c = cellColumn(cell);
1361 column_info[c].p_width = width;
1362 // reset the vertical alignment to top if the fixed width
1363 // is removed or zero because only fixed width columns can
1364 // have a vertical alignment
1365 if (column_info[c].p_width.zero())
1366 column_info[c].valignment = LYX_VALIGN_TOP;
1367 for (row_type r = 0; r < nrows(); ++r) {
1368 idx_type const cidx = cellIndex(r, c);
1369 // because of multicolumns
1370 toggleFixedWidth(cur, cellInset(cidx).get(),
1371 !getPWidth(cidx).zero(), isMultiColumn(cidx),
1373 if (isMultiRow(cidx))
1374 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1376 // cur can become invalid after paragraphs were merged
1381 bool Tabular::setFixedWidth(row_type r, col_type c)
1383 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1384 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1385 || (multicol && !cell_info[r][c].p_width.zero());
1386 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1391 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1392 Length const & width)
1394 if (!isMultiColumn(cell))
1397 cellInfo(cell).p_width = width;
1398 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1399 isMultiColumn(cell), isMultiRow(cell));
1400 // cur can become invalid after paragraphs were merged
1406 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1408 column_info[cellColumn(cell)].varwidth = varwidth;
1413 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1415 cellInfo(cell).mroffset = mroffset;
1420 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1421 Tabular::Feature what)
1423 if (what == SET_SPECIAL_MULTICOLUMN)
1424 cellInfo(cell).align_special = special;
1426 column_info[cellColumn(cell)].align_special = special;
1430 void Tabular::setTopLine(idx_type i, bool line)
1432 cellInfo(i).top_line = line;
1436 void Tabular::setBottomLine(idx_type i, bool line)
1438 cellInfo(i).bottom_line = line;
1442 void Tabular::setTopLineLTrim(idx_type i, bool val)
1444 cellInfo(i).top_line_ltrimmed = val;
1448 void Tabular::setTopLineRTrim(idx_type i, bool val)
1450 cellInfo(i).top_line_rtrimmed = val;
1454 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1456 cellInfo(i).bottom_line_ltrimmed = val;
1460 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1462 cellInfo(i).bottom_line_rtrimmed = val;
1466 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1468 setTopLineLTrim(i, trim.first);
1469 setTopLineRTrim(i, trim.second);
1472 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1474 setBottomLineLTrim(i, trim.first);
1475 setBottomLineRTrim(i, trim.second);
1478 void Tabular::setLeftLine(idx_type cell, bool line)
1480 cellInfo(cell).left_line = line;
1484 void Tabular::setRightLine(idx_type cell, bool line)
1486 cellInfo(cell).right_line = line;
1489 bool Tabular::rowTopLine(row_type r) const
1491 bool all_rows_set = true;
1492 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1493 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1494 return all_rows_set;
1498 bool Tabular::rowBottomLine(row_type r) const
1500 bool all_rows_set = true;
1501 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1502 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1503 return all_rows_set;
1507 bool Tabular::columnLeftLine(col_type c) const
1514 for (row_type r = 0; r < nrows(); ++r) {
1515 idx_type const i = cellIndex(r, c);
1516 if (c == cellColumn(i)) {
1518 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1519 if (cellInfo(i).left_line || right)
1523 return 2 * nrows_left >= total;
1527 bool Tabular::columnRightLine(col_type c) const
1532 int nrows_right = 0;
1534 for (row_type r = 0; r < nrows(); ++r) {
1535 idx_type i = cellIndex(r, c);
1536 if (c == cellColumn(i) + columnSpan(i) - 1) {
1538 bool left = (c + 1 < ncols()
1539 && cellInfo(cellIndex(r, c + 1)).left_line)
1540 || c + 1 == ncols();
1541 if (cellInfo(i).right_line && left)
1545 return 2 * nrows_right >= total;
1549 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1551 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1552 return cellInfo(cell).alignment;
1554 return column_info[cellColumn(cell)].alignment;
1559 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1561 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1562 return cellInfo(cell).valignment;
1563 return column_info[cellColumn(cell)].valignment;
1567 int Tabular::offsetVAlignment() const
1569 // for top-alignment the first horizontal table line must be exactly at
1570 // the position of the base line of the surrounding text line
1571 // for bottom alignment, the same is for the last table line
1572 int offset_valign = 0;
1573 switch (tabular_valignment) {
1574 case Tabular::LYX_VALIGN_BOTTOM:
1575 offset_valign = rowAscent(0) - height();
1577 case Tabular::LYX_VALIGN_MIDDLE:
1578 offset_valign = (- height()) / 2 + rowAscent(0);
1580 case Tabular::LYX_VALIGN_TOP:
1581 offset_valign = rowAscent(0);
1584 return offset_valign;
1588 Length const Tabular::getPWidth(idx_type cell) const
1590 if (isMultiColumn(cell))
1591 return cellInfo(cell).p_width;
1592 return column_info[cellColumn(cell)].p_width;
1596 Length const Tabular::getMROffset(idx_type cell) const
1598 return cellInfo(cell).mroffset;
1602 int Tabular::textHOffset(idx_type cell) const
1604 // the LaTeX Way :-(
1605 int x = WIDTH_OF_LINE;
1607 int const w = cellWidth(cell) - cellInfo(cell).width;
1609 switch (getAlignment(cell)) {
1610 case LYX_ALIGN_CENTER:
1613 case LYX_ALIGN_RIGHT:
1616 case LYX_ALIGN_DECIMAL: {
1617 // we center when no decimal point
1618 if (cellInfo(cell).decimal_width == 0) {
1622 col_type const c = cellColumn(cell);
1623 int max_dhoffset = 0;
1624 for(row_type r = 0; r < row_info.size() ; ++r) {
1625 idx_type const i = cellIndex(r, c);
1626 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1627 && cellInfo(i).decimal_width != 0)
1628 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1630 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1633 // LYX_ALIGN_LEFT: nothing :-)
1641 int Tabular::textVOffset(idx_type cell) const
1643 int voffset = cellInfo(cell).voffset;
1644 if (isMultiRow(cell)) {
1645 row_type const row = cellRow(cell);
1646 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1652 Tabular::idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1655 idx_type const numcells = numberOfCellsInRow(row);
1656 // we check against numcells to make sure we do not crash if all the
1657 // cells are multirow (bug #7535), but in that case our return value
1658 // is really invalid, i.e., it is NOT the first cell in the row. but
1659 // i do not know what to do here. (rgh)
1660 while (c < numcells - 1
1661 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1662 || (ct && column_info[c].change.deleted())))
1664 return cell_info[row][c].cellno;
1668 Tabular::idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1670 col_type c = ncols() - 1;
1671 // of course we check against 0 so we don't crash. but we have the same
1672 // problem as in the previous routine: if all the cells are part of a
1673 // multirow or part of a multi column, then our return value is invalid.
1675 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1676 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1677 || (ct && column_info[c].change.deleted())))
1679 return cell_info[row][c].cellno;
1683 Tabular::row_type Tabular::getFirstRow(bool const ct) const
1688 // exclude deleted rows if ct == true
1689 while (r < nrows() && row_info[r].change.deleted())
1695 Tabular::row_type Tabular::getLastRow(bool const ct) const
1697 row_type r = nrows() - 1;
1700 // exclude deleted rows if ct == true
1701 while (r > 0 && row_info[r].change.deleted())
1707 Tabular::row_type Tabular::cellRow(idx_type cell) const
1709 if (cell >= numberofcells)
1713 return rowofcell[cell];
1717 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1719 if (cell >= numberofcells)
1723 return columnofcell[cell];
1727 void Tabular::write(ostream & os) const
1731 << write_attribute("version", 3)
1732 << write_attribute("rows", nrows())
1733 << write_attribute("columns", ncols())
1735 // global longtable options
1737 << write_attribute("rotate", rotate)
1738 << write_attribute("booktabs", use_booktabs)
1739 << write_attribute("islongtable", is_long_tabular)
1740 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1741 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1742 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1743 << write_attribute("headTopDL", endhead.topDL)
1744 << write_attribute("headBottomDL", endhead.bottomDL)
1745 << write_attribute("footTopDL", endfoot.topDL)
1746 << write_attribute("footBottomDL", endfoot.bottomDL)
1747 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1748 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1749 << write_attribute("lastFootEmpty", endlastfoot.empty);
1750 // longtables cannot be aligned vertically
1751 if (!is_long_tabular)
1752 os << write_attribute("tabularvalignment", tabular_valignment);
1753 os << write_attribute("tabularwidth", tabular_width);
1754 if (is_long_tabular)
1755 os << write_attribute("longtabularalignment", longtabular_alignment);
1757 for (col_type c = 0; c < ncols(); ++c) {
1759 << write_attribute("alignment", column_info[c].alignment);
1760 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1761 os << write_attribute("decimal_point", column_info[c].decimal_point);
1762 os << write_attribute("change", column_info[c].change, buffer().params())
1763 << write_attribute("valignment", column_info[c].valignment)
1764 << write_attribute("width", column_info[c].p_width.asString())
1765 << write_attribute("varwidth", column_info[c].varwidth)
1766 << write_attribute("special", column_info[c].align_special)
1769 for (row_type r = 0; r < nrows(); ++r) {
1770 static const string def("default");
1772 if (row_info[r].top_space_default)
1773 os << write_attribute("topspace", def);
1775 os << write_attribute("topspace", row_info[r].top_space);
1776 if (row_info[r].bottom_space_default)
1777 os << write_attribute("bottomspace", def);
1779 os << write_attribute("bottomspace", row_info[r].bottom_space);
1780 if (row_info[r].interline_space_default)
1781 os << write_attribute("interlinespace", def);
1783 os << write_attribute("interlinespace", row_info[r].interline_space);
1784 os << write_attribute("change", row_info[r].change, buffer().params())
1785 << write_attribute("endhead", row_info[r].endhead)
1786 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1787 << write_attribute("endfoot", row_info[r].endfoot)
1788 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1789 << write_attribute("newpage", row_info[r].newpage)
1790 << write_attribute("caption", row_info[r].caption)
1792 for (col_type c = 0; c < ncols(); ++c) {
1794 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1795 << write_attribute("multirow", cell_info[r][c].multirow)
1796 << write_attribute("mroffset", cell_info[r][c].mroffset)
1797 << write_attribute("alignment", cell_info[r][c].alignment)
1798 << write_attribute("valignment", cell_info[r][c].valignment)
1799 << write_attribute("topline", cell_info[r][c].top_line)
1800 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1801 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1802 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1803 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1804 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1805 << write_attribute("leftline", cell_info[r][c].left_line)
1806 << write_attribute("rightline", cell_info[r][c].right_line)
1807 << write_attribute("rotate", cell_info[r][c].rotate)
1808 << write_attribute("usebox", cell_info[r][c].usebox)
1809 << write_attribute("width", cell_info[r][c].p_width)
1810 << write_attribute("special", cell_info[r][c].align_special)
1812 os << "\\begin_inset ";
1813 cell_info[r][c].inset->write(os);
1814 os << "\n\\end_inset\n"
1816 // FIXME This can be removed again once the mystery
1817 // crash has been resolved.
1822 os << "</lyxtabular>\n";
1826 void Tabular::read(Lexer & lex)
1829 istream & is = lex.getStream();
1831 l_getline(is, line);
1832 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1833 LASSERT(false, return);
1837 if (!getTokenValue(line, "version", version))
1839 LATTEST(version >= 2);
1842 if (!getTokenValue(line, "rows", rows_arg))
1845 if (!getTokenValue(line, "columns", columns_arg))
1847 init(buffer_, rows_arg, columns_arg);
1848 l_getline(is, line);
1849 if (!prefixIs(line, "<features")) {
1850 lyxerr << "Wrong tabular format (expected <features ...> got"
1851 << line << ')' << endl;
1854 getTokenValue(line, "rotate", rotate);
1855 getTokenValue(line, "booktabs", use_booktabs);
1856 getTokenValue(line, "islongtable", is_long_tabular);
1857 getTokenValue(line, "tabularvalignment", tabular_valignment);
1858 getTokenValue(line, "tabularwidth", tabular_width);
1859 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1860 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1861 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1862 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1863 getTokenValue(line, "headTopDL", endhead.topDL);
1864 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1865 getTokenValue(line, "footTopDL", endfoot.topDL);
1866 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1867 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1868 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1869 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1871 for (col_type c = 0; c < ncols(); ++c) {
1873 if (!prefixIs(line,"<column")) {
1874 lyxerr << "Wrong tabular format (expected <column ...> got"
1875 << line << ')' << endl;
1878 getTokenValue(line, "alignment", column_info[c].alignment);
1879 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1880 getTokenValue(line, "valignment", column_info[c].valignment);
1881 getTokenValue(line, "width", column_info[c].p_width);
1882 getTokenValue(line, "special", column_info[c].align_special);
1883 getTokenValue(line, "varwidth", column_info[c].varwidth);
1884 getTokenValue(line, "change", column_info[c].change, buffer().params());
1887 for (row_type i = 0; i < nrows(); ++i) {
1888 l_getline(is, line);
1889 if (!prefixIs(line, "<row")) {
1890 lyxerr << "Wrong tabular format (expected <row ...> got"
1891 << line << ')' << endl;
1894 getTokenValue(line, "topspace", row_info[i].top_space,
1895 row_info[i].top_space_default);
1896 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1897 row_info[i].bottom_space_default);
1898 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1899 row_info[i].interline_space_default);
1900 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1901 getTokenValue(line, "endhead", row_info[i].endhead);
1902 getTokenValue(line, "endfoot", row_info[i].endfoot);
1903 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1904 getTokenValue(line, "newpage", row_info[i].newpage);
1905 getTokenValue(line, "caption", row_info[i].caption);
1906 getTokenValue(line, "change", row_info[i].change, buffer().params());
1907 for (col_type j = 0; j < ncols(); ++j) {
1908 l_getline(is, line);
1909 if (!prefixIs(line, "<cell")) {
1910 lyxerr << "Wrong tabular format (expected <cell ...> got"
1911 << line << ')' << endl;
1914 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1915 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1916 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1917 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1918 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1919 getTokenValue(line, "topline", cell_info[i][j].top_line);
1920 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1921 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1922 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1923 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1924 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1925 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1926 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1927 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1928 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1929 getTokenValue(line, "width", cell_info[i][j].p_width);
1931 getTokenValue(line, "special", cell_info[i][j].align_special);
1932 l_getline(is, line);
1933 if (prefixIs(line, "\\begin_inset")) {
1934 cell_info[i][j].inset->setBuffer(*buffer_);
1935 cell_info[i][j].inset->read(lex);
1936 l_getline(is, line);
1938 if (!prefixIs(line, "</cell>")) {
1939 lyxerr << "Wrong tabular format (expected </cell> got"
1940 << line << ')' << endl;
1944 l_getline(is, line);
1945 if (!prefixIs(line, "</row>")) {
1946 lyxerr << "Wrong tabular format (expected </row> got"
1947 << line << ')' << endl;
1951 while (!prefixIs(line, "</lyxtabular>")) {
1952 l_getline(is, line);
1958 bool Tabular::isMultiColumn(idx_type cell) const
1960 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1961 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1965 bool Tabular::hasMultiColumn(col_type c) const
1967 for (row_type r = 0; r < nrows(); ++r) {
1968 if (isMultiColumn(cellIndex(r, c)))
1975 bool Tabular::hasVarwidthColumn() const
1977 for (col_type c = 0; c < ncols(); ++c) {
1978 if (column_info[c].varwidth)
1985 bool Tabular::isVTypeColumn(col_type c) const
1987 for (row_type r = 0; r < nrows(); ++r) {
1988 idx_type idx = cellIndex(r, c);
1989 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1996 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1998 return cell_info[cellRow(cell)][cellColumn(cell)];
2002 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2004 return cell_info[cellRow(cell)][cellColumn(cell)];
2008 Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2009 bool const right_border)
2011 idx_type const col = cellColumn(cell);
2012 idx_type const row = cellRow(cell);
2013 for (idx_type i = 0; i < number; ++i)
2014 unsetMultiRow(cellIndex(row, col + i));
2016 // unsetting of multirow may have invalidated cell index
2017 cell = cellIndex(row, col);
2018 CellData & cs = cellInfo(cell);
2019 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2020 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2021 cs.alignment = column_info[col].alignment;
2022 setRightLine(cell, right_border);
2023 // non-fixed width multicolumns cannot have multiple paragraphs
2024 if (getPWidth(cell).zero()) {
2025 toggleFixedWidth(cur, cellInset(cell).get(),
2026 !getPWidth(cell).zero(), isMultiColumn(cell),
2028 // cur can become invalid after paragraphs were merged
2032 idx_type lastcell = cellIndex(row, col + number - 1);
2033 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2034 CellData & cs1 = cellInfo(cell + i);
2035 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2036 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2044 bool Tabular::isMultiRow(idx_type cell) const
2046 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2047 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2050 bool Tabular::hasMultiRow(row_type r) const
2052 for (col_type c = 0; c < ncols(); ++c) {
2053 if (isMultiRow(cellIndex(r, c)))
2059 Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2060 bool const bottom_border,
2061 LyXAlignment const halign)
2063 idx_type const col = cellColumn(cell);
2064 idx_type const row = cellRow(cell);
2065 for (idx_type i = 0; i < number; ++i)
2066 unsetMultiColumn(cellIndex(row + i, col));
2068 // unsetting of multirow may have invalidated cell index
2069 cell = cellIndex(row, col);
2070 CellData & cs = cellInfo(cell);
2071 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2072 cs.valignment = LYX_VALIGN_MIDDLE;
2073 // the horizontal alignment of multirow cells can only
2074 // be changed for the whole table row,
2075 // support changing this only for the multirow cell can be done via
2077 if (getPWidth(cell).zero())
2078 cs.alignment = halign;
2080 cs.alignment = LYX_ALIGN_LEFT;
2082 // Multirows cannot have multiple paragraphs
2083 if (getPWidth(cell).zero()) {
2084 toggleFixedWidth(cur, cellInset(cell).get(),
2085 !getPWidth(cell).zero(),
2086 isMultiColumn(cell), isMultiRow(cell));
2087 // cur can become invalid after paragraphs were merged
2091 // set the bottom line of the last selected cell
2092 setBottomLine(cell, bottom_border);
2094 for (idx_type i = 1; i < number; ++i) {
2095 CellData & cs1 = cell_info[row + i][col];
2096 cs1.multirow = CELL_PART_OF_MULTIROW;
2097 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2105 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
2107 row_type const row = cellRow(cell);
2108 col_type const col = cellColumn(cell);
2110 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2117 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
2119 col_type const column = cellColumn(cell);
2120 col_type row = cellRow(cell) + 1;
2121 while (row < nrows() && isPartOfMultiRow(row, column))
2124 return row - cellRow(cell);
2128 void Tabular::unsetMultiColumn(idx_type cell)
2130 if (!isMultiColumn(cell))
2133 row_type const row = cellRow(cell);
2134 col_type const col = cellColumn(cell);
2135 row_type const span = columnSpan(cell);
2136 for (col_type c = 0; c < span; ++c) {
2137 // in the table dialog the lines are set in every case
2138 // when unsetting a multicolumn this leads to an additional right
2139 // line for every cell that was part of the former multicolumn cell,
2140 // except if the cell is in the last column
2141 // therefore remove this line
2142 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2143 && (col + c) < (col + span - 1))
2144 cell_info[row][col + c].right_line = false;
2145 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2151 void Tabular::unsetMultiRow(idx_type cell)
2153 if (!isMultiRow(cell))
2156 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2157 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2158 row_type const row = cellRow(cell);
2159 col_type const col = cellColumn(cell);
2160 row_type const span = rowSpan(cell);
2161 for (row_type r = 0; r < span; ++r)
2162 cell_info[row + r][col].multirow = CELL_NORMAL;
2167 void Tabular::setRotateCell(idx_type cell, int value)
2169 cellInfo(cell).rotate = value;
2173 int Tabular::getRotateCell(idx_type cell) const
2175 return cellInfo(cell).rotate;
2179 bool Tabular::needRotating() const
2181 if (rotate && !is_long_tabular)
2183 for (row_type r = 0; r < nrows(); ++r)
2184 for (col_type c = 0; c < ncols(); ++c)
2185 if (cell_info[r][c].rotate != 0)
2191 bool Tabular::isLastCell(idx_type cell) const
2193 return cell + 1 >= numberofcells;
2197 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2199 if (cellRow(cell) == 0)
2202 col_type const col = cellColumn(cell);
2203 row_type r = cellRow(cell) - 1;
2204 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2207 return cell_info[r][col].cellno;
2211 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2213 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2214 if (nextrow < nrows())
2215 return cell_info[nextrow][cellColumn(cell)].cellno;
2220 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2222 LASSERT(column != npos && column < ncols(), column = 0);
2223 LASSERT(row != npos && row < nrows(), row = 0);
2224 return cell_info[row][column].cellno;
2228 void Tabular::setUsebox(idx_type cell, BoxType type)
2230 cellInfo(cell).usebox = type;
2234 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2236 if (getRotateCell(cell) == 0
2237 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2238 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2240 if (cellInfo(cell).usebox > 1)
2241 return cellInfo(cell).usebox;
2242 return useBox(cell);
2247 // This are functions used for the longtable support
2249 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2255 row_info[row].endfirsthead = flag;
2259 row_info[row].endhead = flag;
2264 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2267 hd.set = haveLTHead();
2268 return row_info[row].endhead;
2272 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2275 hd.set = haveLTFirstHead();
2276 return row_info[row].endfirsthead;
2280 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2286 row_info[row].endlastfoot = flag;
2290 row_info[row].endfoot = flag;
2295 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2298 fd.set = haveLTFoot();
2299 return row_info[row].endfoot;
2303 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2306 fd.set = haveLTLastFoot();
2307 return row_info[row].endlastfoot;
2311 void Tabular::setLTNewPage(row_type row, bool what)
2313 row_info[row].newpage = what;
2317 bool Tabular::getLTNewPage(row_type row) const
2319 return row_info[row].newpage;
2323 bool Tabular::haveLTHead(bool withcaptions) const
2325 if (!is_long_tabular)
2327 for (row_type i = 0; i < nrows(); ++i)
2328 if (row_info[i].endhead &&
2329 (withcaptions || !row_info[i].caption))
2335 bool Tabular::haveLTFirstHead(bool withcaptions) const
2337 if (!is_long_tabular || endfirsthead.empty)
2339 for (row_type r = 0; r < nrows(); ++r)
2340 if (row_info[r].endfirsthead &&
2341 (withcaptions || !row_info[r].caption))
2347 bool Tabular::haveLTFoot(bool withcaptions) const
2349 if (!is_long_tabular)
2351 for (row_type r = 0; r < nrows(); ++r)
2352 if (row_info[r].endfoot &&
2353 (withcaptions || !row_info[r].caption))
2359 bool Tabular::haveLTLastFoot(bool withcaptions) const
2361 if (!is_long_tabular || endlastfoot.empty)
2363 for (row_type r = 0; r < nrows(); ++r)
2364 if (row_info[r].endlastfoot &&
2365 (withcaptions || !row_info[r].caption))
2371 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2373 idx_type i = getFirstCellInRow(row);
2375 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2376 setTopLine(i, false);
2377 setBottomLine(i, false);
2378 setLeftLine(i, false);
2379 setRightLine(i, false);
2380 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2381 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2382 setLTHead(row, true, endfirsthead, true);
2383 row_info[row].endfirsthead = true;
2386 unsetMultiColumn(i);
2387 // When unsetting a caption row, also all existing
2388 // captions in this row must be dissolved.
2390 row_info[row].caption = what;
2395 bool Tabular::ltCaption(row_type row) const
2397 return row_info[row].caption;
2401 bool Tabular::haveLTCaption(CaptionType captiontype) const
2403 if (!is_long_tabular)
2405 for (row_type r = 0; r < nrows(); ++r) {
2406 if (row_info[r].caption) {
2407 switch (captiontype) {
2408 case CAPTION_FIRSTHEAD:
2409 if (row_info[r].endfirsthead)
2413 if (row_info[r].endhead)
2417 if (row_info[r].endfoot)
2420 case CAPTION_LASTFOOT:
2421 if (row_info[r].endlastfoot)
2433 // end longtable support functions
2435 void Tabular::setRowAscent(row_type row, int height)
2437 if (row >= nrows() || row_info[row].ascent == height)
2439 row_info[row].ascent = height;
2443 void Tabular::setRowDescent(row_type row, int height)
2445 if (row >= nrows() || row_info[row].descent == height)
2447 row_info[row].descent = height;
2451 int Tabular::rowAscent(row_type row) const
2453 LASSERT(row < nrows(), row = 0);
2454 return row_info[row].ascent;
2458 int Tabular::rowDescent(row_type row) const
2460 LASSERT(row < nrows(), row = 0);
2461 return row_info[row].descent;
2465 int Tabular::height() const
2468 for (row_type row = 0; row < nrows(); ++row)
2469 height += rowAscent(row) + rowDescent(row) +
2475 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2477 LASSERT(row < nrows(), return false);
2478 LASSERT(column < ncols(), return false);
2479 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2483 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2485 LASSERT(row < nrows(), return false);
2486 LASSERT(column < ncols(), return false);
2487 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2491 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns,
2492 list<col_type> logical_columns) const
2494 // we only output complete row lines and the 1st row here, the rest
2495 // is done in Tabular::TeXBottomHLine(...)
2497 // get for each column the topline (if any)
2498 map<col_type, bool> topline, topltrims, toprtrims;
2500 bool have_trims = false;
2501 for (auto const & c : columns) {
2502 topline[c] = topLine(cellIndex(row, c));
2503 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2504 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2505 // If cell is part of a multirow and not the first cell of the
2506 // multirow, no line must be drawn.
2508 if (isMultiRow(cellIndex(row, c))
2509 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2511 topltrims[c] = false;
2512 toprtrims[c] = false;
2514 // copy trimming to multicolumn parts
2515 if (isPartOfMultiColumn(row, c)) {
2516 topltrims[c] = topltrims[c-1];
2517 toprtrims[c] = toprtrims[c-1];
2519 if (topline.find(c) != topline.end() && topline.find(c)->second)
2521 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2522 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2526 // do nothing if empty first row, or incomplete row line after
2527 row_type first = getFirstRow(!buffer().params().output_changes);
2528 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2531 // Is this the actual first row (excluding longtable caption row)?
2532 bool const realfirstrow = (row == first
2533 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2535 // only output complete row lines and the 1st row's clines
2536 if (nset == columns.size() && !have_trims) {
2538 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2542 } else if (realfirstrow || have_trims) {
2543 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2545 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2546 std::list<col_type>::const_iterator it2 = columns.begin();
2547 // We need to iterate over the logical columns here, but take care for
2549 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2554 if (topline.find(c)->second) {
2555 col_type offset = 0;
2556 for (col_type j = 0 ; j < c; ++j)
2557 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2559 // If the two iterators differ, we are in bidi with swapped columns
2560 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2561 while (isPartOfMultiColumn(row, c))
2564 if (topltrims.find(c) != topltrims.end()
2565 && topltrims.find(c)->second)
2567 col_type cstart = c;
2568 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2569 if (isMultiColumn(cellIndex(row, c))
2570 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2572 if (c > cstart && topltrims.find(c) != topltrims.end()
2573 && topltrims.find(c)->second) {
2574 if (!isPartOfMultiColumn(row, c))
2577 } else if (toprtrims.find(c) != toprtrims.end()
2578 && toprtrims.find(c)->second)
2582 for (col_type j = cstart ; j < c ; ++j)
2583 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2585 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2586 if (toprtrims.find(c) != toprtrims.end()
2587 && toprtrims.find(c)->second)
2592 os << "(" << trim << ")";
2593 if (firstcol > lastcol)
2594 // This can happen with bidi (swapped columns)
2595 os << "{" << lastcol << '-' << firstcol << "}";
2597 os << "{" << firstcol << '-' << lastcol << "}";
2598 if (c == columns.size() - 1)
2608 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns,
2609 list<col_type> logical_columns) const
2611 // we output bottomlines of row r and the toplines of row r+1
2612 // if the latter do not span the whole tabular
2614 // get the bottomlines of row r, and toplines in next row
2615 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2616 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2617 bool nextrowset = true;
2618 for (auto const & c : columns) {
2619 idx_type const idx = cellIndex(row, c);
2620 bottomline[c] = bottomLine(cellIndex(row, c));
2621 bottomltrims[c] = bottomLineTrim(idx).first;
2622 bottomrtrims[c] = bottomLineTrim(idx).second;
2623 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2624 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2625 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2626 // If cell is part of a multirow and not the last cell of the
2627 // multirow, no line must be drawn.
2629 if (isMultiRow(cellIndex(row, c))
2630 && isMultiRow(cellIndex(row + 1, c))
2631 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2632 bottomline[c] = false;
2634 bottomltrims[c] = false;
2635 bottomrtrims[c] = false;
2636 topltrims[c] = false;
2637 toprtrims[c] = false;
2639 // copy trimming in multicolumn parts
2640 if (isPartOfMultiColumn(row, c)) {
2641 topltrims[c] = topltrims[c-1];
2642 toprtrims[c] = toprtrims[c-1];
2643 bottomltrims[c] = bottomltrims[c-1];
2644 bottomrtrims[c] = bottomrtrims[c-1];
2647 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2650 // combine this row's bottom lines and next row's toplines if necessary
2652 bool have_trims = false;
2653 for (auto const & c : columns) {
2655 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2656 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2657 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2658 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2659 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2660 if (bottomline.find(c)->second)
2662 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2663 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2667 // do nothing if empty, OR incomplete row line with a topline in next row
2668 if (nset == 0 || (nextrowset && nset != columns.size()))
2671 if (nset == columns.size() && !have_trims) {
2673 os << (lastrow ? "\\bottomrule" : "\\midrule");
2677 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2679 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2680 std::list<col_type>::const_iterator it2 = columns.begin();
2681 // We need to iterate over the logical columns here, but take care for
2683 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2688 if (bottomline.find(c)->second) {
2689 col_type offset = 0;
2690 for (col_type j = 0 ; j < c; ++j)
2691 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2693 // If the two iterators differ, we are in bidi with swapped columns
2694 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2695 while (isPartOfMultiColumn(row, c))
2698 if (bottomltrims.find(c) != bottomltrims.end()
2699 && bottomltrims.find(c)->second)
2701 col_type cstart = c;
2702 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2703 if (isMultiColumn(cellIndex(row, c))
2705 && isPartOfMultiColumn(row, c + 1))
2708 && bottomltrims.find(c) != bottomltrims.end()
2709 && bottomltrims.find(c)->second) {
2710 if (!isPartOfMultiColumn(row, c))
2713 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2714 && bottomrtrims.find(c)->second)
2718 for (col_type j = cstart ; j < c ; ++j)
2719 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2721 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2722 if (bottomrtrims.find(c) != bottomrtrims.end()
2723 && bottomrtrims.find(c)->second)
2728 os << "(" << trim << ")";
2729 if (firstcol > lastcol)
2730 // This can happen with bidi (swapped columns)
2731 os << "{" << lastcol << '-' << firstcol << "}";
2733 os << "{" << firstcol << '-' << lastcol << "}";
2734 if (c == columns.size() - 1)
2744 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2745 bool & ismulticol, bool & ismultirow,
2746 bool const bidi) const
2748 row_type const r = cellRow(cell);
2749 if (is_long_tabular && row_info[r].caption)
2752 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2753 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2754 // figure out how to set the lines
2755 // we always set double lines to the right of the cell
2756 // or left in bidi RTL, respectively.
2757 col_type const c = cellColumn(cell);
2758 col_type const nextcol = c + columnSpan(cell);
2759 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2760 bool colright = columnRightLine(c);
2761 bool colleft = columnLeftLine(c);
2762 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2763 bool nextcellleft = nextcol < ncols()
2764 && leftLine(cellIndex(r, nextcol));
2765 bool coldouble = colright && nextcolleft;
2766 bool celldouble = rightLine(cell) && nextcellleft;
2768 ismulticol = (isMultiColumn(cell)
2769 || (c == 0 && colleft != leftLine(cell))
2770 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2771 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2772 || (coldouble != celldouble))
2775 // we center in multicol when no decimal point
2777 docstring const align_d = column_info[c].decimal_point;
2778 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2779 bool const nosep = !dit;
2780 ismulticol |= nosep;
2781 celldouble &= nosep;
2784 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2785 int latexcolspan = columnSpan(cell);
2786 for (col_type col = c; col < c + columnSpan(cell); ++col)
2787 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2791 os << "\\multicolumn{" << latexcolspan << "}{";
2792 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2793 || (!bidi && c == 0 && leftLine(cell))))
2795 if (bidi && celldouble)
2796 // add extra vertical line if we want a double one
2798 if (!cellInfo(cell).align_special.empty()) {
2799 os << cellInfo(cell).align_special;
2801 if (!getPWidth(cell).zero()) {
2803 case LYX_ALIGN_LEFT:
2804 os << ">{\\raggedright}";
2806 case LYX_ALIGN_RIGHT:
2807 os << ">{\\raggedleft}";
2809 case LYX_ALIGN_CENTER:
2810 os << ">{\\centering}";
2816 case LYX_VALIGN_TOP:
2819 case LYX_VALIGN_MIDDLE:
2822 case LYX_VALIGN_BOTTOM:
2827 << from_ascii(getPWidth(cell).asLatexString())
2831 case LYX_ALIGN_LEFT:
2834 case LYX_ALIGN_RIGHT:
2841 } // end if else !getPWidth
2842 } // end if else !cellinfo_of_cell
2843 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2845 if (!bidi && celldouble)
2846 // add extra vertical line if we want a double one
2849 } // end if ismulticol
2851 // we only need code for the first multirow cell
2852 ismultirow = isMultiRow(cell);
2854 os << "\\multirow{" << rowSpan(cell) << "}{";
2855 if (!getPWidth(cell).zero())
2856 os << from_ascii(getPWidth(cell).asLatexString());
2858 // we need to set a default value
2861 if (!getMROffset(cell).zero())
2862 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2864 } // end if ismultirow
2866 if (getRotateCell(cell) != 0)
2867 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2869 if (getUsebox(cell) == BOX_PARBOX) {
2872 case LYX_VALIGN_TOP:
2875 case LYX_VALIGN_MIDDLE:
2878 case LYX_VALIGN_BOTTOM:
2882 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2884 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2885 os << "\\begin{minipage}[";
2887 case LYX_VALIGN_TOP:
2890 case LYX_VALIGN_MIDDLE:
2893 case LYX_VALIGN_BOTTOM:
2897 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2899 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2900 os << "\\begin{varwidth}[";
2902 case LYX_VALIGN_TOP:
2905 case LYX_VALIGN_MIDDLE:
2908 case LYX_VALIGN_BOTTOM:
2912 os << "]{\\linewidth}\n";
2917 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2918 bool ismulticol, bool ismultirow) const
2920 row_type const r = cellRow(cell);
2921 if (is_long_tabular && row_info[r].caption)
2925 if (getUsebox(cell) == BOX_PARBOX)
2927 else if (getUsebox(cell) == BOX_MINIPAGE)
2928 os << breakln << "\\end{minipage}";
2929 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2930 os << breakln << "\\end{varwidth}";
2931 if (getRotateCell(cell) != 0)
2932 os << breakln << "\\end{turn}";
2940 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2941 OutputParams const & runparams,
2942 list<col_type> columns,
2943 list<col_type> logical_columns) const
2945 if (!is_long_tabular)
2949 // output caption which is in no header or footer
2950 if (haveLTCaption()) {
2951 for (row_type r = 0; r < nrows(); ++r) {
2952 if (row_info[r].caption &&
2953 !row_info[r].endfirsthead && !row_info[r].endhead &&
2954 !row_info[r].endfoot && !row_info[r].endlastfoot)
2955 TeXRow(os, r, runparams, columns, logical_columns);
2958 // output first header info
2959 if (haveLTFirstHead()) {
2960 if (endfirsthead.topDL)
2962 for (row_type r = 0; r < nrows(); ++r) {
2963 if (row_info[r].endfirsthead)
2964 TeXRow(os, r, runparams, columns, logical_columns);
2966 if (endfirsthead.bottomDL)
2968 os << "\\endfirsthead\n";
2970 // output header info
2972 if (endfirsthead.empty && !haveLTFirstHead())
2973 os << "\\endfirsthead\n";
2976 for (row_type r = 0; r < nrows(); ++r) {
2977 if (row_info[r].endhead)
2978 TeXRow(os, r, runparams, columns, logical_columns);
2980 if (endhead.bottomDL)
2982 os << "\\endhead\n";
2984 // output footer info
2988 for (row_type r = 0; r < nrows(); ++r) {
2989 if (row_info[r].endfoot)
2990 TeXRow(os, r, runparams, columns, logical_columns);
2992 if (endfoot.bottomDL)
2994 os << "\\endfoot\n";
2995 if (endlastfoot.empty && !haveLTLastFoot())
2996 os << "\\endlastfoot\n";
2998 // output lastfooter info
2999 if (haveLTLastFoot()) {
3000 if (endlastfoot.topDL)
3002 for (row_type r = 0; r < nrows(); ++r) {
3003 if (row_info[r].endlastfoot)
3004 TeXRow(os, r, runparams, columns, logical_columns);
3006 if (endlastfoot.bottomDL)
3008 os << "\\endlastfoot\n";
3013 bool Tabular::isValidRow(row_type row) const
3015 if (!is_long_tabular)
3017 return !row_info[row].endhead && !row_info[row].endfirsthead
3018 && !row_info[row].endfoot && !row_info[row].endlastfoot
3019 && !row_info[row].caption;
3023 void Tabular::TeXRow(otexstream & os, row_type row,
3024 OutputParams const & runparams,
3025 list<col_type> columns, list<col_type> logical_columns) const
3027 //output the top line
3028 TeXTopHLine(os, row, columns, logical_columns);
3030 if (row_info[row].top_space_default) {
3032 os << "\\addlinespace\n";
3034 os << "\\noalign{\\vskip\\doublerulesep}\n";
3035 } else if(!row_info[row].top_space.zero()) {
3037 os << "\\addlinespace["
3038 << from_ascii(row_info[row].top_space.asLatexString())
3041 os << "\\noalign{\\vskip"
3042 << from_ascii(row_info[row].top_space.asLatexString())
3046 bool ismulticol = false;
3047 bool ismultirow = false;
3049 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3050 // Luabibdi (used by LuaTeX) behaves like classic
3051 bool const bidi_rtl =
3052 runparams.local_font->isRightToLeft()
3053 && runparams.useBidiPackage();
3054 bool const ct = !buffer().params().output_changes;
3056 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3058 for (auto const & c : columns) {
3059 if (isPartOfMultiColumn(row, c))
3062 idx_type cell = cellIndex(row, c);
3064 if (isPartOfMultiRow(row, c)
3065 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3066 if (cell != lastcell)
3071 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3072 InsetTableCell const * inset = cellInset(cell);
3074 Paragraph const & par = inset->paragraphs().front();
3076 os.texrow().forceStart(par.id(), 0);
3078 bool rtl = par.isRTL(buffer().params())
3080 && getPWidth(cell).zero()
3081 && !runparams.use_polyglossia;
3085 par.getParLanguage(buffer().params())->lang();
3086 if (lang == "farsi")
3088 else if (lang == "arabic_arabi")
3090 // currently, remaining RTL languages are
3091 // arabic_arabtex and hebrew
3095 // pass to the OutputParams that we are in a cell and
3096 // which alignment we have set.
3097 // InsetNewline needs this context information.
3098 OutputParams newrp(runparams);
3099 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3100 ? OutputParams::PLAIN
3101 : OutputParams::ALIGNED;
3103 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3104 // copy cell and split in 2
3105 InsetTableCell head = InsetTableCell(*cellInset(cell));
3106 head.setBuffer(const_cast<Buffer &>(buffer()));
3107 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3109 dit.push_back(CursorSlice(head));
3110 head.setMacrocontextPositionRecursive(dit);
3111 bool hassep = false;
3112 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3114 tail.setBuffer(head.buffer());
3116 dit.push_back(CursorSlice(tail));
3117 tail.setMacrocontextPositionRecursive(dit);
3121 tail.latex(os, newrp);
3124 head.latex(os, newrp);
3126 head.latex(os, newrp);
3129 tail.latex(os, newrp);
3132 } else if (ltCaption(row)) {
3133 // Inside longtable caption rows, we must only output the caption inset
3134 // with its content and omit anything outside of that (see #10791)
3135 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
3136 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
3137 for (; it != i_end; ++it) {
3138 if (it->lyxCode() != CAPTION_CODE)
3140 it->latex(os, runparams);
3143 } else if (!isPartOfMultiRow(row, c)) {
3144 if (!runparams.nice)
3145 os.texrow().start(par.id(), 0);
3146 inset->latex(os, newrp);
3149 runparams.encoding = newrp.encoding;
3153 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3154 if (cell != lastcell) { // not last cell in row
3161 os << "\\tabularnewline";
3162 if (row_info[row].bottom_space_default) {
3164 os << "\\addlinespace";
3166 os << "[\\doublerulesep]";
3167 } else if (!row_info[row].bottom_space.zero()) {
3169 os << "\\addlinespace";
3171 << from_ascii(row_info[row].bottom_space.asLatexString())
3176 //output the bottom line
3177 TeXBottomHLine(os, row, columns, logical_columns);
3179 if (row_info[row].interline_space_default) {
3181 os << "\\addlinespace\n";
3183 os << "\\noalign{\\vskip\\doublerulesep}\n";
3184 } else if (!row_info[row].interline_space.zero()) {
3186 os << "\\addlinespace["
3187 << from_ascii(row_info[row].interline_space.asLatexString())
3190 os << "\\noalign{\\vskip"
3191 << from_ascii(row_info[row].interline_space.asLatexString())
3197 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3199 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3200 && !hasVarwidthColumn();
3201 bool const is_xltabular = is_long_tabular
3202 && (hasVarwidthColumn() || !tabular_width.zero());
3203 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3205 //+---------------------------------------------------------------------
3206 //+ first the opening preamble +
3207 //+---------------------------------------------------------------------
3210 if (!TexRow::isNone(pos))
3211 os.texrow().start(pos);
3214 if (is_long_tabular)
3215 os << "\\begin{landscape}\n";
3217 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3220 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3221 // order for RTL (#9686). Thus we use this list.
3222 bool const bidi_rtl =
3223 runparams.local_font->isRightToLeft()
3224 && runparams.useBidiPackage();
3225 list<col_type> columns;
3226 list<col_type> logical_columns;
3227 for (col_type cl = 0; cl < ncols(); ++cl) {
3228 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3231 columns.push_front(cl);
3233 columns.push_back(cl);
3234 // for some calculations, we need the logical (non-swapped)
3235 // columns also in bidi.
3236 logical_columns.push_back(cl);
3239 // If we use \cline or \cmidrule, we need to locally de-activate
3240 // the - character when using languages that activate it (e.g., Czech, Slovak).
3241 bool deactivate_chars = false;
3242 if ((runparams.use_babel || runparams.use_polyglossia)
3243 && contains(runparams.active_chars, '-')) {
3244 bool have_clines = false;
3245 // Check if we use \cline or \cmidrule
3246 for (row_type row = 0; row < nrows(); ++row) {
3247 col_type bset = 0, tset = 0;
3248 for (auto const & c : columns) {
3249 idx_type const idx = cellIndex(row, c);
3250 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3251 || topLineTrim(idx).first || topLineTrim(idx).second) {
3255 if (bottomLine(cellIndex(row, c)))
3257 if (topLine(cellIndex(row, c)))
3260 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3266 deactivate_chars = true;
3267 os << "\\begingroup\n"
3268 << "\\catcode`\\-=12\n";
3272 if (is_long_tabular) {
3274 os << "\\begin{xltabular}";
3276 os << "\\begin{longtable}";
3277 switch (longtabular_alignment) {
3278 case LYX_LONGTABULAR_ALIGN_LEFT:
3281 case LYX_LONGTABULAR_ALIGN_CENTER:
3284 case LYX_LONGTABULAR_ALIGN_RIGHT:
3289 if (tabular_width.zero())
3290 os << "{" << from_ascii("\\columnwidth") << "}";
3292 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3295 if (is_tabular_star)
3296 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3297 else if (hasVarwidthColumn()) {
3298 os << "\\begin{tabularx}{";
3299 if (tabular_width.zero())
3300 os << from_ascii("\\columnwidth") << "}";
3302 os << from_ascii(tabular_width.asLatexString()) << "}";
3304 os << "\\begin{tabular}";
3305 switch (tabular_valignment) {
3306 case LYX_VALIGN_TOP:
3309 case LYX_VALIGN_MIDDLE:
3311 case LYX_VALIGN_BOTTOM:
3319 if (is_tabular_star)
3320 os << "@{\\extracolsep{\\fill}}";
3322 for (auto const & c : columns) {
3323 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3325 if (!column_info[c].align_special.empty()) {
3326 os << column_info[c].align_special;
3328 if (!column_info[c].p_width.zero()) {
3329 bool decimal = false;
3330 switch (column_info[c].alignment) {
3331 case LYX_ALIGN_LEFT:
3332 os << ">{\\raggedright}";
3334 case LYX_ALIGN_RIGHT:
3335 os << ">{\\raggedleft}";
3337 case LYX_ALIGN_CENTER:
3338 os << ">{\\centering}";
3340 case LYX_ALIGN_NONE:
3341 case LYX_ALIGN_BLOCK:
3342 case LYX_ALIGN_LAYOUT:
3343 case LYX_ALIGN_SPECIAL:
3345 case LYX_ALIGN_DECIMAL: {
3347 os << ">{\\raggedright}";
3349 os << ">{\\raggedleft}";
3356 switch (column_info[c].valignment) {
3357 case LYX_VALIGN_TOP:
3358 // this is the default
3360 case LYX_VALIGN_MIDDLE:
3363 case LYX_VALIGN_BOTTOM:
3369 // Fixed-width cells with alignment at decimal separator
3370 // are output as two cells of half the width with the decimal
3371 // separator as column sep. This effectively puts the content
3372 // centered, which differs from the normal decimal sep alignment
3373 // and is not ideal, but we cannot do better ATM (see #9568).
3374 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3376 docstring const halffixedwith =
3377 from_ascii(Length(column_info[c].p_width.value() / 2,
3378 column_info[c].p_width.unit()).asLatexString());
3382 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3389 << from_ascii(column_info[c].p_width.asLatexString())
3391 } else if (column_info[c].varwidth) {
3392 switch (column_info[c].alignment) {
3393 case LYX_ALIGN_LEFT:
3394 os << ">{\\raggedright\\arraybackslash}";
3396 case LYX_ALIGN_RIGHT:
3397 os << ">{\\raggedleft\\arraybackslash}";
3399 case LYX_ALIGN_CENTER:
3400 os << ">{\\centering\\arraybackslash}";
3402 case LYX_ALIGN_NONE:
3403 case LYX_ALIGN_BLOCK:
3404 case LYX_ALIGN_LAYOUT:
3405 case LYX_ALIGN_SPECIAL:
3406 case LYX_ALIGN_DECIMAL:
3410 } else if (isVTypeColumn(c)) {
3411 switch (column_info[c].alignment) {
3412 case LYX_ALIGN_LEFT:
3413 os << ">{\\raggedright}";
3415 case LYX_ALIGN_RIGHT:
3416 os << ">{\\raggedleft}";
3418 case LYX_ALIGN_CENTER:
3419 os << ">{\\centering}";
3421 case LYX_ALIGN_NONE:
3422 case LYX_ALIGN_BLOCK:
3423 case LYX_ALIGN_LAYOUT:
3424 case LYX_ALIGN_SPECIAL:
3425 case LYX_ALIGN_DECIMAL:
3428 os << "V{\\linewidth}";
3430 switch (column_info[c].alignment) {
3431 case LYX_ALIGN_LEFT:
3434 case LYX_ALIGN_RIGHT:
3437 case LYX_ALIGN_DECIMAL:
3438 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3444 } // end if else !column_info[i].p_width
3445 } // end if else !column_info[i].align_special
3446 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3451 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3453 //+---------------------------------------------------------------------
3454 //+ the single row and columns (cells) +
3455 //+---------------------------------------------------------------------
3457 for (row_type r = 0; r < nrows(); ++r) {
3458 if (!buffer().params().output_changes && row_info[r].change.deleted())
3460 if (isValidRow(r)) {
3461 TeXRow(os, r, runparams, columns, logical_columns);
3462 if (is_long_tabular && row_info[r].newpage)
3463 os << "\\newpage\n";
3467 //+---------------------------------------------------------------------
3468 //+ the closing of the tabular +
3469 //+---------------------------------------------------------------------
3471 if (is_long_tabular) {
3473 os << "\\end{xltabular}";
3475 os << "\\end{longtable}";
3477 if (is_tabular_star)
3478 os << "\\end{tabular*}";
3479 else if (hasVarwidthColumn())
3480 os << "\\end{tabularx}";
3482 os << "\\end{tabular}";
3485 if (deactivate_chars)
3487 os << "\n\\endgroup\n";
3490 if (is_long_tabular)
3491 os << breakln << "\\end{landscape}";
3493 os << breakln << "\\end{turn}";
3496 if (!TexRow::isNone(pos))
3497 os.texrow().start(pos);
3501 void Tabular::docbookRow(XMLStream & xs, row_type row,
3502 OutputParams const & runparams, bool header) const
3504 switch (buffer().params().docbook_table_output) {
3505 case BufferParams::HTMLTable:
3506 docbookRowAsHTML(xs, row, runparams, header);
3508 case BufferParams::CALSTable:
3509 docbookRowAsCALS(xs, row, runparams);
3515 void Tabular::docbookRowAsHTML(XMLStream & xs, row_type row,
3516 OutputParams const & runparams, bool header) const
3518 string const celltag = header ? "th" : "td";
3519 idx_type cell = getFirstCellInRow(row);
3521 xs << xml::StartTag("tr");
3523 for (col_type c = 0; c < ncols(); ++c) {
3524 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3529 Length const cwidth = column_info[c].p_width;
3530 if (!cwidth.zero()) {
3531 string const hwidth = cwidth.asHTMLString();
3532 attr << "style =\"width: " << hwidth << ";\" ";
3536 switch (getAlignment(cell)) {
3537 case LYX_ALIGN_BLOCK:
3540 case LYX_ALIGN_DECIMAL: {
3541 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3542 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3545 case LYX_ALIGN_LEFT:
3548 case LYX_ALIGN_RIGHT:
3556 attr << " valign='";
3557 switch (getVAlignment(cell)) {
3558 case LYX_VALIGN_TOP:
3561 case LYX_VALIGN_BOTTOM:
3564 case LYX_VALIGN_MIDDLE:
3569 if (isMultiColumn(cell))
3570 attr << " colspan='" << columnSpan(cell) << "'";
3571 else if (isMultiRow(cell))
3572 attr << " rowspan='" << rowSpan(cell) << "'";
3574 xs << xml::StartTag(celltag, attr.str(), true);
3575 cellInset(cell)->docbook(xs, runparams);
3576 xs << xml::EndTag(celltag);
3580 xs << xml::EndTag("tr");
3585 void Tabular::docbookRowAsCALS(XMLStream & xs, row_type row,
3586 OutputParams const & runparams) const
3588 idx_type cell = getFirstCellInRow(row);
3590 xs << xml::StartTag("row");
3592 for (col_type c = 0; c < ncols(); ++c) {
3593 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3599 switch (getAlignment(cell)) {
3600 case LYX_ALIGN_BLOCK:
3603 case LYX_ALIGN_DECIMAL: {
3604 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3605 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3608 case LYX_ALIGN_LEFT:
3611 case LYX_ALIGN_RIGHT:
3620 attr << " valign='";
3621 switch (getVAlignment(cell)) {
3622 case LYX_VALIGN_TOP:
3625 case LYX_VALIGN_BOTTOM:
3628 case LYX_VALIGN_MIDDLE:
3633 if (isMultiColumn(cell))
3634 attr << " colspan='" << columnSpan(cell) << "'";
3635 else if (isMultiRow(cell))
3636 attr << " rowspan='" << rowSpan(cell) << "'";
3638 attr << " colname='c" << (c + 1) << "'"; // Column numbering starts at 1.
3640 // All cases where there should be a line *below* this row.
3641 if (row_info[row].bottom_space_default)
3642 attr << " rowsep='1'";
3644 xs << xml::StartTag("entry", attr.str(), true);
3645 cellInset(cell)->docbook(xs, runparams);
3646 xs << xml::EndTag("entry");
3650 xs << xml::EndTag("row");
3655 void Tabular::docbook(XMLStream & xs, OutputParams const & runparams) const
3659 // Some tables are inline. Likely limitation: cannot output a table within a table; is that really a limitation?
3660 if (!runparams.docbook_in_table) { // Check on the *outer* set of parameters, so that the table can be closed
3661 // properly at the end of this function.
3662 xs << xml::StartTag("informaltable");
3666 // "Formal" tables have a title and use the tag <table>; the distinction with <informaltable> is done outside.
3667 // HTML has the caption first with titles forbidden, and CALS has a title first.
3668 if (haveLTCaption()) {
3669 std::string tag = ((buffer().params().docbook_table_output) == BufferParams::HTMLTable) ? "caption" : "title";
3671 xs << xml::StartTag(tag);
3672 for (row_type r = 0; r < nrows(); ++r)
3673 if (row_info[r].caption)
3674 docbookRow(xs, r, runparams);
3675 xs << xml::EndTag(tag);
3679 // CALS header: describe all columns in this table. For names, take 'c' then the ID of the column.
3680 // Start at one, as is customary with CALS!
3681 if (buffer().params().docbook_table_output == BufferParams::CALSTable) {
3682 for (col_type c = 0; c < ncols(); ++c) {
3683 std::stringstream attr;
3684 attr << "colnum='" << (c + 1) << "' ";
3685 attr << "colname='c" << (c + 1) << "' ";
3686 Length const cwidth = column_info[c].p_width;
3688 attr << "colwidth='" << cwidth.asHTMLString() << "' ";
3689 attr << "rowheader='norowheader'"; // Last attribute, hence no space at the end.
3691 xs << xml::CompTag("colspec", attr.str());
3696 // Output the header of the table. For both HTML and CALS, this is surrounded by a thead.
3697 bool const havefirsthead = haveLTFirstHead(false);
3698 // if we have a first head, then we are going to ignore the
3699 // headers for the additional pages, since there aren't any
3700 // in DocBook. this test accomplishes that.
3701 bool const havehead = !havefirsthead && haveLTHead(false);
3702 if (havehead || havefirsthead) {
3703 xs << xml::StartTag("thead") << xml::CR();
3704 for (row_type r = 0; r < nrows(); ++r) {
3705 if (((havefirsthead && row_info[r].endfirsthead) ||
3706 (havehead && row_info[r].endhead)) &&
3707 !row_info[r].caption) {
3708 docbookRow(xs, r, runparams, true); // TODO: HTML vs CALS
3711 xs << xml::EndTag("thead");
3715 // Output the footer of the table. For both HTML and CALS, this is surrounded by a tfoot and output just after
3716 // the header (and before the body).
3717 bool const havelastfoot = haveLTLastFoot(false);
3719 bool const havefoot = !havelastfoot && haveLTFoot(false);
3720 if (havefoot || havelastfoot) {
3721 xs << xml::StartTag("tfoot") << xml::CR();
3722 for (row_type r = 0; r < nrows(); ++r) {
3723 if (((havelastfoot && row_info[r].endlastfoot) ||
3724 (havefoot && row_info[r].endfoot)) &&
3725 !row_info[r].caption) {
3726 docbookRow(xs, r, runparams); // TODO: HTML vs CALS
3729 xs << xml::EndTag("tfoot");
3733 // Output the main part of the table. The tbody container is mandatory for CALS, but optional for HTML (only if
3734 // there is no header and no footer). It never hurts to have it, though.
3735 xs << xml::StartTag("tbody");
3737 for (row_type r = 0; r < nrows(); ++r)
3739 docbookRow(xs, r, runparams);
3740 xs << xml::EndTag("tbody");
3743 // If this method started the table tag, also make it close it.
3744 if (!runparams.docbook_in_table) {
3745 xs << xml::EndTag("informaltable");
3751 docstring Tabular::xhtmlRow(XMLStream & xs, row_type row,
3752 OutputParams const & runparams, bool header) const
3755 string const celltag = header ? "th" : "td";
3756 idx_type cell = getFirstCellInRow(row);
3758 xs << xml::StartTag("tr");
3759 for (col_type c = 0; c < ncols(); ++c) {
3760 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3765 Length const cwidth = column_info[c].p_width;
3766 if (!cwidth.zero()) {
3767 string const hwidth = cwidth.asHTMLString();
3768 attr << "style =\"width: " << hwidth << ";\" ";
3772 switch (getAlignment(cell)) {
3773 case LYX_ALIGN_LEFT:
3776 case LYX_ALIGN_RIGHT:
3784 attr << " valign='";
3785 switch (getVAlignment(cell)) {
3786 case LYX_VALIGN_TOP:
3789 case LYX_VALIGN_BOTTOM:
3792 case LYX_VALIGN_MIDDLE:
3797 if (isMultiColumn(cell))
3798 attr << " colspan='" << columnSpan(cell) << "'";
3799 else if (isMultiRow(cell))
3800 attr << " rowspan='" << rowSpan(cell) << "'";
3802 xs << xml::StartTag(celltag, attr.str(), true) << xml::CR();
3803 ret += cellInset(cell)->xhtml(xs, runparams);
3804 xs << xml::EndTag(celltag) << xml::CR();
3807 xs << xml::EndTag("tr");
3812 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3816 if (is_long_tabular) {
3817 // we'll wrap it in a div, so as to deal with alignment
3819 switch (longtabular_alignment) {
3820 case LYX_LONGTABULAR_ALIGN_LEFT:
3823 case LYX_LONGTABULAR_ALIGN_CENTER:
3826 case LYX_LONGTABULAR_ALIGN_RIGHT:
3830 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
3832 // The caption flag wins over head/foot
3833 if (haveLTCaption()) {
3834 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
3836 for (row_type r = 0; r < nrows(); ++r)
3837 if (row_info[r].caption)
3838 ret += xhtmlRow(xs, r, runparams);
3839 xs << xml::EndTag("div");
3844 xs << xml::StartTag("table");
3847 // output header info
3848 bool const havefirsthead = haveLTFirstHead(false);
3849 // if we have a first head, then we are going to ignore the
3850 // headers for the additional pages, since there aren't any
3851 // in XHTML. this test accomplishes that.
3852 bool const havehead = !havefirsthead && haveLTHead(false);
3853 if (havehead || havefirsthead) {
3854 xs << xml::StartTag("thead");
3856 for (row_type r = 0; r < nrows(); ++r) {
3857 if (((havefirsthead && row_info[r].endfirsthead) ||
3858 (havehead && row_info[r].endhead)) &&
3859 !row_info[r].caption) {
3860 ret += xhtmlRow(xs, r, runparams, true);
3863 xs << xml::EndTag("thead");
3866 // output footer info
3867 bool const havelastfoot = haveLTLastFoot(false);
3869 bool const havefoot = !havelastfoot && haveLTFoot(false);
3870 if (havefoot || havelastfoot) {
3871 xs << xml::StartTag("tfoot") << xml::CR();
3872 for (row_type r = 0; r < nrows(); ++r) {
3873 if (((havelastfoot && row_info[r].endlastfoot) ||
3874 (havefoot && row_info[r].endfoot)) &&
3875 !row_info[r].caption) {
3876 ret += xhtmlRow(xs, r, runparams);
3879 xs << xml::EndTag("tfoot");
3883 xs << xml::StartTag("tbody") << xml::CR();
3884 for (row_type r = 0; r < nrows(); ++r)
3886 ret += xhtmlRow(xs, r, runparams);
3887 xs << xml::EndTag("tbody");
3889 xs << xml::EndTag("table");
3891 if (is_long_tabular) {
3892 xs << xml::EndTag("div");
3899 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3900 vector<unsigned int> const & clen) const
3902 idx_type const fcell = getFirstCellInRow(row);
3903 idx_type const n = numberOfCellsInRow(row) + fcell;
3906 for (idx_type i = fcell; i < n; ++i) {
3916 for (idx_type i = fcell; i < n; ++i) {
3927 col_type column = cellColumn(i);
3928 int len = clen[column];
3929 while (column < ncols() - 1
3930 && isPartOfMultiColumn(row, ++column))
3931 len += clen[column] + 4;
3932 os << docstring(len, ch);
3947 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3948 vector<unsigned int> const & clen) const
3950 idx_type const fcell = getFirstCellInRow(row);
3951 idx_type const n = numberOfCellsInRow(row) + fcell;
3954 for (idx_type i = fcell; i < n; ++i) {
3955 if (bottomLine(i)) {
3964 for (idx_type i = fcell; i < n; ++i) {
3965 if (bottomLine(i)) {
3975 col_type column = cellColumn(i);
3976 int len = clen[column];
3977 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3978 len += clen[column] + 4;
3979 os << docstring(len, ch);
3980 if (bottomLine(i)) {
3994 void Tabular::plaintextPrintCell(odocstringstream & os,
3995 OutputParams const & runparams,
3996 idx_type cell, row_type row, col_type column,
3997 vector<unsigned int> const & clen,
3998 bool onlydata, size_t max_length) const
4000 odocstringstream sstr;
4001 cellInset(cell)->plaintext(sstr, runparams, max_length);
4013 unsigned int len1 = sstr.str().length();
4014 unsigned int len2 = clen[column];
4015 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4016 len2 += clen[column] + 4;
4019 switch (getAlignment(cell)) {
4021 case LYX_ALIGN_LEFT:
4024 case LYX_ALIGN_RIGHT:
4028 case LYX_ALIGN_CENTER:
4034 os << docstring(len1, ' ') << sstr.str()
4035 << docstring(len2, ' ');
4037 if (rightLine(cell))
4044 void Tabular::plaintext(odocstringstream & os,
4045 OutputParams const & runparams, int const depth,
4046 bool onlydata, char_type delim, size_t max_length) const
4048 // first calculate the width of the single columns
4049 vector<unsigned int> clen(ncols());
4052 // first all non multicolumn cells!
4053 for (col_type c = 0; c < ncols(); ++c) {
4055 for (row_type r = 0; r < nrows(); ++r) {
4056 idx_type cell = cellIndex(r, c);
4057 if (isMultiColumn(cell))
4059 odocstringstream sstr;
4060 cellInset(cell)->plaintext(sstr, runparams, max_length);
4061 if (clen[c] < sstr.str().length())
4062 clen[c] = sstr.str().length();
4065 // then all multicolumn cells!
4066 for (col_type c = 0; c < ncols(); ++c) {
4067 for (row_type r = 0; r < nrows(); ++r) {
4068 idx_type cell = cellIndex(r, c);
4069 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
4071 odocstringstream sstr;
4072 cellInset(cell)->plaintext(sstr, runparams, max_length);
4073 int len = int(sstr.str().length());
4074 idx_type const n = columnSpan(cell);
4075 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
4077 if (len > int(clen[c + n - 1]))
4078 clen[c + n - 1] = len;
4083 for (row_type r = 0; r < nrows(); ++r) {
4084 if (!onlydata && plaintextTopHLine(os, r, clen))
4085 os << docstring(depth * 2, ' ');
4086 for (col_type c = 0; c < ncols(); ++c) {
4087 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
4089 if (onlydata && c > 0)
4090 // we don't use operator<< for single UCS4 character.
4091 // see explanation in docstream.h
4093 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
4095 if (os.str().size() > max_length)
4100 os << docstring(depth * 2, ' ');
4101 if (plaintextBottomHLine(os, r, clen))
4102 os << docstring(depth * 2, ' ');
4104 if (os.str().size() > max_length)
4110 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4112 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4116 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4118 return cell_info[row][column].inset;
4122 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4124 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4128 void Tabular::setCellInset(row_type row, col_type column,
4129 shared_ptr<InsetTableCell> ins)
4131 CellData & cd = cell_info[row][column];
4136 void Tabular::validate(LaTeXFeatures & features) const
4138 features.require("NeedTabularnewline");
4140 features.require("booktabs");
4141 if (is_long_tabular && !hasVarwidthColumn()) {
4142 if (tabular_width.zero())
4143 features.require("longtable");
4145 features.require("xltabular");
4147 if (rotate && is_long_tabular)
4148 features.require("lscape");
4150 features.require("rotating");
4151 if (hasVarwidthColumn()) {
4152 if (is_long_tabular)
4153 features.require("xltabular");
4155 features.require("tabularx");
4157 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4158 if (isMultiRow(cell))
4159 features.require("multirow");
4160 if (getUsebox(cell) == BOX_VARWIDTH)
4161 features.require("varwidth");
4162 if (getVAlignment(cell) != LYX_VALIGN_TOP
4163 || !getPWidth(cell).zero()
4164 || isVTypeColumn(cellColumn(cell)))
4165 features.require("array");
4166 // Tell footnote that we need a savenote
4167 // environment in non-long tables or
4168 // longtable headers/footers
4169 else if (!is_long_tabular && !features.inFloat())
4170 features.saveNoteEnv("tabular");
4171 else if (!isValidRow(cellRow(cell)))
4172 features.saveNoteEnv("longtable");
4174 cellInset(cell)->validate(features);
4175 features.saveNoteEnv(string());
4180 Tabular::BoxType Tabular::useBox(idx_type cell) const
4182 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4183 if (parlist.size() > 1)
4184 return BOX_VARWIDTH;
4186 ParagraphList::const_iterator cit = parlist.begin();
4187 ParagraphList::const_iterator end = parlist.end();
4189 for (; cit != end; ++cit)
4190 for (int i = 0; i < cit->size(); ++i)
4191 if (cit->isNewline(i) || cit->layout().isEnvironment())
4192 return BOX_VARWIDTH;
4198 /////////////////////////////////////////////////////////////////////
4202 /////////////////////////////////////////////////////////////////////
4204 InsetTableCell::InsetTableCell(Buffer * buf)
4205 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
4206 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4209 bool InsetTableCell::forcePlainLayout(idx_type) const
4211 return isMultiRow || (isMultiColumn && !isFixedWidth);
4215 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4217 return isFixedWidth;
4221 bool InsetTableCell::forceLocalFontSwitch() const
4227 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4228 FuncStatus & status) const
4230 bool enabled = true;
4231 switch (cmd.action()) {
4232 case LFUN_INSET_DISSOLVE:
4235 case LFUN_MATH_DISPLAY:
4236 if (!hasFixedWidth()) {
4241 return InsetText::getStatus(cur, cmd, status);
4243 status.setEnabled(enabled);
4247 docstring InsetTableCell::asString(bool intoInsets)
4250 if (paragraphs().empty())
4252 ParagraphList::const_iterator it = paragraphs().begin();
4253 ParagraphList::const_iterator en = paragraphs().end();
4255 for (; it != en; ++it) {
4260 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4266 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4267 UpdateType utype, TocBackend & backend) const
4269 InsetText::iterateForToc(di, output_active, utype, backend);
4273 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4276 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4277 return InsetText::xhtml(xs, rp);
4281 void InsetTableCell::docbook(XMLStream & xs, OutputParams const & runparams) const
4283 InsetText::docbook(xs, runparams);
4287 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4289 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4290 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4292 // Hand font through to contained lyxtext:
4293 tm.font_.fontInfo() = mi.base.font;
4294 mi.base.textwidth -= horiz_offset;
4296 // This can happen when a layout has a left and right margin,
4297 // and the view is made very narrow. We can't do better than
4298 // to draw it partly out of view (bug 5890).
4299 if (mi.base.textwidth < 1)
4300 mi.base.textwidth = 1;
4302 // We tell metrics here not to expand on multiple pars
4303 // This is the difference to InsetText::Metrics
4304 if (hasFixedWidth())
4305 tm.metrics(mi, dim, mi.base.textwidth, false);
4307 tm.metrics(mi, dim, 0, false);
4308 mi.base.textwidth += horiz_offset;
4309 dim.asc += topOffset(mi.base.bv);
4310 dim.des += bottomOffset(mi.base.bv);
4311 dim.wid += horiz_offset;
4315 /////////////////////////////////////////////////////////////////////
4319 /////////////////////////////////////////////////////////////////////
4321 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4323 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4324 rowselect_(false), colselect_(false)
4329 InsetTabular::InsetTabular(InsetTabular const & tab)
4330 : Inset(tab), tabular(tab.tabular),
4331 rowselect_(false), colselect_(false)
4336 InsetTabular::~InsetTabular()
4338 hideDialogs("tabular", this);
4342 void InsetTabular::setBuffer(Buffer & buf)
4344 tabular.setBuffer(buf);
4345 Inset::setBuffer(buf);
4349 bool InsetTabular::insetAllowed(InsetCode code) const
4354 case MATHMACRO_CODE:
4359 return tabular.is_long_tabular;
4367 bool InsetTabular::allowMultiPar() const
4369 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
4370 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
4371 if (tabular.cellInset(r,c)->allowMultiPar())
4379 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4381 return tabular.is_long_tabular &&
4382 (newtype == "Standard" || newtype == "Unnumbered");
4386 void InsetTabular::write(ostream & os) const
4388 os << "Tabular" << endl;
4393 string InsetTabular::contextMenu(BufferView const &, int, int) const
4395 // FIXME: depending on the selection state,
4396 // we could offer a different menu.
4397 return cell(0)->contextMenuName() + ";" + contextMenuName();
4401 string InsetTabular::contextMenuName() const
4403 return "context-tabular";
4407 void InsetTabular::read(Lexer & lex)
4409 //bool const old_format = (lex.getString() == "\\LyXTable");
4417 string token = lex.getString();
4418 while (lex && token != "\\end_inset") {
4420 token = lex.getString();
4423 lex.printError("Missing \\end_inset at this point. ");
4427 int InsetTabular::rowFromY(Cursor & cur, int y) const
4429 // top y coordinate of tabular
4430 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4432 for (; r < tabular.nrows() && y > h; ++r)
4433 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4434 + tabular.interRowSpace(r);
4440 int InsetTabular::columnFromX(Cursor & cur, int x) const
4442 // left x coordinate of tabular
4443 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4445 for (; c < tabular.ncols() && x > w; ++c)
4446 w += tabular.cellWidth(c);
4451 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4453 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4454 // mi.base.textwidth << "\n";
4455 LBUFERR(mi.base.bv);
4457 for (row_type r = 0; r < tabular.nrows(); ++r) {
4460 for (col_type c = 0; c < tabular.ncols(); ++c) {
4461 if (tabular.isPartOfMultiColumn(r, c)
4462 || tabular.isPartOfMultiRow(r, c))
4463 // multicolumn or multirow cell, but not first one
4465 idx_type const cell = tabular.cellIndex(r, c);
4468 Length const p_width = tabular.getPWidth(cell);
4469 if (!p_width.zero())
4470 m.base.textwidth = mi.base.inPixels(p_width);
4471 else if (tabular.column_info[c].varwidth)
4472 m.base.textwidth = tabular.column_info[c].width;
4473 tabular.cellInset(cell)->metrics(m, dim0);
4474 if (!p_width.zero() || tabular.column_info[c].varwidth)
4475 dim0.wid = m.base.textwidth;
4476 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4477 + tabular.interColumnSpace(cell);
4479 // FIXME(?): do we need a second metrics call?
4480 TextMetrics const & tm =
4481 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4483 // determine horizontal offset because of decimal align (if necessary)
4484 int decimal_width = 0;
4485 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4486 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4487 tail.setBuffer(tabular.buffer());
4488 // we need to set macrocontext position everywhere
4489 // otherwise we crash with nested insets (e.g. footnotes)
4490 // after decimal point
4491 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4493 dit.push_back(CursorSlice(tail));
4494 tail.setMacrocontextPositionRecursive(dit);
4496 // remove text leading decimal point
4497 docstring const align_d = tabular.column_info[c].decimal_point;
4498 dit = separatorPos(&tail, align_d);
4500 pos_type const psize = tail.paragraphs().front().size();
4502 tail.paragraphs().front().eraseChars(0,
4503 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4505 tail.metrics(m, dim1);
4506 decimal_width = dim1.width();
4509 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4510 tabular.cell_info[r][c].decimal_width = decimal_width;
4512 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4513 // = descent of text in last par + bottomOffset:
4514 int const lastpardes = tm.last().second->descent()
4515 + bottomOffset(mi.base.bv);
4517 switch (tabular.getVAlignment(cell)) {
4518 case Tabular::LYX_VALIGN_TOP:
4520 case Tabular::LYX_VALIGN_MIDDLE:
4521 offset = -(dim0.des - lastpardes)/2;
4523 case Tabular::LYX_VALIGN_BOTTOM:
4524 offset = -(dim0.des - lastpardes);
4527 tabular.cell_info[r][c].voffset = offset;
4528 maxasc = max(maxasc, dim0.asc - offset);
4529 maxdes = max(maxdes, dim0.des + offset);
4531 int const top_space = tabular.row_info[r].top_space_default ?
4532 default_line_space :
4533 mi.base.inPixels(tabular.row_info[r].top_space);
4534 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4535 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4536 default_line_space :
4537 mi.base.inPixels(tabular.row_info[r].bottom_space);
4538 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4541 // We need to recalculate the metrics after column width calculation
4542 // with xtabular (possibly multiple times, so the call is recursive).
4543 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4545 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4546 dim.des = tabular.height() - dim.asc;
4547 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4551 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4553 if (&cur.inset() == this && cur.selection()) {
4554 if (cur.selIsMultiCell()) {
4557 getSelection(cur, rs, re, cs, ce);
4559 idx_type const cell = tabular.cellIndex(row, col);
4560 col_type const cspan = tabular.columnSpan(cell);
4561 row_type const rspan = tabular.rowSpan(cell);
4562 if (col + cspan - 1 >= cs && col <= ce
4563 && row + rspan - 1 >= rs && row <= re)
4566 if (col == tabular.cellColumn(cur.idx())
4567 && row == tabular.cellRow(cur.idx())) {
4568 CursorSlice const & beg = cur.selBegin();
4569 CursorSlice const & end = cur.selEnd();
4571 if ((end.lastpos() > 0 || end.lastpit() > 0)
4572 && end.pos() == end.lastpos() && beg.pos() == 0
4573 && end.pit() == end.lastpit() && beg.pit() == 0)
4581 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4583 x += ADD_TO_TABULAR_WIDTH;
4585 BufferView * bv = pi.base.bv;
4586 Cursor & cur = pi.base.bv->cursor();
4588 // FIXME: As the full background is painted in drawBackground(),
4589 // we have no choice but to do a full repaint for the Text cells.
4590 pi.full_repaint = true;
4592 bool const original_selection_state = pi.selected;
4596 // Save tabular change status
4597 Change tab_change = pi.change;
4599 int yy = y + tabular.offsetVAlignment();
4600 for (row_type r = 0; r < tabular.nrows(); ++r) {
4602 for (col_type c = 0; c < tabular.ncols(); ++c) {
4603 if (tabular.isPartOfMultiColumn(r, c))
4606 idx = tabular.cellIndex(r, c);
4608 if (tabular.isPartOfMultiRow(r, c)) {
4609 nx += tabular.cellWidth(idx);
4613 pi.selected |= isCellSelected(cur, r, c);
4615 // Mark deleted rows/columns
4616 if (tabular.column_info[c].change.changed())
4617 pi.change = tabular.column_info[c].change;
4618 else if (tabular.row_info[r].change.changed())
4619 pi.change = tabular.row_info[r].change;
4621 pi.change = tab_change;
4623 int const cx = nx + tabular.textHOffset(idx);
4624 int const cy = yy + tabular.textVOffset(idx);
4625 // Cache the Inset position.
4626 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4627 cell(idx)->draw(pi, cx, cy);
4628 drawCellLines(pi, nx, yy, r, idx);
4629 nx += tabular.cellWidth(idx);
4630 pi.selected = original_selection_state;
4633 if (r + 1 < tabular.nrows())
4634 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4635 + tabular.interRowSpace(r + 1);
4640 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4642 x += ADD_TO_TABULAR_WIDTH;
4643 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4644 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4645 pi.backgroundColor(this));
4649 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4651 Cursor & cur = pi.base.bv->cursor();
4653 x += ADD_TO_TABULAR_WIDTH;
4655 if (!cur.selection())
4657 if (&cur.inset() != this)
4662 bool const full_cell_selected = isCellSelected(cur,
4663 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4665 if (cur.selIsMultiCell() || full_cell_selected) {
4666 for (row_type r = 0; r < tabular.nrows(); ++r) {
4668 for (col_type c = 0; c < tabular.ncols(); ++c) {
4669 if (tabular.isPartOfMultiColumn(r, c))
4672 idx_type const cell = tabular.cellIndex(r, c);
4674 if (tabular.isPartOfMultiRow(r, c)) {
4675 xx += tabular.cellWidth(cell);
4678 int const w = tabular.cellWidth(cell);
4679 int const h = tabular.cellHeight(cell);
4680 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4681 if (isCellSelected(cur, r, c))
4682 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4685 if (r + 1 < tabular.nrows())
4686 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4687 + tabular.interRowSpace(r + 1);
4691 // FIXME: This code has no effect because InsetTableCell does not handle
4692 // drawSelection other than the trivial implementation in Inset.
4694 // x += cellXPos(cur.idx());
4695 // x += tabular.textHOffset(cur.idx());
4696 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4703 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4704 Color const incol, bool drawline, bool heavy = false)
4706 Color const col = drawline ? incol : Color_tabularonoffline;
4707 if (drawline && lt > 0)
4708 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4709 Painter::line_onoffdash,
4710 Painter::thin_line);
4711 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4712 drawline ? Painter::line_solid : Painter::line_onoffdash,
4713 (heavy ? 2 : 1) * Painter::thin_line);
4714 if (drawline && rt > 0)
4715 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4716 Painter::line_onoffdash,
4717 Painter::thin_line);
4723 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4724 row_type row, idx_type cell) const
4726 y -= tabular.rowAscent(row);
4727 int const w = tabular.cellWidth(cell);
4728 int const h = tabular.cellHeight(cell);
4732 col_type const col = tabular.cellColumn(cell);
4734 // Colour the frame if rows/columns are added or deleted
4735 Color colour = Color_tabularline;
4736 if (tabular.column_info[col].change.changed()
4737 || tabular.row_info[row].change.changed())
4738 colour = InsetTableCell(*tabular.cellInset(cell)).paragraphs().front().lookupChange(0).color();
4741 bool drawline = tabular.topLine(cell)
4742 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4743 bool heavy = tabular.use_booktabs
4744 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4745 && tabular.rowTopLine(row);
4746 if (tabular.topLineTrim(cell).first
4747 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4749 if (tabular.topLineTrim(cell).second
4750 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4752 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4756 drawline = tabular.bottomLine(cell);
4757 row_type const lastrow = tabular.nrows() - 1;
4758 // Consider multi-rows
4760 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4761 && tabular.isPartOfMultiRow(r + 1, col))
4763 heavy = tabular.use_booktabs
4764 && ((row == lastrow && tabular.rowBottomLine(row))
4765 || (r == lastrow && tabular.rowBottomLine(r)));
4766 if (tabular.bottomLineTrim(cell).first)
4768 if (tabular.bottomLineTrim(cell).second)
4770 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4773 drawline = tabular.leftLine(cell)
4774 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4775 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4778 x -= tabular.interColumnSpace(cell);
4779 col_type next_cell_col = col + 1;
4780 while (next_cell_col < tabular.ncols()
4781 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4783 drawline = tabular.rightLine(cell)
4784 || (next_cell_col < tabular.ncols()
4785 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4786 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4790 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4792 //lyxerr << "InsetTabular::edit: " << this << endl;
4796 if (isRightToLeft(cur))
4797 cur.idx() = tabular.getLastCellInRow(0);
4803 if (isRightToLeft(cur))
4804 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4806 cur.idx() = tabular.numberofcells - 1;
4808 cur.pos() = cur.lastpos(); // FIXME crude guess
4810 cur.setCurrentFont();
4811 // FIXME: this accesses the position cache before it is initialized
4812 //cur.bv().fitCursor();
4816 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4818 // In a longtable, tell captions what the current float is
4819 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4820 string const saveflt = cnts.current_float();
4821 if (tabular.is_long_tabular) {
4822 cnts.current_float("table");
4823 // in longtables, we only step the counter once
4824 cnts.step(from_ascii("table"), utype);
4825 cnts.isLongtable(true);
4828 ParIterator it2 = it;
4830 size_t const end = it2.nargs();
4831 for ( ; it2.idx() < end; it2.top().forwardIdx())
4832 buffer().updateBuffer(it2, utype);
4835 if (tabular.is_long_tabular) {
4836 cnts.current_float(saveflt);
4837 cnts.isLongtable(false);
4842 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4843 UpdateType utype, TocBackend & backend) const
4845 DocIterator dit = cpit;
4847 size_t const end = dit.nargs();
4848 for ( ; dit.idx() < end; dit.top().forwardIdx())
4849 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4853 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4855 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4856 return x < x0 || x > x0 + tabular.width();
4860 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4862 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4863 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4864 // there is no margin added vertically to tabular insets.
4865 // However, it works for now.
4866 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4870 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4872 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4876 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4878 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4879 << "\n cur:" << cur);
4880 CursorSlice sl = cur.top();
4881 Cursor & bvcur = cur.bv().cursor();
4883 FuncCode const act = cmd.action();
4887 case LFUN_MOUSE_PRESS: {
4888 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4890 if (hitSelectRow(cur.bv(), cmd.x())) {
4891 row_type r = rowFromY(cur, cmd.y());
4892 cur.idx() = tabular.getFirstCellInRow(r);
4896 cur.idx() = tabular.getLastCellInRow(r);
4897 cur.pit() = cur.lastpit();
4898 cur.pos() = cur.lastpos();
4899 cur.selection(true);
4905 if (hitSelectColumn(cur.bv(), cmd.y())) {
4906 col_type c = columnFromX(cur, cmd.x());
4907 cur.idx() = tabular.cellIndex(0, c);
4911 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4912 cur.pit() = cur.lastpit();
4913 cur.pos() = cur.lastpos();
4914 cur.selection(true);
4919 // do not reset cursor/selection if we have selected
4920 // some cells (bug 2715).
4921 if (cmd.button() == mouse_button::button3
4922 && &bvcur.selBegin().inset() == this
4923 && bvcur.selIsMultiCell())
4926 // Let InsetTableCell do it
4927 cell(cur.idx())->dispatch(cur, cmd);
4930 case LFUN_MOUSE_MOTION:
4931 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4932 if (cmd.button() == mouse_button::button1) {
4933 // only accept motions to places not deeper nested than the real anchor
4934 if (!bvcur.realAnchor().hasPart(cur)) {
4938 // select (additional) row
4940 row_type r = rowFromY(cur, cmd.y());
4941 cur.idx() = tabular.getLastCellInRow(r);
4942 // we need to reset the cursor's pit and pos now, as the old ones
4943 // may no longer be valid.
4946 bvcur.setCursor(cur);
4947 bvcur.selection(true);
4950 // select (additional) column
4952 col_type c = columnFromX(cur, cmd.x());
4953 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4954 // we need to reset the cursor's pit and pos now, as the old ones
4955 // may no longer be valid.
4958 bvcur.setCursor(cur);
4959 bvcur.selection(true);
4962 // only update if selection changes
4963 if (bvcur.idx() == cur.idx() &&
4964 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4965 cur.noScreenUpdate();
4966 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4967 bvcur.setCursor(cur);
4968 bvcur.selection(true);
4969 // if this is a multicell selection, we just set the cursor to
4970 // the beginning of the cell's text.
4971 if (bvcur.selIsMultiCell()) {
4972 bvcur.pit() = bvcur.lastpit();
4973 bvcur.pos() = bvcur.lastpos();
4978 case LFUN_MOUSE_RELEASE:
4983 case LFUN_CELL_BACKWARD:
4985 cur.selection(false);
4988 case LFUN_CELL_FORWARD:
4990 cur.selection(false);
4993 case LFUN_CHAR_FORWARD_SELECT:
4994 case LFUN_CHAR_FORWARD:
4995 case LFUN_CHAR_BACKWARD_SELECT:
4996 case LFUN_CHAR_BACKWARD:
4997 case LFUN_CHAR_RIGHT_SELECT:
4998 case LFUN_CHAR_RIGHT:
4999 case LFUN_CHAR_LEFT_SELECT:
5000 case LFUN_CHAR_LEFT:
5001 case LFUN_WORD_FORWARD:
5002 case LFUN_WORD_FORWARD_SELECT:
5003 case LFUN_WORD_BACKWARD:
5004 case LFUN_WORD_BACKWARD_SELECT:
5005 case LFUN_WORD_RIGHT:
5006 case LFUN_WORD_RIGHT_SELECT:
5007 case LFUN_WORD_LEFT:
5008 case LFUN_WORD_LEFT_SELECT: {
5009 // determine whether we move to next or previous cell, where to enter
5010 // the new cell from, and which command to "finish" (i.e., exit the
5013 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
5014 FuncCode finish_lfun;
5016 if (act == LFUN_CHAR_FORWARD
5017 || act == LFUN_CHAR_FORWARD_SELECT
5018 || act == LFUN_WORD_FORWARD
5019 || act == LFUN_WORD_FORWARD_SELECT) {
5021 finish_lfun = LFUN_FINISHED_FORWARD;
5023 else if (act == LFUN_CHAR_BACKWARD
5024 || act == LFUN_CHAR_BACKWARD_SELECT
5025 || act == LFUN_WORD_BACKWARD
5026 || act == LFUN_WORD_BACKWARD_SELECT) {
5028 finish_lfun = LFUN_FINISHED_BACKWARD;
5030 // LEFT or RIGHT commands --- the interpretation will depend on the
5031 // table's direction.
5033 bool const right = act == LFUN_CHAR_RIGHT
5034 || act == LFUN_CHAR_RIGHT_SELECT
5035 || act == LFUN_WORD_RIGHT
5036 || act == LFUN_WORD_RIGHT_SELECT;
5037 next_cell = isRightToLeft(cur) != right;
5039 if (lyxrc.visual_cursor)
5040 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
5042 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
5045 bool const select = act == LFUN_CHAR_FORWARD_SELECT
5046 || act == LFUN_CHAR_BACKWARD_SELECT
5047 || act == LFUN_CHAR_RIGHT_SELECT
5048 || act == LFUN_CHAR_LEFT_SELECT
5049 || act == LFUN_WORD_FORWARD_SELECT
5050 || act == LFUN_WORD_RIGHT_SELECT
5051 || act == LFUN_WORD_BACKWARD_SELECT
5052 || act == LFUN_WORD_LEFT_SELECT;
5054 // If we have a multicell selection or we're
5055 // not doing some LFUN_*_SELECT thing anyway...
5056 if (!cur.selIsMultiCell() || !select) {
5057 col_type const c = tabular.cellColumn(cur.idx());
5058 row_type const r = tabular.cellRow(cur.idx());
5059 // Are we trying to select the whole cell and is the whole cell
5060 // not yet selected?
5061 bool const select_whole = select && !isCellSelected(cur, r, c) &&
5062 ((next_cell && cur.pit() == cur.lastpit()
5063 && cur.pos() == cur.lastpos())
5064 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
5066 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
5068 // ...try to dispatch to the cell's inset.
5069 cell(cur.idx())->dispatch(cur, cmd);
5071 // When we already have a selection we want to select the whole cell
5072 // before going to the next cell.
5073 if (select_whole && !empty_cell){
5074 getText(cur.idx())->selectAll(cur);
5076 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5080 // FIXME: When we support the selection of an empty cell, remove
5081 // the !empty_cell from this condition. For now we jump to the next
5082 // cell if the current cell is empty.
5083 if (cur.result().dispatched() && !empty_cell)
5087 // move to next/prev cell, as appropriate
5088 // note that we will always do this if we're selecting and we have
5089 // a multicell selection
5090 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
5091 << " cell from: " << int(entry_from));
5093 moveNextCell(cur, entry_from);
5095 movePrevCell(cur, entry_from);
5096 // if we're exiting the table, call the appropriate FINISHED lfun
5097 if (sl == cur.top()) {
5098 cmd = FuncRequest(finish_lfun);
5103 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5108 case LFUN_DOWN_SELECT:
5110 if (!(cur.selection() && cur.selIsMultiCell()))
5111 cell(cur.idx())->dispatch(cur, cmd);
5113 cur.dispatched(); // override the cell's decision
5114 if (sl == cur.top()) {
5115 // if our Text didn't do anything to the cursor
5116 // then we try to put the cursor into the cell below
5117 // setting also the right targetX.
5118 cur.selHandle(act == LFUN_DOWN_SELECT);
5119 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
5120 int const xtarget = cur.targetX();
5121 // WARNING: Once cur.idx() has been reset, the cursor is in
5122 // an inconsistent state until pos() has been set. Be careful
5123 // what you do with it!
5124 cur.idx() = tabular.cellBelow(cur.idx());
5126 TextMetrics const & tm =
5127 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5128 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5129 cur.setCurrentFont();
5132 if (sl == cur.top()) {
5133 // we trick it to go to forward after leaving the
5135 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5138 if (cur.selIsMultiCell()) {
5139 cur.pit() = cur.lastpit();
5140 cur.pos() = cur.lastpos();
5141 cur.setCurrentFont();
5142 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5145 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5148 case LFUN_UP_SELECT:
5150 if (!(cur.selection() && cur.selIsMultiCell()))
5151 cell(cur.idx())->dispatch(cur, cmd);
5152 cur.dispatched(); // override the cell's decision
5153 if (sl == cur.top()) {
5154 // if our Text didn't do anything to the cursor
5155 // then we try to put the cursor into the cell above
5156 // setting also the right targetX.
5157 cur.selHandle(act == LFUN_UP_SELECT);
5158 if (tabular.cellRow(cur.idx()) != 0) {
5159 int const xtarget = cur.targetX();
5160 // WARNING: Once cur.idx() has been reset, the cursor is in
5161 // an inconsistent state until pos() has been set. Be careful
5162 // what you do with it!
5163 cur.idx() = tabular.cellAbove(cur.idx());
5164 cur.pit() = cur.lastpit();
5165 Text const * text = cell(cur.idx())->getText(0);
5166 TextMetrics const & tm = cur.bv().textMetrics(text);
5167 ParagraphMetrics const & pm =
5168 tm.parMetrics(cur.lastpit());
5169 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5170 cur.setCurrentFont();
5173 if (sl == cur.top()) {
5174 cmd = FuncRequest(LFUN_UP);
5177 if (cur.selIsMultiCell()) {
5179 cur.pos() = cur.lastpos();
5180 cur.setCurrentFont();
5181 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5184 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5187 case LFUN_LAYOUT_TABULAR:
5188 cur.bv().showDialog("tabular");
5191 case LFUN_INSET_MODIFY:
5192 // we come from the dialog
5193 if (cmd.getArg(0) == "tabular")
5194 tabularFeatures(cur, cmd.getLongArg(1));
5199 case LFUN_TABULAR_FEATURE:
5200 tabularFeatures(cur, to_utf8(cmd.argument()));
5203 // insert file functions
5204 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5205 case LFUN_FILE_INSERT_PLAINTEXT:
5207 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5208 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5209 FileName(to_utf8(cmd.argument())));
5212 cur.recordUndoInset();
5213 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5214 // content has been replaced,
5215 // so cursor might be invalid
5216 cur.pos() = cur.lastpos();
5217 cur.pit() = cur.lastpit();
5218 bvcur.setCursor(cur);
5225 if (cur.selIsMultiCell()) {
5226 if (copySelection(cur)) {
5227 cur.recordUndoInset();
5231 cell(cur.idx())->dispatch(cur, cmd);
5234 case LFUN_SELF_INSERT:
5235 if (cur.selIsMultiCell()) {
5236 cur.recordUndoInset();
5238 BufferView * bv = &cur.bv();
5239 docstring::const_iterator cit = cmd.argument().begin();
5240 docstring::const_iterator const end = cmd.argument().end();
5241 for (; cit != end; ++cit)
5242 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5245 bv->bookmarkEditPosition();
5247 cell(cur.idx())->dispatch(cur, cmd);
5250 case LFUN_CHAR_DELETE_BACKWARD:
5251 case LFUN_CHAR_DELETE_FORWARD:
5252 if (cur.selIsMultiCell()) {
5253 cur.recordUndoInset();
5256 cell(cur.idx())->dispatch(cur, cmd);
5260 if (!cur.selection())
5262 if (cur.selIsMultiCell()) {
5266 cell(cur.idx())->dispatch(cur, cmd);
5269 case LFUN_CLIPBOARD_PASTE:
5270 case LFUN_PRIMARY_SELECTION_PASTE: {
5271 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5272 theClipboard().getAsText(Clipboard::PlainTextType) :
5273 theSelection().get();
5276 // pass to InsertPlaintextString, but
5277 // only if we have multi-cell content
5278 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5279 cur.recordUndoInset();
5280 if (insertPlaintextString(cur.bv(), clip, false)) {
5281 // content has been replaced,
5282 // so cursor might be invalid
5283 cur.pos() = cur.lastpos();
5284 cur.pit() = cur.lastpit();
5285 bvcur.setCursor(cur);
5289 // Let the cell handle normal text
5290 cell(cur.idx())->dispatch(cur, cmd);
5295 if (!tabularStackDirty()) {
5296 // Check if we have plain text or HTML with rows/columns.
5297 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5298 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5299 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5300 docstring const clip =
5301 theClipboard().getAsText(Clipboard::AnyTextType);
5302 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5303 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5304 doDispatch(cur, ncmd);
5308 else if (theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5309 // This might be tabular data from another LyX instance. Check!
5310 docstring const clip =
5311 theClipboard().getAsText(Clipboard::PlainTextType);
5312 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5313 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5314 doDispatch(cur, ncmd);
5318 if (!cur.selIsMultiCell())
5319 cell(cur.idx())->dispatch(cur, cmd);
5322 if (theClipboard().isInternal()) {
5323 cur.recordUndoInset();
5324 pasteClipboard(cur);
5328 case LFUN_CHANGE_ACCEPT:
5329 case LFUN_CHANGE_REJECT:
5330 case LFUN_FONT_EMPH:
5331 case LFUN_FONT_BOLD:
5332 case LFUN_FONT_BOLDSYMBOL:
5333 case LFUN_FONT_ROMAN:
5334 case LFUN_FONT_NOUN:
5335 case LFUN_FONT_ITAL:
5336 case LFUN_FONT_FRAK:
5337 case LFUN_FONT_TYPEWRITER:
5338 case LFUN_FONT_SANS:
5339 case LFUN_TEXTSTYLE_APPLY:
5340 case LFUN_TEXTSTYLE_UPDATE:
5341 case LFUN_FONT_SIZE:
5342 case LFUN_FONT_UNDERLINE:
5343 case LFUN_FONT_STRIKEOUT:
5344 case LFUN_FONT_CROSSOUT:
5345 case LFUN_FONT_UNDERUNDERLINE:
5346 case LFUN_FONT_UNDERWAVE:
5348 case LFUN_PARAGRAPH_PARAMS_APPLY:
5349 case LFUN_PARAGRAPH_PARAMS:
5350 case LFUN_WORD_CAPITALIZE:
5351 case LFUN_WORD_UPCASE:
5352 case LFUN_WORD_LOWCASE:
5353 case LFUN_CHARS_TRANSPOSE: {
5354 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5355 if (cur.selIsMultiCell()) {
5358 getSelection(cur, rs, re, cs, ce);
5359 Cursor tmpcur = cur;
5360 for (row_type r = rs; r <= re; ++r) {
5361 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5362 // whole row selected
5363 if (act == LFUN_CHANGE_ACCEPT) {
5364 if (tabular.row_info[r].change.inserted())
5365 tabular.row_info[r].change.setUnchanged();
5366 else if (tabular.row_info[r].change.deleted()) {
5367 tabular.deleteRow(r, true);
5372 if (tabular.row_info[r].change.deleted())
5373 tabular.row_info[r].change.setUnchanged();
5374 else if (tabular.row_info[r].change.inserted()) {
5375 tabular.deleteRow(r, true);
5381 for (col_type c = cs; c <= ce; ++c) {
5382 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5383 // whole col selected
5384 if (act == LFUN_CHANGE_ACCEPT) {
5385 if (tabular.column_info[c].change.inserted())
5386 tabular.column_info[c].change.setUnchanged();
5387 else if (tabular.column_info[c].change.deleted()) {
5388 tabular.deleteColumn(c, true);
5393 if (tabular.column_info[c].change.deleted())
5394 tabular.column_info[c].change.setUnchanged();
5395 else if (tabular.column_info[c].change.inserted()) {
5396 tabular.deleteColumn(c, true);
5402 // cursor follows cell:
5403 tmpcur.idx() = tabular.cellIndex(r, c);
5404 // select this cell only:
5407 tmpcur.resetAnchor();
5408 tmpcur.pit() = tmpcur.lastpit();
5409 tmpcur.pos() = tmpcur.top().lastpos();
5410 tmpcur.setCursor(tmpcur);
5411 tmpcur.setSelection();
5412 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5416 tabular.updateIndexes();
5417 // cursor might be invalid
5419 // change bar might need to be redrawn
5420 cur.screenUpdateFlags(Update::Force);
5421 cur.forceBufferUpdate();
5425 cell(cur.idx())->dispatch(cur, cmd);
5430 case LFUN_CHANGE_NEXT:
5431 case LFUN_CHANGE_PREVIOUS: {
5432 // BufferView::dispatch has already moved the cursor, we just
5433 // need to select here if we have a changed row or column
5434 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5436 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5440 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5441 cur.pit() = cur.lastpit();
5442 cur.pos() = cur.lastpos();
5443 cur.selection(true);
5447 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5449 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5453 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5454 cur.pit() = cur.lastpit();
5455 cur.pos() = cur.lastpos();
5456 cur.selection(true);
5463 case LFUN_INSET_SETTINGS:
5464 // relay this lfun to Inset, not to the cell.
5465 Inset::doDispatch(cur, cmd);
5469 // we try to handle this event in the insets dispatch function.
5470 cell(cur.idx())->dispatch(cur, cmd);
5476 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5477 string const & argument, FuncStatus & status) const
5480 int action = Tabular::LAST_ACTION;
5482 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5483 if (tabularFeature[i].feature == s) {
5484 action = tabularFeature[i].action;
5488 if (action == Tabular::LAST_ACTION) {
5490 status.setUnknown(true);
5494 row_type sel_row_start = 0;
5495 row_type sel_row_end = 0;
5496 col_type sel_col_start = 0;
5497 col_type sel_col_end = 0;
5498 Tabular::ltType dummyltt;
5501 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5504 case Tabular::SET_PWIDTH:
5505 case Tabular::SET_MPWIDTH:
5506 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5507 case Tabular::SET_SPECIAL_COLUMN:
5508 case Tabular::SET_SPECIAL_MULTICOLUMN:
5509 case Tabular::APPEND_ROW:
5510 case Tabular::APPEND_COLUMN:
5511 case Tabular::DELETE_ROW:
5512 case Tabular::DELETE_COLUMN:
5513 case Tabular::COPY_ROW:
5514 case Tabular::COPY_COLUMN:
5515 case Tabular::SET_TOP_SPACE:
5516 case Tabular::SET_BOTTOM_SPACE:
5517 case Tabular::SET_INTERLINE_SPACE:
5521 case Tabular::SET_TABULAR_WIDTH:
5522 status.setEnabled(!tabular.rotate
5523 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5526 case Tabular::MOVE_COLUMN_RIGHT:
5527 case Tabular::MOVE_COLUMN_LEFT:
5528 case Tabular::MOVE_ROW_DOWN:
5529 case Tabular::MOVE_ROW_UP: {
5530 if (cur.selection()) {
5531 status.message(_("Selections not supported."));
5532 status.setEnabled(false);
5536 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
5537 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
5538 (action == Tabular::MOVE_COLUMN_LEFT &&
5539 tabular.cellColumn(cur.idx()) == 0) ||
5540 (action == Tabular::MOVE_ROW_DOWN &&
5541 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
5542 (action == Tabular::MOVE_ROW_UP &&
5543 tabular.cellRow(cur.idx()) == 0)) {
5544 status.setEnabled(false);
5548 if (action == Tabular::MOVE_COLUMN_RIGHT ||
5549 action == Tabular::MOVE_COLUMN_LEFT) {
5550 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
5551 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
5552 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
5553 status.message(_("Multi-column in current or"
5554 " destination column."));
5555 status.setEnabled(false);
5560 if (action == Tabular::MOVE_ROW_DOWN ||
5561 action == Tabular::MOVE_ROW_UP) {
5562 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
5563 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
5564 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
5565 status.message(_("Multi-row in current or"
5566 " destination row."));
5567 status.setEnabled(false);
5572 status.setEnabled(true);
5576 case Tabular::SET_DECIMAL_POINT:
5578 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5581 case Tabular::SET_MULTICOLUMN:
5582 case Tabular::UNSET_MULTICOLUMN:
5583 case Tabular::MULTICOLUMN:
5584 // If a row is set as longtable caption, it must not be allowed
5585 // to unset that this row is a multicolumn.
5586 status.setEnabled(sel_row_start == sel_row_end
5587 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5588 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5591 case Tabular::SET_MULTIROW:
5592 case Tabular::UNSET_MULTIROW:
5593 case Tabular::MULTIROW:
5594 // If a row is set as longtable caption, it must not be allowed
5595 // to unset that this row is a multirow.
5596 status.setEnabled(sel_col_start == sel_col_end
5597 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5598 status.setOnOff(tabular.isMultiRow(cur.idx()));
5601 case Tabular::SET_ALL_LINES:
5602 case Tabular::UNSET_ALL_LINES:
5603 case Tabular::SET_INNER_LINES:
5604 case Tabular::SET_BORDER_LINES:
5605 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5608 case Tabular::RESET_FORMAL_DEFAULT:
5609 status.setEnabled(tabular.use_booktabs);
5612 case Tabular::SET_LINE_TOP:
5613 case Tabular::SET_LINE_BOTTOM:
5614 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5617 case Tabular::SET_LINE_LEFT:
5618 case Tabular::SET_LINE_RIGHT:
5619 status.setEnabled(!tabular.use_booktabs
5620 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5623 case Tabular::SET_LTRIM_TOP:
5624 case Tabular::SET_RTRIM_TOP:
5625 status.setEnabled(tabular.use_booktabs
5626 && tabular.cellRow(cur.idx()) != 0
5627 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5630 case Tabular::SET_LTRIM_BOTTOM:
5631 case Tabular::SET_RTRIM_BOTTOM:
5632 status.setEnabled(tabular.use_booktabs
5633 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5634 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5637 case Tabular::TOGGLE_LINE_TOP:
5638 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5639 status.setOnOff(tabular.topLine(cur.idx()));
5642 case Tabular::TOGGLE_LINE_BOTTOM:
5643 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5644 status.setOnOff(tabular.bottomLine(cur.idx()));
5647 case Tabular::TOGGLE_LINE_LEFT:
5648 status.setEnabled(!tabular.use_booktabs
5649 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5650 status.setOnOff(tabular.leftLine(cur.idx()));
5653 case Tabular::TOGGLE_LINE_RIGHT:
5654 status.setEnabled(!tabular.use_booktabs
5655 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5656 status.setOnOff(tabular.rightLine(cur.idx()));
5659 case Tabular::TOGGLE_LTRIM_TOP:
5660 status.setEnabled(tabular.use_booktabs
5661 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5662 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5665 case Tabular::TOGGLE_RTRIM_TOP:
5666 status.setEnabled(tabular.use_booktabs
5667 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5668 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5671 case Tabular::TOGGLE_LTRIM_BOTTOM:
5672 status.setEnabled(tabular.use_booktabs
5673 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5674 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5677 case Tabular::TOGGLE_RTRIM_BOTTOM:
5678 status.setEnabled(tabular.use_booktabs
5679 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5680 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5683 // multirow cells only inherit the alignment of the column if the column has
5684 // no width, otherwise they are left-aligned
5685 // therefore allow always left but right and center only if there is no width
5686 case Tabular::M_ALIGN_LEFT:
5689 case Tabular::ALIGN_LEFT:
5690 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5693 case Tabular::M_ALIGN_RIGHT:
5696 case Tabular::ALIGN_RIGHT:
5697 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5698 && !tabular.getPWidth(cur.idx()).zero()));
5699 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5702 case Tabular::M_ALIGN_CENTER:
5705 case Tabular::ALIGN_CENTER:
5706 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5707 && !tabular.getPWidth(cur.idx()).zero()));
5708 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5711 case Tabular::ALIGN_BLOCK:
5712 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5713 && !tabular.isMultiRow(cur.idx()));
5714 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5717 case Tabular::ALIGN_DECIMAL:
5718 status.setEnabled(!tabular.isMultiRow(cur.idx())
5719 && !tabular.isMultiColumn(cur.idx()));
5720 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5723 case Tabular::M_VALIGN_TOP:
5726 case Tabular::VALIGN_TOP:
5727 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5728 && !tabular.isMultiRow(cur.idx()));
5730 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5733 case Tabular::M_VALIGN_BOTTOM:
5736 case Tabular::VALIGN_BOTTOM:
5737 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5738 && !tabular.isMultiRow(cur.idx()));
5740 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5743 case Tabular::M_VALIGN_MIDDLE:
5746 case Tabular::VALIGN_MIDDLE:
5747 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5748 && !tabular.isMultiRow(cur.idx()));
5750 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5753 case Tabular::SET_LONGTABULAR:
5754 case Tabular::TOGGLE_LONGTABULAR:
5755 // setting as longtable is not allowed when table is inside a float
5756 if (cur.innerInsetOfType(FLOAT_CODE) != nullptr
5757 || cur.innerInsetOfType(WRAP_CODE) != nullptr)
5758 status.setEnabled(false);
5760 status.setEnabled(true);
5761 status.setOnOff(tabular.is_long_tabular);
5764 case Tabular::UNSET_LONGTABULAR:
5765 status.setOnOff(!tabular.is_long_tabular);
5768 case Tabular::TOGGLE_ROTATE_TABULAR:
5769 case Tabular::SET_ROTATE_TABULAR:
5770 status.setOnOff(tabular.rotate != 0);
5773 case Tabular::TABULAR_VALIGN_TOP:
5774 status.setEnabled(tabular.tabular_width.zero());
5775 status.setOnOff(tabular.tabular_valignment
5776 == Tabular::LYX_VALIGN_TOP);
5778 case Tabular::TABULAR_VALIGN_MIDDLE:
5779 status.setEnabled(tabular.tabular_width.zero());
5780 status.setOnOff(tabular.tabular_valignment
5781 == Tabular::LYX_VALIGN_MIDDLE);
5783 case Tabular::TABULAR_VALIGN_BOTTOM:
5784 status.setEnabled(tabular.tabular_width.zero());
5785 status.setOnOff(tabular.tabular_valignment
5786 == Tabular::LYX_VALIGN_BOTTOM);
5789 case Tabular::LONGTABULAR_ALIGN_LEFT:
5790 status.setOnOff(tabular.longtabular_alignment
5791 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5793 case Tabular::LONGTABULAR_ALIGN_CENTER:
5794 status.setOnOff(tabular.longtabular_alignment
5795 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5797 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5798 status.setOnOff(tabular.longtabular_alignment
5799 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5802 case Tabular::UNSET_ROTATE_TABULAR:
5803 status.setOnOff(tabular.rotate == 0);
5806 case Tabular::TOGGLE_ROTATE_CELL:
5807 case Tabular::SET_ROTATE_CELL:
5808 status.setOnOff(!oneCellHasRotationState(false,
5809 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5812 case Tabular::UNSET_ROTATE_CELL:
5813 status.setOnOff(!oneCellHasRotationState(true,
5814 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5817 case Tabular::SET_USEBOX:
5818 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5821 // every row can only be one thing:
5822 // either a footer or header
5823 case Tabular::SET_LTFIRSTHEAD:
5824 status.setEnabled(sel_row_start == sel_row_end);
5825 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5828 case Tabular::UNSET_LTFIRSTHEAD:
5829 status.setEnabled(sel_row_start == sel_row_end);
5830 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5833 case Tabular::SET_LTHEAD:
5834 status.setEnabled(sel_row_start == sel_row_end);
5835 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5838 case Tabular::UNSET_LTHEAD:
5839 status.setEnabled(sel_row_start == sel_row_end);
5840 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5843 case Tabular::SET_LTFOOT:
5844 status.setEnabled(sel_row_start == sel_row_end);
5845 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5848 case Tabular::UNSET_LTFOOT:
5849 status.setEnabled(sel_row_start == sel_row_end);
5850 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5853 case Tabular::SET_LTLASTFOOT:
5854 status.setEnabled(sel_row_start == sel_row_end);
5855 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5858 case Tabular::UNSET_LTLASTFOOT:
5859 status.setEnabled(sel_row_start == sel_row_end);
5860 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5863 case Tabular::SET_LTNEWPAGE:
5864 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5866 case Tabular::UNSET_LTNEWPAGE:
5867 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5870 // only one row in head/firsthead/foot/lasthead can be the caption
5871 // and a multirow cannot be set as caption
5872 case Tabular::SET_LTCAPTION:
5873 status.setEnabled(sel_row_start == sel_row_end
5874 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5875 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5876 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5877 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5878 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5879 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5880 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5881 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5882 && !tabular.isMultiRow(sel_row_start));
5883 status.setOnOff(tabular.ltCaption(sel_row_start));
5886 case Tabular::UNSET_LTCAPTION:
5887 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5890 case Tabular::TOGGLE_LTCAPTION:
5891 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5892 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5893 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5894 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5895 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5896 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5897 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5898 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5899 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5900 status.setOnOff(tabular.ltCaption(sel_row_start));
5903 case Tabular::TOGGLE_BOOKTABS:
5904 case Tabular::SET_BOOKTABS:
5905 status.setOnOff(tabular.use_booktabs);
5908 case Tabular::UNSET_BOOKTABS:
5909 status.setOnOff(!tabular.use_booktabs);
5914 status.setEnabled(false);
5921 // function sets an object as defined in FuncStatus.h:
5922 // states OK, Unknown, Disabled, On, Off.
5923 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5924 FuncStatus & status) const
5926 switch (cmd.action()) {
5927 case LFUN_INSET_MODIFY:
5928 if (cmd.getArg(0) != "tabular")
5930 if (cmd.getArg(1) == "for-dialog") {
5931 // The dialog is asking the status of a command
5932 if (&cur.inset() != this)
5934 string action = cmd.getArg(2);
5935 string arg = cmd.getLongArg(3);
5936 return getFeatureStatus(cur, action, arg, status);
5938 // We always enable the lfun if it is coming from the dialog
5939 // because the dialog makes sure all the settings are valid,
5940 // even though the first argument might not be valid now.
5941 status.setEnabled(true);
5945 case LFUN_TABULAR_FEATURE: {
5946 if (&cur.inset() != this)
5948 string action = cmd.getArg(0);
5949 string arg = cmd.getLongArg(1);
5950 return getFeatureStatus(cur, action, arg, status);
5953 case LFUN_CAPTION_INSERT: {
5954 // caption is only allowed in caption cell of longtable
5955 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5956 status.setEnabled(false);
5959 // only standard caption is allowed
5960 string arg = cmd.getArg(0);
5961 if (!arg.empty() && arg != "Standard") {
5962 status.setEnabled(false);
5965 // check if there is already a caption
5966 bool have_caption = false;
5967 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5968 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5969 ParagraphList::const_iterator pend = itc.paragraphs().end();
5970 for (; pit != pend; ++pit) {
5971 InsetList::const_iterator it = pit->insetList().begin();
5972 InsetList::const_iterator end = pit->insetList().end();
5973 for (; it != end; ++it) {
5974 if (it->inset->lyxCode() == CAPTION_CODE) {
5975 have_caption = true;
5980 status.setEnabled(!have_caption);
5984 // These are only enabled inside tabular
5985 case LFUN_CELL_BACKWARD:
5986 case LFUN_CELL_FORWARD:
5987 status.setEnabled(true);
5990 // disable these with multiple cells selected
5991 case LFUN_INSET_INSERT:
5992 case LFUN_TABULAR_INSERT:
5993 case LFUN_TABULAR_STYLE_INSERT:
5994 case LFUN_FLEX_INSERT:
5995 case LFUN_FLOAT_INSERT:
5996 case LFUN_FLOAT_WIDE_INSERT:
5997 case LFUN_FOOTNOTE_INSERT:
5998 case LFUN_MARGINALNOTE_INSERT:
5999 case LFUN_MATH_INSERT:
6000 case LFUN_MATH_MODE:
6001 case LFUN_MATH_MUTATE:
6002 case LFUN_MATH_DISPLAY:
6003 case LFUN_NOTE_INSERT:
6004 case LFUN_ARGUMENT_INSERT:
6005 case LFUN_BOX_INSERT:
6006 case LFUN_BRANCH_INSERT:
6007 case LFUN_PHANTOM_INSERT:
6008 case LFUN_WRAP_INSERT:
6009 case LFUN_PREVIEW_INSERT:
6010 case LFUN_ERT_INSERT: {
6011 if (cur.selIsMultiCell()) {
6012 status.setEnabled(false);
6015 return cell(cur.idx())->getStatus(cur, cmd, status);
6018 case LFUN_CHANGE_ACCEPT:
6019 case LFUN_CHANGE_REJECT: {
6020 if (cur.selIsMultiCell()) {
6023 getSelection(cur, rs, re, cs, ce);
6024 for (row_type r = rs; r <= re; ++r) {
6025 if (tabular.row_info[r].change.changed()) {
6026 status.setEnabled(true);
6029 for (col_type c = cs; c <= ce; ++c) {
6030 if (tabular.column_info[c].change.changed()) {
6031 status.setEnabled(true);
6037 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
6038 status.setEnabled(true);
6041 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
6042 status.setEnabled(true);
6046 return cell(cur.idx())->getStatus(cur, cmd, status);
6049 // disable in non-fixed-width cells
6050 case LFUN_PARAGRAPH_BREAK:
6051 // multirow does not allow paragraph breaks
6052 if (tabular.isMultiRow(cur.idx())) {
6053 status.setEnabled(false);
6057 case LFUN_NEWLINE_INSERT:
6058 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
6059 && tabular.getPWidth(cur.idx()).zero()) {
6060 status.setEnabled(false);
6063 return cell(cur.idx())->getStatus(cur, cmd, status);
6065 case LFUN_NEWPAGE_INSERT:
6066 status.setEnabled(false);
6070 if (tabularStackDirty() && theClipboard().isInternal()) {
6071 if (cur.selIsMultiCell()) {
6074 getSelection(cur, rs, re, cs, ce);
6075 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
6076 && paste_tabular->nrows() == re - rs + 1)
6077 status.setEnabled(true);
6079 status.setEnabled(false);
6080 status.message(_("Selection size should match clipboard content."));
6083 status.setEnabled(true);
6086 return cell(cur.idx())->getStatus(cur, cmd, status);
6088 case LFUN_INSET_SETTINGS:
6089 // relay this lfun to Inset, not to the cell.
6090 return Inset::getStatus(cur, cmd, status);
6093 // we try to handle this event in the insets dispatch function.
6094 return cell(cur.idx())->getStatus(cur, cmd, status);
6100 Inset::RowFlags InsetTabular::rowFlags() const
6102 if (tabular.is_long_tabular) {
6103 switch (tabular.longtabular_alignment) {
6104 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
6105 return Display | AlignLeft;
6106 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
6108 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
6109 return Display | AlignRight;
6118 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
6120 tabular.latex(os, runparams);
6124 int InsetTabular::plaintext(odocstringstream & os,
6125 OutputParams const & runparams, size_t max_length) const
6127 os << '\n'; // output table on a new line
6128 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
6129 tabular.plaintext(os, runparams, dp, false, 0, max_length);
6130 return PLAINTEXT_NEWLINE;
6134 void InsetTabular::docbook(XMLStream & xs, OutputParams const & runparams) const
6136 tabular.docbook(xs, runparams);
6140 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6142 return tabular.xhtml(xs, rp);
6146 void InsetTabular::validate(LaTeXFeatures & features) const
6148 tabular.validate(features);
6149 features.useInsetLayout(getLayout());
6153 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6155 return tabular.cellInset(idx);
6159 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6161 return tabular.cellInset(idx);
6165 void InsetTabular::cursorPos(BufferView const & bv,
6166 CursorSlice const & sl, bool boundary, int & x, int & y) const
6168 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6170 // y offset correction
6171 y += cellYPos(sl.idx());
6172 y += tabular.textVOffset(sl.idx());
6173 y += tabular.offsetVAlignment();
6175 // x offset correction
6176 x += cellXPos(sl.idx());
6177 x += tabular.textHOffset(sl.idx());
6178 x += ADD_TO_TABULAR_WIDTH;
6182 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6186 Inset const & inset = *tabular.cellInset(cell);
6187 Point o = bv.coordCache().getInsets().xy(&inset);
6188 int const xbeg = o.x_ - tabular.textHOffset(cell);
6189 int const xend = xbeg + tabular.cellWidth(cell);
6190 row_type const row = tabular.cellRow(cell);
6191 int const ybeg = o.y_ - tabular.rowAscent(row)
6192 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6193 int const yend = ybeg + tabular.cellHeight(cell);
6205 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6206 // << " ybeg=" << ybeg << " yend=" << yend
6207 // << " xx=" << xx << " yy=" << yy
6208 // << " dist=" << xx + yy << endl;
6213 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6215 //lyxerr << "InsetTabular::editXY: " << this << endl;
6217 cur.idx() = getNearestCell(cur.bv(), x, y);
6218 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6222 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6224 cur.idx() = getNearestCell(cur.bv(), x, y);
6225 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6229 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6231 idx_type idx_min = 0;
6232 int dist_min = numeric_limits<int>::max();
6233 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6234 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6235 int const d = dist(bv, i, x, y);
6246 int InsetTabular::cellYPos(idx_type const cell) const
6248 row_type row = tabular.cellRow(cell);
6250 for (row_type r = 0; r < row; ++r)
6251 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6252 + tabular.interRowSpace(r + 1);
6257 int InsetTabular::cellXPos(idx_type const cell) const
6259 col_type col = tabular.cellColumn(cell);
6261 for (col_type c = 0; c < col; ++c)
6262 lx += tabular.column_info[c].width;
6267 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6269 row_type const row = tabular.cellRow(cur.idx());
6270 col_type const col = tabular.cellColumn(cur.idx());
6272 if (isRightToLeft(cur)) {
6273 if (tabular.cellColumn(cur.idx()) == 0) {
6274 if (row == tabular.nrows() - 1)
6276 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6281 cur.idx() = tabular.getLastCellInRow(row - 1);
6283 cur.idx() = tabular.cellIndex(row, col - 1);
6286 if (tabular.isLastCell(cur.idx()))
6288 if (cur.idx() == tabular.getLastCellInRow(row))
6289 cur.idx() = tabular.cellIndex(row + 1, 0);
6291 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6292 cur.idx() = tabular.cellIndex(row, colnextcell);
6296 cur.boundary(false);
6298 if (cur.selIsMultiCell()) {
6299 cur.pit() = cur.lastpit();
6300 cur.pos() = cur.lastpos();
6307 // in visual mode, place cursor at extreme left or right
6309 switch(entry_from) {
6311 case ENTRY_DIRECTION_RIGHT:
6312 cur.posVisToRowExtremity(false /* !left */);
6314 case ENTRY_DIRECTION_LEFT:
6315 cur.posVisToRowExtremity(true /* left */);
6317 case ENTRY_DIRECTION_IGNORE:
6318 // nothing to do in this case
6322 cur.setCurrentFont();
6326 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6328 row_type const row = tabular.cellRow(cur.idx());
6329 col_type const col = tabular.cellColumn(cur.idx());
6331 if (isRightToLeft(cur)) {
6332 if (cur.idx() == tabular.getLastCellInRow(row)) {
6335 cur.idx() = tabular.getFirstCellInRow(row);
6336 cur.idx() = tabular.cellAbove(cur.idx());
6338 if (tabular.isLastCell(cur.idx()))
6340 if (cur.idx() == tabular.getLastCellInRow(row))
6341 cur.idx() = tabular.cellIndex(row + 1, 0);
6343 cur.idx() = tabular.cellIndex(row, col + 1);
6346 if (cur.idx() == 0) // first cell
6349 cur.idx() = tabular.getLastCellInRow(row - 1);
6351 cur.idx() = tabular.cellIndex(row, col - 1);
6354 if (cur.selIsMultiCell()) {
6355 cur.pit() = cur.lastpit();
6356 cur.pos() = cur.lastpos();
6360 cur.pit() = cur.lastpit();
6361 cur.pos() = cur.lastpos();
6363 // in visual mode, place cursor at extreme left or right
6365 switch(entry_from) {
6367 case ENTRY_DIRECTION_RIGHT:
6368 cur.posVisToRowExtremity(false /* !left */);
6370 case ENTRY_DIRECTION_LEFT:
6371 cur.posVisToRowExtremity(true /* left */);
6373 case ENTRY_DIRECTION_IGNORE:
6374 // nothing to do in this case
6378 cur.setCurrentFont();
6382 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6384 cur.recordUndoInset(this);
6386 istringstream is(argument);
6387 // limit the size of strings we read to avoid memory problems
6391 size_t safe_guard = 0;
6396 if (safe_guard > 1000) {
6397 LYXERR0("parameter max count reached!");
6401 Tabular::Feature action = Tabular::LAST_ACTION;
6404 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6405 if (s != tabularFeature[i].feature)
6407 action = tabularFeature[i].action;
6410 if (action == Tabular::LAST_ACTION) {
6411 LYXERR0("Feature not found " << s);
6415 if (tabularFeature[i].need_value)
6417 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6418 tabularFeatures(cur, action, val);
6423 static void checkLongtableSpecial(Tabular::ltType & ltt,
6424 string const & special, bool & flag)
6426 if (special == "dl_above") {
6429 } else if (special == "dl_below") {
6430 ltt.bottomDL = flag;
6432 } else if (special == "empty") {
6442 bool InsetTabular::oneCellHasRotationState(bool rotated,
6443 row_type row_start, row_type row_end,
6444 col_type col_start, col_type col_end) const
6446 for (row_type r = row_start; r <= row_end; ++r)
6447 for (col_type c = col_start; c <= col_end; ++c)
6449 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6452 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6459 void InsetTabular::tabularFeatures(Cursor & cur,
6460 Tabular::Feature feature, string const & value)
6462 col_type sel_col_start;
6463 col_type sel_col_end;
6464 row_type sel_row_start;
6465 row_type sel_row_end;
6466 bool setLines = false;
6467 bool setLinesInnerOnly = false;
6468 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6469 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6473 case Tabular::M_ALIGN_LEFT:
6474 case Tabular::ALIGN_LEFT:
6475 setAlign = LYX_ALIGN_LEFT;
6478 case Tabular::M_ALIGN_RIGHT:
6479 case Tabular::ALIGN_RIGHT:
6480 setAlign = LYX_ALIGN_RIGHT;
6483 case Tabular::M_ALIGN_CENTER:
6484 case Tabular::ALIGN_CENTER:
6485 setAlign = LYX_ALIGN_CENTER;
6488 case Tabular::ALIGN_BLOCK:
6489 setAlign = LYX_ALIGN_BLOCK;
6492 case Tabular::ALIGN_DECIMAL:
6493 setAlign = LYX_ALIGN_DECIMAL;
6496 case Tabular::M_VALIGN_TOP:
6497 case Tabular::VALIGN_TOP:
6498 setVAlign = Tabular::LYX_VALIGN_TOP;
6501 case Tabular::M_VALIGN_BOTTOM:
6502 case Tabular::VALIGN_BOTTOM:
6503 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6506 case Tabular::M_VALIGN_MIDDLE:
6507 case Tabular::VALIGN_MIDDLE:
6508 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6515 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6516 row_type const row = tabular.cellRow(cur.idx());
6517 col_type const column = tabular.cellColumn(cur.idx());
6519 Tabular::ltType ltt;
6523 case Tabular::SET_TABULAR_WIDTH:
6524 tabular.setTabularWidth(Length(value));
6527 case Tabular::SET_PWIDTH: {
6528 Length const len(value);
6529 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6530 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6532 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6533 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6538 case Tabular::SET_MPWIDTH:
6539 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6542 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6543 bool const varwidth = value == "on";
6544 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6545 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6549 case Tabular::SET_MROFFSET:
6550 tabular.setMROffset(cur, cur.idx(), Length(value));
6553 case Tabular::SET_SPECIAL_COLUMN:
6554 case Tabular::SET_SPECIAL_MULTICOLUMN:
6555 if (value == "none")
6556 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6558 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6561 case Tabular::APPEND_ROW:
6562 // append the row into the tabular
6563 tabular.appendRow(row);
6566 case Tabular::APPEND_COLUMN:
6567 // append the column into the tabular
6568 tabular.appendColumn(column);
6569 cur.idx() = tabular.cellIndex(row, column);
6572 case Tabular::DELETE_ROW:
6573 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6574 for (col_type c = 0; c < tabular.ncols(); c++) {
6575 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6576 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6577 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6578 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6582 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6583 tabular.deleteRow(sel_row_start);
6584 if (sel_row_start >= tabular.nrows())
6586 cur.idx() = tabular.cellIndex(sel_row_start, column);
6589 cur.selection(false);
6592 case Tabular::DELETE_COLUMN:
6593 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6594 for (row_type r = 0; r < tabular.nrows(); r++)
6595 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6596 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6599 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6600 for (row_type r = 0; r < tabular.nrows(); r++)
6601 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6602 tabular.leftLine(tabular.cellIndex(r, 0)));
6605 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6606 tabular.deleteColumn(sel_col_start);
6607 if (sel_col_start >= tabular.ncols())
6609 cur.idx() = tabular.cellIndex(row, sel_col_start);
6612 cur.selection(false);
6615 case Tabular::COPY_ROW:
6616 tabular.copyRow(row);
6619 case Tabular::COPY_COLUMN:
6620 tabular.copyColumn(column);
6621 cur.idx() = tabular.cellIndex(row, column);
6624 case Tabular::MOVE_COLUMN_RIGHT:
6625 tabular.moveColumn(column, Tabular::RIGHT);
6626 cur.idx() = tabular.cellIndex(row, column + 1);
6629 case Tabular::MOVE_COLUMN_LEFT:
6630 tabular.moveColumn(column, Tabular::LEFT);
6631 cur.idx() = tabular.cellIndex(row, column - 1);
6634 case Tabular::MOVE_ROW_DOWN:
6635 tabular.moveRow(row, Tabular::DOWN);
6636 cur.idx() = tabular.cellIndex(row + 1, column);
6639 case Tabular::MOVE_ROW_UP:
6640 tabular.moveRow(row, Tabular::UP);
6641 cur.idx() = tabular.cellIndex(row - 1, column);
6644 case Tabular::SET_LINE_TOP:
6645 case Tabular::TOGGLE_LINE_TOP: {
6646 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6647 ? (value == "true") : !tabular.topLine(cur.idx());
6648 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6649 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6650 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6654 case Tabular::SET_LINE_BOTTOM:
6655 case Tabular::TOGGLE_LINE_BOTTOM: {
6656 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6657 ? (value == "true") : !tabular.bottomLine(cur.idx());
6658 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6659 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6660 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6664 case Tabular::SET_LTRIM_TOP:
6665 case Tabular::TOGGLE_LTRIM_TOP: {
6666 bool l = (feature == Tabular::SET_LTRIM_TOP)
6667 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6668 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6669 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6670 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6674 case Tabular::SET_RTRIM_TOP:
6675 case Tabular::TOGGLE_RTRIM_TOP: {
6676 bool l = (feature == Tabular::SET_RTRIM_TOP)
6677 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6678 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6679 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6680 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6684 case Tabular::SET_LTRIM_BOTTOM:
6685 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6686 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6687 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6688 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6689 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6690 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6694 case Tabular::SET_RTRIM_BOTTOM:
6695 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6696 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6697 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6698 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6699 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6700 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6704 case Tabular::SET_LINE_LEFT:
6705 case Tabular::TOGGLE_LINE_LEFT: {
6706 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6707 ? (value == "true") : !tabular.leftLine(cur.idx());
6708 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6709 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6710 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6714 case Tabular::SET_LINE_RIGHT:
6715 case Tabular::TOGGLE_LINE_RIGHT: {
6716 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6717 ? (value == "true") : !tabular.rightLine(cur.idx());
6718 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6719 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6720 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6724 case Tabular::M_ALIGN_LEFT:
6725 case Tabular::M_ALIGN_RIGHT:
6726 case Tabular::M_ALIGN_CENTER:
6727 case Tabular::ALIGN_LEFT:
6728 case Tabular::ALIGN_RIGHT:
6729 case Tabular::ALIGN_CENTER:
6730 case Tabular::ALIGN_BLOCK:
6731 case Tabular::ALIGN_DECIMAL:
6732 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6733 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6734 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6735 !tabular.getPWidth(c).zero());
6738 case Tabular::M_VALIGN_TOP:
6739 case Tabular::M_VALIGN_BOTTOM:
6740 case Tabular::M_VALIGN_MIDDLE:
6743 case Tabular::VALIGN_TOP:
6744 case Tabular::VALIGN_BOTTOM:
6745 case Tabular::VALIGN_MIDDLE:
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.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6751 case Tabular::SET_MULTICOLUMN: {
6752 if (!cur.selection()) {
6753 // just multicol for one single cell
6754 // check whether we are completely in a multicol
6755 if (!tabular.isMultiColumn(cur.idx()))
6756 tabular.setMultiColumn(cur, cur.idx(), 1,
6757 tabular.rightLine(cur.idx()));
6760 // we have a selection so this means we just add all these
6761 // cells to form a multicolumn cell
6762 idx_type const s_start = cur.selBegin().idx();
6763 row_type const col_start = tabular.cellColumn(s_start);
6764 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6765 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6766 tabular.rightLine(cur.selEnd().idx()));
6769 cur.selection(false);
6773 case Tabular::UNSET_MULTICOLUMN: {
6774 if (!cur.selection()) {
6775 if (tabular.isMultiColumn(cur.idx()))
6776 tabular.unsetMultiColumn(cur.idx());
6781 case Tabular::MULTICOLUMN: {
6782 if (!cur.selection()) {
6783 if (tabular.isMultiColumn(cur.idx()))
6784 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6786 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6790 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6791 row_type const r = sel_row_start;
6792 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6793 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6796 // If the selection contains at least one singlecol cell
6797 // or multiple multicol cells,
6798 // we assume the user will merge is to a single multicol
6800 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6802 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6806 case Tabular::SET_MULTIROW: {
6807 if (!cur.selection()) {
6808 // just multirow for one single cell
6809 // check whether we are completely in a multirow
6810 if (!tabular.isMultiRow(cur.idx()))
6811 tabular.setMultiRow(cur, cur.idx(), 1,
6812 tabular.bottomLine(cur.idx()),
6813 tabular.getAlignment(cur.idx()));
6816 // we have a selection so this means we just add all this
6817 // cells to form a multirow cell
6818 idx_type const s_start = cur.selBegin().idx();
6819 row_type const row_start = tabular.cellRow(s_start);
6820 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6821 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6822 tabular.bottomLine(cur.selEnd().idx()),
6823 tabular.getAlignment(cur.selEnd().idx()));
6826 cur.selection(false);
6830 case Tabular::UNSET_MULTIROW: {
6831 if (!cur.selection()) {
6832 if (tabular.isMultiRow(cur.idx()))
6833 tabular.unsetMultiRow(cur.idx());
6838 case Tabular::MULTIROW: {
6839 if (!cur.selection()) {
6840 if (tabular.isMultiRow(cur.idx()))
6841 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6843 tabularFeatures(cur, Tabular::SET_MULTIROW);
6847 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6848 col_type const c = sel_col_start;
6849 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6850 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6853 // If the selection contains at least one singlerow cell
6854 // or multiple multirow cells,
6855 // we assume the user will merge is to a single multirow
6857 tabularFeatures(cur, Tabular::SET_MULTIROW);
6859 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6863 case Tabular::SET_INNER_LINES:
6864 setLinesInnerOnly = true;
6866 case Tabular::SET_ALL_LINES:
6869 case Tabular::UNSET_ALL_LINES:
6870 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6871 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6872 idx_type const cell = tabular.cellIndex(r, c);
6873 if (!setLinesInnerOnly || r != sel_row_start)
6874 tabular.setTopLine(cell, setLines);
6875 if ((!setLinesInnerOnly || r != sel_row_end)
6876 && (!setLines || r == sel_row_end))
6877 tabular.setBottomLine(cell, setLines);
6878 if ((!setLinesInnerOnly || c != sel_col_end)
6879 && (!setLines || c == sel_col_end))
6880 tabular.setRightLine(cell, setLines);
6881 if ((!setLinesInnerOnly || c != sel_col_start))
6882 tabular.setLeftLine(cell, setLines);
6886 case Tabular::RESET_FORMAL_DEFAULT:
6887 for (row_type r = 0; r < tabular.nrows(); ++r) {
6888 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6889 for (col_type c = 0; c < tabular.ncols(); ++c) {
6890 idx_type const cell = tabular.cellIndex(r, c);
6891 tabular.setTopLine(cell, head_or_foot);
6892 tabular.setBottomLine(cell, head_or_foot);
6897 case Tabular::SET_BORDER_LINES:
6898 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6899 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6900 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6902 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6903 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6904 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6908 case Tabular::TOGGLE_LONGTABULAR:
6909 if (tabular.is_long_tabular)
6910 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6912 tabular.is_long_tabular = true;
6915 case Tabular::SET_LONGTABULAR:
6916 tabular.is_long_tabular = true;
6919 case Tabular::UNSET_LONGTABULAR:
6920 for (row_type r = 0; r < tabular.nrows(); ++r) {
6921 if (tabular.ltCaption(r)) {
6922 cur.idx() = tabular.cellIndex(r, 0);
6925 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6928 tabular.is_long_tabular = false;
6931 case Tabular::SET_ROTATE_TABULAR:
6932 tabular.rotate = convert<int>(value);
6935 case Tabular::UNSET_ROTATE_TABULAR:
6939 case Tabular::TOGGLE_ROTATE_TABULAR:
6940 // when pressing the rotate button we default to 90° rotation
6941 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6944 case Tabular::TABULAR_VALIGN_TOP:
6945 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6948 case Tabular::TABULAR_VALIGN_MIDDLE:
6949 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6952 case Tabular::TABULAR_VALIGN_BOTTOM:
6953 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6956 case Tabular::LONGTABULAR_ALIGN_LEFT:
6957 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6960 case Tabular::LONGTABULAR_ALIGN_CENTER:
6961 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6964 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6965 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6968 case Tabular::SET_ROTATE_CELL:
6969 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6970 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6971 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6974 case Tabular::UNSET_ROTATE_CELL:
6975 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6976 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6977 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6980 case Tabular::TOGGLE_ROTATE_CELL:
6982 bool oneNotRotated = oneCellHasRotationState(false,
6983 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6985 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6986 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6987 // when pressing the rotate cell button we default to 90° rotation
6989 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6991 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6996 case Tabular::SET_USEBOX: {
6997 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6998 if (val == tabular.getUsebox(cur.idx()))
6999 val = Tabular::BOX_NONE;
7000 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7001 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7002 tabular.setUsebox(tabular.cellIndex(r, c), val);
7006 case Tabular::UNSET_LTFIRSTHEAD:
7009 case Tabular::SET_LTFIRSTHEAD:
7010 tabular.getRowOfLTFirstHead(row, ltt);
7011 checkLongtableSpecial(ltt, value, flag);
7012 tabular.setLTHead(row, flag, ltt, true);
7015 case Tabular::UNSET_LTHEAD:
7018 case Tabular::SET_LTHEAD:
7019 tabular.getRowOfLTHead(row, ltt);
7020 checkLongtableSpecial(ltt, value, flag);
7021 tabular.setLTHead(row, flag, ltt, false);
7024 case Tabular::UNSET_LTFOOT:
7027 case Tabular::SET_LTFOOT:
7028 tabular.getRowOfLTFoot(row, ltt);
7029 checkLongtableSpecial(ltt, value, flag);
7030 tabular.setLTFoot(row, flag, ltt, false);
7033 case Tabular::UNSET_LTLASTFOOT:
7036 case Tabular::SET_LTLASTFOOT:
7037 tabular.getRowOfLTLastFoot(row, ltt);
7038 checkLongtableSpecial(ltt, value, flag);
7039 tabular.setLTFoot(row, flag, ltt, true);
7042 case Tabular::UNSET_LTNEWPAGE:
7045 case Tabular::SET_LTNEWPAGE:
7046 tabular.setLTNewPage(row, flag);
7049 case Tabular::SET_LTCAPTION: {
7050 if (tabular.ltCaption(row))
7052 cur.idx() = tabular.setLTCaption(cur, row, true);
7055 cur.selection(false);
7056 // If a row is set as caption, then also insert
7057 // a caption. Otherwise the LaTeX output is broken.
7058 // Select cell if it is non-empty
7059 if (cur.lastpos() > 0 || cur.lastpit() > 0)
7060 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
7061 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
7065 case Tabular::UNSET_LTCAPTION: {
7066 if (!tabular.ltCaption(row))
7068 cur.idx() = tabular.setLTCaption(cur, row, false);
7071 cur.selection(false);
7072 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
7073 if (lyx::getStatus(fr).enabled())
7078 case Tabular::TOGGLE_LTCAPTION: {
7079 if (tabular.ltCaption(row))
7080 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
7082 tabularFeatures(cur, Tabular::SET_LTCAPTION);
7086 case Tabular::TOGGLE_BOOKTABS:
7087 tabular.use_booktabs = !tabular.use_booktabs;
7090 case Tabular::SET_BOOKTABS:
7091 tabular.use_booktabs = true;
7094 case Tabular::UNSET_BOOKTABS:
7095 tabular.use_booktabs = false;
7098 case Tabular::SET_TOP_SPACE: {
7100 if (value == "default")
7101 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7102 tabular.row_info[r].top_space_default = true;
7103 else if (value == "none")
7104 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7105 tabular.row_info[r].top_space_default = false;
7106 tabular.row_info[r].top_space = len;
7108 else if (isValidLength(value, &len))
7109 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7110 tabular.row_info[r].top_space_default = false;
7111 tabular.row_info[r].top_space = len;
7116 case Tabular::SET_BOTTOM_SPACE: {
7118 if (value == "default")
7119 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7120 tabular.row_info[r].bottom_space_default = true;
7121 else if (value == "none")
7122 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7123 tabular.row_info[r].bottom_space_default = false;
7124 tabular.row_info[r].bottom_space = len;
7126 else if (isValidLength(value, &len))
7127 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7128 tabular.row_info[r].bottom_space_default = false;
7129 tabular.row_info[r].bottom_space = len;
7134 case Tabular::SET_INTERLINE_SPACE: {
7136 if (value == "default")
7137 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7138 tabular.row_info[r].interline_space_default = true;
7139 else if (value == "none")
7140 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7141 tabular.row_info[r].interline_space_default = false;
7142 tabular.row_info[r].interline_space = len;
7144 else if (isValidLength(value, &len))
7145 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7146 tabular.row_info[r].interline_space_default = false;
7147 tabular.row_info[r].interline_space = len;
7152 case Tabular::SET_DECIMAL_POINT:
7153 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7154 tabular.column_info[c].decimal_point = from_utf8(value);
7157 // dummy stuff just to avoid warnings
7158 case Tabular::LAST_ACTION:
7164 bool InsetTabular::copySelection(Cursor & cur)
7166 if (!cur.selection())
7171 getSelection(cur, rs, re, cs, ce);
7173 paste_tabular.reset(new Tabular(tabular));
7175 for (row_type r = 0; r < rs; ++r)
7176 paste_tabular->deleteRow(0, true);
7178 row_type const rows = re - rs + 1;
7179 while (paste_tabular->nrows() > rows)
7180 paste_tabular->deleteRow(rows, true);
7182 for (col_type c = 0; c < cs; ++c)
7183 paste_tabular->deleteColumn(0, true);
7185 col_type const columns = ce - cs + 1;
7186 while (paste_tabular->ncols() > columns)
7187 paste_tabular->deleteColumn(columns, true);
7189 paste_tabular->setBuffer(tabular.buffer());
7191 odocstringstream os;
7192 OutputParams const runparams(nullptr);
7193 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7194 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7195 cap::copySelection(cur, os.str());
7197 // mark tabular stack dirty
7198 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7199 // when we (hopefully) have a one-for-all paste mechanism.
7200 // This must be called after cap::copySelection.
7201 dirtyTabularStack(true);
7207 bool InsetTabular::pasteClipboard(Cursor & cur)
7211 col_type actcol = tabular.cellColumn(cur.idx());
7212 row_type actrow = tabular.cellRow(cur.idx());
7214 if (cur.selIsMultiCell()) {
7217 getSelection(cur, actrow, re, actcol, ce);
7220 col_type const oldncols = tabular.ncols();
7221 for (row_type r1 = 0, r2 = actrow; r1 < paste_tabular->nrows(); ++r1, ++r2) {
7222 // Append rows if needed
7223 if (r2 == tabular.nrows())
7224 tabular.insertRow(r2 - 1, false);
7225 for (col_type c1 = 0, c2 = actcol; c1 < paste_tabular->ncols(); ++c1, ++c2) {
7226 // Append columns if needed
7227 if (c2 == tabular.ncols())
7228 tabular.insertColumn(c2 - 1, false);
7229 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7230 tabular.isPartOfMultiColumn(r2, c2))
7232 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7236 if (tabular.isPartOfMultiColumn(r2, c2)) {
7240 shared_ptr<InsetTableCell> inset(
7241 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7242 tabular.setCellInset(r2, c2, inset);
7243 // FIXME?: why do we need to do this explicitly? (EL)
7244 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7246 if (!lyxrc.ct_markup_copied) {
7247 // do not paste deleted text
7248 inset->acceptChanges();
7249 inset->setChange(Change(buffer().params().track_changes ?
7250 Change::INSERTED : Change::UNCHANGED));
7256 // amend cursor position if cols have been appended
7257 cur.idx() += actrow * (tabular.ncols() - oldncols);
7262 void InsetTabular::cutSelection(Cursor & cur)
7264 if (!cur.selection())
7269 getSelection(cur, rs, re, cs, ce);
7270 for (row_type r = rs; r <= re; ++r) {
7271 for (col_type c = cs; c <= ce; ++c) {
7272 shared_ptr<InsetTableCell> t
7273 = cell(tabular.cellIndex(r, c));
7274 if (buffer().params().track_changes)
7275 // FIXME: Change tracking (MG)
7276 t->setChange(Change(Change::DELETED));
7282 // cursor position might be invalid now
7283 if (cur.pit() > cur.lastpit())
7284 cur.pit() = cur.lastpit();
7285 if (cur.pos() > cur.lastpos())
7286 cur.pos() = cur.lastpos();
7287 cur.clearSelection();
7291 bool InsetTabular::isRightToLeft(Cursor & cur) const
7293 // LASSERT: It might be better to abandon this Buffer.
7294 LASSERT(cur.depth() > 1, return false);
7295 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7296 pos_type const parentpos = cur[cur.depth() - 2].pos();
7297 return parentpar.getFontSettings(buffer().params(),
7298 parentpos).language()->rightToLeft();
7302 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7305 LASSERT(stidx <= enidx, return docstring());
7307 col_type const col1 = tabular.cellColumn(stidx);
7308 col_type const col2 = tabular.cellColumn(enidx);
7309 row_type const row1 = tabular.cellRow(stidx);
7310 row_type const row2 = tabular.cellRow(enidx);
7312 for (col_type col = col1; col <= col2; col++)
7313 for (row_type row = row1; row <= row2; row++) {
7318 retval += tabular.cellInset(row, col)->asString(intoInsets);
7324 ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
7326 LASSERT(stidx <= enidx, return ParagraphList());
7327 ParagraphList retval;
7328 col_type const col1 = tabular.cellColumn(stidx);
7329 col_type const col2 = tabular.cellColumn(enidx);
7330 row_type const row1 = tabular.cellRow(stidx);
7331 row_type const row2 = tabular.cellRow(enidx);
7332 for (col_type col = col1; col <= col2; col++)
7333 for (row_type row = row1; row <= row2; row++)
7334 for (auto const & par : tabular.cellInset(row, col)->paragraphs())
7335 retval.push_back(par);
7340 void InsetTabular::getSelection(Cursor & cur,
7341 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7343 CursorSlice const & beg = cur.selBegin();
7344 CursorSlice const & end = cur.selEnd();
7345 cs = tabular.cellColumn(beg.idx());
7346 ce = tabular.cellColumn(end.idx());
7350 rs = tabular.cellRow(beg.idx());
7351 re = tabular.cellRow(end.idx());
7357 Text * InsetTabular::getText(int idx) const
7359 return size_t(idx) < nargs() ? cell(idx)->getText(0) : nullptr;
7363 bool InsetTabular::isChanged() const
7365 for (idx_type idx = 0; idx < nargs(); ++idx) {
7366 if (cell(idx)->isChanged())
7368 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7370 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7377 void InsetTabular::setChange(Change const & change)
7379 for (idx_type idx = 0; idx < nargs(); ++idx)
7380 cell(idx)->setChange(change);
7384 void InsetTabular::acceptChanges()
7386 for (idx_type idx = 0; idx < nargs(); ++idx)
7387 cell(idx)->acceptChanges();
7388 for (row_type row = 0; row < tabular.nrows(); ++row) {
7389 if (tabular.row_info[row].change.inserted())
7390 tabular.row_info[row].change.setUnchanged();
7391 else if (tabular.row_info[row].change.deleted())
7392 tabular.deleteRow(row, true);
7394 for (col_type col = 0; col < tabular.ncols(); ++col) {
7395 if (tabular.column_info[col].change.inserted())
7396 tabular.column_info[col].change.setUnchanged();
7397 else if (tabular.column_info[col].change.deleted())
7398 tabular.deleteColumn(col, true);
7400 tabular.updateIndexes();
7404 void InsetTabular::rejectChanges()
7406 for (idx_type idx = 0; idx < nargs(); ++idx)
7407 cell(idx)->rejectChanges();
7408 for (row_type row = 0; row < tabular.nrows(); ++row) {
7409 if (tabular.row_info[row].change.deleted())
7410 tabular.row_info[row].change.setUnchanged();
7411 else if (tabular.row_info[row].change.inserted())
7412 tabular.deleteRow(row, true);
7414 for (col_type col = 0; col < tabular.ncols(); ++col) {
7415 if (tabular.column_info[col].change.deleted())
7416 tabular.column_info[col].change.setUnchanged();
7417 else if (tabular.column_info[col].change.inserted())
7418 tabular.deleteColumn(col, true);
7420 tabular.updateIndexes();
7424 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7426 return tabular.getPWidth(cell).zero();
7430 bool InsetTabular::forcePlainLayout(idx_type cell) const
7432 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
7436 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7439 if (buf.length() <= 0)
7444 col_type maxCols = 1;
7445 size_t const len = buf.length();
7449 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7457 maxCols = max(cols, maxCols);
7463 maxCols = max(cols, maxCols);
7469 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7470 loctab = paste_tabular.get();
7471 dirtyTabularStack(true);
7474 cell = bv.cursor().idx();
7475 ocol = tabular.cellColumn(cell);
7476 row = tabular.cellRow(cell);
7480 idx_type cells = loctab->numberofcells;
7483 rows = loctab->nrows();
7484 col_type columns = loctab->ncols();
7487 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7493 // append column if necessary
7494 if (cols == columns) {
7495 loctab->appendColumn(cols - 1);
7496 columns = loctab->ncols();
7497 cells = loctab->numberofcells;
7500 if (cols < columns) {
7501 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7502 Font const font = bv.textMetrics(&inset->text()).
7504 inset->setText(buf.substr(op, p - op), font,
7505 buffer().params().track_changes);
7511 // we can only set this if we are not too far right
7512 if (cols < columns) {
7513 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7514 Font const font = bv.textMetrics(&inset->text()).
7516 inset->setText(buf.substr(op, p - op), font,
7517 buffer().params().track_changes);
7521 // append row if necessary
7522 if (row == rows && p < len - 1) {
7523 loctab->appendRow(row - 1);
7524 rows = loctab->nrows();
7525 cells = loctab->numberofcells;
7528 cell = loctab->cellIndex(row, cols);
7534 // check for the last cell if there is no trailing '\n'
7535 if (cell < cells && op < len) {
7536 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7537 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7538 inset->setText(buf.substr(op, len - op), font,
7539 buffer().params().track_changes);
7545 void InsetTabular::addPreview(DocIterator const & inset_pos,
7546 PreviewLoader & loader) const
7548 DocIterator cell_pos = inset_pos;
7550 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7551 for (row_type r = 0; r < tabular.nrows(); ++r) {
7552 for (col_type c = 0; c < tabular.ncols(); ++c) {
7553 cell_pos.top().idx() = tabular.cellIndex(r, c);
7554 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7560 bool InsetTabular::completionSupported(Cursor const & cur) const
7562 Cursor const & bvCur = cur.bv().cursor();
7563 if (&bvCur.inset() != this)
7565 return cur.text()->completionSupported(cur);
7569 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7571 return completionSupported(cur);
7575 bool InsetTabular::automaticInlineCompletion() const
7577 return lyxrc.completion_inline_text;
7581 bool InsetTabular::automaticPopupCompletion() const
7583 return lyxrc.completion_popup_text;
7587 bool InsetTabular::showCompletionCursor() const
7589 return lyxrc.completion_cursor_text;
7593 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7595 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : nullptr;
7599 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7601 if (!completionSupported(cur))
7603 return cur.text()->completionPrefix(cur);
7607 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7609 if (!completionSupported(cur))
7612 return cur.text()->insertCompletion(cur, s, finished);
7616 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7617 Dimension & dim) const
7619 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7620 tm.completionPosAndDim(cur, x, y, dim);
7624 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7626 istringstream data(in);
7628 lex.setStream(data);
7635 if (!lex || token != "tabular") {
7636 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7640 // This is part of the inset proper that is usually swallowed
7641 // by Buffer::readInset
7643 if (!lex || token != "Tabular") {
7644 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7652 string InsetTabular::params2string(InsetTabular const & inset)
7655 data << "tabular" << ' ';
7657 data << "\\end_inset\n";
7662 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7664 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
7665 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
7666 if (!tabular.isPartOfMultiColumn(r,c) &&
7667 !tabular.isPartOfMultiRow(r,c))
7670 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7671 ParagraphList::iterator it = parlist.begin();
7672 ParagraphList::iterator const en = parlist.end();
7673 for (; it != en; ++it)
7674 it->setLayout(dc.plainLayout());