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"
43 #include "MetricsInfo.h"
45 #include "output_xhtml.h"
46 #include "Paragraph.h"
47 #include "ParIterator.h"
49 #include "texstream.h"
50 #include "TextClass.h"
51 #include "TextMetrics.h"
53 #include "frontends/Application.h"
54 #include "frontends/alert.h"
55 #include "frontends/Clipboard.h"
56 #include "frontends/Painter.h"
57 #include "frontends/Selection.h"
59 #include "support/Changer.h"
60 #include "support/convert.h"
61 #include "support/debug.h"
62 #include "support/docstream.h"
63 #include "support/FileName.h"
64 #include "support/gettext.h"
65 #include "support/lassert.h"
66 #include "support/Lexer.h"
67 #include "support/lstrings.h"
68 #include "support/unique_ptr.h"
77 using namespace lyx::support;
83 using cap::dirtyTabularStack;
84 using cap::tabularStackDirty;
86 using graphics::PreviewLoader;
88 using frontend::Painter;
89 using frontend::Clipboard;
91 namespace Alert = frontend::Alert;
96 int const ADD_TO_HEIGHT = 2; // in cell
97 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
98 int const default_line_space = 10; // ?
99 int const WIDTH_OF_LINE = 5; // space between double lines
103 unique_ptr<Tabular> paste_tabular;
106 struct TabularFeature {
107 Tabular::Feature action;
113 TabularFeature tabularFeature[] =
115 // the SET/UNSET actions are used by the table dialog,
116 // the TOGGLE actions by the table toolbar buttons
117 // FIXME: these values have been hardcoded in InsetMathGrid and other
119 { Tabular::APPEND_ROW, "append-row", false },
120 { Tabular::APPEND_COLUMN, "append-column", false },
121 { Tabular::DELETE_ROW, "delete-row", false },
122 { Tabular::DELETE_COLUMN, "delete-column", false },
123 { Tabular::COPY_ROW, "copy-row", false },
124 { Tabular::COPY_COLUMN, "copy-column", false },
125 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
126 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
127 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
128 { Tabular::MOVE_ROW_UP, "move-row-up", false },
129 { Tabular::SET_LINE_TOP, "set-line-top", true },
130 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
131 { Tabular::SET_LTRIM_TOP, "set-ltrim-top", true },
132 { Tabular::SET_LTRIM_BOTTOM, "set-ltrim-bottom", true },
133 { Tabular::SET_RTRIM_TOP, "set-rtrim-top", true },
134 { Tabular::SET_RTRIM_BOTTOM, "set-rtrim-bottom", true },
135 { Tabular::SET_LINE_LEFT, "set-line-left", true },
136 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
137 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
138 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
139 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
140 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
141 { Tabular::TOGGLE_LTRIM_TOP, "toggle-ltrim-top", false },
142 { Tabular::TOGGLE_LTRIM_BOTTOM, "toggle-ltrim-bottom", false },
143 { Tabular::TOGGLE_RTRIM_TOP, "toggle-rtrim-top", false },
144 { Tabular::TOGGLE_RTRIM_BOTTOM, "toggle-rtrim-bottom", false },
145 { Tabular::ALIGN_LEFT, "align-left", false },
146 { Tabular::ALIGN_RIGHT, "align-right", false },
147 { Tabular::ALIGN_CENTER, "align-center", false },
148 { Tabular::ALIGN_BLOCK, "align-block", false },
149 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
150 { Tabular::VALIGN_TOP, "valign-top", false },
151 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
152 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
153 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
154 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
155 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
156 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
157 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
158 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
159 { Tabular::MULTICOLUMN, "multicolumn", false },
160 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
161 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
162 { Tabular::MULTIROW, "multirow", false },
163 { Tabular::SET_MULTIROW, "set-multirow", false },
164 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
165 { Tabular::SET_MROFFSET, "set-mroffset", true },
166 { Tabular::SET_ALL_LINES, "set-all-lines", false },
167 { Tabular::TOGGLE_ALL_LINES, "toggle-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::TOGGLE_BORDER_LINES, "toggle-border-lines", false },
206 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
207 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
208 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
209 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
210 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
211 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
212 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
213 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
214 { Tabular::SET_INNER_LINES, "set-inner-lines", false },
215 { Tabular::TOGGLE_INNER_LINES, "toggle-inner-lines", false },
216 { Tabular::LAST_ACTION, "", false }
220 string const tostr(LyXAlignment const & num)
225 case LYX_ALIGN_BLOCK:
229 case LYX_ALIGN_CENTER:
231 case LYX_ALIGN_RIGHT:
233 case LYX_ALIGN_LAYOUT:
235 case LYX_ALIGN_SPECIAL:
237 case LYX_ALIGN_DECIMAL:
244 string const tostr(Tabular::HAlignment const & num)
247 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
249 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
251 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
258 string const tostr(Tabular::VAlignment const & num)
261 case Tabular::LYX_VALIGN_TOP:
263 case Tabular::LYX_VALIGN_MIDDLE:
265 case Tabular::LYX_VALIGN_BOTTOM:
272 string const tostr(Tabular::BoxType const & num)
275 case Tabular::BOX_NONE:
277 case Tabular::BOX_PARBOX:
279 case Tabular::BOX_MINIPAGE:
281 case Tabular::BOX_VARWIDTH:
288 // I would have liked a fromstr template a lot better. (Lgb)
289 bool string2type(string const & str, LyXAlignment & num)
292 num = LYX_ALIGN_NONE;
293 else if (str == "block")
294 num = LYX_ALIGN_BLOCK;
295 else if (str == "left")
296 num = LYX_ALIGN_LEFT;
297 else if (str == "center")
298 num = LYX_ALIGN_CENTER;
299 else if (str == "right")
300 num = LYX_ALIGN_RIGHT;
301 else if (str == "decimal")
302 num = LYX_ALIGN_DECIMAL;
309 bool string2type(string const & str, Tabular::HAlignment & num)
312 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
313 else if (str == "center" )
314 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
315 else if (str == "right")
316 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
323 bool string2type(string const & str, Tabular::VAlignment & num)
326 num = Tabular::LYX_VALIGN_TOP;
327 else if (str == "middle" )
328 num = Tabular::LYX_VALIGN_MIDDLE;
329 else if (str == "bottom")
330 num = Tabular::LYX_VALIGN_BOTTOM;
337 bool string2type(string const & str, Tabular::BoxType & num)
340 num = Tabular::BOX_NONE;
341 else if (str == "parbox")
342 num = Tabular::BOX_PARBOX;
343 else if (str == "minipage")
344 num = Tabular::BOX_MINIPAGE;
345 else if (str == "varwidth")
346 num = Tabular::BOX_VARWIDTH;
353 bool string2type(string const & str, bool & num)
357 else if (str == "false")
365 bool getTokenValue(string const & str, char const * token, string & ret)
368 size_t token_length = strlen(token);
369 size_t pos = str.find(token);
371 if (pos == string::npos || pos + token_length + 1 >= str.length()
372 || str[pos + token_length] != '=')
374 pos += token_length + 1;
376 if (ch != '"' && ch != '\'') { // only read till next space
380 while (pos < str.length() - 1 && str[++pos] != ch)
387 bool getTokenValue(string const & str, char const * token, docstring & ret)
390 bool const success = getTokenValue(str, token, tmp);
391 ret = from_utf8(tmp);
396 bool getTokenValue(string const & str, char const * token, int & num)
400 if (!getTokenValue(str, token, tmp))
402 num = convert<int>(tmp);
407 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
410 return getTokenValue(str, token, tmp) && string2type(tmp, num);
414 bool getTokenValue(string const & str, char const * token,
415 Tabular::HAlignment & num)
418 return getTokenValue(str, token, tmp) && string2type(tmp, num);
422 bool getTokenValue(string const & str, char const * token,
423 Tabular::VAlignment & num)
426 return getTokenValue(str, token, tmp) && string2type(tmp, num);
430 bool getTokenValue(string const & str, char const * token,
431 Tabular::BoxType & num)
434 return getTokenValue(str, token, tmp) && string2type(tmp, num);
438 bool getTokenValue(string const & str, char const * token, bool & flag)
440 // set the flag always to false as this should be the default for bools
441 // not in the file-format.
444 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
448 bool getTokenValue(string const & str, char const * token, Length & len)
450 // set the length to be zero() as default as this it should be if not
451 // in the file format.
454 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
458 bool getTokenValue(string const & str, char const * token, Change & change, BufferParams & bp)
460 // set the change to be Change() as default as this it should be if not
461 // in the file format.
464 if (getTokenValue(str, token, tmp)) {
465 vector<string> const changedata = getVectorFromString(tmp, " ");
466 if (changedata.size() != 3) {
467 Alert::warning(_("Change tracking data incomplete"),
468 _("Change tracking information for tabular row/column "
469 "is incomplete. I will ignore this."));
472 BufferParams::AuthorMap const & am = bp.author_map_;
473 int aid = convert<int>(changedata[1]);
474 if (am.find(aid) == am.end()) {
475 // FIXME Use ErrorList
476 Alert::warning(_("Change tracking author index missing"),
477 bformat(_("A change tracking author information for index "
478 "%1$d is missing. This can happen after a wrong "
479 "merge by a version control system. In this case, "
480 "either fix the merge, or have this information "
481 "missing until the corresponding tracked changes "
482 "are merged or this user edits the file again.\n"),
484 bp.addAuthor(Author(aid));
486 istringstream is(changedata[2]);
489 if (changedata[0] == "inserted") {
490 change = Change(Change::INSERTED, am.find(aid)->second, ct);
492 } else if (changedata[0] == "deleted") {
493 change = Change(Change::DELETED, am.find(aid)->second, ct);
501 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
506 if (!getTokenValue(str, token, tmp))
508 if (tmp == "default") {
512 return isValidLength(tmp, &len);
516 void l_getline(istream & is, string & str)
519 while (str.empty()) {
521 if (!str.empty() && str[str.length() - 1] == '\r')
522 str.erase(str.length() - 1);
527 string const write_attribute(string const & name, T const & t)
529 string const s = tostr(t);
530 return s.empty() ? s : " " + name + "=\"" + s + "\"";
534 string const write_attribute(string const & name, string const & t)
536 return t.empty() ? t : " " + name + "=\"" + t + "\"";
541 string const write_attribute(string const & name, docstring const & t)
543 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
548 string const write_attribute(string const & name, bool const & b)
550 // we write only true attribute values so we remove a bit of the
551 // file format bloat for tabulars.
552 return b ? write_attribute(name, convert<string>(b)) : string();
557 string const write_attribute(string const & name, int const & i)
559 // we write only true attribute values so we remove a bit of the
560 // file format bloat for tabulars.
561 return i ? write_attribute(name, convert<string>(i)) : string();
566 string const write_attribute(string const & name, idx_type const & i)
568 // we write only true attribute values so we remove a bit of the
569 // file format bloat for tabulars.
570 return i ? write_attribute(name, convert<string>(i)) : string();
575 string const write_attribute(string const & name, Length const & value)
577 // we write only the value if we really have one same reason as above.
578 return value.zero() ? string() : write_attribute(name, value.asString());
581 string const write_attribute(string const & name, Change const & change, BufferParams const & bp)
583 odocstringstream ods;
584 if (change.inserted())
585 ods << from_ascii("inserted");
586 else if (change.deleted())
587 ods << from_ascii("deleted");
588 if (change.changed()) {
589 ods << " " << bp.authors().get(change.author).bufferId()
590 << " " << change.changetime;
591 return write_attribute(name, ods.str());
599 string const featureAsString(Tabular::Feature action)
601 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
602 if (tabularFeature[i].action == action)
603 return tabularFeature[i].feature;
609 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
611 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
612 for (; dit; dit.forwardChar())
613 if (dit.inTexted() && dit.depth() == 1
614 && dit.paragraph().find(align_d, false, false, dit.pos()))
621 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
623 InsetTableCell tail = head;
624 DocIterator const dit = separatorPos(&head, align_d);
625 hassep = static_cast<bool>(dit);
627 pos_type const psize = head.paragraphs().front().size();
628 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
629 tail.paragraphs().front().eraseChars(0,
630 dit.pos() < psize ? dit.pos() + 1 : psize, false);
637 /////////////////////////////////////////////////////////////////////
641 /////////////////////////////////////////////////////////////////////
644 Tabular::CellData::CellData(Buffer * buf)
647 multicolumn(Tabular::CELL_NORMAL),
648 multirow(Tabular::CELL_NORMAL),
649 alignment(LYX_ALIGN_CENTER),
650 valignment(LYX_VALIGN_TOP),
658 top_line_rtrimmed(false),
659 top_line_ltrimmed(false),
660 bottom_line_rtrimmed(false),
661 bottom_line_ltrimmed(false),
664 inset(new InsetTableCell(buf))
666 inset->setBuffer(*buf);
670 Tabular::CellData::CellData(CellData const & cs)
673 multicolumn(cs.multicolumn),
674 multirow(cs.multirow),
675 mroffset(cs.mroffset),
676 alignment(cs.alignment),
677 valignment(cs.valignment),
678 decimal_hoffset(cs.decimal_hoffset),
679 decimal_width(cs.decimal_width),
681 top_line(cs.top_line),
682 bottom_line(cs.bottom_line),
683 left_line(cs.left_line),
684 right_line(cs.right_line),
685 top_line_rtrimmed(cs.top_line_rtrimmed),
686 top_line_ltrimmed(cs.top_line_ltrimmed),
687 bottom_line_rtrimmed(cs.bottom_line_rtrimmed),
688 bottom_line_ltrimmed(cs.bottom_line_ltrimmed),
691 align_special(cs.align_special),
693 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
697 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
703 multicolumn = cs.multicolumn;
704 multirow = cs.multirow;
705 mroffset = cs.mroffset;
706 alignment = cs.alignment;
707 valignment = cs.valignment;
708 decimal_hoffset = cs.decimal_hoffset;
709 decimal_width = cs.decimal_width;
710 voffset = cs.voffset;
711 top_line = cs.top_line;
712 bottom_line = cs.bottom_line;
713 left_line = cs.left_line;
714 right_line = cs.right_line;
715 top_line_rtrimmed = cs.top_line_rtrimmed;
716 top_line_ltrimmed = cs.top_line_ltrimmed;
717 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
718 bottom_line_ltrimmed = cs.bottom_line_ltrimmed;
721 align_special = cs.align_special;
722 p_width = cs.p_width;
723 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
727 Tabular::RowData::RowData()
730 top_space_default(false),
731 bottom_space_default(false),
732 interline_space_default(false),
743 Tabular::ColumnData::ColumnData()
744 : alignment(LYX_ALIGN_CENTER),
745 valignment(LYX_VALIGN_TOP),
753 Tabular::ltType::ltType()
754 : set(false), topDL(false),
760 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
762 init(buffer, rows_arg, columns_arg);
766 void Tabular::setBuffer(Buffer & buffer)
769 for (row_type i = 0; i < nrows(); ++i)
770 for (col_type j = 0; j < ncols(); ++j)
771 cell_info[i][j].inset->setBuffer(*buffer_);
775 // activates all lines and sets all widths to 0
776 void Tabular::init(Buffer * buf, row_type rows_arg,
777 col_type columns_arg)
780 row_info = row_vector(rows_arg);
781 column_info = column_vector(columns_arg);
782 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
783 row_info.reserve(10);
784 column_info.reserve(10);
785 cell_info.reserve(100);
786 is_long_tabular = false;
788 tabular_valignment = LYX_VALIGN_MIDDLE;
789 tabular_width = Length();
790 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
792 use_booktabs = false;
793 // set silly default lines
794 for (row_type r = 0; r < nrows(); ++r)
795 for (col_type c = 0; c < ncols(); ++c) {
796 cell_info[r][c].inset->setBuffer(*buffer_);
797 cell_info[r][c].top_line = true;
798 cell_info[r][c].left_line = true;
799 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
800 cell_info[r][c].right_line = c == ncols() - 1;
805 void Tabular::deleteRow(row_type const row, bool const force)
807 // Not allowed to delete last row
811 LASSERT(row < nrows(), return);
813 // If we are in change tracking mode, and the row is not marked
814 // as inserted by the same author, we do not delete it, but mark
816 bool const track_deletion = force ? false
817 : buffer().params().track_changes
818 && (!row_info[row].change.inserted()
819 || !row_info[row].change.currentAuthor());
821 for (col_type c = 0; c < ncols(); ++c) {
822 // Care about multirow cells
823 if (row + 1 < nrows() &&
824 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
825 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
826 cell_info[row + 1][c] = cell_info[row][c];
830 row_info[row].change.setDeleted();
832 row_info.erase(row_info.begin() + row);
833 cell_info.erase(cell_info.begin() + row);
839 void Tabular::copyRow(row_type const row)
841 insertRow(row, true);
845 void Tabular::appendRow(row_type row)
847 insertRow(row, false);
851 void Tabular::insertRow(row_type const row, bool copy)
853 row_info.insert(row_info.begin() + row + 1, row_info[row]);
854 cell_info.insert(cell_info.begin() + row + 1,
855 cell_vector(0, CellData(buffer_)));
857 for (col_type c = 0; c < ncols(); ++c) {
858 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
859 copy ? cell_info[row][c] : CellData(buffer_));
860 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
861 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
865 for (col_type c = 0; c < ncols(); ++c) {
866 if (isPartOfMultiRow(row, c))
868 // inherit line settings
869 idx_type const i = cellIndex(row + 1, c);
870 idx_type const j = cellIndex(row, c);
871 setLeftLine(i, leftLine(j));
872 setRightLine(i, rightLine(j));
873 setTopLine(i, topLine(j));
874 if (topLine(j) && bottomLine(j)) {
875 setBottomLine(i, true);
876 setBottomLine(j, false);
879 if (buffer().params().track_changes) {
880 row_info[row + 1].change.setInserted();
886 void Tabular::moveColumn(col_type col_start, col_type col_end,
887 ColDirection direction)
889 if (direction == Tabular::LEFT) {
890 for (col_type col = col_start; col <= col_end; ++col) {
891 std::swap(column_info[col - 1], column_info[col]);
892 for (row_type r = 0; r < nrows(); ++r) {
893 std::swap(cell_info[r][col - 1], cell_info[r][col]);
894 std::swap(cell_info[r][col - 1].left_line, cell_info[r][col].left_line);
895 std::swap(cell_info[r][col - 1].right_line, cell_info[r][col].right_line);
897 if (buffer().params().track_changes) {
898 idx_type const i = cellIndex(r, col - 1);
899 idx_type const j = cellIndex(r, col);
900 cellInfo(i).inset->setChange(Change(Change::INSERTED));
901 cellInfo(j).inset->setChange(Change(Change::INSERTED));
909 for (col_type col = col_end; col >= col_start; --col) {
910 std::swap(column_info[col], column_info[col + 1]);
911 for (row_type r = 0; r < nrows(); ++r) {
912 std::swap(cell_info[r][col], cell_info[r][col + 1]);
913 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
914 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
916 if (buffer().params().track_changes) {
917 idx_type const i = cellIndex(r, col);
918 idx_type const j = cellIndex(r, col + 1);
919 cellInfo(i).inset->setChange(Change(Change::INSERTED));
920 cellInfo(j).inset->setChange(Change(Change::INSERTED));
931 void Tabular::moveRow(row_type row_start, row_type row_end, RowDirection direction)
933 if (direction == Tabular::UP) {
934 for (row_type row = row_start; row <= row_end; ++row) {
935 std::swap(row_info[row - 1], row_info[row]);
936 for (col_type c = 0; c < ncols(); ++c) {
937 std::swap(cell_info[row - 1][c], cell_info[row][c]);
938 std::swap(cell_info[row - 1][c].top_line, cell_info[row][c].top_line);
939 std::swap(cell_info[row - 1][c].bottom_line, cell_info[row][c].bottom_line);
941 idx_type const i = cellIndex(row - 1, c);
942 idx_type const j = cellIndex(row, c);
943 if (buffer().params().track_changes) {
944 cellInfo(i).inset->setChange(Change(Change::INSERTED));
945 cellInfo(j).inset->setChange(Change(Change::INSERTED));
953 for (row_type row = row_end; row >= row_start; --row) {
954 std::swap(row_info[row], row_info[row + 1]);
955 for (col_type c = 0; c < ncols(); ++c) {
956 std::swap(cell_info[row][c], cell_info[row + 1][c]);
957 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
958 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
960 idx_type const i = cellIndex(row, c);
961 idx_type const j = cellIndex(row + 1, c);
962 if (buffer().params().track_changes) {
963 cellInfo(i).inset->setChange(Change(Change::INSERTED));
964 cellInfo(j).inset->setChange(Change(Change::INSERTED));
975 void Tabular::deleteColumn(col_type const col, bool const force)
977 // Not allowed to delete last column
981 LASSERT(col < ncols(), return);
983 // If we are in change tracking mode, and the column is not marked
984 // as inserted by the same author, we do not delete it, but mark
986 bool const track_deletion = force ? false
987 : buffer().params().track_changes
988 && (!column_info[col].change.inserted()
989 || !column_info[col].change.currentAuthor());
991 for (row_type r = 0; r < nrows(); ++r) {
992 // Care about multicolumn cells
993 if (col + 1 < ncols() &&
994 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
995 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
996 cell_info[r][col + 1] = cell_info[r][col];
999 cell_info[r].erase(cell_info[r].begin() + col);
1002 column_info[col].change.setDeleted();
1004 column_info.erase(column_info.begin() + col);
1009 void Tabular::copyColumn(col_type const col)
1011 insertColumn(col, true);
1015 void Tabular::appendColumn(col_type col)
1017 insertColumn(col, false);
1021 void Tabular::insertColumn(col_type const col, bool copy)
1023 bool const ct = buffer().params().track_changes;
1024 column_info.insert(column_info.begin() + col + 1, column_info[col]);
1026 for (row_type r = 0; r < nrows(); ++r) {
1027 cell_info[r].insert(cell_info[r].begin() + col + 1,
1028 copy ? cell_info[r][col] : CellData(buffer_));
1029 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
1030 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
1033 for (row_type r = 0; r < nrows(); ++r) {
1034 // inherit line settings
1035 idx_type const i = cellIndex(r, col + 1);
1036 idx_type const j = cellIndex(r, col);
1037 setBottomLine(i, bottomLine(j));
1038 setTopLine(i, topLine(j));
1039 setLeftLine(i, leftLine(j));
1040 setRightLine(i, rightLine(j));
1041 if (rightLine(i) && rightLine(j)) {
1042 setRightLine(j, false);
1046 column_info[col + 1].change.setInserted();
1052 void Tabular::updateIndexes()
1054 setBuffer(buffer());
1056 // reset cell number
1057 for (row_type row = 0; row < nrows(); ++row)
1058 for (col_type column = 0; column < ncols(); ++column) {
1059 if (!isPartOfMultiColumn(row, column)
1060 && !isPartOfMultiRow(row, column))
1062 if (isPartOfMultiRow(row, column))
1063 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1065 cell_info[row][column].cellno = numberofcells - 1;
1068 rowofcell.resize(numberofcells);
1069 columnofcell.resize(numberofcells);
1071 // reset column and row of cells and update their width, alignment, caption, and ct status
1072 for (row_type row = 0; row < nrows(); ++row) {
1073 for (col_type column = 0; column < ncols(); ++column) {
1074 cell_info[row][column].inset->toggleCaptionRow(is_long_tabular && ltCaption(row));
1075 if (isPartOfMultiColumn(row, column)) {
1076 cell_info[row][column].inset->toggleMultiCol(true);
1079 cell_info[row][column].inset->toggleMultiCol(false);
1080 // columnofcell needs to be called before setting width and alignment
1081 // multirow cells inherit the width from the column width
1082 if (!isPartOfMultiRow(row, column)) {
1083 columnofcell[i] = column;
1086 setFixedWidth(row, column);
1087 if (isPartOfMultiRow(row, column)) {
1088 cell_info[row][column].inset->toggleMultiRow(true);
1091 cell_info[row][column].inset->toggleMultiRow(false);
1092 cell_info[row][column].inset->setContentAlignment(
1093 getAlignment(cellIndex(row, column)));
1094 if (buffer().params().track_changes) {
1095 if (row_info[row].change.changed())
1096 cell_info[row][column].inset->setChange(row_info[row].change);
1097 if (column_info[column].change.changed())
1098 cell_info[row][column].inset->setChange(column_info[column].change);
1106 idx_type Tabular::numberOfCellsInRow(row_type const row) const
1108 idx_type result = 0;
1109 for (col_type c = 0; c < ncols(); ++c)
1110 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1116 bool Tabular::topLine(idx_type const cell) const
1118 return cellInfo(cell).top_line;
1122 bool Tabular::bottomLine(idx_type const cell) const
1124 return cellInfo(cell).bottom_line;
1128 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1130 if (use_booktabs && !ignore_bt)
1132 return cellInfo(cell).left_line;
1136 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1138 if (use_booktabs && !ignore_bt)
1140 return cellInfo(cell).right_line;
1144 bool Tabular::outsideBorders(
1145 row_type const sel_row_start, row_type const sel_row_end,
1146 col_type const sel_col_start, col_type const sel_col_end) const
1149 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
1150 if (!leftLine(cellIndex(r, sel_col_start))
1151 || !rightLine(cellIndex(r, sel_col_end)))
1154 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
1155 if (!topLine(cellIndex(sel_row_start, c))
1156 || !bottomLine(cellIndex(sel_row_end, c)))
1163 bool Tabular::innerBorders(
1164 row_type const sel_row_start, row_type const sel_row_end,
1165 col_type const sel_col_start, col_type const sel_col_end) const
1167 // Single cell has no inner borders
1168 if (sel_row_start == sel_row_end && sel_col_start == sel_col_end)
1170 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
1171 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
1172 idx_type const cell = cellIndex(r, c);
1173 if ((r != sel_row_start && !topLine(cell)
1174 && cell_info[r][c].multirow != CELL_PART_OF_MULTIROW)
1176 && c != sel_col_start && !leftLine(cell)
1177 && cell_info[r][c].multicolumn != CELL_PART_OF_MULTICOLUMN))
1184 void Tabular::setLines(
1185 row_type const sel_row_start, row_type const sel_row_end,
1186 col_type const sel_col_start, col_type const sel_col_end,
1187 bool setLinesInnerOnly, bool setLines)
1189 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
1190 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
1191 idx_type const cell = cellIndex(r, c);
1192 if (!(setLinesInnerOnly && r == sel_row_start)
1193 // for multirows, cell is taken care of at beginning
1194 && cell_info[r][c].multirow != CELL_PART_OF_MULTIROW)
1195 setTopLine(cell, setLines);
1196 if (!(setLinesInnerOnly && r == sel_row_end)
1197 && (r == sel_row_end || (!setLines
1198 // for multirows, cell is taken care of at the last part
1199 && cell_info[r + 1][c].multirow != CELL_PART_OF_MULTIROW)))
1200 setBottomLine(cell, setLines);
1201 if (!(setLinesInnerOnly && c == sel_col_start)
1202 // for multicolumns, cell is taken care of at beginning
1203 && cell_info[r][c].multicolumn != CELL_PART_OF_MULTICOLUMN)
1204 setLeftLine(cell, setLines);
1205 if (!(setLinesInnerOnly && c == sel_col_end)
1206 && (c == sel_col_end || (!setLines
1207 // for multicolumns, cell is taken care of at the last part
1208 && cell_info[r][c + 1].multicolumn != CELL_PART_OF_MULTICOLUMN)))
1209 setRightLine(cell, setLines);
1214 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1217 return make_pair(false, false);
1218 return make_pair(cellInfo(cell).top_line_ltrimmed,
1219 cellInfo(cell).top_line_rtrimmed);
1223 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1226 return make_pair(false, false);
1227 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1228 cellInfo(cell).bottom_line_rtrimmed);
1232 int Tabular::interRowSpace(row_type row) const
1234 if (!row || row >= nrows())
1237 int const interline_space = row_info[row - 1].interline_space_default ?
1238 default_line_space :
1239 row_info[row - 1].interline_space.inPixels(width());
1240 if (rowTopLine(row) && rowBottomLine(row - 1))
1241 return interline_space + WIDTH_OF_LINE;
1242 return interline_space;
1246 int Tabular::interColumnSpace(idx_type cell) const
1248 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1249 if (rightLine(cell) && nextcol < ncols()
1250 && leftLine(cellIndex(cellRow(cell), nextcol)))
1251 return WIDTH_OF_LINE;
1256 int Tabular::cellWidth(idx_type cell) const
1259 col_type const span = columnSpan(cell);
1260 col_type const col = cellColumn(cell);
1261 for(col_type c = col; c < col + span ; ++c)
1262 w += column_info[c].width;
1267 int Tabular::cellHeight(idx_type cell) const
1269 row_type const span = rowSpan(cell);
1270 row_type const row = cellRow(cell);
1272 for(row_type r = row; r < row + span ; ++r) {
1273 h += rowAscent(r) + rowDescent(r);
1274 if (r != row + span - 1)
1275 h += interRowSpace(r + 1);
1282 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1284 vector<int> max_dwidth(ncols(), 0);
1285 // collect max. fixed width of column
1286 map<col_type, int> max_pwidth;
1287 // collect max. variable width of column
1288 map<col_type, int> max_width;
1290 for(col_type c = 0; c < ncols(); ++c)
1291 for(row_type r = 0; r < nrows(); ++r) {
1292 idx_type const i = cellIndex(r, c);
1293 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1294 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1295 if (!getPWidth(i).zero())
1296 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1297 else if (!column_info[c].varwidth)
1298 max_width[c] = max(max_width[c], cell_info[r][c].width);
1301 // If we have a fixed tabular width, we take this into account
1302 Length tab_width = tabular_width;
1303 bool const tabularx = hasVarwidthColumn();
1304 if (tabularx && tab_width.zero())
1305 // If no tabular width is specified with X columns,
1306 // we use 100% colwidth
1307 tab_width = Length(100, Length::PCW);
1309 if (!tab_width.zero()) {
1310 restwidth = mi.base.inPixels(tab_width);
1311 // Subtract the fixed widths from the table width
1312 for (auto const & w : max_pwidth)
1313 restwidth -= w.second;
1316 // If we have a fixed width, distribute the available table width
1317 // (minus the fixed widths) to the variable-width columns
1319 int restcols = ncols() - max_pwidth.size();
1320 if ((restwidth > 0) && (restcols != 0))
1321 vcolwidth = restwidth / restcols;
1323 // Now consider that some variable width columns exceed the vcolwidth
1324 if (vcolwidth > 0) {
1325 bool changed = false;
1326 for (auto const & w : max_width) {
1327 if (tabularx || w.second > vcolwidth) {
1329 restwidth -= w.second;
1333 if (changed && restwidth > 0 && restcols != 0)
1334 vcolwidth = restwidth / restcols;
1337 bool update = false;
1338 // for each col get max of single col cells
1339 for(col_type c = 0; c < ncols(); ++c) {
1341 for(row_type r = 0; r < nrows(); ++r) {
1342 idx_type const i = cellIndex(r, c);
1343 if (columnSpan(i) == 1) {
1344 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1345 && cell_info[r][c].decimal_width != 0)
1346 new_width = max(new_width, cellInfo(i).width
1347 + max_dwidth[c] - cellInfo(i).decimal_width);
1348 else if (getPWidth(i).zero() && vcolwidth > 0) {
1349 if (tabularx && !column_info[c].varwidth)
1350 new_width = max(new_width, cellInfo(i).width);
1352 new_width = vcolwidth;
1354 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1356 new_width = max(new_width, cellInfo(i).width);
1360 if (column_info[c].width != new_width) {
1361 column_info[c].width = new_width;
1362 // Do not trigger update when no space is left for variable
1363 // columns, as this will loop
1364 update = tab_width.zero() || restwidth > 0;
1367 // update col widths to fit merged cells
1368 for(col_type c = 0; c < ncols(); ++c)
1369 for(row_type r = 0; r < nrows(); ++r) {
1370 idx_type const i = cellIndex(r, c);
1371 int const span = columnSpan(i);
1372 if (span == 1 || c > cellColumn(i))
1376 for(col_type j = c; j < c + span ; ++j)
1377 old_width += column_info[j].width;
1379 if (cellInfo(i).width > old_width) {
1380 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1381 // Do not trigger update when no space is left for variable
1382 // columns, as this will loop
1383 update = tab_width.zero() || restwidth > 0;
1391 int Tabular::width() const
1394 for (col_type c = 0; c < ncols(); ++c)
1395 width += column_info[c].width;
1400 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1403 col_type const col = cellColumn(cell);
1404 // set alignment for the whole row if we are not in a multicolumn cell,
1405 // exclude possible multicolumn cells in the row
1406 if (!isMultiColumn(cell)) {
1407 for (row_type r = 0; r < nrows(); ++r) {
1408 // only if the column has no width the multirow inherits the
1409 // alignment of the column, otherwise it is left aligned
1410 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1411 && !isMultiColumn(cellIndex(r, col))) {
1412 cell_info[r][col].alignment = align;
1413 cell_info[r][col].inset->setContentAlignment(align);
1415 if ((isMultiRow(cellIndex(r, col)) && has_width)
1416 && !isMultiColumn(cellIndex(r, col))) {
1417 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1418 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1421 column_info[col].alignment = align;
1422 docstring & dpoint = column_info[col].decimal_point;
1423 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1424 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1425 dpoint = tlang->decimalSeparator();
1428 cellInfo(cell).alignment = align;
1429 cellInset(cell)->setContentAlignment(align);
1434 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1437 if (!isMultiColumn(cell) || onlycolumn)
1438 column_info[cellColumn(cell)].valignment = align;
1440 cellInfo(cell).valignment = align;
1447 * Allow line and paragraph breaks for fixed width multirow cells
1448 * or disallow them, merge cell paragraphs and reset layout to standard
1449 * for variable width multicol cells.
1451 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1452 bool const fixedWidth, bool const multirow)
1454 inset->toggleFixedWidth(fixedWidth);
1458 BufferParams const & bp = cur.bv().buffer().params();
1461 // undo information has already been recorded
1462 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1463 bp.documentClass().plainLayoutName());
1470 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1471 Length const & width)
1473 col_type const c = cellColumn(cell);
1475 column_info[c].p_width = width;
1476 // reset the vertical alignment to top if the fixed width
1477 // is removed or zero because only fixed width columns can
1478 // have a vertical alignment
1479 if (column_info[c].p_width.zero())
1480 column_info[c].valignment = LYX_VALIGN_TOP;
1481 for (row_type r = 0; r < nrows(); ++r) {
1482 idx_type const cidx = cellIndex(r, c);
1483 // because of multicolumns
1484 toggleFixedWidth(cur, cellInset(cidx).get(),
1485 !getPWidth(cidx).zero(), isMultiRow(cidx));
1486 if (isMultiRow(cidx))
1487 setAlignment(cidx, column_info[c].alignment,
1488 !column_info[c].p_width.zero());
1490 // cur can become invalid after paragraphs were merged
1495 bool Tabular::setFixedWidth(row_type r, col_type c)
1497 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1498 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1499 || (multicol && !cell_info[r][c].p_width.zero());
1500 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1505 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1506 Length const & width)
1508 if (!isMultiColumn(cell))
1511 cellInfo(cell).p_width = width;
1512 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1514 // cur can become invalid after paragraphs were merged
1520 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1522 column_info[cellColumn(cell)].varwidth = varwidth;
1523 cellInset(cell).get()->toggleVarWidth(varwidth);
1528 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1530 cellInfo(cell).mroffset = mroffset;
1535 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1536 Tabular::Feature what)
1538 if (what == SET_SPECIAL_MULTICOLUMN)
1539 cellInfo(cell).align_special = special;
1541 column_info[cellColumn(cell)].align_special = special;
1545 void Tabular::setTopLine(idx_type i, bool line)
1547 cellInfo(i).top_line = line;
1551 void Tabular::setBottomLine(idx_type i, bool line)
1553 cellInfo(i).bottom_line = line;
1557 void Tabular::setTopLineLTrim(idx_type i, bool val)
1559 cellInfo(i).top_line_ltrimmed = val;
1563 void Tabular::setTopLineRTrim(idx_type i, bool val)
1565 cellInfo(i).top_line_rtrimmed = val;
1569 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1571 cellInfo(i).bottom_line_ltrimmed = val;
1575 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1577 cellInfo(i).bottom_line_rtrimmed = val;
1581 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1583 setTopLineLTrim(i, trim.first);
1584 setTopLineRTrim(i, trim.second);
1587 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1589 setBottomLineLTrim(i, trim.first);
1590 setBottomLineRTrim(i, trim.second);
1593 void Tabular::setLeftLine(idx_type cell, bool line)
1595 cellInfo(cell).left_line = line;
1599 void Tabular::setRightLine(idx_type cell, bool line)
1601 cellInfo(cell).right_line = line;
1604 bool Tabular::rowTopLine(row_type r) const
1606 bool all_rows_set = true;
1607 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1608 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1609 return all_rows_set;
1613 bool Tabular::rowBottomLine(row_type r) const
1615 bool all_rows_set = true;
1616 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1617 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1618 return all_rows_set;
1622 bool Tabular::columnLeftLine(col_type c) const
1629 for (row_type r = 0; r < nrows(); ++r) {
1630 idx_type const i = cellIndex(r, c);
1631 if (c == cellColumn(i)) {
1633 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1634 if (cellInfo(i).left_line || right)
1638 return 2 * nrows_left >= total;
1642 bool Tabular::columnRightLine(col_type c) const
1647 int nrows_right = 0;
1649 for (row_type r = 0; r < nrows(); ++r) {
1650 idx_type i = cellIndex(r, c);
1651 if (c == cellColumn(i) + columnSpan(i) - 1) {
1653 bool left = (c + 1 < ncols()
1654 && cellInfo(cellIndex(r, c + 1)).left_line)
1655 || c + 1 == ncols();
1656 if (cellInfo(i).right_line && left)
1660 return 2 * nrows_right >= total;
1664 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1666 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1667 return cellInfo(cell).alignment;
1669 return column_info[cellColumn(cell)].alignment;
1674 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1676 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1677 return cellInfo(cell).valignment;
1678 return column_info[cellColumn(cell)].valignment;
1682 int Tabular::offsetVAlignment() const
1684 // for top-alignment the first horizontal table line must be exactly at
1685 // the position of the base line of the surrounding text line
1686 // for bottom alignment, the same is for the last table line
1687 int offset_valign = 0;
1688 switch (tabular_valignment) {
1689 case Tabular::LYX_VALIGN_BOTTOM:
1690 offset_valign = rowAscent(0) - height();
1692 case Tabular::LYX_VALIGN_MIDDLE:
1693 offset_valign = (- height()) / 2 + rowAscent(0);
1695 case Tabular::LYX_VALIGN_TOP:
1696 offset_valign = rowAscent(0);
1699 return offset_valign;
1703 Length const Tabular::getPWidth(idx_type cell) const
1705 if (isMultiColumn(cell))
1706 return cellInfo(cell).p_width;
1707 return column_info[cellColumn(cell)].p_width;
1711 Length const Tabular::getMROffset(idx_type cell) const
1713 return cellInfo(cell).mroffset;
1717 int Tabular::textHOffset(idx_type cell) const
1719 // the LaTeX Way :-(
1720 int x = WIDTH_OF_LINE;
1722 int const w = cellWidth(cell) - cellInfo(cell).width;
1724 switch (getAlignment(cell)) {
1725 case LYX_ALIGN_CENTER:
1728 case LYX_ALIGN_RIGHT:
1731 case LYX_ALIGN_DECIMAL: {
1732 // we center when no decimal point
1733 if (cellInfo(cell).decimal_width == 0) {
1737 col_type const c = cellColumn(cell);
1738 int max_dhoffset = 0;
1739 for(row_type r = 0; r < row_info.size() ; ++r) {
1740 idx_type const i = cellIndex(r, c);
1741 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1742 && cellInfo(i).decimal_width != 0)
1743 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1745 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1748 // LYX_ALIGN_LEFT: nothing :-)
1756 int Tabular::textVOffset(idx_type cell) const
1758 int voffset = cellInfo(cell).voffset;
1759 if (isMultiRow(cell)) {
1760 row_type const row = cellRow(cell);
1761 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1767 idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1770 idx_type const numcells = numberOfCellsInRow(row);
1771 // we check against numcells to make sure we do not crash if all the
1772 // cells are multirow (bug #7535), but in that case our return value
1773 // is really invalid, i.e., it is NOT the first cell in the row. but
1774 // i do not know what to do here. (rgh)
1775 while (c < numcells - 1
1776 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1777 || (ct && column_info[c].change.deleted())))
1779 return cell_info[row][c].cellno;
1783 idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1785 col_type c = ncols() - 1;
1786 // of course we check against 0 so we don't crash. but we have the same
1787 // problem as in the previous routine: if all the cells are part of a
1788 // multirow or part of a multi column, then our return value is invalid.
1790 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1791 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1792 || (ct && column_info[c].change.deleted())))
1794 return cell_info[row][c].cellno;
1798 row_type Tabular::getFirstRow(bool const ct) const
1803 // exclude deleted rows if ct == true
1804 while (r < nrows() && row_info[r].change.deleted())
1810 row_type Tabular::getLastRow(bool const ct) const
1812 row_type r = nrows() - 1;
1815 // exclude deleted rows if ct == true
1816 while (r > 0 && row_info[r].change.deleted())
1822 row_type Tabular::cellRow(idx_type cell) const
1824 if (cell >= numberofcells)
1828 return rowofcell[cell];
1832 col_type Tabular::cellColumn(idx_type cell) const
1834 if (cell >= numberofcells)
1838 return columnofcell[cell];
1842 void Tabular::write(ostream & os) const
1846 << write_attribute("version", 3)
1847 << write_attribute("rows", nrows())
1848 << write_attribute("columns", ncols())
1850 // global longtable options
1852 << write_attribute("rotate", rotate)
1853 << write_attribute("booktabs", use_booktabs)
1854 << write_attribute("islongtable", is_long_tabular)
1855 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1856 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1857 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1858 << write_attribute("headTopDL", endhead.topDL)
1859 << write_attribute("headBottomDL", endhead.bottomDL)
1860 << write_attribute("footTopDL", endfoot.topDL)
1861 << write_attribute("footBottomDL", endfoot.bottomDL)
1862 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1863 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1864 << write_attribute("lastFootEmpty", endlastfoot.empty);
1865 // longtables cannot be aligned vertically
1866 if (!is_long_tabular)
1867 os << write_attribute("tabularvalignment", tabular_valignment);
1868 os << write_attribute("tabularwidth", tabular_width);
1869 if (is_long_tabular)
1870 os << write_attribute("longtabularalignment", longtabular_alignment);
1872 for (col_type c = 0; c < ncols(); ++c) {
1874 << write_attribute("alignment", column_info[c].alignment);
1875 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1876 os << write_attribute("decimal_point", column_info[c].decimal_point);
1877 os << write_attribute("change", column_info[c].change, buffer().params())
1878 << write_attribute("valignment", column_info[c].valignment)
1879 << write_attribute("width", column_info[c].p_width.asString())
1880 << write_attribute("varwidth", column_info[c].varwidth)
1881 << write_attribute("special", column_info[c].align_special)
1884 for (row_type r = 0; r < nrows(); ++r) {
1885 static const string def("default");
1887 if (row_info[r].top_space_default)
1888 os << write_attribute("topspace", def);
1890 os << write_attribute("topspace", row_info[r].top_space);
1891 if (row_info[r].bottom_space_default)
1892 os << write_attribute("bottomspace", def);
1894 os << write_attribute("bottomspace", row_info[r].bottom_space);
1895 if (row_info[r].interline_space_default)
1896 os << write_attribute("interlinespace", def);
1898 os << write_attribute("interlinespace", row_info[r].interline_space);
1899 os << write_attribute("change", row_info[r].change, buffer().params())
1900 << write_attribute("endhead", row_info[r].endhead)
1901 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1902 << write_attribute("endfoot", row_info[r].endfoot)
1903 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1904 << write_attribute("newpage", row_info[r].newpage)
1905 << write_attribute("caption", row_info[r].caption)
1907 for (col_type c = 0; c < ncols(); ++c) {
1909 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1910 << write_attribute("multirow", cell_info[r][c].multirow)
1911 << write_attribute("mroffset", cell_info[r][c].mroffset)
1912 << write_attribute("alignment", cell_info[r][c].alignment)
1913 << write_attribute("valignment", cell_info[r][c].valignment)
1914 << write_attribute("topline", cell_info[r][c].top_line)
1915 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1916 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1917 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1918 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1919 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1920 << write_attribute("leftline", cell_info[r][c].left_line)
1921 << write_attribute("rightline", cell_info[r][c].right_line)
1922 << write_attribute("rotate", cell_info[r][c].rotate)
1923 << write_attribute("usebox", cell_info[r][c].usebox)
1924 << write_attribute("width", cell_info[r][c].p_width)
1925 << write_attribute("special", cell_info[r][c].align_special)
1927 os << "\\begin_inset ";
1928 cell_info[r][c].inset->write(os);
1929 os << "\n\\end_inset\n"
1931 // FIXME This can be removed again once the mystery
1932 // crash has been resolved.
1937 os << "</lyxtabular>\n";
1941 void Tabular::read(Lexer & lex)
1944 istream & is = lex.getStream();
1946 l_getline(is, line);
1947 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1948 LASSERT(false, return);
1952 if (!getTokenValue(line, "version", version))
1954 LATTEST(version >= 2);
1957 if (!getTokenValue(line, "rows", rows_arg))
1960 if (!getTokenValue(line, "columns", columns_arg))
1962 init(buffer_, rows_arg, columns_arg);
1963 l_getline(is, line);
1964 if (!prefixIs(line, "<features")) {
1965 lyxerr << "Wrong tabular format (expected <features ...> got"
1966 << line << ')' << endl;
1969 getTokenValue(line, "rotate", rotate);
1970 getTokenValue(line, "booktabs", use_booktabs);
1971 getTokenValue(line, "islongtable", is_long_tabular);
1972 getTokenValue(line, "tabularvalignment", tabular_valignment);
1973 getTokenValue(line, "tabularwidth", tabular_width);
1974 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1975 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1976 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1977 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1978 getTokenValue(line, "headTopDL", endhead.topDL);
1979 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1980 getTokenValue(line, "footTopDL", endfoot.topDL);
1981 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1982 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1983 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1984 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1986 for (col_type c = 0; c < ncols(); ++c) {
1988 if (!prefixIs(line,"<column")) {
1989 lyxerr << "Wrong tabular format (expected <column ...> got"
1990 << line << ')' << endl;
1993 getTokenValue(line, "alignment", column_info[c].alignment);
1994 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1995 getTokenValue(line, "valignment", column_info[c].valignment);
1996 getTokenValue(line, "width", column_info[c].p_width);
1997 getTokenValue(line, "special", column_info[c].align_special);
1998 getTokenValue(line, "varwidth", column_info[c].varwidth);
1999 getTokenValue(line, "change", column_info[c].change, buffer().params());
2002 for (row_type i = 0; i < nrows(); ++i) {
2003 l_getline(is, line);
2004 if (!prefixIs(line, "<row")) {
2005 lyxerr << "Wrong tabular format (expected <row ...> got"
2006 << line << ')' << endl;
2009 getTokenValue(line, "topspace", row_info[i].top_space,
2010 row_info[i].top_space_default);
2011 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
2012 row_info[i].bottom_space_default);
2013 getTokenValue(line, "interlinespace", row_info[i].interline_space,
2014 row_info[i].interline_space_default);
2015 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
2016 getTokenValue(line, "endhead", row_info[i].endhead);
2017 getTokenValue(line, "endfoot", row_info[i].endfoot);
2018 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
2019 getTokenValue(line, "newpage", row_info[i].newpage);
2020 getTokenValue(line, "caption", row_info[i].caption);
2021 getTokenValue(line, "change", row_info[i].change, buffer().params());
2022 for (col_type j = 0; j < ncols(); ++j) {
2023 l_getline(is, line);
2024 if (!prefixIs(line, "<cell")) {
2025 lyxerr << "Wrong tabular format (expected <cell ...> got"
2026 << line << ')' << endl;
2029 cell_info[i][j].inset->toggleCaptionRow(is_long_tabular && row_info[i].caption);
2030 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
2031 getTokenValue(line, "multirow", cell_info[i][j].multirow);
2032 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
2033 getTokenValue(line, "alignment", cell_info[i][j].alignment);
2034 getTokenValue(line, "valignment", cell_info[i][j].valignment);
2035 getTokenValue(line, "topline", cell_info[i][j].top_line);
2036 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
2037 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
2038 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
2039 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
2040 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
2041 getTokenValue(line, "leftline", cell_info[i][j].left_line);
2042 getTokenValue(line, "rightline", cell_info[i][j].right_line);
2043 getTokenValue(line, "rotate", cell_info[i][j].rotate);
2044 getTokenValue(line, "usebox", cell_info[i][j].usebox);
2045 getTokenValue(line, "width", cell_info[i][j].p_width);
2047 getTokenValue(line, "special", cell_info[i][j].align_special);
2048 l_getline(is, line);
2049 if (prefixIs(line, "\\begin_inset")) {
2050 cell_info[i][j].inset->setBuffer(*buffer_);
2051 cell_info[i][j].inset->read(lex);
2052 l_getline(is, line);
2054 if (!prefixIs(line, "</cell>")) {
2055 lyxerr << "Wrong tabular format (expected </cell> got"
2056 << line << ')' << endl;
2060 l_getline(is, line);
2061 if (!prefixIs(line, "</row>")) {
2062 lyxerr << "Wrong tabular format (expected </row> got"
2063 << line << ')' << endl;
2067 while (!prefixIs(line, "</lyxtabular>")) {
2068 l_getline(is, line);
2074 bool Tabular::isMultiColumn(idx_type cell) const
2076 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2077 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
2081 bool Tabular::hasMultiColumn(col_type c) const
2083 for (row_type r = 0; r < nrows(); ++r) {
2084 if (isMultiColumn(cellIndex(r, c)))
2091 bool Tabular::hasVarwidthColumn() const
2093 for (col_type c = 0; c < ncols(); ++c) {
2094 if (column_info[c].varwidth)
2101 bool Tabular::isVTypeColumn(col_type c) const
2103 for (row_type r = 0; r < nrows(); ++r) {
2104 idx_type idx = cellIndex(r, c);
2105 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH
2106 && getAlignment(idx) == LYX_ALIGN_LEFT
2107 && getVAlignment(idx) == LYX_VALIGN_TOP)
2114 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
2116 return cell_info[cellRow(cell)][cellColumn(cell)];
2120 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2122 return cell_info[cellRow(cell)][cellColumn(cell)];
2126 idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2127 bool const right_border)
2129 idx_type const col = cellColumn(cell);
2130 idx_type const row = cellRow(cell);
2131 for (idx_type i = 0; i < number; ++i)
2132 unsetMultiRow(cellIndex(row, col + i));
2134 // unsetting of multirow may have invalidated cell index
2135 cell = cellIndex(row, col);
2136 CellData & cs = cellInfo(cell);
2137 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2138 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2139 cs.alignment = column_info[col].alignment;
2140 setRightLine(cell, right_border);
2141 // non-fixed width multicolumns cannot have multiple paragraphs
2142 if (getPWidth(cell).zero()) {
2143 toggleFixedWidth(cur, cellInset(cell).get(),
2144 !getPWidth(cell).zero(), isMultiRow(cell));
2145 // cur can become invalid after paragraphs were merged
2149 idx_type lastcell = cellIndex(row, col + number - 1);
2150 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2151 CellData & cs1 = cellInfo(cell + i);
2152 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2153 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2161 bool Tabular::isMultiRow(idx_type cell) const
2163 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2164 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2167 bool Tabular::hasMultiRow(row_type r) const
2169 for (col_type c = 0; c < ncols(); ++c) {
2170 if (isMultiRow(cellIndex(r, c)))
2176 idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2177 bool const bottom_border,
2178 LyXAlignment const halign)
2180 idx_type const col = cellColumn(cell);
2181 idx_type const row = cellRow(cell);
2182 for (idx_type i = 0; i < number; ++i)
2183 unsetMultiColumn(cellIndex(row + i, col));
2185 // unsetting of multirow may have invalidated cell index
2186 cell = cellIndex(row, col);
2187 CellData & cs = cellInfo(cell);
2188 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2189 cs.valignment = LYX_VALIGN_MIDDLE;
2190 // the horizontal alignment of multirow cells can only
2191 // be changed for the whole table row,
2192 // support changing this only for the multirow cell can be done via
2194 if (getPWidth(cell).zero())
2195 cs.alignment = halign;
2197 cs.alignment = LYX_ALIGN_LEFT;
2199 // Multirows cannot have multiple paragraphs
2200 if (getPWidth(cell).zero()) {
2201 toggleFixedWidth(cur, cellInset(cell).get(),
2202 !getPWidth(cell).zero(),
2204 // cur can become invalid after paragraphs were merged
2208 // set the bottom line of the last selected cell
2209 setBottomLine(cell, bottom_border);
2211 for (idx_type i = 1; i < number; ++i) {
2212 CellData & cs1 = cell_info[row + i][col];
2213 cs1.multirow = CELL_PART_OF_MULTIROW;
2214 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2222 idx_type Tabular::columnSpan(idx_type cell) const
2224 row_type const row = cellRow(cell);
2225 col_type const col = cellColumn(cell);
2227 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2234 idx_type Tabular::rowSpan(idx_type cell) const
2236 col_type const column = cellColumn(cell);
2237 col_type row = cellRow(cell) + 1;
2238 while (row < nrows() && isPartOfMultiRow(row, column))
2241 return row - cellRow(cell);
2245 void Tabular::unsetMultiColumn(idx_type cell)
2247 if (!isMultiColumn(cell))
2250 row_type const row = cellRow(cell);
2251 col_type const col = cellColumn(cell);
2252 row_type const span = columnSpan(cell);
2253 for (col_type c = 0; c < span; ++c) {
2254 // in the table dialog the lines are set in every case
2255 // when unsetting a multicolumn this leads to an additional right
2256 // line for every cell that was part of the former multicolumn cell,
2257 // except if the cell is in the last column
2258 // therefore remove this line
2259 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2260 && (col + c) < (col + span - 1))
2261 cell_info[row][col + c].right_line = false;
2262 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2268 void Tabular::unsetMultiRow(idx_type cell)
2270 if (!isMultiRow(cell))
2273 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2274 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2275 row_type const row = cellRow(cell);
2276 col_type const col = cellColumn(cell);
2277 row_type const span = rowSpan(cell);
2278 for (row_type r = 0; r < span; ++r)
2279 cell_info[row + r][col].multirow = CELL_NORMAL;
2284 void Tabular::setRotateCell(idx_type cell, int value)
2286 cellInfo(cell).rotate = value;
2290 int Tabular::getRotateCell(idx_type cell) const
2292 return cellInfo(cell).rotate;
2296 bool Tabular::needRotating() const
2298 if (rotate && !is_long_tabular)
2300 for (row_type r = 0; r < nrows(); ++r)
2301 for (col_type c = 0; c < ncols(); ++c)
2302 if (cell_info[r][c].rotate != 0)
2308 bool Tabular::isLastCell(idx_type cell) const
2310 return cell + 1 >= numberofcells;
2314 idx_type Tabular::cellAbove(idx_type cell) const
2316 if (cellRow(cell) == 0)
2319 col_type const col = cellColumn(cell);
2320 row_type r = cellRow(cell) - 1;
2321 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2324 return cell_info[r][col].cellno;
2328 idx_type Tabular::cellBelow(idx_type cell) const
2330 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2331 if (nextrow < nrows())
2332 return cell_info[nextrow][cellColumn(cell)].cellno;
2337 idx_type Tabular::cellIndex(row_type row, col_type column) const
2339 LASSERT(column != npos && column < ncols(), column = 0);
2340 LASSERT(row != npos && row < nrows(), row = 0);
2341 return cell_info[row][column].cellno;
2345 void Tabular::setUsebox(idx_type cell, BoxType type)
2347 cellInfo(cell).usebox = type;
2351 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2353 if (getRotateCell(cell) == 0
2354 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2355 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2357 if (cellInfo(cell).usebox > 1)
2358 return cellInfo(cell).usebox;
2359 return useBox(cell);
2364 // This are functions used for the longtable support
2366 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2372 row_info[row].endfirsthead = flag;
2376 row_info[row].endhead = flag;
2381 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2384 hd.set = haveLTHead();
2385 return row_info[row].endhead;
2389 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2392 hd.set = haveLTFirstHead();
2393 return row_info[row].endfirsthead;
2397 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2403 row_info[row].endlastfoot = flag;
2407 row_info[row].endfoot = flag;
2412 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2415 fd.set = haveLTFoot();
2416 return row_info[row].endfoot;
2420 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2423 fd.set = haveLTLastFoot();
2424 return row_info[row].endlastfoot;
2428 void Tabular::setLTNewPage(row_type row, bool what)
2430 row_info[row].newpage = what;
2434 bool Tabular::getLTNewPage(row_type row) const
2436 return row_info[row].newpage;
2440 bool Tabular::haveLTHead(bool withcaptions) const
2442 if (!is_long_tabular)
2444 for (row_type i = 0; i < nrows(); ++i)
2445 if (row_info[i].endhead &&
2446 (withcaptions || !row_info[i].caption))
2452 bool Tabular::haveLTFirstHead(bool withcaptions) const
2454 if (!is_long_tabular || endfirsthead.empty)
2456 for (row_type r = 0; r < nrows(); ++r)
2457 if (row_info[r].endfirsthead &&
2458 (withcaptions || !row_info[r].caption))
2464 bool Tabular::haveLTFoot(bool withcaptions) const
2466 if (!is_long_tabular)
2468 for (row_type r = 0; r < nrows(); ++r)
2469 if (row_info[r].endfoot &&
2470 (withcaptions || !row_info[r].caption))
2476 bool Tabular::haveLTLastFoot(bool withcaptions) const
2478 if (!is_long_tabular || endlastfoot.empty)
2480 for (row_type r = 0; r < nrows(); ++r)
2481 if (row_info[r].endlastfoot &&
2482 (withcaptions || !row_info[r].caption))
2488 idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2490 idx_type i = getFirstCellInRow(row);
2492 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2493 setTopLine(i, false);
2494 setBottomLine(i, false);
2495 setLeftLine(i, false);
2496 setRightLine(i, false);
2497 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2498 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2499 setLTHead(row, true, endfirsthead, true);
2500 row_info[row].endfirsthead = true;
2503 unsetMultiColumn(i);
2504 // When unsetting a caption row, also all existing
2505 // captions in this row must be dissolved.
2507 row_info[row].caption = what;
2512 bool Tabular::ltCaption(row_type row) const
2514 return row_info[row].caption;
2518 bool Tabular::haveLTCaption(CaptionType captiontype) const
2520 if (!is_long_tabular)
2522 for (row_type r = 0; r < nrows(); ++r) {
2523 if (row_info[r].caption) {
2524 switch (captiontype) {
2525 case CAPTION_FIRSTHEAD:
2526 if (row_info[r].endfirsthead)
2530 if (row_info[r].endhead)
2534 if (row_info[r].endfoot)
2537 case CAPTION_LASTFOOT:
2538 if (row_info[r].endlastfoot)
2550 // end longtable support functions
2552 void Tabular::setRowAscent(row_type row, int height)
2554 if (row >= nrows() || row_info[row].ascent == height)
2556 row_info[row].ascent = height;
2560 void Tabular::setRowDescent(row_type row, int height)
2562 if (row >= nrows() || row_info[row].descent == height)
2564 row_info[row].descent = height;
2568 int Tabular::rowAscent(row_type row) const
2570 LASSERT(row < nrows(), row = 0);
2571 return row_info[row].ascent;
2575 int Tabular::rowDescent(row_type row) const
2577 LASSERT(row < nrows(), row = 0);
2578 return row_info[row].descent;
2582 int Tabular::height() const
2585 for (row_type row = 0; row < nrows(); ++row)
2586 height += rowAscent(row) + rowDescent(row) +
2592 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2594 LASSERT(row < nrows(), return false);
2595 LASSERT(column < ncols(), return false);
2596 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2600 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2602 LASSERT(row < nrows(), return false);
2603 LASSERT(column < ncols(), return false);
2604 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2608 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> const & columns,
2609 list<col_type> const & logical_columns) const
2611 // we only output complete row lines and the 1st row here, the rest
2612 // is done in Tabular::TeXBottomHLine(...)
2614 // get for each column the topline (if any)
2615 map<col_type, bool> topline, topltrims, toprtrims;
2617 bool have_trims = false;
2618 for (auto const & c : columns) {
2619 topline[c] = topLine(cellIndex(row, c));
2620 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2621 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2622 // If cell is part of a multirow and not the first cell of the
2623 // multirow, no line must be drawn.
2625 if (isMultiRow(cellIndex(row, c))
2626 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2628 topltrims[c] = false;
2629 toprtrims[c] = false;
2631 // copy trimming to multicolumn parts
2632 if (isPartOfMultiColumn(row, c)) {
2633 topltrims[c] = topltrims[c-1];
2634 toprtrims[c] = toprtrims[c-1];
2636 if (topline.find(c) != topline.end() && topline.find(c)->second)
2638 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2639 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2643 // do nothing if empty first row, or incomplete row line after
2644 row_type first = getFirstRow(!buffer().params().output_changes);
2645 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2648 // Is this the actual first row (excluding longtable caption row)?
2649 bool const realfirstrow = (row == first
2650 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2652 // only output complete row lines and the 1st row's clines
2653 if (nset == columns.size() && !have_trims) {
2655 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2659 } else if (realfirstrow || have_trims) {
2660 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2662 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2663 std::list<col_type>::const_iterator it2 = columns.begin();
2664 // We need to iterate over the logical columns here, but take care for
2666 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2671 if (topline.find(c)->second) {
2672 col_type offset = 0;
2673 for (col_type j = 0 ; j < c; ++j)
2674 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2676 // If the two iterators differ, we are in bidi with swapped columns
2677 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2678 while (isPartOfMultiColumn(row, c))
2681 if (topltrims.find(c) != topltrims.end()
2682 && topltrims.find(c)->second)
2684 col_type cstart = c;
2685 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2686 if (isMultiColumn(cellIndex(row, c))
2687 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2689 if (c > cstart && topltrims.find(c) != topltrims.end()
2690 && topltrims.find(c)->second) {
2691 if (!isPartOfMultiColumn(row, c))
2694 } else if (toprtrims.find(c) != toprtrims.end()
2695 && toprtrims.find(c)->second)
2699 for (col_type j = cstart ; j <= c ; ++j)
2700 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2702 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2703 if (toprtrims.find(c) != toprtrims.end()
2704 && toprtrims.find(c)->second)
2709 os << "(" << trim << ")";
2710 if (firstcol > lastcol)
2711 // This can happen with bidi (swapped columns)
2712 os << "{" << lastcol << '-' << firstcol << "}";
2714 os << "{" << firstcol << '-' << lastcol << "}";
2715 if (c == columns.size() - 1)
2725 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> const & columns,
2726 list<col_type> const & logical_columns) const
2728 // we output bottomlines of row r and the toplines of row r+1
2729 // if the latter do not span the whole tabular
2731 // get the bottomlines of row r, and toplines in next row
2732 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2733 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2734 bool nextrowset = true;
2735 for (auto const & c : columns) {
2736 idx_type const idx = cellIndex(row, c);
2737 bottomline[c] = bottomLine(cellIndex(row, c));
2738 bottomltrims[c] = bottomLineTrim(idx).first;
2739 bottomrtrims[c] = bottomLineTrim(idx).second;
2740 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2741 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2742 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2743 // If cell is part of a multirow and not the last cell of the
2744 // multirow, no line must be drawn.
2746 if (isMultiRow(cellIndex(row, c))
2747 && isMultiRow(cellIndex(row + 1, c))
2748 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2749 bottomline[c] = false;
2751 bottomltrims[c] = false;
2752 bottomrtrims[c] = false;
2753 topltrims[c] = false;
2754 toprtrims[c] = false;
2756 // copy trimming in multicolumn parts
2757 if (isPartOfMultiColumn(row, c)) {
2758 topltrims[c] = topltrims[c-1];
2759 toprtrims[c] = toprtrims[c-1];
2760 bottomltrims[c] = bottomltrims[c-1];
2761 bottomrtrims[c] = bottomrtrims[c-1];
2764 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2767 // combine this row's bottom lines and next row's toplines if necessary
2769 bool have_trims = false;
2770 for (auto const & c : columns) {
2772 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2773 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2774 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2775 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2776 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2777 if (bottomline.find(c)->second)
2779 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2780 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2784 // do nothing if empty, OR incomplete row line with a topline in next row
2785 if (nset == 0 || (nextrowset && nset != columns.size()))
2788 if (nset == columns.size() && !have_trims) {
2790 os << (lastrow ? "\\bottomrule" : "\\midrule");
2794 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2796 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2797 std::list<col_type>::const_iterator it2 = columns.begin();
2798 // We need to iterate over the logical columns here, but take care for
2800 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2805 if (bottomline.find(c)->second) {
2806 col_type offset = 0;
2807 for (col_type j = 0 ; j < c; ++j)
2808 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2810 // If the two iterators differ, we are in bidi with swapped columns
2811 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2812 while (isPartOfMultiColumn(row, c))
2815 if (bottomltrims.find(c) != bottomltrims.end()
2816 && bottomltrims.find(c)->second)
2818 col_type cstart = c;
2819 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2820 if (isMultiColumn(cellIndex(row, c))
2822 && isPartOfMultiColumn(row, c + 1))
2825 && bottomltrims.find(c) != bottomltrims.end()
2826 && bottomltrims.find(c)->second) {
2827 if (!isPartOfMultiColumn(row, c))
2830 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2831 && bottomrtrims.find(c)->second)
2835 for (col_type j = cstart ; j <= c ; ++j)
2836 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2838 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2839 if (bottomrtrims.find(c) != bottomrtrims.end()
2840 && bottomrtrims.find(c)->second)
2845 os << "(" << trim << ")";
2846 if (firstcol > lastcol)
2847 // This can happen with bidi (swapped columns)
2848 os << "{" << lastcol << '-' << firstcol << "}";
2850 os << "{" << firstcol << '-' << lastcol << "}";
2851 if (c == columns.size() - 1)
2861 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2862 bool & ismulticol, bool & ismultirow,
2863 bool const bidi) const
2865 row_type const r = cellRow(cell);
2866 if (is_long_tabular && row_info[r].caption)
2869 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2870 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2871 // figure out how to set the lines
2872 // we always set double lines to the right of the cell
2873 // or left in bidi RTL, respectively.
2874 col_type const c = cellColumn(cell);
2875 col_type const nextcol = c + columnSpan(cell);
2876 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2877 bool colright = columnRightLine(c);
2878 bool colleft = columnLeftLine(c);
2879 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2880 bool nextcellleft = nextcol < ncols()
2881 && leftLine(cellIndex(r, nextcol));
2882 bool coldouble = colright && nextcolleft;
2883 bool celldouble = rightLine(cell) && nextcellleft;
2885 ismulticol = (isMultiColumn(cell)
2886 || (c == 0 && colleft != leftLine(cell))
2887 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2888 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2889 || (coldouble != celldouble))
2892 // we center in multicol when no decimal point
2894 docstring const align_d = column_info[c].decimal_point;
2895 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2896 bool const nosep = !dit;
2897 ismulticol |= nosep;
2898 celldouble &= nosep;
2901 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2902 int latexcolspan = columnSpan(cell);
2903 for (col_type col = c; col < c + columnSpan(cell); ++col)
2904 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2908 os << "\\multicolumn{" << latexcolspan << "}{";
2909 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2910 || (!bidi && c == 0 && leftLine(cell))))
2912 if (bidi && celldouble)
2913 // add extra vertical line if we want a double one
2915 if (!cellInfo(cell).align_special.empty()) {
2916 os << cellInfo(cell).align_special;
2918 if (!getPWidth(cell).zero()) {
2920 case LYX_ALIGN_LEFT:
2921 os << ">{\\raggedright}";
2923 case LYX_ALIGN_RIGHT:
2924 os << ">{\\raggedleft}";
2926 case LYX_ALIGN_CENTER:
2927 os << ">{\\centering}";
2933 case LYX_VALIGN_TOP:
2936 case LYX_VALIGN_MIDDLE:
2939 case LYX_VALIGN_BOTTOM:
2944 << from_ascii(getPWidth(cell).asLatexString())
2947 if ((getRotateCell(cell) == 0 && useBox(cell) == BOX_VARWIDTH
2948 && align == LYX_ALIGN_LEFT))
2949 os << "V{\\linewidth}";
2952 case LYX_ALIGN_LEFT:
2955 case LYX_ALIGN_RIGHT:
2963 } // end if else !getPWidth
2964 } // end if else !cellinfo_of_cell
2965 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2967 if (!bidi && celldouble)
2968 // add extra vertical line if we want a double one
2971 } // end if ismulticol
2973 // we only need code for the first multirow cell
2974 ismultirow = isMultiRow(cell);
2976 os << "\\multirow{" << rowSpan(cell) << "}{";
2977 if (!getPWidth(cell).zero())
2978 os << from_ascii(getPWidth(cell).asLatexString());
2980 if (column_info[c].varwidth)
2981 // this inherits varwidth size
2984 // we need to set a default value
2988 if (!getMROffset(cell).zero())
2989 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2991 } // end if ismultirow
2993 if (getRotateCell(cell) != 0)
2994 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2996 if (getUsebox(cell) == BOX_PARBOX) {
2999 case LYX_VALIGN_TOP:
3002 case LYX_VALIGN_MIDDLE:
3005 case LYX_VALIGN_BOTTOM:
3009 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
3011 } else if (getUsebox(cell) == BOX_MINIPAGE) {
3012 os << "\\begin{minipage}[";
3014 case LYX_VALIGN_TOP:
3017 case LYX_VALIGN_MIDDLE:
3020 case LYX_VALIGN_BOTTOM:
3024 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
3026 } else if (getUsebox(cell) == BOX_VARWIDTH
3027 && (getRotateCell(cell) != 0 || align != LYX_ALIGN_LEFT
3028 || valign != LYX_VALIGN_TOP || hasNewlines(cell))) {
3029 os << "\\begin{cellvarwidth}[";
3031 case LYX_VALIGN_TOP:
3034 case LYX_VALIGN_MIDDLE:
3037 case LYX_VALIGN_BOTTOM:
3043 case LYX_ALIGN_RIGHT:
3044 os << "\\raggedleft\n";
3046 case LYX_ALIGN_CENTER:
3047 os << "\\centering\n";
3049 case LYX_ALIGN_LEFT:
3050 //os << "\\narrowragged\n";
3052 case LYX_ALIGN_BLOCK:
3053 case LYX_ALIGN_DECIMAL:
3054 case LYX_ALIGN_NONE:
3055 case LYX_ALIGN_LAYOUT:
3056 case LYX_ALIGN_SPECIAL:
3063 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
3064 bool ismulticol, bool ismultirow) const
3066 row_type const r = cellRow(cell);
3067 if (is_long_tabular && row_info[r].caption)
3071 if (getUsebox(cell) == BOX_PARBOX)
3073 else if (getUsebox(cell) == BOX_MINIPAGE)
3074 os << breakln << "\\end{minipage}";
3075 else if (getUsebox(cell) == BOX_VARWIDTH
3076 && (getRotateCell(cell) != 0 || getAlignment(cell) != LYX_ALIGN_LEFT
3077 || getVAlignment(cell) != LYX_VALIGN_TOP || hasNewlines(cell)))
3078 os << breakln << "\\end{cellvarwidth}";
3079 if (getRotateCell(cell) != 0)
3080 os << breakln << "\\end{turn}";
3088 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
3089 OutputParams const & runparams,
3090 list<col_type> const & columns,
3091 list<col_type> const & logical_columns) const
3093 if (!is_long_tabular)
3097 // output caption which is in no header or footer
3098 if (haveLTCaption()) {
3099 for (row_type r = 0; r < nrows(); ++r) {
3100 if (row_info[r].caption &&
3101 !row_info[r].endfirsthead && !row_info[r].endhead &&
3102 !row_info[r].endfoot && !row_info[r].endlastfoot)
3103 TeXRow(os, r, runparams, columns, logical_columns);
3106 // output first header info
3107 if (haveLTFirstHead()) {
3108 if (endfirsthead.topDL)
3110 for (row_type r = 0; r < nrows(); ++r) {
3111 if (row_info[r].endfirsthead)
3112 TeXRow(os, r, runparams, columns, logical_columns);
3114 if (endfirsthead.bottomDL)
3116 os << "\\endfirsthead\n";
3118 // output header info
3120 if (endfirsthead.empty && !haveLTFirstHead())
3121 os << "\\endfirsthead\n";
3124 for (row_type r = 0; r < nrows(); ++r) {
3125 if (row_info[r].endhead)
3126 TeXRow(os, r, runparams, columns, logical_columns);
3128 if (endhead.bottomDL)
3130 os << "\\endhead\n";
3132 // output footer info
3136 for (row_type r = 0; r < nrows(); ++r) {
3137 if (row_info[r].endfoot)
3138 TeXRow(os, r, runparams, columns, logical_columns);
3140 if (endfoot.bottomDL)
3142 os << "\\endfoot\n";
3143 if (endlastfoot.empty && !haveLTLastFoot())
3144 os << "\\endlastfoot\n";
3146 // output lastfooter info
3147 if (haveLTLastFoot()) {
3148 if (endlastfoot.topDL)
3150 for (row_type r = 0; r < nrows(); ++r) {
3151 if (row_info[r].endlastfoot)
3152 TeXRow(os, r, runparams, columns, logical_columns);
3154 if (endlastfoot.bottomDL)
3156 os << "\\endlastfoot\n";
3161 bool Tabular::isValidRow(row_type row) const
3163 if (!is_long_tabular)
3165 return !row_info[row].endhead && !row_info[row].endfirsthead
3166 && !row_info[row].endfoot && !row_info[row].endlastfoot
3167 && !row_info[row].caption;
3171 void Tabular::TeXRow(otexstream & os, row_type row,
3172 OutputParams const & runparams,
3173 list<col_type> const & columns, list<col_type> const & logical_columns) const
3175 //output the top line
3176 TeXTopHLine(os, row, columns, logical_columns);
3178 if (row_info[row].top_space_default) {
3180 os << "\\addlinespace\n";
3182 os << "\\noalign{\\vskip\\doublerulesep}\n";
3183 } else if(!row_info[row].top_space.zero()) {
3185 os << "\\addlinespace["
3186 << from_ascii(row_info[row].top_space.asLatexString())
3189 os << "\\noalign{\\vskip"
3190 << from_ascii(row_info[row].top_space.asLatexString())
3194 bool ismulticol = false;
3195 bool ismultirow = false;
3197 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3198 // Luabibdi (used by LuaTeX) behaves like classic
3199 bool const bidi_rtl =
3200 runparams.local_font
3201 && runparams.local_font->isRightToLeft()
3202 && buffer().params().useBidiPackage(runparams);
3203 bool const ct = !buffer().params().output_changes;
3205 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3207 for (auto const & c : columns) {
3208 if (isPartOfMultiColumn(row, c))
3211 idx_type cell = cellIndex(row, c);
3213 if (isPartOfMultiRow(row, c)
3214 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3215 if (cell != lastcell)
3220 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3221 InsetTableCell const * inset = cellInset(cell);
3223 Paragraph const & par = inset->paragraphs().front();
3225 os.texrow().forceStart(par.id(), 0);
3227 bool rtl = par.isRTL(buffer().params())
3229 && getPWidth(cell).zero()
3230 && !runparams.isFullUnicode();
3234 par.getParLanguage(buffer().params())->lang();
3235 if (lang == "farsi")
3237 else if (lang == "arabic_arabi")
3239 // currently, remaining RTL languages are
3240 // arabic_arabtex and hebrew
3244 // pass to the OutputParams that we are in a cell and
3245 // which alignment we have set.
3246 // InsetNewline needs this context information.
3247 OutputParams newrp(runparams);
3248 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3249 ? OutputParams::PLAIN
3250 : OutputParams::ALIGNED;
3252 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3253 // copy cell and split in 2
3254 InsetTableCell head = InsetTableCell(*cellInset(cell));
3255 head.setBuffer(const_cast<Buffer &>(buffer()));
3256 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3258 dit.push_back(CursorSlice(head));
3259 head.setMacrocontextPositionRecursive(dit);
3260 bool hassep = false;
3261 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3263 tail.setBuffer(head.buffer());
3265 dit.push_back(CursorSlice(tail));
3266 tail.setMacrocontextPositionRecursive(dit);
3270 tail.latex(os, newrp);
3273 head.latex(os, newrp);
3275 head.latex(os, newrp);
3278 tail.latex(os, newrp);
3281 } else if (ltCaption(row)) {
3282 // Inside longtable caption rows, we must only output the caption inset
3283 // with its content and omit anything outside of that (see #10791)
3284 InsetTableCell & tc_inset = *const_cast<InsetTableCell *>(inset);
3285 for (Inset const & it : tc_inset) {
3286 if (it.lyxCode() != CAPTION_CODE)
3288 it.latex(os, runparams);
3291 } else if (!isPartOfMultiRow(row, c)) {
3292 if (!runparams.nice)
3293 os.texrow().start(par.id(), 0);
3294 if (isMultiRow(cell) && !LaTeXFeatures::isAvailableAtLeastFrom("multirow", 2021, 1, 29))
3295 newrp.isNonLong = true;
3296 inset->latex(os, newrp);
3299 runparams.encoding = newrp.encoding;
3303 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3304 if (cell != lastcell) { // not last cell in row
3311 os << "\\tabularnewline";
3312 if (row_info[row].bottom_space_default) {
3314 os << "\\addlinespace";
3316 os << "[\\doublerulesep]";
3317 } else if (!row_info[row].bottom_space.zero()) {
3319 os << "\\addlinespace";
3321 << from_ascii(row_info[row].bottom_space.asLatexString())
3326 //output the bottom line
3327 TeXBottomHLine(os, row, columns, logical_columns);
3329 if (row_info[row].interline_space_default) {
3331 os << "\\addlinespace\n";
3333 os << "\\noalign{\\vskip\\doublerulesep}\n";
3334 } else if (!row_info[row].interline_space.zero()) {
3336 os << "\\addlinespace["
3337 << from_ascii(row_info[row].interline_space.asLatexString())
3340 os << "\\noalign{\\vskip"
3341 << from_ascii(row_info[row].interline_space.asLatexString())
3347 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3349 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3350 && !hasVarwidthColumn();
3351 bool const is_xltabular = is_long_tabular
3352 && (hasVarwidthColumn() || !tabular_width.zero());
3353 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3355 //+---------------------------------------------------------------------
3356 //+ first the opening preamble +
3357 //+---------------------------------------------------------------------
3360 if (!TexRow::isNone(pos))
3361 os.texrow().start(pos);
3364 if (is_long_tabular)
3365 os << "\\begin{landscape}\n";
3367 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3370 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3371 // order for RTL (#9686). Thus we use this list.
3372 bool const bidi_rtl =
3373 runparams.local_font
3374 && runparams.local_font->isRightToLeft()
3375 && buffer().params().useBidiPackage(runparams);
3376 list<col_type> columns;
3377 list<col_type> logical_columns;
3378 for (col_type cl = 0; cl < ncols(); ++cl) {
3379 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3382 columns.push_front(cl);
3384 columns.push_back(cl);
3385 // for some calculations, we need the logical (non-swapped)
3386 // columns also in bidi.
3387 logical_columns.push_back(cl);
3390 // If we use \cline or \cmidrule, we need to locally de-activate
3391 // the - character when using languages that activate it (e.g., Czech, Slovak).
3392 bool deactivate_chars = false;
3393 if ((runparams.use_babel || runparams.use_polyglossia)
3394 && contains(runparams.active_chars, '-')) {
3395 bool have_clines = false;
3396 // Check if we use \cline or \cmidrule
3397 for (row_type row = 0; row < nrows(); ++row) {
3398 col_type bset = 0, tset = 0;
3399 for (auto const & c : columns) {
3400 idx_type const idx = cellIndex(row, c);
3401 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3402 || topLineTrim(idx).first || topLineTrim(idx).second) {
3406 if (bottomLine(cellIndex(row, c)))
3408 if (topLine(cellIndex(row, c)))
3411 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3417 deactivate_chars = true;
3418 os << "\\begingroup\n"
3419 << "\\catcode`\\-=12\n";
3423 if (is_long_tabular) {
3425 os << "\\begin{xltabular}";
3427 os << "\\begin{longtable}";
3428 switch (longtabular_alignment) {
3429 case LYX_LONGTABULAR_ALIGN_LEFT:
3432 case LYX_LONGTABULAR_ALIGN_CENTER:
3435 case LYX_LONGTABULAR_ALIGN_RIGHT:
3440 if (tabular_width.zero())
3441 os << "{" << from_ascii("\\columnwidth") << "}";
3443 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3446 if (is_tabular_star)
3447 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3448 else if (hasVarwidthColumn()) {
3449 os << "\\begin{tabularx}{";
3450 if (tabular_width.zero())
3451 os << from_ascii("\\columnwidth") << "}";
3453 os << from_ascii(tabular_width.asLatexString()) << "}";
3455 os << "\\begin{tabular}";
3456 switch (tabular_valignment) {
3457 case LYX_VALIGN_TOP:
3460 case LYX_VALIGN_MIDDLE:
3462 case LYX_VALIGN_BOTTOM:
3470 if (is_tabular_star)
3471 os << "@{\\extracolsep{\\fill}}";
3473 for (auto const & c : columns) {
3474 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3476 if (!column_info[c].align_special.empty()) {
3477 os << column_info[c].align_special;
3479 if (!column_info[c].p_width.zero()) {
3480 bool decimal = false;
3481 switch (column_info[c].alignment) {
3482 case LYX_ALIGN_LEFT:
3483 os << ">{\\raggedright}";
3485 case LYX_ALIGN_RIGHT:
3486 os << ">{\\raggedleft}";
3488 case LYX_ALIGN_CENTER:
3489 os << ">{\\centering}";
3491 case LYX_ALIGN_NONE:
3492 case LYX_ALIGN_BLOCK:
3493 case LYX_ALIGN_LAYOUT:
3494 case LYX_ALIGN_SPECIAL:
3496 case LYX_ALIGN_DECIMAL: {
3498 os << ">{\\raggedright}";
3500 os << ">{\\raggedleft}";
3507 switch (column_info[c].valignment) {
3508 case LYX_VALIGN_TOP:
3509 // this is the default
3511 case LYX_VALIGN_MIDDLE:
3514 case LYX_VALIGN_BOTTOM:
3520 // Fixed-width cells with alignment at decimal separator
3521 // are output as two cells of half the width with the decimal
3522 // separator as column sep. This effectively puts the content
3523 // centered, which differs from the normal decimal sep alignment
3524 // and is not ideal, but we cannot do better ATM (see #9568).
3525 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3527 docstring const halffixedwith =
3528 from_ascii(Length(column_info[c].p_width.value() / 2,
3529 column_info[c].p_width.unit()).asLatexString());
3533 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3540 << from_ascii(column_info[c].p_width.asLatexString())
3542 } else if (column_info[c].varwidth) {
3543 switch (column_info[c].alignment) {
3544 case LYX_ALIGN_LEFT:
3545 os << ">{\\raggedright\\arraybackslash}";
3547 case LYX_ALIGN_RIGHT:
3548 os << ">{\\raggedleft\\arraybackslash}";
3550 case LYX_ALIGN_CENTER:
3551 os << ">{\\centering\\arraybackslash}";
3553 case LYX_ALIGN_NONE:
3554 case LYX_ALIGN_BLOCK:
3555 case LYX_ALIGN_LAYOUT:
3556 case LYX_ALIGN_SPECIAL:
3557 case LYX_ALIGN_DECIMAL:
3561 } else if (isVTypeColumn(c))
3562 os << "V{\\linewidth}";
3564 switch (column_info[c].alignment) {
3565 case LYX_ALIGN_LEFT:
3568 case LYX_ALIGN_RIGHT:
3571 case LYX_ALIGN_DECIMAL:
3572 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3578 } // end if else !column_info[i].p_width
3579 } // end if else !column_info[i].align_special
3580 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3585 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3587 //+---------------------------------------------------------------------
3588 //+ the single row and columns (cells) +
3589 //+---------------------------------------------------------------------
3591 for (row_type r = 0; r < nrows(); ++r) {
3592 if (!buffer().params().output_changes && row_info[r].change.deleted())
3594 if (isValidRow(r)) {
3595 TeXRow(os, r, runparams, columns, logical_columns);
3596 if (is_long_tabular && row_info[r].newpage)
3597 os << "\\newpage\n";
3601 //+---------------------------------------------------------------------
3602 //+ the closing of the tabular +
3603 //+---------------------------------------------------------------------
3605 if (is_long_tabular) {
3607 os << "\\end{xltabular}";
3609 os << "\\end{longtable}";
3611 if (is_tabular_star)
3612 os << "\\end{tabular*}";
3613 else if (hasVarwidthColumn())
3614 os << "\\end{tabularx}";
3616 os << "\\end{tabular}";
3619 if (deactivate_chars)
3621 os << "\n\\endgroup\n";
3624 if (is_long_tabular)
3625 os << breakln << "\\end{landscape}";
3627 os << breakln << "\\end{turn}";
3630 if (!TexRow::isNone(pos))
3631 os.texrow().start(pos);
3635 std::string Tabular::getVAlignAsXmlAttribute(idx_type cell) const
3637 switch (getVAlignment(cell)) {
3638 case LYX_VALIGN_TOP:
3639 return "valign='top'";
3640 case LYX_VALIGN_BOTTOM:
3641 return "valign='bottom'";
3642 case LYX_VALIGN_MIDDLE:
3643 return "valign='middle'";
3645 // This case only silences a compiler warning, as all the cases are covered above.
3651 std::string Tabular::getVAlignAsCssAttribute(idx_type cell) const
3653 switch (getVAlignment(cell)) {
3654 case LYX_VALIGN_TOP:
3655 return "vertical-align: top";
3656 case LYX_VALIGN_BOTTOM:
3657 return "vertical-align: bottom";
3658 case LYX_VALIGN_MIDDLE:
3659 return "vertical-align: middle";
3661 // This case only silences a compiler warning, as all the cases are covered above.
3667 std::string Tabular::getHAlignAsXmlAttribute(idx_type cell) const
3669 switch (getAlignment(cell)) {
3670 case LYX_ALIGN_LEFT:
3671 return "align='left'";
3672 case LYX_ALIGN_RIGHT:
3673 return "align='right'";
3674 case LYX_ALIGN_BLOCK:
3675 return "align='justify'";
3676 case LYX_ALIGN_DECIMAL: {
3677 Language const *lang = buffer().paragraphs().front().getParLanguage(buffer().params());
3678 return "align='char' char='" + to_utf8(lang->decimalSeparator()) + "'";
3681 return "align='center'";
3686 std::string Tabular::getHAlignAsCssAttribute(idx_type cell) const
3688 switch (getAlignment(cell)) {
3689 case LYX_ALIGN_LEFT:
3690 return "text-align: left";
3691 case LYX_ALIGN_RIGHT:
3692 return "text-align: right";
3693 case LYX_ALIGN_BLOCK:
3694 return "text-align: justify";
3695 case LYX_ALIGN_DECIMAL: {
3696 // In theory, character-level alignment is supported for CSS4, but it's
3698 // https://www.w3.org/TR/css-text-4/#character-alignment
3699 Language const *lang = buffer().paragraphs().front().getParLanguage(buffer().params());
3700 return "text-align: \"" + to_utf8(lang->decimalSeparator()) + "\"";
3703 return "text-align: center";
3708 Tabular::XmlRowWiseBorders Tabular::computeXmlBorders(row_type row) const
3710 Tabular::XmlRowWiseBorders borders;
3712 // Determine whether borders are required.
3713 for (col_type c = 0; c < ncols(); ++c) {
3714 if (row < nrows() - 1) {
3715 if (!bottomLine(cellIndex(row, c))
3716 || !topLine(cellIndex(row + 1, c))) {
3717 borders.completeBorder = false;
3719 if (!bottomLine(cellIndex(row, c))
3720 && !topLine(cellIndex(row + 1, c))) {
3721 borders.completeBorderBelow = false;
3723 } else if (row == nrows() - 1 && !bottomLine(cellIndex(row, c))) {
3724 borders.completeBorderBelow = false;
3727 if ((row > 0 && !bottomLine(cellIndex(row - 1, c)) && !topLine(cellIndex(row, c))) ||
3728 (row == 0 && !topLine(cellIndex(row, c)))) {
3729 borders.completeBorderAbove = false;
3733 // Size of booktabs borders.
3735 if (borders.completeBorderAbove)
3736 borders.borderTopWidth = row == 0 ? 2 : 1.5;
3737 if (borders.completeBorderBelow) {
3738 borders.borderBottomWidth = row == nrows() - 1 ? 2 : 1.5;
3739 borders.borderBottomWidthComplete = 3 * borders.borderBottomWidth;
3747 std::vector<std::string> Tabular::computeCssStylePerCell(row_type row, col_type col, idx_type cell) const
3749 std::vector<std::string> styles;
3752 Length const col_width = column_info[col].p_width;
3753 if (!col_width.zero())
3754 styles.emplace_back("width: " + col_width.asHTMLString());
3756 // Borders and booktabs.
3757 const Tabular::XmlRowWiseBorders borders = computeXmlBorders(row);
3759 if (bottomLine(cell)) {
3760 if (row < nrows() - 1 && borders.completeBorder)
3761 styles.emplace_back("border-bottom: " + to_string(borders.borderBottomWidthComplete) + "px double");
3763 styles.emplace_back("border-bottom: " + to_string(borders.borderBottomWidth) + "px solid");
3765 if (rightLine(cell)) {
3766 if (col < ncols() - 1 && leftLine(cell + 1))
3767 styles.emplace_back("border-right: 3px double");
3769 styles.emplace_back("border-right: 1px solid");
3772 styles.emplace_back("border-left: 1px solid");
3774 styles.emplace_back("border-top: " + to_string(borders.borderTopWidth) + "px solid");
3780 docstring Tabular::xmlRow(XMLStream & xs, const row_type row, OutputParams const & runparams,
3781 const bool header, const XmlOutputFormat output_format, BufferParams::TableOutput docbook_table_output) const
3784 const bool is_xhtml_table = output_format == XmlOutputFormat::XHTML ||
3785 docbook_table_output == BufferParams::TableOutput::HTMLTable;
3786 const bool is_cals_table = output_format == XmlOutputFormat::DOCBOOK &&
3787 docbook_table_output == BufferParams::TableOutput::CALSTable;
3789 std::string const row_tag = is_xhtml_table ? "tr" : "row";
3790 std::string const cell_tag = is_xhtml_table ? (header ? "th" : "td") : "entry";
3791 Tabular::XmlRowWiseBorders const borders = computeXmlBorders(row);
3792 idx_type cell = getFirstCellInRow(row);
3794 std::string row_attr;
3795 bool cals_row_has_rowsep = false; // TODO: is this required? Is it possible that only a/several cells request a row separator, but not the complete row?
3796 // CALS only: all cases where there should be a line *below* this row.
3797 if (is_cals_table && (row_info[row].bottom_space_default || bottomLine(cell))) {
3798 if (borders.completeBorderBelow) {
3799 row_attr = "rowsep='1'";
3800 cals_row_has_rowsep = true;
3804 xs << xml::StartTag(row_tag, row_attr);
3806 for (col_type c = 0; c < ncols(); ++c, ++cell) {
3807 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3810 stringstream attr; // Tag-level attributes, both for HTML and CALS.
3811 stringstream style; // Tag-level CSS, only for HTML tables output in HTML.
3813 if (is_cals_table) {
3814 if (!cals_row_has_rowsep && bottomLine(cell))
3815 attr << "rowsep='1' ";
3818 if (output_format == XmlOutputFormat::XHTML) {
3819 // In HTML5, prefer to use CSS instead of attributes for alignment
3820 // (align and valign).
3821 style << getHAlignAsCssAttribute(cell) << "; "
3822 << getVAlignAsCssAttribute(cell);
3824 // In DocBook, both for HTML and CALS tables, stick to attributes.
3825 attr << getHAlignAsXmlAttribute(cell) << " "
3826 << getVAlignAsXmlAttribute(cell);
3829 if (is_xhtml_table) {
3830 if (isMultiColumn(cell))
3831 attr << " colspan='" << columnSpan(cell) << "'";
3832 else if (isMultiRow(cell))
3833 attr << " rowspan='" << rowSpan(cell) << "'";
3834 } else if (is_cals_table) {
3835 if (isMultiColumn(cell))
3836 attr << " namest='c" << c << " nameend='c" << (c + columnSpan(cell)) << "'";
3837 else if (isMultiRow(cell))
3838 attr << " morerows='" << rowSpan(cell) << "'";
3840 attr << " colname='c" << (c + 1) << "'"; // CALS column numbering starts at 1.
3843 // Final step: prepend the CSS style.
3844 std::string attr_str = attr.str();
3845 if (is_xhtml_table) {
3846 const std::vector<std::string> styles = computeCssStylePerCell(row, c, cell);
3848 std::string attr_str_prefix = "style='" + style.str();
3849 if (!style.str().empty())
3850 attr_str_prefix += "; ";
3851 for (auto it = styles.begin(); it != styles.end(); ++it) {
3852 attr_str_prefix += *it;
3853 if (it != styles.end() - 1)
3854 attr_str_prefix += "; ";
3856 attr_str_prefix += "' ";
3858 attr_str.insert(0, attr_str_prefix);
3861 // Render the cell as either XHTML or DocBook.
3862 xs << xml::StartTag(cell_tag, attr_str, true);
3863 if (output_format == XmlOutputFormat::XHTML) {
3864 ret += cellInset(cell)->xhtml(xs, runparams);
3865 } else if (output_format == XmlOutputFormat::DOCBOOK) {
3866 // DocBook: no return value for this function.
3867 OutputParams rp = runparams;
3868 rp.docbook_in_par = false;
3869 rp.docbook_force_pars = true;
3870 cellInset(cell)->docbook(xs, rp);
3872 xs << xml::EndTag(cell_tag);
3875 xs << xml::EndTag(row_tag);
3882 void Tabular::xmlHeader(XMLStream & xs, OutputParams const & runparams, const XmlOutputFormat output_format) const
3884 // Output the header of the table. For both HTML and CALS, this is surrounded by a thead.
3885 bool const have_first_head = haveLTFirstHead(false);
3886 // if we have a first head, then we are going to ignore the
3887 // headers for the additional pages, since there aren't any
3888 // in HTML or DocBook.
3889 bool const have_head = !have_first_head && haveLTHead(false);
3891 if (have_head || have_first_head) {
3892 xs << xml::StartTag("thead") << xml::CR();
3893 for (row_type r = 0; r < nrows(); ++r) {
3894 if (((have_first_head && row_info[r].endfirsthead) ||
3895 (have_head && row_info[r].endhead)) &&
3896 !row_info[r].caption) {
3897 xmlRow(xs, r, runparams, true, output_format, buffer().params().docbook_table_output);
3900 xs << xml::EndTag("thead");
3906 void Tabular::xmlFooter(XMLStream & xs, OutputParams const & runparams, const XmlOutputFormat output_format) const
3908 // Output the footer of the table. For both HTML and CALS, this is surrounded by a tfoot and output just after
3909 // the header (and before the body).
3910 bool const have_last_foot = haveLTLastFoot(false);
3911 bool const have_foot = !have_last_foot && haveLTFoot(false);
3913 if (have_foot || have_last_foot) {
3914 xs << xml::StartTag("tfoot") << xml::CR();
3915 for (row_type r = 0; r < nrows(); ++r) {
3916 if (((have_last_foot && row_info[r].endlastfoot) ||
3917 (have_foot && row_info[r].endfoot)) &&
3918 !row_info[r].caption) {
3919 xmlRow(xs, r, runparams, false, output_format, buffer().params().docbook_table_output);
3922 xs << xml::EndTag("tfoot");
3928 void Tabular::xmlBody(XMLStream & xs, OutputParams const & runparams, const XmlOutputFormat output_format) const
3930 // Output the main part of the table. The tbody container is mandatory for CALS, but optional for HTML (only if
3931 // there is no header and no footer). It never hurts to have it, though.
3932 xs << xml::StartTag("tbody");
3934 for (row_type r = 0; r < nrows(); ++r)
3936 xmlRow(xs, r, runparams, false, output_format, buffer().params().docbook_table_output);
3937 xs << xml::EndTag("tbody");
3942 void Tabular::docbook(XMLStream & xs, OutputParams const & runparams) const
3944 // Some tables are inline. Likely limitation: cannot output a table within a table; is that really a limitation?
3945 if (!runparams.docbook_in_table) { // Check on the *outer* set of parameters, so that the table can be closed
3946 // properly at the end of this function.
3947 xs << xml::StartTag("informaltable");
3951 // "Formal" tables have a title and use the tag <table>; the distinction with <informaltable> is done outside.
3952 // HTML has the caption first with titles forbidden, and CALS has a title first.
3953 if (haveLTCaption()) {
3954 std::string caption_tag = ((buffer().params().docbook_table_output) == BufferParams::HTMLTable) ? "caption" : "title";
3956 xs << xml::StartTag(caption_tag);
3957 for (row_type r = 0; r < nrows(); ++r)
3958 if (row_info[r].caption)
3959 xmlRow(xs, r, runparams, false, XmlOutputFormat::DOCBOOK, buffer().params().docbook_table_output);
3960 xs << xml::EndTag(caption_tag);
3964 // CALS header: describe all columns in this table. For names, take 'c' then the ID of the column.
3965 // Start at one, as is customary with CALS!
3966 if (buffer().params().docbook_table_output == BufferParams::CALSTable) {
3967 for (col_type c = 0; c < ncols(); ++c) {
3968 std::stringstream attr;
3969 attr << "colnum='" << (c + 1) << "' ";
3970 attr << "colname='c" << (c + 1) << "' ";
3971 Length const cwidth = column_info[c].p_width;
3973 attr << "colwidth='" << cwidth.asHTMLString() << "' ";
3974 attr << "rowheader='norowheader'"; // Last attribute, hence no space at the end.
3976 xs << xml::CompTag("colspec", attr.str());
3981 xmlHeader(xs, runparams, XmlOutputFormat::DOCBOOK);
3982 xmlFooter(xs, runparams, XmlOutputFormat::DOCBOOK);
3983 xmlBody(xs, runparams, XmlOutputFormat::DOCBOOK);
3985 // If this method started the table tag, also make it close it.
3986 if (!runparams.docbook_in_table) {
3987 xs << xml::EndTag("informaltable");
3993 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3997 if (is_long_tabular) {
3998 // We'll wrap it in a div to deal with alignment.
4000 switch (longtabular_alignment) {
4001 case LYX_LONGTABULAR_ALIGN_LEFT:
4004 case LYX_LONGTABULAR_ALIGN_CENTER:
4007 case LYX_LONGTABULAR_ALIGN_RIGHT:
4011 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
4014 // The caption flag is output before header/footer.
4015 if (haveLTCaption()) {
4016 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
4018 for (row_type r = 0; r < nrows(); ++r)
4019 if (row_info[r].caption)
4020 ret += xmlRow(xs, r, runparams, false, XmlOutputFormat::XHTML);
4021 xs << xml::EndTag("div");
4026 xs << xml::StartTag("table");
4029 xmlHeader(xs, runparams, XmlOutputFormat::XHTML);
4030 xmlFooter(xs, runparams, XmlOutputFormat::XHTML);
4031 xmlBody(xs, runparams, XmlOutputFormat::XHTML);
4033 xs << xml::EndTag("table");
4035 if (is_long_tabular) {
4036 xs << xml::EndTag("div");
4043 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
4044 vector<unsigned int> const & clen) const
4046 idx_type const fcell = getFirstCellInRow(row);
4047 idx_type const n = numberOfCellsInRow(row) + fcell;
4050 for (idx_type i = fcell; i < n; ++i) {
4060 for (idx_type i = fcell; i < n; ++i) {
4071 col_type column = cellColumn(i);
4072 int len = clen[column];
4073 while (column < ncols() - 1
4074 && isPartOfMultiColumn(row, ++column))
4075 len += clen[column] + 4;
4076 os << docstring(len, ch);
4091 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
4092 vector<unsigned int> const & clen) const
4094 idx_type const fcell = getFirstCellInRow(row);
4095 idx_type const n = numberOfCellsInRow(row) + fcell;
4098 for (idx_type i = fcell; i < n; ++i) {
4099 if (bottomLine(i)) {
4108 for (idx_type i = fcell; i < n; ++i) {
4109 if (bottomLine(i)) {
4119 col_type column = cellColumn(i);
4120 int len = clen[column];
4121 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4122 len += clen[column] + 4;
4123 os << docstring(len, ch);
4124 if (bottomLine(i)) {
4138 void Tabular::plaintextPrintCell(odocstringstream & os,
4139 OutputParams const & runparams,
4140 idx_type cell, row_type row, col_type column,
4141 vector<unsigned int> const & clen,
4142 bool onlydata, size_t max_length) const
4144 odocstringstream sstr;
4145 cellInset(cell)->plaintext(sstr, runparams, max_length);
4157 unsigned int len1 = sstr.str().length();
4158 unsigned int len2 = clen[column];
4159 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4160 len2 += clen[column] + 4;
4163 switch (getAlignment(cell)) {
4165 case LYX_ALIGN_LEFT:
4168 case LYX_ALIGN_RIGHT:
4172 case LYX_ALIGN_CENTER:
4178 os << docstring(len1, ' ') << sstr.str()
4179 << docstring(len2, ' ');
4181 if (rightLine(cell))
4188 void Tabular::plaintext(odocstringstream & os,
4189 OutputParams const & runparams, int const depth,
4190 bool onlydata, char_type delim, size_t max_length) const
4192 // first calculate the width of the single columns
4193 vector<unsigned int> clen(ncols());
4196 // first all non multicolumn cells!
4197 for (col_type c = 0; c < ncols(); ++c) {
4199 for (row_type r = 0; r < nrows(); ++r) {
4200 idx_type cell = cellIndex(r, c);
4201 if (isMultiColumn(cell))
4203 odocstringstream sstr;
4204 cellInset(cell)->plaintext(sstr, runparams, max_length);
4205 if (clen[c] < sstr.str().length())
4206 clen[c] = sstr.str().length();
4209 // then all multicolumn cells!
4210 for (col_type c = 0; c < ncols(); ++c) {
4211 for (row_type r = 0; r < nrows(); ++r) {
4212 idx_type cell = cellIndex(r, c);
4213 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
4215 odocstringstream sstr;
4216 cellInset(cell)->plaintext(sstr, runparams, max_length);
4217 int len = int(sstr.str().length());
4218 idx_type const n = columnSpan(cell);
4219 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
4221 if (len > int(clen[c + n - 1]))
4222 clen[c + n - 1] = len;
4227 for (row_type r = 0; r < nrows(); ++r) {
4228 if (!onlydata && plaintextTopHLine(os, r, clen))
4229 os << docstring(depth * 2, ' ');
4230 for (col_type c = 0; c < ncols(); ++c) {
4231 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
4233 if (onlydata && c > 0)
4234 // we don't use operator<< for single UCS4 character.
4235 // see explanation in docstream.h
4237 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
4239 if (os.str().size() > max_length)
4244 os << docstring(depth * 2, ' ');
4245 if (plaintextBottomHLine(os, r, clen))
4246 os << docstring(depth * 2, ' ');
4248 if (os.str().size() > max_length)
4254 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4256 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4260 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4262 return cell_info[row][column].inset;
4266 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4268 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4272 void Tabular::setCellInset(row_type row, col_type column,
4273 shared_ptr<InsetTableCell> ins)
4275 CellData & cd = cell_info[row][column];
4280 void Tabular::validate(LaTeXFeatures & features) const
4282 features.require("NeedTabularnewline");
4284 features.require("booktabs");
4285 if (is_long_tabular && !hasVarwidthColumn()) {
4286 if (tabular_width.zero())
4287 features.require("longtable");
4289 features.require("xltabular");
4291 if (rotate && is_long_tabular)
4292 features.require("lscape");
4294 features.require("rotating");
4295 if (hasVarwidthColumn()) {
4296 if (is_long_tabular)
4297 features.require("xltabular");
4299 features.require("tabularx");
4301 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4302 if (isMultiRow(cell))
4303 features.require("multirow");
4304 if (getUsebox(cell) == BOX_VARWIDTH) {
4305 features.require("varwidth");
4306 features.require("cellvarwidth");
4308 if (getVAlignment(cell) != LYX_VALIGN_TOP
4309 || !getPWidth(cell).zero()
4310 || isVTypeColumn(cellColumn(cell)))
4311 features.require("array");
4312 // Tell footnote that we need a savenote
4313 // environment in non-long tables or
4314 // longtable headers/footers
4315 else if (!is_long_tabular && !features.inFloat())
4316 features.saveNoteEnv("tabular");
4317 else if (!isValidRow(cellRow(cell)))
4318 features.saveNoteEnv("longtable");
4320 cellInset(cell)->validate(features);
4321 features.saveNoteEnv(string());
4326 Tabular::BoxType Tabular::useBox(idx_type cell) const
4328 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4329 if (parlist.size() > 1)
4330 return BOX_VARWIDTH;
4332 ParagraphList::const_iterator cit = parlist.begin();
4333 ParagraphList::const_iterator end = parlist.end();
4335 for (; cit != end; ++cit)
4336 for (int i = 0; i < cit->size(); ++i)
4337 if (cit->isNewline(i) || cit->layout().isEnvironment())
4338 return BOX_VARWIDTH;
4344 bool Tabular::hasNewlines(idx_type cell) const
4346 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4347 ParagraphList::const_iterator cit = parlist.begin();
4348 ParagraphList::const_iterator end = parlist.end();
4350 for (; cit != end; ++cit)
4351 for (int i = 0; i < cit->size(); ++i)
4352 if (cit->isNewline(i))
4359 /////////////////////////////////////////////////////////////////////
4363 /////////////////////////////////////////////////////////////////////
4365 InsetTableCell::InsetTableCell(Buffer * buf)
4366 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false), isVarwidth(false),
4367 isMultiColumn(false), isMultiRow(false), isCaptionRow(false),
4368 contentAlign(LYX_ALIGN_CENTER)
4371 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4373 return isFixedWidth;
4377 bool InsetTableCell::forceLocalFontSwitch() const
4383 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4384 FuncStatus & status) const
4386 bool enabled = true;
4387 switch (cmd.action()) {
4388 case LFUN_INSET_SPLIT:
4389 case LFUN_INSET_DISSOLVE:
4392 case LFUN_MATH_DISPLAY:
4393 if (!hasFixedWidth()) {
4398 return InsetText::getStatus(cur, cmd, status);
4400 status.setEnabled(enabled);
4404 docstring InsetTableCell::asString(bool intoInsets)
4407 if (paragraphs().empty())
4409 ParagraphList::const_iterator it = paragraphs().begin();
4410 ParagraphList::const_iterator en = paragraphs().end();
4412 for (; it != en; ++it) {
4417 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4423 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4424 UpdateType utype, TocBackend & backend) const
4426 InsetText::iterateForToc(di, output_active, utype, backend);
4430 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4433 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4434 return InsetText::xhtml(xs, rp);
4438 void InsetTableCell::docbook(XMLStream & xs, OutputParams const & runparams) const
4440 InsetText::docbook(xs, runparams);
4444 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4446 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4447 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4449 // Hand font through to contained lyxtext:
4450 tm.font_.fontInfo() = mi.base.font;
4451 mi.base.textwidth -= horiz_offset;
4453 // This can happen when a layout has a left and right margin,
4454 // and the view is made very narrow. We can't do better than
4455 // to draw it partly out of view (bug 5890).
4456 if (mi.base.textwidth < 1)
4457 mi.base.textwidth = 1;
4459 // We tell metrics here not to expand on multiple pars
4460 // This is the difference to InsetText::Metrics
4461 Changer changetight = changeVar(mi.tight_insets, true);
4462 if (hasFixedWidth())
4463 tm.metrics(mi, dim, mi.base.textwidth);
4465 tm.metrics(mi, dim, 0);
4466 mi.base.textwidth += horiz_offset;
4467 dim.asc += topOffset(mi.base.bv);
4468 dim.des += bottomOffset(mi.base.bv);
4469 dim.wid += horiz_offset;
4473 bool InsetTableCell::insetAllowed(InsetCode code) const
4478 case MATHMACRO_CODE:
4483 return isCaptionRow;
4491 /////////////////////////////////////////////////////////////////////
4495 /////////////////////////////////////////////////////////////////////
4497 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4499 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4500 rowselect_(false), colselect_(false)
4505 InsetTabular::InsetTabular(InsetTabular const & tab)
4506 : Inset(tab), tabular(tab.tabular),
4507 rowselect_(false), colselect_(false)
4512 InsetTabular::~InsetTabular()
4514 hideDialogs("tabular", this);
4518 void InsetTabular::setBuffer(Buffer & buf)
4520 tabular.setBuffer(buf);
4521 Inset::setBuffer(buf);
4525 bool InsetTabular::insetAllowed(InsetCode code) const
4530 case MATHMACRO_CODE:
4535 // this is handled on cell level
4544 bool InsetTabular::allowMultiPar() const
4546 for (col_type c = 0; c < tabular.ncols(); ++c) {
4547 for (row_type r = 0; r < tabular.nrows(); ++r) {
4548 if (tabular.cellInset(r,c)->allowMultiPar())
4556 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4558 return tabular.is_long_tabular &&
4559 (newtype == "Standard" || newtype == "Unnumbered");
4563 void InsetTabular::write(ostream & os) const
4565 os << "Tabular" << endl;
4570 string InsetTabular::contextMenu(BufferView const &, int, int) const
4572 // FIXME: depending on the selection state,
4573 // we could offer a different menu.
4574 return cell(0)->contextMenuName() + ";" + contextMenuName();
4578 string InsetTabular::contextMenuName() const
4580 return "context-tabular";
4584 void InsetTabular::read(Lexer & lex)
4586 //bool const old_format = (lex.getString() == "\\LyXTable");
4594 string token = lex.getString();
4595 while (lex && token != "\\end_inset") {
4597 token = lex.getString();
4600 lex.printError("Missing \\end_inset at this point. ");
4604 int InsetTabular::rowFromY(Cursor & cur, int y) const
4606 // top y coordinate of tabular
4607 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4609 for (; r < tabular.nrows() && y > h; ++r)
4610 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4611 + tabular.interRowSpace(r);
4617 int InsetTabular::columnFromX(Cursor & cur, int x) const
4619 // left x coordinate of tabular
4620 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4622 for (; c < tabular.ncols() && x > w; ++c)
4623 w += tabular.cellWidth(c);
4628 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4630 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4631 // mi.base.textwidth << "\n";
4632 LBUFERR(mi.base.bv);
4634 for (row_type r = 0; r < tabular.nrows(); ++r) {
4637 for (col_type c = 0; c < tabular.ncols(); ++c) {
4638 if (tabular.isPartOfMultiColumn(r, c)
4639 || tabular.isPartOfMultiRow(r, c))
4640 // multicolumn or multirow cell, but not first one
4642 idx_type const cell = tabular.cellIndex(r, c);
4645 Length const p_width = tabular.getPWidth(cell);
4646 if (!p_width.zero())
4647 m.base.textwidth = mi.base.inPixels(p_width);
4648 else if (tabular.column_info[c].varwidth)
4649 m.base.textwidth = tabular.column_info[c].width;
4650 tabular.cellInset(cell)->metrics(m, dim0);
4651 if (!p_width.zero() || tabular.column_info[c].varwidth)
4652 dim0.wid = m.base.textwidth;
4653 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4654 + tabular.interColumnSpace(cell);
4656 // FIXME(?): do we need a second metrics call?
4657 TextMetrics const & tm =
4658 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4660 // determine horizontal offset because of decimal align (if necessary)
4661 int decimal_width = 0;
4662 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4663 InsetTableCell tail = *tabular.cellInset(cell);
4664 tail.setBuffer(tabular.buffer());
4665 // we need to set macrocontext position everywhere
4666 // otherwise we crash with nested insets (e.g. footnotes)
4667 // after decimal point
4668 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4670 dit.push_back(CursorSlice(tail));
4671 tail.setMacrocontextPositionRecursive(dit);
4673 // remove text leading decimal point
4674 docstring const align_d = tabular.column_info[c].decimal_point;
4675 dit = separatorPos(&tail, align_d);
4677 pos_type const psize = tail.paragraphs().front().size();
4679 tail.paragraphs().front().eraseChars(0,
4680 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4682 tail.metrics(m, dim1);
4683 decimal_width = dim1.width();
4686 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4687 tabular.cell_info[r][c].decimal_width = decimal_width;
4689 // with LYX_VALIGN_BOTTOM the descent is relative to the last
4690 // row of the last par (note that the par might have multile rows!)
4691 // = descent of text in last row + bottomOffset:
4692 int const lastpardes = tm.last().second->rows().back().descent()
4693 + bottomOffset(mi.base.bv);
4695 switch (tabular.getVAlignment(cell)) {
4696 case Tabular::LYX_VALIGN_TOP:
4698 case Tabular::LYX_VALIGN_MIDDLE:
4699 offset = -(dim0.des - lastpardes)/2;
4701 case Tabular::LYX_VALIGN_BOTTOM:
4702 offset = -(dim0.des - lastpardes);
4705 tabular.cell_info[r][c].voffset = offset;
4706 maxasc = max(maxasc, dim0.asc - offset);
4707 maxdes = max(maxdes, dim0.des + offset);
4709 int const top_space = tabular.row_info[r].top_space_default ?
4710 default_line_space :
4711 mi.base.inPixels(tabular.row_info[r].top_space);
4712 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4713 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4714 default_line_space :
4715 mi.base.inPixels(tabular.row_info[r].bottom_space);
4716 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4719 // We need to recalculate the metrics after column width calculation
4720 // with xtabular (possibly multiple times, so the call is recursive).
4721 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4723 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4724 dim.des = tabular.height() - dim.asc;
4725 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4729 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4731 if (&cur.inset() == this && cur.selection()) {
4732 if (cur.selIsMultiCell()) {
4735 getSelection(cur, rs, re, cs, ce);
4737 idx_type const cell = tabular.cellIndex(row, col);
4738 col_type const cspan = tabular.columnSpan(cell);
4739 row_type const rspan = tabular.rowSpan(cell);
4740 if (col + cspan - 1 >= cs && col <= ce
4741 && row + rspan - 1 >= rs && row <= re)
4744 if (col == tabular.cellColumn(cur.idx())
4745 && row == tabular.cellRow(cur.idx())) {
4746 CursorSlice const & beg = cur.selBegin();
4747 CursorSlice const & end = cur.selEnd();
4749 if ((end.lastpos() > 0 || end.lastpit() > 0)
4750 && end.pos() == end.lastpos() && beg.pos() == 0
4751 && end.pit() == end.lastpit() && beg.pit() == 0)
4759 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4761 x += ADD_TO_TABULAR_WIDTH;
4763 BufferView * bv = pi.base.bv;
4764 Cursor & cur = pi.base.bv->cursor();
4766 // FIXME: As the full background is painted in drawBackground(),
4767 // we have no choice but to do a full repaint for the Text cells.
4768 pi.full_repaint = true;
4770 bool const original_selection_state = pi.selected;
4774 // Save tabular change status
4775 Change tab_change = pi.change;
4777 int yy = y + tabular.offsetVAlignment();
4778 for (row_type r = 0; r < tabular.nrows(); ++r) {
4780 for (col_type c = 0; c < tabular.ncols(); ++c) {
4781 if (tabular.isPartOfMultiColumn(r, c))
4784 idx = tabular.cellIndex(r, c);
4786 if (tabular.isPartOfMultiRow(r, c)) {
4787 nx += tabular.cellWidth(idx);
4791 pi.selected |= isCellSelected(cur, r, c);
4793 // Mark deleted rows/columns
4794 if (tabular.column_info[c].change.changed())
4795 pi.change = tabular.column_info[c].change;
4796 else if (tabular.row_info[r].change.changed())
4797 pi.change = tabular.row_info[r].change;
4799 pi.change = tab_change;
4801 int const cx = nx + tabular.textHOffset(idx);
4802 int const cy = yy + tabular.textVOffset(idx);
4803 // Cache the Inset position.
4804 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4805 cell(idx)->draw(pi, cx, cy);
4806 drawCellLines(pi, nx, yy, r, idx);
4807 nx += tabular.cellWidth(idx);
4808 pi.selected = original_selection_state;
4811 if (r + 1 < tabular.nrows())
4812 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4813 + tabular.interRowSpace(r + 1);
4818 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4820 x += ADD_TO_TABULAR_WIDTH;
4821 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4822 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4823 pi.backgroundColor(this));
4827 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4829 Cursor & cur = pi.base.bv->cursor();
4831 x += ADD_TO_TABULAR_WIDTH;
4833 if (!cur.selection())
4835 if (&cur.inset() != this)
4840 bool const full_cell_selected = isCellSelected(cur,
4841 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4843 if (cur.selIsMultiCell() || full_cell_selected) {
4844 for (row_type r = 0; r < tabular.nrows(); ++r) {
4846 for (col_type c = 0; c < tabular.ncols(); ++c) {
4847 if (tabular.isPartOfMultiColumn(r, c))
4850 idx_type const cell = tabular.cellIndex(r, c);
4852 if (tabular.isPartOfMultiRow(r, c)) {
4853 xx += tabular.cellWidth(cell);
4856 int const w = tabular.cellWidth(cell);
4857 int const h = tabular.cellHeight(cell);
4858 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4859 if (isCellSelected(cur, r, c))
4860 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4863 if (r + 1 < tabular.nrows())
4864 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4865 + tabular.interRowSpace(r + 1);
4869 // FIXME: This code has no effect because InsetTableCell does not handle
4870 // drawSelection other than the trivial implementation in Inset.
4872 // x += cellXPos(cur.idx());
4873 // x += tabular.textHOffset(cur.idx());
4874 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4881 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4882 Color const incol, bool drawline, bool heavy = false)
4884 Color const col = drawline ? incol : Color_tabularonoffline;
4885 if (drawline && lt > 0)
4886 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4887 Painter::line_onoffdash,
4888 Painter::thin_line);
4889 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4890 drawline ? Painter::line_solid : Painter::line_onoffdash,
4891 (heavy ? 2 : 1) * Painter::thin_line);
4892 if (drawline && rt > 0)
4893 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4894 Painter::line_onoffdash,
4895 Painter::thin_line);
4901 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4902 row_type row, idx_type cell) const
4904 y -= tabular.rowAscent(row);
4905 int const w = tabular.cellWidth(cell);
4906 int const h = tabular.cellHeight(cell);
4910 col_type const col = tabular.cellColumn(cell);
4912 // Colour the frame if rows/columns are added or deleted
4913 Color colour = Color_tabularline;
4914 if (tabular.column_info[col].change.changed()
4915 || tabular.row_info[row].change.changed())
4916 colour = tabular.cellInset(cell)->paragraphs().front().lookupChange(0).color();
4919 bool drawline = tabular.topLine(cell)
4920 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4921 bool heavy = tabular.use_booktabs
4922 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4923 && tabular.rowTopLine(row);
4924 if (tabular.topLineTrim(cell).first
4925 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4927 if (tabular.topLineTrim(cell).second
4928 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4930 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4934 drawline = tabular.bottomLine(cell);
4935 row_type const lastrow = tabular.nrows() - 1;
4936 // Consider multi-rows
4938 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4939 && tabular.isPartOfMultiRow(r + 1, col))
4941 heavy = tabular.use_booktabs
4942 && ((row == lastrow && tabular.rowBottomLine(row))
4943 || (r == lastrow && tabular.rowBottomLine(r)));
4944 if (tabular.bottomLineTrim(cell).first)
4946 if (tabular.bottomLineTrim(cell).second)
4948 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4951 drawline = tabular.leftLine(cell)
4952 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4953 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4956 x -= tabular.interColumnSpace(cell);
4957 col_type next_cell_col = col + 1;
4958 while (next_cell_col < tabular.ncols()
4959 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4961 drawline = tabular.rightLine(cell)
4962 || (next_cell_col < tabular.ncols()
4963 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4964 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4968 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4970 //lyxerr << "InsetTabular::edit: " << this << endl;
4974 if (isRightToLeft(cur))
4975 cur.idx() = tabular.getLastCellInRow(0);
4981 if (isRightToLeft(cur))
4982 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4984 cur.idx() = tabular.numberofcells - 1;
4986 cur.pos() = cur.lastpos(); // FIXME crude guess
4988 cur.setCurrentFont();
4989 // FIXME: this accesses the position cache before it is initialized
4990 //cur.bv().fitCursor();
4994 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4996 // In a longtable, tell captions what the current float is
4997 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4998 string const saveflt = cnts.current_float();
4999 if (tabular.is_long_tabular) {
5000 cnts.current_float("table");
5001 // in longtables, we only step the counter once
5002 cnts.step(from_ascii("table"), utype);
5003 cnts.isLongtable(true);
5006 ParIterator it2 = it;
5008 size_t const end = it2.nargs();
5009 for ( ; it2.idx() < end; it2.top().forwardIdx())
5010 buffer().updateBuffer(it2, utype);
5013 if (tabular.is_long_tabular) {
5014 cnts.current_float(saveflt);
5015 cnts.isLongtable(false);
5020 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
5021 UpdateType utype, TocBackend & backend) const
5023 DocIterator dit = cpit;
5025 size_t const end = dit.nargs();
5026 for ( ; dit.idx() < end; dit.top().forwardIdx())
5027 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
5031 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
5033 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
5034 return x < x0 || x > x0 + tabular.width();
5038 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
5040 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
5041 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
5042 // there is no margin added vertically to tabular insets.
5043 // However, it works for now.
5044 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
5048 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
5050 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
5054 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
5056 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
5057 << "\n cur:" << cur);
5058 CursorSlice sl = cur.top();
5059 Cursor & bvcur = cur.bv().cursor();
5061 FuncCode const act = cmd.action();
5065 case LFUN_MOUSE_PRESS: {
5066 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
5068 if (hitSelectRow(cur.bv(), cmd.x())) {
5069 row_type r = rowFromY(cur, cmd.y());
5070 cur.idx() = tabular.getFirstCellInRow(r);
5074 cur.idx() = tabular.getLastCellInRow(r);
5075 cur.pit() = cur.lastpit();
5076 cur.pos() = cur.lastpos();
5077 cur.selection(true);
5083 if (hitSelectColumn(cur.bv(), cmd.y())) {
5084 col_type c = columnFromX(cur, cmd.x());
5085 cur.idx() = tabular.cellIndex(0, c);
5089 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
5090 cur.pit() = cur.lastpit();
5091 cur.pos() = cur.lastpos();
5092 cur.selection(true);
5097 // do not reset cursor/selection if we have selected
5098 // some cells (bug 2715).
5099 if (cmd.button() == mouse_button::button3
5100 && &bvcur.selBegin().inset() == this
5101 && bvcur.selIsMultiCell())
5104 // Let InsetTableCell do it
5105 cell(cur.idx())->dispatch(cur, cmd);
5108 case LFUN_MOUSE_MOTION:
5109 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
5110 if (cmd.button() == mouse_button::button1) {
5111 // only accept motions to places not deeper nested than the real anchor
5112 if (!bvcur.realAnchor().hasPart(cur)) {
5116 // select (additional) row
5118 row_type r = rowFromY(cur, cmd.y());
5119 cur.idx() = tabular.getLastCellInRow(r);
5120 // we need to reset the cursor's pit and pos now, as the old ones
5121 // may no longer be valid.
5124 bvcur.setCursor(cur);
5125 bvcur.selection(true);
5128 // select (additional) column
5130 col_type c = columnFromX(cur, cmd.x());
5131 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
5132 // we need to reset the cursor's pit and pos now, as the old ones
5133 // may no longer be valid.
5136 bvcur.setCursor(cur);
5137 bvcur.selection(true);
5140 // only update if selection changes
5141 if (bvcur.idx() == cur.idx() &&
5142 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
5143 cur.noScreenUpdate();
5144 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
5145 bvcur.setCursor(cur);
5146 bvcur.selection(true);
5147 // if this is a multicell selection, we just set the cursor to
5148 // the beginning of the cell's text.
5149 if (bvcur.selIsMultiCell()) {
5150 bvcur.pit() = bvcur.lastpit();
5151 bvcur.pos() = bvcur.lastpos();
5153 // Let InsetTableCell do it
5154 cell(cur.idx())->dispatch(cur, cmd);
5158 case LFUN_MOUSE_RELEASE:
5163 case LFUN_CELL_BACKWARD:
5165 cur.selection(false);
5168 case LFUN_CELL_FORWARD:
5170 cur.selection(false);
5173 case LFUN_CHAR_FORWARD_SELECT:
5174 case LFUN_CHAR_FORWARD:
5175 case LFUN_CHAR_BACKWARD_SELECT:
5176 case LFUN_CHAR_BACKWARD:
5177 case LFUN_CHAR_RIGHT_SELECT:
5178 case LFUN_CHAR_RIGHT:
5179 case LFUN_CHAR_LEFT_SELECT:
5180 case LFUN_CHAR_LEFT:
5181 case LFUN_WORD_FORWARD:
5182 case LFUN_WORD_FORWARD_SELECT:
5183 case LFUN_WORD_BACKWARD:
5184 case LFUN_WORD_BACKWARD_SELECT:
5185 case LFUN_WORD_RIGHT:
5186 case LFUN_WORD_RIGHT_SELECT:
5187 case LFUN_WORD_LEFT:
5188 case LFUN_WORD_LEFT_SELECT: {
5189 // determine whether we move to next or previous cell, where to enter
5190 // the new cell from, and which command to "finish" (i.e., exit the
5193 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
5194 FuncCode finish_lfun;
5196 if (act == LFUN_CHAR_FORWARD
5197 || act == LFUN_CHAR_FORWARD_SELECT
5198 || act == LFUN_WORD_FORWARD
5199 || act == LFUN_WORD_FORWARD_SELECT) {
5201 finish_lfun = LFUN_FINISHED_FORWARD;
5203 else if (act == LFUN_CHAR_BACKWARD
5204 || act == LFUN_CHAR_BACKWARD_SELECT
5205 || act == LFUN_WORD_BACKWARD
5206 || act == LFUN_WORD_BACKWARD_SELECT) {
5208 finish_lfun = LFUN_FINISHED_BACKWARD;
5210 // LEFT or RIGHT commands --- the interpretation will depend on the
5211 // table's direction.
5213 bool const right = act == LFUN_CHAR_RIGHT
5214 || act == LFUN_CHAR_RIGHT_SELECT
5215 || act == LFUN_WORD_RIGHT
5216 || act == LFUN_WORD_RIGHT_SELECT;
5217 next_cell = isRightToLeft(cur) != right;
5219 if (lyxrc.visual_cursor)
5220 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
5222 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
5225 bool const select = act == LFUN_CHAR_FORWARD_SELECT
5226 || act == LFUN_CHAR_BACKWARD_SELECT
5227 || act == LFUN_CHAR_RIGHT_SELECT
5228 || act == LFUN_CHAR_LEFT_SELECT
5229 || act == LFUN_WORD_FORWARD_SELECT
5230 || act == LFUN_WORD_RIGHT_SELECT
5231 || act == LFUN_WORD_BACKWARD_SELECT
5232 || act == LFUN_WORD_LEFT_SELECT;
5234 // If we have a multicell selection or we're
5235 // not doing some LFUN_*_SELECT thing anyway...
5236 if (!cur.selIsMultiCell() || !select) {
5237 col_type const c = tabular.cellColumn(cur.idx());
5238 row_type const r = tabular.cellRow(cur.idx());
5239 // Are we trying to select the whole cell and is the whole cell
5240 // not yet selected?
5241 bool const select_whole = select && !isCellSelected(cur, r, c) &&
5242 ((next_cell && cur.pit() == cur.lastpit()
5243 && cur.pos() == cur.lastpos())
5244 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
5246 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
5248 // ...try to dispatch to the cell's inset.
5249 cell(cur.idx())->dispatch(cur, cmd);
5251 // When we already have a selection we want to select the whole cell
5252 // before going to the next cell.
5253 if (select_whole && !empty_cell){
5254 getText(cur.idx())->selectAll(cur);
5256 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5260 // FIXME: When we support the selection of an empty cell, remove
5261 // the !empty_cell from this condition. For now we jump to the next
5262 // cell if the current cell is empty.
5263 if (cur.result().dispatched() && !empty_cell)
5267 // move to next/prev cell, as appropriate
5268 // note that we will always do this if we're selecting and we have
5269 // a multicell selection
5270 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
5271 << " cell from: " << int(entry_from));
5273 moveNextCell(cur, entry_from);
5275 movePrevCell(cur, entry_from);
5276 // if we're exiting the table, call the appropriate FINISHED lfun
5277 if (sl == cur.top()) {
5278 cmd = FuncRequest(finish_lfun);
5283 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5288 case LFUN_DOWN_SELECT:
5290 if (!(cur.selection() && cur.selIsMultiCell()))
5291 cell(cur.idx())->dispatch(cur, cmd);
5293 cur.dispatched(); // override the cell's decision
5294 if (sl == cur.top()) {
5295 // if our Text didn't do anything to the cursor
5296 // then we try to put the cursor into the cell below
5297 // setting also the right targetX.
5298 cur.selHandle(act == LFUN_DOWN_SELECT);
5299 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
5300 int const xtarget = cur.targetX();
5301 // WARNING: Once cur.idx() has been reset, the cursor is in
5302 // an inconsistent state until pos() has been set. Be careful
5303 // what you do with it!
5304 cur.idx() = tabular.cellBelow(cur.idx());
5306 TextMetrics const & tm =
5307 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5308 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5309 cur.setCurrentFont();
5312 if (sl == cur.top()) {
5313 // we trick it to go to forward after leaving the
5315 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5318 if (cur.selIsMultiCell()) {
5319 cur.pit() = cur.lastpit();
5320 cur.pos() = cur.lastpos();
5321 cur.setCurrentFont();
5322 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5325 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5328 case LFUN_UP_SELECT:
5330 if (!(cur.selection() && cur.selIsMultiCell()))
5331 cell(cur.idx())->dispatch(cur, cmd);
5332 cur.dispatched(); // override the cell's decision
5333 if (sl == cur.top()) {
5334 // if our Text didn't do anything to the cursor
5335 // then we try to put the cursor into the cell above
5336 // setting also the right targetX.
5337 cur.selHandle(act == LFUN_UP_SELECT);
5338 if (tabular.cellRow(cur.idx()) != 0) {
5339 int const xtarget = cur.targetX();
5340 // WARNING: Once cur.idx() has been reset, the cursor is in
5341 // an inconsistent state until pos() has been set. Be careful
5342 // what you do with it!
5343 cur.idx() = tabular.cellAbove(cur.idx());
5344 cur.pit() = cur.lastpit();
5345 Text const * text = cell(cur.idx())->getText(0);
5346 TextMetrics const & tm = cur.bv().textMetrics(text);
5347 ParagraphMetrics const & pm =
5348 tm.parMetrics(cur.lastpit());
5349 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5350 cur.setCurrentFont();
5353 if (sl == cur.top()) {
5354 cmd = FuncRequest(LFUN_UP);
5357 if (cur.selIsMultiCell()) {
5359 cur.pos() = cur.lastpos();
5360 cur.setCurrentFont();
5361 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5364 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5367 case LFUN_LAYOUT_TABULAR:
5368 cur.bv().showDialog("tabular");
5371 case LFUN_INSET_MODIFY:
5372 // we come from the dialog
5373 if (cmd.getArg(0) == "tabular")
5374 tabularFeatures(cur, cmd.getLongArg(1));
5379 case LFUN_TABULAR_FEATURE:
5380 tabularFeatures(cur, to_utf8(cmd.argument()));
5383 // insert file functions
5384 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5385 case LFUN_FILE_INSERT_PLAINTEXT:
5387 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5388 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5389 FileName(to_utf8(cmd.argument())));
5392 cur.recordUndoInset();
5393 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5394 // content has been replaced,
5395 // so cursor might be invalid
5396 cur.pos() = cur.lastpos();
5397 cur.pit() = cur.lastpit();
5398 bvcur.setCursor(cur);
5405 if (cur.selIsMultiCell())
5408 cell(cur.idx())->dispatch(cur, cmd);
5412 if (cur.selIsMultiCell()) {
5413 if (copySelection(cur)) {
5414 cur.recordUndoInset();
5418 cell(cur.idx())->dispatch(cur, cmd);
5421 case LFUN_SELF_INSERT:
5422 if (cur.selIsMultiCell()) {
5423 cur.recordUndoInset();
5425 BufferView * bv = &cur.bv();
5426 docstring::const_iterator cit = cmd.argument().begin();
5427 docstring::const_iterator const end = cmd.argument().end();
5428 for (; cit != end; ++cit)
5429 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5432 bv->bookmarkEditPosition();
5434 cell(cur.idx())->dispatch(cur, cmd);
5437 case LFUN_CHAR_DELETE_BACKWARD:
5438 case LFUN_CHAR_DELETE_FORWARD:
5439 if (cur.selIsMultiCell()) {
5440 cur.recordUndoInset();
5443 cell(cur.idx())->dispatch(cur, cmd);
5446 case LFUN_CLIPBOARD_PASTE:
5447 case LFUN_PRIMARY_SELECTION_PASTE: {
5448 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5449 theClipboard().getAsText(Clipboard::PlainTextType) :
5450 theSelection().get();
5453 // pass to InsertPlaintextString, but
5454 // only if we have multi-cell content
5455 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5456 cur.recordUndoInset();
5457 if (insertPlaintextString(cur.bv(), clip, false)) {
5458 // content has been replaced,
5459 // so cursor might be invalid
5461 bvcur.setCursor(cur);
5465 // Let the cell handle normal text
5466 cell(cur.idx())->dispatch(cur, cmd);
5471 if (!tabularStackDirty()) {
5472 // Check if we have plain text or HTML with rows/columns.
5473 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5474 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5475 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5476 docstring const clip =
5477 theClipboard().getAsText(Clipboard::AnyTextType);
5478 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5479 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5480 doDispatch(cur, ncmd);
5484 else if (!theClipboard().isInternal()
5485 && theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5486 // This might be tabular data from another LyX instance. Check!
5487 docstring const clip =
5488 theClipboard().getAsText(Clipboard::PlainTextType);
5489 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5490 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5491 doDispatch(cur, ncmd);
5495 if (!cur.selIsMultiCell())
5496 cell(cur.idx())->dispatch(cur, cmd);
5499 if (theClipboard().isInternal()) {
5500 cur.recordUndoInset();
5501 pasteClipboard(cur);
5505 case LFUN_CHANGE_ACCEPT:
5506 case LFUN_CHANGE_REJECT:
5507 case LFUN_FONT_DEFAULT:
5508 case LFUN_FONT_EMPH:
5509 case LFUN_FONT_BOLD:
5510 case LFUN_FONT_BOLDSYMBOL:
5511 case LFUN_FONT_ROMAN:
5512 case LFUN_FONT_NOUN:
5513 case LFUN_FONT_ITAL:
5514 case LFUN_FONT_FRAK:
5515 case LFUN_FONT_TYPEWRITER:
5516 case LFUN_FONT_SANS:
5517 case LFUN_TEXTSTYLE_APPLY:
5518 case LFUN_TEXTSTYLE_UPDATE:
5519 case LFUN_FONT_SIZE:
5520 case LFUN_FONT_UNDERLINE:
5521 case LFUN_FONT_STRIKEOUT:
5522 case LFUN_FONT_CROSSOUT:
5523 case LFUN_FONT_UNDERUNDERLINE:
5524 case LFUN_FONT_UNDERWAVE:
5526 case LFUN_PARAGRAPH_PARAMS_APPLY:
5527 case LFUN_PARAGRAPH_PARAMS:
5528 case LFUN_WORD_CAPITALIZE:
5529 case LFUN_WORD_UPCASE:
5530 case LFUN_WORD_LOWCASE:
5531 case LFUN_CHARS_TRANSPOSE: {
5532 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5533 if (cur.selIsMultiCell()) {
5536 getSelection(cur, rs, re, cs, ce);
5537 Cursor tmpcur = cur;
5538 for (row_type r = rs; r <= re; ++r) {
5539 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5540 // whole row selected
5541 if (act == LFUN_CHANGE_ACCEPT) {
5542 if (tabular.row_info[r].change.inserted())
5543 tabular.row_info[r].change.setUnchanged();
5544 else if (tabular.row_info[r].change.deleted()) {
5545 tabular.deleteRow(r, true);
5550 if (tabular.row_info[r].change.deleted())
5551 tabular.row_info[r].change.setUnchanged();
5552 else if (tabular.row_info[r].change.inserted()) {
5553 tabular.deleteRow(r, true);
5559 for (col_type c = cs; c <= ce; ++c) {
5560 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5561 // whole col selected
5562 if (act == LFUN_CHANGE_ACCEPT) {
5563 if (tabular.column_info[c].change.inserted())
5564 tabular.column_info[c].change.setUnchanged();
5565 else if (tabular.column_info[c].change.deleted()) {
5566 tabular.deleteColumn(c, true);
5571 if (tabular.column_info[c].change.deleted())
5572 tabular.column_info[c].change.setUnchanged();
5573 else if (tabular.column_info[c].change.inserted()) {
5574 tabular.deleteColumn(c, true);
5580 // cursor follows cell:
5581 tmpcur.idx() = tabular.cellIndex(r, c);
5582 // select this cell only:
5585 tmpcur.resetAnchor();
5586 tmpcur.pit() = tmpcur.lastpit();
5587 tmpcur.pos() = tmpcur.top().lastpos();
5588 tmpcur.setCursor(tmpcur);
5589 tmpcur.setSelection();
5590 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5594 tabular.updateIndexes();
5595 // cursor might be invalid
5597 // change bar might need to be redrawn
5598 cur.screenUpdateFlags(Update::Force);
5599 cur.forceBufferUpdate();
5603 cell(cur.idx())->dispatch(cur, cmd);
5608 case LFUN_CHANGE_NEXT:
5609 case LFUN_CHANGE_PREVIOUS: {
5610 // BufferView::dispatch has already moved the cursor, we just
5611 // need to select here if we have a changed row or column
5612 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5614 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5618 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5619 cur.pit() = cur.lastpit();
5620 cur.pos() = cur.lastpos();
5621 cur.selection(true);
5625 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5627 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5631 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5632 cur.pit() = cur.lastpit();
5633 cur.pos() = cur.lastpos();
5634 cur.selection(true);
5641 case LFUN_INSET_SETTINGS:
5642 // relay this lfun to Inset, not to the cell.
5643 Inset::doDispatch(cur, cmd);
5647 // we try to handle this event in the insets dispatch function.
5648 cell(cur.idx())->dispatch(cur, cmd);
5654 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5655 string const & argument, FuncStatus & status) const
5658 int action = Tabular::LAST_ACTION;
5660 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5661 if (tabularFeature[i].feature == s) {
5662 action = tabularFeature[i].action;
5666 if (action == Tabular::LAST_ACTION) {
5668 status.setUnknown(true);
5672 row_type sel_row_start = 0;
5673 row_type sel_row_end = 0;
5674 col_type sel_col_start = 0;
5675 col_type sel_col_end = 0;
5676 Tabular::ltType dummyltt;
5679 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5682 case Tabular::SET_PWIDTH:
5683 case Tabular::SET_MPWIDTH:
5684 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5685 case Tabular::SET_SPECIAL_COLUMN:
5686 case Tabular::SET_SPECIAL_MULTICOLUMN:
5687 case Tabular::APPEND_ROW:
5688 case Tabular::APPEND_COLUMN:
5689 case Tabular::COPY_ROW:
5690 case Tabular::COPY_COLUMN:
5691 case Tabular::SET_TOP_SPACE:
5692 case Tabular::SET_BOTTOM_SPACE:
5693 case Tabular::SET_INTERLINE_SPACE:
5697 case Tabular::DELETE_ROW:
5698 status.setEnabled(tabular.nrows() > 1);
5700 case Tabular::DELETE_COLUMN:
5701 status.setEnabled(tabular.ncols() > 1);
5704 case Tabular::SET_TABULAR_WIDTH:
5705 status.setEnabled(!tabular.rotate
5706 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5709 case Tabular::MOVE_COLUMN_RIGHT:
5710 case Tabular::MOVE_COLUMN_LEFT:
5711 case Tabular::MOVE_ROW_DOWN:
5712 case Tabular::MOVE_ROW_UP: {
5715 if (cur.selIsMultiCell())
5716 getSelection(cur, rs, re, cs, ce);
5718 rs = tabular.cellRow(cur.idx());
5720 cs = tabular.cellColumn(cur.idx());
5723 if ((action == Tabular::MOVE_COLUMN_RIGHT
5724 && tabular.ncols() == ce + 1)
5725 || (action == Tabular::MOVE_COLUMN_LEFT && cs == 0)
5726 || (action == Tabular::MOVE_ROW_DOWN
5727 && tabular.nrows() == re + 1)
5728 || (action == Tabular::MOVE_ROW_UP && rs == 0)) {
5729 status.setEnabled(false);
5733 if (action == Tabular::MOVE_COLUMN_RIGHT
5734 || action == Tabular::MOVE_COLUMN_LEFT) {
5735 bool has_multicol = (action == Tabular::MOVE_COLUMN_RIGHT)
5736 ? tabular.hasMultiColumn(ce + 1)
5737 : tabular.hasMultiColumn(cs - 1);
5738 for (col_type c = cs; c <= ce; ++c) {
5739 if (tabular.hasMultiColumn(c)) {
5740 has_multicol = true;
5745 status.message(_("Column movement not supported with multi-columns."));
5746 status.setEnabled(false);
5751 if (action == Tabular::MOVE_ROW_DOWN
5752 || action == Tabular::MOVE_ROW_UP) {
5753 bool has_multirow = (action == Tabular::MOVE_ROW_DOWN)
5754 ? tabular.hasMultiRow(re + 1)
5755 : tabular.hasMultiRow(rs - 1);
5756 for (row_type r = rs; r <= re; ++r) {
5757 if (tabular.hasMultiRow(r)) {
5758 has_multirow = true;
5763 status.message(_("Row movement not supported with multi-rows."));
5764 status.setEnabled(false);
5769 status.setEnabled(true);
5773 case Tabular::SET_DECIMAL_POINT:
5775 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5778 case Tabular::SET_MULTICOLUMN:
5779 case Tabular::UNSET_MULTICOLUMN:
5780 case Tabular::MULTICOLUMN:
5781 // If a row is set as longtable caption, it must not be allowed
5782 // to unset that this row is a multicolumn.
5783 status.setEnabled(sel_row_start == sel_row_end
5784 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5785 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5788 case Tabular::SET_MULTIROW:
5789 case Tabular::UNSET_MULTIROW:
5790 case Tabular::MULTIROW:
5791 // If a row is set as longtable caption, it must not be allowed
5792 // to unset that this row is a multirow.
5793 status.setEnabled(sel_col_start == sel_col_end
5794 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5795 status.setOnOff(tabular.isMultiRow(cur.idx()));
5798 case Tabular::TOGGLE_INNER_LINES:
5799 status.setOnOff(tabular.innerBorders(sel_row_start, sel_row_end,
5800 sel_col_start, sel_col_end));
5801 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5803 case Tabular::TOGGLE_ALL_LINES:
5804 status.setOnOff(tabular.innerBorders(sel_row_start, sel_row_end,
5805 sel_col_start, sel_col_end)
5806 && tabular.outsideBorders(sel_row_start, sel_row_end,
5807 sel_col_start, sel_col_end));
5808 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5810 case Tabular::SET_ALL_LINES:
5811 case Tabular::UNSET_ALL_LINES:
5812 case Tabular::SET_INNER_LINES:
5813 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5816 case Tabular::TOGGLE_BORDER_LINES:
5817 status.setOnOff(tabular.outsideBorders(sel_row_start, sel_row_end,
5818 sel_col_start, sel_col_end));
5820 case Tabular::SET_BORDER_LINES:
5821 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5824 case Tabular::RESET_FORMAL_DEFAULT:
5825 status.setEnabled(tabular.use_booktabs);
5828 case Tabular::SET_LINE_TOP:
5829 case Tabular::SET_LINE_BOTTOM:
5830 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5833 case Tabular::SET_LINE_LEFT:
5834 case Tabular::SET_LINE_RIGHT:
5835 status.setEnabled(!tabular.use_booktabs
5836 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5839 case Tabular::SET_LTRIM_TOP:
5840 case Tabular::SET_RTRIM_TOP:
5841 status.setEnabled(tabular.use_booktabs
5842 && tabular.cellRow(cur.idx()) != 0
5843 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5846 case Tabular::SET_LTRIM_BOTTOM:
5847 case Tabular::SET_RTRIM_BOTTOM:
5848 status.setEnabled(tabular.use_booktabs
5849 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5850 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5853 case Tabular::TOGGLE_LINE_TOP:
5854 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5855 status.setOnOff(tabular.topLine(cur.idx()));
5858 case Tabular::TOGGLE_LINE_BOTTOM:
5859 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5860 status.setOnOff(tabular.bottomLine(cur.idx()));
5863 case Tabular::TOGGLE_LINE_LEFT:
5864 status.setEnabled(!tabular.use_booktabs
5865 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5866 status.setOnOff(tabular.leftLine(cur.idx()));
5869 case Tabular::TOGGLE_LINE_RIGHT:
5870 status.setEnabled(!tabular.use_booktabs
5871 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5872 status.setOnOff(tabular.rightLine(cur.idx()));
5875 case Tabular::TOGGLE_LTRIM_TOP:
5876 status.setEnabled(tabular.use_booktabs
5877 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5878 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5881 case Tabular::TOGGLE_RTRIM_TOP:
5882 status.setEnabled(tabular.use_booktabs
5883 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5884 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5887 case Tabular::TOGGLE_LTRIM_BOTTOM:
5888 status.setEnabled(tabular.use_booktabs
5889 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5890 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5893 case Tabular::TOGGLE_RTRIM_BOTTOM:
5894 status.setEnabled(tabular.use_booktabs
5895 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5896 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5899 // multirow cells only inherit the alignment of the column if the column has
5900 // no width, otherwise they are left-aligned
5901 // therefore allow always left but right and center only if there is no width
5902 case Tabular::M_ALIGN_LEFT:
5905 case Tabular::ALIGN_LEFT:
5906 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5909 case Tabular::M_ALIGN_RIGHT:
5912 case Tabular::ALIGN_RIGHT:
5913 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5914 && !tabular.getPWidth(cur.idx()).zero()));
5915 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5918 case Tabular::M_ALIGN_CENTER:
5921 case Tabular::ALIGN_CENTER:
5922 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5923 && !tabular.getPWidth(cur.idx()).zero()));
5924 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5927 case Tabular::ALIGN_BLOCK:
5928 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5929 && !tabular.isMultiRow(cur.idx()));
5930 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5933 case Tabular::ALIGN_DECIMAL:
5934 status.setEnabled(!tabular.isMultiRow(cur.idx())
5935 && !tabular.isMultiColumn(cur.idx()));
5936 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5939 case Tabular::M_VALIGN_TOP:
5942 case Tabular::VALIGN_TOP:
5943 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5944 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5946 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5949 case Tabular::M_VALIGN_BOTTOM:
5952 case Tabular::VALIGN_BOTTOM:
5953 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5954 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5956 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5959 case Tabular::M_VALIGN_MIDDLE:
5962 case Tabular::VALIGN_MIDDLE:
5963 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5964 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5966 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5969 case Tabular::SET_LONGTABULAR:
5970 case Tabular::TOGGLE_LONGTABULAR:
5971 // setting as longtable is not allowed when table is inside a float
5972 if (cur.innerInsetOfType(FLOAT_CODE) != nullptr
5973 || cur.innerInsetOfType(WRAP_CODE) != nullptr)
5974 status.setEnabled(false);
5976 status.setEnabled(true);
5977 status.setOnOff(tabular.is_long_tabular);
5980 case Tabular::UNSET_LONGTABULAR:
5981 status.setOnOff(!tabular.is_long_tabular);
5984 case Tabular::TOGGLE_ROTATE_TABULAR:
5985 case Tabular::SET_ROTATE_TABULAR:
5986 status.setOnOff(tabular.rotate != 0);
5989 case Tabular::TABULAR_VALIGN_TOP:
5990 status.setEnabled(tabular.tabular_width.zero());
5991 status.setOnOff(tabular.tabular_valignment
5992 == Tabular::LYX_VALIGN_TOP);
5994 case Tabular::TABULAR_VALIGN_MIDDLE:
5995 status.setEnabled(tabular.tabular_width.zero());
5996 status.setOnOff(tabular.tabular_valignment
5997 == Tabular::LYX_VALIGN_MIDDLE);
5999 case Tabular::TABULAR_VALIGN_BOTTOM:
6000 status.setEnabled(tabular.tabular_width.zero());
6001 status.setOnOff(tabular.tabular_valignment
6002 == Tabular::LYX_VALIGN_BOTTOM);
6005 case Tabular::LONGTABULAR_ALIGN_LEFT:
6006 status.setOnOff(tabular.longtabular_alignment
6007 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
6009 case Tabular::LONGTABULAR_ALIGN_CENTER:
6010 status.setOnOff(tabular.longtabular_alignment
6011 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
6013 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6014 status.setOnOff(tabular.longtabular_alignment
6015 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
6018 case Tabular::UNSET_ROTATE_TABULAR:
6019 status.setOnOff(tabular.rotate == 0);
6022 case Tabular::TOGGLE_ROTATE_CELL:
6023 case Tabular::SET_ROTATE_CELL:
6024 status.setOnOff(!oneCellHasRotationState(false,
6025 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
6028 case Tabular::UNSET_ROTATE_CELL:
6029 status.setOnOff(!oneCellHasRotationState(true,
6030 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
6033 case Tabular::SET_USEBOX:
6034 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
6037 // every row can only be one thing:
6038 // either a footer or header
6039 case Tabular::SET_LTFIRSTHEAD:
6040 status.setEnabled(sel_row_start == sel_row_end);
6041 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
6044 case Tabular::UNSET_LTFIRSTHEAD:
6045 status.setEnabled(sel_row_start == sel_row_end);
6046 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
6049 case Tabular::SET_LTHEAD:
6050 status.setEnabled(sel_row_start == sel_row_end);
6051 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
6054 case Tabular::UNSET_LTHEAD:
6055 status.setEnabled(sel_row_start == sel_row_end);
6056 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
6059 case Tabular::SET_LTFOOT:
6060 status.setEnabled(sel_row_start == sel_row_end);
6061 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
6064 case Tabular::UNSET_LTFOOT:
6065 status.setEnabled(sel_row_start == sel_row_end);
6066 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
6069 case Tabular::SET_LTLASTFOOT:
6070 status.setEnabled(sel_row_start == sel_row_end);
6071 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
6074 case Tabular::UNSET_LTLASTFOOT:
6075 status.setEnabled(sel_row_start == sel_row_end);
6076 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
6079 case Tabular::SET_LTNEWPAGE:
6080 status.setOnOff(tabular.getLTNewPage(sel_row_start));
6082 case Tabular::UNSET_LTNEWPAGE:
6083 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
6086 // only one row in head/firsthead/foot/lasthead can be the caption
6087 // and a multirow cannot be set as caption
6088 case Tabular::SET_LTCAPTION:
6089 status.setEnabled(sel_row_start == sel_row_end
6090 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
6091 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
6092 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
6093 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
6094 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
6095 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
6096 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
6097 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
6098 && !tabular.isMultiRow(sel_row_start));
6099 status.setOnOff(tabular.ltCaption(sel_row_start));
6102 case Tabular::UNSET_LTCAPTION:
6103 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
6106 case Tabular::TOGGLE_LTCAPTION:
6107 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
6108 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
6109 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
6110 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
6111 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
6112 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
6113 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
6114 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
6115 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
6116 status.setOnOff(tabular.ltCaption(sel_row_start));
6119 case Tabular::TOGGLE_BOOKTABS:
6120 case Tabular::SET_BOOKTABS:
6121 status.setOnOff(tabular.use_booktabs);
6124 case Tabular::UNSET_BOOKTABS:
6125 status.setOnOff(!tabular.use_booktabs);
6130 status.setEnabled(false);
6137 // function sets an object as defined in FuncStatus.h:
6138 // states OK, Unknown, Disabled, On, Off.
6139 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
6140 FuncStatus & status) const
6142 switch (cmd.action()) {
6143 case LFUN_INSET_MODIFY:
6144 if (cmd.getArg(0) != "tabular")
6146 if (cmd.getArg(1) == "for-dialog") {
6147 // The dialog is asking the status of a command
6148 if (&cur.inset() != this)
6150 string action = cmd.getArg(2);
6151 string arg = cmd.getLongArg(3);
6152 return getFeatureStatus(cur, action, arg, status);
6154 // We always enable the lfun if it is coming from the dialog
6155 // because the dialog makes sure all the settings are valid,
6156 // even though the first argument might not be valid now.
6157 status.setEnabled(true);
6161 case LFUN_TABULAR_FEATURE: {
6162 if (&cur.inset() != this)
6164 string action = cmd.getArg(0);
6165 string arg = cmd.getLongArg(1);
6166 return getFeatureStatus(cur, action, arg, status);
6169 case LFUN_CAPTION_INSERT: {
6170 // caption is only allowed in caption cell of longtable
6171 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
6172 status.setEnabled(false);
6175 // only standard caption is allowed
6176 string arg = cmd.getArg(0);
6177 if (!arg.empty() && arg != "Standard") {
6178 status.setEnabled(false);
6181 // check if there is already a caption
6182 bool have_caption = false;
6183 InsetTableCell itc = *tabular.cellInset(cur.idx());
6184 ParagraphList::const_iterator pit = itc.paragraphs().begin();
6185 ParagraphList::const_iterator pend = itc.paragraphs().end();
6186 for (; pit != pend; ++pit) {
6187 InsetList::const_iterator it = pit->insetList().begin();
6188 InsetList::const_iterator end = pit->insetList().end();
6189 for (; it != end; ++it) {
6190 if (it->inset->lyxCode() == CAPTION_CODE) {
6191 have_caption = true;
6196 status.setEnabled(!have_caption);
6200 // These are only enabled inside tabular
6201 case LFUN_CELL_BACKWARD:
6202 case LFUN_CELL_FORWARD:
6203 status.setEnabled(true);
6206 // disable these with multiple cells selected
6207 case LFUN_INSET_INSERT:
6208 case LFUN_TABULAR_INSERT:
6209 case LFUN_TABULAR_STYLE_INSERT:
6210 case LFUN_FLEX_INSERT:
6211 case LFUN_FLOAT_INSERT:
6212 case LFUN_FLOAT_WIDE_INSERT:
6213 case LFUN_FOOTNOTE_INSERT:
6214 case LFUN_MARGINALNOTE_INSERT:
6215 case LFUN_MATH_INSERT:
6216 case LFUN_MATH_MODE:
6217 case LFUN_MATH_MUTATE:
6218 case LFUN_MATH_DISPLAY:
6219 case LFUN_NOTE_INSERT:
6220 case LFUN_ARGUMENT_INSERT:
6221 case LFUN_BOX_INSERT:
6222 case LFUN_BRANCH_INSERT:
6223 case LFUN_PHANTOM_INSERT:
6224 case LFUN_WRAP_INSERT:
6225 case LFUN_PREVIEW_INSERT:
6226 case LFUN_ERT_INSERT: {
6227 if (cur.selIsMultiCell()) {
6228 status.setEnabled(false);
6231 return cell(cur.idx())->getStatus(cur, cmd, status);
6234 case LFUN_CHANGE_ACCEPT:
6235 case LFUN_CHANGE_REJECT: {
6236 if (cur.selIsMultiCell()) {
6239 getSelection(cur, rs, re, cs, ce);
6240 for (row_type r = rs; r <= re; ++r) {
6241 if (tabular.row_info[r].change.changed()) {
6242 status.setEnabled(true);
6245 for (col_type c = cs; c <= ce; ++c) {
6246 if (tabular.column_info[c].change.changed()) {
6247 status.setEnabled(true);
6253 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
6254 status.setEnabled(true);
6257 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
6258 status.setEnabled(true);
6262 return cell(cur.idx())->getStatus(cur, cmd, status);
6265 case LFUN_PARAGRAPH_BREAK:
6266 case LFUN_NEWLINE_INSERT:
6267 return cell(cur.idx())->getStatus(cur, cmd, status);
6269 case LFUN_NEWPAGE_INSERT:
6270 status.setEnabled(false);
6274 if (tabularStackDirty() && theClipboard().isInternal()) {
6275 if (cur.selIsMultiCell()) {
6278 getSelection(cur, rs, re, cs, ce);
6279 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
6280 && paste_tabular->nrows() == re - rs + 1)
6281 status.setEnabled(true);
6283 status.setEnabled(false);
6284 status.message(_("Selection size should match clipboard content."));
6287 status.setEnabled(true);
6290 return cell(cur.idx())->getStatus(cur, cmd, status);
6292 case LFUN_INSET_SETTINGS:
6293 // relay this lfun to Inset, not to the cell.
6294 return Inset::getStatus(cur, cmd, status);
6297 // we try to handle this event in the insets dispatch function.
6298 return cell(cur.idx())->getStatus(cur, cmd, status);
6304 int InsetTabular::rowFlags() const
6306 if (tabular.is_long_tabular) {
6307 switch (tabular.longtabular_alignment) {
6308 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
6309 return Display | AlignLeft;
6310 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
6312 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
6313 return Display | AlignRight;
6322 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
6324 tabular.latex(os, runparams);
6328 int InsetTabular::plaintext(odocstringstream & os,
6329 OutputParams const & runparams, size_t max_length) const
6331 os << '\n'; // output table on a new line
6332 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
6333 tabular.plaintext(os, runparams, dp, false, 0, max_length);
6334 return PLAINTEXT_NEWLINE;
6338 void InsetTabular::docbook(XMLStream & xs, OutputParams const & runparams) const
6340 tabular.docbook(xs, runparams);
6344 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6346 return tabular.xhtml(xs, rp);
6350 void InsetTabular::validate(LaTeXFeatures & features) const
6352 tabular.validate(features);
6353 features.useInsetLayout(getLayout());
6357 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6359 return tabular.cellInset(idx);
6363 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6365 return tabular.cellInset(idx);
6369 void InsetTabular::cursorPos(BufferView const & bv,
6370 CursorSlice const & sl, bool boundary, int & x, int & y) const
6372 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6374 // y offset correction
6375 y += cellYPos(sl.idx());
6376 y += tabular.textVOffset(sl.idx());
6377 y += tabular.offsetVAlignment();
6379 // x offset correction
6380 x += cellXPos(sl.idx());
6381 x += tabular.textHOffset(sl.idx());
6382 x += ADD_TO_TABULAR_WIDTH;
6386 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6390 Inset const & inset = *tabular.cellInset(cell);
6391 Point o = bv.coordCache().getInsets().xy(&inset);
6392 int const xbeg = o.x_ - tabular.textHOffset(cell);
6393 int const xend = xbeg + tabular.cellWidth(cell);
6394 row_type const row = tabular.cellRow(cell);
6395 int const ybeg = o.y_ - tabular.rowAscent(row)
6396 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6397 int const yend = ybeg + tabular.cellHeight(cell);
6409 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6410 // << " ybeg=" << ybeg << " yend=" << yend
6411 // << " xx=" << xx << " yy=" << yy
6412 // << " dist=" << xx + yy << endl;
6417 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6419 //lyxerr << "InsetTabular::editXY: " << this << endl;
6421 cur.idx() = getNearestCell(cur.bv(), x, y);
6422 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6426 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6428 cur.idx() = getNearestCell(cur.bv(), x, y);
6429 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6433 idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6435 idx_type idx_min = 0;
6436 int dist_min = numeric_limits<int>::max();
6437 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6438 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6439 int const d = dist(bv, i, x, y);
6450 int InsetTabular::cellYPos(idx_type const cell) const
6452 row_type row = tabular.cellRow(cell);
6454 for (row_type r = 0; r < row; ++r)
6455 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6456 + tabular.interRowSpace(r + 1);
6461 int InsetTabular::cellXPos(idx_type const cell) const
6463 col_type col = tabular.cellColumn(cell);
6465 for (col_type c = 0; c < col; ++c)
6466 lx += tabular.column_info[c].width;
6471 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6473 row_type const row = tabular.cellRow(cur.idx());
6474 col_type const col = tabular.cellColumn(cur.idx());
6476 if (isRightToLeft(cur)) {
6477 if (tabular.cellColumn(cur.idx()) == 0) {
6478 if (row == tabular.nrows() - 1)
6480 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6485 cur.idx() = tabular.getLastCellInRow(row - 1);
6487 cur.idx() = tabular.cellIndex(row, col - 1);
6490 if (tabular.isLastCell(cur.idx()))
6492 if (cur.idx() == tabular.getLastCellInRow(row))
6493 cur.idx() = tabular.cellIndex(row + 1, 0);
6495 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6496 cur.idx() = tabular.cellIndex(row, colnextcell);
6500 cur.boundary(false);
6502 if (cur.selIsMultiCell()) {
6503 cur.pit() = cur.lastpit();
6504 cur.pos() = cur.lastpos();
6511 // in visual mode, place cursor at extreme left or right
6513 switch(entry_from) {
6515 case ENTRY_DIRECTION_RIGHT:
6516 cur.posVisToRowExtremity(false /* !left */);
6518 case ENTRY_DIRECTION_LEFT:
6519 cur.posVisToRowExtremity(true /* left */);
6521 case ENTRY_DIRECTION_IGNORE:
6522 // nothing to do in this case
6526 cur.setCurrentFont();
6530 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6532 row_type const row = tabular.cellRow(cur.idx());
6533 col_type const col = tabular.cellColumn(cur.idx());
6535 if (isRightToLeft(cur)) {
6536 if (cur.idx() == tabular.getLastCellInRow(row)) {
6539 cur.idx() = tabular.getFirstCellInRow(row);
6540 cur.idx() = tabular.cellAbove(cur.idx());
6542 if (tabular.isLastCell(cur.idx()))
6544 if (cur.idx() == tabular.getLastCellInRow(row))
6545 cur.idx() = tabular.cellIndex(row + 1, 0);
6547 cur.idx() = tabular.cellIndex(row, col + 1);
6550 if (cur.idx() == 0) // first cell
6553 cur.idx() = tabular.getLastCellInRow(row - 1);
6555 cur.idx() = tabular.cellIndex(row, col - 1);
6558 if (cur.selIsMultiCell()) {
6559 cur.pit() = cur.lastpit();
6560 cur.pos() = cur.lastpos();
6564 cur.pit() = cur.lastpit();
6565 cur.pos() = cur.lastpos();
6567 // in visual mode, place cursor at extreme left or right
6569 switch(entry_from) {
6571 case ENTRY_DIRECTION_RIGHT:
6572 cur.posVisToRowExtremity(false /* !left */);
6574 case ENTRY_DIRECTION_LEFT:
6575 cur.posVisToRowExtremity(true /* left */);
6577 case ENTRY_DIRECTION_IGNORE:
6578 // nothing to do in this case
6582 cur.setCurrentFont();
6586 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6588 cur.recordUndoInset(this);
6590 istringstream is(argument);
6591 // limit the size of strings we read to avoid memory problems
6595 size_t safe_guard = 0;
6600 if (safe_guard > 1000) {
6601 LYXERR0("parameter max count reached!");
6605 Tabular::Feature action = Tabular::LAST_ACTION;
6608 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6609 if (s != tabularFeature[i].feature)
6611 action = tabularFeature[i].action;
6614 if (action == Tabular::LAST_ACTION) {
6615 LYXERR0("Feature not found " << s);
6619 if (tabularFeature[i].need_value)
6621 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6622 tabularFeatures(cur, action, val);
6627 static void checkLongtableSpecial(Tabular::ltType & ltt,
6628 string const & special, bool & flag)
6630 if (special == "dl_above") {
6633 } else if (special == "dl_below") {
6634 ltt.bottomDL = flag;
6636 } else if (special == "empty") {
6646 bool InsetTabular::oneCellHasRotationState(bool rotated,
6647 row_type row_start, row_type row_end,
6648 col_type col_start, col_type col_end) const
6650 for (row_type r = row_start; r <= row_end; ++r)
6651 for (col_type c = col_start; c <= col_end; ++c)
6653 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6656 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6663 void InsetTabular::tabularFeatures(Cursor & cur,
6664 Tabular::Feature feature, string const & value)
6666 col_type sel_col_start;
6667 col_type sel_col_end;
6668 row_type sel_row_start;
6669 row_type sel_row_end;
6670 bool setLines = false;
6671 bool toggle = false;
6672 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6673 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6677 case Tabular::M_ALIGN_LEFT:
6678 case Tabular::ALIGN_LEFT:
6679 setAlign = LYX_ALIGN_LEFT;
6682 case Tabular::M_ALIGN_RIGHT:
6683 case Tabular::ALIGN_RIGHT:
6684 setAlign = LYX_ALIGN_RIGHT;
6687 case Tabular::M_ALIGN_CENTER:
6688 case Tabular::ALIGN_CENTER:
6689 setAlign = LYX_ALIGN_CENTER;
6692 case Tabular::ALIGN_BLOCK:
6693 setAlign = LYX_ALIGN_BLOCK;
6696 case Tabular::ALIGN_DECIMAL:
6697 setAlign = LYX_ALIGN_DECIMAL;
6700 case Tabular::M_VALIGN_TOP:
6701 case Tabular::VALIGN_TOP:
6702 setVAlign = Tabular::LYX_VALIGN_TOP;
6705 case Tabular::M_VALIGN_BOTTOM:
6706 case Tabular::VALIGN_BOTTOM:
6707 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6710 case Tabular::M_VALIGN_MIDDLE:
6711 case Tabular::VALIGN_MIDDLE:
6712 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6719 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6720 row_type const row = tabular.cellRow(cur.idx());
6721 col_type const column = tabular.cellColumn(cur.idx());
6723 Tabular::ltType ltt;
6727 case Tabular::SET_TABULAR_WIDTH:
6728 tabular.setTabularWidth(Length(value));
6731 case Tabular::SET_PWIDTH: {
6732 Length const len(value);
6733 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6734 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6736 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6737 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6742 case Tabular::SET_MPWIDTH:
6743 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6746 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6747 bool const varwidth = value == "on";
6748 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6749 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6753 case Tabular::SET_MROFFSET:
6754 tabular.setMROffset(cur, cur.idx(), Length(value));
6757 case Tabular::SET_SPECIAL_COLUMN:
6758 case Tabular::SET_SPECIAL_MULTICOLUMN:
6759 if (value == "none")
6760 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6762 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6765 case Tabular::APPEND_ROW:
6766 // append the row into the tabular
6767 tabular.appendRow(row);
6770 case Tabular::APPEND_COLUMN:
6771 // append the column into the tabular
6772 tabular.appendColumn(column);
6773 cur.idx() = tabular.cellIndex(row, column);
6776 case Tabular::DELETE_ROW:
6777 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6778 for (col_type c = 0; c < tabular.ncols(); c++) {
6779 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6780 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6781 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6782 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6786 for (row_type r = sel_row_end; r >= sel_row_start; --r) {
6787 tabular.deleteRow(r);
6791 if (sel_row_start >= tabular.nrows())
6793 cur.idx() = tabular.cellIndex(sel_row_start, column);
6796 cur.selection(false);
6799 case Tabular::DELETE_COLUMN:
6800 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6801 for (row_type r = 0; r < tabular.nrows(); r++)
6802 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6803 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6806 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6807 for (row_type r = 0; r < tabular.nrows(); r++)
6808 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6809 tabular.leftLine(tabular.cellIndex(r, 0)));
6812 for (col_type c = sel_col_end; c >= sel_col_start; --c) {
6813 tabular.deleteColumn(c);
6817 if (sel_col_start >= tabular.ncols())
6819 cur.idx() = tabular.cellIndex(row, sel_col_start);
6822 cur.selection(false);
6825 case Tabular::COPY_ROW:
6826 tabular.copyRow(row);
6829 case Tabular::COPY_COLUMN:
6830 tabular.copyColumn(column);
6831 cur.idx() = tabular.cellIndex(row, column);
6834 case Tabular::MOVE_COLUMN_RIGHT:
6835 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::RIGHT);
6836 if (cur.selection()) {
6837 cur.selection(false);
6838 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start + 1);
6840 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end + 1);
6843 cur.idx() = tabular.cellIndex(row, column + 1);
6846 case Tabular::MOVE_COLUMN_LEFT:
6847 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::LEFT);
6848 if (cur.selection()) {
6849 cur.selection(false);
6850 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start - 1);
6852 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end - 1);
6855 cur.idx() = tabular.cellIndex(row, column - 1);
6858 case Tabular::MOVE_ROW_DOWN:
6859 tabular.moveRow(sel_row_start, sel_row_end, Tabular::DOWN);
6860 if (cur.selection()) {
6861 cur.selection(false);
6862 cur.idx() = tabular.cellIndex(sel_row_start + 1, sel_col_start);
6864 cur.idx() = tabular.cellIndex(sel_row_end + 1, sel_col_end);
6867 cur.idx() = tabular.cellIndex(row + 1, column);
6870 case Tabular::MOVE_ROW_UP:
6871 tabular.moveRow(sel_row_start, sel_row_end, Tabular::UP);
6872 if (cur.selection()) {
6873 cur.selection(false);
6874 cur.idx() = tabular.cellIndex(sel_row_start - 1, sel_col_start);
6876 cur.idx() = tabular.cellIndex(sel_row_end - 1, sel_col_end);
6879 cur.idx() = tabular.cellIndex(row - 1, column);
6882 case Tabular::SET_LINE_TOP:
6883 case Tabular::TOGGLE_LINE_TOP: {
6884 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6885 ? (value == "true") : !tabular.topLine(cur.idx());
6886 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6887 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6888 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6892 case Tabular::SET_LINE_BOTTOM:
6893 case Tabular::TOGGLE_LINE_BOTTOM: {
6894 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6895 ? (value == "true") : !tabular.bottomLine(cur.idx());
6896 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6897 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6898 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6902 case Tabular::SET_LTRIM_TOP:
6903 case Tabular::TOGGLE_LTRIM_TOP: {
6904 bool l = (feature == Tabular::SET_LTRIM_TOP)
6905 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6906 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6907 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6908 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6912 case Tabular::SET_RTRIM_TOP:
6913 case Tabular::TOGGLE_RTRIM_TOP: {
6914 bool l = (feature == Tabular::SET_RTRIM_TOP)
6915 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6916 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6917 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6918 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6922 case Tabular::SET_LTRIM_BOTTOM:
6923 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6924 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6925 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6926 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6927 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6928 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6932 case Tabular::SET_RTRIM_BOTTOM:
6933 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6934 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6935 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6936 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6937 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6938 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6942 case Tabular::SET_LINE_LEFT:
6943 case Tabular::TOGGLE_LINE_LEFT: {
6944 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6945 ? (value == "true") : !tabular.leftLine(cur.idx());
6946 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6947 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6948 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6952 case Tabular::SET_LINE_RIGHT:
6953 case Tabular::TOGGLE_LINE_RIGHT: {
6954 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6955 ? (value == "true") : !tabular.rightLine(cur.idx());
6956 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6957 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6958 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6962 case Tabular::M_ALIGN_LEFT:
6963 case Tabular::M_ALIGN_RIGHT:
6964 case Tabular::M_ALIGN_CENTER:
6965 case Tabular::ALIGN_LEFT:
6966 case Tabular::ALIGN_RIGHT:
6967 case Tabular::ALIGN_CENTER:
6968 case Tabular::ALIGN_BLOCK:
6969 case Tabular::ALIGN_DECIMAL:
6970 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6971 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6972 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6973 !tabular.getPWidth(c).zero());
6976 case Tabular::M_VALIGN_TOP:
6977 case Tabular::M_VALIGN_BOTTOM:
6978 case Tabular::M_VALIGN_MIDDLE:
6981 case Tabular::VALIGN_TOP:
6982 case Tabular::VALIGN_BOTTOM:
6983 case Tabular::VALIGN_MIDDLE:
6984 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6985 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6986 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6989 case Tabular::SET_MULTICOLUMN: {
6990 if (!cur.selection()) {
6991 // just multicol for one single cell
6992 // check whether we are completely in a multicol
6993 if (!tabular.isMultiColumn(cur.idx()))
6994 tabular.setMultiColumn(cur, cur.idx(), 1,
6995 tabular.rightLine(cur.idx()));
6998 // we have a selection so this means we just add all these
6999 // cells to form a multicolumn cell
7000 idx_type const s_start = cur.selBegin().idx();
7001 row_type const col_start = tabular.cellColumn(s_start);
7002 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
7003 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
7004 tabular.rightLine(cur.selEnd().idx()));
7007 cur.selection(false);
7011 case Tabular::UNSET_MULTICOLUMN: {
7012 if (!cur.selection()) {
7013 if (tabular.isMultiColumn(cur.idx()))
7014 tabular.unsetMultiColumn(cur.idx());
7019 case Tabular::MULTICOLUMN: {
7020 if (!cur.selection()) {
7021 if (tabular.isMultiColumn(cur.idx()))
7022 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
7024 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
7028 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
7029 row_type const r = sel_row_start;
7030 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
7031 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
7034 // If the selection contains at least one singlecol cell
7035 // or multiple multicol cells,
7036 // we assume the user will merge is to a single multicol
7038 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
7040 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
7044 case Tabular::SET_MULTIROW: {
7045 if (!cur.selection()) {
7046 // just multirow for one single cell
7047 // check whether we are completely in a multirow
7048 if (!tabular.isMultiRow(cur.idx()))
7049 tabular.setMultiRow(cur, cur.idx(), 1,
7050 tabular.bottomLine(cur.idx()),
7051 tabular.getAlignment(cur.idx()));
7054 // we have a selection so this means we just add all this
7055 // cells to form a multirow cell
7056 idx_type const s_start = cur.selBegin().idx();
7057 row_type const row_start = tabular.cellRow(s_start);
7058 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
7059 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
7060 tabular.bottomLine(cur.selEnd().idx()),
7061 tabular.getAlignment(cur.selEnd().idx()));
7064 cur.selection(false);
7068 case Tabular::UNSET_MULTIROW: {
7069 if (!cur.selection()) {
7070 if (tabular.isMultiRow(cur.idx()))
7071 tabular.unsetMultiRow(cur.idx());
7076 case Tabular::MULTIROW: {
7077 if (!cur.selection()) {
7078 if (tabular.isMultiRow(cur.idx()))
7079 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
7081 tabularFeatures(cur, Tabular::SET_MULTIROW);
7085 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7086 col_type const c = sel_col_start;
7087 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
7088 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
7091 // If the selection contains at least one singlerow cell
7092 // or multiple multirow cells,
7093 // we assume the user will merge is to a single multirow
7095 tabularFeatures(cur, Tabular::SET_MULTIROW);
7097 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
7101 case Tabular::TOGGLE_INNER_LINES:
7104 case Tabular::SET_INNER_LINES:
7106 setLines = !tabular.innerBorders(sel_row_start, sel_row_end,
7107 sel_col_start, sel_col_end);
7110 tabular.setLines(sel_row_start, sel_row_end,
7111 sel_col_start, sel_col_end,
7115 case Tabular::TOGGLE_ALL_LINES:
7118 case Tabular::SET_ALL_LINES:
7120 setLines = !tabular.innerBorders(sel_row_start, sel_row_end,
7121 sel_col_start, sel_col_end)
7122 || !tabular.outsideBorders(sel_row_start, sel_row_end,
7123 sel_col_start, sel_col_end);
7127 case Tabular::UNSET_ALL_LINES:
7128 tabular.setLines(sel_row_start, sel_row_end,
7129 sel_col_start, sel_col_end,
7133 case Tabular::RESET_FORMAL_DEFAULT:
7134 for (row_type r = 0; r < tabular.nrows(); ++r) {
7135 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
7136 for (col_type c = 0; c < tabular.ncols(); ++c) {
7137 idx_type const cell = tabular.cellIndex(r, c);
7138 tabular.setTopLine(cell, head_or_foot);
7139 tabular.setBottomLine(cell, head_or_foot);
7144 case Tabular::TOGGLE_BORDER_LINES:
7147 case Tabular::SET_BORDER_LINES: {
7148 bool const border = toggle &&
7149 tabular.outsideBorders(sel_row_start, sel_row_end,
7150 sel_col_start, sel_col_end)
7152 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7153 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), border);
7154 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), border);
7156 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
7157 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), border);
7158 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), border);
7163 case Tabular::TOGGLE_LONGTABULAR:
7164 if (tabular.is_long_tabular)
7165 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
7167 tabular.is_long_tabular = true;
7170 case Tabular::SET_LONGTABULAR:
7171 tabular.is_long_tabular = true;
7174 case Tabular::UNSET_LONGTABULAR:
7175 for (row_type r = 0; r < tabular.nrows(); ++r) {
7176 if (tabular.ltCaption(r)) {
7177 cur.idx() = tabular.cellIndex(r, 0);
7180 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
7183 tabular.is_long_tabular = false;
7186 case Tabular::SET_ROTATE_TABULAR:
7187 tabular.rotate = convert<int>(value);
7190 case Tabular::UNSET_ROTATE_TABULAR:
7194 case Tabular::TOGGLE_ROTATE_TABULAR:
7195 // when pressing the rotate button we default to 90° rotation
7196 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
7199 case Tabular::TABULAR_VALIGN_TOP:
7200 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
7203 case Tabular::TABULAR_VALIGN_MIDDLE:
7204 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
7207 case Tabular::TABULAR_VALIGN_BOTTOM:
7208 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
7211 case Tabular::LONGTABULAR_ALIGN_LEFT:
7212 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
7215 case Tabular::LONGTABULAR_ALIGN_CENTER:
7216 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
7219 case Tabular::LONGTABULAR_ALIGN_RIGHT:
7220 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
7223 case Tabular::SET_ROTATE_CELL:
7224 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7225 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7226 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
7229 case Tabular::UNSET_ROTATE_CELL:
7230 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7231 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7232 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7235 case Tabular::TOGGLE_ROTATE_CELL:
7237 bool oneNotRotated = oneCellHasRotationState(false,
7238 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
7240 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7241 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
7242 // when pressing the rotate cell button we default to 90° rotation
7244 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
7246 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7251 case Tabular::SET_USEBOX: {
7252 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
7253 if (val == tabular.getUsebox(cur.idx()))
7254 val = Tabular::BOX_NONE;
7255 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7256 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7257 tabular.setUsebox(tabular.cellIndex(r, c), val);
7261 case Tabular::UNSET_LTFIRSTHEAD:
7264 case Tabular::SET_LTFIRSTHEAD:
7265 tabular.getRowOfLTFirstHead(row, ltt);
7266 checkLongtableSpecial(ltt, value, flag);
7267 tabular.setLTHead(row, flag, ltt, true);
7270 case Tabular::UNSET_LTHEAD:
7273 case Tabular::SET_LTHEAD:
7274 tabular.getRowOfLTHead(row, ltt);
7275 checkLongtableSpecial(ltt, value, flag);
7276 tabular.setLTHead(row, flag, ltt, false);
7279 case Tabular::UNSET_LTFOOT:
7282 case Tabular::SET_LTFOOT:
7283 tabular.getRowOfLTFoot(row, ltt);
7284 checkLongtableSpecial(ltt, value, flag);
7285 tabular.setLTFoot(row, flag, ltt, false);
7288 case Tabular::UNSET_LTLASTFOOT:
7291 case Tabular::SET_LTLASTFOOT:
7292 tabular.getRowOfLTLastFoot(row, ltt);
7293 checkLongtableSpecial(ltt, value, flag);
7294 tabular.setLTFoot(row, flag, ltt, true);
7297 case Tabular::UNSET_LTNEWPAGE:
7300 case Tabular::SET_LTNEWPAGE:
7301 tabular.setLTNewPage(row, flag);
7304 case Tabular::SET_LTCAPTION: {
7305 if (tabular.ltCaption(row))
7307 cur.idx() = tabular.setLTCaption(cur, row, true);
7310 cur.selection(false);
7311 // update the captionRow status of all cells
7312 tabular.updateIndexes();
7313 // If a row is set as caption, then also insert
7314 // a caption. Otherwise the LaTeX output is broken.
7315 // Select cell if it is non-empty
7316 if (cur.lastpos() > 0 || cur.lastpit() > 0)
7317 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
7318 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
7322 case Tabular::UNSET_LTCAPTION: {
7323 if (!tabular.ltCaption(row))
7325 cur.idx() = tabular.setLTCaption(cur, row, false);
7328 cur.selection(false);
7329 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
7330 if (lyx::getStatus(fr).enabled())
7332 // update the captionRow status of all cells
7333 tabular.updateIndexes();
7337 case Tabular::TOGGLE_LTCAPTION: {
7338 if (tabular.ltCaption(row))
7339 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
7341 tabularFeatures(cur, Tabular::SET_LTCAPTION);
7345 case Tabular::TOGGLE_BOOKTABS:
7346 tabular.use_booktabs = !tabular.use_booktabs;
7349 case Tabular::SET_BOOKTABS:
7350 tabular.use_booktabs = true;
7353 case Tabular::UNSET_BOOKTABS:
7354 tabular.use_booktabs = false;
7357 case Tabular::SET_TOP_SPACE: {
7359 if (value == "default")
7360 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7361 tabular.row_info[r].top_space_default = true;
7362 else if (value == "none")
7363 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7364 tabular.row_info[r].top_space_default = false;
7365 tabular.row_info[r].top_space = len;
7367 else if (isValidLength(value, &len))
7368 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7369 tabular.row_info[r].top_space_default = false;
7370 tabular.row_info[r].top_space = len;
7375 case Tabular::SET_BOTTOM_SPACE: {
7377 if (value == "default")
7378 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7379 tabular.row_info[r].bottom_space_default = true;
7380 else if (value == "none")
7381 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7382 tabular.row_info[r].bottom_space_default = false;
7383 tabular.row_info[r].bottom_space = len;
7385 else if (isValidLength(value, &len))
7386 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7387 tabular.row_info[r].bottom_space_default = false;
7388 tabular.row_info[r].bottom_space = len;
7393 case Tabular::SET_INTERLINE_SPACE: {
7395 if (value == "default")
7396 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7397 tabular.row_info[r].interline_space_default = true;
7398 else if (value == "none")
7399 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7400 tabular.row_info[r].interline_space_default = false;
7401 tabular.row_info[r].interline_space = len;
7403 else if (isValidLength(value, &len))
7404 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7405 tabular.row_info[r].interline_space_default = false;
7406 tabular.row_info[r].interline_space = len;
7411 case Tabular::SET_DECIMAL_POINT:
7412 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7413 tabular.column_info[c].decimal_point = from_utf8(value);
7416 // dummy stuff just to avoid warnings
7417 case Tabular::LAST_ACTION:
7423 bool InsetTabular::copySelection(Cursor & cur)
7425 if (!cur.selection())
7430 getSelection(cur, rs, re, cs, ce);
7432 paste_tabular.reset(new Tabular(tabular));
7434 for (row_type r = 0; r < rs; ++r)
7435 paste_tabular->deleteRow(0, true);
7437 row_type const rows = re - rs + 1;
7438 while (paste_tabular->nrows() > rows)
7439 paste_tabular->deleteRow(rows, true);
7441 for (col_type c = 0; c < cs; ++c)
7442 paste_tabular->deleteColumn(0, true);
7444 col_type const columns = ce - cs + 1;
7445 while (paste_tabular->ncols() > columns)
7446 paste_tabular->deleteColumn(columns, true);
7448 paste_tabular->setBuffer(tabular.buffer());
7450 odocstringstream os;
7451 OutputParams const runparams(nullptr);
7452 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7453 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7454 cap::copySelection(cur, os.str());
7456 // mark tabular stack dirty
7457 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7458 // when we (hopefully) have a one-for-all paste mechanism.
7459 // This must be called after cap::copySelection.
7460 dirtyTabularStack(true);
7466 bool InsetTabular::pasteClipboard(Cursor & cur)
7470 col_type actcol = tabular.cellColumn(cur.idx());
7471 row_type actrow = tabular.cellRow(cur.idx());
7473 if (cur.selIsMultiCell()) {
7476 getSelection(cur, actrow, re, actcol, ce);
7479 col_type const oldncols = tabular.ncols();
7480 for (row_type r1 = 0, r2 = actrow; r1 < paste_tabular->nrows(); ++r1, ++r2) {
7481 // Append rows if needed
7482 if (r2 == tabular.nrows())
7483 tabular.insertRow(r2 - 1, false);
7484 for (col_type c1 = 0, c2 = actcol; c1 < paste_tabular->ncols(); ++c1, ++c2) {
7485 // Append columns if needed
7486 if (c2 == tabular.ncols())
7487 tabular.insertColumn(c2 - 1, false);
7488 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7489 tabular.isPartOfMultiColumn(r2, c2))
7491 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7495 if (tabular.isPartOfMultiColumn(r2, c2)) {
7499 shared_ptr<InsetTableCell> inset(
7500 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7501 tabular.setCellInset(r2, c2, inset);
7502 // FIXME?: why do we need to do this explicitly? (EL)
7503 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7505 if (lyxrc.ct_markup_copied) {
7506 // Only change to inserted if ct is active,
7507 // otherwise leave markup as is
7508 if (buffer().params().track_changes)
7509 inset->setChange(Change(Change::INSERTED));
7511 // Resolve all markup to inserted or unchanged
7512 inset->setChange(Change(buffer().params().track_changes ?
7513 Change::INSERTED : Change::UNCHANGED));
7518 // amend cursor position if cols have been appended
7519 cur.idx() += actrow * (tabular.ncols() - oldncols);
7524 void InsetTabular::cutSelection(Cursor & cur)
7526 if (!cur.selection())
7531 getSelection(cur, rs, re, cs, ce);
7532 for (row_type r = rs; r <= re; ++r) {
7533 for (col_type c = cs; c <= ce; ++c) {
7534 shared_ptr<InsetTableCell> t
7535 = cell(tabular.cellIndex(r, c));
7536 if (buffer().params().track_changes)
7537 // FIXME: Change tracking (MG)
7538 t->setChange(Change(Change::DELETED));
7544 // cursor position might be invalid now
7545 if (cur.pit() > cur.lastpit())
7546 cur.pit() = cur.lastpit();
7547 if (cur.pos() > cur.lastpos())
7548 cur.pos() = cur.lastpos();
7549 cur.clearSelection();
7553 bool InsetTabular::isRightToLeft(Cursor & cur) const
7555 // LASSERT: It might be better to abandon this Buffer.
7556 LASSERT(cur.depth() > 1, return false);
7557 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7558 pos_type const parentpos = cur[cur.depth() - 2].pos();
7559 return parentpar.getFontSettings(buffer().params(),
7560 parentpos).language()->rightToLeft();
7564 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7567 LASSERT(stidx <= enidx, return docstring());
7569 col_type col1 = tabular.cellColumn(stidx);
7570 col_type col2 = tabular.cellColumn(enidx);
7571 row_type row1 = tabular.cellRow(stidx);
7572 row_type row2 = tabular.cellRow(enidx);
7573 // stidx might be in a later column or row than enidx
7579 for (col_type col = col1; col <= col2; col++)
7580 for (row_type row = row1; row <= row2; row++) {
7585 retval += tabular.cellInset(row, col)->asString(intoInsets);
7591 ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
7593 LASSERT(stidx <= enidx, return ParagraphList());
7594 ParagraphList retval;
7595 col_type col1 = tabular.cellColumn(stidx);
7596 col_type col2 = tabular.cellColumn(enidx);
7597 row_type row1 = tabular.cellRow(stidx);
7598 row_type row2 = tabular.cellRow(enidx);
7599 // stidx might be in a later column or row than enidx
7604 for (col_type col = col1; col <= col2; col++)
7605 for (row_type row = row1; row <= row2; row++)
7606 for (auto const & par : tabular.cellInset(row, col)->paragraphs())
7607 retval.push_back(par);
7612 void InsetTabular::getSelection(Cursor & cur,
7613 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7615 CursorSlice const & beg = cur.selBegin();
7616 CursorSlice const & end = cur.selEnd();
7617 cs = tabular.cellColumn(beg.idx());
7618 ce = tabular.cellColumn(end.idx());
7622 rs = tabular.cellRow(beg.idx());
7623 re = tabular.cellRow(end.idx());
7629 Text * InsetTabular::getText(int idx) const
7631 return size_t(idx) < nargs() ? cell(idx)->getText(0) : nullptr;
7635 bool InsetTabular::isChanged() const
7637 for (idx_type idx = 0; idx < nargs(); ++idx) {
7638 if (cell(idx)->isChanged())
7640 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7642 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7649 void InsetTabular::setChange(Change const & change)
7651 for (idx_type idx = 0; idx < nargs(); ++idx)
7652 cell(idx)->setChange(change);
7656 void InsetTabular::acceptChanges()
7658 for (idx_type idx = 0; idx < nargs(); ++idx)
7659 cell(idx)->acceptChanges();
7660 for (row_type row = 0; row < tabular.nrows(); ++row) {
7661 if (tabular.row_info[row].change.inserted())
7662 tabular.row_info[row].change.setUnchanged();
7663 else if (tabular.row_info[row].change.deleted())
7664 tabular.deleteRow(row, true);
7666 for (col_type col = 0; col < tabular.ncols(); ++col) {
7667 if (tabular.column_info[col].change.inserted())
7668 tabular.column_info[col].change.setUnchanged();
7669 else if (tabular.column_info[col].change.deleted())
7670 tabular.deleteColumn(col, true);
7672 tabular.updateIndexes();
7676 void InsetTabular::rejectChanges()
7678 for (idx_type idx = 0; idx < nargs(); ++idx)
7679 cell(idx)->rejectChanges();
7680 for (row_type row = 0; row < tabular.nrows(); ++row) {
7681 if (tabular.row_info[row].change.deleted())
7682 tabular.row_info[row].change.setUnchanged();
7683 else if (tabular.row_info[row].change.inserted())
7684 tabular.deleteRow(row, true);
7686 for (col_type col = 0; col < tabular.ncols(); ++col) {
7687 if (tabular.column_info[col].change.deleted())
7688 tabular.column_info[col].change.setUnchanged();
7689 else if (tabular.column_info[col].change.inserted())
7690 tabular.deleteColumn(col, true);
7692 tabular.updateIndexes();
7696 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7698 return tabular.getPWidth(cell).zero();
7702 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7705 if (buf.length() <= 0)
7710 col_type maxCols = 1;
7711 size_t const len = buf.length();
7715 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7723 maxCols = max(cols, maxCols);
7729 maxCols = max(cols, maxCols);
7735 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7736 loctab = paste_tabular.get();
7737 dirtyTabularStack(true);
7740 cell = bv.cursor().idx();
7741 ocol = tabular.cellColumn(cell);
7742 row = tabular.cellRow(cell);
7746 idx_type cells = loctab->numberofcells;
7749 rows = loctab->nrows();
7750 col_type columns = loctab->ncols();
7753 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7759 // append column if necessary
7760 if (cols == columns) {
7761 loctab->appendColumn(cols - 1);
7762 columns = loctab->ncols();
7763 cells = loctab->numberofcells;
7766 if (cols < columns) {
7767 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7768 Font const font = bv.textMetrics(&inset->text()).
7770 inset->setText(buf.substr(op, p - op), font,
7771 buffer().params().track_changes);
7777 // we can only set this if we are not too far right
7778 if (cols < columns) {
7779 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7780 Font const font = bv.textMetrics(&inset->text()).
7782 inset->setText(buf.substr(op, p - op), font,
7783 buffer().params().track_changes);
7787 // append row if necessary
7788 if (row == rows && p < len - 1) {
7789 loctab->appendRow(row - 1);
7790 rows = loctab->nrows();
7791 cells = loctab->numberofcells;
7794 cell = loctab->cellIndex(row, cols);
7800 // check for the last cell if there is no trailing '\n'
7801 if (cell < cells && op < len) {
7802 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7803 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7804 inset->setText(buf.substr(op, len - op), font,
7805 buffer().params().track_changes);
7811 void InsetTabular::addPreview(DocIterator const & inset_pos,
7812 PreviewLoader & loader) const
7814 DocIterator cell_pos = inset_pos;
7816 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7817 for (row_type r = 0; r < tabular.nrows(); ++r) {
7818 for (col_type c = 0; c < tabular.ncols(); ++c) {
7819 cell_pos.top().idx() = tabular.cellIndex(r, c);
7820 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7826 bool InsetTabular::completionSupported(Cursor const & cur) const
7828 Cursor const & bvCur = cur.bv().cursor();
7829 if (&bvCur.inset() != this)
7831 return cur.text()->completionSupported(cur);
7835 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7837 return completionSupported(cur);
7841 bool InsetTabular::automaticInlineCompletion() const
7843 return lyxrc.completion_inline_text;
7847 bool InsetTabular::automaticPopupCompletion() const
7849 return lyxrc.completion_popup_text;
7853 bool InsetTabular::showCompletionCursor() const
7855 return lyxrc.completion_cursor_text &&
7856 (lyxrc.completion_inline_text || lyxrc.completion_popup_text);
7860 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7862 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : nullptr;
7866 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7868 if (!completionSupported(cur))
7870 return cur.text()->completionPrefix(cur);
7874 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
7876 if (!completionSupported(cur))
7879 return cur.text()->insertCompletion(cur, s);
7883 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7884 Dimension & dim) const
7886 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7887 tm.completionPosAndDim(cur, x, y, dim);
7891 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7893 istringstream data(in);
7895 lex.setStream(data);
7902 if (!lex || token != "tabular") {
7903 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7907 // This is part of the inset proper that is usually swallowed
7908 // by Buffer::readInset
7910 if (!lex || token != "Tabular") {
7911 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7919 string InsetTabular::params2string(InsetTabular const & inset)
7922 data << "tabular" << ' ';
7924 data << "\\end_inset\n";
7929 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7931 for (col_type c = 0; c < tabular.ncols(); ++c) {
7932 for (row_type r = 0; r < tabular.nrows(); ++r) {
7933 if (!tabular.isPartOfMultiColumn(r,c) &&
7934 !tabular.isPartOfMultiRow(r,c))
7937 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7938 ParagraphList::iterator it = parlist.begin();
7939 ParagraphList::iterator const en = parlist.end();
7940 for (; it != en; ++it)
7941 it->setLayout(dc.plainLayout());