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);
787 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 // If we are in change tracking mode, and the row is not marked
812 // as inserted by the same author, we do not delete it, but mark
814 bool const track_deletion = force ? false
815 : buffer().params().track_changes
816 && (!row_info[row].change.inserted()
817 || !row_info[row].change.currentAuthor());
819 for (col_type c = 0; c < ncols(); ++c) {
820 // Care about multirow cells
821 if (row + 1 < nrows() &&
822 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
823 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
824 cell_info[row + 1][c] = cell_info[row][c];
828 row_info[row].change.setDeleted();
830 row_info.erase(row_info.begin() + row);
831 cell_info.erase(cell_info.begin() + row);
837 void Tabular::copyRow(row_type const row)
839 insertRow(row, true);
843 void Tabular::appendRow(row_type row)
845 insertRow(row, false);
849 void Tabular::insertRow(row_type const row, bool copy)
851 row_info.insert(row_info.begin() + row + 1, row_info[row]);
852 cell_info.insert(cell_info.begin() + row + 1,
853 cell_vector(0, CellData(buffer_)));
855 for (col_type c = 0; c < ncols(); ++c) {
856 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
857 copy ? cell_info[row][c] : CellData(buffer_));
858 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
859 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
863 for (col_type c = 0; c < ncols(); ++c) {
864 if (isPartOfMultiRow(row, c))
866 // inherit line settings
867 idx_type const i = cellIndex(row + 1, c);
868 idx_type const j = cellIndex(row, c);
869 setLeftLine(i, leftLine(j));
870 setRightLine(i, rightLine(j));
871 setTopLine(i, topLine(j));
872 if (topLine(j) && bottomLine(j)) {
873 setBottomLine(i, true);
874 setBottomLine(j, false);
877 if (buffer().params().track_changes) {
878 row_info[row + 1].change.setInserted();
884 void Tabular::moveColumn(col_type col_start, col_type col_end,
885 ColDirection direction)
887 if (direction == Tabular::LEFT) {
888 for (col_type col = col_start; col <= col_end; ++col) {
889 std::swap(column_info[col - 1], column_info[col]);
890 for (row_type r = 0; r < nrows(); ++r) {
891 std::swap(cell_info[r][col - 1], cell_info[r][col]);
892 std::swap(cell_info[r][col - 1].left_line, cell_info[r][col].left_line);
893 std::swap(cell_info[r][col - 1].right_line, cell_info[r][col].right_line);
895 if (buffer().params().track_changes) {
896 idx_type const i = cellIndex(r, col - 1);
897 idx_type const j = cellIndex(r, col);
898 cellInfo(i).inset->setChange(Change(Change::INSERTED));
899 cellInfo(j).inset->setChange(Change(Change::INSERTED));
907 for (col_type col = col_end; col >= col_start; --col) {
908 std::swap(column_info[col], column_info[col + 1]);
909 for (row_type r = 0; r < nrows(); ++r) {
910 std::swap(cell_info[r][col], cell_info[r][col + 1]);
911 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
912 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
914 if (buffer().params().track_changes) {
915 idx_type const i = cellIndex(r, col);
916 idx_type const j = cellIndex(r, col + 1);
917 cellInfo(i).inset->setChange(Change(Change::INSERTED));
918 cellInfo(j).inset->setChange(Change(Change::INSERTED));
929 void Tabular::moveRow(row_type row_start, row_type row_end, RowDirection direction)
931 if (direction == Tabular::UP) {
932 for (row_type row = row_start; row <= row_end; ++row) {
933 std::swap(row_info[row - 1], row_info[row]);
934 for (col_type c = 0; c < ncols(); ++c) {
935 std::swap(cell_info[row - 1][c], cell_info[row][c]);
936 std::swap(cell_info[row - 1][c].top_line, cell_info[row][c].top_line);
937 std::swap(cell_info[row - 1][c].bottom_line, cell_info[row][c].bottom_line);
939 idx_type const i = cellIndex(row - 1, c);
940 idx_type const j = cellIndex(row, c);
941 if (buffer().params().track_changes) {
942 cellInfo(i).inset->setChange(Change(Change::INSERTED));
943 cellInfo(j).inset->setChange(Change(Change::INSERTED));
951 for (row_type row = row_end; row >= row_start; --row) {
952 std::swap(row_info[row], row_info[row + 1]);
953 for (col_type c = 0; c < ncols(); ++c) {
954 std::swap(cell_info[row][c], cell_info[row + 1][c]);
955 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
956 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
958 idx_type const i = cellIndex(row, c);
959 idx_type const j = cellIndex(row + 1, c);
960 if (buffer().params().track_changes) {
961 cellInfo(i).inset->setChange(Change(Change::INSERTED));
962 cellInfo(j).inset->setChange(Change(Change::INSERTED));
973 void Tabular::deleteColumn(col_type const col, bool const force)
975 // Not allowed to delete last column
979 // If we are in change tracking mode, and the column is not marked
980 // as inserted by the same author, we do not delete it, but mark
982 bool const track_deletion = force ? false
983 : buffer().params().track_changes
984 && (!column_info[col].change.inserted()
985 || !column_info[col].change.currentAuthor());
987 for (row_type r = 0; r < nrows(); ++r) {
988 // Care about multicolumn cells
989 if (col + 1 < ncols() &&
990 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
991 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
992 cell_info[r][col + 1] = cell_info[r][col];
995 cell_info[r].erase(cell_info[r].begin() + col);
998 column_info[col].change.setDeleted();
1000 column_info.erase(column_info.begin() + col);
1005 void Tabular::copyColumn(col_type const col)
1007 insertColumn(col, true);
1011 void Tabular::appendColumn(col_type col)
1013 insertColumn(col, false);
1017 void Tabular::insertColumn(col_type const col, bool copy)
1019 bool const ct = buffer().params().track_changes;
1020 column_info.insert(column_info.begin() + col + 1, column_info[col]);
1022 for (row_type r = 0; r < nrows(); ++r) {
1023 cell_info[r].insert(cell_info[r].begin() + col + 1,
1024 copy ? cell_info[r][col] : CellData(buffer_));
1025 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
1026 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
1029 for (row_type r = 0; r < nrows(); ++r) {
1030 // inherit line settings
1031 idx_type const i = cellIndex(r, col + 1);
1032 idx_type const j = cellIndex(r, col);
1033 setBottomLine(i, bottomLine(j));
1034 setTopLine(i, topLine(j));
1035 setLeftLine(i, leftLine(j));
1036 setRightLine(i, rightLine(j));
1037 if (rightLine(i) && rightLine(j)) {
1038 setRightLine(j, false);
1042 column_info[col + 1].change.setInserted();
1048 void Tabular::updateIndexes()
1050 setBuffer(buffer());
1052 // reset cell number
1053 for (row_type row = 0; row < nrows(); ++row)
1054 for (col_type column = 0; column < ncols(); ++column) {
1055 if (!isPartOfMultiColumn(row, column)
1056 && !isPartOfMultiRow(row, column))
1058 if (isPartOfMultiRow(row, column))
1059 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1061 cell_info[row][column].cellno = numberofcells - 1;
1064 rowofcell.resize(numberofcells);
1065 columnofcell.resize(numberofcells);
1067 // reset column and row of cells and update their width, alignment, caption, and ct status
1068 for (row_type row = 0; row < nrows(); ++row) {
1069 for (col_type column = 0; column < ncols(); ++column) {
1070 cell_info[row][column].inset->toggleCaptionRow(is_long_tabular && ltCaption(row));
1071 if (isPartOfMultiColumn(row, column)) {
1072 cell_info[row][column].inset->toggleMultiCol(true);
1075 cell_info[row][column].inset->toggleMultiCol(false);
1076 // columnofcell needs to be called before setting width and alignment
1077 // multirow cells inherit the width from the column width
1078 if (!isPartOfMultiRow(row, column)) {
1079 columnofcell[i] = column;
1082 setFixedWidth(row, column);
1083 if (isPartOfMultiRow(row, column)) {
1084 cell_info[row][column].inset->toggleMultiRow(true);
1087 cell_info[row][column].inset->toggleMultiRow(false);
1088 cell_info[row][column].inset->setContentAlignment(
1089 getAlignment(cellIndex(row, column)));
1090 if (buffer().params().track_changes) {
1091 if (row_info[row].change.changed())
1092 cell_info[row][column].inset->setChange(row_info[row].change);
1093 if (column_info[column].change.changed())
1094 cell_info[row][column].inset->setChange(column_info[column].change);
1102 idx_type Tabular::numberOfCellsInRow(row_type const row) const
1104 idx_type result = 0;
1105 for (col_type c = 0; c < ncols(); ++c)
1106 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1112 bool Tabular::topLine(idx_type const cell) const
1114 return cellInfo(cell).top_line;
1118 bool Tabular::bottomLine(idx_type const cell) const
1120 return cellInfo(cell).bottom_line;
1124 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1126 if (use_booktabs && !ignore_bt)
1128 return cellInfo(cell).left_line;
1132 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1134 if (use_booktabs && !ignore_bt)
1136 return cellInfo(cell).right_line;
1140 bool Tabular::outsideBorders(
1141 row_type const sel_row_start, row_type const sel_row_end,
1142 col_type const sel_col_start, col_type const sel_col_end) const
1145 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
1146 if (!leftLine(cellIndex(r, sel_col_start))
1147 || !rightLine(cellIndex(r, sel_col_end)))
1150 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
1151 if (!topLine(cellIndex(sel_row_start, c))
1152 || !bottomLine(cellIndex(sel_row_end, c)))
1159 bool Tabular::innerBorders(
1160 row_type const sel_row_start, row_type const sel_row_end,
1161 col_type const sel_col_start, col_type const sel_col_end) const
1163 // Single cell has no inner borders
1164 if (sel_row_start == sel_row_end && sel_col_start == sel_col_end)
1166 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
1167 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
1168 idx_type const cell = cellIndex(r, c);
1169 if ((r != sel_row_start && !topLine(cell)
1170 && cell_info[r][c].multirow != CELL_PART_OF_MULTIROW)
1172 && c != sel_col_start && !leftLine(cell)
1173 && cell_info[r][c].multicolumn != CELL_PART_OF_MULTICOLUMN))
1180 void Tabular::setLines(
1181 row_type const sel_row_start, row_type const sel_row_end,
1182 col_type const sel_col_start, col_type const sel_col_end,
1183 bool setLinesInnerOnly, bool setLines)
1185 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
1186 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
1187 idx_type const cell = cellIndex(r, c);
1188 if (!(setLinesInnerOnly && r == sel_row_start)
1189 // for multirows, cell is taken care of at beginning
1190 && cell_info[r][c].multirow != CELL_PART_OF_MULTIROW)
1191 setTopLine(cell, setLines);
1192 if (!(setLinesInnerOnly && r == sel_row_end)
1193 && (r == sel_row_end || (!setLines
1194 // for multirows, cell is taken care of at the last part
1195 && cell_info[r + 1][c].multirow != CELL_PART_OF_MULTIROW)))
1196 setBottomLine(cell, setLines);
1197 if (!(setLinesInnerOnly && c == sel_col_start)
1198 // for multicolumns, cell is taken care of at beginning
1199 && cell_info[r][c].multicolumn != CELL_PART_OF_MULTICOLUMN)
1200 setLeftLine(cell, setLines);
1201 if (!(setLinesInnerOnly && c == sel_col_end)
1202 && (c == sel_col_end || (!setLines
1203 // for multicolumns, cell is taken care of at the last part
1204 && cell_info[r][c + 1].multicolumn != CELL_PART_OF_MULTICOLUMN)))
1205 setRightLine(cell, setLines);
1210 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1213 return make_pair(false, false);
1214 return make_pair(cellInfo(cell).top_line_ltrimmed,
1215 cellInfo(cell).top_line_rtrimmed);
1219 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1222 return make_pair(false, false);
1223 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1224 cellInfo(cell).bottom_line_rtrimmed);
1228 int Tabular::interRowSpace(row_type row) const
1230 if (!row || row >= nrows())
1233 int const interline_space = row_info[row - 1].interline_space_default ?
1234 default_line_space :
1235 row_info[row - 1].interline_space.inPixels(width());
1236 if (rowTopLine(row) && rowBottomLine(row - 1))
1237 return interline_space + WIDTH_OF_LINE;
1238 return interline_space;
1242 int Tabular::interColumnSpace(idx_type cell) const
1244 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1245 if (rightLine(cell) && nextcol < ncols()
1246 && leftLine(cellIndex(cellRow(cell), nextcol)))
1247 return WIDTH_OF_LINE;
1252 int Tabular::cellWidth(idx_type cell) const
1255 col_type const span = columnSpan(cell);
1256 col_type const col = cellColumn(cell);
1257 for(col_type c = col; c < col + span ; ++c)
1258 w += column_info[c].width;
1263 int Tabular::cellHeight(idx_type cell) const
1265 row_type const span = rowSpan(cell);
1266 row_type const row = cellRow(cell);
1268 for(row_type r = row; r < row + span ; ++r) {
1269 h += rowAscent(r) + rowDescent(r);
1270 if (r != row + span - 1)
1271 h += interRowSpace(r + 1);
1278 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1280 vector<int> max_dwidth(ncols(), 0);
1281 // collect max. fixed width of column
1282 map<col_type, int> max_pwidth;
1283 // collect max. variable width of column
1284 map<col_type, int> max_width;
1286 for(col_type c = 0; c < ncols(); ++c)
1287 for(row_type r = 0; r < nrows(); ++r) {
1288 idx_type const i = cellIndex(r, c);
1289 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1290 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1291 if (!getPWidth(i).zero())
1292 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1293 else if (!column_info[c].varwidth)
1294 max_width[c] = max(max_width[c], cell_info[r][c].width);
1297 // If we have a fixed tabular width, we take this into account
1298 Length tab_width = tabular_width;
1299 bool const tabularx = hasVarwidthColumn();
1300 if (tabularx && tab_width.zero())
1301 // If no tabular width is specified with X columns,
1302 // we use 100% colwidth
1303 tab_width = Length(100, Length::PCW);
1305 if (!tab_width.zero()) {
1306 restwidth = mi.base.inPixels(tab_width);
1307 // Subtract the fixed widths from the table width
1308 for (auto const & w : max_pwidth)
1309 restwidth -= w.second;
1312 // If we have a fixed width, distribute the available table width
1313 // (minus the fixed widths) to the variable-width columns
1315 int restcols = ncols() - max_pwidth.size();
1316 if ((restwidth > 0) && (restcols != 0))
1317 vcolwidth = restwidth / restcols;
1319 // Now consider that some variable width columns exceed the vcolwidth
1320 if (vcolwidth > 0) {
1321 bool changed = false;
1322 for (auto const & w : max_width) {
1323 if (tabularx || w.second > vcolwidth) {
1325 restwidth -= w.second;
1329 if (changed && restwidth > 0)
1330 vcolwidth = restwidth / restcols;
1333 bool update = false;
1334 // for each col get max of single col cells
1335 for(col_type c = 0; c < ncols(); ++c) {
1337 for(row_type r = 0; r < nrows(); ++r) {
1338 idx_type const i = cellIndex(r, c);
1339 if (columnSpan(i) == 1) {
1340 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1341 && cell_info[r][c].decimal_width != 0)
1342 new_width = max(new_width, cellInfo(i).width
1343 + max_dwidth[c] - cellInfo(i).decimal_width);
1344 else if (getPWidth(i).zero() && vcolwidth > 0) {
1345 if (tabularx && !column_info[c].varwidth)
1346 new_width = max(new_width, cellInfo(i).width);
1348 new_width = vcolwidth;
1350 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1352 new_width = max(new_width, cellInfo(i).width);
1356 if (column_info[c].width != new_width) {
1357 column_info[c].width = new_width;
1358 // Do not trigger update when no space is left for variable
1359 // columns, as this will loop
1360 update = tab_width.zero() || restwidth > 0;
1363 // update col widths to fit merged cells
1364 for(col_type c = 0; c < ncols(); ++c)
1365 for(row_type r = 0; r < nrows(); ++r) {
1366 idx_type const i = cellIndex(r, c);
1367 int const span = columnSpan(i);
1368 if (span == 1 || c > cellColumn(i))
1372 for(col_type j = c; j < c + span ; ++j)
1373 old_width += column_info[j].width;
1375 if (cellInfo(i).width > old_width) {
1376 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1377 // Do not trigger update when no space is left for variable
1378 // columns, as this will loop
1379 update = tab_width.zero() || restwidth > 0;
1387 int Tabular::width() const
1390 for (col_type c = 0; c < ncols(); ++c)
1391 width += column_info[c].width;
1396 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1399 col_type const col = cellColumn(cell);
1400 // set alignment for the whole row if we are not in a multicolumn cell,
1401 // exclude possible multicolumn cells in the row
1402 if (!isMultiColumn(cell)) {
1403 for (row_type r = 0; r < nrows(); ++r) {
1404 // only if the column has no width the multirow inherits the
1405 // alignment of the column, otherwise it is left aligned
1406 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1407 && !isMultiColumn(cellIndex(r, col))) {
1408 cell_info[r][col].alignment = align;
1409 cell_info[r][col].inset->setContentAlignment(align);
1411 if ((isMultiRow(cellIndex(r, col)) && has_width)
1412 && !isMultiColumn(cellIndex(r, col))) {
1413 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1414 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1417 column_info[col].alignment = align;
1418 docstring & dpoint = column_info[col].decimal_point;
1419 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1420 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1421 dpoint = tlang->decimalSeparator();
1424 cellInfo(cell).alignment = align;
1425 cellInset(cell)->setContentAlignment(align);
1430 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1433 if (!isMultiColumn(cell) || onlycolumn)
1434 column_info[cellColumn(cell)].valignment = align;
1436 cellInfo(cell).valignment = align;
1443 * Allow line and paragraph breaks for fixed width multirow cells
1444 * or disallow them, merge cell paragraphs and reset layout to standard
1445 * for variable width multicol cells.
1447 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1448 bool const fixedWidth, bool const multirow)
1450 inset->toggleFixedWidth(fixedWidth);
1454 BufferParams const & bp = cur.bv().buffer().params();
1457 // undo information has already been recorded
1458 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1459 bp.documentClass().plainLayoutName());
1466 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1467 Length const & width)
1469 col_type const c = cellColumn(cell);
1471 column_info[c].p_width = width;
1472 // reset the vertical alignment to top if the fixed width
1473 // is removed or zero because only fixed width columns can
1474 // have a vertical alignment
1475 if (column_info[c].p_width.zero())
1476 column_info[c].valignment = LYX_VALIGN_TOP;
1477 for (row_type r = 0; r < nrows(); ++r) {
1478 idx_type const cidx = cellIndex(r, c);
1479 // because of multicolumns
1480 toggleFixedWidth(cur, cellInset(cidx).get(),
1481 !getPWidth(cidx).zero(), isMultiRow(cidx));
1482 if (isMultiRow(cidx))
1483 setAlignment(cidx, column_info[c].alignment,
1484 !column_info[c].p_width.zero());
1486 // cur can become invalid after paragraphs were merged
1491 bool Tabular::setFixedWidth(row_type r, col_type c)
1493 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1494 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1495 || (multicol && !cell_info[r][c].p_width.zero());
1496 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1501 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1502 Length const & width)
1504 if (!isMultiColumn(cell))
1507 cellInfo(cell).p_width = width;
1508 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1510 // cur can become invalid after paragraphs were merged
1516 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1518 column_info[cellColumn(cell)].varwidth = varwidth;
1519 cellInset(cell).get()->toggleVarWidth(varwidth);
1524 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1526 cellInfo(cell).mroffset = mroffset;
1531 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1532 Tabular::Feature what)
1534 if (what == SET_SPECIAL_MULTICOLUMN)
1535 cellInfo(cell).align_special = special;
1537 column_info[cellColumn(cell)].align_special = special;
1541 void Tabular::setTopLine(idx_type i, bool line)
1543 cellInfo(i).top_line = line;
1547 void Tabular::setBottomLine(idx_type i, bool line)
1549 cellInfo(i).bottom_line = line;
1553 void Tabular::setTopLineLTrim(idx_type i, bool val)
1555 cellInfo(i).top_line_ltrimmed = val;
1559 void Tabular::setTopLineRTrim(idx_type i, bool val)
1561 cellInfo(i).top_line_rtrimmed = val;
1565 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1567 cellInfo(i).bottom_line_ltrimmed = val;
1571 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1573 cellInfo(i).bottom_line_rtrimmed = val;
1577 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1579 setTopLineLTrim(i, trim.first);
1580 setTopLineRTrim(i, trim.second);
1583 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1585 setBottomLineLTrim(i, trim.first);
1586 setBottomLineRTrim(i, trim.second);
1589 void Tabular::setLeftLine(idx_type cell, bool line)
1591 cellInfo(cell).left_line = line;
1595 void Tabular::setRightLine(idx_type cell, bool line)
1597 cellInfo(cell).right_line = line;
1600 bool Tabular::rowTopLine(row_type r) const
1602 bool all_rows_set = true;
1603 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1604 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1605 return all_rows_set;
1609 bool Tabular::rowBottomLine(row_type r) const
1611 bool all_rows_set = true;
1612 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1613 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1614 return all_rows_set;
1618 bool Tabular::columnLeftLine(col_type c) const
1625 for (row_type r = 0; r < nrows(); ++r) {
1626 idx_type const i = cellIndex(r, c);
1627 if (c == cellColumn(i)) {
1629 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1630 if (cellInfo(i).left_line || right)
1634 return 2 * nrows_left >= total;
1638 bool Tabular::columnRightLine(col_type c) const
1643 int nrows_right = 0;
1645 for (row_type r = 0; r < nrows(); ++r) {
1646 idx_type i = cellIndex(r, c);
1647 if (c == cellColumn(i) + columnSpan(i) - 1) {
1649 bool left = (c + 1 < ncols()
1650 && cellInfo(cellIndex(r, c + 1)).left_line)
1651 || c + 1 == ncols();
1652 if (cellInfo(i).right_line && left)
1656 return 2 * nrows_right >= total;
1660 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1662 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1663 return cellInfo(cell).alignment;
1665 return column_info[cellColumn(cell)].alignment;
1670 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1672 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1673 return cellInfo(cell).valignment;
1674 return column_info[cellColumn(cell)].valignment;
1678 int Tabular::offsetVAlignment() const
1680 // for top-alignment the first horizontal table line must be exactly at
1681 // the position of the base line of the surrounding text line
1682 // for bottom alignment, the same is for the last table line
1683 int offset_valign = 0;
1684 switch (tabular_valignment) {
1685 case Tabular::LYX_VALIGN_BOTTOM:
1686 offset_valign = rowAscent(0) - height();
1688 case Tabular::LYX_VALIGN_MIDDLE:
1689 offset_valign = (- height()) / 2 + rowAscent(0);
1691 case Tabular::LYX_VALIGN_TOP:
1692 offset_valign = rowAscent(0);
1695 return offset_valign;
1699 Length const Tabular::getPWidth(idx_type cell) const
1701 if (isMultiColumn(cell))
1702 return cellInfo(cell).p_width;
1703 return column_info[cellColumn(cell)].p_width;
1707 Length const Tabular::getMROffset(idx_type cell) const
1709 return cellInfo(cell).mroffset;
1713 int Tabular::textHOffset(idx_type cell) const
1715 // the LaTeX Way :-(
1716 int x = WIDTH_OF_LINE;
1718 int const w = cellWidth(cell) - cellInfo(cell).width;
1720 switch (getAlignment(cell)) {
1721 case LYX_ALIGN_CENTER:
1724 case LYX_ALIGN_RIGHT:
1727 case LYX_ALIGN_DECIMAL: {
1728 // we center when no decimal point
1729 if (cellInfo(cell).decimal_width == 0) {
1733 col_type const c = cellColumn(cell);
1734 int max_dhoffset = 0;
1735 for(row_type r = 0; r < row_info.size() ; ++r) {
1736 idx_type const i = cellIndex(r, c);
1737 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1738 && cellInfo(i).decimal_width != 0)
1739 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1741 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1744 // LYX_ALIGN_LEFT: nothing :-)
1752 int Tabular::textVOffset(idx_type cell) const
1754 int voffset = cellInfo(cell).voffset;
1755 if (isMultiRow(cell)) {
1756 row_type const row = cellRow(cell);
1757 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1763 idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1766 idx_type const numcells = numberOfCellsInRow(row);
1767 // we check against numcells to make sure we do not crash if all the
1768 // cells are multirow (bug #7535), but in that case our return value
1769 // is really invalid, i.e., it is NOT the first cell in the row. but
1770 // i do not know what to do here. (rgh)
1771 while (c < numcells - 1
1772 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1773 || (ct && column_info[c].change.deleted())))
1775 return cell_info[row][c].cellno;
1779 idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1781 col_type c = ncols() - 1;
1782 // of course we check against 0 so we don't crash. but we have the same
1783 // problem as in the previous routine: if all the cells are part of a
1784 // multirow or part of a multi column, then our return value is invalid.
1786 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1787 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1788 || (ct && column_info[c].change.deleted())))
1790 return cell_info[row][c].cellno;
1794 row_type Tabular::getFirstRow(bool const ct) const
1799 // exclude deleted rows if ct == true
1800 while (r < nrows() && row_info[r].change.deleted())
1806 row_type Tabular::getLastRow(bool const ct) const
1808 row_type r = nrows() - 1;
1811 // exclude deleted rows if ct == true
1812 while (r > 0 && row_info[r].change.deleted())
1818 row_type Tabular::cellRow(idx_type cell) const
1820 if (cell >= numberofcells)
1824 return rowofcell[cell];
1828 col_type Tabular::cellColumn(idx_type cell) const
1830 if (cell >= numberofcells)
1834 return columnofcell[cell];
1838 void Tabular::write(ostream & os) const
1842 << write_attribute("version", 3)
1843 << write_attribute("rows", nrows())
1844 << write_attribute("columns", ncols())
1846 // global longtable options
1848 << write_attribute("rotate", rotate)
1849 << write_attribute("booktabs", use_booktabs)
1850 << write_attribute("islongtable", is_long_tabular)
1851 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1852 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1853 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1854 << write_attribute("headTopDL", endhead.topDL)
1855 << write_attribute("headBottomDL", endhead.bottomDL)
1856 << write_attribute("footTopDL", endfoot.topDL)
1857 << write_attribute("footBottomDL", endfoot.bottomDL)
1858 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1859 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1860 << write_attribute("lastFootEmpty", endlastfoot.empty);
1861 // longtables cannot be aligned vertically
1862 if (!is_long_tabular)
1863 os << write_attribute("tabularvalignment", tabular_valignment);
1864 os << write_attribute("tabularwidth", tabular_width);
1865 if (is_long_tabular)
1866 os << write_attribute("longtabularalignment", longtabular_alignment);
1868 for (col_type c = 0; c < ncols(); ++c) {
1870 << write_attribute("alignment", column_info[c].alignment);
1871 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1872 os << write_attribute("decimal_point", column_info[c].decimal_point);
1873 os << write_attribute("change", column_info[c].change, buffer().params())
1874 << write_attribute("valignment", column_info[c].valignment)
1875 << write_attribute("width", column_info[c].p_width.asString())
1876 << write_attribute("varwidth", column_info[c].varwidth)
1877 << write_attribute("special", column_info[c].align_special)
1880 for (row_type r = 0; r < nrows(); ++r) {
1881 static const string def("default");
1883 if (row_info[r].top_space_default)
1884 os << write_attribute("topspace", def);
1886 os << write_attribute("topspace", row_info[r].top_space);
1887 if (row_info[r].bottom_space_default)
1888 os << write_attribute("bottomspace", def);
1890 os << write_attribute("bottomspace", row_info[r].bottom_space);
1891 if (row_info[r].interline_space_default)
1892 os << write_attribute("interlinespace", def);
1894 os << write_attribute("interlinespace", row_info[r].interline_space);
1895 os << write_attribute("change", row_info[r].change, buffer().params())
1896 << write_attribute("endhead", row_info[r].endhead)
1897 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1898 << write_attribute("endfoot", row_info[r].endfoot)
1899 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1900 << write_attribute("newpage", row_info[r].newpage)
1901 << write_attribute("caption", row_info[r].caption)
1903 for (col_type c = 0; c < ncols(); ++c) {
1905 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1906 << write_attribute("multirow", cell_info[r][c].multirow)
1907 << write_attribute("mroffset", cell_info[r][c].mroffset)
1908 << write_attribute("alignment", cell_info[r][c].alignment)
1909 << write_attribute("valignment", cell_info[r][c].valignment)
1910 << write_attribute("topline", cell_info[r][c].top_line)
1911 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1912 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1913 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1914 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1915 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1916 << write_attribute("leftline", cell_info[r][c].left_line)
1917 << write_attribute("rightline", cell_info[r][c].right_line)
1918 << write_attribute("rotate", cell_info[r][c].rotate)
1919 << write_attribute("usebox", cell_info[r][c].usebox)
1920 << write_attribute("width", cell_info[r][c].p_width)
1921 << write_attribute("special", cell_info[r][c].align_special)
1923 os << "\\begin_inset ";
1924 cell_info[r][c].inset->write(os);
1925 os << "\n\\end_inset\n"
1927 // FIXME This can be removed again once the mystery
1928 // crash has been resolved.
1933 os << "</lyxtabular>\n";
1937 void Tabular::read(Lexer & lex)
1940 istream & is = lex.getStream();
1942 l_getline(is, line);
1943 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1944 LASSERT(false, return);
1948 if (!getTokenValue(line, "version", version))
1950 LATTEST(version >= 2);
1953 if (!getTokenValue(line, "rows", rows_arg))
1956 if (!getTokenValue(line, "columns", columns_arg))
1958 init(buffer_, rows_arg, columns_arg);
1959 l_getline(is, line);
1960 if (!prefixIs(line, "<features")) {
1961 lyxerr << "Wrong tabular format (expected <features ...> got"
1962 << line << ')' << endl;
1965 getTokenValue(line, "rotate", rotate);
1966 getTokenValue(line, "booktabs", use_booktabs);
1967 getTokenValue(line, "islongtable", is_long_tabular);
1968 getTokenValue(line, "tabularvalignment", tabular_valignment);
1969 getTokenValue(line, "tabularwidth", tabular_width);
1970 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1971 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1972 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1973 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1974 getTokenValue(line, "headTopDL", endhead.topDL);
1975 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1976 getTokenValue(line, "footTopDL", endfoot.topDL);
1977 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1978 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1979 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1980 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1982 for (col_type c = 0; c < ncols(); ++c) {
1984 if (!prefixIs(line,"<column")) {
1985 lyxerr << "Wrong tabular format (expected <column ...> got"
1986 << line << ')' << endl;
1989 getTokenValue(line, "alignment", column_info[c].alignment);
1990 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1991 getTokenValue(line, "valignment", column_info[c].valignment);
1992 getTokenValue(line, "width", column_info[c].p_width);
1993 getTokenValue(line, "special", column_info[c].align_special);
1994 getTokenValue(line, "varwidth", column_info[c].varwidth);
1995 getTokenValue(line, "change", column_info[c].change, buffer().params());
1998 for (row_type i = 0; i < nrows(); ++i) {
1999 l_getline(is, line);
2000 if (!prefixIs(line, "<row")) {
2001 lyxerr << "Wrong tabular format (expected <row ...> got"
2002 << line << ')' << endl;
2005 getTokenValue(line, "topspace", row_info[i].top_space,
2006 row_info[i].top_space_default);
2007 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
2008 row_info[i].bottom_space_default);
2009 getTokenValue(line, "interlinespace", row_info[i].interline_space,
2010 row_info[i].interline_space_default);
2011 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
2012 getTokenValue(line, "endhead", row_info[i].endhead);
2013 getTokenValue(line, "endfoot", row_info[i].endfoot);
2014 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
2015 getTokenValue(line, "newpage", row_info[i].newpage);
2016 getTokenValue(line, "caption", row_info[i].caption);
2017 getTokenValue(line, "change", row_info[i].change, buffer().params());
2018 for (col_type j = 0; j < ncols(); ++j) {
2019 l_getline(is, line);
2020 if (!prefixIs(line, "<cell")) {
2021 lyxerr << "Wrong tabular format (expected <cell ...> got"
2022 << line << ')' << endl;
2025 cell_info[i][j].inset->toggleCaptionRow(is_long_tabular && row_info[i].caption);
2026 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
2027 getTokenValue(line, "multirow", cell_info[i][j].multirow);
2028 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
2029 getTokenValue(line, "alignment", cell_info[i][j].alignment);
2030 getTokenValue(line, "valignment", cell_info[i][j].valignment);
2031 getTokenValue(line, "topline", cell_info[i][j].top_line);
2032 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
2033 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
2034 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
2035 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
2036 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
2037 getTokenValue(line, "leftline", cell_info[i][j].left_line);
2038 getTokenValue(line, "rightline", cell_info[i][j].right_line);
2039 getTokenValue(line, "rotate", cell_info[i][j].rotate);
2040 getTokenValue(line, "usebox", cell_info[i][j].usebox);
2041 getTokenValue(line, "width", cell_info[i][j].p_width);
2043 getTokenValue(line, "special", cell_info[i][j].align_special);
2044 l_getline(is, line);
2045 if (prefixIs(line, "\\begin_inset")) {
2046 cell_info[i][j].inset->setBuffer(*buffer_);
2047 cell_info[i][j].inset->read(lex);
2048 l_getline(is, line);
2050 if (!prefixIs(line, "</cell>")) {
2051 lyxerr << "Wrong tabular format (expected </cell> got"
2052 << line << ')' << endl;
2056 l_getline(is, line);
2057 if (!prefixIs(line, "</row>")) {
2058 lyxerr << "Wrong tabular format (expected </row> got"
2059 << line << ')' << endl;
2063 while (!prefixIs(line, "</lyxtabular>")) {
2064 l_getline(is, line);
2070 bool Tabular::isMultiColumn(idx_type cell) const
2072 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2073 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
2077 bool Tabular::hasMultiColumn(col_type c) const
2079 for (row_type r = 0; r < nrows(); ++r) {
2080 if (isMultiColumn(cellIndex(r, c)))
2087 bool Tabular::hasVarwidthColumn() const
2089 for (col_type c = 0; c < ncols(); ++c) {
2090 if (column_info[c].varwidth)
2097 bool Tabular::isVTypeColumn(col_type c) const
2099 for (row_type r = 0; r < nrows(); ++r) {
2100 idx_type idx = cellIndex(r, c);
2101 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH
2102 && getAlignment(idx) == LYX_ALIGN_LEFT
2103 && getVAlignment(idx) == LYX_VALIGN_TOP)
2110 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
2112 return cell_info[cellRow(cell)][cellColumn(cell)];
2116 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2118 return cell_info[cellRow(cell)][cellColumn(cell)];
2122 idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2123 bool const right_border)
2125 idx_type const col = cellColumn(cell);
2126 idx_type const row = cellRow(cell);
2127 for (idx_type i = 0; i < number; ++i)
2128 unsetMultiRow(cellIndex(row, col + i));
2130 // unsetting of multirow may have invalidated cell index
2131 cell = cellIndex(row, col);
2132 CellData & cs = cellInfo(cell);
2133 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2134 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2135 cs.alignment = column_info[col].alignment;
2136 setRightLine(cell, right_border);
2137 // non-fixed width multicolumns cannot have multiple paragraphs
2138 if (getPWidth(cell).zero()) {
2139 toggleFixedWidth(cur, cellInset(cell).get(),
2140 !getPWidth(cell).zero(), isMultiRow(cell));
2141 // cur can become invalid after paragraphs were merged
2145 idx_type lastcell = cellIndex(row, col + number - 1);
2146 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2147 CellData & cs1 = cellInfo(cell + i);
2148 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2149 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2157 bool Tabular::isMultiRow(idx_type cell) const
2159 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2160 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2163 bool Tabular::hasMultiRow(row_type r) const
2165 for (col_type c = 0; c < ncols(); ++c) {
2166 if (isMultiRow(cellIndex(r, c)))
2172 idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2173 bool const bottom_border,
2174 LyXAlignment const halign)
2176 idx_type const col = cellColumn(cell);
2177 idx_type const row = cellRow(cell);
2178 for (idx_type i = 0; i < number; ++i)
2179 unsetMultiColumn(cellIndex(row + i, col));
2181 // unsetting of multirow may have invalidated cell index
2182 cell = cellIndex(row, col);
2183 CellData & cs = cellInfo(cell);
2184 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2185 cs.valignment = LYX_VALIGN_MIDDLE;
2186 // the horizontal alignment of multirow cells can only
2187 // be changed for the whole table row,
2188 // support changing this only for the multirow cell can be done via
2190 if (getPWidth(cell).zero())
2191 cs.alignment = halign;
2193 cs.alignment = LYX_ALIGN_LEFT;
2195 // Multirows cannot have multiple paragraphs
2196 if (getPWidth(cell).zero()) {
2197 toggleFixedWidth(cur, cellInset(cell).get(),
2198 !getPWidth(cell).zero(),
2200 // cur can become invalid after paragraphs were merged
2204 // set the bottom line of the last selected cell
2205 setBottomLine(cell, bottom_border);
2207 for (idx_type i = 1; i < number; ++i) {
2208 CellData & cs1 = cell_info[row + i][col];
2209 cs1.multirow = CELL_PART_OF_MULTIROW;
2210 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2218 idx_type Tabular::columnSpan(idx_type cell) const
2220 row_type const row = cellRow(cell);
2221 col_type const col = cellColumn(cell);
2223 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2230 idx_type Tabular::rowSpan(idx_type cell) const
2232 col_type const column = cellColumn(cell);
2233 col_type row = cellRow(cell) + 1;
2234 while (row < nrows() && isPartOfMultiRow(row, column))
2237 return row - cellRow(cell);
2241 void Tabular::unsetMultiColumn(idx_type cell)
2243 if (!isMultiColumn(cell))
2246 row_type const row = cellRow(cell);
2247 col_type const col = cellColumn(cell);
2248 row_type const span = columnSpan(cell);
2249 for (col_type c = 0; c < span; ++c) {
2250 // in the table dialog the lines are set in every case
2251 // when unsetting a multicolumn this leads to an additional right
2252 // line for every cell that was part of the former multicolumn cell,
2253 // except if the cell is in the last column
2254 // therefore remove this line
2255 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2256 && (col + c) < (col + span - 1))
2257 cell_info[row][col + c].right_line = false;
2258 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2264 void Tabular::unsetMultiRow(idx_type cell)
2266 if (!isMultiRow(cell))
2269 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2270 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2271 row_type const row = cellRow(cell);
2272 col_type const col = cellColumn(cell);
2273 row_type const span = rowSpan(cell);
2274 for (row_type r = 0; r < span; ++r)
2275 cell_info[row + r][col].multirow = CELL_NORMAL;
2280 void Tabular::setRotateCell(idx_type cell, int value)
2282 cellInfo(cell).rotate = value;
2286 int Tabular::getRotateCell(idx_type cell) const
2288 return cellInfo(cell).rotate;
2292 bool Tabular::needRotating() const
2294 if (rotate && !is_long_tabular)
2296 for (row_type r = 0; r < nrows(); ++r)
2297 for (col_type c = 0; c < ncols(); ++c)
2298 if (cell_info[r][c].rotate != 0)
2304 bool Tabular::isLastCell(idx_type cell) const
2306 return cell + 1 >= numberofcells;
2310 idx_type Tabular::cellAbove(idx_type cell) const
2312 if (cellRow(cell) == 0)
2315 col_type const col = cellColumn(cell);
2316 row_type r = cellRow(cell) - 1;
2317 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2320 return cell_info[r][col].cellno;
2324 idx_type Tabular::cellBelow(idx_type cell) const
2326 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2327 if (nextrow < nrows())
2328 return cell_info[nextrow][cellColumn(cell)].cellno;
2333 idx_type Tabular::cellIndex(row_type row, col_type column) const
2335 LASSERT(column != npos && column < ncols(), column = 0);
2336 LASSERT(row != npos && row < nrows(), row = 0);
2337 return cell_info[row][column].cellno;
2341 void Tabular::setUsebox(idx_type cell, BoxType type)
2343 cellInfo(cell).usebox = type;
2347 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2349 if (getRotateCell(cell) == 0
2350 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2351 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2353 if (cellInfo(cell).usebox > 1)
2354 return cellInfo(cell).usebox;
2355 return useBox(cell);
2360 // This are functions used for the longtable support
2362 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2368 row_info[row].endfirsthead = flag;
2372 row_info[row].endhead = flag;
2377 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2380 hd.set = haveLTHead();
2381 return row_info[row].endhead;
2385 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2388 hd.set = haveLTFirstHead();
2389 return row_info[row].endfirsthead;
2393 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2399 row_info[row].endlastfoot = flag;
2403 row_info[row].endfoot = flag;
2408 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2411 fd.set = haveLTFoot();
2412 return row_info[row].endfoot;
2416 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2419 fd.set = haveLTLastFoot();
2420 return row_info[row].endlastfoot;
2424 void Tabular::setLTNewPage(row_type row, bool what)
2426 row_info[row].newpage = what;
2430 bool Tabular::getLTNewPage(row_type row) const
2432 return row_info[row].newpage;
2436 bool Tabular::haveLTHead(bool withcaptions) const
2438 if (!is_long_tabular)
2440 for (row_type i = 0; i < nrows(); ++i)
2441 if (row_info[i].endhead &&
2442 (withcaptions || !row_info[i].caption))
2448 bool Tabular::haveLTFirstHead(bool withcaptions) const
2450 if (!is_long_tabular || endfirsthead.empty)
2452 for (row_type r = 0; r < nrows(); ++r)
2453 if (row_info[r].endfirsthead &&
2454 (withcaptions || !row_info[r].caption))
2460 bool Tabular::haveLTFoot(bool withcaptions) const
2462 if (!is_long_tabular)
2464 for (row_type r = 0; r < nrows(); ++r)
2465 if (row_info[r].endfoot &&
2466 (withcaptions || !row_info[r].caption))
2472 bool Tabular::haveLTLastFoot(bool withcaptions) const
2474 if (!is_long_tabular || endlastfoot.empty)
2476 for (row_type r = 0; r < nrows(); ++r)
2477 if (row_info[r].endlastfoot &&
2478 (withcaptions || !row_info[r].caption))
2484 idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2486 idx_type i = getFirstCellInRow(row);
2488 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2489 setTopLine(i, false);
2490 setBottomLine(i, false);
2491 setLeftLine(i, false);
2492 setRightLine(i, false);
2493 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2494 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2495 setLTHead(row, true, endfirsthead, true);
2496 row_info[row].endfirsthead = true;
2499 unsetMultiColumn(i);
2500 // When unsetting a caption row, also all existing
2501 // captions in this row must be dissolved.
2503 row_info[row].caption = what;
2508 bool Tabular::ltCaption(row_type row) const
2510 return row_info[row].caption;
2514 bool Tabular::haveLTCaption(CaptionType captiontype) const
2516 if (!is_long_tabular)
2518 for (row_type r = 0; r < nrows(); ++r) {
2519 if (row_info[r].caption) {
2520 switch (captiontype) {
2521 case CAPTION_FIRSTHEAD:
2522 if (row_info[r].endfirsthead)
2526 if (row_info[r].endhead)
2530 if (row_info[r].endfoot)
2533 case CAPTION_LASTFOOT:
2534 if (row_info[r].endlastfoot)
2546 // end longtable support functions
2548 void Tabular::setRowAscent(row_type row, int height)
2550 if (row >= nrows() || row_info[row].ascent == height)
2552 row_info[row].ascent = height;
2556 void Tabular::setRowDescent(row_type row, int height)
2558 if (row >= nrows() || row_info[row].descent == height)
2560 row_info[row].descent = height;
2564 int Tabular::rowAscent(row_type row) const
2566 LASSERT(row < nrows(), row = 0);
2567 return row_info[row].ascent;
2571 int Tabular::rowDescent(row_type row) const
2573 LASSERT(row < nrows(), row = 0);
2574 return row_info[row].descent;
2578 int Tabular::height() const
2581 for (row_type row = 0; row < nrows(); ++row)
2582 height += rowAscent(row) + rowDescent(row) +
2588 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2590 LASSERT(row < nrows(), return false);
2591 LASSERT(column < ncols(), return false);
2592 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2596 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2598 LASSERT(row < nrows(), return false);
2599 LASSERT(column < ncols(), return false);
2600 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2604 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> const & columns,
2605 list<col_type> const & logical_columns) const
2607 // we only output complete row lines and the 1st row here, the rest
2608 // is done in Tabular::TeXBottomHLine(...)
2610 // get for each column the topline (if any)
2611 map<col_type, bool> topline, topltrims, toprtrims;
2613 bool have_trims = false;
2614 for (auto const & c : columns) {
2615 topline[c] = topLine(cellIndex(row, c));
2616 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2617 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2618 // If cell is part of a multirow and not the first cell of the
2619 // multirow, no line must be drawn.
2621 if (isMultiRow(cellIndex(row, c))
2622 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2624 topltrims[c] = false;
2625 toprtrims[c] = false;
2627 // copy trimming to multicolumn parts
2628 if (isPartOfMultiColumn(row, c)) {
2629 topltrims[c] = topltrims[c-1];
2630 toprtrims[c] = toprtrims[c-1];
2632 if (topline.find(c) != topline.end() && topline.find(c)->second)
2634 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2635 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2639 // do nothing if empty first row, or incomplete row line after
2640 row_type first = getFirstRow(!buffer().params().output_changes);
2641 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2644 // Is this the actual first row (excluding longtable caption row)?
2645 bool const realfirstrow = (row == first
2646 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2648 // only output complete row lines and the 1st row's clines
2649 if (nset == columns.size() && !have_trims) {
2651 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2655 } else if (realfirstrow || have_trims) {
2656 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2658 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2659 std::list<col_type>::const_iterator it2 = columns.begin();
2660 // We need to iterate over the logical columns here, but take care for
2662 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2667 if (topline.find(c)->second) {
2668 col_type offset = 0;
2669 for (col_type j = 0 ; j < c; ++j)
2670 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2672 // If the two iterators differ, we are in bidi with swapped columns
2673 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2674 while (isPartOfMultiColumn(row, c))
2677 if (topltrims.find(c) != topltrims.end()
2678 && topltrims.find(c)->second)
2680 col_type cstart = c;
2681 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2682 if (isMultiColumn(cellIndex(row, c))
2683 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2685 if (c > cstart && topltrims.find(c) != topltrims.end()
2686 && topltrims.find(c)->second) {
2687 if (!isPartOfMultiColumn(row, c))
2690 } else if (toprtrims.find(c) != toprtrims.end()
2691 && toprtrims.find(c)->second)
2695 for (col_type j = cstart ; j <= c ; ++j)
2696 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2698 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2699 if (toprtrims.find(c) != toprtrims.end()
2700 && toprtrims.find(c)->second)
2705 os << "(" << trim << ")";
2706 if (firstcol > lastcol)
2707 // This can happen with bidi (swapped columns)
2708 os << "{" << lastcol << '-' << firstcol << "}";
2710 os << "{" << firstcol << '-' << lastcol << "}";
2711 if (c == columns.size() - 1)
2721 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> const & columns,
2722 list<col_type> const & logical_columns) const
2724 // we output bottomlines of row r and the toplines of row r+1
2725 // if the latter do not span the whole tabular
2727 // get the bottomlines of row r, and toplines in next row
2728 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2729 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2730 bool nextrowset = true;
2731 for (auto const & c : columns) {
2732 idx_type const idx = cellIndex(row, c);
2733 bottomline[c] = bottomLine(cellIndex(row, c));
2734 bottomltrims[c] = bottomLineTrim(idx).first;
2735 bottomrtrims[c] = bottomLineTrim(idx).second;
2736 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2737 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2738 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2739 // If cell is part of a multirow and not the last cell of the
2740 // multirow, no line must be drawn.
2742 if (isMultiRow(cellIndex(row, c))
2743 && isMultiRow(cellIndex(row + 1, c))
2744 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2745 bottomline[c] = false;
2747 bottomltrims[c] = false;
2748 bottomrtrims[c] = false;
2749 topltrims[c] = false;
2750 toprtrims[c] = false;
2752 // copy trimming in multicolumn parts
2753 if (isPartOfMultiColumn(row, c)) {
2754 topltrims[c] = topltrims[c-1];
2755 toprtrims[c] = toprtrims[c-1];
2756 bottomltrims[c] = bottomltrims[c-1];
2757 bottomrtrims[c] = bottomrtrims[c-1];
2760 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2763 // combine this row's bottom lines and next row's toplines if necessary
2765 bool have_trims = false;
2766 for (auto const & c : columns) {
2768 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2769 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2770 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2771 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2772 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2773 if (bottomline.find(c)->second)
2775 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2776 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2780 // do nothing if empty, OR incomplete row line with a topline in next row
2781 if (nset == 0 || (nextrowset && nset != columns.size()))
2784 if (nset == columns.size() && !have_trims) {
2786 os << (lastrow ? "\\bottomrule" : "\\midrule");
2790 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2792 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2793 std::list<col_type>::const_iterator it2 = columns.begin();
2794 // We need to iterate over the logical columns here, but take care for
2796 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2801 if (bottomline.find(c)->second) {
2802 col_type offset = 0;
2803 for (col_type j = 0 ; j < c; ++j)
2804 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2806 // If the two iterators differ, we are in bidi with swapped columns
2807 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2808 while (isPartOfMultiColumn(row, c))
2811 if (bottomltrims.find(c) != bottomltrims.end()
2812 && bottomltrims.find(c)->second)
2814 col_type cstart = c;
2815 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2816 if (isMultiColumn(cellIndex(row, c))
2818 && isPartOfMultiColumn(row, c + 1))
2821 && bottomltrims.find(c) != bottomltrims.end()
2822 && bottomltrims.find(c)->second) {
2823 if (!isPartOfMultiColumn(row, c))
2826 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2827 && bottomrtrims.find(c)->second)
2831 for (col_type j = cstart ; j <= c ; ++j)
2832 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2834 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2835 if (bottomrtrims.find(c) != bottomrtrims.end()
2836 && bottomrtrims.find(c)->second)
2841 os << "(" << trim << ")";
2842 if (firstcol > lastcol)
2843 // This can happen with bidi (swapped columns)
2844 os << "{" << lastcol << '-' << firstcol << "}";
2846 os << "{" << firstcol << '-' << lastcol << "}";
2847 if (c == columns.size() - 1)
2857 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2858 bool & ismulticol, bool & ismultirow,
2859 bool const bidi) const
2861 row_type const r = cellRow(cell);
2862 if (is_long_tabular && row_info[r].caption)
2865 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2866 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2867 // figure out how to set the lines
2868 // we always set double lines to the right of the cell
2869 // or left in bidi RTL, respectively.
2870 col_type const c = cellColumn(cell);
2871 col_type const nextcol = c + columnSpan(cell);
2872 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2873 bool colright = columnRightLine(c);
2874 bool colleft = columnLeftLine(c);
2875 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2876 bool nextcellleft = nextcol < ncols()
2877 && leftLine(cellIndex(r, nextcol));
2878 bool coldouble = colright && nextcolleft;
2879 bool celldouble = rightLine(cell) && nextcellleft;
2881 ismulticol = (isMultiColumn(cell)
2882 || (c == 0 && colleft != leftLine(cell))
2883 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2884 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2885 || (coldouble != celldouble))
2888 // we center in multicol when no decimal point
2890 docstring const align_d = column_info[c].decimal_point;
2891 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2892 bool const nosep = !dit;
2893 ismulticol |= nosep;
2894 celldouble &= nosep;
2897 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2898 int latexcolspan = columnSpan(cell);
2899 for (col_type col = c; col < c + columnSpan(cell); ++col)
2900 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2904 os << "\\multicolumn{" << latexcolspan << "}{";
2905 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2906 || (!bidi && c == 0 && leftLine(cell))))
2908 if (bidi && celldouble)
2909 // add extra vertical line if we want a double one
2911 if (!cellInfo(cell).align_special.empty()) {
2912 os << cellInfo(cell).align_special;
2914 if (!getPWidth(cell).zero()) {
2916 case LYX_ALIGN_LEFT:
2917 os << ">{\\raggedright}";
2919 case LYX_ALIGN_RIGHT:
2920 os << ">{\\raggedleft}";
2922 case LYX_ALIGN_CENTER:
2923 os << ">{\\centering}";
2929 case LYX_VALIGN_TOP:
2932 case LYX_VALIGN_MIDDLE:
2935 case LYX_VALIGN_BOTTOM:
2940 << from_ascii(getPWidth(cell).asLatexString())
2943 if ((getRotateCell(cell) == 0 && useBox(cell) == BOX_VARWIDTH
2944 && align == LYX_ALIGN_LEFT))
2945 os << "V{\\linewidth}";
2948 case LYX_ALIGN_LEFT:
2951 case LYX_ALIGN_RIGHT:
2959 } // end if else !getPWidth
2960 } // end if else !cellinfo_of_cell
2961 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2963 if (!bidi && celldouble)
2964 // add extra vertical line if we want a double one
2967 } // end if ismulticol
2969 // we only need code for the first multirow cell
2970 ismultirow = isMultiRow(cell);
2972 os << "\\multirow{" << rowSpan(cell) << "}{";
2973 if (!getPWidth(cell).zero())
2974 os << from_ascii(getPWidth(cell).asLatexString());
2976 if (column_info[c].varwidth)
2977 // this inherits varwidth size
2980 // we need to set a default value
2984 if (!getMROffset(cell).zero())
2985 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2987 } // end if ismultirow
2989 if (getRotateCell(cell) != 0)
2990 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2992 if (getUsebox(cell) == BOX_PARBOX) {
2995 case LYX_VALIGN_TOP:
2998 case LYX_VALIGN_MIDDLE:
3001 case LYX_VALIGN_BOTTOM:
3005 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
3007 } else if (getUsebox(cell) == BOX_MINIPAGE) {
3008 os << "\\begin{minipage}[";
3010 case LYX_VALIGN_TOP:
3013 case LYX_VALIGN_MIDDLE:
3016 case LYX_VALIGN_BOTTOM:
3020 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
3022 } else if (getUsebox(cell) == BOX_VARWIDTH
3023 && (getRotateCell(cell) != 0 || align != LYX_ALIGN_LEFT
3024 || valign != LYX_VALIGN_TOP || hasNewlines(cell))) {
3025 os << "\\begin{cellvarwidth}[";
3027 case LYX_VALIGN_TOP:
3030 case LYX_VALIGN_MIDDLE:
3033 case LYX_VALIGN_BOTTOM:
3039 case LYX_ALIGN_RIGHT:
3040 os << "\\raggedleft\n";
3042 case LYX_ALIGN_CENTER:
3043 os << "\\centering\n";
3045 case LYX_ALIGN_LEFT:
3046 //os << "\\narrowragged\n";
3048 case LYX_ALIGN_BLOCK:
3049 case LYX_ALIGN_DECIMAL:
3050 case LYX_ALIGN_NONE:
3051 case LYX_ALIGN_LAYOUT:
3052 case LYX_ALIGN_SPECIAL:
3059 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
3060 bool ismulticol, bool ismultirow) const
3062 row_type const r = cellRow(cell);
3063 if (is_long_tabular && row_info[r].caption)
3067 if (getUsebox(cell) == BOX_PARBOX)
3069 else if (getUsebox(cell) == BOX_MINIPAGE)
3070 os << breakln << "\\end{minipage}";
3071 else if (getUsebox(cell) == BOX_VARWIDTH
3072 && (getRotateCell(cell) != 0 || getAlignment(cell) != LYX_ALIGN_LEFT
3073 || getVAlignment(cell) != LYX_VALIGN_TOP || hasNewlines(cell)))
3074 os << breakln << "\\end{cellvarwidth}";
3075 if (getRotateCell(cell) != 0)
3076 os << breakln << "\\end{turn}";
3084 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
3085 OutputParams const & runparams,
3086 list<col_type> const & columns,
3087 list<col_type> const & logical_columns) const
3089 if (!is_long_tabular)
3093 // output caption which is in no header or footer
3094 if (haveLTCaption()) {
3095 for (row_type r = 0; r < nrows(); ++r) {
3096 if (row_info[r].caption &&
3097 !row_info[r].endfirsthead && !row_info[r].endhead &&
3098 !row_info[r].endfoot && !row_info[r].endlastfoot)
3099 TeXRow(os, r, runparams, columns, logical_columns);
3102 // output first header info
3103 if (haveLTFirstHead()) {
3104 if (endfirsthead.topDL)
3106 for (row_type r = 0; r < nrows(); ++r) {
3107 if (row_info[r].endfirsthead)
3108 TeXRow(os, r, runparams, columns, logical_columns);
3110 if (endfirsthead.bottomDL)
3112 os << "\\endfirsthead\n";
3114 // output header info
3116 if (endfirsthead.empty && !haveLTFirstHead())
3117 os << "\\endfirsthead\n";
3120 for (row_type r = 0; r < nrows(); ++r) {
3121 if (row_info[r].endhead)
3122 TeXRow(os, r, runparams, columns, logical_columns);
3124 if (endhead.bottomDL)
3126 os << "\\endhead\n";
3128 // output footer info
3132 for (row_type r = 0; r < nrows(); ++r) {
3133 if (row_info[r].endfoot)
3134 TeXRow(os, r, runparams, columns, logical_columns);
3136 if (endfoot.bottomDL)
3138 os << "\\endfoot\n";
3139 if (endlastfoot.empty && !haveLTLastFoot())
3140 os << "\\endlastfoot\n";
3142 // output lastfooter info
3143 if (haveLTLastFoot()) {
3144 if (endlastfoot.topDL)
3146 for (row_type r = 0; r < nrows(); ++r) {
3147 if (row_info[r].endlastfoot)
3148 TeXRow(os, r, runparams, columns, logical_columns);
3150 if (endlastfoot.bottomDL)
3152 os << "\\endlastfoot\n";
3157 bool Tabular::isValidRow(row_type row) const
3159 if (!is_long_tabular)
3161 return !row_info[row].endhead && !row_info[row].endfirsthead
3162 && !row_info[row].endfoot && !row_info[row].endlastfoot
3163 && !row_info[row].caption;
3167 void Tabular::TeXRow(otexstream & os, row_type row,
3168 OutputParams const & runparams,
3169 list<col_type> const & columns, list<col_type> const & logical_columns) const
3171 //output the top line
3172 TeXTopHLine(os, row, columns, logical_columns);
3174 if (row_info[row].top_space_default) {
3176 os << "\\addlinespace\n";
3178 os << "\\noalign{\\vskip\\doublerulesep}\n";
3179 } else if(!row_info[row].top_space.zero()) {
3181 os << "\\addlinespace["
3182 << from_ascii(row_info[row].top_space.asLatexString())
3185 os << "\\noalign{\\vskip"
3186 << from_ascii(row_info[row].top_space.asLatexString())
3190 bool ismulticol = false;
3191 bool ismultirow = false;
3193 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3194 // Luabibdi (used by LuaTeX) behaves like classic
3195 bool const bidi_rtl =
3196 runparams.local_font
3197 && runparams.local_font->isRightToLeft()
3198 && buffer().params().useBidiPackage(runparams);
3199 bool const ct = !buffer().params().output_changes;
3201 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3203 for (auto const & c : columns) {
3204 if (isPartOfMultiColumn(row, c))
3207 idx_type cell = cellIndex(row, c);
3209 if (isPartOfMultiRow(row, c)
3210 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3211 if (cell != lastcell)
3216 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3217 InsetTableCell const * inset = cellInset(cell);
3219 Paragraph const & par = inset->paragraphs().front();
3221 os.texrow().forceStart(par.id(), 0);
3223 bool rtl = par.isRTL(buffer().params())
3225 && getPWidth(cell).zero()
3226 && !runparams.isFullUnicode();
3230 par.getParLanguage(buffer().params())->lang();
3231 if (lang == "farsi")
3233 else if (lang == "arabic_arabi")
3235 // currently, remaining RTL languages are
3236 // arabic_arabtex and hebrew
3240 // pass to the OutputParams that we are in a cell and
3241 // which alignment we have set.
3242 // InsetNewline needs this context information.
3243 OutputParams newrp(runparams);
3244 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3245 ? OutputParams::PLAIN
3246 : OutputParams::ALIGNED;
3248 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3249 // copy cell and split in 2
3250 InsetTableCell head = InsetTableCell(*cellInset(cell));
3251 head.setBuffer(const_cast<Buffer &>(buffer()));
3252 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3254 dit.push_back(CursorSlice(head));
3255 head.setMacrocontextPositionRecursive(dit);
3256 bool hassep = false;
3257 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3259 tail.setBuffer(head.buffer());
3261 dit.push_back(CursorSlice(tail));
3262 tail.setMacrocontextPositionRecursive(dit);
3266 tail.latex(os, newrp);
3269 head.latex(os, newrp);
3271 head.latex(os, newrp);
3274 tail.latex(os, newrp);
3277 } else if (ltCaption(row)) {
3278 // Inside longtable caption rows, we must only output the caption inset
3279 // with its content and omit anything outside of that (see #10791)
3280 InsetTableCell & tc_inset = *const_cast<InsetTableCell *>(inset);
3281 for (Inset const & it : tc_inset) {
3282 if (it.lyxCode() != CAPTION_CODE)
3284 it.latex(os, runparams);
3287 } else if (!isPartOfMultiRow(row, c)) {
3288 if (!runparams.nice)
3289 os.texrow().start(par.id(), 0);
3290 if (isMultiRow(cell) && !LaTeXFeatures::isAvailableAtLeastFrom("multirow", 2021, 1, 29))
3291 newrp.isNonLong = true;
3292 inset->latex(os, newrp);
3295 runparams.encoding = newrp.encoding;
3299 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3300 if (cell != lastcell) { // not last cell in row
3307 os << "\\tabularnewline";
3308 if (row_info[row].bottom_space_default) {
3310 os << "\\addlinespace";
3312 os << "[\\doublerulesep]";
3313 } else if (!row_info[row].bottom_space.zero()) {
3315 os << "\\addlinespace";
3317 << from_ascii(row_info[row].bottom_space.asLatexString())
3322 //output the bottom line
3323 TeXBottomHLine(os, row, columns, logical_columns);
3325 if (row_info[row].interline_space_default) {
3327 os << "\\addlinespace\n";
3329 os << "\\noalign{\\vskip\\doublerulesep}\n";
3330 } else if (!row_info[row].interline_space.zero()) {
3332 os << "\\addlinespace["
3333 << from_ascii(row_info[row].interline_space.asLatexString())
3336 os << "\\noalign{\\vskip"
3337 << from_ascii(row_info[row].interline_space.asLatexString())
3343 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3345 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3346 && !hasVarwidthColumn();
3347 bool const is_xltabular = is_long_tabular
3348 && (hasVarwidthColumn() || !tabular_width.zero());
3349 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3351 //+---------------------------------------------------------------------
3352 //+ first the opening preamble +
3353 //+---------------------------------------------------------------------
3356 if (!TexRow::isNone(pos))
3357 os.texrow().start(pos);
3360 if (is_long_tabular)
3361 os << "\\begin{landscape}\n";
3363 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3366 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3367 // order for RTL (#9686). Thus we use this list.
3368 bool const bidi_rtl =
3369 runparams.local_font
3370 && runparams.local_font->isRightToLeft()
3371 && buffer().params().useBidiPackage(runparams);
3372 list<col_type> columns;
3373 list<col_type> logical_columns;
3374 for (col_type cl = 0; cl < ncols(); ++cl) {
3375 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3378 columns.push_front(cl);
3380 columns.push_back(cl);
3381 // for some calculations, we need the logical (non-swapped)
3382 // columns also in bidi.
3383 logical_columns.push_back(cl);
3386 // If we use \cline or \cmidrule, we need to locally de-activate
3387 // the - character when using languages that activate it (e.g., Czech, Slovak).
3388 bool deactivate_chars = false;
3389 if ((runparams.use_babel || runparams.use_polyglossia)
3390 && contains(runparams.active_chars, '-')) {
3391 bool have_clines = false;
3392 // Check if we use \cline or \cmidrule
3393 for (row_type row = 0; row < nrows(); ++row) {
3394 col_type bset = 0, tset = 0;
3395 for (auto const & c : columns) {
3396 idx_type const idx = cellIndex(row, c);
3397 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3398 || topLineTrim(idx).first || topLineTrim(idx).second) {
3402 if (bottomLine(cellIndex(row, c)))
3404 if (topLine(cellIndex(row, c)))
3407 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3413 deactivate_chars = true;
3414 os << "\\begingroup\n"
3415 << "\\catcode`\\-=12\n";
3419 if (is_long_tabular) {
3421 os << "\\begin{xltabular}";
3423 os << "\\begin{longtable}";
3424 switch (longtabular_alignment) {
3425 case LYX_LONGTABULAR_ALIGN_LEFT:
3428 case LYX_LONGTABULAR_ALIGN_CENTER:
3431 case LYX_LONGTABULAR_ALIGN_RIGHT:
3436 if (tabular_width.zero())
3437 os << "{" << from_ascii("\\columnwidth") << "}";
3439 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3442 if (is_tabular_star)
3443 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3444 else if (hasVarwidthColumn()) {
3445 os << "\\begin{tabularx}{";
3446 if (tabular_width.zero())
3447 os << from_ascii("\\columnwidth") << "}";
3449 os << from_ascii(tabular_width.asLatexString()) << "}";
3451 os << "\\begin{tabular}";
3452 switch (tabular_valignment) {
3453 case LYX_VALIGN_TOP:
3456 case LYX_VALIGN_MIDDLE:
3458 case LYX_VALIGN_BOTTOM:
3466 if (is_tabular_star)
3467 os << "@{\\extracolsep{\\fill}}";
3469 for (auto const & c : columns) {
3470 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3472 if (!column_info[c].align_special.empty()) {
3473 os << column_info[c].align_special;
3475 if (!column_info[c].p_width.zero()) {
3476 bool decimal = false;
3477 switch (column_info[c].alignment) {
3478 case LYX_ALIGN_LEFT:
3479 os << ">{\\raggedright}";
3481 case LYX_ALIGN_RIGHT:
3482 os << ">{\\raggedleft}";
3484 case LYX_ALIGN_CENTER:
3485 os << ">{\\centering}";
3487 case LYX_ALIGN_NONE:
3488 case LYX_ALIGN_BLOCK:
3489 case LYX_ALIGN_LAYOUT:
3490 case LYX_ALIGN_SPECIAL:
3492 case LYX_ALIGN_DECIMAL: {
3494 os << ">{\\raggedright}";
3496 os << ">{\\raggedleft}";
3503 switch (column_info[c].valignment) {
3504 case LYX_VALIGN_TOP:
3505 // this is the default
3507 case LYX_VALIGN_MIDDLE:
3510 case LYX_VALIGN_BOTTOM:
3516 // Fixed-width cells with alignment at decimal separator
3517 // are output as two cells of half the width with the decimal
3518 // separator as column sep. This effectively puts the content
3519 // centered, which differs from the normal decimal sep alignment
3520 // and is not ideal, but we cannot do better ATM (see #9568).
3521 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3523 docstring const halffixedwith =
3524 from_ascii(Length(column_info[c].p_width.value() / 2,
3525 column_info[c].p_width.unit()).asLatexString());
3529 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3536 << from_ascii(column_info[c].p_width.asLatexString())
3538 } else if (column_info[c].varwidth) {
3539 switch (column_info[c].alignment) {
3540 case LYX_ALIGN_LEFT:
3541 os << ">{\\raggedright\\arraybackslash}";
3543 case LYX_ALIGN_RIGHT:
3544 os << ">{\\raggedleft\\arraybackslash}";
3546 case LYX_ALIGN_CENTER:
3547 os << ">{\\centering\\arraybackslash}";
3549 case LYX_ALIGN_NONE:
3550 case LYX_ALIGN_BLOCK:
3551 case LYX_ALIGN_LAYOUT:
3552 case LYX_ALIGN_SPECIAL:
3553 case LYX_ALIGN_DECIMAL:
3557 } else if (isVTypeColumn(c))
3558 os << "V{\\linewidth}";
3560 switch (column_info[c].alignment) {
3561 case LYX_ALIGN_LEFT:
3564 case LYX_ALIGN_RIGHT:
3567 case LYX_ALIGN_DECIMAL:
3568 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3574 } // end if else !column_info[i].p_width
3575 } // end if else !column_info[i].align_special
3576 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3581 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3583 //+---------------------------------------------------------------------
3584 //+ the single row and columns (cells) +
3585 //+---------------------------------------------------------------------
3587 for (row_type r = 0; r < nrows(); ++r) {
3588 if (!buffer().params().output_changes && row_info[r].change.deleted())
3590 if (isValidRow(r)) {
3591 TeXRow(os, r, runparams, columns, logical_columns);
3592 if (is_long_tabular && row_info[r].newpage)
3593 os << "\\newpage\n";
3597 //+---------------------------------------------------------------------
3598 //+ the closing of the tabular +
3599 //+---------------------------------------------------------------------
3601 if (is_long_tabular) {
3603 os << "\\end{xltabular}";
3605 os << "\\end{longtable}";
3607 if (is_tabular_star)
3608 os << "\\end{tabular*}";
3609 else if (hasVarwidthColumn())
3610 os << "\\end{tabularx}";
3612 os << "\\end{tabular}";
3615 if (deactivate_chars)
3617 os << "\n\\endgroup\n";
3620 if (is_long_tabular)
3621 os << breakln << "\\end{landscape}";
3623 os << breakln << "\\end{turn}";
3626 if (!TexRow::isNone(pos))
3627 os.texrow().start(pos);
3631 std::string Tabular::getVAlignAsXmlAttribute(idx_type cell) const
3633 switch (getVAlignment(cell)) {
3634 case LYX_VALIGN_TOP:
3635 return "valign='top'";
3636 case LYX_VALIGN_BOTTOM:
3637 return "valign='bottom'";
3638 case LYX_VALIGN_MIDDLE:
3639 return "valign='middle'";
3641 // This case only silences a compiler warning, as all the cases are covered above.
3647 std::string Tabular::getVAlignAsCssAttribute(idx_type cell) const
3649 switch (getVAlignment(cell)) {
3650 case LYX_VALIGN_TOP:
3651 return "vertical-align: top";
3652 case LYX_VALIGN_BOTTOM:
3653 return "vertical-align: bottom";
3654 case LYX_VALIGN_MIDDLE:
3655 return "vertical-align: middle";
3657 // This case only silences a compiler warning, as all the cases are covered above.
3663 std::string Tabular::getHAlignAsXmlAttribute(idx_type cell) const
3665 switch (getAlignment(cell)) {
3666 case LYX_ALIGN_LEFT:
3667 return "align='left'";
3668 case LYX_ALIGN_RIGHT:
3669 return "align='right'";
3670 case LYX_ALIGN_BLOCK:
3671 return "align='justify'";
3672 case LYX_ALIGN_DECIMAL: {
3673 Language const *lang = buffer().paragraphs().front().getParLanguage(buffer().params());
3674 return "align='char' char='" + to_utf8(lang->decimalSeparator()) + "'";
3677 return "align='center'";
3682 std::string Tabular::getHAlignAsCssAttribute(idx_type cell) const
3684 switch (getAlignment(cell)) {
3685 case LYX_ALIGN_LEFT:
3686 return "text-align: left";
3687 case LYX_ALIGN_RIGHT:
3688 return "text-align: right";
3689 case LYX_ALIGN_BLOCK:
3690 return "text-align: justify";
3691 case LYX_ALIGN_DECIMAL: {
3692 // In theory, character-level alignment is supported for CSS4, but it's
3694 // https://www.w3.org/TR/css-text-4/#character-alignment
3695 Language const *lang = buffer().paragraphs().front().getParLanguage(buffer().params());
3696 return "text-align: \"" + to_utf8(lang->decimalSeparator()) + "\"";
3699 return "text-align: center";
3704 Tabular::XmlRowWiseBorders Tabular::computeXmlBorders(row_type row) const
3706 Tabular::XmlRowWiseBorders borders;
3708 // Determine whether borders are required.
3709 for (col_type c = 0; c < ncols(); ++c) {
3710 if (row < nrows() - 1) {
3711 if (!bottomLine(cellIndex(row, c))
3712 || !topLine(cellIndex(row + 1, c))) {
3713 borders.completeBorder = false;
3715 if (!bottomLine(cellIndex(row, c))
3716 && !topLine(cellIndex(row + 1, c))) {
3717 borders.completeBorderBelow = false;
3719 } else if (row == nrows() - 1 && !bottomLine(cellIndex(row, c))) {
3720 borders.completeBorderBelow = false;
3723 if ((row > 0 && !bottomLine(cellIndex(row - 1, c)) && !topLine(cellIndex(row, c))) ||
3724 (row == 0 && !topLine(cellIndex(row, c)))) {
3725 borders.completeBorderAbove = false;
3729 // Size of booktabs borders.
3731 if (borders.completeBorderAbove)
3732 borders.borderTopWidth = row == 0 ? 2 : 1.5;
3733 if (borders.completeBorderBelow) {
3734 borders.borderBottomWidth = row == nrows() - 1 ? 2 : 1.5;
3735 borders.borderBottomWidthComplete = 3 * borders.borderBottomWidth;
3743 std::vector<std::string> Tabular::computeCssStylePerCell(row_type row, col_type col, idx_type cell) const
3745 std::vector<std::string> styles;
3748 Length const col_width = column_info[col].p_width;
3749 if (!col_width.zero())
3750 styles.emplace_back("width: " + col_width.asHTMLString());
3752 // Borders and booktabs.
3753 const Tabular::XmlRowWiseBorders borders = computeXmlBorders(row);
3755 if (bottomLine(cell)) {
3756 if (row < nrows() - 1 && borders.completeBorder)
3757 styles.emplace_back("border-bottom: " + to_string(borders.borderBottomWidthComplete) + "px double");
3759 styles.emplace_back("border-bottom: " + to_string(borders.borderBottomWidth) + "px solid");
3761 if (rightLine(cell)) {
3762 if (col < ncols() - 1 && leftLine(cell + 1))
3763 styles.emplace_back("border-right: 3px double");
3765 styles.emplace_back("border-right: 1px solid");
3768 styles.emplace_back("border-left: 1px solid");
3770 styles.emplace_back("border-top: " + to_string(borders.borderTopWidth) + "px solid");
3776 docstring Tabular::xmlRow(XMLStream & xs, const row_type row, OutputParams const & runparams,
3777 const bool header, const XmlOutputFormat output_format, BufferParams::TableOutput docbook_table_output) const
3780 const bool is_xhtml_table = output_format == XmlOutputFormat::XHTML ||
3781 docbook_table_output == BufferParams::TableOutput::HTMLTable;
3782 const bool is_cals_table = output_format == XmlOutputFormat::DOCBOOK &&
3783 docbook_table_output == BufferParams::TableOutput::CALSTable;
3785 std::string const row_tag = is_xhtml_table ? "tr" : "row";
3786 std::string const cell_tag = is_xhtml_table ? (header ? "th" : "td") : "entry";
3787 Tabular::XmlRowWiseBorders const borders = computeXmlBorders(row);
3788 idx_type cell = getFirstCellInRow(row);
3790 std::string row_attr;
3791 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?
3792 // CALS only: all cases where there should be a line *below* this row.
3793 if (is_cals_table && (row_info[row].bottom_space_default || bottomLine(cell))) {
3794 if (borders.completeBorderBelow) {
3795 row_attr = "rowsep='1'";
3796 cals_row_has_rowsep = true;
3800 xs << xml::StartTag(row_tag, row_attr);
3802 for (col_type c = 0; c < ncols(); ++c, ++cell) {
3803 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3806 stringstream attr; // Tag-level attributes, both for HTML and CALS.
3807 stringstream style; // Tag-level CSS, only for HTML tables output in HTML.
3809 if (is_cals_table) {
3810 if (!cals_row_has_rowsep && bottomLine(cell))
3811 attr << "rowsep='1' ";
3814 if (output_format == XmlOutputFormat::XHTML) {
3815 // In HTML5, prefer to use CSS instead of attributes for alignment
3816 // (align and valign).
3817 style << getHAlignAsCssAttribute(cell) << "; "
3818 << getVAlignAsCssAttribute(cell);
3820 // In DocBook, both for HTML and CALS tables, stick to attributes.
3821 attr << getHAlignAsXmlAttribute(cell) << " "
3822 << getVAlignAsXmlAttribute(cell);
3825 if (is_xhtml_table) {
3826 if (isMultiColumn(cell))
3827 attr << " colspan='" << columnSpan(cell) << "'";
3828 else if (isMultiRow(cell))
3829 attr << " rowspan='" << rowSpan(cell) << "'";
3830 } else if (is_cals_table) {
3831 if (isMultiColumn(cell))
3832 attr << " namest='c" << c << " nameend='c" << (c + columnSpan(cell)) << "'";
3833 else if (isMultiRow(cell))
3834 attr << " morerows='" << rowSpan(cell) << "'";
3836 attr << " colname='c" << (c + 1) << "'"; // CALS column numbering starts at 1.
3839 // Final step: prepend the CSS style.
3840 std::string attr_str = attr.str();
3841 if (is_xhtml_table) {
3842 const std::vector<std::string> styles = computeCssStylePerCell(row, c, cell);
3844 std::string attr_str_prefix = "style='" + style.str();
3845 if (!style.str().empty())
3846 attr_str_prefix += "; ";
3847 for (auto it = styles.begin(); it != styles.end(); ++it) {
3848 attr_str_prefix += *it;
3849 if (it != styles.end() - 1)
3850 attr_str_prefix += "; ";
3852 attr_str_prefix += "' ";
3854 attr_str.insert(0, attr_str_prefix);
3857 // Render the cell as either XHTML or DocBook.
3858 xs << xml::StartTag(cell_tag, attr_str, true);
3859 if (output_format == XmlOutputFormat::XHTML) {
3860 ret += cellInset(cell)->xhtml(xs, runparams);
3861 } else if (output_format == XmlOutputFormat::DOCBOOK) {
3862 // DocBook: no return value for this function.
3863 OutputParams rp = runparams;
3864 rp.docbook_in_par = false;
3865 rp.docbook_force_pars = true;
3866 cellInset(cell)->docbook(xs, rp);
3868 xs << xml::EndTag(cell_tag);
3871 xs << xml::EndTag(row_tag);
3878 void Tabular::xmlHeader(XMLStream & xs, OutputParams const & runparams, const XmlOutputFormat output_format) const
3880 // Output the header of the table. For both HTML and CALS, this is surrounded by a thead.
3881 bool const have_first_head = haveLTFirstHead(false);
3882 // if we have a first head, then we are going to ignore the
3883 // headers for the additional pages, since there aren't any
3884 // in HTML or DocBook.
3885 bool const have_head = !have_first_head && haveLTHead(false);
3887 if (have_head || have_first_head) {
3888 xs << xml::StartTag("thead") << xml::CR();
3889 for (row_type r = 0; r < nrows(); ++r) {
3890 if (((have_first_head && row_info[r].endfirsthead) ||
3891 (have_head && row_info[r].endhead)) &&
3892 !row_info[r].caption) {
3893 xmlRow(xs, r, runparams, true, output_format, buffer().params().docbook_table_output);
3896 xs << xml::EndTag("thead");
3902 void Tabular::xmlFooter(XMLStream & xs, OutputParams const & runparams, const XmlOutputFormat output_format) const
3904 // Output the footer of the table. For both HTML and CALS, this is surrounded by a tfoot and output just after
3905 // the header (and before the body).
3906 bool const have_last_foot = haveLTLastFoot(false);
3907 bool const have_foot = !have_last_foot && haveLTFoot(false);
3909 if (have_foot || have_last_foot) {
3910 xs << xml::StartTag("tfoot") << xml::CR();
3911 for (row_type r = 0; r < nrows(); ++r) {
3912 if (((have_last_foot && row_info[r].endlastfoot) ||
3913 (have_foot && row_info[r].endfoot)) &&
3914 !row_info[r].caption) {
3915 xmlRow(xs, r, runparams, false, output_format, buffer().params().docbook_table_output);
3918 xs << xml::EndTag("tfoot");
3924 void Tabular::xmlBody(XMLStream & xs, OutputParams const & runparams, const XmlOutputFormat output_format) const
3926 // Output the main part of the table. The tbody container is mandatory for CALS, but optional for HTML (only if
3927 // there is no header and no footer). It never hurts to have it, though.
3928 xs << xml::StartTag("tbody");
3930 for (row_type r = 0; r < nrows(); ++r)
3932 xmlRow(xs, r, runparams, false, output_format, buffer().params().docbook_table_output);
3933 xs << xml::EndTag("tbody");
3938 void Tabular::docbook(XMLStream & xs, OutputParams const & runparams) const
3940 // Some tables are inline. Likely limitation: cannot output a table within a table; is that really a limitation?
3941 if (!runparams.docbook_in_table) { // Check on the *outer* set of parameters, so that the table can be closed
3942 // properly at the end of this function.
3943 xs << xml::StartTag("informaltable");
3947 // "Formal" tables have a title and use the tag <table>; the distinction with <informaltable> is done outside.
3948 // HTML has the caption first with titles forbidden, and CALS has a title first.
3949 if (haveLTCaption()) {
3950 std::string caption_tag = ((buffer().params().docbook_table_output) == BufferParams::HTMLTable) ? "caption" : "title";
3952 xs << xml::StartTag(caption_tag);
3953 for (row_type r = 0; r < nrows(); ++r)
3954 if (row_info[r].caption)
3955 xmlRow(xs, r, runparams, false, XmlOutputFormat::DOCBOOK, buffer().params().docbook_table_output);
3956 xs << xml::EndTag(caption_tag);
3960 // CALS header: describe all columns in this table. For names, take 'c' then the ID of the column.
3961 // Start at one, as is customary with CALS!
3962 if (buffer().params().docbook_table_output == BufferParams::CALSTable) {
3963 for (col_type c = 0; c < ncols(); ++c) {
3964 std::stringstream attr;
3965 attr << "colnum='" << (c + 1) << "' ";
3966 attr << "colname='c" << (c + 1) << "' ";
3967 Length const cwidth = column_info[c].p_width;
3969 attr << "colwidth='" << cwidth.asHTMLString() << "' ";
3970 attr << "rowheader='norowheader'"; // Last attribute, hence no space at the end.
3972 xs << xml::CompTag("colspec", attr.str());
3977 xmlHeader(xs, runparams, XmlOutputFormat::DOCBOOK);
3978 xmlFooter(xs, runparams, XmlOutputFormat::DOCBOOK);
3979 xmlBody(xs, runparams, XmlOutputFormat::DOCBOOK);
3981 // If this method started the table tag, also make it close it.
3982 if (!runparams.docbook_in_table) {
3983 xs << xml::EndTag("informaltable");
3989 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3993 if (is_long_tabular) {
3994 // We'll wrap it in a div to deal with alignment.
3996 switch (longtabular_alignment) {
3997 case LYX_LONGTABULAR_ALIGN_LEFT:
4000 case LYX_LONGTABULAR_ALIGN_CENTER:
4003 case LYX_LONGTABULAR_ALIGN_RIGHT:
4007 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
4010 // The caption flag is output before header/footer.
4011 if (haveLTCaption()) {
4012 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
4014 for (row_type r = 0; r < nrows(); ++r)
4015 if (row_info[r].caption)
4016 ret += xmlRow(xs, r, runparams, false, XmlOutputFormat::XHTML);
4017 xs << xml::EndTag("div");
4022 xs << xml::StartTag("table");
4025 xmlHeader(xs, runparams, XmlOutputFormat::XHTML);
4026 xmlFooter(xs, runparams, XmlOutputFormat::XHTML);
4027 xmlBody(xs, runparams, XmlOutputFormat::XHTML);
4029 xs << xml::EndTag("table");
4031 if (is_long_tabular) {
4032 xs << xml::EndTag("div");
4039 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
4040 vector<unsigned int> const & clen) const
4042 idx_type const fcell = getFirstCellInRow(row);
4043 idx_type const n = numberOfCellsInRow(row) + fcell;
4046 for (idx_type i = fcell; i < n; ++i) {
4056 for (idx_type i = fcell; i < n; ++i) {
4067 col_type column = cellColumn(i);
4068 int len = clen[column];
4069 while (column < ncols() - 1
4070 && isPartOfMultiColumn(row, ++column))
4071 len += clen[column] + 4;
4072 os << docstring(len, ch);
4087 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
4088 vector<unsigned int> const & clen) const
4090 idx_type const fcell = getFirstCellInRow(row);
4091 idx_type const n = numberOfCellsInRow(row) + fcell;
4094 for (idx_type i = fcell; i < n; ++i) {
4095 if (bottomLine(i)) {
4104 for (idx_type i = fcell; i < n; ++i) {
4105 if (bottomLine(i)) {
4115 col_type column = cellColumn(i);
4116 int len = clen[column];
4117 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4118 len += clen[column] + 4;
4119 os << docstring(len, ch);
4120 if (bottomLine(i)) {
4134 void Tabular::plaintextPrintCell(odocstringstream & os,
4135 OutputParams const & runparams,
4136 idx_type cell, row_type row, col_type column,
4137 vector<unsigned int> const & clen,
4138 bool onlydata, size_t max_length) const
4140 odocstringstream sstr;
4141 cellInset(cell)->plaintext(sstr, runparams, max_length);
4153 unsigned int len1 = sstr.str().length();
4154 unsigned int len2 = clen[column];
4155 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4156 len2 += clen[column] + 4;
4159 switch (getAlignment(cell)) {
4161 case LYX_ALIGN_LEFT:
4164 case LYX_ALIGN_RIGHT:
4168 case LYX_ALIGN_CENTER:
4174 os << docstring(len1, ' ') << sstr.str()
4175 << docstring(len2, ' ');
4177 if (rightLine(cell))
4184 void Tabular::plaintext(odocstringstream & os,
4185 OutputParams const & runparams, int const depth,
4186 bool onlydata, char_type delim, size_t max_length) const
4188 // first calculate the width of the single columns
4189 vector<unsigned int> clen(ncols());
4192 // first all non multicolumn cells!
4193 for (col_type c = 0; c < ncols(); ++c) {
4195 for (row_type r = 0; r < nrows(); ++r) {
4196 idx_type cell = cellIndex(r, c);
4197 if (isMultiColumn(cell))
4199 odocstringstream sstr;
4200 cellInset(cell)->plaintext(sstr, runparams, max_length);
4201 if (clen[c] < sstr.str().length())
4202 clen[c] = sstr.str().length();
4205 // then all multicolumn cells!
4206 for (col_type c = 0; c < ncols(); ++c) {
4207 for (row_type r = 0; r < nrows(); ++r) {
4208 idx_type cell = cellIndex(r, c);
4209 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
4211 odocstringstream sstr;
4212 cellInset(cell)->plaintext(sstr, runparams, max_length);
4213 int len = int(sstr.str().length());
4214 idx_type const n = columnSpan(cell);
4215 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
4217 if (len > int(clen[c + n - 1]))
4218 clen[c + n - 1] = len;
4223 for (row_type r = 0; r < nrows(); ++r) {
4224 if (!onlydata && plaintextTopHLine(os, r, clen))
4225 os << docstring(depth * 2, ' ');
4226 for (col_type c = 0; c < ncols(); ++c) {
4227 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
4229 if (onlydata && c > 0)
4230 // we don't use operator<< for single UCS4 character.
4231 // see explanation in docstream.h
4233 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
4235 if (os.str().size() > max_length)
4240 os << docstring(depth * 2, ' ');
4241 if (plaintextBottomHLine(os, r, clen))
4242 os << docstring(depth * 2, ' ');
4244 if (os.str().size() > max_length)
4250 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4252 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4256 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4258 return cell_info[row][column].inset;
4262 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4264 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4268 void Tabular::setCellInset(row_type row, col_type column,
4269 shared_ptr<InsetTableCell> ins)
4271 CellData & cd = cell_info[row][column];
4276 void Tabular::validate(LaTeXFeatures & features) const
4278 features.require("NeedTabularnewline");
4280 features.require("booktabs");
4281 if (is_long_tabular && !hasVarwidthColumn()) {
4282 if (tabular_width.zero())
4283 features.require("longtable");
4285 features.require("xltabular");
4287 if (rotate && is_long_tabular)
4288 features.require("lscape");
4290 features.require("rotating");
4291 if (hasVarwidthColumn()) {
4292 if (is_long_tabular)
4293 features.require("xltabular");
4295 features.require("tabularx");
4297 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4298 if (isMultiRow(cell))
4299 features.require("multirow");
4300 if (getUsebox(cell) == BOX_VARWIDTH) {
4301 features.require("varwidth");
4302 features.require("cellvarwidth");
4304 if (getVAlignment(cell) != LYX_VALIGN_TOP
4305 || !getPWidth(cell).zero()
4306 || isVTypeColumn(cellColumn(cell)))
4307 features.require("array");
4308 // Tell footnote that we need a savenote
4309 // environment in non-long tables or
4310 // longtable headers/footers
4311 else if (!is_long_tabular && !features.inFloat())
4312 features.saveNoteEnv("tabular");
4313 else if (!isValidRow(cellRow(cell)))
4314 features.saveNoteEnv("longtable");
4316 cellInset(cell)->validate(features);
4317 features.saveNoteEnv(string());
4322 Tabular::BoxType Tabular::useBox(idx_type cell) const
4324 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4325 if (parlist.size() > 1)
4326 return BOX_VARWIDTH;
4328 ParagraphList::const_iterator cit = parlist.begin();
4329 ParagraphList::const_iterator end = parlist.end();
4331 for (; cit != end; ++cit)
4332 for (int i = 0; i < cit->size(); ++i)
4333 if (cit->isNewline(i) || cit->layout().isEnvironment())
4334 return BOX_VARWIDTH;
4340 bool Tabular::hasNewlines(idx_type cell) const
4342 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4343 ParagraphList::const_iterator cit = parlist.begin();
4344 ParagraphList::const_iterator end = parlist.end();
4346 for (; cit != end; ++cit)
4347 for (int i = 0; i < cit->size(); ++i)
4348 if (cit->isNewline(i))
4355 /////////////////////////////////////////////////////////////////////
4359 /////////////////////////////////////////////////////////////////////
4361 InsetTableCell::InsetTableCell(Buffer * buf)
4362 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false), isVarwidth(false),
4363 isMultiColumn(false), isMultiRow(false), isCaptionRow(false),
4364 contentAlign(LYX_ALIGN_CENTER)
4367 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4369 return isFixedWidth;
4373 bool InsetTableCell::forceLocalFontSwitch() const
4379 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4380 FuncStatus & status) const
4382 bool enabled = true;
4383 switch (cmd.action()) {
4384 case LFUN_INSET_SPLIT:
4385 case LFUN_INSET_DISSOLVE:
4388 case LFUN_MATH_DISPLAY:
4389 if (!hasFixedWidth()) {
4394 return InsetText::getStatus(cur, cmd, status);
4396 status.setEnabled(enabled);
4400 docstring InsetTableCell::asString(bool intoInsets)
4403 if (paragraphs().empty())
4405 ParagraphList::const_iterator it = paragraphs().begin();
4406 ParagraphList::const_iterator en = paragraphs().end();
4408 for (; it != en; ++it) {
4413 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4419 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4420 UpdateType utype, TocBackend & backend) const
4422 InsetText::iterateForToc(di, output_active, utype, backend);
4426 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4429 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4430 return InsetText::xhtml(xs, rp);
4434 void InsetTableCell::docbook(XMLStream & xs, OutputParams const & runparams) const
4436 InsetText::docbook(xs, runparams);
4440 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4442 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4443 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4445 // Hand font through to contained lyxtext:
4446 tm.font_.fontInfo() = mi.base.font;
4447 mi.base.textwidth -= horiz_offset;
4449 // This can happen when a layout has a left and right margin,
4450 // and the view is made very narrow. We can't do better than
4451 // to draw it partly out of view (bug 5890).
4452 if (mi.base.textwidth < 1)
4453 mi.base.textwidth = 1;
4455 // We tell metrics here not to expand on multiple pars
4456 // This is the difference to InsetText::Metrics
4457 Changer changetight = changeVar(mi.tight_insets, true);
4458 if (hasFixedWidth())
4459 tm.metrics(mi, dim, mi.base.textwidth);
4461 tm.metrics(mi, dim, 0);
4462 mi.base.textwidth += horiz_offset;
4463 dim.asc += topOffset(mi.base.bv);
4464 dim.des += bottomOffset(mi.base.bv);
4465 dim.wid += horiz_offset;
4469 bool InsetTableCell::insetAllowed(InsetCode code) const
4474 case MATHMACRO_CODE:
4479 return isCaptionRow;
4487 /////////////////////////////////////////////////////////////////////
4491 /////////////////////////////////////////////////////////////////////
4493 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4495 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4496 rowselect_(false), colselect_(false)
4501 InsetTabular::InsetTabular(InsetTabular const & tab)
4502 : Inset(tab), tabular(tab.tabular),
4503 rowselect_(false), colselect_(false)
4508 InsetTabular::~InsetTabular()
4510 hideDialogs("tabular", this);
4514 void InsetTabular::setBuffer(Buffer & buf)
4516 tabular.setBuffer(buf);
4517 Inset::setBuffer(buf);
4521 bool InsetTabular::insetAllowed(InsetCode code) const
4526 case MATHMACRO_CODE:
4531 // this is handled on cell level
4540 bool InsetTabular::allowMultiPar() const
4542 for (col_type c = 0; c < tabular.ncols(); ++c) {
4543 for (row_type r = 0; r < tabular.nrows(); ++r) {
4544 if (tabular.cellInset(r,c)->allowMultiPar())
4552 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4554 return tabular.is_long_tabular &&
4555 (newtype == "Standard" || newtype == "Unnumbered");
4559 void InsetTabular::write(ostream & os) const
4561 os << "Tabular" << endl;
4566 string InsetTabular::contextMenu(BufferView const &, int, int) const
4568 // FIXME: depending on the selection state,
4569 // we could offer a different menu.
4570 return cell(0)->contextMenuName() + ";" + contextMenuName();
4574 string InsetTabular::contextMenuName() const
4576 return "context-tabular";
4580 void InsetTabular::read(Lexer & lex)
4582 //bool const old_format = (lex.getString() == "\\LyXTable");
4590 string token = lex.getString();
4591 while (lex && token != "\\end_inset") {
4593 token = lex.getString();
4596 lex.printError("Missing \\end_inset at this point. ");
4600 int InsetTabular::rowFromY(Cursor & cur, int y) const
4602 // top y coordinate of tabular
4603 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4605 for (; r < tabular.nrows() && y > h; ++r)
4606 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4607 + tabular.interRowSpace(r);
4613 int InsetTabular::columnFromX(Cursor & cur, int x) const
4615 // left x coordinate of tabular
4616 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4618 for (; c < tabular.ncols() && x > w; ++c)
4619 w += tabular.cellWidth(c);
4624 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4626 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4627 // mi.base.textwidth << "\n";
4628 LBUFERR(mi.base.bv);
4630 for (row_type r = 0; r < tabular.nrows(); ++r) {
4633 for (col_type c = 0; c < tabular.ncols(); ++c) {
4634 if (tabular.isPartOfMultiColumn(r, c)
4635 || tabular.isPartOfMultiRow(r, c))
4636 // multicolumn or multirow cell, but not first one
4638 idx_type const cell = tabular.cellIndex(r, c);
4641 Length const p_width = tabular.getPWidth(cell);
4642 if (!p_width.zero())
4643 m.base.textwidth = mi.base.inPixels(p_width);
4644 else if (tabular.column_info[c].varwidth)
4645 m.base.textwidth = tabular.column_info[c].width;
4646 tabular.cellInset(cell)->metrics(m, dim0);
4647 if (!p_width.zero() || tabular.column_info[c].varwidth)
4648 dim0.wid = m.base.textwidth;
4649 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4650 + tabular.interColumnSpace(cell);
4652 // FIXME(?): do we need a second metrics call?
4653 TextMetrics const & tm =
4654 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4656 // determine horizontal offset because of decimal align (if necessary)
4657 int decimal_width = 0;
4658 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4659 InsetTableCell tail = *tabular.cellInset(cell);
4660 tail.setBuffer(tabular.buffer());
4661 // we need to set macrocontext position everywhere
4662 // otherwise we crash with nested insets (e.g. footnotes)
4663 // after decimal point
4664 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4666 dit.push_back(CursorSlice(tail));
4667 tail.setMacrocontextPositionRecursive(dit);
4669 // remove text leading decimal point
4670 docstring const align_d = tabular.column_info[c].decimal_point;
4671 dit = separatorPos(&tail, align_d);
4673 pos_type const psize = tail.paragraphs().front().size();
4675 tail.paragraphs().front().eraseChars(0,
4676 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4678 tail.metrics(m, dim1);
4679 decimal_width = dim1.width();
4682 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4683 tabular.cell_info[r][c].decimal_width = decimal_width;
4685 // with LYX_VALIGN_BOTTOM the descent is relative to the last
4686 // row of the last par (note that the par might have multile rows!)
4687 // = descent of text in last row + bottomOffset:
4688 int const lastpardes = tm.last().second->rows().back().descent()
4689 + bottomOffset(mi.base.bv);
4691 switch (tabular.getVAlignment(cell)) {
4692 case Tabular::LYX_VALIGN_TOP:
4694 case Tabular::LYX_VALIGN_MIDDLE:
4695 offset = -(dim0.des - lastpardes)/2;
4697 case Tabular::LYX_VALIGN_BOTTOM:
4698 offset = -(dim0.des - lastpardes);
4701 tabular.cell_info[r][c].voffset = offset;
4702 maxasc = max(maxasc, dim0.asc - offset);
4703 maxdes = max(maxdes, dim0.des + offset);
4705 int const top_space = tabular.row_info[r].top_space_default ?
4706 default_line_space :
4707 mi.base.inPixels(tabular.row_info[r].top_space);
4708 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4709 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4710 default_line_space :
4711 mi.base.inPixels(tabular.row_info[r].bottom_space);
4712 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4715 // We need to recalculate the metrics after column width calculation
4716 // with xtabular (possibly multiple times, so the call is recursive).
4717 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4719 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4720 dim.des = tabular.height() - dim.asc;
4721 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4725 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4727 if (&cur.inset() == this && cur.selection()) {
4728 if (cur.selIsMultiCell()) {
4731 getSelection(cur, rs, re, cs, ce);
4733 idx_type const cell = tabular.cellIndex(row, col);
4734 col_type const cspan = tabular.columnSpan(cell);
4735 row_type const rspan = tabular.rowSpan(cell);
4736 if (col + cspan - 1 >= cs && col <= ce
4737 && row + rspan - 1 >= rs && row <= re)
4740 if (col == tabular.cellColumn(cur.idx())
4741 && row == tabular.cellRow(cur.idx())) {
4742 CursorSlice const & beg = cur.selBegin();
4743 CursorSlice const & end = cur.selEnd();
4745 if ((end.lastpos() > 0 || end.lastpit() > 0)
4746 && end.pos() == end.lastpos() && beg.pos() == 0
4747 && end.pit() == end.lastpit() && beg.pit() == 0)
4755 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4757 x += ADD_TO_TABULAR_WIDTH;
4759 BufferView * bv = pi.base.bv;
4760 Cursor & cur = pi.base.bv->cursor();
4762 // FIXME: As the full background is painted in drawBackground(),
4763 // we have no choice but to do a full repaint for the Text cells.
4764 pi.full_repaint = true;
4766 bool const original_selection_state = pi.selected;
4770 // Save tabular change status
4771 Change tab_change = pi.change;
4773 int yy = y + tabular.offsetVAlignment();
4774 for (row_type r = 0; r < tabular.nrows(); ++r) {
4776 for (col_type c = 0; c < tabular.ncols(); ++c) {
4777 if (tabular.isPartOfMultiColumn(r, c))
4780 idx = tabular.cellIndex(r, c);
4782 if (tabular.isPartOfMultiRow(r, c)) {
4783 nx += tabular.cellWidth(idx);
4787 pi.selected |= isCellSelected(cur, r, c);
4789 // Mark deleted rows/columns
4790 if (tabular.column_info[c].change.changed())
4791 pi.change = tabular.column_info[c].change;
4792 else if (tabular.row_info[r].change.changed())
4793 pi.change = tabular.row_info[r].change;
4795 pi.change = tab_change;
4797 int const cx = nx + tabular.textHOffset(idx);
4798 int const cy = yy + tabular.textVOffset(idx);
4799 // Cache the Inset position.
4800 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4801 cell(idx)->draw(pi, cx, cy);
4802 drawCellLines(pi, nx, yy, r, idx);
4803 nx += tabular.cellWidth(idx);
4804 pi.selected = original_selection_state;
4807 if (r + 1 < tabular.nrows())
4808 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4809 + tabular.interRowSpace(r + 1);
4814 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4816 x += ADD_TO_TABULAR_WIDTH;
4817 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4818 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4819 pi.backgroundColor(this));
4823 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4825 Cursor & cur = pi.base.bv->cursor();
4827 x += ADD_TO_TABULAR_WIDTH;
4829 if (!cur.selection())
4831 if (&cur.inset() != this)
4836 bool const full_cell_selected = isCellSelected(cur,
4837 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4839 if (cur.selIsMultiCell() || full_cell_selected) {
4840 for (row_type r = 0; r < tabular.nrows(); ++r) {
4842 for (col_type c = 0; c < tabular.ncols(); ++c) {
4843 if (tabular.isPartOfMultiColumn(r, c))
4846 idx_type const cell = tabular.cellIndex(r, c);
4848 if (tabular.isPartOfMultiRow(r, c)) {
4849 xx += tabular.cellWidth(cell);
4852 int const w = tabular.cellWidth(cell);
4853 int const h = tabular.cellHeight(cell);
4854 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4855 if (isCellSelected(cur, r, c))
4856 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4859 if (r + 1 < tabular.nrows())
4860 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4861 + tabular.interRowSpace(r + 1);
4865 // FIXME: This code has no effect because InsetTableCell does not handle
4866 // drawSelection other than the trivial implementation in Inset.
4868 // x += cellXPos(cur.idx());
4869 // x += tabular.textHOffset(cur.idx());
4870 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4877 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4878 Color const incol, bool drawline, bool heavy = false)
4880 Color const col = drawline ? incol : Color_tabularonoffline;
4881 if (drawline && lt > 0)
4882 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4883 Painter::line_onoffdash,
4884 Painter::thin_line);
4885 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4886 drawline ? Painter::line_solid : Painter::line_onoffdash,
4887 (heavy ? 2 : 1) * Painter::thin_line);
4888 if (drawline && rt > 0)
4889 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4890 Painter::line_onoffdash,
4891 Painter::thin_line);
4897 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4898 row_type row, idx_type cell) const
4900 y -= tabular.rowAscent(row);
4901 int const w = tabular.cellWidth(cell);
4902 int const h = tabular.cellHeight(cell);
4906 col_type const col = tabular.cellColumn(cell);
4908 // Colour the frame if rows/columns are added or deleted
4909 Color colour = Color_tabularline;
4910 if (tabular.column_info[col].change.changed()
4911 || tabular.row_info[row].change.changed())
4912 colour = tabular.cellInset(cell)->paragraphs().front().lookupChange(0).color();
4915 bool drawline = tabular.topLine(cell)
4916 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4917 bool heavy = tabular.use_booktabs
4918 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4919 && tabular.rowTopLine(row);
4920 if (tabular.topLineTrim(cell).first
4921 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4923 if (tabular.topLineTrim(cell).second
4924 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4926 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4930 drawline = tabular.bottomLine(cell);
4931 row_type const lastrow = tabular.nrows() - 1;
4932 // Consider multi-rows
4934 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4935 && tabular.isPartOfMultiRow(r + 1, col))
4937 heavy = tabular.use_booktabs
4938 && ((row == lastrow && tabular.rowBottomLine(row))
4939 || (r == lastrow && tabular.rowBottomLine(r)));
4940 if (tabular.bottomLineTrim(cell).first)
4942 if (tabular.bottomLineTrim(cell).second)
4944 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4947 drawline = tabular.leftLine(cell)
4948 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4949 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4952 x -= tabular.interColumnSpace(cell);
4953 col_type next_cell_col = col + 1;
4954 while (next_cell_col < tabular.ncols()
4955 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4957 drawline = tabular.rightLine(cell)
4958 || (next_cell_col < tabular.ncols()
4959 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4960 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4964 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4966 //lyxerr << "InsetTabular::edit: " << this << endl;
4970 if (isRightToLeft(cur))
4971 cur.idx() = tabular.getLastCellInRow(0);
4977 if (isRightToLeft(cur))
4978 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4980 cur.idx() = tabular.numberofcells - 1;
4982 cur.pos() = cur.lastpos(); // FIXME crude guess
4984 cur.setCurrentFont();
4985 // FIXME: this accesses the position cache before it is initialized
4986 //cur.bv().fitCursor();
4990 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4992 // In a longtable, tell captions what the current float is
4993 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4994 string const saveflt = cnts.current_float();
4995 if (tabular.is_long_tabular) {
4996 cnts.current_float("table");
4997 // in longtables, we only step the counter once
4998 cnts.step(from_ascii("table"), utype);
4999 cnts.isLongtable(true);
5002 ParIterator it2 = it;
5004 size_t const end = it2.nargs();
5005 for ( ; it2.idx() < end; it2.top().forwardIdx())
5006 buffer().updateBuffer(it2, utype);
5009 if (tabular.is_long_tabular) {
5010 cnts.current_float(saveflt);
5011 cnts.isLongtable(false);
5016 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
5017 UpdateType utype, TocBackend & backend) const
5019 DocIterator dit = cpit;
5021 size_t const end = dit.nargs();
5022 for ( ; dit.idx() < end; dit.top().forwardIdx())
5023 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
5027 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
5029 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
5030 return x < x0 || x > x0 + tabular.width();
5034 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
5036 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
5037 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
5038 // there is no margin added vertically to tabular insets.
5039 // However, it works for now.
5040 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
5044 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
5046 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
5050 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
5052 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
5053 << "\n cur:" << cur);
5054 CursorSlice sl = cur.top();
5055 Cursor & bvcur = cur.bv().cursor();
5057 FuncCode const act = cmd.action();
5061 case LFUN_MOUSE_PRESS: {
5062 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
5064 if (hitSelectRow(cur.bv(), cmd.x())) {
5065 row_type r = rowFromY(cur, cmd.y());
5066 cur.idx() = tabular.getFirstCellInRow(r);
5070 cur.idx() = tabular.getLastCellInRow(r);
5071 cur.pit() = cur.lastpit();
5072 cur.pos() = cur.lastpos();
5073 cur.selection(true);
5079 if (hitSelectColumn(cur.bv(), cmd.y())) {
5080 col_type c = columnFromX(cur, cmd.x());
5081 cur.idx() = tabular.cellIndex(0, c);
5085 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
5086 cur.pit() = cur.lastpit();
5087 cur.pos() = cur.lastpos();
5088 cur.selection(true);
5093 // do not reset cursor/selection if we have selected
5094 // some cells (bug 2715).
5095 if (cmd.button() == mouse_button::button3
5096 && &bvcur.selBegin().inset() == this
5097 && bvcur.selIsMultiCell())
5100 // Let InsetTableCell do it
5101 cell(cur.idx())->dispatch(cur, cmd);
5104 case LFUN_MOUSE_MOTION:
5105 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
5106 if (cmd.button() == mouse_button::button1) {
5107 // only accept motions to places not deeper nested than the real anchor
5108 if (!bvcur.realAnchor().hasPart(cur)) {
5112 // select (additional) row
5114 row_type r = rowFromY(cur, cmd.y());
5115 cur.idx() = tabular.getLastCellInRow(r);
5116 // we need to reset the cursor's pit and pos now, as the old ones
5117 // may no longer be valid.
5120 bvcur.setCursor(cur);
5121 bvcur.selection(true);
5124 // select (additional) column
5126 col_type c = columnFromX(cur, cmd.x());
5127 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
5128 // we need to reset the cursor's pit and pos now, as the old ones
5129 // may no longer be valid.
5132 bvcur.setCursor(cur);
5133 bvcur.selection(true);
5136 // only update if selection changes
5137 if (bvcur.idx() == cur.idx() &&
5138 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
5139 cur.noScreenUpdate();
5140 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
5141 bvcur.setCursor(cur);
5142 bvcur.selection(true);
5143 // if this is a multicell selection, we just set the cursor to
5144 // the beginning of the cell's text.
5145 if (bvcur.selIsMultiCell()) {
5146 bvcur.pit() = bvcur.lastpit();
5147 bvcur.pos() = bvcur.lastpos();
5149 // Let InsetTableCell do it
5150 cell(cur.idx())->dispatch(cur, cmd);
5154 case LFUN_MOUSE_RELEASE:
5159 case LFUN_CELL_BACKWARD:
5161 cur.selection(false);
5164 case LFUN_CELL_FORWARD:
5166 cur.selection(false);
5169 case LFUN_CHAR_FORWARD_SELECT:
5170 case LFUN_CHAR_FORWARD:
5171 case LFUN_CHAR_BACKWARD_SELECT:
5172 case LFUN_CHAR_BACKWARD:
5173 case LFUN_CHAR_RIGHT_SELECT:
5174 case LFUN_CHAR_RIGHT:
5175 case LFUN_CHAR_LEFT_SELECT:
5176 case LFUN_CHAR_LEFT:
5177 case LFUN_WORD_FORWARD:
5178 case LFUN_WORD_FORWARD_SELECT:
5179 case LFUN_WORD_BACKWARD:
5180 case LFUN_WORD_BACKWARD_SELECT:
5181 case LFUN_WORD_RIGHT:
5182 case LFUN_WORD_RIGHT_SELECT:
5183 case LFUN_WORD_LEFT:
5184 case LFUN_WORD_LEFT_SELECT: {
5185 // determine whether we move to next or previous cell, where to enter
5186 // the new cell from, and which command to "finish" (i.e., exit the
5189 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
5190 FuncCode finish_lfun;
5192 if (act == LFUN_CHAR_FORWARD
5193 || act == LFUN_CHAR_FORWARD_SELECT
5194 || act == LFUN_WORD_FORWARD
5195 || act == LFUN_WORD_FORWARD_SELECT) {
5197 finish_lfun = LFUN_FINISHED_FORWARD;
5199 else if (act == LFUN_CHAR_BACKWARD
5200 || act == LFUN_CHAR_BACKWARD_SELECT
5201 || act == LFUN_WORD_BACKWARD
5202 || act == LFUN_WORD_BACKWARD_SELECT) {
5204 finish_lfun = LFUN_FINISHED_BACKWARD;
5206 // LEFT or RIGHT commands --- the interpretation will depend on the
5207 // table's direction.
5209 bool const right = act == LFUN_CHAR_RIGHT
5210 || act == LFUN_CHAR_RIGHT_SELECT
5211 || act == LFUN_WORD_RIGHT
5212 || act == LFUN_WORD_RIGHT_SELECT;
5213 next_cell = isRightToLeft(cur) != right;
5215 if (lyxrc.visual_cursor)
5216 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
5218 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
5221 bool const select = act == LFUN_CHAR_FORWARD_SELECT
5222 || act == LFUN_CHAR_BACKWARD_SELECT
5223 || act == LFUN_CHAR_RIGHT_SELECT
5224 || act == LFUN_CHAR_LEFT_SELECT
5225 || act == LFUN_WORD_FORWARD_SELECT
5226 || act == LFUN_WORD_RIGHT_SELECT
5227 || act == LFUN_WORD_BACKWARD_SELECT
5228 || act == LFUN_WORD_LEFT_SELECT;
5230 // If we have a multicell selection or we're
5231 // not doing some LFUN_*_SELECT thing anyway...
5232 if (!cur.selIsMultiCell() || !select) {
5233 col_type const c = tabular.cellColumn(cur.idx());
5234 row_type const r = tabular.cellRow(cur.idx());
5235 // Are we trying to select the whole cell and is the whole cell
5236 // not yet selected?
5237 bool const select_whole = select && !isCellSelected(cur, r, c) &&
5238 ((next_cell && cur.pit() == cur.lastpit()
5239 && cur.pos() == cur.lastpos())
5240 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
5242 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
5244 // ...try to dispatch to the cell's inset.
5245 cell(cur.idx())->dispatch(cur, cmd);
5247 // When we already have a selection we want to select the whole cell
5248 // before going to the next cell.
5249 if (select_whole && !empty_cell){
5250 getText(cur.idx())->selectAll(cur);
5252 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5256 // FIXME: When we support the selection of an empty cell, remove
5257 // the !empty_cell from this condition. For now we jump to the next
5258 // cell if the current cell is empty.
5259 if (cur.result().dispatched() && !empty_cell)
5263 // move to next/prev cell, as appropriate
5264 // note that we will always do this if we're selecting and we have
5265 // a multicell selection
5266 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
5267 << " cell from: " << int(entry_from));
5269 moveNextCell(cur, entry_from);
5271 movePrevCell(cur, entry_from);
5272 // if we're exiting the table, call the appropriate FINISHED lfun
5273 if (sl == cur.top()) {
5274 cmd = FuncRequest(finish_lfun);
5279 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5284 case LFUN_DOWN_SELECT:
5286 if (!(cur.selection() && cur.selIsMultiCell()))
5287 cell(cur.idx())->dispatch(cur, cmd);
5289 cur.dispatched(); // override the cell's decision
5290 if (sl == cur.top()) {
5291 // if our Text didn't do anything to the cursor
5292 // then we try to put the cursor into the cell below
5293 // setting also the right targetX.
5294 cur.selHandle(act == LFUN_DOWN_SELECT);
5295 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
5296 int const xtarget = cur.targetX();
5297 // WARNING: Once cur.idx() has been reset, the cursor is in
5298 // an inconsistent state until pos() has been set. Be careful
5299 // what you do with it!
5300 cur.idx() = tabular.cellBelow(cur.idx());
5302 TextMetrics const & tm =
5303 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5304 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5305 cur.setCurrentFont();
5308 if (sl == cur.top()) {
5309 // we trick it to go to forward after leaving the
5311 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5314 if (cur.selIsMultiCell()) {
5315 cur.pit() = cur.lastpit();
5316 cur.pos() = cur.lastpos();
5317 cur.setCurrentFont();
5318 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5321 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5324 case LFUN_UP_SELECT:
5326 if (!(cur.selection() && cur.selIsMultiCell()))
5327 cell(cur.idx())->dispatch(cur, cmd);
5328 cur.dispatched(); // override the cell's decision
5329 if (sl == cur.top()) {
5330 // if our Text didn't do anything to the cursor
5331 // then we try to put the cursor into the cell above
5332 // setting also the right targetX.
5333 cur.selHandle(act == LFUN_UP_SELECT);
5334 if (tabular.cellRow(cur.idx()) != 0) {
5335 int const xtarget = cur.targetX();
5336 // WARNING: Once cur.idx() has been reset, the cursor is in
5337 // an inconsistent state until pos() has been set. Be careful
5338 // what you do with it!
5339 cur.idx() = tabular.cellAbove(cur.idx());
5340 cur.pit() = cur.lastpit();
5341 Text const * text = cell(cur.idx())->getText(0);
5342 TextMetrics const & tm = cur.bv().textMetrics(text);
5343 ParagraphMetrics const & pm =
5344 tm.parMetrics(cur.lastpit());
5345 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5346 cur.setCurrentFont();
5349 if (sl == cur.top()) {
5350 cmd = FuncRequest(LFUN_UP);
5353 if (cur.selIsMultiCell()) {
5355 cur.pos() = cur.lastpos();
5356 cur.setCurrentFont();
5357 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5360 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5363 case LFUN_LAYOUT_TABULAR:
5364 cur.bv().showDialog("tabular");
5367 case LFUN_INSET_MODIFY:
5368 // we come from the dialog
5369 if (cmd.getArg(0) == "tabular")
5370 tabularFeatures(cur, cmd.getLongArg(1));
5375 case LFUN_TABULAR_FEATURE:
5376 tabularFeatures(cur, to_utf8(cmd.argument()));
5379 // insert file functions
5380 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5381 case LFUN_FILE_INSERT_PLAINTEXT:
5383 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5384 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5385 FileName(to_utf8(cmd.argument())));
5388 cur.recordUndoInset();
5389 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5390 // content has been replaced,
5391 // so cursor might be invalid
5392 cur.pos() = cur.lastpos();
5393 cur.pit() = cur.lastpit();
5394 bvcur.setCursor(cur);
5401 if (cur.selIsMultiCell())
5404 cell(cur.idx())->dispatch(cur, cmd);
5408 if (cur.selIsMultiCell()) {
5409 if (copySelection(cur)) {
5410 cur.recordUndoInset();
5414 cell(cur.idx())->dispatch(cur, cmd);
5417 case LFUN_SELF_INSERT:
5418 if (cur.selIsMultiCell()) {
5419 cur.recordUndoInset();
5421 BufferView * bv = &cur.bv();
5422 docstring::const_iterator cit = cmd.argument().begin();
5423 docstring::const_iterator const end = cmd.argument().end();
5424 for (; cit != end; ++cit)
5425 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5428 bv->bookmarkEditPosition();
5430 cell(cur.idx())->dispatch(cur, cmd);
5433 case LFUN_CHAR_DELETE_BACKWARD:
5434 case LFUN_CHAR_DELETE_FORWARD:
5435 if (cur.selIsMultiCell()) {
5436 cur.recordUndoInset();
5439 cell(cur.idx())->dispatch(cur, cmd);
5442 case LFUN_CLIPBOARD_PASTE:
5443 case LFUN_PRIMARY_SELECTION_PASTE: {
5444 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5445 theClipboard().getAsText(Clipboard::PlainTextType) :
5446 theSelection().get();
5449 // pass to InsertPlaintextString, but
5450 // only if we have multi-cell content
5451 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5452 cur.recordUndoInset();
5453 if (insertPlaintextString(cur.bv(), clip, false)) {
5454 // content has been replaced,
5455 // so cursor might be invalid
5457 bvcur.setCursor(cur);
5461 // Let the cell handle normal text
5462 cell(cur.idx())->dispatch(cur, cmd);
5467 if (!tabularStackDirty()) {
5468 // Check if we have plain text or HTML with rows/columns.
5469 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5470 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5471 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5472 docstring const clip =
5473 theClipboard().getAsText(Clipboard::AnyTextType);
5474 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5475 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5476 doDispatch(cur, ncmd);
5480 else if (!theClipboard().isInternal()
5481 && theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5482 // This might be tabular data from another LyX instance. Check!
5483 docstring const clip =
5484 theClipboard().getAsText(Clipboard::PlainTextType);
5485 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5486 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5487 doDispatch(cur, ncmd);
5491 if (!cur.selIsMultiCell())
5492 cell(cur.idx())->dispatch(cur, cmd);
5495 if (theClipboard().isInternal()) {
5496 cur.recordUndoInset();
5497 pasteClipboard(cur);
5501 case LFUN_CHANGE_ACCEPT:
5502 case LFUN_CHANGE_REJECT:
5503 case LFUN_FONT_DEFAULT:
5504 case LFUN_FONT_EMPH:
5505 case LFUN_FONT_BOLD:
5506 case LFUN_FONT_BOLDSYMBOL:
5507 case LFUN_FONT_ROMAN:
5508 case LFUN_FONT_NOUN:
5509 case LFUN_FONT_ITAL:
5510 case LFUN_FONT_FRAK:
5511 case LFUN_FONT_TYPEWRITER:
5512 case LFUN_FONT_SANS:
5513 case LFUN_TEXTSTYLE_APPLY:
5514 case LFUN_TEXTSTYLE_UPDATE:
5515 case LFUN_FONT_SIZE:
5516 case LFUN_FONT_UNDERLINE:
5517 case LFUN_FONT_STRIKEOUT:
5518 case LFUN_FONT_CROSSOUT:
5519 case LFUN_FONT_UNDERUNDERLINE:
5520 case LFUN_FONT_UNDERWAVE:
5522 case LFUN_PARAGRAPH_PARAMS_APPLY:
5523 case LFUN_PARAGRAPH_PARAMS:
5524 case LFUN_WORD_CAPITALIZE:
5525 case LFUN_WORD_UPCASE:
5526 case LFUN_WORD_LOWCASE:
5527 case LFUN_CHARS_TRANSPOSE: {
5528 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5529 if (cur.selIsMultiCell()) {
5532 getSelection(cur, rs, re, cs, ce);
5533 Cursor tmpcur = cur;
5534 for (row_type r = rs; r <= re; ++r) {
5535 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5536 // whole row selected
5537 if (act == LFUN_CHANGE_ACCEPT) {
5538 if (tabular.row_info[r].change.inserted())
5539 tabular.row_info[r].change.setUnchanged();
5540 else if (tabular.row_info[r].change.deleted()) {
5541 tabular.deleteRow(r, true);
5546 if (tabular.row_info[r].change.deleted())
5547 tabular.row_info[r].change.setUnchanged();
5548 else if (tabular.row_info[r].change.inserted()) {
5549 tabular.deleteRow(r, true);
5555 for (col_type c = cs; c <= ce; ++c) {
5556 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5557 // whole col selected
5558 if (act == LFUN_CHANGE_ACCEPT) {
5559 if (tabular.column_info[c].change.inserted())
5560 tabular.column_info[c].change.setUnchanged();
5561 else if (tabular.column_info[c].change.deleted()) {
5562 tabular.deleteColumn(c, true);
5567 if (tabular.column_info[c].change.deleted())
5568 tabular.column_info[c].change.setUnchanged();
5569 else if (tabular.column_info[c].change.inserted()) {
5570 tabular.deleteColumn(c, true);
5576 // cursor follows cell:
5577 tmpcur.idx() = tabular.cellIndex(r, c);
5578 // select this cell only:
5581 tmpcur.resetAnchor();
5582 tmpcur.pit() = tmpcur.lastpit();
5583 tmpcur.pos() = tmpcur.top().lastpos();
5584 tmpcur.setCursor(tmpcur);
5585 tmpcur.setSelection();
5586 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5590 tabular.updateIndexes();
5591 // cursor might be invalid
5593 // change bar might need to be redrawn
5594 cur.screenUpdateFlags(Update::Force);
5595 cur.forceBufferUpdate();
5599 cell(cur.idx())->dispatch(cur, cmd);
5604 case LFUN_CHANGE_NEXT:
5605 case LFUN_CHANGE_PREVIOUS: {
5606 // BufferView::dispatch has already moved the cursor, we just
5607 // need to select here if we have a changed row or column
5608 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5610 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5614 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5615 cur.pit() = cur.lastpit();
5616 cur.pos() = cur.lastpos();
5617 cur.selection(true);
5621 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5623 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5627 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5628 cur.pit() = cur.lastpit();
5629 cur.pos() = cur.lastpos();
5630 cur.selection(true);
5637 case LFUN_INSET_SETTINGS:
5638 // relay this lfun to Inset, not to the cell.
5639 Inset::doDispatch(cur, cmd);
5643 // we try to handle this event in the insets dispatch function.
5644 cell(cur.idx())->dispatch(cur, cmd);
5650 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5651 string const & argument, FuncStatus & status) const
5654 int action = Tabular::LAST_ACTION;
5656 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5657 if (tabularFeature[i].feature == s) {
5658 action = tabularFeature[i].action;
5662 if (action == Tabular::LAST_ACTION) {
5664 status.setUnknown(true);
5668 row_type sel_row_start = 0;
5669 row_type sel_row_end = 0;
5670 col_type sel_col_start = 0;
5671 col_type sel_col_end = 0;
5672 Tabular::ltType dummyltt;
5675 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5678 case Tabular::SET_PWIDTH:
5679 case Tabular::SET_MPWIDTH:
5680 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5681 case Tabular::SET_SPECIAL_COLUMN:
5682 case Tabular::SET_SPECIAL_MULTICOLUMN:
5683 case Tabular::APPEND_ROW:
5684 case Tabular::APPEND_COLUMN:
5685 case Tabular::COPY_ROW:
5686 case Tabular::COPY_COLUMN:
5687 case Tabular::SET_TOP_SPACE:
5688 case Tabular::SET_BOTTOM_SPACE:
5689 case Tabular::SET_INTERLINE_SPACE:
5693 case Tabular::DELETE_ROW:
5694 status.setEnabled(tabular.nrows() > 1);
5696 case Tabular::DELETE_COLUMN:
5697 status.setEnabled(tabular.ncols() > 1);
5700 case Tabular::SET_TABULAR_WIDTH:
5701 status.setEnabled(!tabular.rotate
5702 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5705 case Tabular::MOVE_COLUMN_RIGHT:
5706 case Tabular::MOVE_COLUMN_LEFT:
5707 case Tabular::MOVE_ROW_DOWN:
5708 case Tabular::MOVE_ROW_UP: {
5711 if (cur.selIsMultiCell())
5712 getSelection(cur, rs, re, cs, ce);
5714 rs = tabular.cellRow(cur.idx());
5716 cs = tabular.cellColumn(cur.idx());
5719 if ((action == Tabular::MOVE_COLUMN_RIGHT
5720 && tabular.ncols() == ce + 1)
5721 || (action == Tabular::MOVE_COLUMN_LEFT && cs == 0)
5722 || (action == Tabular::MOVE_ROW_DOWN
5723 && tabular.nrows() == re + 1)
5724 || (action == Tabular::MOVE_ROW_UP && rs == 0)) {
5725 status.setEnabled(false);
5729 if (action == Tabular::MOVE_COLUMN_RIGHT
5730 || action == Tabular::MOVE_COLUMN_LEFT) {
5731 bool has_multicol = (action == Tabular::MOVE_COLUMN_RIGHT)
5732 ? tabular.hasMultiColumn(ce + 1)
5733 : tabular.hasMultiColumn(cs - 1);
5734 for (col_type c = cs; c <= ce; ++c) {
5735 if (tabular.hasMultiColumn(c)) {
5736 has_multicol = true;
5741 status.message(_("Column movement not supported with multi-columns."));
5742 status.setEnabled(false);
5747 if (action == Tabular::MOVE_ROW_DOWN
5748 || action == Tabular::MOVE_ROW_UP) {
5749 bool has_multirow = (action == Tabular::MOVE_ROW_DOWN)
5750 ? tabular.hasMultiRow(re + 1)
5751 : tabular.hasMultiRow(rs - 1);
5752 for (row_type r = rs; r <= re; ++r) {
5753 if (tabular.hasMultiRow(r)) {
5754 has_multirow = true;
5759 status.message(_("Row movement not supported with multi-rows."));
5760 status.setEnabled(false);
5765 status.setEnabled(true);
5769 case Tabular::SET_DECIMAL_POINT:
5771 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5774 case Tabular::SET_MULTICOLUMN:
5775 case Tabular::UNSET_MULTICOLUMN:
5776 case Tabular::MULTICOLUMN:
5777 // If a row is set as longtable caption, it must not be allowed
5778 // to unset that this row is a multicolumn.
5779 status.setEnabled(sel_row_start == sel_row_end
5780 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5781 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5784 case Tabular::SET_MULTIROW:
5785 case Tabular::UNSET_MULTIROW:
5786 case Tabular::MULTIROW:
5787 // If a row is set as longtable caption, it must not be allowed
5788 // to unset that this row is a multirow.
5789 status.setEnabled(sel_col_start == sel_col_end
5790 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5791 status.setOnOff(tabular.isMultiRow(cur.idx()));
5794 case Tabular::TOGGLE_INNER_LINES:
5795 status.setOnOff(tabular.innerBorders(sel_row_start, sel_row_end,
5796 sel_col_start, sel_col_end));
5797 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5799 case Tabular::TOGGLE_ALL_LINES:
5800 status.setOnOff(tabular.innerBorders(sel_row_start, sel_row_end,
5801 sel_col_start, sel_col_end)
5802 && tabular.outsideBorders(sel_row_start, sel_row_end,
5803 sel_col_start, sel_col_end));
5804 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5806 case Tabular::SET_ALL_LINES:
5807 case Tabular::UNSET_ALL_LINES:
5808 case Tabular::SET_INNER_LINES:
5809 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5812 case Tabular::TOGGLE_BORDER_LINES:
5813 status.setOnOff(tabular.outsideBorders(sel_row_start, sel_row_end,
5814 sel_col_start, sel_col_end));
5816 case Tabular::SET_BORDER_LINES:
5817 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5820 case Tabular::RESET_FORMAL_DEFAULT:
5821 status.setEnabled(tabular.use_booktabs);
5824 case Tabular::SET_LINE_TOP:
5825 case Tabular::SET_LINE_BOTTOM:
5826 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5829 case Tabular::SET_LINE_LEFT:
5830 case Tabular::SET_LINE_RIGHT:
5831 status.setEnabled(!tabular.use_booktabs
5832 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5835 case Tabular::SET_LTRIM_TOP:
5836 case Tabular::SET_RTRIM_TOP:
5837 status.setEnabled(tabular.use_booktabs
5838 && tabular.cellRow(cur.idx()) != 0
5839 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5842 case Tabular::SET_LTRIM_BOTTOM:
5843 case Tabular::SET_RTRIM_BOTTOM:
5844 status.setEnabled(tabular.use_booktabs
5845 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5846 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5849 case Tabular::TOGGLE_LINE_TOP:
5850 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5851 status.setOnOff(tabular.topLine(cur.idx()));
5854 case Tabular::TOGGLE_LINE_BOTTOM:
5855 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5856 status.setOnOff(tabular.bottomLine(cur.idx()));
5859 case Tabular::TOGGLE_LINE_LEFT:
5860 status.setEnabled(!tabular.use_booktabs
5861 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5862 status.setOnOff(tabular.leftLine(cur.idx()));
5865 case Tabular::TOGGLE_LINE_RIGHT:
5866 status.setEnabled(!tabular.use_booktabs
5867 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5868 status.setOnOff(tabular.rightLine(cur.idx()));
5871 case Tabular::TOGGLE_LTRIM_TOP:
5872 status.setEnabled(tabular.use_booktabs
5873 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5874 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5877 case Tabular::TOGGLE_RTRIM_TOP:
5878 status.setEnabled(tabular.use_booktabs
5879 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5880 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5883 case Tabular::TOGGLE_LTRIM_BOTTOM:
5884 status.setEnabled(tabular.use_booktabs
5885 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5886 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5889 case Tabular::TOGGLE_RTRIM_BOTTOM:
5890 status.setEnabled(tabular.use_booktabs
5891 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5892 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5895 // multirow cells only inherit the alignment of the column if the column has
5896 // no width, otherwise they are left-aligned
5897 // therefore allow always left but right and center only if there is no width
5898 case Tabular::M_ALIGN_LEFT:
5901 case Tabular::ALIGN_LEFT:
5902 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5905 case Tabular::M_ALIGN_RIGHT:
5908 case Tabular::ALIGN_RIGHT:
5909 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5910 && !tabular.getPWidth(cur.idx()).zero()));
5911 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5914 case Tabular::M_ALIGN_CENTER:
5917 case Tabular::ALIGN_CENTER:
5918 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5919 && !tabular.getPWidth(cur.idx()).zero()));
5920 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5923 case Tabular::ALIGN_BLOCK:
5924 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5925 && !tabular.isMultiRow(cur.idx()));
5926 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5929 case Tabular::ALIGN_DECIMAL:
5930 status.setEnabled(!tabular.isMultiRow(cur.idx())
5931 && !tabular.isMultiColumn(cur.idx()));
5932 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5935 case Tabular::M_VALIGN_TOP:
5938 case Tabular::VALIGN_TOP:
5939 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5940 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5942 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5945 case Tabular::M_VALIGN_BOTTOM:
5948 case Tabular::VALIGN_BOTTOM:
5949 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5950 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5952 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5955 case Tabular::M_VALIGN_MIDDLE:
5958 case Tabular::VALIGN_MIDDLE:
5959 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5960 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5962 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5965 case Tabular::SET_LONGTABULAR:
5966 case Tabular::TOGGLE_LONGTABULAR:
5967 // setting as longtable is not allowed when table is inside a float
5968 if (cur.innerInsetOfType(FLOAT_CODE) != nullptr
5969 || cur.innerInsetOfType(WRAP_CODE) != nullptr)
5970 status.setEnabled(false);
5972 status.setEnabled(true);
5973 status.setOnOff(tabular.is_long_tabular);
5976 case Tabular::UNSET_LONGTABULAR:
5977 status.setOnOff(!tabular.is_long_tabular);
5980 case Tabular::TOGGLE_ROTATE_TABULAR:
5981 case Tabular::SET_ROTATE_TABULAR:
5982 status.setOnOff(tabular.rotate != 0);
5985 case Tabular::TABULAR_VALIGN_TOP:
5986 status.setEnabled(tabular.tabular_width.zero());
5987 status.setOnOff(tabular.tabular_valignment
5988 == Tabular::LYX_VALIGN_TOP);
5990 case Tabular::TABULAR_VALIGN_MIDDLE:
5991 status.setEnabled(tabular.tabular_width.zero());
5992 status.setOnOff(tabular.tabular_valignment
5993 == Tabular::LYX_VALIGN_MIDDLE);
5995 case Tabular::TABULAR_VALIGN_BOTTOM:
5996 status.setEnabled(tabular.tabular_width.zero());
5997 status.setOnOff(tabular.tabular_valignment
5998 == Tabular::LYX_VALIGN_BOTTOM);
6001 case Tabular::LONGTABULAR_ALIGN_LEFT:
6002 status.setOnOff(tabular.longtabular_alignment
6003 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
6005 case Tabular::LONGTABULAR_ALIGN_CENTER:
6006 status.setOnOff(tabular.longtabular_alignment
6007 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
6009 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6010 status.setOnOff(tabular.longtabular_alignment
6011 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
6014 case Tabular::UNSET_ROTATE_TABULAR:
6015 status.setOnOff(tabular.rotate == 0);
6018 case Tabular::TOGGLE_ROTATE_CELL:
6019 case Tabular::SET_ROTATE_CELL:
6020 status.setOnOff(!oneCellHasRotationState(false,
6021 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
6024 case Tabular::UNSET_ROTATE_CELL:
6025 status.setOnOff(!oneCellHasRotationState(true,
6026 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
6029 case Tabular::SET_USEBOX:
6030 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
6033 // every row can only be one thing:
6034 // either a footer or header
6035 case Tabular::SET_LTFIRSTHEAD:
6036 status.setEnabled(sel_row_start == sel_row_end);
6037 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
6040 case Tabular::UNSET_LTFIRSTHEAD:
6041 status.setEnabled(sel_row_start == sel_row_end);
6042 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
6045 case Tabular::SET_LTHEAD:
6046 status.setEnabled(sel_row_start == sel_row_end);
6047 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
6050 case Tabular::UNSET_LTHEAD:
6051 status.setEnabled(sel_row_start == sel_row_end);
6052 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
6055 case Tabular::SET_LTFOOT:
6056 status.setEnabled(sel_row_start == sel_row_end);
6057 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
6060 case Tabular::UNSET_LTFOOT:
6061 status.setEnabled(sel_row_start == sel_row_end);
6062 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
6065 case Tabular::SET_LTLASTFOOT:
6066 status.setEnabled(sel_row_start == sel_row_end);
6067 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
6070 case Tabular::UNSET_LTLASTFOOT:
6071 status.setEnabled(sel_row_start == sel_row_end);
6072 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
6075 case Tabular::SET_LTNEWPAGE:
6076 status.setOnOff(tabular.getLTNewPage(sel_row_start));
6078 case Tabular::UNSET_LTNEWPAGE:
6079 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
6082 // only one row in head/firsthead/foot/lasthead can be the caption
6083 // and a multirow cannot be set as caption
6084 case Tabular::SET_LTCAPTION:
6085 status.setEnabled(sel_row_start == sel_row_end
6086 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
6087 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
6088 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
6089 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
6090 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
6091 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
6092 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
6093 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
6094 && !tabular.isMultiRow(sel_row_start));
6095 status.setOnOff(tabular.ltCaption(sel_row_start));
6098 case Tabular::UNSET_LTCAPTION:
6099 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
6102 case Tabular::TOGGLE_LTCAPTION:
6103 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
6104 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
6105 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
6106 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
6107 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
6108 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
6109 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
6110 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
6111 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
6112 status.setOnOff(tabular.ltCaption(sel_row_start));
6115 case Tabular::TOGGLE_BOOKTABS:
6116 case Tabular::SET_BOOKTABS:
6117 status.setOnOff(tabular.use_booktabs);
6120 case Tabular::UNSET_BOOKTABS:
6121 status.setOnOff(!tabular.use_booktabs);
6126 status.setEnabled(false);
6133 // function sets an object as defined in FuncStatus.h:
6134 // states OK, Unknown, Disabled, On, Off.
6135 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
6136 FuncStatus & status) const
6138 switch (cmd.action()) {
6139 case LFUN_INSET_MODIFY:
6140 if (cmd.getArg(0) != "tabular")
6142 if (cmd.getArg(1) == "for-dialog") {
6143 // The dialog is asking the status of a command
6144 if (&cur.inset() != this)
6146 string action = cmd.getArg(2);
6147 string arg = cmd.getLongArg(3);
6148 return getFeatureStatus(cur, action, arg, status);
6150 // We always enable the lfun if it is coming from the dialog
6151 // because the dialog makes sure all the settings are valid,
6152 // even though the first argument might not be valid now.
6153 status.setEnabled(true);
6157 case LFUN_TABULAR_FEATURE: {
6158 if (&cur.inset() != this)
6160 string action = cmd.getArg(0);
6161 string arg = cmd.getLongArg(1);
6162 return getFeatureStatus(cur, action, arg, status);
6165 case LFUN_CAPTION_INSERT: {
6166 // caption is only allowed in caption cell of longtable
6167 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
6168 status.setEnabled(false);
6171 // only standard caption is allowed
6172 string arg = cmd.getArg(0);
6173 if (!arg.empty() && arg != "Standard") {
6174 status.setEnabled(false);
6177 // check if there is already a caption
6178 bool have_caption = false;
6179 InsetTableCell itc = *tabular.cellInset(cur.idx());
6180 ParagraphList::const_iterator pit = itc.paragraphs().begin();
6181 ParagraphList::const_iterator pend = itc.paragraphs().end();
6182 for (; pit != pend; ++pit) {
6183 InsetList::const_iterator it = pit->insetList().begin();
6184 InsetList::const_iterator end = pit->insetList().end();
6185 for (; it != end; ++it) {
6186 if (it->inset->lyxCode() == CAPTION_CODE) {
6187 have_caption = true;
6192 status.setEnabled(!have_caption);
6196 // These are only enabled inside tabular
6197 case LFUN_CELL_BACKWARD:
6198 case LFUN_CELL_FORWARD:
6199 status.setEnabled(true);
6202 // disable these with multiple cells selected
6203 case LFUN_INSET_INSERT:
6204 case LFUN_TABULAR_INSERT:
6205 case LFUN_TABULAR_STYLE_INSERT:
6206 case LFUN_FLEX_INSERT:
6207 case LFUN_FLOAT_INSERT:
6208 case LFUN_FLOAT_WIDE_INSERT:
6209 case LFUN_FOOTNOTE_INSERT:
6210 case LFUN_MARGINALNOTE_INSERT:
6211 case LFUN_MATH_INSERT:
6212 case LFUN_MATH_MODE:
6213 case LFUN_MATH_MUTATE:
6214 case LFUN_MATH_DISPLAY:
6215 case LFUN_NOTE_INSERT:
6216 case LFUN_ARGUMENT_INSERT:
6217 case LFUN_BOX_INSERT:
6218 case LFUN_BRANCH_INSERT:
6219 case LFUN_PHANTOM_INSERT:
6220 case LFUN_WRAP_INSERT:
6221 case LFUN_PREVIEW_INSERT:
6222 case LFUN_ERT_INSERT: {
6223 if (cur.selIsMultiCell()) {
6224 status.setEnabled(false);
6227 return cell(cur.idx())->getStatus(cur, cmd, status);
6230 case LFUN_CHANGE_ACCEPT:
6231 case LFUN_CHANGE_REJECT: {
6232 if (cur.selIsMultiCell()) {
6235 getSelection(cur, rs, re, cs, ce);
6236 for (row_type r = rs; r <= re; ++r) {
6237 if (tabular.row_info[r].change.changed()) {
6238 status.setEnabled(true);
6241 for (col_type c = cs; c <= ce; ++c) {
6242 if (tabular.column_info[c].change.changed()) {
6243 status.setEnabled(true);
6249 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
6250 status.setEnabled(true);
6253 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
6254 status.setEnabled(true);
6258 return cell(cur.idx())->getStatus(cur, cmd, status);
6261 case LFUN_PARAGRAPH_BREAK:
6262 case LFUN_NEWLINE_INSERT:
6263 return cell(cur.idx())->getStatus(cur, cmd, status);
6265 case LFUN_NEWPAGE_INSERT:
6266 status.setEnabled(false);
6270 if (tabularStackDirty() && theClipboard().isInternal()) {
6271 if (cur.selIsMultiCell()) {
6274 getSelection(cur, rs, re, cs, ce);
6275 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
6276 && paste_tabular->nrows() == re - rs + 1)
6277 status.setEnabled(true);
6279 status.setEnabled(false);
6280 status.message(_("Selection size should match clipboard content."));
6283 status.setEnabled(true);
6286 return cell(cur.idx())->getStatus(cur, cmd, status);
6288 case LFUN_INSET_SETTINGS:
6289 // relay this lfun to Inset, not to the cell.
6290 return Inset::getStatus(cur, cmd, status);
6293 // we try to handle this event in the insets dispatch function.
6294 return cell(cur.idx())->getStatus(cur, cmd, status);
6300 int InsetTabular::rowFlags() const
6302 if (tabular.is_long_tabular) {
6303 switch (tabular.longtabular_alignment) {
6304 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
6305 return Display | AlignLeft;
6306 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
6308 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
6309 return Display | AlignRight;
6318 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
6320 tabular.latex(os, runparams);
6324 int InsetTabular::plaintext(odocstringstream & os,
6325 OutputParams const & runparams, size_t max_length) const
6327 os << '\n'; // output table on a new line
6328 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
6329 tabular.plaintext(os, runparams, dp, false, 0, max_length);
6330 return PLAINTEXT_NEWLINE;
6334 void InsetTabular::docbook(XMLStream & xs, OutputParams const & runparams) const
6336 tabular.docbook(xs, runparams);
6340 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6342 return tabular.xhtml(xs, rp);
6346 void InsetTabular::validate(LaTeXFeatures & features) const
6348 tabular.validate(features);
6349 features.useInsetLayout(getLayout());
6353 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6355 return tabular.cellInset(idx);
6359 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6361 return tabular.cellInset(idx);
6365 void InsetTabular::cursorPos(BufferView const & bv,
6366 CursorSlice const & sl, bool boundary, int & x, int & y) const
6368 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6370 // y offset correction
6371 y += cellYPos(sl.idx());
6372 y += tabular.textVOffset(sl.idx());
6373 y += tabular.offsetVAlignment();
6375 // x offset correction
6376 x += cellXPos(sl.idx());
6377 x += tabular.textHOffset(sl.idx());
6378 x += ADD_TO_TABULAR_WIDTH;
6382 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6386 Inset const & inset = *tabular.cellInset(cell);
6387 Point o = bv.coordCache().getInsets().xy(&inset);
6388 int const xbeg = o.x_ - tabular.textHOffset(cell);
6389 int const xend = xbeg + tabular.cellWidth(cell);
6390 row_type const row = tabular.cellRow(cell);
6391 int const ybeg = o.y_ - tabular.rowAscent(row)
6392 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6393 int const yend = ybeg + tabular.cellHeight(cell);
6405 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6406 // << " ybeg=" << ybeg << " yend=" << yend
6407 // << " xx=" << xx << " yy=" << yy
6408 // << " dist=" << xx + yy << endl;
6413 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6415 //lyxerr << "InsetTabular::editXY: " << this << endl;
6417 cur.idx() = getNearestCell(cur.bv(), x, y);
6418 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6422 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6424 cur.idx() = getNearestCell(cur.bv(), x, y);
6425 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6429 idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6431 idx_type idx_min = 0;
6432 int dist_min = numeric_limits<int>::max();
6433 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6434 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6435 int const d = dist(bv, i, x, y);
6446 int InsetTabular::cellYPos(idx_type const cell) const
6448 row_type row = tabular.cellRow(cell);
6450 for (row_type r = 0; r < row; ++r)
6451 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6452 + tabular.interRowSpace(r + 1);
6457 int InsetTabular::cellXPos(idx_type const cell) const
6459 col_type col = tabular.cellColumn(cell);
6461 for (col_type c = 0; c < col; ++c)
6462 lx += tabular.column_info[c].width;
6467 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6469 row_type const row = tabular.cellRow(cur.idx());
6470 col_type const col = tabular.cellColumn(cur.idx());
6472 if (isRightToLeft(cur)) {
6473 if (tabular.cellColumn(cur.idx()) == 0) {
6474 if (row == tabular.nrows() - 1)
6476 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6481 cur.idx() = tabular.getLastCellInRow(row - 1);
6483 cur.idx() = tabular.cellIndex(row, col - 1);
6486 if (tabular.isLastCell(cur.idx()))
6488 if (cur.idx() == tabular.getLastCellInRow(row))
6489 cur.idx() = tabular.cellIndex(row + 1, 0);
6491 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6492 cur.idx() = tabular.cellIndex(row, colnextcell);
6496 cur.boundary(false);
6498 if (cur.selIsMultiCell()) {
6499 cur.pit() = cur.lastpit();
6500 cur.pos() = cur.lastpos();
6507 // in visual mode, place cursor at extreme left or right
6509 switch(entry_from) {
6511 case ENTRY_DIRECTION_RIGHT:
6512 cur.posVisToRowExtremity(false /* !left */);
6514 case ENTRY_DIRECTION_LEFT:
6515 cur.posVisToRowExtremity(true /* left */);
6517 case ENTRY_DIRECTION_IGNORE:
6518 // nothing to do in this case
6522 cur.setCurrentFont();
6526 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6528 row_type const row = tabular.cellRow(cur.idx());
6529 col_type const col = tabular.cellColumn(cur.idx());
6531 if (isRightToLeft(cur)) {
6532 if (cur.idx() == tabular.getLastCellInRow(row)) {
6535 cur.idx() = tabular.getFirstCellInRow(row);
6536 cur.idx() = tabular.cellAbove(cur.idx());
6538 if (tabular.isLastCell(cur.idx()))
6540 if (cur.idx() == tabular.getLastCellInRow(row))
6541 cur.idx() = tabular.cellIndex(row + 1, 0);
6543 cur.idx() = tabular.cellIndex(row, col + 1);
6546 if (cur.idx() == 0) // first cell
6549 cur.idx() = tabular.getLastCellInRow(row - 1);
6551 cur.idx() = tabular.cellIndex(row, col - 1);
6554 if (cur.selIsMultiCell()) {
6555 cur.pit() = cur.lastpit();
6556 cur.pos() = cur.lastpos();
6560 cur.pit() = cur.lastpit();
6561 cur.pos() = cur.lastpos();
6563 // in visual mode, place cursor at extreme left or right
6565 switch(entry_from) {
6567 case ENTRY_DIRECTION_RIGHT:
6568 cur.posVisToRowExtremity(false /* !left */);
6570 case ENTRY_DIRECTION_LEFT:
6571 cur.posVisToRowExtremity(true /* left */);
6573 case ENTRY_DIRECTION_IGNORE:
6574 // nothing to do in this case
6578 cur.setCurrentFont();
6582 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6584 cur.recordUndoInset(this);
6586 istringstream is(argument);
6587 // limit the size of strings we read to avoid memory problems
6591 size_t safe_guard = 0;
6596 if (safe_guard > 1000) {
6597 LYXERR0("parameter max count reached!");
6601 Tabular::Feature action = Tabular::LAST_ACTION;
6604 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6605 if (s != tabularFeature[i].feature)
6607 action = tabularFeature[i].action;
6610 if (action == Tabular::LAST_ACTION) {
6611 LYXERR0("Feature not found " << s);
6615 if (tabularFeature[i].need_value)
6617 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6618 tabularFeatures(cur, action, val);
6623 static void checkLongtableSpecial(Tabular::ltType & ltt,
6624 string const & special, bool & flag)
6626 if (special == "dl_above") {
6629 } else if (special == "dl_below") {
6630 ltt.bottomDL = flag;
6632 } else if (special == "empty") {
6642 bool InsetTabular::oneCellHasRotationState(bool rotated,
6643 row_type row_start, row_type row_end,
6644 col_type col_start, col_type col_end) const
6646 for (row_type r = row_start; r <= row_end; ++r)
6647 for (col_type c = col_start; c <= col_end; ++c)
6649 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6652 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6659 void InsetTabular::tabularFeatures(Cursor & cur,
6660 Tabular::Feature feature, string const & value)
6662 col_type sel_col_start;
6663 col_type sel_col_end;
6664 row_type sel_row_start;
6665 row_type sel_row_end;
6666 bool setLines = false;
6667 bool toggle = false;
6668 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6669 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6673 case Tabular::M_ALIGN_LEFT:
6674 case Tabular::ALIGN_LEFT:
6675 setAlign = LYX_ALIGN_LEFT;
6678 case Tabular::M_ALIGN_RIGHT:
6679 case Tabular::ALIGN_RIGHT:
6680 setAlign = LYX_ALIGN_RIGHT;
6683 case Tabular::M_ALIGN_CENTER:
6684 case Tabular::ALIGN_CENTER:
6685 setAlign = LYX_ALIGN_CENTER;
6688 case Tabular::ALIGN_BLOCK:
6689 setAlign = LYX_ALIGN_BLOCK;
6692 case Tabular::ALIGN_DECIMAL:
6693 setAlign = LYX_ALIGN_DECIMAL;
6696 case Tabular::M_VALIGN_TOP:
6697 case Tabular::VALIGN_TOP:
6698 setVAlign = Tabular::LYX_VALIGN_TOP;
6701 case Tabular::M_VALIGN_BOTTOM:
6702 case Tabular::VALIGN_BOTTOM:
6703 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6706 case Tabular::M_VALIGN_MIDDLE:
6707 case Tabular::VALIGN_MIDDLE:
6708 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6715 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6716 row_type const row = tabular.cellRow(cur.idx());
6717 col_type const column = tabular.cellColumn(cur.idx());
6719 Tabular::ltType ltt;
6723 case Tabular::SET_TABULAR_WIDTH:
6724 tabular.setTabularWidth(Length(value));
6727 case Tabular::SET_PWIDTH: {
6728 Length const len(value);
6729 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6730 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6732 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6733 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6738 case Tabular::SET_MPWIDTH:
6739 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6742 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6743 bool const varwidth = value == "on";
6744 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6745 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6749 case Tabular::SET_MROFFSET:
6750 tabular.setMROffset(cur, cur.idx(), Length(value));
6753 case Tabular::SET_SPECIAL_COLUMN:
6754 case Tabular::SET_SPECIAL_MULTICOLUMN:
6755 if (value == "none")
6756 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6758 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6761 case Tabular::APPEND_ROW:
6762 // append the row into the tabular
6763 tabular.appendRow(row);
6766 case Tabular::APPEND_COLUMN:
6767 // append the column into the tabular
6768 tabular.appendColumn(column);
6769 cur.idx() = tabular.cellIndex(row, column);
6772 case Tabular::DELETE_ROW:
6773 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6774 for (col_type c = 0; c < tabular.ncols(); c++) {
6775 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6776 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6777 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6778 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6782 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6783 tabular.deleteRow(r);
6784 if (sel_row_start >= tabular.nrows())
6786 cur.idx() = tabular.cellIndex(sel_row_start, column);
6789 cur.selection(false);
6792 case Tabular::DELETE_COLUMN:
6793 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6794 for (row_type r = 0; r < tabular.nrows(); r++)
6795 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6796 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6799 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6800 for (row_type r = 0; r < tabular.nrows(); r++)
6801 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6802 tabular.leftLine(tabular.cellIndex(r, 0)));
6805 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6806 tabular.deleteColumn(c);
6807 if (sel_col_start >= tabular.ncols())
6809 cur.idx() = tabular.cellIndex(row, sel_col_start);
6812 cur.selection(false);
6815 case Tabular::COPY_ROW:
6816 tabular.copyRow(row);
6819 case Tabular::COPY_COLUMN:
6820 tabular.copyColumn(column);
6821 cur.idx() = tabular.cellIndex(row, column);
6824 case Tabular::MOVE_COLUMN_RIGHT:
6825 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::RIGHT);
6826 if (cur.selection()) {
6827 cur.selection(false);
6828 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start + 1);
6830 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end + 1);
6833 cur.idx() = tabular.cellIndex(row, column + 1);
6836 case Tabular::MOVE_COLUMN_LEFT:
6837 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::LEFT);
6838 if (cur.selection()) {
6839 cur.selection(false);
6840 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start - 1);
6842 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end - 1);
6845 cur.idx() = tabular.cellIndex(row, column - 1);
6848 case Tabular::MOVE_ROW_DOWN:
6849 tabular.moveRow(sel_row_start, sel_row_end, Tabular::DOWN);
6850 if (cur.selection()) {
6851 cur.selection(false);
6852 cur.idx() = tabular.cellIndex(sel_row_start + 1, sel_col_start);
6854 cur.idx() = tabular.cellIndex(sel_row_end + 1, sel_col_end);
6857 cur.idx() = tabular.cellIndex(row + 1, column);
6860 case Tabular::MOVE_ROW_UP:
6861 tabular.moveRow(sel_row_start, sel_row_end, Tabular::UP);
6862 if (cur.selection()) {
6863 cur.selection(false);
6864 cur.idx() = tabular.cellIndex(sel_row_start - 1, sel_col_start);
6866 cur.idx() = tabular.cellIndex(sel_row_end - 1, sel_col_end);
6869 cur.idx() = tabular.cellIndex(row - 1, column);
6872 case Tabular::SET_LINE_TOP:
6873 case Tabular::TOGGLE_LINE_TOP: {
6874 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6875 ? (value == "true") : !tabular.topLine(cur.idx());
6876 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6877 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6878 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6882 case Tabular::SET_LINE_BOTTOM:
6883 case Tabular::TOGGLE_LINE_BOTTOM: {
6884 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6885 ? (value == "true") : !tabular.bottomLine(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.setBottomLine(tabular.cellIndex(r, c), lineSet);
6892 case Tabular::SET_LTRIM_TOP:
6893 case Tabular::TOGGLE_LTRIM_TOP: {
6894 bool l = (feature == Tabular::SET_LTRIM_TOP)
6895 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
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.setTopLineLTrim(tabular.cellIndex(r, c), l);
6902 case Tabular::SET_RTRIM_TOP:
6903 case Tabular::TOGGLE_RTRIM_TOP: {
6904 bool l = (feature == Tabular::SET_RTRIM_TOP)
6905 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
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.setTopLineRTrim(tabular.cellIndex(r, c), l);
6912 case Tabular::SET_LTRIM_BOTTOM:
6913 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6914 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6915 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
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.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6922 case Tabular::SET_RTRIM_BOTTOM:
6923 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6924 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6925 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
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.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6932 case Tabular::SET_LINE_LEFT:
6933 case Tabular::TOGGLE_LINE_LEFT: {
6934 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6935 ? (value == "true") : !tabular.leftLine(cur.idx());
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.setLeftLine(tabular.cellIndex(r, c), lineSet);
6942 case Tabular::SET_LINE_RIGHT:
6943 case Tabular::TOGGLE_LINE_RIGHT: {
6944 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6945 ? (value == "true") : !tabular.rightLine(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.setRightLine(tabular.cellIndex(r, c), lineSet);
6952 case Tabular::M_ALIGN_LEFT:
6953 case Tabular::M_ALIGN_RIGHT:
6954 case Tabular::M_ALIGN_CENTER:
6955 case Tabular::ALIGN_LEFT:
6956 case Tabular::ALIGN_RIGHT:
6957 case Tabular::ALIGN_CENTER:
6958 case Tabular::ALIGN_BLOCK:
6959 case Tabular::ALIGN_DECIMAL:
6960 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6961 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6962 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6963 !tabular.getPWidth(c).zero());
6966 case Tabular::M_VALIGN_TOP:
6967 case Tabular::M_VALIGN_BOTTOM:
6968 case Tabular::M_VALIGN_MIDDLE:
6971 case Tabular::VALIGN_TOP:
6972 case Tabular::VALIGN_BOTTOM:
6973 case Tabular::VALIGN_MIDDLE:
6974 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6975 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6976 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6979 case Tabular::SET_MULTICOLUMN: {
6980 if (!cur.selection()) {
6981 // just multicol for one single cell
6982 // check whether we are completely in a multicol
6983 if (!tabular.isMultiColumn(cur.idx()))
6984 tabular.setMultiColumn(cur, cur.idx(), 1,
6985 tabular.rightLine(cur.idx()));
6988 // we have a selection so this means we just add all these
6989 // cells to form a multicolumn cell
6990 idx_type const s_start = cur.selBegin().idx();
6991 row_type const col_start = tabular.cellColumn(s_start);
6992 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6993 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6994 tabular.rightLine(cur.selEnd().idx()));
6997 cur.selection(false);
7001 case Tabular::UNSET_MULTICOLUMN: {
7002 if (!cur.selection()) {
7003 if (tabular.isMultiColumn(cur.idx()))
7004 tabular.unsetMultiColumn(cur.idx());
7009 case Tabular::MULTICOLUMN: {
7010 if (!cur.selection()) {
7011 if (tabular.isMultiColumn(cur.idx()))
7012 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
7014 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
7018 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
7019 row_type const r = sel_row_start;
7020 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
7021 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
7024 // If the selection contains at least one singlecol cell
7025 // or multiple multicol cells,
7026 // we assume the user will merge is to a single multicol
7028 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
7030 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
7034 case Tabular::SET_MULTIROW: {
7035 if (!cur.selection()) {
7036 // just multirow for one single cell
7037 // check whether we are completely in a multirow
7038 if (!tabular.isMultiRow(cur.idx()))
7039 tabular.setMultiRow(cur, cur.idx(), 1,
7040 tabular.bottomLine(cur.idx()),
7041 tabular.getAlignment(cur.idx()));
7044 // we have a selection so this means we just add all this
7045 // cells to form a multirow cell
7046 idx_type const s_start = cur.selBegin().idx();
7047 row_type const row_start = tabular.cellRow(s_start);
7048 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
7049 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
7050 tabular.bottomLine(cur.selEnd().idx()),
7051 tabular.getAlignment(cur.selEnd().idx()));
7054 cur.selection(false);
7058 case Tabular::UNSET_MULTIROW: {
7059 if (!cur.selection()) {
7060 if (tabular.isMultiRow(cur.idx()))
7061 tabular.unsetMultiRow(cur.idx());
7066 case Tabular::MULTIROW: {
7067 if (!cur.selection()) {
7068 if (tabular.isMultiRow(cur.idx()))
7069 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
7071 tabularFeatures(cur, Tabular::SET_MULTIROW);
7075 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7076 col_type const c = sel_col_start;
7077 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
7078 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
7081 // If the selection contains at least one singlerow cell
7082 // or multiple multirow cells,
7083 // we assume the user will merge is to a single multirow
7085 tabularFeatures(cur, Tabular::SET_MULTIROW);
7087 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
7091 case Tabular::TOGGLE_INNER_LINES:
7094 case Tabular::SET_INNER_LINES:
7096 setLines = !tabular.innerBorders(sel_row_start, sel_row_end,
7097 sel_col_start, sel_col_end);
7100 tabular.setLines(sel_row_start, sel_row_end,
7101 sel_col_start, sel_col_end,
7105 case Tabular::TOGGLE_ALL_LINES:
7108 case Tabular::SET_ALL_LINES:
7110 setLines = !tabular.innerBorders(sel_row_start, sel_row_end,
7111 sel_col_start, sel_col_end)
7112 || !tabular.outsideBorders(sel_row_start, sel_row_end,
7113 sel_col_start, sel_col_end);
7117 case Tabular::UNSET_ALL_LINES:
7118 tabular.setLines(sel_row_start, sel_row_end,
7119 sel_col_start, sel_col_end,
7123 case Tabular::RESET_FORMAL_DEFAULT:
7124 for (row_type r = 0; r < tabular.nrows(); ++r) {
7125 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
7126 for (col_type c = 0; c < tabular.ncols(); ++c) {
7127 idx_type const cell = tabular.cellIndex(r, c);
7128 tabular.setTopLine(cell, head_or_foot);
7129 tabular.setBottomLine(cell, head_or_foot);
7134 case Tabular::TOGGLE_BORDER_LINES:
7137 case Tabular::SET_BORDER_LINES: {
7138 bool const border = toggle &&
7139 tabular.outsideBorders(sel_row_start, sel_row_end,
7140 sel_col_start, sel_col_end)
7142 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7143 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), border);
7144 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), border);
7146 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
7147 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), border);
7148 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), border);
7153 case Tabular::TOGGLE_LONGTABULAR:
7154 if (tabular.is_long_tabular)
7155 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
7157 tabular.is_long_tabular = true;
7160 case Tabular::SET_LONGTABULAR:
7161 tabular.is_long_tabular = true;
7164 case Tabular::UNSET_LONGTABULAR:
7165 for (row_type r = 0; r < tabular.nrows(); ++r) {
7166 if (tabular.ltCaption(r)) {
7167 cur.idx() = tabular.cellIndex(r, 0);
7170 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
7173 tabular.is_long_tabular = false;
7176 case Tabular::SET_ROTATE_TABULAR:
7177 tabular.rotate = convert<int>(value);
7180 case Tabular::UNSET_ROTATE_TABULAR:
7184 case Tabular::TOGGLE_ROTATE_TABULAR:
7185 // when pressing the rotate button we default to 90° rotation
7186 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
7189 case Tabular::TABULAR_VALIGN_TOP:
7190 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
7193 case Tabular::TABULAR_VALIGN_MIDDLE:
7194 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
7197 case Tabular::TABULAR_VALIGN_BOTTOM:
7198 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
7201 case Tabular::LONGTABULAR_ALIGN_LEFT:
7202 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
7205 case Tabular::LONGTABULAR_ALIGN_CENTER:
7206 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
7209 case Tabular::LONGTABULAR_ALIGN_RIGHT:
7210 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
7213 case Tabular::SET_ROTATE_CELL:
7214 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7215 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7216 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
7219 case Tabular::UNSET_ROTATE_CELL:
7220 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7221 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7222 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7225 case Tabular::TOGGLE_ROTATE_CELL:
7227 bool oneNotRotated = oneCellHasRotationState(false,
7228 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
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 // when pressing the rotate cell button we default to 90° rotation
7234 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
7236 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7241 case Tabular::SET_USEBOX: {
7242 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
7243 if (val == tabular.getUsebox(cur.idx()))
7244 val = Tabular::BOX_NONE;
7245 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7246 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7247 tabular.setUsebox(tabular.cellIndex(r, c), val);
7251 case Tabular::UNSET_LTFIRSTHEAD:
7254 case Tabular::SET_LTFIRSTHEAD:
7255 tabular.getRowOfLTFirstHead(row, ltt);
7256 checkLongtableSpecial(ltt, value, flag);
7257 tabular.setLTHead(row, flag, ltt, true);
7260 case Tabular::UNSET_LTHEAD:
7263 case Tabular::SET_LTHEAD:
7264 tabular.getRowOfLTHead(row, ltt);
7265 checkLongtableSpecial(ltt, value, flag);
7266 tabular.setLTHead(row, flag, ltt, false);
7269 case Tabular::UNSET_LTFOOT:
7272 case Tabular::SET_LTFOOT:
7273 tabular.getRowOfLTFoot(row, ltt);
7274 checkLongtableSpecial(ltt, value, flag);
7275 tabular.setLTFoot(row, flag, ltt, false);
7278 case Tabular::UNSET_LTLASTFOOT:
7281 case Tabular::SET_LTLASTFOOT:
7282 tabular.getRowOfLTLastFoot(row, ltt);
7283 checkLongtableSpecial(ltt, value, flag);
7284 tabular.setLTFoot(row, flag, ltt, true);
7287 case Tabular::UNSET_LTNEWPAGE:
7290 case Tabular::SET_LTNEWPAGE:
7291 tabular.setLTNewPage(row, flag);
7294 case Tabular::SET_LTCAPTION: {
7295 if (tabular.ltCaption(row))
7297 cur.idx() = tabular.setLTCaption(cur, row, true);
7300 cur.selection(false);
7301 // update the captionRow status of all cells
7302 tabular.updateIndexes();
7303 // If a row is set as caption, then also insert
7304 // a caption. Otherwise the LaTeX output is broken.
7305 // Select cell if it is non-empty
7306 if (cur.lastpos() > 0 || cur.lastpit() > 0)
7307 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
7308 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
7312 case Tabular::UNSET_LTCAPTION: {
7313 if (!tabular.ltCaption(row))
7315 cur.idx() = tabular.setLTCaption(cur, row, false);
7318 cur.selection(false);
7319 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
7320 if (lyx::getStatus(fr).enabled())
7322 // update the captionRow status of all cells
7323 tabular.updateIndexes();
7327 case Tabular::TOGGLE_LTCAPTION: {
7328 if (tabular.ltCaption(row))
7329 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
7331 tabularFeatures(cur, Tabular::SET_LTCAPTION);
7335 case Tabular::TOGGLE_BOOKTABS:
7336 tabular.use_booktabs = !tabular.use_booktabs;
7339 case Tabular::SET_BOOKTABS:
7340 tabular.use_booktabs = true;
7343 case Tabular::UNSET_BOOKTABS:
7344 tabular.use_booktabs = false;
7347 case Tabular::SET_TOP_SPACE: {
7349 if (value == "default")
7350 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7351 tabular.row_info[r].top_space_default = true;
7352 else if (value == "none")
7353 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7354 tabular.row_info[r].top_space_default = false;
7355 tabular.row_info[r].top_space = len;
7357 else if (isValidLength(value, &len))
7358 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7359 tabular.row_info[r].top_space_default = false;
7360 tabular.row_info[r].top_space = len;
7365 case Tabular::SET_BOTTOM_SPACE: {
7367 if (value == "default")
7368 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7369 tabular.row_info[r].bottom_space_default = true;
7370 else if (value == "none")
7371 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7372 tabular.row_info[r].bottom_space_default = false;
7373 tabular.row_info[r].bottom_space = len;
7375 else if (isValidLength(value, &len))
7376 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7377 tabular.row_info[r].bottom_space_default = false;
7378 tabular.row_info[r].bottom_space = len;
7383 case Tabular::SET_INTERLINE_SPACE: {
7385 if (value == "default")
7386 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7387 tabular.row_info[r].interline_space_default = true;
7388 else if (value == "none")
7389 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7390 tabular.row_info[r].interline_space_default = false;
7391 tabular.row_info[r].interline_space = len;
7393 else if (isValidLength(value, &len))
7394 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7395 tabular.row_info[r].interline_space_default = false;
7396 tabular.row_info[r].interline_space = len;
7401 case Tabular::SET_DECIMAL_POINT:
7402 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7403 tabular.column_info[c].decimal_point = from_utf8(value);
7406 // dummy stuff just to avoid warnings
7407 case Tabular::LAST_ACTION:
7413 bool InsetTabular::copySelection(Cursor & cur)
7415 if (!cur.selection())
7420 getSelection(cur, rs, re, cs, ce);
7422 paste_tabular.reset(new Tabular(tabular));
7424 for (row_type r = 0; r < rs; ++r)
7425 paste_tabular->deleteRow(0, true);
7427 row_type const rows = re - rs + 1;
7428 while (paste_tabular->nrows() > rows)
7429 paste_tabular->deleteRow(rows, true);
7431 for (col_type c = 0; c < cs; ++c)
7432 paste_tabular->deleteColumn(0, true);
7434 col_type const columns = ce - cs + 1;
7435 while (paste_tabular->ncols() > columns)
7436 paste_tabular->deleteColumn(columns, true);
7438 paste_tabular->setBuffer(tabular.buffer());
7440 odocstringstream os;
7441 OutputParams const runparams(nullptr);
7442 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7443 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7444 cap::copySelection(cur, os.str());
7446 // mark tabular stack dirty
7447 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7448 // when we (hopefully) have a one-for-all paste mechanism.
7449 // This must be called after cap::copySelection.
7450 dirtyTabularStack(true);
7456 bool InsetTabular::pasteClipboard(Cursor & cur)
7460 col_type actcol = tabular.cellColumn(cur.idx());
7461 row_type actrow = tabular.cellRow(cur.idx());
7463 if (cur.selIsMultiCell()) {
7466 getSelection(cur, actrow, re, actcol, ce);
7469 col_type const oldncols = tabular.ncols();
7470 for (row_type r1 = 0, r2 = actrow; r1 < paste_tabular->nrows(); ++r1, ++r2) {
7471 // Append rows if needed
7472 if (r2 == tabular.nrows())
7473 tabular.insertRow(r2 - 1, false);
7474 for (col_type c1 = 0, c2 = actcol; c1 < paste_tabular->ncols(); ++c1, ++c2) {
7475 // Append columns if needed
7476 if (c2 == tabular.ncols())
7477 tabular.insertColumn(c2 - 1, false);
7478 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7479 tabular.isPartOfMultiColumn(r2, c2))
7481 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7485 if (tabular.isPartOfMultiColumn(r2, c2)) {
7489 shared_ptr<InsetTableCell> inset(
7490 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7491 tabular.setCellInset(r2, c2, inset);
7492 // FIXME?: why do we need to do this explicitly? (EL)
7493 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7495 if (lyxrc.ct_markup_copied) {
7496 // Only change to inserted if ct is active,
7497 // otherwise leave markup as is
7498 if (buffer().params().track_changes)
7499 inset->setChange(Change(Change::INSERTED));
7501 // Resolve all markup to inserted or unchanged
7502 inset->setChange(Change(buffer().params().track_changes ?
7503 Change::INSERTED : Change::UNCHANGED));
7508 // amend cursor position if cols have been appended
7509 cur.idx() += actrow * (tabular.ncols() - oldncols);
7514 void InsetTabular::cutSelection(Cursor & cur)
7516 if (!cur.selection())
7521 getSelection(cur, rs, re, cs, ce);
7522 for (row_type r = rs; r <= re; ++r) {
7523 for (col_type c = cs; c <= ce; ++c) {
7524 shared_ptr<InsetTableCell> t
7525 = cell(tabular.cellIndex(r, c));
7526 if (buffer().params().track_changes)
7527 // FIXME: Change tracking (MG)
7528 t->setChange(Change(Change::DELETED));
7534 // cursor position might be invalid now
7535 if (cur.pit() > cur.lastpit())
7536 cur.pit() = cur.lastpit();
7537 if (cur.pos() > cur.lastpos())
7538 cur.pos() = cur.lastpos();
7539 cur.clearSelection();
7543 bool InsetTabular::isRightToLeft(Cursor & cur) const
7545 // LASSERT: It might be better to abandon this Buffer.
7546 LASSERT(cur.depth() > 1, return false);
7547 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7548 pos_type const parentpos = cur[cur.depth() - 2].pos();
7549 return parentpar.getFontSettings(buffer().params(),
7550 parentpos).language()->rightToLeft();
7554 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7557 LASSERT(stidx <= enidx, return docstring());
7559 col_type col1 = tabular.cellColumn(stidx);
7560 col_type col2 = tabular.cellColumn(enidx);
7561 row_type row1 = tabular.cellRow(stidx);
7562 row_type row2 = tabular.cellRow(enidx);
7563 // stidx might be in a later column or row than enidx
7569 for (col_type col = col1; col <= col2; col++)
7570 for (row_type row = row1; row <= row2; row++) {
7575 retval += tabular.cellInset(row, col)->asString(intoInsets);
7581 ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
7583 LASSERT(stidx <= enidx, return ParagraphList());
7584 ParagraphList retval;
7585 col_type col1 = tabular.cellColumn(stidx);
7586 col_type col2 = tabular.cellColumn(enidx);
7587 row_type row1 = tabular.cellRow(stidx);
7588 row_type row2 = tabular.cellRow(enidx);
7589 // stidx might be in a later column or row than enidx
7594 for (col_type col = col1; col <= col2; col++)
7595 for (row_type row = row1; row <= row2; row++)
7596 for (auto const & par : tabular.cellInset(row, col)->paragraphs())
7597 retval.push_back(par);
7602 void InsetTabular::getSelection(Cursor & cur,
7603 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7605 CursorSlice const & beg = cur.selBegin();
7606 CursorSlice const & end = cur.selEnd();
7607 cs = tabular.cellColumn(beg.idx());
7608 ce = tabular.cellColumn(end.idx());
7612 rs = tabular.cellRow(beg.idx());
7613 re = tabular.cellRow(end.idx());
7619 Text * InsetTabular::getText(int idx) const
7621 return size_t(idx) < nargs() ? cell(idx)->getText(0) : nullptr;
7625 bool InsetTabular::isChanged() const
7627 for (idx_type idx = 0; idx < nargs(); ++idx) {
7628 if (cell(idx)->isChanged())
7630 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7632 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7639 void InsetTabular::setChange(Change const & change)
7641 for (idx_type idx = 0; idx < nargs(); ++idx)
7642 cell(idx)->setChange(change);
7646 void InsetTabular::acceptChanges()
7648 for (idx_type idx = 0; idx < nargs(); ++idx)
7649 cell(idx)->acceptChanges();
7650 for (row_type row = 0; row < tabular.nrows(); ++row) {
7651 if (tabular.row_info[row].change.inserted())
7652 tabular.row_info[row].change.setUnchanged();
7653 else if (tabular.row_info[row].change.deleted())
7654 tabular.deleteRow(row, true);
7656 for (col_type col = 0; col < tabular.ncols(); ++col) {
7657 if (tabular.column_info[col].change.inserted())
7658 tabular.column_info[col].change.setUnchanged();
7659 else if (tabular.column_info[col].change.deleted())
7660 tabular.deleteColumn(col, true);
7662 tabular.updateIndexes();
7666 void InsetTabular::rejectChanges()
7668 for (idx_type idx = 0; idx < nargs(); ++idx)
7669 cell(idx)->rejectChanges();
7670 for (row_type row = 0; row < tabular.nrows(); ++row) {
7671 if (tabular.row_info[row].change.deleted())
7672 tabular.row_info[row].change.setUnchanged();
7673 else if (tabular.row_info[row].change.inserted())
7674 tabular.deleteRow(row, true);
7676 for (col_type col = 0; col < tabular.ncols(); ++col) {
7677 if (tabular.column_info[col].change.deleted())
7678 tabular.column_info[col].change.setUnchanged();
7679 else if (tabular.column_info[col].change.inserted())
7680 tabular.deleteColumn(col, true);
7682 tabular.updateIndexes();
7686 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7688 return tabular.getPWidth(cell).zero();
7692 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7695 if (buf.length() <= 0)
7700 col_type maxCols = 1;
7701 size_t const len = buf.length();
7705 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7713 maxCols = max(cols, maxCols);
7719 maxCols = max(cols, maxCols);
7725 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7726 loctab = paste_tabular.get();
7727 dirtyTabularStack(true);
7730 cell = bv.cursor().idx();
7731 ocol = tabular.cellColumn(cell);
7732 row = tabular.cellRow(cell);
7736 idx_type cells = loctab->numberofcells;
7739 rows = loctab->nrows();
7740 col_type columns = loctab->ncols();
7743 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7749 // append column if necessary
7750 if (cols == columns) {
7751 loctab->appendColumn(cols - 1);
7752 columns = loctab->ncols();
7753 cells = loctab->numberofcells;
7756 if (cols < columns) {
7757 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7758 Font const font = bv.textMetrics(&inset->text()).
7760 inset->setText(buf.substr(op, p - op), font,
7761 buffer().params().track_changes);
7767 // we can only set this if we are not too far right
7768 if (cols < columns) {
7769 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7770 Font const font = bv.textMetrics(&inset->text()).
7772 inset->setText(buf.substr(op, p - op), font,
7773 buffer().params().track_changes);
7777 // append row if necessary
7778 if (row == rows && p < len - 1) {
7779 loctab->appendRow(row - 1);
7780 rows = loctab->nrows();
7781 cells = loctab->numberofcells;
7784 cell = loctab->cellIndex(row, cols);
7790 // check for the last cell if there is no trailing '\n'
7791 if (cell < cells && op < len) {
7792 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7793 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7794 inset->setText(buf.substr(op, len - op), font,
7795 buffer().params().track_changes);
7801 void InsetTabular::addPreview(DocIterator const & inset_pos,
7802 PreviewLoader & loader) const
7804 DocIterator cell_pos = inset_pos;
7806 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7807 for (row_type r = 0; r < tabular.nrows(); ++r) {
7808 for (col_type c = 0; c < tabular.ncols(); ++c) {
7809 cell_pos.top().idx() = tabular.cellIndex(r, c);
7810 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7816 bool InsetTabular::completionSupported(Cursor const & cur) const
7818 Cursor const & bvCur = cur.bv().cursor();
7819 if (&bvCur.inset() != this)
7821 return cur.text()->completionSupported(cur);
7825 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7827 return completionSupported(cur);
7831 bool InsetTabular::automaticInlineCompletion() const
7833 return lyxrc.completion_inline_text;
7837 bool InsetTabular::automaticPopupCompletion() const
7839 return lyxrc.completion_popup_text;
7843 bool InsetTabular::showCompletionCursor() const
7845 return lyxrc.completion_cursor_text &&
7846 (lyxrc.completion_inline_text || lyxrc.completion_popup_text);
7850 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7852 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : nullptr;
7856 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7858 if (!completionSupported(cur))
7860 return cur.text()->completionPrefix(cur);
7864 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
7866 if (!completionSupported(cur))
7869 return cur.text()->insertCompletion(cur, s);
7873 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7874 Dimension & dim) const
7876 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7877 tm.completionPosAndDim(cur, x, y, dim);
7881 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7883 istringstream data(in);
7885 lex.setStream(data);
7892 if (!lex || token != "tabular") {
7893 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7897 // This is part of the inset proper that is usually swallowed
7898 // by Buffer::readInset
7900 if (!lex || token != "Tabular") {
7901 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7909 string InsetTabular::params2string(InsetTabular const & inset)
7912 data << "tabular" << ' ';
7914 data << "\\end_inset\n";
7919 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7921 for (col_type c = 0; c < tabular.ncols(); ++c) {
7922 for (row_type r = 0; r < tabular.nrows(); ++r) {
7923 if (!tabular.isPartOfMultiColumn(r,c) &&
7924 !tabular.isPartOfMultiRow(r,c))
7927 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7928 ParagraphList::iterator it = parlist.begin();
7929 ParagraphList::iterator const en = parlist.end();
7930 for (; it != en; ++it)
7931 it->setLayout(dc.plainLayout());