2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
16 * \author Scott Kostyshak
18 * Full author contact details are available in file CREDITS.
23 #include "InsetTabular.h"
26 #include "buffer_funcs.h"
28 #include "BufferParams.h"
29 #include "BufferView.h"
30 #include "CoordCache.h"
33 #include "CutAndPaste.h"
34 #include "DispatchResult.h"
35 #include "FuncRequest.h"
36 #include "FuncStatus.h"
37 #include "InsetIterator.h"
38 #include "InsetList.h"
40 #include "LaTeXFeatures.h"
44 #include "MetricsInfo.h"
46 #include "output_xhtml.h"
47 #include "Paragraph.h"
48 #include "ParIterator.h"
50 #include "texstream.h"
51 #include "TextClass.h"
52 #include "TextMetrics.h"
54 #include "frontends/Application.h"
55 #include "frontends/alert.h"
56 #include "frontends/Clipboard.h"
57 #include "frontends/Painter.h"
58 #include "frontends/Selection.h"
60 #include "support/Changer.h"
61 #include "support/convert.h"
62 #include "support/debug.h"
63 #include "support/docstream.h"
64 #include "support/FileName.h"
65 #include "support/gettext.h"
66 #include "support/lassert.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 bool const ct = force ? false : buffer().params().track_changes;
813 for (col_type c = 0; c < ncols(); ++c) {
814 // Care about multirow cells
815 if (row + 1 < nrows() &&
816 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
817 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
818 cell_info[row + 1][c] = cell_info[row][c];
822 row_info[row].change.setDeleted();
824 row_info.erase(row_info.begin() + row);
825 cell_info.erase(cell_info.begin() + row);
831 void Tabular::copyRow(row_type const row)
833 insertRow(row, true);
837 void Tabular::appendRow(row_type row)
839 insertRow(row, false);
843 void Tabular::insertRow(row_type const row, bool copy)
845 row_info.insert(row_info.begin() + row + 1, row_info[row]);
846 cell_info.insert(cell_info.begin() + row + 1,
847 cell_vector(0, CellData(buffer_)));
849 for (col_type c = 0; c < ncols(); ++c) {
850 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
851 copy ? cell_info[row][c] : CellData(buffer_));
852 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
853 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
857 for (col_type c = 0; c < ncols(); ++c) {
858 if (isPartOfMultiRow(row, c))
860 // inherit line settings
861 idx_type const i = cellIndex(row + 1, c);
862 idx_type const j = cellIndex(row, c);
863 setLeftLine(i, leftLine(j));
864 setRightLine(i, rightLine(j));
865 setTopLine(i, topLine(j));
866 if (topLine(j) && bottomLine(j)) {
867 setBottomLine(i, true);
868 setBottomLine(j, false);
871 if (buffer().params().track_changes) {
872 row_info[row + 1].change.setInserted();
878 void Tabular::moveColumn(col_type col_start, col_type col_end,
879 ColDirection direction)
881 if (direction == Tabular::LEFT) {
882 for (col_type col = col_start; col <= col_end; ++col) {
883 std::swap(column_info[col - 1], column_info[col]);
884 for (row_type r = 0; r < nrows(); ++r) {
885 std::swap(cell_info[r][col - 1], cell_info[r][col]);
886 std::swap(cell_info[r][col - 1].left_line, cell_info[r][col].left_line);
887 std::swap(cell_info[r][col - 1].right_line, cell_info[r][col].right_line);
889 if (buffer().params().track_changes) {
890 idx_type const i = cellIndex(r, col - 1);
891 idx_type const j = cellIndex(r, col);
892 cellInfo(i).inset->setChange(Change(Change::INSERTED));
893 cellInfo(j).inset->setChange(Change(Change::INSERTED));
901 for (col_type col = col_end; col >= col_start; --col) {
902 std::swap(column_info[col], column_info[col + 1]);
903 for (row_type r = 0; r < nrows(); ++r) {
904 std::swap(cell_info[r][col], cell_info[r][col + 1]);
905 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
906 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
908 if (buffer().params().track_changes) {
909 idx_type const i = cellIndex(r, col);
910 idx_type const j = cellIndex(r, col + 1);
911 cellInfo(i).inset->setChange(Change(Change::INSERTED));
912 cellInfo(j).inset->setChange(Change(Change::INSERTED));
923 void Tabular::moveRow(row_type row_start, row_type row_end, RowDirection direction)
925 if (direction == Tabular::UP) {
926 for (row_type row = row_start; row <= row_end; ++row) {
927 std::swap(row_info[row - 1], row_info[row]);
928 for (col_type c = 0; c < ncols(); ++c) {
929 std::swap(cell_info[row - 1][c], cell_info[row][c]);
930 std::swap(cell_info[row - 1][c].top_line, cell_info[row][c].top_line);
931 std::swap(cell_info[row - 1][c].bottom_line, cell_info[row][c].bottom_line);
933 idx_type const i = cellIndex(row - 1, c);
934 idx_type const j = cellIndex(row, c);
935 if (buffer().params().track_changes) {
936 cellInfo(i).inset->setChange(Change(Change::INSERTED));
937 cellInfo(j).inset->setChange(Change(Change::INSERTED));
945 for (row_type row = row_end; row >= row_start; --row) {
946 std::swap(row_info[row], row_info[row + 1]);
947 for (col_type c = 0; c < ncols(); ++c) {
948 std::swap(cell_info[row][c], cell_info[row + 1][c]);
949 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
950 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
952 idx_type const i = cellIndex(row, c);
953 idx_type const j = cellIndex(row + 1, c);
954 if (buffer().params().track_changes) {
955 cellInfo(i).inset->setChange(Change(Change::INSERTED));
956 cellInfo(j).inset->setChange(Change(Change::INSERTED));
967 void Tabular::deleteColumn(col_type const col, bool const force)
969 // Not allowed to delete last column
973 bool const ct = force ? false : buffer().params().track_changes;
975 for (row_type r = 0; r < nrows(); ++r) {
976 // Care about multicolumn cells
977 if (col + 1 < ncols() &&
978 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
979 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
980 cell_info[r][col + 1] = cell_info[r][col];
983 cell_info[r].erase(cell_info[r].begin() + col);
986 column_info[col].change.setDeleted();
988 column_info.erase(column_info.begin() + col);
993 void Tabular::copyColumn(col_type const col)
995 insertColumn(col, true);
999 void Tabular::appendColumn(col_type col)
1001 insertColumn(col, false);
1005 void Tabular::insertColumn(col_type const col, bool copy)
1007 bool const ct = buffer().params().track_changes;
1008 column_info.insert(column_info.begin() + col + 1, column_info[col]);
1010 for (row_type r = 0; r < nrows(); ++r) {
1011 cell_info[r].insert(cell_info[r].begin() + col + 1,
1012 copy ? cell_info[r][col] : CellData(buffer_));
1013 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
1014 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
1017 for (row_type r = 0; r < nrows(); ++r) {
1018 // inherit line settings
1019 idx_type const i = cellIndex(r, col + 1);
1020 idx_type const j = cellIndex(r, col);
1021 setBottomLine(i, bottomLine(j));
1022 setTopLine(i, topLine(j));
1023 setLeftLine(i, leftLine(j));
1024 setRightLine(i, rightLine(j));
1025 if (rightLine(i) && rightLine(j)) {
1026 setRightLine(j, false);
1030 column_info[col + 1].change.setInserted();
1036 void Tabular::updateIndexes()
1038 setBuffer(buffer());
1040 // reset cell number
1041 for (row_type row = 0; row < nrows(); ++row)
1042 for (col_type column = 0; column < ncols(); ++column) {
1043 if (!isPartOfMultiColumn(row, column)
1044 && !isPartOfMultiRow(row, column))
1046 if (isPartOfMultiRow(row, column))
1047 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1049 cell_info[row][column].cellno = numberofcells - 1;
1052 rowofcell.resize(numberofcells);
1053 columnofcell.resize(numberofcells);
1055 // reset column and row of cells and update their width, alignment and ct status
1056 for (row_type row = 0; row < nrows(); ++row) {
1057 for (col_type column = 0; column < ncols(); ++column) {
1058 if (isPartOfMultiColumn(row, column)) {
1059 cell_info[row][column].inset->toggleMultiCol(true);
1062 cell_info[row][column].inset->toggleMultiCol(false);
1063 // columnofcell needs to be called before setting width and alignment
1064 // multirow cells inherit the width from the column width
1065 if (!isPartOfMultiRow(row, column)) {
1066 columnofcell[i] = column;
1069 setFixedWidth(row, column);
1070 if (isPartOfMultiRow(row, column)) {
1071 cell_info[row][column].inset->toggleMultiRow(true);
1074 cell_info[row][column].inset->toggleMultiRow(false);
1075 cell_info[row][column].inset->setContentAlignment(
1076 getAlignment(cellIndex(row, column)));
1077 if (buffer().params().track_changes) {
1078 if (row_info[row].change.changed())
1079 cell_info[row][column].inset->setChange(row_info[row].change);
1080 if (column_info[column].change.changed())
1081 cell_info[row][column].inset->setChange(column_info[column].change);
1089 idx_type Tabular::numberOfCellsInRow(row_type const row) const
1091 idx_type result = 0;
1092 for (col_type c = 0; c < ncols(); ++c)
1093 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1099 bool Tabular::topLine(idx_type const cell) const
1101 return cellInfo(cell).top_line;
1105 bool Tabular::bottomLine(idx_type const cell) const
1107 return cellInfo(cell).bottom_line;
1111 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1113 if (use_booktabs && !ignore_bt)
1115 return cellInfo(cell).left_line;
1119 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1121 if (use_booktabs && !ignore_bt)
1123 return cellInfo(cell).right_line;
1127 bool Tabular::outsideBorders(
1128 row_type const sel_row_start, row_type const sel_row_end,
1129 col_type const sel_col_start, col_type const sel_col_end) const
1132 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
1133 if (!leftLine(cellIndex(r, sel_col_start))
1134 || !rightLine(cellIndex(r, sel_col_end)))
1137 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
1138 if (!topLine(cellIndex(sel_row_start, c))
1139 || !bottomLine(cellIndex(sel_row_end, c)))
1146 bool Tabular::innerBorders(
1147 row_type const sel_row_start, row_type const sel_row_end,
1148 col_type const sel_col_start, col_type const sel_col_end) const
1150 // Single cell has no inner borders
1151 if (sel_row_start == sel_row_end && sel_col_start == sel_col_end)
1153 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
1154 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
1155 idx_type const cell = cellIndex(r, c);
1156 if ((r != sel_row_start && !topLine(cell)
1157 && cell_info[r][c].multirow != CELL_PART_OF_MULTIROW)
1159 && c != sel_col_start && !leftLine(cell)
1160 && cell_info[r][c].multicolumn != CELL_PART_OF_MULTICOLUMN))
1167 void Tabular::setLines(
1168 row_type const sel_row_start, row_type const sel_row_end,
1169 col_type const sel_col_start, col_type const sel_col_end,
1170 bool setLinesInnerOnly, bool setLines)
1172 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
1173 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
1174 idx_type const cell = cellIndex(r, c);
1175 if (!(setLinesInnerOnly && r == sel_row_start)
1176 // for multirows, cell is taken care of at beginning
1177 && cell_info[r][c].multirow != CELL_PART_OF_MULTIROW)
1178 setTopLine(cell, setLines);
1179 if (!(setLinesInnerOnly && r == sel_row_end)
1180 && (r == sel_row_end || (!setLines
1181 // for multirows, cell is taken care of at the last part
1182 && cell_info[r + 1][c].multirow != CELL_PART_OF_MULTIROW)))
1183 setBottomLine(cell, setLines);
1184 if (!(setLinesInnerOnly && c == sel_col_start)
1185 // for multicolumns, cell is taken care of at beginning
1186 && cell_info[r][c].multicolumn != CELL_PART_OF_MULTICOLUMN)
1187 setLeftLine(cell, setLines);
1188 if (!(setLinesInnerOnly && c == sel_col_end)
1189 && (c == sel_col_end || (!setLines
1190 // for multicolumns, cell is taken care of at the last part
1191 && cell_info[r][c + 1].multicolumn != CELL_PART_OF_MULTICOLUMN)))
1192 setRightLine(cell, setLines);
1197 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1200 return make_pair(false, false);
1201 return make_pair(cellInfo(cell).top_line_ltrimmed,
1202 cellInfo(cell).top_line_rtrimmed);
1206 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1209 return make_pair(false, false);
1210 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1211 cellInfo(cell).bottom_line_rtrimmed);
1215 int Tabular::interRowSpace(row_type row) const
1217 if (!row || row >= nrows())
1220 int const interline_space = row_info[row - 1].interline_space_default ?
1221 default_line_space :
1222 row_info[row - 1].interline_space.inPixels(width());
1223 if (rowTopLine(row) && rowBottomLine(row - 1))
1224 return interline_space + WIDTH_OF_LINE;
1225 return interline_space;
1229 int Tabular::interColumnSpace(idx_type cell) const
1231 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1232 if (rightLine(cell) && nextcol < ncols()
1233 && leftLine(cellIndex(cellRow(cell), nextcol)))
1234 return WIDTH_OF_LINE;
1239 int Tabular::cellWidth(idx_type cell) const
1242 col_type const span = columnSpan(cell);
1243 col_type const col = cellColumn(cell);
1244 for(col_type c = col; c < col + span ; ++c)
1245 w += column_info[c].width;
1250 int Tabular::cellHeight(idx_type cell) const
1252 row_type const span = rowSpan(cell);
1253 row_type const row = cellRow(cell);
1255 for(row_type r = row; r < row + span ; ++r) {
1256 h += rowAscent(r) + rowDescent(r);
1257 if (r != row + span - 1)
1258 h += interRowSpace(r + 1);
1265 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1267 vector<int> max_dwidth(ncols(), 0);
1268 // collect max. fixed width of column
1269 map<col_type, int> max_pwidth;
1270 // collect max. variable width of column
1271 map<col_type, int> max_width;
1273 for(col_type c = 0; c < ncols(); ++c)
1274 for(row_type r = 0; r < nrows(); ++r) {
1275 idx_type const i = cellIndex(r, c);
1276 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1277 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1278 if (!getPWidth(i).zero())
1279 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1280 else if (!column_info[c].varwidth)
1281 max_width[c] = max(max_width[c], cell_info[r][c].width);
1284 // If we have a fixed tabular width, we take this into account
1285 Length tab_width = tabular_width;
1286 bool const tabularx = hasVarwidthColumn();
1287 if (tabularx && tab_width.zero())
1288 // If no tabular width is specified with X columns,
1289 // we use 100% colwidth
1290 tab_width = Length(100, Length::PCW);
1292 if (!tab_width.zero()) {
1293 restwidth = mi.base.inPixels(tab_width);
1294 // Subtract the fixed widths from the table width
1295 for (auto const & w : max_pwidth)
1296 restwidth -= w.second;
1299 // If we have a fixed width, distribute the available table width
1300 // (minus the fixed widths) to the variable-width columns
1302 int restcols = ncols() - max_pwidth.size();
1303 if ((restwidth > 0) && (restcols != 0))
1304 vcolwidth = restwidth / restcols;
1306 // Now consider that some variable width columns exceed the vcolwidth
1307 if (vcolwidth > 0) {
1308 bool changed = false;
1309 for (auto const & w : max_width) {
1310 if (tabularx || w.second > vcolwidth) {
1312 restwidth -= w.second;
1316 if (changed && restwidth > 0)
1317 vcolwidth = restwidth / restcols;
1320 bool update = false;
1321 // for each col get max of single col cells
1322 for(col_type c = 0; c < ncols(); ++c) {
1324 for(row_type r = 0; r < nrows(); ++r) {
1325 idx_type const i = cellIndex(r, c);
1326 if (columnSpan(i) == 1) {
1327 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1328 && cell_info[r][c].decimal_width != 0)
1329 new_width = max(new_width, cellInfo(i).width
1330 + max_dwidth[c] - cellInfo(i).decimal_width);
1331 else if (getPWidth(i).zero() && vcolwidth > 0) {
1332 if (tabularx && !column_info[c].varwidth)
1333 new_width = max(new_width, cellInfo(i).width);
1335 new_width = vcolwidth;
1337 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1339 new_width = max(new_width, cellInfo(i).width);
1343 if (column_info[c].width != new_width) {
1344 column_info[c].width = new_width;
1345 // Do not trigger update when no space is left for variable
1346 // columns, as this will loop
1347 update = tab_width.zero() || restwidth > 0;
1350 // update col widths to fit merged cells
1351 for(col_type c = 0; c < ncols(); ++c)
1352 for(row_type r = 0; r < nrows(); ++r) {
1353 idx_type const i = cellIndex(r, c);
1354 int const span = columnSpan(i);
1355 if (span == 1 || c > cellColumn(i))
1359 for(col_type j = c; j < c + span ; ++j)
1360 old_width += column_info[j].width;
1362 if (cellInfo(i).width > old_width) {
1363 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1364 // Do not trigger update when no space is left for variable
1365 // columns, as this will loop
1366 update = tab_width.zero() || restwidth > 0;
1374 int Tabular::width() const
1377 for (col_type c = 0; c < ncols(); ++c)
1378 width += column_info[c].width;
1383 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1386 col_type const col = cellColumn(cell);
1387 // set alignment for the whole row if we are not in a multicolumn cell,
1388 // exclude possible multicolumn cells in the row
1389 if (!isMultiColumn(cell)) {
1390 for (row_type r = 0; r < nrows(); ++r) {
1391 // only if the column has no width the multirow inherits the
1392 // alignment of the column, otherwise it is left aligned
1393 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1394 && !isMultiColumn(cellIndex(r, col))) {
1395 cell_info[r][col].alignment = align;
1396 cell_info[r][col].inset->setContentAlignment(align);
1398 if ((isMultiRow(cellIndex(r, col)) && has_width)
1399 && !isMultiColumn(cellIndex(r, col))) {
1400 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1401 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1404 column_info[col].alignment = align;
1405 docstring & dpoint = column_info[col].decimal_point;
1406 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1407 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1408 dpoint = tlang->decimalSeparator();
1411 cellInfo(cell).alignment = align;
1412 cellInset(cell)->setContentAlignment(align);
1417 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1420 if (!isMultiColumn(cell) || onlycolumn)
1421 column_info[cellColumn(cell)].valignment = align;
1423 cellInfo(cell).valignment = align;
1430 * Allow line and paragraph breaks for fixed width multirow cells
1431 * or disallow them, merge cell paragraphs and reset layout to standard
1432 * for variable width multicol cells.
1434 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1435 bool const fixedWidth, bool const multirow)
1437 inset->toggleFixedWidth(fixedWidth);
1441 BufferParams const & bp = cur.bv().buffer().params();
1444 // undo information has already been recorded
1445 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1446 bp.documentClass().plainLayoutName());
1453 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1454 Length const & width)
1456 col_type const c = cellColumn(cell);
1458 column_info[c].p_width = width;
1459 // reset the vertical alignment to top if the fixed width
1460 // is removed or zero because only fixed width columns can
1461 // have a vertical alignment
1462 if (column_info[c].p_width.zero())
1463 column_info[c].valignment = LYX_VALIGN_TOP;
1464 for (row_type r = 0; r < nrows(); ++r) {
1465 idx_type const cidx = cellIndex(r, c);
1466 // because of multicolumns
1467 toggleFixedWidth(cur, cellInset(cidx).get(),
1468 !getPWidth(cidx).zero(), isMultiRow(cidx));
1469 if (isMultiRow(cidx))
1470 setAlignment(cidx, column_info[c].alignment,
1471 !column_info[c].p_width.zero());
1473 // cur can become invalid after paragraphs were merged
1478 bool Tabular::setFixedWidth(row_type r, col_type c)
1480 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1481 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1482 || (multicol && !cell_info[r][c].p_width.zero());
1483 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1488 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1489 Length const & width)
1491 if (!isMultiColumn(cell))
1494 cellInfo(cell).p_width = width;
1495 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1497 // cur can become invalid after paragraphs were merged
1503 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1505 column_info[cellColumn(cell)].varwidth = varwidth;
1506 cellInset(cell).get()->toggleVarWidth(varwidth);
1511 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1513 cellInfo(cell).mroffset = mroffset;
1518 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1519 Tabular::Feature what)
1521 if (what == SET_SPECIAL_MULTICOLUMN)
1522 cellInfo(cell).align_special = special;
1524 column_info[cellColumn(cell)].align_special = special;
1528 void Tabular::setTopLine(idx_type i, bool line)
1530 cellInfo(i).top_line = line;
1534 void Tabular::setBottomLine(idx_type i, bool line)
1536 cellInfo(i).bottom_line = line;
1540 void Tabular::setTopLineLTrim(idx_type i, bool val)
1542 cellInfo(i).top_line_ltrimmed = val;
1546 void Tabular::setTopLineRTrim(idx_type i, bool val)
1548 cellInfo(i).top_line_rtrimmed = val;
1552 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1554 cellInfo(i).bottom_line_ltrimmed = val;
1558 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1560 cellInfo(i).bottom_line_rtrimmed = val;
1564 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1566 setTopLineLTrim(i, trim.first);
1567 setTopLineRTrim(i, trim.second);
1570 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1572 setBottomLineLTrim(i, trim.first);
1573 setBottomLineRTrim(i, trim.second);
1576 void Tabular::setLeftLine(idx_type cell, bool line)
1578 cellInfo(cell).left_line = line;
1582 void Tabular::setRightLine(idx_type cell, bool line)
1584 cellInfo(cell).right_line = line;
1587 bool Tabular::rowTopLine(row_type r) const
1589 bool all_rows_set = true;
1590 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1591 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1592 return all_rows_set;
1596 bool Tabular::rowBottomLine(row_type r) const
1598 bool all_rows_set = true;
1599 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1600 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1601 return all_rows_set;
1605 bool Tabular::columnLeftLine(col_type c) const
1612 for (row_type r = 0; r < nrows(); ++r) {
1613 idx_type const i = cellIndex(r, c);
1614 if (c == cellColumn(i)) {
1616 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1617 if (cellInfo(i).left_line || right)
1621 return 2 * nrows_left >= total;
1625 bool Tabular::columnRightLine(col_type c) const
1630 int nrows_right = 0;
1632 for (row_type r = 0; r < nrows(); ++r) {
1633 idx_type i = cellIndex(r, c);
1634 if (c == cellColumn(i) + columnSpan(i) - 1) {
1636 bool left = (c + 1 < ncols()
1637 && cellInfo(cellIndex(r, c + 1)).left_line)
1638 || c + 1 == ncols();
1639 if (cellInfo(i).right_line && left)
1643 return 2 * nrows_right >= total;
1647 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1649 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1650 return cellInfo(cell).alignment;
1652 return column_info[cellColumn(cell)].alignment;
1657 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1659 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1660 return cellInfo(cell).valignment;
1661 return column_info[cellColumn(cell)].valignment;
1665 int Tabular::offsetVAlignment() const
1667 // for top-alignment the first horizontal table line must be exactly at
1668 // the position of the base line of the surrounding text line
1669 // for bottom alignment, the same is for the last table line
1670 int offset_valign = 0;
1671 switch (tabular_valignment) {
1672 case Tabular::LYX_VALIGN_BOTTOM:
1673 offset_valign = rowAscent(0) - height();
1675 case Tabular::LYX_VALIGN_MIDDLE:
1676 offset_valign = (- height()) / 2 + rowAscent(0);
1678 case Tabular::LYX_VALIGN_TOP:
1679 offset_valign = rowAscent(0);
1682 return offset_valign;
1686 Length const Tabular::getPWidth(idx_type cell) const
1688 if (isMultiColumn(cell))
1689 return cellInfo(cell).p_width;
1690 return column_info[cellColumn(cell)].p_width;
1694 Length const Tabular::getMROffset(idx_type cell) const
1696 return cellInfo(cell).mroffset;
1700 int Tabular::textHOffset(idx_type cell) const
1702 // the LaTeX Way :-(
1703 int x = WIDTH_OF_LINE;
1705 int const w = cellWidth(cell) - cellInfo(cell).width;
1707 switch (getAlignment(cell)) {
1708 case LYX_ALIGN_CENTER:
1711 case LYX_ALIGN_RIGHT:
1714 case LYX_ALIGN_DECIMAL: {
1715 // we center when no decimal point
1716 if (cellInfo(cell).decimal_width == 0) {
1720 col_type const c = cellColumn(cell);
1721 int max_dhoffset = 0;
1722 for(row_type r = 0; r < row_info.size() ; ++r) {
1723 idx_type const i = cellIndex(r, c);
1724 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1725 && cellInfo(i).decimal_width != 0)
1726 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1728 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1731 // LYX_ALIGN_LEFT: nothing :-)
1739 int Tabular::textVOffset(idx_type cell) const
1741 int voffset = cellInfo(cell).voffset;
1742 if (isMultiRow(cell)) {
1743 row_type const row = cellRow(cell);
1744 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1750 idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1753 idx_type const numcells = numberOfCellsInRow(row);
1754 // we check against numcells to make sure we do not crash if all the
1755 // cells are multirow (bug #7535), but in that case our return value
1756 // is really invalid, i.e., it is NOT the first cell in the row. but
1757 // i do not know what to do here. (rgh)
1758 while (c < numcells - 1
1759 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1760 || (ct && column_info[c].change.deleted())))
1762 return cell_info[row][c].cellno;
1766 idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1768 col_type c = ncols() - 1;
1769 // of course we check against 0 so we don't crash. but we have the same
1770 // problem as in the previous routine: if all the cells are part of a
1771 // multirow or part of a multi column, then our return value is invalid.
1773 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1774 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1775 || (ct && column_info[c].change.deleted())))
1777 return cell_info[row][c].cellno;
1781 row_type Tabular::getFirstRow(bool const ct) const
1786 // exclude deleted rows if ct == true
1787 while (r < nrows() && row_info[r].change.deleted())
1793 row_type Tabular::getLastRow(bool const ct) const
1795 row_type r = nrows() - 1;
1798 // exclude deleted rows if ct == true
1799 while (r > 0 && row_info[r].change.deleted())
1805 row_type Tabular::cellRow(idx_type cell) const
1807 if (cell >= numberofcells)
1811 return rowofcell[cell];
1815 col_type Tabular::cellColumn(idx_type cell) const
1817 if (cell >= numberofcells)
1821 return columnofcell[cell];
1825 void Tabular::write(ostream & os) const
1829 << write_attribute("version", 3)
1830 << write_attribute("rows", nrows())
1831 << write_attribute("columns", ncols())
1833 // global longtable options
1835 << write_attribute("rotate", rotate)
1836 << write_attribute("booktabs", use_booktabs)
1837 << write_attribute("islongtable", is_long_tabular)
1838 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1839 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1840 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1841 << write_attribute("headTopDL", endhead.topDL)
1842 << write_attribute("headBottomDL", endhead.bottomDL)
1843 << write_attribute("footTopDL", endfoot.topDL)
1844 << write_attribute("footBottomDL", endfoot.bottomDL)
1845 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1846 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1847 << write_attribute("lastFootEmpty", endlastfoot.empty);
1848 // longtables cannot be aligned vertically
1849 if (!is_long_tabular)
1850 os << write_attribute("tabularvalignment", tabular_valignment);
1851 os << write_attribute("tabularwidth", tabular_width);
1852 if (is_long_tabular)
1853 os << write_attribute("longtabularalignment", longtabular_alignment);
1855 for (col_type c = 0; c < ncols(); ++c) {
1857 << write_attribute("alignment", column_info[c].alignment);
1858 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1859 os << write_attribute("decimal_point", column_info[c].decimal_point);
1860 os << write_attribute("change", column_info[c].change, buffer().params())
1861 << write_attribute("valignment", column_info[c].valignment)
1862 << write_attribute("width", column_info[c].p_width.asString())
1863 << write_attribute("varwidth", column_info[c].varwidth)
1864 << write_attribute("special", column_info[c].align_special)
1867 for (row_type r = 0; r < nrows(); ++r) {
1868 static const string def("default");
1870 if (row_info[r].top_space_default)
1871 os << write_attribute("topspace", def);
1873 os << write_attribute("topspace", row_info[r].top_space);
1874 if (row_info[r].bottom_space_default)
1875 os << write_attribute("bottomspace", def);
1877 os << write_attribute("bottomspace", row_info[r].bottom_space);
1878 if (row_info[r].interline_space_default)
1879 os << write_attribute("interlinespace", def);
1881 os << write_attribute("interlinespace", row_info[r].interline_space);
1882 os << write_attribute("change", row_info[r].change, buffer().params())
1883 << write_attribute("endhead", row_info[r].endhead)
1884 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1885 << write_attribute("endfoot", row_info[r].endfoot)
1886 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1887 << write_attribute("newpage", row_info[r].newpage)
1888 << write_attribute("caption", row_info[r].caption)
1890 for (col_type c = 0; c < ncols(); ++c) {
1892 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1893 << write_attribute("multirow", cell_info[r][c].multirow)
1894 << write_attribute("mroffset", cell_info[r][c].mroffset)
1895 << write_attribute("alignment", cell_info[r][c].alignment)
1896 << write_attribute("valignment", cell_info[r][c].valignment)
1897 << write_attribute("topline", cell_info[r][c].top_line)
1898 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1899 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1900 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1901 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1902 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1903 << write_attribute("leftline", cell_info[r][c].left_line)
1904 << write_attribute("rightline", cell_info[r][c].right_line)
1905 << write_attribute("rotate", cell_info[r][c].rotate)
1906 << write_attribute("usebox", cell_info[r][c].usebox)
1907 << write_attribute("width", cell_info[r][c].p_width)
1908 << write_attribute("special", cell_info[r][c].align_special)
1910 os << "\\begin_inset ";
1911 cell_info[r][c].inset->write(os);
1912 os << "\n\\end_inset\n"
1914 // FIXME This can be removed again once the mystery
1915 // crash has been resolved.
1920 os << "</lyxtabular>\n";
1924 void Tabular::read(Lexer & lex)
1927 istream & is = lex.getStream();
1929 l_getline(is, line);
1930 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1931 LASSERT(false, return);
1935 if (!getTokenValue(line, "version", version))
1937 LATTEST(version >= 2);
1940 if (!getTokenValue(line, "rows", rows_arg))
1943 if (!getTokenValue(line, "columns", columns_arg))
1945 init(buffer_, rows_arg, columns_arg);
1946 l_getline(is, line);
1947 if (!prefixIs(line, "<features")) {
1948 lyxerr << "Wrong tabular format (expected <features ...> got"
1949 << line << ')' << endl;
1952 getTokenValue(line, "rotate", rotate);
1953 getTokenValue(line, "booktabs", use_booktabs);
1954 getTokenValue(line, "islongtable", is_long_tabular);
1955 getTokenValue(line, "tabularvalignment", tabular_valignment);
1956 getTokenValue(line, "tabularwidth", tabular_width);
1957 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1958 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1959 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1960 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1961 getTokenValue(line, "headTopDL", endhead.topDL);
1962 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1963 getTokenValue(line, "footTopDL", endfoot.topDL);
1964 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1965 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1966 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1967 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1969 for (col_type c = 0; c < ncols(); ++c) {
1971 if (!prefixIs(line,"<column")) {
1972 lyxerr << "Wrong tabular format (expected <column ...> got"
1973 << line << ')' << endl;
1976 getTokenValue(line, "alignment", column_info[c].alignment);
1977 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1978 getTokenValue(line, "valignment", column_info[c].valignment);
1979 getTokenValue(line, "width", column_info[c].p_width);
1980 getTokenValue(line, "special", column_info[c].align_special);
1981 getTokenValue(line, "varwidth", column_info[c].varwidth);
1982 getTokenValue(line, "change", column_info[c].change, buffer().params());
1985 for (row_type i = 0; i < nrows(); ++i) {
1986 l_getline(is, line);
1987 if (!prefixIs(line, "<row")) {
1988 lyxerr << "Wrong tabular format (expected <row ...> got"
1989 << line << ')' << endl;
1992 getTokenValue(line, "topspace", row_info[i].top_space,
1993 row_info[i].top_space_default);
1994 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1995 row_info[i].bottom_space_default);
1996 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1997 row_info[i].interline_space_default);
1998 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1999 getTokenValue(line, "endhead", row_info[i].endhead);
2000 getTokenValue(line, "endfoot", row_info[i].endfoot);
2001 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
2002 getTokenValue(line, "newpage", row_info[i].newpage);
2003 getTokenValue(line, "caption", row_info[i].caption);
2004 getTokenValue(line, "change", row_info[i].change, buffer().params());
2005 for (col_type j = 0; j < ncols(); ++j) {
2006 l_getline(is, line);
2007 if (!prefixIs(line, "<cell")) {
2008 lyxerr << "Wrong tabular format (expected <cell ...> got"
2009 << line << ')' << endl;
2012 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
2013 getTokenValue(line, "multirow", cell_info[i][j].multirow);
2014 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
2015 getTokenValue(line, "alignment", cell_info[i][j].alignment);
2016 getTokenValue(line, "valignment", cell_info[i][j].valignment);
2017 getTokenValue(line, "topline", cell_info[i][j].top_line);
2018 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
2019 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
2020 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
2021 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
2022 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
2023 getTokenValue(line, "leftline", cell_info[i][j].left_line);
2024 getTokenValue(line, "rightline", cell_info[i][j].right_line);
2025 getTokenValue(line, "rotate", cell_info[i][j].rotate);
2026 getTokenValue(line, "usebox", cell_info[i][j].usebox);
2027 getTokenValue(line, "width", cell_info[i][j].p_width);
2029 getTokenValue(line, "special", cell_info[i][j].align_special);
2030 l_getline(is, line);
2031 if (prefixIs(line, "\\begin_inset")) {
2032 cell_info[i][j].inset->setBuffer(*buffer_);
2033 cell_info[i][j].inset->read(lex);
2034 l_getline(is, line);
2036 if (!prefixIs(line, "</cell>")) {
2037 lyxerr << "Wrong tabular format (expected </cell> got"
2038 << line << ')' << endl;
2042 l_getline(is, line);
2043 if (!prefixIs(line, "</row>")) {
2044 lyxerr << "Wrong tabular format (expected </row> got"
2045 << line << ')' << endl;
2049 while (!prefixIs(line, "</lyxtabular>")) {
2050 l_getline(is, line);
2056 bool Tabular::isMultiColumn(idx_type cell) const
2058 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2059 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
2063 bool Tabular::hasMultiColumn(col_type c) const
2065 for (row_type r = 0; r < nrows(); ++r) {
2066 if (isMultiColumn(cellIndex(r, c)))
2073 bool Tabular::hasVarwidthColumn() const
2075 for (col_type c = 0; c < ncols(); ++c) {
2076 if (column_info[c].varwidth)
2083 bool Tabular::isVTypeColumn(col_type c) const
2085 for (row_type r = 0; r < nrows(); ++r) {
2086 idx_type idx = cellIndex(r, c);
2087 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH
2088 && getAlignment(idx) == LYX_ALIGN_LEFT
2089 && getVAlignment(idx) == LYX_VALIGN_TOP)
2096 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
2098 return cell_info[cellRow(cell)][cellColumn(cell)];
2102 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2104 return cell_info[cellRow(cell)][cellColumn(cell)];
2108 idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2109 bool const right_border)
2111 idx_type const col = cellColumn(cell);
2112 idx_type const row = cellRow(cell);
2113 for (idx_type i = 0; i < number; ++i)
2114 unsetMultiRow(cellIndex(row, col + i));
2116 // unsetting of multirow may have invalidated cell index
2117 cell = cellIndex(row, col);
2118 CellData & cs = cellInfo(cell);
2119 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2120 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2121 cs.alignment = column_info[col].alignment;
2122 setRightLine(cell, right_border);
2123 // non-fixed width multicolumns cannot have multiple paragraphs
2124 if (getPWidth(cell).zero()) {
2125 toggleFixedWidth(cur, cellInset(cell).get(),
2126 !getPWidth(cell).zero(), isMultiRow(cell));
2127 // cur can become invalid after paragraphs were merged
2131 idx_type lastcell = cellIndex(row, col + number - 1);
2132 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2133 CellData & cs1 = cellInfo(cell + i);
2134 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2135 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2143 bool Tabular::isMultiRow(idx_type cell) const
2145 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2146 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2149 bool Tabular::hasMultiRow(row_type r) const
2151 for (col_type c = 0; c < ncols(); ++c) {
2152 if (isMultiRow(cellIndex(r, c)))
2158 idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2159 bool const bottom_border,
2160 LyXAlignment const halign)
2162 idx_type const col = cellColumn(cell);
2163 idx_type const row = cellRow(cell);
2164 for (idx_type i = 0; i < number; ++i)
2165 unsetMultiColumn(cellIndex(row + i, col));
2167 // unsetting of multirow may have invalidated cell index
2168 cell = cellIndex(row, col);
2169 CellData & cs = cellInfo(cell);
2170 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2171 cs.valignment = LYX_VALIGN_MIDDLE;
2172 // the horizontal alignment of multirow cells can only
2173 // be changed for the whole table row,
2174 // support changing this only for the multirow cell can be done via
2176 if (getPWidth(cell).zero())
2177 cs.alignment = halign;
2179 cs.alignment = LYX_ALIGN_LEFT;
2181 // Multirows cannot have multiple paragraphs
2182 if (getPWidth(cell).zero()) {
2183 toggleFixedWidth(cur, cellInset(cell).get(),
2184 !getPWidth(cell).zero(),
2186 // cur can become invalid after paragraphs were merged
2190 // set the bottom line of the last selected cell
2191 setBottomLine(cell, bottom_border);
2193 for (idx_type i = 1; i < number; ++i) {
2194 CellData & cs1 = cell_info[row + i][col];
2195 cs1.multirow = CELL_PART_OF_MULTIROW;
2196 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2204 idx_type Tabular::columnSpan(idx_type cell) const
2206 row_type const row = cellRow(cell);
2207 col_type const col = cellColumn(cell);
2209 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2216 idx_type Tabular::rowSpan(idx_type cell) const
2218 col_type const column = cellColumn(cell);
2219 col_type row = cellRow(cell) + 1;
2220 while (row < nrows() && isPartOfMultiRow(row, column))
2223 return row - cellRow(cell);
2227 void Tabular::unsetMultiColumn(idx_type cell)
2229 if (!isMultiColumn(cell))
2232 row_type const row = cellRow(cell);
2233 col_type const col = cellColumn(cell);
2234 row_type const span = columnSpan(cell);
2235 for (col_type c = 0; c < span; ++c) {
2236 // in the table dialog the lines are set in every case
2237 // when unsetting a multicolumn this leads to an additional right
2238 // line for every cell that was part of the former multicolumn cell,
2239 // except if the cell is in the last column
2240 // therefore remove this line
2241 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2242 && (col + c) < (col + span - 1))
2243 cell_info[row][col + c].right_line = false;
2244 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2250 void Tabular::unsetMultiRow(idx_type cell)
2252 if (!isMultiRow(cell))
2255 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2256 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2257 row_type const row = cellRow(cell);
2258 col_type const col = cellColumn(cell);
2259 row_type const span = rowSpan(cell);
2260 for (row_type r = 0; r < span; ++r)
2261 cell_info[row + r][col].multirow = CELL_NORMAL;
2266 void Tabular::setRotateCell(idx_type cell, int value)
2268 cellInfo(cell).rotate = value;
2272 int Tabular::getRotateCell(idx_type cell) const
2274 return cellInfo(cell).rotate;
2278 bool Tabular::needRotating() const
2280 if (rotate && !is_long_tabular)
2282 for (row_type r = 0; r < nrows(); ++r)
2283 for (col_type c = 0; c < ncols(); ++c)
2284 if (cell_info[r][c].rotate != 0)
2290 bool Tabular::isLastCell(idx_type cell) const
2292 return cell + 1 >= numberofcells;
2296 idx_type Tabular::cellAbove(idx_type cell) const
2298 if (cellRow(cell) == 0)
2301 col_type const col = cellColumn(cell);
2302 row_type r = cellRow(cell) - 1;
2303 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2306 return cell_info[r][col].cellno;
2310 idx_type Tabular::cellBelow(idx_type cell) const
2312 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2313 if (nextrow < nrows())
2314 return cell_info[nextrow][cellColumn(cell)].cellno;
2319 idx_type Tabular::cellIndex(row_type row, col_type column) const
2321 LASSERT(column != npos && column < ncols(), column = 0);
2322 LASSERT(row != npos && row < nrows(), row = 0);
2323 return cell_info[row][column].cellno;
2327 void Tabular::setUsebox(idx_type cell, BoxType type)
2329 cellInfo(cell).usebox = type;
2333 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2335 if (getRotateCell(cell) == 0
2336 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2337 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2339 if (cellInfo(cell).usebox > 1)
2340 return cellInfo(cell).usebox;
2341 return useBox(cell);
2346 // This are functions used for the longtable support
2348 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2354 row_info[row].endfirsthead = flag;
2358 row_info[row].endhead = flag;
2363 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2366 hd.set = haveLTHead();
2367 return row_info[row].endhead;
2371 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2374 hd.set = haveLTFirstHead();
2375 return row_info[row].endfirsthead;
2379 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2385 row_info[row].endlastfoot = flag;
2389 row_info[row].endfoot = flag;
2394 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2397 fd.set = haveLTFoot();
2398 return row_info[row].endfoot;
2402 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2405 fd.set = haveLTLastFoot();
2406 return row_info[row].endlastfoot;
2410 void Tabular::setLTNewPage(row_type row, bool what)
2412 row_info[row].newpage = what;
2416 bool Tabular::getLTNewPage(row_type row) const
2418 return row_info[row].newpage;
2422 bool Tabular::haveLTHead(bool withcaptions) const
2424 if (!is_long_tabular)
2426 for (row_type i = 0; i < nrows(); ++i)
2427 if (row_info[i].endhead &&
2428 (withcaptions || !row_info[i].caption))
2434 bool Tabular::haveLTFirstHead(bool withcaptions) const
2436 if (!is_long_tabular || endfirsthead.empty)
2438 for (row_type r = 0; r < nrows(); ++r)
2439 if (row_info[r].endfirsthead &&
2440 (withcaptions || !row_info[r].caption))
2446 bool Tabular::haveLTFoot(bool withcaptions) const
2448 if (!is_long_tabular)
2450 for (row_type r = 0; r < nrows(); ++r)
2451 if (row_info[r].endfoot &&
2452 (withcaptions || !row_info[r].caption))
2458 bool Tabular::haveLTLastFoot(bool withcaptions) const
2460 if (!is_long_tabular || endlastfoot.empty)
2462 for (row_type r = 0; r < nrows(); ++r)
2463 if (row_info[r].endlastfoot &&
2464 (withcaptions || !row_info[r].caption))
2470 idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2472 idx_type i = getFirstCellInRow(row);
2474 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2475 setTopLine(i, false);
2476 setBottomLine(i, false);
2477 setLeftLine(i, false);
2478 setRightLine(i, false);
2479 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2480 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2481 setLTHead(row, true, endfirsthead, true);
2482 row_info[row].endfirsthead = true;
2485 unsetMultiColumn(i);
2486 // When unsetting a caption row, also all existing
2487 // captions in this row must be dissolved.
2489 row_info[row].caption = what;
2494 bool Tabular::ltCaption(row_type row) const
2496 return row_info[row].caption;
2500 bool Tabular::haveLTCaption(CaptionType captiontype) const
2502 if (!is_long_tabular)
2504 for (row_type r = 0; r < nrows(); ++r) {
2505 if (row_info[r].caption) {
2506 switch (captiontype) {
2507 case CAPTION_FIRSTHEAD:
2508 if (row_info[r].endfirsthead)
2512 if (row_info[r].endhead)
2516 if (row_info[r].endfoot)
2519 case CAPTION_LASTFOOT:
2520 if (row_info[r].endlastfoot)
2532 // end longtable support functions
2534 void Tabular::setRowAscent(row_type row, int height)
2536 if (row >= nrows() || row_info[row].ascent == height)
2538 row_info[row].ascent = height;
2542 void Tabular::setRowDescent(row_type row, int height)
2544 if (row >= nrows() || row_info[row].descent == height)
2546 row_info[row].descent = height;
2550 int Tabular::rowAscent(row_type row) const
2552 LASSERT(row < nrows(), row = 0);
2553 return row_info[row].ascent;
2557 int Tabular::rowDescent(row_type row) const
2559 LASSERT(row < nrows(), row = 0);
2560 return row_info[row].descent;
2564 int Tabular::height() const
2567 for (row_type row = 0; row < nrows(); ++row)
2568 height += rowAscent(row) + rowDescent(row) +
2574 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2576 LASSERT(row < nrows(), return false);
2577 LASSERT(column < ncols(), return false);
2578 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2582 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2584 LASSERT(row < nrows(), return false);
2585 LASSERT(column < ncols(), return false);
2586 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2590 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns,
2591 list<col_type> logical_columns) const
2593 // we only output complete row lines and the 1st row here, the rest
2594 // is done in Tabular::TeXBottomHLine(...)
2596 // get for each column the topline (if any)
2597 map<col_type, bool> topline, topltrims, toprtrims;
2599 bool have_trims = false;
2600 for (auto const & c : columns) {
2601 topline[c] = topLine(cellIndex(row, c));
2602 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2603 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2604 // If cell is part of a multirow and not the first cell of the
2605 // multirow, no line must be drawn.
2607 if (isMultiRow(cellIndex(row, c))
2608 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2610 topltrims[c] = false;
2611 toprtrims[c] = false;
2613 // copy trimming to multicolumn parts
2614 if (isPartOfMultiColumn(row, c)) {
2615 topltrims[c] = topltrims[c-1];
2616 toprtrims[c] = toprtrims[c-1];
2618 if (topline.find(c) != topline.end() && topline.find(c)->second)
2620 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2621 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2625 // do nothing if empty first row, or incomplete row line after
2626 row_type first = getFirstRow(!buffer().params().output_changes);
2627 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2630 // Is this the actual first row (excluding longtable caption row)?
2631 bool const realfirstrow = (row == first
2632 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2634 // only output complete row lines and the 1st row's clines
2635 if (nset == columns.size() && !have_trims) {
2637 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2641 } else if (realfirstrow || have_trims) {
2642 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2644 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2645 std::list<col_type>::const_iterator it2 = columns.begin();
2646 // We need to iterate over the logical columns here, but take care for
2648 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2653 if (topline.find(c)->second) {
2654 col_type offset = 0;
2655 for (col_type j = 0 ; j < c; ++j)
2656 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2658 // If the two iterators differ, we are in bidi with swapped columns
2659 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2660 while (isPartOfMultiColumn(row, c))
2663 if (topltrims.find(c) != topltrims.end()
2664 && topltrims.find(c)->second)
2666 col_type cstart = c;
2667 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2668 if (isMultiColumn(cellIndex(row, c))
2669 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2671 if (c > cstart && topltrims.find(c) != topltrims.end()
2672 && topltrims.find(c)->second) {
2673 if (!isPartOfMultiColumn(row, c))
2676 } else if (toprtrims.find(c) != toprtrims.end()
2677 && toprtrims.find(c)->second)
2681 for (col_type j = cstart ; j <= c ; ++j)
2682 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2684 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2685 if (toprtrims.find(c) != toprtrims.end()
2686 && toprtrims.find(c)->second)
2691 os << "(" << trim << ")";
2692 if (firstcol > lastcol)
2693 // This can happen with bidi (swapped columns)
2694 os << "{" << lastcol << '-' << firstcol << "}";
2696 os << "{" << firstcol << '-' << lastcol << "}";
2697 if (c == columns.size() - 1)
2707 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns,
2708 list<col_type> logical_columns) const
2710 // we output bottomlines of row r and the toplines of row r+1
2711 // if the latter do not span the whole tabular
2713 // get the bottomlines of row r, and toplines in next row
2714 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2715 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2716 bool nextrowset = true;
2717 for (auto const & c : columns) {
2718 idx_type const idx = cellIndex(row, c);
2719 bottomline[c] = bottomLine(cellIndex(row, c));
2720 bottomltrims[c] = bottomLineTrim(idx).first;
2721 bottomrtrims[c] = bottomLineTrim(idx).second;
2722 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2723 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2724 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2725 // If cell is part of a multirow and not the last cell of the
2726 // multirow, no line must be drawn.
2728 if (isMultiRow(cellIndex(row, c))
2729 && isMultiRow(cellIndex(row + 1, c))
2730 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2731 bottomline[c] = false;
2733 bottomltrims[c] = false;
2734 bottomrtrims[c] = false;
2735 topltrims[c] = false;
2736 toprtrims[c] = false;
2738 // copy trimming in multicolumn parts
2739 if (isPartOfMultiColumn(row, c)) {
2740 topltrims[c] = topltrims[c-1];
2741 toprtrims[c] = toprtrims[c-1];
2742 bottomltrims[c] = bottomltrims[c-1];
2743 bottomrtrims[c] = bottomrtrims[c-1];
2746 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2749 // combine this row's bottom lines and next row's toplines if necessary
2751 bool have_trims = false;
2752 for (auto const & c : columns) {
2754 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2755 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2756 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2757 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2758 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2759 if (bottomline.find(c)->second)
2761 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2762 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2766 // do nothing if empty, OR incomplete row line with a topline in next row
2767 if (nset == 0 || (nextrowset && nset != columns.size()))
2770 if (nset == columns.size() && !have_trims) {
2772 os << (lastrow ? "\\bottomrule" : "\\midrule");
2776 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2778 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2779 std::list<col_type>::const_iterator it2 = columns.begin();
2780 // We need to iterate over the logical columns here, but take care for
2782 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2787 if (bottomline.find(c)->second) {
2788 col_type offset = 0;
2789 for (col_type j = 0 ; j < c; ++j)
2790 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2792 // If the two iterators differ, we are in bidi with swapped columns
2793 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2794 while (isPartOfMultiColumn(row, c))
2797 if (bottomltrims.find(c) != bottomltrims.end()
2798 && bottomltrims.find(c)->second)
2800 col_type cstart = c;
2801 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2802 if (isMultiColumn(cellIndex(row, c))
2804 && isPartOfMultiColumn(row, c + 1))
2807 && bottomltrims.find(c) != bottomltrims.end()
2808 && bottomltrims.find(c)->second) {
2809 if (!isPartOfMultiColumn(row, c))
2812 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2813 && bottomrtrims.find(c)->second)
2817 for (col_type j = cstart ; j <= c ; ++j)
2818 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2820 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2821 if (bottomrtrims.find(c) != bottomrtrims.end()
2822 && bottomrtrims.find(c)->second)
2827 os << "(" << trim << ")";
2828 if (firstcol > lastcol)
2829 // This can happen with bidi (swapped columns)
2830 os << "{" << lastcol << '-' << firstcol << "}";
2832 os << "{" << firstcol << '-' << lastcol << "}";
2833 if (c == columns.size() - 1)
2843 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2844 bool & ismulticol, bool & ismultirow,
2845 bool const bidi) const
2847 row_type const r = cellRow(cell);
2848 if (is_long_tabular && row_info[r].caption)
2851 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2852 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2853 // figure out how to set the lines
2854 // we always set double lines to the right of the cell
2855 // or left in bidi RTL, respectively.
2856 col_type const c = cellColumn(cell);
2857 col_type const nextcol = c + columnSpan(cell);
2858 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2859 bool colright = columnRightLine(c);
2860 bool colleft = columnLeftLine(c);
2861 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2862 bool nextcellleft = nextcol < ncols()
2863 && leftLine(cellIndex(r, nextcol));
2864 bool coldouble = colright && nextcolleft;
2865 bool celldouble = rightLine(cell) && nextcellleft;
2867 ismulticol = (isMultiColumn(cell)
2868 || (c == 0 && colleft != leftLine(cell))
2869 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2870 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2871 || (coldouble != celldouble))
2874 // we center in multicol when no decimal point
2876 docstring const align_d = column_info[c].decimal_point;
2877 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2878 bool const nosep = !dit;
2879 ismulticol |= nosep;
2880 celldouble &= nosep;
2883 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2884 int latexcolspan = columnSpan(cell);
2885 for (col_type col = c; col < c + columnSpan(cell); ++col)
2886 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2890 os << "\\multicolumn{" << latexcolspan << "}{";
2891 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2892 || (!bidi && c == 0 && leftLine(cell))))
2894 if (bidi && celldouble)
2895 // add extra vertical line if we want a double one
2897 if (!cellInfo(cell).align_special.empty()) {
2898 os << cellInfo(cell).align_special;
2900 if (!getPWidth(cell).zero()) {
2902 case LYX_ALIGN_LEFT:
2903 os << ">{\\raggedright}";
2905 case LYX_ALIGN_RIGHT:
2906 os << ">{\\raggedleft}";
2908 case LYX_ALIGN_CENTER:
2909 os << ">{\\centering}";
2915 case LYX_VALIGN_TOP:
2918 case LYX_VALIGN_MIDDLE:
2921 case LYX_VALIGN_BOTTOM:
2926 << from_ascii(getPWidth(cell).asLatexString())
2929 if ((getRotateCell(cell) == 0 && useBox(cell) == BOX_VARWIDTH
2930 && align == LYX_ALIGN_LEFT))
2931 os << "V{\\linewidth}";
2934 case LYX_ALIGN_LEFT:
2937 case LYX_ALIGN_RIGHT:
2945 } // end if else !getPWidth
2946 } // end if else !cellinfo_of_cell
2947 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2949 if (!bidi && celldouble)
2950 // add extra vertical line if we want a double one
2953 } // end if ismulticol
2955 // we only need code for the first multirow cell
2956 ismultirow = isMultiRow(cell);
2958 os << "\\multirow{" << rowSpan(cell) << "}{";
2959 if (!getPWidth(cell).zero())
2960 os << from_ascii(getPWidth(cell).asLatexString());
2962 if (column_info[c].varwidth)
2963 // this inherits varwidth size
2966 // we need to set a default value
2970 if (!getMROffset(cell).zero())
2971 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2973 } // end if ismultirow
2975 if (getRotateCell(cell) != 0)
2976 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2978 if (getUsebox(cell) == BOX_PARBOX) {
2981 case LYX_VALIGN_TOP:
2984 case LYX_VALIGN_MIDDLE:
2987 case LYX_VALIGN_BOTTOM:
2991 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2993 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2994 os << "\\begin{minipage}[";
2996 case LYX_VALIGN_TOP:
2999 case LYX_VALIGN_MIDDLE:
3002 case LYX_VALIGN_BOTTOM:
3006 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
3008 } else if (getUsebox(cell) == BOX_VARWIDTH
3009 && (getRotateCell(cell) != 0 || align != LYX_ALIGN_LEFT
3010 || valign != LYX_VALIGN_TOP)) {
3011 os << "\\begin{cellvarwidth}[";
3013 case LYX_VALIGN_TOP:
3016 case LYX_VALIGN_MIDDLE:
3019 case LYX_VALIGN_BOTTOM:
3025 case LYX_ALIGN_RIGHT:
3026 os << "\\raggedleft\n";
3028 case LYX_ALIGN_CENTER:
3029 os << "\\centering\n";
3031 case LYX_ALIGN_LEFT:
3032 //os << "\\narrowragged\n";
3034 case LYX_ALIGN_BLOCK:
3035 case LYX_ALIGN_DECIMAL:
3036 case LYX_ALIGN_NONE:
3037 case LYX_ALIGN_LAYOUT:
3038 case LYX_ALIGN_SPECIAL:
3045 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
3046 bool ismulticol, bool ismultirow) const
3048 row_type const r = cellRow(cell);
3049 if (is_long_tabular && row_info[r].caption)
3053 if (getUsebox(cell) == BOX_PARBOX)
3055 else if (getUsebox(cell) == BOX_MINIPAGE)
3056 os << breakln << "\\end{minipage}";
3057 else if (getUsebox(cell) == BOX_VARWIDTH
3058 && (getRotateCell(cell) != 0 || getAlignment(cell) != LYX_ALIGN_LEFT
3059 || getVAlignment(cell) != LYX_VALIGN_TOP))
3060 os << breakln << "\\end{cellvarwidth}";
3061 if (getRotateCell(cell) != 0)
3062 os << breakln << "\\end{turn}";
3070 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
3071 OutputParams const & runparams,
3072 list<col_type> columns,
3073 list<col_type> logical_columns) const
3075 if (!is_long_tabular)
3079 // output caption which is in no header or footer
3080 if (haveLTCaption()) {
3081 for (row_type r = 0; r < nrows(); ++r) {
3082 if (row_info[r].caption &&
3083 !row_info[r].endfirsthead && !row_info[r].endhead &&
3084 !row_info[r].endfoot && !row_info[r].endlastfoot)
3085 TeXRow(os, r, runparams, columns, logical_columns);
3088 // output first header info
3089 if (haveLTFirstHead()) {
3090 if (endfirsthead.topDL)
3092 for (row_type r = 0; r < nrows(); ++r) {
3093 if (row_info[r].endfirsthead)
3094 TeXRow(os, r, runparams, columns, logical_columns);
3096 if (endfirsthead.bottomDL)
3098 os << "\\endfirsthead\n";
3100 // output header info
3102 if (endfirsthead.empty && !haveLTFirstHead())
3103 os << "\\endfirsthead\n";
3106 for (row_type r = 0; r < nrows(); ++r) {
3107 if (row_info[r].endhead)
3108 TeXRow(os, r, runparams, columns, logical_columns);
3110 if (endhead.bottomDL)
3112 os << "\\endhead\n";
3114 // output footer info
3118 for (row_type r = 0; r < nrows(); ++r) {
3119 if (row_info[r].endfoot)
3120 TeXRow(os, r, runparams, columns, logical_columns);
3122 if (endfoot.bottomDL)
3124 os << "\\endfoot\n";
3125 if (endlastfoot.empty && !haveLTLastFoot())
3126 os << "\\endlastfoot\n";
3128 // output lastfooter info
3129 if (haveLTLastFoot()) {
3130 if (endlastfoot.topDL)
3132 for (row_type r = 0; r < nrows(); ++r) {
3133 if (row_info[r].endlastfoot)
3134 TeXRow(os, r, runparams, columns, logical_columns);
3136 if (endlastfoot.bottomDL)
3138 os << "\\endlastfoot\n";
3143 bool Tabular::isValidRow(row_type row) const
3145 if (!is_long_tabular)
3147 return !row_info[row].endhead && !row_info[row].endfirsthead
3148 && !row_info[row].endfoot && !row_info[row].endlastfoot
3149 && !row_info[row].caption;
3153 void Tabular::TeXRow(otexstream & os, row_type row,
3154 OutputParams const & runparams,
3155 list<col_type> columns, list<col_type> logical_columns) const
3157 //output the top line
3158 TeXTopHLine(os, row, columns, logical_columns);
3160 if (row_info[row].top_space_default) {
3162 os << "\\addlinespace\n";
3164 os << "\\noalign{\\vskip\\doublerulesep}\n";
3165 } else if(!row_info[row].top_space.zero()) {
3167 os << "\\addlinespace["
3168 << from_ascii(row_info[row].top_space.asLatexString())
3171 os << "\\noalign{\\vskip"
3172 << from_ascii(row_info[row].top_space.asLatexString())
3176 bool ismulticol = false;
3177 bool ismultirow = false;
3179 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3180 // Luabibdi (used by LuaTeX) behaves like classic
3181 bool const bidi_rtl =
3182 runparams.local_font->isRightToLeft()
3183 && runparams.useBidiPackage();
3184 bool const ct = !buffer().params().output_changes;
3186 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3188 for (auto const & c : columns) {
3189 if (isPartOfMultiColumn(row, c))
3192 idx_type cell = cellIndex(row, c);
3194 if (isPartOfMultiRow(row, c)
3195 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3196 if (cell != lastcell)
3201 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3202 InsetTableCell const * inset = cellInset(cell);
3204 Paragraph const & par = inset->paragraphs().front();
3206 os.texrow().forceStart(par.id(), 0);
3208 bool rtl = par.isRTL(buffer().params())
3210 && getPWidth(cell).zero()
3211 && !runparams.use_polyglossia;
3215 par.getParLanguage(buffer().params())->lang();
3216 if (lang == "farsi")
3218 else if (lang == "arabic_arabi")
3220 // currently, remaining RTL languages are
3221 // arabic_arabtex and hebrew
3225 // pass to the OutputParams that we are in a cell and
3226 // which alignment we have set.
3227 // InsetNewline needs this context information.
3228 OutputParams newrp(runparams);
3229 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3230 ? OutputParams::PLAIN
3231 : OutputParams::ALIGNED;
3233 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3234 // copy cell and split in 2
3235 InsetTableCell head = InsetTableCell(*cellInset(cell));
3236 head.setBuffer(const_cast<Buffer &>(buffer()));
3237 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3239 dit.push_back(CursorSlice(head));
3240 head.setMacrocontextPositionRecursive(dit);
3241 bool hassep = false;
3242 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3244 tail.setBuffer(head.buffer());
3246 dit.push_back(CursorSlice(tail));
3247 tail.setMacrocontextPositionRecursive(dit);
3251 tail.latex(os, newrp);
3254 head.latex(os, newrp);
3256 head.latex(os, newrp);
3259 tail.latex(os, newrp);
3262 } else if (ltCaption(row)) {
3263 // Inside longtable caption rows, we must only output the caption inset
3264 // with its content and omit anything outside of that (see #10791)
3265 InsetTableCell & tc_inset = *const_cast<InsetTableCell *>(inset);
3266 for (Inset const & it : tc_inset) {
3267 if (it.lyxCode() != CAPTION_CODE)
3269 it.latex(os, runparams);
3272 } else if (!isPartOfMultiRow(row, c)) {
3273 if (!runparams.nice)
3274 os.texrow().start(par.id(), 0);
3275 if (isMultiRow(cell) && !LaTeXFeatures::isAvailable("multirow-2021/01/29"))
3276 newrp.isNonLong = true;
3277 inset->latex(os, newrp);
3280 runparams.encoding = newrp.encoding;
3284 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3285 if (cell != lastcell) { // not last cell in row
3292 os << "\\tabularnewline";
3293 if (row_info[row].bottom_space_default) {
3295 os << "\\addlinespace";
3297 os << "[\\doublerulesep]";
3298 } else if (!row_info[row].bottom_space.zero()) {
3300 os << "\\addlinespace";
3302 << from_ascii(row_info[row].bottom_space.asLatexString())
3307 //output the bottom line
3308 TeXBottomHLine(os, row, columns, logical_columns);
3310 if (row_info[row].interline_space_default) {
3312 os << "\\addlinespace\n";
3314 os << "\\noalign{\\vskip\\doublerulesep}\n";
3315 } else if (!row_info[row].interline_space.zero()) {
3317 os << "\\addlinespace["
3318 << from_ascii(row_info[row].interline_space.asLatexString())
3321 os << "\\noalign{\\vskip"
3322 << from_ascii(row_info[row].interline_space.asLatexString())
3328 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3330 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3331 && !hasVarwidthColumn();
3332 bool const is_xltabular = is_long_tabular
3333 && (hasVarwidthColumn() || !tabular_width.zero());
3334 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3336 //+---------------------------------------------------------------------
3337 //+ first the opening preamble +
3338 //+---------------------------------------------------------------------
3341 if (!TexRow::isNone(pos))
3342 os.texrow().start(pos);
3345 if (is_long_tabular)
3346 os << "\\begin{landscape}\n";
3348 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3351 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3352 // order for RTL (#9686). Thus we use this list.
3353 bool const bidi_rtl =
3354 runparams.local_font->isRightToLeft()
3355 && runparams.useBidiPackage();
3356 list<col_type> columns;
3357 list<col_type> logical_columns;
3358 for (col_type cl = 0; cl < ncols(); ++cl) {
3359 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3362 columns.push_front(cl);
3364 columns.push_back(cl);
3365 // for some calculations, we need the logical (non-swapped)
3366 // columns also in bidi.
3367 logical_columns.push_back(cl);
3370 // If we use \cline or \cmidrule, we need to locally de-activate
3371 // the - character when using languages that activate it (e.g., Czech, Slovak).
3372 bool deactivate_chars = false;
3373 if ((runparams.use_babel || runparams.use_polyglossia)
3374 && contains(runparams.active_chars, '-')) {
3375 bool have_clines = false;
3376 // Check if we use \cline or \cmidrule
3377 for (row_type row = 0; row < nrows(); ++row) {
3378 col_type bset = 0, tset = 0;
3379 for (auto const & c : columns) {
3380 idx_type const idx = cellIndex(row, c);
3381 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3382 || topLineTrim(idx).first || topLineTrim(idx).second) {
3386 if (bottomLine(cellIndex(row, c)))
3388 if (topLine(cellIndex(row, c)))
3391 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3397 deactivate_chars = true;
3398 os << "\\begingroup\n"
3399 << "\\catcode`\\-=12\n";
3403 if (is_long_tabular) {
3405 os << "\\begin{xltabular}";
3407 os << "\\begin{longtable}";
3408 switch (longtabular_alignment) {
3409 case LYX_LONGTABULAR_ALIGN_LEFT:
3412 case LYX_LONGTABULAR_ALIGN_CENTER:
3415 case LYX_LONGTABULAR_ALIGN_RIGHT:
3420 if (tabular_width.zero())
3421 os << "{" << from_ascii("\\columnwidth") << "}";
3423 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3426 if (is_tabular_star)
3427 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3428 else if (hasVarwidthColumn()) {
3429 os << "\\begin{tabularx}{";
3430 if (tabular_width.zero())
3431 os << from_ascii("\\columnwidth") << "}";
3433 os << from_ascii(tabular_width.asLatexString()) << "}";
3435 os << "\\begin{tabular}";
3436 switch (tabular_valignment) {
3437 case LYX_VALIGN_TOP:
3440 case LYX_VALIGN_MIDDLE:
3442 case LYX_VALIGN_BOTTOM:
3450 if (is_tabular_star)
3451 os << "@{\\extracolsep{\\fill}}";
3453 for (auto const & c : columns) {
3454 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3456 if (!column_info[c].align_special.empty()) {
3457 os << column_info[c].align_special;
3459 if (!column_info[c].p_width.zero()) {
3460 bool decimal = false;
3461 switch (column_info[c].alignment) {
3462 case LYX_ALIGN_LEFT:
3463 os << ">{\\raggedright}";
3465 case LYX_ALIGN_RIGHT:
3466 os << ">{\\raggedleft}";
3468 case LYX_ALIGN_CENTER:
3469 os << ">{\\centering}";
3471 case LYX_ALIGN_NONE:
3472 case LYX_ALIGN_BLOCK:
3473 case LYX_ALIGN_LAYOUT:
3474 case LYX_ALIGN_SPECIAL:
3476 case LYX_ALIGN_DECIMAL: {
3478 os << ">{\\raggedright}";
3480 os << ">{\\raggedleft}";
3487 switch (column_info[c].valignment) {
3488 case LYX_VALIGN_TOP:
3489 // this is the default
3491 case LYX_VALIGN_MIDDLE:
3494 case LYX_VALIGN_BOTTOM:
3500 // Fixed-width cells with alignment at decimal separator
3501 // are output as two cells of half the width with the decimal
3502 // separator as column sep. This effectively puts the content
3503 // centered, which differs from the normal decimal sep alignment
3504 // and is not ideal, but we cannot do better ATM (see #9568).
3505 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3507 docstring const halffixedwith =
3508 from_ascii(Length(column_info[c].p_width.value() / 2,
3509 column_info[c].p_width.unit()).asLatexString());
3513 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3520 << from_ascii(column_info[c].p_width.asLatexString())
3522 } else if (column_info[c].varwidth) {
3523 switch (column_info[c].alignment) {
3524 case LYX_ALIGN_LEFT:
3525 os << ">{\\raggedright\\arraybackslash}";
3527 case LYX_ALIGN_RIGHT:
3528 os << ">{\\raggedleft\\arraybackslash}";
3530 case LYX_ALIGN_CENTER:
3531 os << ">{\\centering\\arraybackslash}";
3533 case LYX_ALIGN_NONE:
3534 case LYX_ALIGN_BLOCK:
3535 case LYX_ALIGN_LAYOUT:
3536 case LYX_ALIGN_SPECIAL:
3537 case LYX_ALIGN_DECIMAL:
3541 } else if (isVTypeColumn(c))
3542 os << "V{\\linewidth}";
3544 switch (column_info[c].alignment) {
3545 case LYX_ALIGN_LEFT:
3548 case LYX_ALIGN_RIGHT:
3551 case LYX_ALIGN_DECIMAL:
3552 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3558 } // end if else !column_info[i].p_width
3559 } // end if else !column_info[i].align_special
3560 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3565 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3567 //+---------------------------------------------------------------------
3568 //+ the single row and columns (cells) +
3569 //+---------------------------------------------------------------------
3571 for (row_type r = 0; r < nrows(); ++r) {
3572 if (!buffer().params().output_changes && row_info[r].change.deleted())
3574 if (isValidRow(r)) {
3575 TeXRow(os, r, runparams, columns, logical_columns);
3576 if (is_long_tabular && row_info[r].newpage)
3577 os << "\\newpage\n";
3581 //+---------------------------------------------------------------------
3582 //+ the closing of the tabular +
3583 //+---------------------------------------------------------------------
3585 if (is_long_tabular) {
3587 os << "\\end{xltabular}";
3589 os << "\\end{longtable}";
3591 if (is_tabular_star)
3592 os << "\\end{tabular*}";
3593 else if (hasVarwidthColumn())
3594 os << "\\end{tabularx}";
3596 os << "\\end{tabular}";
3599 if (deactivate_chars)
3601 os << "\n\\endgroup\n";
3604 if (is_long_tabular)
3605 os << breakln << "\\end{landscape}";
3607 os << breakln << "\\end{turn}";
3610 if (!TexRow::isNone(pos))
3611 os.texrow().start(pos);
3615 std::string Tabular::getVAlignAsXmlAttribute(idx_type cell) const
3617 switch (getVAlignment(cell)) {
3618 case LYX_VALIGN_TOP:
3619 return "valign='top'";
3620 case LYX_VALIGN_BOTTOM:
3621 return "valign='bottom'";
3622 case LYX_VALIGN_MIDDLE:
3623 return "valign='middle'";
3625 // This case only silences a compiler warning, as all the cases are covered above.
3631 std::string Tabular::getHAlignAsXmlAttribute(idx_type cell, bool is_xhtml) const
3633 // TODO: the Boolean flag isn't really clean; switch to an enum at some point.
3634 switch (getAlignment(cell)) {
3635 case LYX_ALIGN_LEFT:
3636 return "align='left'";
3637 case LYX_ALIGN_RIGHT:
3638 return "align='right'";
3641 // HTML only supports left, right, and center.
3643 return "align='center'";
3645 // DocBook also has justify and decimal.
3646 if (getAlignment(cell) == LYX_ALIGN_BLOCK) {
3647 return "align='justify'";
3648 } else if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3649 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3650 return "align='char' char='" + to_utf8(tlang->decimalSeparator()) + "'";
3652 return "align='center'";
3658 docstring Tabular::xmlRow(XMLStream & xs, row_type row, OutputParams const & runparams,
3659 bool header, bool is_xhtml, BufferParams::TableOutput docbook_table_output) const
3663 std::string const row_tag = (is_xhtml || docbook_table_output == BufferParams::TableOutput::HTMLTable) ? "tr" : "row";
3664 std::string const cell_tag = (is_xhtml || docbook_table_output == BufferParams::TableOutput::HTMLTable) ? (header ? "th" : "td") : "entry";
3665 idx_type cell = getFirstCellInRow(row);
3667 xs << xml::StartTag(row_tag);
3669 for (col_type c = 0; c < ncols(); ++c) {
3670 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3675 if (is_xhtml || docbook_table_output == BufferParams::TableOutput::HTMLTable) {
3676 Length const cwidth = column_info[c].p_width;
3677 if (!cwidth.zero()) {
3678 string const hwidth = cwidth.asHTMLString();
3679 attr << "style='width: " << hwidth << ";' ";
3683 attr << getHAlignAsXmlAttribute(cell, false) << " " << getVAlignAsXmlAttribute(cell);
3685 if (isMultiColumn(cell))
3686 attr << " colspan='" << columnSpan(cell) << "'";
3687 else if (isMultiRow(cell))
3688 attr << " rowspan='" << rowSpan(cell) << "'";
3689 else if (!is_xhtml && docbook_table_output == BufferParams::TableOutput::CALSTable)
3690 attr << " colname='c" << (c + 1) << "'"; // CALS column numbering starts at 1.
3692 // All cases where there should be a line *below* this row.
3693 if (!is_xhtml && docbook_table_output == BufferParams::TableOutput::CALSTable && row_info[row].bottom_space_default)
3694 attr << " rowsep='1'";
3696 // Render the cell as either XHTML or DocBook.
3697 xs << xml::StartTag(cell_tag, attr.str(), true);
3699 ret += cellInset(cell)->xhtml(xs, runparams);
3701 // DocBook: no return value for this function.
3702 OutputParams rp = runparams;
3703 rp.docbook_in_par = false;
3704 rp.docbook_force_pars = true;
3705 cellInset(cell)->docbook(xs, rp);
3707 xs << xml::EndTag(cell_tag);
3711 xs << xml::EndTag(row_tag);
3718 void Tabular::docbook(XMLStream & xs, OutputParams const & runparams) const
3722 // Some tables are inline. Likely limitation: cannot output a table within a table; is that really a limitation?
3723 if (!runparams.docbook_in_table) { // Check on the *outer* set of parameters, so that the table can be closed
3724 // properly at the end of this function.
3725 xs << xml::StartTag("informaltable");
3729 // "Formal" tables have a title and use the tag <table>; the distinction with <informaltable> is done outside.
3730 // HTML has the caption first with titles forbidden, and CALS has a title first.
3731 if (haveLTCaption()) {
3732 std::string tag = ((buffer().params().docbook_table_output) == BufferParams::HTMLTable) ? "caption" : "title";
3734 xs << xml::StartTag(tag);
3735 for (row_type r = 0; r < nrows(); ++r)
3736 if (row_info[r].caption)
3737 xmlRow(xs, r, runparams, false, false, buffer().params().docbook_table_output);
3738 xs << xml::EndTag(tag);
3742 // CALS header: describe all columns in this table. For names, take 'c' then the ID of the column.
3743 // Start at one, as is customary with CALS!
3744 if (buffer().params().docbook_table_output == BufferParams::CALSTable) {
3745 for (col_type c = 0; c < ncols(); ++c) {
3746 std::stringstream attr;
3747 attr << "colnum='" << (c + 1) << "' ";
3748 attr << "colname='c" << (c + 1) << "' ";
3749 Length const cwidth = column_info[c].p_width;
3751 attr << "colwidth='" << cwidth.asHTMLString() << "' ";
3752 attr << "rowheader='norowheader'"; // Last attribute, hence no space at the end.
3754 xs << xml::CompTag("colspec", attr.str());
3759 // Output the header of the table. For both HTML and CALS, this is surrounded by a thead.
3760 bool const havefirsthead = haveLTFirstHead(false);
3761 // if we have a first head, then we are going to ignore the
3762 // headers for the additional pages, since there aren't any
3763 // in DocBook. this test accomplishes that.
3764 bool const havehead = !havefirsthead && haveLTHead(false);
3765 if (havehead || havefirsthead) {
3766 xs << xml::StartTag("thead") << xml::CR();
3767 for (row_type r = 0; r < nrows(); ++r) {
3768 if (((havefirsthead && row_info[r].endfirsthead) ||
3769 (havehead && row_info[r].endhead)) &&
3770 !row_info[r].caption) {
3771 xmlRow(xs, r, runparams, true, false, buffer().params().docbook_table_output);
3774 xs << xml::EndTag("thead");
3778 // Output the footer of the table. For both HTML and CALS, this is surrounded by a tfoot and output just after
3779 // the header (and before the body).
3780 bool const havelastfoot = haveLTLastFoot(false);
3782 bool const havefoot = !havelastfoot && haveLTFoot(false);
3783 if (havefoot || havelastfoot) {
3784 xs << xml::StartTag("tfoot") << xml::CR();
3785 for (row_type r = 0; r < nrows(); ++r) {
3786 if (((havelastfoot && row_info[r].endlastfoot) ||
3787 (havefoot && row_info[r].endfoot)) &&
3788 !row_info[r].caption) {
3789 xmlRow(xs, r, runparams, false, false, buffer().params().docbook_table_output);
3792 xs << xml::EndTag("tfoot");
3796 // Output the main part of the table. The tbody container is mandatory for CALS, but optional for HTML (only if
3797 // there is no header and no footer). It never hurts to have it, though.
3798 xs << xml::StartTag("tbody");
3800 for (row_type r = 0; r < nrows(); ++r)
3802 xmlRow(xs, r, runparams, false, false, buffer().params().docbook_table_output);
3803 xs << xml::EndTag("tbody");
3806 // If this method started the table tag, also make it close it.
3807 if (!runparams.docbook_in_table) {
3808 xs << xml::EndTag("informaltable");
3814 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3818 if (is_long_tabular) {
3819 // we'll wrap it in a div, so as to deal with alignment
3821 switch (longtabular_alignment) {
3822 case LYX_LONGTABULAR_ALIGN_LEFT:
3825 case LYX_LONGTABULAR_ALIGN_CENTER:
3828 case LYX_LONGTABULAR_ALIGN_RIGHT:
3832 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
3834 // The caption flag wins over head/foot
3835 if (haveLTCaption()) {
3836 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
3838 for (row_type r = 0; r < nrows(); ++r)
3839 if (row_info[r].caption)
3840 ret += xmlRow(xs, r, runparams);
3841 xs << xml::EndTag("div");
3846 xs << xml::StartTag("table");
3849 // output header info
3850 bool const havefirsthead = haveLTFirstHead(false);
3851 // if we have a first head, then we are going to ignore the
3852 // headers for the additional pages, since there aren't any
3853 // in XHTML. this test accomplishes that.
3854 bool const havehead = !havefirsthead && haveLTHead(false);
3855 if (havehead || havefirsthead) {
3856 xs << xml::StartTag("thead");
3858 for (row_type r = 0; r < nrows(); ++r) {
3859 if (((havefirsthead && row_info[r].endfirsthead) ||
3860 (havehead && row_info[r].endhead)) &&
3861 !row_info[r].caption) {
3862 ret += xmlRow(xs, r, runparams, true);
3865 xs << xml::EndTag("thead");
3868 // output footer info
3869 bool const havelastfoot = haveLTLastFoot(false);
3871 bool const havefoot = !havelastfoot && haveLTFoot(false);
3872 if (havefoot || havelastfoot) {
3873 xs << xml::StartTag("tfoot") << xml::CR();
3874 for (row_type r = 0; r < nrows(); ++r) {
3875 if (((havelastfoot && row_info[r].endlastfoot) ||
3876 (havefoot && row_info[r].endfoot)) &&
3877 !row_info[r].caption) {
3878 ret += xmlRow(xs, r, runparams);
3881 xs << xml::EndTag("tfoot");
3885 xs << xml::StartTag("tbody") << xml::CR();
3886 for (row_type r = 0; r < nrows(); ++r)
3888 ret += xmlRow(xs, r, runparams);
3889 xs << xml::EndTag("tbody");
3891 xs << xml::EndTag("table");
3893 if (is_long_tabular) {
3894 xs << xml::EndTag("div");
3901 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3902 vector<unsigned int> const & clen) const
3904 idx_type const fcell = getFirstCellInRow(row);
3905 idx_type const n = numberOfCellsInRow(row) + fcell;
3908 for (idx_type i = fcell; i < n; ++i) {
3918 for (idx_type i = fcell; i < n; ++i) {
3929 col_type column = cellColumn(i);
3930 int len = clen[column];
3931 while (column < ncols() - 1
3932 && isPartOfMultiColumn(row, ++column))
3933 len += clen[column] + 4;
3934 os << docstring(len, ch);
3949 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3950 vector<unsigned int> const & clen) const
3952 idx_type const fcell = getFirstCellInRow(row);
3953 idx_type const n = numberOfCellsInRow(row) + fcell;
3956 for (idx_type i = fcell; i < n; ++i) {
3957 if (bottomLine(i)) {
3966 for (idx_type i = fcell; i < n; ++i) {
3967 if (bottomLine(i)) {
3977 col_type column = cellColumn(i);
3978 int len = clen[column];
3979 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3980 len += clen[column] + 4;
3981 os << docstring(len, ch);
3982 if (bottomLine(i)) {
3996 void Tabular::plaintextPrintCell(odocstringstream & os,
3997 OutputParams const & runparams,
3998 idx_type cell, row_type row, col_type column,
3999 vector<unsigned int> const & clen,
4000 bool onlydata, size_t max_length) const
4002 odocstringstream sstr;
4003 cellInset(cell)->plaintext(sstr, runparams, max_length);
4015 unsigned int len1 = sstr.str().length();
4016 unsigned int len2 = clen[column];
4017 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4018 len2 += clen[column] + 4;
4021 switch (getAlignment(cell)) {
4023 case LYX_ALIGN_LEFT:
4026 case LYX_ALIGN_RIGHT:
4030 case LYX_ALIGN_CENTER:
4036 os << docstring(len1, ' ') << sstr.str()
4037 << docstring(len2, ' ');
4039 if (rightLine(cell))
4046 void Tabular::plaintext(odocstringstream & os,
4047 OutputParams const & runparams, int const depth,
4048 bool onlydata, char_type delim, size_t max_length) const
4050 // first calculate the width of the single columns
4051 vector<unsigned int> clen(ncols());
4054 // first all non multicolumn cells!
4055 for (col_type c = 0; c < ncols(); ++c) {
4057 for (row_type r = 0; r < nrows(); ++r) {
4058 idx_type cell = cellIndex(r, c);
4059 if (isMultiColumn(cell))
4061 odocstringstream sstr;
4062 cellInset(cell)->plaintext(sstr, runparams, max_length);
4063 if (clen[c] < sstr.str().length())
4064 clen[c] = sstr.str().length();
4067 // then all multicolumn cells!
4068 for (col_type c = 0; c < ncols(); ++c) {
4069 for (row_type r = 0; r < nrows(); ++r) {
4070 idx_type cell = cellIndex(r, c);
4071 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
4073 odocstringstream sstr;
4074 cellInset(cell)->plaintext(sstr, runparams, max_length);
4075 int len = int(sstr.str().length());
4076 idx_type const n = columnSpan(cell);
4077 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
4079 if (len > int(clen[c + n - 1]))
4080 clen[c + n - 1] = len;
4085 for (row_type r = 0; r < nrows(); ++r) {
4086 if (!onlydata && plaintextTopHLine(os, r, clen))
4087 os << docstring(depth * 2, ' ');
4088 for (col_type c = 0; c < ncols(); ++c) {
4089 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
4091 if (onlydata && c > 0)
4092 // we don't use operator<< for single UCS4 character.
4093 // see explanation in docstream.h
4095 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
4097 if (os.str().size() > max_length)
4102 os << docstring(depth * 2, ' ');
4103 if (plaintextBottomHLine(os, r, clen))
4104 os << docstring(depth * 2, ' ');
4106 if (os.str().size() > max_length)
4112 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4114 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4118 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4120 return cell_info[row][column].inset;
4124 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4126 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4130 void Tabular::setCellInset(row_type row, col_type column,
4131 shared_ptr<InsetTableCell> ins)
4133 CellData & cd = cell_info[row][column];
4138 void Tabular::validate(LaTeXFeatures & features) const
4140 features.require("NeedTabularnewline");
4142 features.require("booktabs");
4143 if (is_long_tabular && !hasVarwidthColumn()) {
4144 if (tabular_width.zero())
4145 features.require("longtable");
4147 features.require("xltabular");
4149 if (rotate && is_long_tabular)
4150 features.require("lscape");
4152 features.require("rotating");
4153 if (hasVarwidthColumn()) {
4154 if (is_long_tabular)
4155 features.require("xltabular");
4157 features.require("tabularx");
4159 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4160 if (isMultiRow(cell))
4161 features.require("multirow");
4162 if (getUsebox(cell) == BOX_VARWIDTH) {
4163 features.require("varwidth");
4164 features.require("cellvarwidth");
4166 if (getVAlignment(cell) != LYX_VALIGN_TOP
4167 || !getPWidth(cell).zero()
4168 || isVTypeColumn(cellColumn(cell)))
4169 features.require("array");
4170 // Tell footnote that we need a savenote
4171 // environment in non-long tables or
4172 // longtable headers/footers
4173 else if (!is_long_tabular && !features.inFloat())
4174 features.saveNoteEnv("tabular");
4175 else if (!isValidRow(cellRow(cell)))
4176 features.saveNoteEnv("longtable");
4178 cellInset(cell)->validate(features);
4179 features.saveNoteEnv(string());
4184 Tabular::BoxType Tabular::useBox(idx_type cell) const
4186 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4187 if (parlist.size() > 1)
4188 return BOX_VARWIDTH;
4190 ParagraphList::const_iterator cit = parlist.begin();
4191 ParagraphList::const_iterator end = parlist.end();
4193 for (; cit != end; ++cit)
4194 for (int i = 0; i < cit->size(); ++i)
4195 if (cit->isNewline(i) || cit->layout().isEnvironment())
4196 return BOX_VARWIDTH;
4202 /////////////////////////////////////////////////////////////////////
4206 /////////////////////////////////////////////////////////////////////
4208 InsetTableCell::InsetTableCell(Buffer * buf)
4209 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false), isVarwidth(false),
4210 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4213 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4215 return isFixedWidth;
4219 bool InsetTableCell::forceLocalFontSwitch() const
4225 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4226 FuncStatus & status) const
4228 bool enabled = true;
4229 switch (cmd.action()) {
4230 case LFUN_INSET_SPLIT:
4231 case LFUN_INSET_DISSOLVE:
4234 case LFUN_MATH_DISPLAY:
4235 if (!hasFixedWidth()) {
4240 return InsetText::getStatus(cur, cmd, status);
4242 status.setEnabled(enabled);
4246 docstring InsetTableCell::asString(bool intoInsets)
4249 if (paragraphs().empty())
4251 ParagraphList::const_iterator it = paragraphs().begin();
4252 ParagraphList::const_iterator en = paragraphs().end();
4254 for (; it != en; ++it) {
4259 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4265 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4266 UpdateType utype, TocBackend & backend) const
4268 InsetText::iterateForToc(di, output_active, utype, backend);
4272 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4275 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4276 return InsetText::xhtml(xs, rp);
4280 void InsetTableCell::docbook(XMLStream & xs, OutputParams const & runparams) const
4282 InsetText::docbook(xs, runparams);
4286 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4288 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4289 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4291 // Hand font through to contained lyxtext:
4292 tm.font_.fontInfo() = mi.base.font;
4293 mi.base.textwidth -= horiz_offset;
4295 // This can happen when a layout has a left and right margin,
4296 // and the view is made very narrow. We can't do better than
4297 // to draw it partly out of view (bug 5890).
4298 if (mi.base.textwidth < 1)
4299 mi.base.textwidth = 1;
4301 // We tell metrics here not to expand on multiple pars
4302 // This is the difference to InsetText::Metrics
4303 Changer changetight = changeVar(mi.tight_insets, true);
4304 if (hasFixedWidth())
4305 tm.metrics(mi, dim, mi.base.textwidth);
4307 tm.metrics(mi, dim, 0);
4308 mi.base.textwidth += horiz_offset;
4309 dim.asc += topOffset(mi.base.bv);
4310 dim.des += bottomOffset(mi.base.bv);
4311 dim.wid += horiz_offset;
4315 /////////////////////////////////////////////////////////////////////
4319 /////////////////////////////////////////////////////////////////////
4321 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4323 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4324 rowselect_(false), colselect_(false)
4329 InsetTabular::InsetTabular(InsetTabular const & tab)
4330 : Inset(tab), tabular(tab.tabular),
4331 rowselect_(false), colselect_(false)
4336 InsetTabular::~InsetTabular()
4338 hideDialogs("tabular", this);
4342 void InsetTabular::setBuffer(Buffer & buf)
4344 tabular.setBuffer(buf);
4345 Inset::setBuffer(buf);
4349 bool InsetTabular::insetAllowed(InsetCode code) const
4354 case MATHMACRO_CODE:
4359 return tabular.is_long_tabular;
4367 bool InsetTabular::allowMultiPar() const
4369 for (col_type c = 0; c < tabular.ncols(); ++c) {
4370 for (row_type r = 0; r < tabular.nrows(); ++r) {
4371 if (tabular.cellInset(r,c)->allowMultiPar())
4379 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4381 return tabular.is_long_tabular &&
4382 (newtype == "Standard" || newtype == "Unnumbered");
4386 void InsetTabular::write(ostream & os) const
4388 os << "Tabular" << endl;
4393 string InsetTabular::contextMenu(BufferView const &, int, int) const
4395 // FIXME: depending on the selection state,
4396 // we could offer a different menu.
4397 return cell(0)->contextMenuName() + ";" + contextMenuName();
4401 string InsetTabular::contextMenuName() const
4403 return "context-tabular";
4407 void InsetTabular::read(Lexer & lex)
4409 //bool const old_format = (lex.getString() == "\\LyXTable");
4417 string token = lex.getString();
4418 while (lex && token != "\\end_inset") {
4420 token = lex.getString();
4423 lex.printError("Missing \\end_inset at this point. ");
4427 int InsetTabular::rowFromY(Cursor & cur, int y) const
4429 // top y coordinate of tabular
4430 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4432 for (; r < tabular.nrows() && y > h; ++r)
4433 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4434 + tabular.interRowSpace(r);
4440 int InsetTabular::columnFromX(Cursor & cur, int x) const
4442 // left x coordinate of tabular
4443 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4445 for (; c < tabular.ncols() && x > w; ++c)
4446 w += tabular.cellWidth(c);
4451 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4453 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4454 // mi.base.textwidth << "\n";
4455 LBUFERR(mi.base.bv);
4457 for (row_type r = 0; r < tabular.nrows(); ++r) {
4460 for (col_type c = 0; c < tabular.ncols(); ++c) {
4461 if (tabular.isPartOfMultiColumn(r, c)
4462 || tabular.isPartOfMultiRow(r, c))
4463 // multicolumn or multirow cell, but not first one
4465 idx_type const cell = tabular.cellIndex(r, c);
4468 Length const p_width = tabular.getPWidth(cell);
4469 if (!p_width.zero())
4470 m.base.textwidth = mi.base.inPixels(p_width);
4471 else if (tabular.column_info[c].varwidth)
4472 m.base.textwidth = tabular.column_info[c].width;
4473 tabular.cellInset(cell)->metrics(m, dim0);
4474 if (!p_width.zero() || tabular.column_info[c].varwidth)
4475 dim0.wid = m.base.textwidth;
4476 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4477 + tabular.interColumnSpace(cell);
4479 // FIXME(?): do we need a second metrics call?
4480 TextMetrics const & tm =
4481 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4483 // determine horizontal offset because of decimal align (if necessary)
4484 int decimal_width = 0;
4485 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4486 InsetTableCell tail = *tabular.cellInset(cell);
4487 tail.setBuffer(tabular.buffer());
4488 // we need to set macrocontext position everywhere
4489 // otherwise we crash with nested insets (e.g. footnotes)
4490 // after decimal point
4491 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4493 dit.push_back(CursorSlice(tail));
4494 tail.setMacrocontextPositionRecursive(dit);
4496 // remove text leading decimal point
4497 docstring const align_d = tabular.column_info[c].decimal_point;
4498 dit = separatorPos(&tail, align_d);
4500 pos_type const psize = tail.paragraphs().front().size();
4502 tail.paragraphs().front().eraseChars(0,
4503 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4505 tail.metrics(m, dim1);
4506 decimal_width = dim1.width();
4509 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4510 tabular.cell_info[r][c].decimal_width = decimal_width;
4512 // with LYX_VALIGN_BOTTOM the descent is relative to the last
4513 // row of the last par (note that the par might have multile rows!)
4514 // = descent of text in last row + bottomOffset:
4515 int const lastpardes = tm.last().second->rows().back().descent()
4516 + bottomOffset(mi.base.bv);
4518 switch (tabular.getVAlignment(cell)) {
4519 case Tabular::LYX_VALIGN_TOP:
4521 case Tabular::LYX_VALIGN_MIDDLE:
4522 offset = -(dim0.des - lastpardes)/2;
4524 case Tabular::LYX_VALIGN_BOTTOM:
4525 offset = -(dim0.des - lastpardes);
4528 tabular.cell_info[r][c].voffset = offset;
4529 maxasc = max(maxasc, dim0.asc - offset);
4530 maxdes = max(maxdes, dim0.des + offset);
4532 int const top_space = tabular.row_info[r].top_space_default ?
4533 default_line_space :
4534 mi.base.inPixels(tabular.row_info[r].top_space);
4535 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4536 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4537 default_line_space :
4538 mi.base.inPixels(tabular.row_info[r].bottom_space);
4539 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4542 // We need to recalculate the metrics after column width calculation
4543 // with xtabular (possibly multiple times, so the call is recursive).
4544 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4546 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4547 dim.des = tabular.height() - dim.asc;
4548 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4552 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4554 if (&cur.inset() == this && cur.selection()) {
4555 if (cur.selIsMultiCell()) {
4558 getSelection(cur, rs, re, cs, ce);
4560 idx_type const cell = tabular.cellIndex(row, col);
4561 col_type const cspan = tabular.columnSpan(cell);
4562 row_type const rspan = tabular.rowSpan(cell);
4563 if (col + cspan - 1 >= cs && col <= ce
4564 && row + rspan - 1 >= rs && row <= re)
4567 if (col == tabular.cellColumn(cur.idx())
4568 && row == tabular.cellRow(cur.idx())) {
4569 CursorSlice const & beg = cur.selBegin();
4570 CursorSlice const & end = cur.selEnd();
4572 if ((end.lastpos() > 0 || end.lastpit() > 0)
4573 && end.pos() == end.lastpos() && beg.pos() == 0
4574 && end.pit() == end.lastpit() && beg.pit() == 0)
4582 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4584 x += ADD_TO_TABULAR_WIDTH;
4586 BufferView * bv = pi.base.bv;
4587 Cursor & cur = pi.base.bv->cursor();
4589 // FIXME: As the full background is painted in drawBackground(),
4590 // we have no choice but to do a full repaint for the Text cells.
4591 pi.full_repaint = true;
4593 bool const original_selection_state = pi.selected;
4597 // Save tabular change status
4598 Change tab_change = pi.change;
4600 int yy = y + tabular.offsetVAlignment();
4601 for (row_type r = 0; r < tabular.nrows(); ++r) {
4603 for (col_type c = 0; c < tabular.ncols(); ++c) {
4604 if (tabular.isPartOfMultiColumn(r, c))
4607 idx = tabular.cellIndex(r, c);
4609 if (tabular.isPartOfMultiRow(r, c)) {
4610 nx += tabular.cellWidth(idx);
4614 pi.selected |= isCellSelected(cur, r, c);
4616 // Mark deleted rows/columns
4617 if (tabular.column_info[c].change.changed())
4618 pi.change = tabular.column_info[c].change;
4619 else if (tabular.row_info[r].change.changed())
4620 pi.change = tabular.row_info[r].change;
4622 pi.change = tab_change;
4624 int const cx = nx + tabular.textHOffset(idx);
4625 int const cy = yy + tabular.textVOffset(idx);
4626 // Cache the Inset position.
4627 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4628 cell(idx)->draw(pi, cx, cy);
4629 drawCellLines(pi, nx, yy, r, idx);
4630 nx += tabular.cellWidth(idx);
4631 pi.selected = original_selection_state;
4634 if (r + 1 < tabular.nrows())
4635 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4636 + tabular.interRowSpace(r + 1);
4641 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4643 x += ADD_TO_TABULAR_WIDTH;
4644 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4645 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4646 pi.backgroundColor(this));
4650 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4652 Cursor & cur = pi.base.bv->cursor();
4654 x += ADD_TO_TABULAR_WIDTH;
4656 if (!cur.selection())
4658 if (&cur.inset() != this)
4663 bool const full_cell_selected = isCellSelected(cur,
4664 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4666 if (cur.selIsMultiCell() || full_cell_selected) {
4667 for (row_type r = 0; r < tabular.nrows(); ++r) {
4669 for (col_type c = 0; c < tabular.ncols(); ++c) {
4670 if (tabular.isPartOfMultiColumn(r, c))
4673 idx_type const cell = tabular.cellIndex(r, c);
4675 if (tabular.isPartOfMultiRow(r, c)) {
4676 xx += tabular.cellWidth(cell);
4679 int const w = tabular.cellWidth(cell);
4680 int const h = tabular.cellHeight(cell);
4681 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4682 if (isCellSelected(cur, r, c))
4683 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4686 if (r + 1 < tabular.nrows())
4687 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4688 + tabular.interRowSpace(r + 1);
4692 // FIXME: This code has no effect because InsetTableCell does not handle
4693 // drawSelection other than the trivial implementation in Inset.
4695 // x += cellXPos(cur.idx());
4696 // x += tabular.textHOffset(cur.idx());
4697 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4704 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4705 Color const incol, bool drawline, bool heavy = false)
4707 Color const col = drawline ? incol : Color_tabularonoffline;
4708 if (drawline && lt > 0)
4709 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4710 Painter::line_onoffdash,
4711 Painter::thin_line);
4712 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4713 drawline ? Painter::line_solid : Painter::line_onoffdash,
4714 (heavy ? 2 : 1) * Painter::thin_line);
4715 if (drawline && rt > 0)
4716 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4717 Painter::line_onoffdash,
4718 Painter::thin_line);
4724 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4725 row_type row, idx_type cell) const
4727 y -= tabular.rowAscent(row);
4728 int const w = tabular.cellWidth(cell);
4729 int const h = tabular.cellHeight(cell);
4733 col_type const col = tabular.cellColumn(cell);
4735 // Colour the frame if rows/columns are added or deleted
4736 Color colour = Color_tabularline;
4737 if (tabular.column_info[col].change.changed()
4738 || tabular.row_info[row].change.changed())
4739 colour = tabular.cellInset(cell)->paragraphs().front().lookupChange(0).color();
4742 bool drawline = tabular.topLine(cell)
4743 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4744 bool heavy = tabular.use_booktabs
4745 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4746 && tabular.rowTopLine(row);
4747 if (tabular.topLineTrim(cell).first
4748 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4750 if (tabular.topLineTrim(cell).second
4751 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4753 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4757 drawline = tabular.bottomLine(cell);
4758 row_type const lastrow = tabular.nrows() - 1;
4759 // Consider multi-rows
4761 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4762 && tabular.isPartOfMultiRow(r + 1, col))
4764 heavy = tabular.use_booktabs
4765 && ((row == lastrow && tabular.rowBottomLine(row))
4766 || (r == lastrow && tabular.rowBottomLine(r)));
4767 if (tabular.bottomLineTrim(cell).first)
4769 if (tabular.bottomLineTrim(cell).second)
4771 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4774 drawline = tabular.leftLine(cell)
4775 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4776 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4779 x -= tabular.interColumnSpace(cell);
4780 col_type next_cell_col = col + 1;
4781 while (next_cell_col < tabular.ncols()
4782 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4784 drawline = tabular.rightLine(cell)
4785 || (next_cell_col < tabular.ncols()
4786 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4787 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4791 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4793 //lyxerr << "InsetTabular::edit: " << this << endl;
4797 if (isRightToLeft(cur))
4798 cur.idx() = tabular.getLastCellInRow(0);
4804 if (isRightToLeft(cur))
4805 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4807 cur.idx() = tabular.numberofcells - 1;
4809 cur.pos() = cur.lastpos(); // FIXME crude guess
4811 cur.setCurrentFont();
4812 // FIXME: this accesses the position cache before it is initialized
4813 //cur.bv().fitCursor();
4817 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4819 // In a longtable, tell captions what the current float is
4820 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4821 string const saveflt = cnts.current_float();
4822 if (tabular.is_long_tabular) {
4823 cnts.current_float("table");
4824 // in longtables, we only step the counter once
4825 cnts.step(from_ascii("table"), utype);
4826 cnts.isLongtable(true);
4829 ParIterator it2 = it;
4831 size_t const end = it2.nargs();
4832 for ( ; it2.idx() < end; it2.top().forwardIdx())
4833 buffer().updateBuffer(it2, utype);
4836 if (tabular.is_long_tabular) {
4837 cnts.current_float(saveflt);
4838 cnts.isLongtable(false);
4843 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4844 UpdateType utype, TocBackend & backend) const
4846 DocIterator dit = cpit;
4848 size_t const end = dit.nargs();
4849 for ( ; dit.idx() < end; dit.top().forwardIdx())
4850 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4854 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4856 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4857 return x < x0 || x > x0 + tabular.width();
4861 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4863 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4864 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4865 // there is no margin added vertically to tabular insets.
4866 // However, it works for now.
4867 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4871 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4873 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4877 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4879 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4880 << "\n cur:" << cur);
4881 CursorSlice sl = cur.top();
4882 Cursor & bvcur = cur.bv().cursor();
4884 FuncCode const act = cmd.action();
4888 case LFUN_MOUSE_PRESS: {
4889 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4891 if (hitSelectRow(cur.bv(), cmd.x())) {
4892 row_type r = rowFromY(cur, cmd.y());
4893 cur.idx() = tabular.getFirstCellInRow(r);
4897 cur.idx() = tabular.getLastCellInRow(r);
4898 cur.pit() = cur.lastpit();
4899 cur.pos() = cur.lastpos();
4900 cur.selection(true);
4906 if (hitSelectColumn(cur.bv(), cmd.y())) {
4907 col_type c = columnFromX(cur, cmd.x());
4908 cur.idx() = tabular.cellIndex(0, c);
4912 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4913 cur.pit() = cur.lastpit();
4914 cur.pos() = cur.lastpos();
4915 cur.selection(true);
4920 // do not reset cursor/selection if we have selected
4921 // some cells (bug 2715).
4922 if (cmd.button() == mouse_button::button3
4923 && &bvcur.selBegin().inset() == this
4924 && bvcur.selIsMultiCell())
4927 // Let InsetTableCell do it
4928 cell(cur.idx())->dispatch(cur, cmd);
4931 case LFUN_MOUSE_MOTION:
4932 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4933 if (cmd.button() == mouse_button::button1) {
4934 // only accept motions to places not deeper nested than the real anchor
4935 if (!bvcur.realAnchor().hasPart(cur)) {
4939 // select (additional) row
4941 row_type r = rowFromY(cur, cmd.y());
4942 cur.idx() = tabular.getLastCellInRow(r);
4943 // we need to reset the cursor's pit and pos now, as the old ones
4944 // may no longer be valid.
4947 bvcur.setCursor(cur);
4948 bvcur.selection(true);
4951 // select (additional) column
4953 col_type c = columnFromX(cur, cmd.x());
4954 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4955 // we need to reset the cursor's pit and pos now, as the old ones
4956 // may no longer be valid.
4959 bvcur.setCursor(cur);
4960 bvcur.selection(true);
4963 // only update if selection changes
4964 if (bvcur.idx() == cur.idx() &&
4965 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4966 cur.noScreenUpdate();
4967 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4968 bvcur.setCursor(cur);
4969 bvcur.selection(true);
4970 // if this is a multicell selection, we just set the cursor to
4971 // the beginning of the cell's text.
4972 if (bvcur.selIsMultiCell()) {
4973 bvcur.pit() = bvcur.lastpit();
4974 bvcur.pos() = bvcur.lastpos();
4979 case LFUN_MOUSE_RELEASE:
4984 case LFUN_CELL_BACKWARD:
4986 cur.selection(false);
4989 case LFUN_CELL_FORWARD:
4991 cur.selection(false);
4994 case LFUN_CHAR_FORWARD_SELECT:
4995 case LFUN_CHAR_FORWARD:
4996 case LFUN_CHAR_BACKWARD_SELECT:
4997 case LFUN_CHAR_BACKWARD:
4998 case LFUN_CHAR_RIGHT_SELECT:
4999 case LFUN_CHAR_RIGHT:
5000 case LFUN_CHAR_LEFT_SELECT:
5001 case LFUN_CHAR_LEFT:
5002 case LFUN_WORD_FORWARD:
5003 case LFUN_WORD_FORWARD_SELECT:
5004 case LFUN_WORD_BACKWARD:
5005 case LFUN_WORD_BACKWARD_SELECT:
5006 case LFUN_WORD_RIGHT:
5007 case LFUN_WORD_RIGHT_SELECT:
5008 case LFUN_WORD_LEFT:
5009 case LFUN_WORD_LEFT_SELECT: {
5010 // determine whether we move to next or previous cell, where to enter
5011 // the new cell from, and which command to "finish" (i.e., exit the
5014 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
5015 FuncCode finish_lfun;
5017 if (act == LFUN_CHAR_FORWARD
5018 || act == LFUN_CHAR_FORWARD_SELECT
5019 || act == LFUN_WORD_FORWARD
5020 || act == LFUN_WORD_FORWARD_SELECT) {
5022 finish_lfun = LFUN_FINISHED_FORWARD;
5024 else if (act == LFUN_CHAR_BACKWARD
5025 || act == LFUN_CHAR_BACKWARD_SELECT
5026 || act == LFUN_WORD_BACKWARD
5027 || act == LFUN_WORD_BACKWARD_SELECT) {
5029 finish_lfun = LFUN_FINISHED_BACKWARD;
5031 // LEFT or RIGHT commands --- the interpretation will depend on the
5032 // table's direction.
5034 bool const right = act == LFUN_CHAR_RIGHT
5035 || act == LFUN_CHAR_RIGHT_SELECT
5036 || act == LFUN_WORD_RIGHT
5037 || act == LFUN_WORD_RIGHT_SELECT;
5038 next_cell = isRightToLeft(cur) != right;
5040 if (lyxrc.visual_cursor)
5041 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
5043 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
5046 bool const select = act == LFUN_CHAR_FORWARD_SELECT
5047 || act == LFUN_CHAR_BACKWARD_SELECT
5048 || act == LFUN_CHAR_RIGHT_SELECT
5049 || act == LFUN_CHAR_LEFT_SELECT
5050 || act == LFUN_WORD_FORWARD_SELECT
5051 || act == LFUN_WORD_RIGHT_SELECT
5052 || act == LFUN_WORD_BACKWARD_SELECT
5053 || act == LFUN_WORD_LEFT_SELECT;
5055 // If we have a multicell selection or we're
5056 // not doing some LFUN_*_SELECT thing anyway...
5057 if (!cur.selIsMultiCell() || !select) {
5058 col_type const c = tabular.cellColumn(cur.idx());
5059 row_type const r = tabular.cellRow(cur.idx());
5060 // Are we trying to select the whole cell and is the whole cell
5061 // not yet selected?
5062 bool const select_whole = select && !isCellSelected(cur, r, c) &&
5063 ((next_cell && cur.pit() == cur.lastpit()
5064 && cur.pos() == cur.lastpos())
5065 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
5067 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
5069 // ...try to dispatch to the cell's inset.
5070 cell(cur.idx())->dispatch(cur, cmd);
5072 // When we already have a selection we want to select the whole cell
5073 // before going to the next cell.
5074 if (select_whole && !empty_cell){
5075 getText(cur.idx())->selectAll(cur);
5077 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5081 // FIXME: When we support the selection of an empty cell, remove
5082 // the !empty_cell from this condition. For now we jump to the next
5083 // cell if the current cell is empty.
5084 if (cur.result().dispatched() && !empty_cell)
5088 // move to next/prev cell, as appropriate
5089 // note that we will always do this if we're selecting and we have
5090 // a multicell selection
5091 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
5092 << " cell from: " << int(entry_from));
5094 moveNextCell(cur, entry_from);
5096 movePrevCell(cur, entry_from);
5097 // if we're exiting the table, call the appropriate FINISHED lfun
5098 if (sl == cur.top()) {
5099 cmd = FuncRequest(finish_lfun);
5104 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5109 case LFUN_DOWN_SELECT:
5111 if (!(cur.selection() && cur.selIsMultiCell()))
5112 cell(cur.idx())->dispatch(cur, cmd);
5114 cur.dispatched(); // override the cell's decision
5115 if (sl == cur.top()) {
5116 // if our Text didn't do anything to the cursor
5117 // then we try to put the cursor into the cell below
5118 // setting also the right targetX.
5119 cur.selHandle(act == LFUN_DOWN_SELECT);
5120 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
5121 int const xtarget = cur.targetX();
5122 // WARNING: Once cur.idx() has been reset, the cursor is in
5123 // an inconsistent state until pos() has been set. Be careful
5124 // what you do with it!
5125 cur.idx() = tabular.cellBelow(cur.idx());
5127 TextMetrics const & tm =
5128 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5129 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5130 cur.setCurrentFont();
5133 if (sl == cur.top()) {
5134 // we trick it to go to forward after leaving the
5136 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5139 if (cur.selIsMultiCell()) {
5140 cur.pit() = cur.lastpit();
5141 cur.pos() = cur.lastpos();
5142 cur.setCurrentFont();
5143 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5146 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5149 case LFUN_UP_SELECT:
5151 if (!(cur.selection() && cur.selIsMultiCell()))
5152 cell(cur.idx())->dispatch(cur, cmd);
5153 cur.dispatched(); // override the cell's decision
5154 if (sl == cur.top()) {
5155 // if our Text didn't do anything to the cursor
5156 // then we try to put the cursor into the cell above
5157 // setting also the right targetX.
5158 cur.selHandle(act == LFUN_UP_SELECT);
5159 if (tabular.cellRow(cur.idx()) != 0) {
5160 int const xtarget = cur.targetX();
5161 // WARNING: Once cur.idx() has been reset, the cursor is in
5162 // an inconsistent state until pos() has been set. Be careful
5163 // what you do with it!
5164 cur.idx() = tabular.cellAbove(cur.idx());
5165 cur.pit() = cur.lastpit();
5166 Text const * text = cell(cur.idx())->getText(0);
5167 TextMetrics const & tm = cur.bv().textMetrics(text);
5168 ParagraphMetrics const & pm =
5169 tm.parMetrics(cur.lastpit());
5170 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5171 cur.setCurrentFont();
5174 if (sl == cur.top()) {
5175 cmd = FuncRequest(LFUN_UP);
5178 if (cur.selIsMultiCell()) {
5180 cur.pos() = cur.lastpos();
5181 cur.setCurrentFont();
5182 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5185 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5188 case LFUN_LAYOUT_TABULAR:
5189 cur.bv().showDialog("tabular");
5192 case LFUN_INSET_MODIFY:
5193 // we come from the dialog
5194 if (cmd.getArg(0) == "tabular")
5195 tabularFeatures(cur, cmd.getLongArg(1));
5200 case LFUN_TABULAR_FEATURE:
5201 tabularFeatures(cur, to_utf8(cmd.argument()));
5204 // insert file functions
5205 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5206 case LFUN_FILE_INSERT_PLAINTEXT:
5208 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5209 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5210 FileName(to_utf8(cmd.argument())));
5213 cur.recordUndoInset();
5214 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5215 // content has been replaced,
5216 // so cursor might be invalid
5217 cur.pos() = cur.lastpos();
5218 cur.pit() = cur.lastpit();
5219 bvcur.setCursor(cur);
5226 if (cur.selIsMultiCell())
5229 cell(cur.idx())->dispatch(cur, cmd);
5233 if (cur.selIsMultiCell()) {
5234 if (copySelection(cur)) {
5235 cur.recordUndoInset();
5239 cell(cur.idx())->dispatch(cur, cmd);
5242 case LFUN_SELF_INSERT:
5243 if (cur.selIsMultiCell()) {
5244 cur.recordUndoInset();
5246 BufferView * bv = &cur.bv();
5247 docstring::const_iterator cit = cmd.argument().begin();
5248 docstring::const_iterator const end = cmd.argument().end();
5249 for (; cit != end; ++cit)
5250 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5253 bv->bookmarkEditPosition();
5255 cell(cur.idx())->dispatch(cur, cmd);
5258 case LFUN_CHAR_DELETE_BACKWARD:
5259 case LFUN_CHAR_DELETE_FORWARD:
5260 if (cur.selIsMultiCell()) {
5261 cur.recordUndoInset();
5264 cell(cur.idx())->dispatch(cur, cmd);
5267 case LFUN_CLIPBOARD_PASTE:
5268 case LFUN_PRIMARY_SELECTION_PASTE: {
5269 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5270 theClipboard().getAsText(Clipboard::PlainTextType) :
5271 theSelection().get();
5274 // pass to InsertPlaintextString, but
5275 // only if we have multi-cell content
5276 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5277 cur.recordUndoInset();
5278 if (insertPlaintextString(cur.bv(), clip, false)) {
5279 // content has been replaced,
5280 // so cursor might be invalid
5282 bvcur.setCursor(cur);
5286 // Let the cell handle normal text
5287 cell(cur.idx())->dispatch(cur, cmd);
5292 if (!tabularStackDirty()) {
5293 // Check if we have plain text or HTML with rows/columns.
5294 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5295 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5296 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5297 docstring const clip =
5298 theClipboard().getAsText(Clipboard::AnyTextType);
5299 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5300 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5301 doDispatch(cur, ncmd);
5305 else if (!theClipboard().isInternal()
5306 && theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5307 // This might be tabular data from another LyX instance. Check!
5308 docstring const clip =
5309 theClipboard().getAsText(Clipboard::PlainTextType);
5310 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5311 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5312 doDispatch(cur, ncmd);
5316 if (!cur.selIsMultiCell())
5317 cell(cur.idx())->dispatch(cur, cmd);
5320 if (theClipboard().isInternal()) {
5321 cur.recordUndoInset();
5322 pasteClipboard(cur);
5326 case LFUN_CHANGE_ACCEPT:
5327 case LFUN_CHANGE_REJECT:
5328 case LFUN_FONT_EMPH:
5329 case LFUN_FONT_BOLD:
5330 case LFUN_FONT_BOLDSYMBOL:
5331 case LFUN_FONT_ROMAN:
5332 case LFUN_FONT_NOUN:
5333 case LFUN_FONT_ITAL:
5334 case LFUN_FONT_FRAK:
5335 case LFUN_FONT_TYPEWRITER:
5336 case LFUN_FONT_SANS:
5337 case LFUN_TEXTSTYLE_APPLY:
5338 case LFUN_TEXTSTYLE_UPDATE:
5339 case LFUN_FONT_SIZE:
5340 case LFUN_FONT_UNDERLINE:
5341 case LFUN_FONT_STRIKEOUT:
5342 case LFUN_FONT_CROSSOUT:
5343 case LFUN_FONT_UNDERUNDERLINE:
5344 case LFUN_FONT_UNDERWAVE:
5346 case LFUN_PARAGRAPH_PARAMS_APPLY:
5347 case LFUN_PARAGRAPH_PARAMS:
5348 case LFUN_WORD_CAPITALIZE:
5349 case LFUN_WORD_UPCASE:
5350 case LFUN_WORD_LOWCASE:
5351 case LFUN_CHARS_TRANSPOSE: {
5352 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5353 if (cur.selIsMultiCell()) {
5356 getSelection(cur, rs, re, cs, ce);
5357 Cursor tmpcur = cur;
5358 for (row_type r = rs; r <= re; ++r) {
5359 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5360 // whole row selected
5361 if (act == LFUN_CHANGE_ACCEPT) {
5362 if (tabular.row_info[r].change.inserted())
5363 tabular.row_info[r].change.setUnchanged();
5364 else if (tabular.row_info[r].change.deleted()) {
5365 tabular.deleteRow(r, true);
5370 if (tabular.row_info[r].change.deleted())
5371 tabular.row_info[r].change.setUnchanged();
5372 else if (tabular.row_info[r].change.inserted()) {
5373 tabular.deleteRow(r, true);
5379 for (col_type c = cs; c <= ce; ++c) {
5380 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5381 // whole col selected
5382 if (act == LFUN_CHANGE_ACCEPT) {
5383 if (tabular.column_info[c].change.inserted())
5384 tabular.column_info[c].change.setUnchanged();
5385 else if (tabular.column_info[c].change.deleted()) {
5386 tabular.deleteColumn(c, true);
5391 if (tabular.column_info[c].change.deleted())
5392 tabular.column_info[c].change.setUnchanged();
5393 else if (tabular.column_info[c].change.inserted()) {
5394 tabular.deleteColumn(c, true);
5400 // cursor follows cell:
5401 tmpcur.idx() = tabular.cellIndex(r, c);
5402 // select this cell only:
5405 tmpcur.resetAnchor();
5406 tmpcur.pit() = tmpcur.lastpit();
5407 tmpcur.pos() = tmpcur.top().lastpos();
5408 tmpcur.setCursor(tmpcur);
5409 tmpcur.setSelection();
5410 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5414 tabular.updateIndexes();
5415 // cursor might be invalid
5417 // change bar might need to be redrawn
5418 cur.screenUpdateFlags(Update::Force);
5419 cur.forceBufferUpdate();
5423 cell(cur.idx())->dispatch(cur, cmd);
5428 case LFUN_CHANGE_NEXT:
5429 case LFUN_CHANGE_PREVIOUS: {
5430 // BufferView::dispatch has already moved the cursor, we just
5431 // need to select here if we have a changed row or column
5432 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5434 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5438 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5439 cur.pit() = cur.lastpit();
5440 cur.pos() = cur.lastpos();
5441 cur.selection(true);
5445 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5447 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5451 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5452 cur.pit() = cur.lastpit();
5453 cur.pos() = cur.lastpos();
5454 cur.selection(true);
5461 case LFUN_INSET_SETTINGS:
5462 // relay this lfun to Inset, not to the cell.
5463 Inset::doDispatch(cur, cmd);
5467 // we try to handle this event in the insets dispatch function.
5468 cell(cur.idx())->dispatch(cur, cmd);
5474 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5475 string const & argument, FuncStatus & status) const
5478 int action = Tabular::LAST_ACTION;
5480 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5481 if (tabularFeature[i].feature == s) {
5482 action = tabularFeature[i].action;
5486 if (action == Tabular::LAST_ACTION) {
5488 status.setUnknown(true);
5492 row_type sel_row_start = 0;
5493 row_type sel_row_end = 0;
5494 col_type sel_col_start = 0;
5495 col_type sel_col_end = 0;
5496 Tabular::ltType dummyltt;
5499 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5502 case Tabular::SET_PWIDTH:
5503 case Tabular::SET_MPWIDTH:
5504 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5505 case Tabular::SET_SPECIAL_COLUMN:
5506 case Tabular::SET_SPECIAL_MULTICOLUMN:
5507 case Tabular::APPEND_ROW:
5508 case Tabular::APPEND_COLUMN:
5509 case Tabular::COPY_ROW:
5510 case Tabular::COPY_COLUMN:
5511 case Tabular::SET_TOP_SPACE:
5512 case Tabular::SET_BOTTOM_SPACE:
5513 case Tabular::SET_INTERLINE_SPACE:
5517 case Tabular::DELETE_ROW:
5518 status.setEnabled(tabular.nrows() > 1);
5520 case Tabular::DELETE_COLUMN:
5521 status.setEnabled(tabular.ncols() > 1);
5524 case Tabular::SET_TABULAR_WIDTH:
5525 status.setEnabled(!tabular.rotate
5526 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5529 case Tabular::MOVE_COLUMN_RIGHT:
5530 case Tabular::MOVE_COLUMN_LEFT:
5531 case Tabular::MOVE_ROW_DOWN:
5532 case Tabular::MOVE_ROW_UP: {
5535 if (cur.selIsMultiCell())
5536 getSelection(cur, rs, re, cs, ce);
5538 rs = tabular.cellRow(cur.idx());
5540 cs = tabular.cellColumn(cur.idx());
5543 if ((action == Tabular::MOVE_COLUMN_RIGHT
5544 && tabular.ncols() == ce + 1)
5545 || (action == Tabular::MOVE_COLUMN_LEFT && cs == 0)
5546 || (action == Tabular::MOVE_ROW_DOWN
5547 && tabular.nrows() == re + 1)
5548 || (action == Tabular::MOVE_ROW_UP && rs == 0)) {
5549 status.setEnabled(false);
5553 if (action == Tabular::MOVE_COLUMN_RIGHT
5554 || action == Tabular::MOVE_COLUMN_LEFT) {
5555 bool has_multicol = (action == Tabular::MOVE_COLUMN_RIGHT)
5556 ? tabular.hasMultiColumn(ce + 1)
5557 : tabular.hasMultiColumn(cs - 1);
5558 for (col_type c = cs; c <= ce; ++c) {
5559 if (tabular.hasMultiColumn(c)) {
5560 has_multicol = true;
5565 status.message(_("Column movement not supported with multi-columns."));
5566 status.setEnabled(false);
5571 if (action == Tabular::MOVE_ROW_DOWN
5572 || action == Tabular::MOVE_ROW_UP) {
5573 bool has_multirow = (action == Tabular::MOVE_ROW_DOWN)
5574 ? tabular.hasMultiRow(re + 1)
5575 : tabular.hasMultiRow(rs - 1);
5576 for (row_type r = rs; r <= re; ++r) {
5577 if (tabular.hasMultiRow(r)) {
5578 has_multirow = true;
5583 status.message(_("Row movement not supported with multi-rows."));
5584 status.setEnabled(false);
5589 status.setEnabled(true);
5593 case Tabular::SET_DECIMAL_POINT:
5595 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5598 case Tabular::SET_MULTICOLUMN:
5599 case Tabular::UNSET_MULTICOLUMN:
5600 case Tabular::MULTICOLUMN:
5601 // If a row is set as longtable caption, it must not be allowed
5602 // to unset that this row is a multicolumn.
5603 status.setEnabled(sel_row_start == sel_row_end
5604 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5605 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5608 case Tabular::SET_MULTIROW:
5609 case Tabular::UNSET_MULTIROW:
5610 case Tabular::MULTIROW:
5611 // If a row is set as longtable caption, it must not be allowed
5612 // to unset that this row is a multirow.
5613 status.setEnabled(sel_col_start == sel_col_end
5614 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5615 status.setOnOff(tabular.isMultiRow(cur.idx()));
5618 case Tabular::TOGGLE_INNER_LINES:
5619 status.setOnOff(tabular.innerBorders(sel_row_start, sel_row_end,
5620 sel_col_start, sel_col_end));
5621 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5623 case Tabular::TOGGLE_ALL_LINES:
5624 status.setOnOff(tabular.innerBorders(sel_row_start, sel_row_end,
5625 sel_col_start, sel_col_end)
5626 && tabular.outsideBorders(sel_row_start, sel_row_end,
5627 sel_col_start, sel_col_end));
5628 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5630 case Tabular::SET_ALL_LINES:
5631 case Tabular::UNSET_ALL_LINES:
5632 case Tabular::SET_INNER_LINES:
5633 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5636 case Tabular::TOGGLE_BORDER_LINES:
5637 status.setOnOff(tabular.outsideBorders(sel_row_start, sel_row_end,
5638 sel_col_start, sel_col_end));
5640 case Tabular::SET_BORDER_LINES:
5641 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5644 case Tabular::RESET_FORMAL_DEFAULT:
5645 status.setEnabled(tabular.use_booktabs);
5648 case Tabular::SET_LINE_TOP:
5649 case Tabular::SET_LINE_BOTTOM:
5650 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5653 case Tabular::SET_LINE_LEFT:
5654 case Tabular::SET_LINE_RIGHT:
5655 status.setEnabled(!tabular.use_booktabs
5656 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5659 case Tabular::SET_LTRIM_TOP:
5660 case Tabular::SET_RTRIM_TOP:
5661 status.setEnabled(tabular.use_booktabs
5662 && tabular.cellRow(cur.idx()) != 0
5663 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5666 case Tabular::SET_LTRIM_BOTTOM:
5667 case Tabular::SET_RTRIM_BOTTOM:
5668 status.setEnabled(tabular.use_booktabs
5669 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5670 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5673 case Tabular::TOGGLE_LINE_TOP:
5674 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5675 status.setOnOff(tabular.topLine(cur.idx()));
5678 case Tabular::TOGGLE_LINE_BOTTOM:
5679 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5680 status.setOnOff(tabular.bottomLine(cur.idx()));
5683 case Tabular::TOGGLE_LINE_LEFT:
5684 status.setEnabled(!tabular.use_booktabs
5685 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5686 status.setOnOff(tabular.leftLine(cur.idx()));
5689 case Tabular::TOGGLE_LINE_RIGHT:
5690 status.setEnabled(!tabular.use_booktabs
5691 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5692 status.setOnOff(tabular.rightLine(cur.idx()));
5695 case Tabular::TOGGLE_LTRIM_TOP:
5696 status.setEnabled(tabular.use_booktabs
5697 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5698 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5701 case Tabular::TOGGLE_RTRIM_TOP:
5702 status.setEnabled(tabular.use_booktabs
5703 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5704 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5707 case Tabular::TOGGLE_LTRIM_BOTTOM:
5708 status.setEnabled(tabular.use_booktabs
5709 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5710 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5713 case Tabular::TOGGLE_RTRIM_BOTTOM:
5714 status.setEnabled(tabular.use_booktabs
5715 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5716 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5719 // multirow cells only inherit the alignment of the column if the column has
5720 // no width, otherwise they are left-aligned
5721 // therefore allow always left but right and center only if there is no width
5722 case Tabular::M_ALIGN_LEFT:
5725 case Tabular::ALIGN_LEFT:
5726 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5729 case Tabular::M_ALIGN_RIGHT:
5732 case Tabular::ALIGN_RIGHT:
5733 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5734 && !tabular.getPWidth(cur.idx()).zero()));
5735 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5738 case Tabular::M_ALIGN_CENTER:
5741 case Tabular::ALIGN_CENTER:
5742 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5743 && !tabular.getPWidth(cur.idx()).zero()));
5744 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5747 case Tabular::ALIGN_BLOCK:
5748 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5749 && !tabular.isMultiRow(cur.idx()));
5750 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5753 case Tabular::ALIGN_DECIMAL:
5754 status.setEnabled(!tabular.isMultiRow(cur.idx())
5755 && !tabular.isMultiColumn(cur.idx()));
5756 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5759 case Tabular::M_VALIGN_TOP:
5762 case Tabular::VALIGN_TOP:
5763 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5764 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5766 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5769 case Tabular::M_VALIGN_BOTTOM:
5772 case Tabular::VALIGN_BOTTOM:
5773 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5774 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5776 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5779 case Tabular::M_VALIGN_MIDDLE:
5782 case Tabular::VALIGN_MIDDLE:
5783 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5784 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5786 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5789 case Tabular::SET_LONGTABULAR:
5790 case Tabular::TOGGLE_LONGTABULAR:
5791 // setting as longtable is not allowed when table is inside a float
5792 if (cur.innerInsetOfType(FLOAT_CODE) != nullptr
5793 || cur.innerInsetOfType(WRAP_CODE) != nullptr)
5794 status.setEnabled(false);
5796 status.setEnabled(true);
5797 status.setOnOff(tabular.is_long_tabular);
5800 case Tabular::UNSET_LONGTABULAR:
5801 status.setOnOff(!tabular.is_long_tabular);
5804 case Tabular::TOGGLE_ROTATE_TABULAR:
5805 case Tabular::SET_ROTATE_TABULAR:
5806 status.setOnOff(tabular.rotate != 0);
5809 case Tabular::TABULAR_VALIGN_TOP:
5810 status.setEnabled(tabular.tabular_width.zero());
5811 status.setOnOff(tabular.tabular_valignment
5812 == Tabular::LYX_VALIGN_TOP);
5814 case Tabular::TABULAR_VALIGN_MIDDLE:
5815 status.setEnabled(tabular.tabular_width.zero());
5816 status.setOnOff(tabular.tabular_valignment
5817 == Tabular::LYX_VALIGN_MIDDLE);
5819 case Tabular::TABULAR_VALIGN_BOTTOM:
5820 status.setEnabled(tabular.tabular_width.zero());
5821 status.setOnOff(tabular.tabular_valignment
5822 == Tabular::LYX_VALIGN_BOTTOM);
5825 case Tabular::LONGTABULAR_ALIGN_LEFT:
5826 status.setOnOff(tabular.longtabular_alignment
5827 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5829 case Tabular::LONGTABULAR_ALIGN_CENTER:
5830 status.setOnOff(tabular.longtabular_alignment
5831 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5833 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5834 status.setOnOff(tabular.longtabular_alignment
5835 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5838 case Tabular::UNSET_ROTATE_TABULAR:
5839 status.setOnOff(tabular.rotate == 0);
5842 case Tabular::TOGGLE_ROTATE_CELL:
5843 case Tabular::SET_ROTATE_CELL:
5844 status.setOnOff(!oneCellHasRotationState(false,
5845 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5848 case Tabular::UNSET_ROTATE_CELL:
5849 status.setOnOff(!oneCellHasRotationState(true,
5850 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5853 case Tabular::SET_USEBOX:
5854 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5857 // every row can only be one thing:
5858 // either a footer or header
5859 case Tabular::SET_LTFIRSTHEAD:
5860 status.setEnabled(sel_row_start == sel_row_end);
5861 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5864 case Tabular::UNSET_LTFIRSTHEAD:
5865 status.setEnabled(sel_row_start == sel_row_end);
5866 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5869 case Tabular::SET_LTHEAD:
5870 status.setEnabled(sel_row_start == sel_row_end);
5871 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5874 case Tabular::UNSET_LTHEAD:
5875 status.setEnabled(sel_row_start == sel_row_end);
5876 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5879 case Tabular::SET_LTFOOT:
5880 status.setEnabled(sel_row_start == sel_row_end);
5881 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5884 case Tabular::UNSET_LTFOOT:
5885 status.setEnabled(sel_row_start == sel_row_end);
5886 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5889 case Tabular::SET_LTLASTFOOT:
5890 status.setEnabled(sel_row_start == sel_row_end);
5891 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5894 case Tabular::UNSET_LTLASTFOOT:
5895 status.setEnabled(sel_row_start == sel_row_end);
5896 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5899 case Tabular::SET_LTNEWPAGE:
5900 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5902 case Tabular::UNSET_LTNEWPAGE:
5903 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5906 // only one row in head/firsthead/foot/lasthead can be the caption
5907 // and a multirow cannot be set as caption
5908 case Tabular::SET_LTCAPTION:
5909 status.setEnabled(sel_row_start == sel_row_end
5910 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5911 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5912 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5913 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5914 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5915 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5916 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5917 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5918 && !tabular.isMultiRow(sel_row_start));
5919 status.setOnOff(tabular.ltCaption(sel_row_start));
5922 case Tabular::UNSET_LTCAPTION:
5923 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5926 case Tabular::TOGGLE_LTCAPTION:
5927 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5928 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5929 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5930 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5931 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5932 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5933 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5934 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5935 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5936 status.setOnOff(tabular.ltCaption(sel_row_start));
5939 case Tabular::TOGGLE_BOOKTABS:
5940 case Tabular::SET_BOOKTABS:
5941 status.setOnOff(tabular.use_booktabs);
5944 case Tabular::UNSET_BOOKTABS:
5945 status.setOnOff(!tabular.use_booktabs);
5950 status.setEnabled(false);
5957 // function sets an object as defined in FuncStatus.h:
5958 // states OK, Unknown, Disabled, On, Off.
5959 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5960 FuncStatus & status) const
5962 switch (cmd.action()) {
5963 case LFUN_INSET_MODIFY:
5964 if (cmd.getArg(0) != "tabular")
5966 if (cmd.getArg(1) == "for-dialog") {
5967 // The dialog is asking the status of a command
5968 if (&cur.inset() != this)
5970 string action = cmd.getArg(2);
5971 string arg = cmd.getLongArg(3);
5972 return getFeatureStatus(cur, action, arg, status);
5974 // We always enable the lfun if it is coming from the dialog
5975 // because the dialog makes sure all the settings are valid,
5976 // even though the first argument might not be valid now.
5977 status.setEnabled(true);
5981 case LFUN_TABULAR_FEATURE: {
5982 if (&cur.inset() != this)
5984 string action = cmd.getArg(0);
5985 string arg = cmd.getLongArg(1);
5986 return getFeatureStatus(cur, action, arg, status);
5989 case LFUN_CAPTION_INSERT: {
5990 // caption is only allowed in caption cell of longtable
5991 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5992 status.setEnabled(false);
5995 // only standard caption is allowed
5996 string arg = cmd.getArg(0);
5997 if (!arg.empty() && arg != "Standard") {
5998 status.setEnabled(false);
6001 // check if there is already a caption
6002 bool have_caption = false;
6003 InsetTableCell itc = *tabular.cellInset(cur.idx());
6004 ParagraphList::const_iterator pit = itc.paragraphs().begin();
6005 ParagraphList::const_iterator pend = itc.paragraphs().end();
6006 for (; pit != pend; ++pit) {
6007 InsetList::const_iterator it = pit->insetList().begin();
6008 InsetList::const_iterator end = pit->insetList().end();
6009 for (; it != end; ++it) {
6010 if (it->inset->lyxCode() == CAPTION_CODE) {
6011 have_caption = true;
6016 status.setEnabled(!have_caption);
6020 // These are only enabled inside tabular
6021 case LFUN_CELL_BACKWARD:
6022 case LFUN_CELL_FORWARD:
6023 status.setEnabled(true);
6026 // disable these with multiple cells selected
6027 case LFUN_INSET_INSERT:
6028 case LFUN_TABULAR_INSERT:
6029 case LFUN_TABULAR_STYLE_INSERT:
6030 case LFUN_FLEX_INSERT:
6031 case LFUN_FLOAT_INSERT:
6032 case LFUN_FLOAT_WIDE_INSERT:
6033 case LFUN_FOOTNOTE_INSERT:
6034 case LFUN_MARGINALNOTE_INSERT:
6035 case LFUN_MATH_INSERT:
6036 case LFUN_MATH_MODE:
6037 case LFUN_MATH_MUTATE:
6038 case LFUN_MATH_DISPLAY:
6039 case LFUN_NOTE_INSERT:
6040 case LFUN_ARGUMENT_INSERT:
6041 case LFUN_BOX_INSERT:
6042 case LFUN_BRANCH_INSERT:
6043 case LFUN_PHANTOM_INSERT:
6044 case LFUN_WRAP_INSERT:
6045 case LFUN_PREVIEW_INSERT:
6046 case LFUN_ERT_INSERT: {
6047 if (cur.selIsMultiCell()) {
6048 status.setEnabled(false);
6051 return cell(cur.idx())->getStatus(cur, cmd, status);
6054 case LFUN_CHANGE_ACCEPT:
6055 case LFUN_CHANGE_REJECT: {
6056 if (cur.selIsMultiCell()) {
6059 getSelection(cur, rs, re, cs, ce);
6060 for (row_type r = rs; r <= re; ++r) {
6061 if (tabular.row_info[r].change.changed()) {
6062 status.setEnabled(true);
6065 for (col_type c = cs; c <= ce; ++c) {
6066 if (tabular.column_info[c].change.changed()) {
6067 status.setEnabled(true);
6073 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
6074 status.setEnabled(true);
6077 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
6078 status.setEnabled(true);
6082 return cell(cur.idx())->getStatus(cur, cmd, status);
6085 case LFUN_PARAGRAPH_BREAK:
6086 case LFUN_NEWLINE_INSERT:
6087 return cell(cur.idx())->getStatus(cur, cmd, status);
6089 case LFUN_NEWPAGE_INSERT:
6090 status.setEnabled(false);
6094 if (tabularStackDirty() && theClipboard().isInternal()) {
6095 if (cur.selIsMultiCell()) {
6098 getSelection(cur, rs, re, cs, ce);
6099 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
6100 && paste_tabular->nrows() == re - rs + 1)
6101 status.setEnabled(true);
6103 status.setEnabled(false);
6104 status.message(_("Selection size should match clipboard content."));
6107 status.setEnabled(true);
6110 return cell(cur.idx())->getStatus(cur, cmd, status);
6112 case LFUN_INSET_SETTINGS:
6113 // relay this lfun to Inset, not to the cell.
6114 return Inset::getStatus(cur, cmd, status);
6117 // we try to handle this event in the insets dispatch function.
6118 return cell(cur.idx())->getStatus(cur, cmd, status);
6124 int InsetTabular::rowFlags() const
6126 if (tabular.is_long_tabular) {
6127 switch (tabular.longtabular_alignment) {
6128 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
6129 return Display | AlignLeft;
6130 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
6132 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
6133 return Display | AlignRight;
6142 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
6144 tabular.latex(os, runparams);
6148 int InsetTabular::plaintext(odocstringstream & os,
6149 OutputParams const & runparams, size_t max_length) const
6151 os << '\n'; // output table on a new line
6152 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
6153 tabular.plaintext(os, runparams, dp, false, 0, max_length);
6154 return PLAINTEXT_NEWLINE;
6158 void InsetTabular::docbook(XMLStream & xs, OutputParams const & runparams) const
6160 tabular.docbook(xs, runparams);
6164 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6166 return tabular.xhtml(xs, rp);
6170 void InsetTabular::validate(LaTeXFeatures & features) const
6172 tabular.validate(features);
6173 features.useInsetLayout(getLayout());
6177 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6179 return tabular.cellInset(idx);
6183 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6185 return tabular.cellInset(idx);
6189 void InsetTabular::cursorPos(BufferView const & bv,
6190 CursorSlice const & sl, bool boundary, int & x, int & y) const
6192 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6194 // y offset correction
6195 y += cellYPos(sl.idx());
6196 y += tabular.textVOffset(sl.idx());
6197 y += tabular.offsetVAlignment();
6199 // x offset correction
6200 x += cellXPos(sl.idx());
6201 x += tabular.textHOffset(sl.idx());
6202 x += ADD_TO_TABULAR_WIDTH;
6206 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6210 Inset const & inset = *tabular.cellInset(cell);
6211 Point o = bv.coordCache().getInsets().xy(&inset);
6212 int const xbeg = o.x_ - tabular.textHOffset(cell);
6213 int const xend = xbeg + tabular.cellWidth(cell);
6214 row_type const row = tabular.cellRow(cell);
6215 int const ybeg = o.y_ - tabular.rowAscent(row)
6216 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6217 int const yend = ybeg + tabular.cellHeight(cell);
6229 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6230 // << " ybeg=" << ybeg << " yend=" << yend
6231 // << " xx=" << xx << " yy=" << yy
6232 // << " dist=" << xx + yy << endl;
6237 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6239 //lyxerr << "InsetTabular::editXY: " << this << endl;
6241 cur.idx() = getNearestCell(cur.bv(), x, y);
6242 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6246 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6248 cur.idx() = getNearestCell(cur.bv(), x, y);
6249 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6253 idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6255 idx_type idx_min = 0;
6256 int dist_min = numeric_limits<int>::max();
6257 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6258 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6259 int const d = dist(bv, i, x, y);
6270 int InsetTabular::cellYPos(idx_type const cell) const
6272 row_type row = tabular.cellRow(cell);
6274 for (row_type r = 0; r < row; ++r)
6275 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6276 + tabular.interRowSpace(r + 1);
6281 int InsetTabular::cellXPos(idx_type const cell) const
6283 col_type col = tabular.cellColumn(cell);
6285 for (col_type c = 0; c < col; ++c)
6286 lx += tabular.column_info[c].width;
6291 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6293 row_type const row = tabular.cellRow(cur.idx());
6294 col_type const col = tabular.cellColumn(cur.idx());
6296 if (isRightToLeft(cur)) {
6297 if (tabular.cellColumn(cur.idx()) == 0) {
6298 if (row == tabular.nrows() - 1)
6300 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6305 cur.idx() = tabular.getLastCellInRow(row - 1);
6307 cur.idx() = tabular.cellIndex(row, col - 1);
6310 if (tabular.isLastCell(cur.idx()))
6312 if (cur.idx() == tabular.getLastCellInRow(row))
6313 cur.idx() = tabular.cellIndex(row + 1, 0);
6315 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6316 cur.idx() = tabular.cellIndex(row, colnextcell);
6320 cur.boundary(false);
6322 if (cur.selIsMultiCell()) {
6323 cur.pit() = cur.lastpit();
6324 cur.pos() = cur.lastpos();
6331 // in visual mode, place cursor at extreme left or right
6333 switch(entry_from) {
6335 case ENTRY_DIRECTION_RIGHT:
6336 cur.posVisToRowExtremity(false /* !left */);
6338 case ENTRY_DIRECTION_LEFT:
6339 cur.posVisToRowExtremity(true /* left */);
6341 case ENTRY_DIRECTION_IGNORE:
6342 // nothing to do in this case
6346 cur.setCurrentFont();
6350 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6352 row_type const row = tabular.cellRow(cur.idx());
6353 col_type const col = tabular.cellColumn(cur.idx());
6355 if (isRightToLeft(cur)) {
6356 if (cur.idx() == tabular.getLastCellInRow(row)) {
6359 cur.idx() = tabular.getFirstCellInRow(row);
6360 cur.idx() = tabular.cellAbove(cur.idx());
6362 if (tabular.isLastCell(cur.idx()))
6364 if (cur.idx() == tabular.getLastCellInRow(row))
6365 cur.idx() = tabular.cellIndex(row + 1, 0);
6367 cur.idx() = tabular.cellIndex(row, col + 1);
6370 if (cur.idx() == 0) // first cell
6373 cur.idx() = tabular.getLastCellInRow(row - 1);
6375 cur.idx() = tabular.cellIndex(row, col - 1);
6378 if (cur.selIsMultiCell()) {
6379 cur.pit() = cur.lastpit();
6380 cur.pos() = cur.lastpos();
6384 cur.pit() = cur.lastpit();
6385 cur.pos() = cur.lastpos();
6387 // in visual mode, place cursor at extreme left or right
6389 switch(entry_from) {
6391 case ENTRY_DIRECTION_RIGHT:
6392 cur.posVisToRowExtremity(false /* !left */);
6394 case ENTRY_DIRECTION_LEFT:
6395 cur.posVisToRowExtremity(true /* left */);
6397 case ENTRY_DIRECTION_IGNORE:
6398 // nothing to do in this case
6402 cur.setCurrentFont();
6406 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6408 cur.recordUndoInset(this);
6410 istringstream is(argument);
6411 // limit the size of strings we read to avoid memory problems
6415 size_t safe_guard = 0;
6420 if (safe_guard > 1000) {
6421 LYXERR0("parameter max count reached!");
6425 Tabular::Feature action = Tabular::LAST_ACTION;
6428 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6429 if (s != tabularFeature[i].feature)
6431 action = tabularFeature[i].action;
6434 if (action == Tabular::LAST_ACTION) {
6435 LYXERR0("Feature not found " << s);
6439 if (tabularFeature[i].need_value)
6441 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6442 tabularFeatures(cur, action, val);
6447 static void checkLongtableSpecial(Tabular::ltType & ltt,
6448 string const & special, bool & flag)
6450 if (special == "dl_above") {
6453 } else if (special == "dl_below") {
6454 ltt.bottomDL = flag;
6456 } else if (special == "empty") {
6466 bool InsetTabular::oneCellHasRotationState(bool rotated,
6467 row_type row_start, row_type row_end,
6468 col_type col_start, col_type col_end) const
6470 for (row_type r = row_start; r <= row_end; ++r)
6471 for (col_type c = col_start; c <= col_end; ++c)
6473 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6476 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6483 void InsetTabular::tabularFeatures(Cursor & cur,
6484 Tabular::Feature feature, string const & value)
6486 col_type sel_col_start;
6487 col_type sel_col_end;
6488 row_type sel_row_start;
6489 row_type sel_row_end;
6490 bool setLines = false;
6491 bool toggle = false;
6492 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6493 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6497 case Tabular::M_ALIGN_LEFT:
6498 case Tabular::ALIGN_LEFT:
6499 setAlign = LYX_ALIGN_LEFT;
6502 case Tabular::M_ALIGN_RIGHT:
6503 case Tabular::ALIGN_RIGHT:
6504 setAlign = LYX_ALIGN_RIGHT;
6507 case Tabular::M_ALIGN_CENTER:
6508 case Tabular::ALIGN_CENTER:
6509 setAlign = LYX_ALIGN_CENTER;
6512 case Tabular::ALIGN_BLOCK:
6513 setAlign = LYX_ALIGN_BLOCK;
6516 case Tabular::ALIGN_DECIMAL:
6517 setAlign = LYX_ALIGN_DECIMAL;
6520 case Tabular::M_VALIGN_TOP:
6521 case Tabular::VALIGN_TOP:
6522 setVAlign = Tabular::LYX_VALIGN_TOP;
6525 case Tabular::M_VALIGN_BOTTOM:
6526 case Tabular::VALIGN_BOTTOM:
6527 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6530 case Tabular::M_VALIGN_MIDDLE:
6531 case Tabular::VALIGN_MIDDLE:
6532 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6539 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6540 row_type const row = tabular.cellRow(cur.idx());
6541 col_type const column = tabular.cellColumn(cur.idx());
6543 Tabular::ltType ltt;
6547 case Tabular::SET_TABULAR_WIDTH:
6548 tabular.setTabularWidth(Length(value));
6551 case Tabular::SET_PWIDTH: {
6552 Length const len(value);
6553 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6554 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6556 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6557 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6562 case Tabular::SET_MPWIDTH:
6563 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6566 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6567 bool const varwidth = value == "on";
6568 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6569 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6573 case Tabular::SET_MROFFSET:
6574 tabular.setMROffset(cur, cur.idx(), Length(value));
6577 case Tabular::SET_SPECIAL_COLUMN:
6578 case Tabular::SET_SPECIAL_MULTICOLUMN:
6579 if (value == "none")
6580 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6582 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6585 case Tabular::APPEND_ROW:
6586 // append the row into the tabular
6587 tabular.appendRow(row);
6590 case Tabular::APPEND_COLUMN:
6591 // append the column into the tabular
6592 tabular.appendColumn(column);
6593 cur.idx() = tabular.cellIndex(row, column);
6596 case Tabular::DELETE_ROW:
6597 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6598 for (col_type c = 0; c < tabular.ncols(); c++) {
6599 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6600 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6601 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6602 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6606 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6607 tabular.deleteRow(sel_row_start);
6608 if (sel_row_start >= tabular.nrows())
6610 cur.idx() = tabular.cellIndex(sel_row_start, column);
6613 cur.selection(false);
6616 case Tabular::DELETE_COLUMN:
6617 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6618 for (row_type r = 0; r < tabular.nrows(); r++)
6619 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6620 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6623 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6624 for (row_type r = 0; r < tabular.nrows(); r++)
6625 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6626 tabular.leftLine(tabular.cellIndex(r, 0)));
6629 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6630 tabular.deleteColumn(sel_col_start);
6631 if (sel_col_start >= tabular.ncols())
6633 cur.idx() = tabular.cellIndex(row, sel_col_start);
6636 cur.selection(false);
6639 case Tabular::COPY_ROW:
6640 tabular.copyRow(row);
6643 case Tabular::COPY_COLUMN:
6644 tabular.copyColumn(column);
6645 cur.idx() = tabular.cellIndex(row, column);
6648 case Tabular::MOVE_COLUMN_RIGHT:
6649 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::RIGHT);
6650 if (cur.selection()) {
6651 cur.selection(false);
6652 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start + 1);
6654 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end + 1);
6657 cur.idx() = tabular.cellIndex(row, column + 1);
6660 case Tabular::MOVE_COLUMN_LEFT:
6661 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::LEFT);
6662 if (cur.selection()) {
6663 cur.selection(false);
6664 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start - 1);
6666 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end - 1);
6669 cur.idx() = tabular.cellIndex(row, column - 1);
6672 case Tabular::MOVE_ROW_DOWN:
6673 tabular.moveRow(sel_row_start, sel_row_end, Tabular::DOWN);
6674 if (cur.selection()) {
6675 cur.selection(false);
6676 cur.idx() = tabular.cellIndex(sel_row_start + 1, sel_col_start);
6678 cur.idx() = tabular.cellIndex(sel_row_end + 1, sel_col_end);
6681 cur.idx() = tabular.cellIndex(row + 1, column);
6684 case Tabular::MOVE_ROW_UP:
6685 tabular.moveRow(sel_row_start, sel_row_end, Tabular::UP);
6686 if (cur.selection()) {
6687 cur.selection(false);
6688 cur.idx() = tabular.cellIndex(sel_row_start - 1, sel_col_start);
6690 cur.idx() = tabular.cellIndex(sel_row_end - 1, sel_col_end);
6693 cur.idx() = tabular.cellIndex(row - 1, column);
6696 case Tabular::SET_LINE_TOP:
6697 case Tabular::TOGGLE_LINE_TOP: {
6698 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6699 ? (value == "true") : !tabular.topLine(cur.idx());
6700 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6701 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6702 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6706 case Tabular::SET_LINE_BOTTOM:
6707 case Tabular::TOGGLE_LINE_BOTTOM: {
6708 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6709 ? (value == "true") : !tabular.bottomLine(cur.idx());
6710 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6711 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6712 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6716 case Tabular::SET_LTRIM_TOP:
6717 case Tabular::TOGGLE_LTRIM_TOP: {
6718 bool l = (feature == Tabular::SET_LTRIM_TOP)
6719 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6720 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6721 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6722 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6726 case Tabular::SET_RTRIM_TOP:
6727 case Tabular::TOGGLE_RTRIM_TOP: {
6728 bool l = (feature == Tabular::SET_RTRIM_TOP)
6729 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6730 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6731 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6732 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6736 case Tabular::SET_LTRIM_BOTTOM:
6737 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6738 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6739 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6740 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6741 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6742 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6746 case Tabular::SET_RTRIM_BOTTOM:
6747 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6748 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6749 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6750 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6751 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6752 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6756 case Tabular::SET_LINE_LEFT:
6757 case Tabular::TOGGLE_LINE_LEFT: {
6758 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6759 ? (value == "true") : !tabular.leftLine(cur.idx());
6760 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6761 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6762 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6766 case Tabular::SET_LINE_RIGHT:
6767 case Tabular::TOGGLE_LINE_RIGHT: {
6768 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6769 ? (value == "true") : !tabular.rightLine(cur.idx());
6770 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6771 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6772 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6776 case Tabular::M_ALIGN_LEFT:
6777 case Tabular::M_ALIGN_RIGHT:
6778 case Tabular::M_ALIGN_CENTER:
6779 case Tabular::ALIGN_LEFT:
6780 case Tabular::ALIGN_RIGHT:
6781 case Tabular::ALIGN_CENTER:
6782 case Tabular::ALIGN_BLOCK:
6783 case Tabular::ALIGN_DECIMAL:
6784 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6785 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6786 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6787 !tabular.getPWidth(c).zero());
6790 case Tabular::M_VALIGN_TOP:
6791 case Tabular::M_VALIGN_BOTTOM:
6792 case Tabular::M_VALIGN_MIDDLE:
6795 case Tabular::VALIGN_TOP:
6796 case Tabular::VALIGN_BOTTOM:
6797 case Tabular::VALIGN_MIDDLE:
6798 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6799 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6800 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6803 case Tabular::SET_MULTICOLUMN: {
6804 if (!cur.selection()) {
6805 // just multicol for one single cell
6806 // check whether we are completely in a multicol
6807 if (!tabular.isMultiColumn(cur.idx()))
6808 tabular.setMultiColumn(cur, cur.idx(), 1,
6809 tabular.rightLine(cur.idx()));
6812 // we have a selection so this means we just add all these
6813 // cells to form a multicolumn cell
6814 idx_type const s_start = cur.selBegin().idx();
6815 row_type const col_start = tabular.cellColumn(s_start);
6816 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6817 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6818 tabular.rightLine(cur.selEnd().idx()));
6821 cur.selection(false);
6825 case Tabular::UNSET_MULTICOLUMN: {
6826 if (!cur.selection()) {
6827 if (tabular.isMultiColumn(cur.idx()))
6828 tabular.unsetMultiColumn(cur.idx());
6833 case Tabular::MULTICOLUMN: {
6834 if (!cur.selection()) {
6835 if (tabular.isMultiColumn(cur.idx()))
6836 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6838 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6842 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6843 row_type const r = sel_row_start;
6844 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6845 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6848 // If the selection contains at least one singlecol cell
6849 // or multiple multicol cells,
6850 // we assume the user will merge is to a single multicol
6852 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6854 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6858 case Tabular::SET_MULTIROW: {
6859 if (!cur.selection()) {
6860 // just multirow for one single cell
6861 // check whether we are completely in a multirow
6862 if (!tabular.isMultiRow(cur.idx()))
6863 tabular.setMultiRow(cur, cur.idx(), 1,
6864 tabular.bottomLine(cur.idx()),
6865 tabular.getAlignment(cur.idx()));
6868 // we have a selection so this means we just add all this
6869 // cells to form a multirow cell
6870 idx_type const s_start = cur.selBegin().idx();
6871 row_type const row_start = tabular.cellRow(s_start);
6872 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6873 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6874 tabular.bottomLine(cur.selEnd().idx()),
6875 tabular.getAlignment(cur.selEnd().idx()));
6878 cur.selection(false);
6882 case Tabular::UNSET_MULTIROW: {
6883 if (!cur.selection()) {
6884 if (tabular.isMultiRow(cur.idx()))
6885 tabular.unsetMultiRow(cur.idx());
6890 case Tabular::MULTIROW: {
6891 if (!cur.selection()) {
6892 if (tabular.isMultiRow(cur.idx()))
6893 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6895 tabularFeatures(cur, Tabular::SET_MULTIROW);
6899 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6900 col_type const c = sel_col_start;
6901 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6902 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6905 // If the selection contains at least one singlerow cell
6906 // or multiple multirow cells,
6907 // we assume the user will merge is to a single multirow
6909 tabularFeatures(cur, Tabular::SET_MULTIROW);
6911 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6915 case Tabular::TOGGLE_INNER_LINES:
6918 case Tabular::SET_INNER_LINES:
6920 setLines = !tabular.innerBorders(sel_row_start, sel_row_end,
6921 sel_col_start, sel_col_end);
6924 tabular.setLines(sel_row_start, sel_row_end,
6925 sel_col_start, sel_col_end,
6929 case Tabular::TOGGLE_ALL_LINES:
6932 case Tabular::SET_ALL_LINES:
6934 setLines = !tabular.innerBorders(sel_row_start, sel_row_end,
6935 sel_col_start, sel_col_end)
6936 || !tabular.outsideBorders(sel_row_start, sel_row_end,
6937 sel_col_start, sel_col_end);
6941 case Tabular::UNSET_ALL_LINES:
6942 tabular.setLines(sel_row_start, sel_row_end,
6943 sel_col_start, sel_col_end,
6947 case Tabular::RESET_FORMAL_DEFAULT:
6948 for (row_type r = 0; r < tabular.nrows(); ++r) {
6949 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6950 for (col_type c = 0; c < tabular.ncols(); ++c) {
6951 idx_type const cell = tabular.cellIndex(r, c);
6952 tabular.setTopLine(cell, head_or_foot);
6953 tabular.setBottomLine(cell, head_or_foot);
6958 case Tabular::TOGGLE_BORDER_LINES:
6961 case Tabular::SET_BORDER_LINES: {
6962 bool const border = toggle &&
6963 tabular.outsideBorders(sel_row_start, sel_row_end,
6964 sel_col_start, sel_col_end)
6966 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6967 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), border);
6968 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), border);
6970 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6971 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), border);
6972 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), border);
6977 case Tabular::TOGGLE_LONGTABULAR:
6978 if (tabular.is_long_tabular)
6979 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6981 tabular.is_long_tabular = true;
6984 case Tabular::SET_LONGTABULAR:
6985 tabular.is_long_tabular = true;
6988 case Tabular::UNSET_LONGTABULAR:
6989 for (row_type r = 0; r < tabular.nrows(); ++r) {
6990 if (tabular.ltCaption(r)) {
6991 cur.idx() = tabular.cellIndex(r, 0);
6994 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6997 tabular.is_long_tabular = false;
7000 case Tabular::SET_ROTATE_TABULAR:
7001 tabular.rotate = convert<int>(value);
7004 case Tabular::UNSET_ROTATE_TABULAR:
7008 case Tabular::TOGGLE_ROTATE_TABULAR:
7009 // when pressing the rotate button we default to 90° rotation
7010 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
7013 case Tabular::TABULAR_VALIGN_TOP:
7014 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
7017 case Tabular::TABULAR_VALIGN_MIDDLE:
7018 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
7021 case Tabular::TABULAR_VALIGN_BOTTOM:
7022 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
7025 case Tabular::LONGTABULAR_ALIGN_LEFT:
7026 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
7029 case Tabular::LONGTABULAR_ALIGN_CENTER:
7030 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
7033 case Tabular::LONGTABULAR_ALIGN_RIGHT:
7034 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
7037 case Tabular::SET_ROTATE_CELL:
7038 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7039 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7040 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
7043 case Tabular::UNSET_ROTATE_CELL:
7044 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7045 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7046 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7049 case Tabular::TOGGLE_ROTATE_CELL:
7051 bool oneNotRotated = oneCellHasRotationState(false,
7052 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
7054 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7055 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
7056 // when pressing the rotate cell button we default to 90° rotation
7058 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
7060 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7065 case Tabular::SET_USEBOX: {
7066 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
7067 if (val == tabular.getUsebox(cur.idx()))
7068 val = Tabular::BOX_NONE;
7069 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7070 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7071 tabular.setUsebox(tabular.cellIndex(r, c), val);
7075 case Tabular::UNSET_LTFIRSTHEAD:
7078 case Tabular::SET_LTFIRSTHEAD:
7079 tabular.getRowOfLTFirstHead(row, ltt);
7080 checkLongtableSpecial(ltt, value, flag);
7081 tabular.setLTHead(row, flag, ltt, true);
7084 case Tabular::UNSET_LTHEAD:
7087 case Tabular::SET_LTHEAD:
7088 tabular.getRowOfLTHead(row, ltt);
7089 checkLongtableSpecial(ltt, value, flag);
7090 tabular.setLTHead(row, flag, ltt, false);
7093 case Tabular::UNSET_LTFOOT:
7096 case Tabular::SET_LTFOOT:
7097 tabular.getRowOfLTFoot(row, ltt);
7098 checkLongtableSpecial(ltt, value, flag);
7099 tabular.setLTFoot(row, flag, ltt, false);
7102 case Tabular::UNSET_LTLASTFOOT:
7105 case Tabular::SET_LTLASTFOOT:
7106 tabular.getRowOfLTLastFoot(row, ltt);
7107 checkLongtableSpecial(ltt, value, flag);
7108 tabular.setLTFoot(row, flag, ltt, true);
7111 case Tabular::UNSET_LTNEWPAGE:
7114 case Tabular::SET_LTNEWPAGE:
7115 tabular.setLTNewPage(row, flag);
7118 case Tabular::SET_LTCAPTION: {
7119 if (tabular.ltCaption(row))
7121 cur.idx() = tabular.setLTCaption(cur, row, true);
7124 cur.selection(false);
7125 // If a row is set as caption, then also insert
7126 // a caption. Otherwise the LaTeX output is broken.
7127 // Select cell if it is non-empty
7128 if (cur.lastpos() > 0 || cur.lastpit() > 0)
7129 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
7130 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
7134 case Tabular::UNSET_LTCAPTION: {
7135 if (!tabular.ltCaption(row))
7137 cur.idx() = tabular.setLTCaption(cur, row, false);
7140 cur.selection(false);
7141 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
7142 if (lyx::getStatus(fr).enabled())
7147 case Tabular::TOGGLE_LTCAPTION: {
7148 if (tabular.ltCaption(row))
7149 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
7151 tabularFeatures(cur, Tabular::SET_LTCAPTION);
7155 case Tabular::TOGGLE_BOOKTABS:
7156 tabular.use_booktabs = !tabular.use_booktabs;
7159 case Tabular::SET_BOOKTABS:
7160 tabular.use_booktabs = true;
7163 case Tabular::UNSET_BOOKTABS:
7164 tabular.use_booktabs = false;
7167 case Tabular::SET_TOP_SPACE: {
7169 if (value == "default")
7170 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7171 tabular.row_info[r].top_space_default = true;
7172 else if (value == "none")
7173 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7174 tabular.row_info[r].top_space_default = false;
7175 tabular.row_info[r].top_space = len;
7177 else if (isValidLength(value, &len))
7178 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7179 tabular.row_info[r].top_space_default = false;
7180 tabular.row_info[r].top_space = len;
7185 case Tabular::SET_BOTTOM_SPACE: {
7187 if (value == "default")
7188 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7189 tabular.row_info[r].bottom_space_default = true;
7190 else if (value == "none")
7191 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7192 tabular.row_info[r].bottom_space_default = false;
7193 tabular.row_info[r].bottom_space = len;
7195 else if (isValidLength(value, &len))
7196 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7197 tabular.row_info[r].bottom_space_default = false;
7198 tabular.row_info[r].bottom_space = len;
7203 case Tabular::SET_INTERLINE_SPACE: {
7205 if (value == "default")
7206 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7207 tabular.row_info[r].interline_space_default = true;
7208 else if (value == "none")
7209 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7210 tabular.row_info[r].interline_space_default = false;
7211 tabular.row_info[r].interline_space = len;
7213 else if (isValidLength(value, &len))
7214 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7215 tabular.row_info[r].interline_space_default = false;
7216 tabular.row_info[r].interline_space = len;
7221 case Tabular::SET_DECIMAL_POINT:
7222 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7223 tabular.column_info[c].decimal_point = from_utf8(value);
7226 // dummy stuff just to avoid warnings
7227 case Tabular::LAST_ACTION:
7233 bool InsetTabular::copySelection(Cursor & cur)
7235 if (!cur.selection())
7240 getSelection(cur, rs, re, cs, ce);
7242 paste_tabular.reset(new Tabular(tabular));
7244 for (row_type r = 0; r < rs; ++r)
7245 paste_tabular->deleteRow(0, true);
7247 row_type const rows = re - rs + 1;
7248 while (paste_tabular->nrows() > rows)
7249 paste_tabular->deleteRow(rows, true);
7251 for (col_type c = 0; c < cs; ++c)
7252 paste_tabular->deleteColumn(0, true);
7254 col_type const columns = ce - cs + 1;
7255 while (paste_tabular->ncols() > columns)
7256 paste_tabular->deleteColumn(columns, true);
7258 paste_tabular->setBuffer(tabular.buffer());
7260 odocstringstream os;
7261 OutputParams const runparams(nullptr);
7262 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7263 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7264 cap::copySelection(cur, os.str());
7266 // mark tabular stack dirty
7267 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7268 // when we (hopefully) have a one-for-all paste mechanism.
7269 // This must be called after cap::copySelection.
7270 dirtyTabularStack(true);
7276 bool InsetTabular::pasteClipboard(Cursor & cur)
7280 col_type actcol = tabular.cellColumn(cur.idx());
7281 row_type actrow = tabular.cellRow(cur.idx());
7283 if (cur.selIsMultiCell()) {
7286 getSelection(cur, actrow, re, actcol, ce);
7289 col_type const oldncols = tabular.ncols();
7290 for (row_type r1 = 0, r2 = actrow; r1 < paste_tabular->nrows(); ++r1, ++r2) {
7291 // Append rows if needed
7292 if (r2 == tabular.nrows())
7293 tabular.insertRow(r2 - 1, false);
7294 for (col_type c1 = 0, c2 = actcol; c1 < paste_tabular->ncols(); ++c1, ++c2) {
7295 // Append columns if needed
7296 if (c2 == tabular.ncols())
7297 tabular.insertColumn(c2 - 1, false);
7298 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7299 tabular.isPartOfMultiColumn(r2, c2))
7301 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7305 if (tabular.isPartOfMultiColumn(r2, c2)) {
7309 shared_ptr<InsetTableCell> inset(
7310 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7311 tabular.setCellInset(r2, c2, inset);
7312 // FIXME?: why do we need to do this explicitly? (EL)
7313 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7315 if (lyxrc.ct_markup_copied) {
7316 // Only change to inserted if ct is active,
7317 // otherwise leave markup as is
7318 if (buffer().params().track_changes)
7319 inset->setChange(Change(Change::INSERTED));
7321 // Resolve all markup to inserted or unchanged
7322 inset->setChange(Change(buffer().params().track_changes ?
7323 Change::INSERTED : Change::UNCHANGED));
7328 // amend cursor position if cols have been appended
7329 cur.idx() += actrow * (tabular.ncols() - oldncols);
7334 void InsetTabular::cutSelection(Cursor & cur)
7336 if (!cur.selection())
7341 getSelection(cur, rs, re, cs, ce);
7342 for (row_type r = rs; r <= re; ++r) {
7343 for (col_type c = cs; c <= ce; ++c) {
7344 shared_ptr<InsetTableCell> t
7345 = cell(tabular.cellIndex(r, c));
7346 if (buffer().params().track_changes)
7347 // FIXME: Change tracking (MG)
7348 t->setChange(Change(Change::DELETED));
7354 // cursor position might be invalid now
7355 if (cur.pit() > cur.lastpit())
7356 cur.pit() = cur.lastpit();
7357 if (cur.pos() > cur.lastpos())
7358 cur.pos() = cur.lastpos();
7359 cur.clearSelection();
7363 bool InsetTabular::isRightToLeft(Cursor & cur) const
7365 // LASSERT: It might be better to abandon this Buffer.
7366 LASSERT(cur.depth() > 1, return false);
7367 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7368 pos_type const parentpos = cur[cur.depth() - 2].pos();
7369 return parentpar.getFontSettings(buffer().params(),
7370 parentpos).language()->rightToLeft();
7374 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7377 LASSERT(stidx <= enidx, return docstring());
7379 col_type const col1 = tabular.cellColumn(stidx);
7380 col_type const col2 = tabular.cellColumn(enidx);
7381 row_type const row1 = tabular.cellRow(stidx);
7382 row_type const row2 = tabular.cellRow(enidx);
7384 for (col_type col = col1; col <= col2; col++)
7385 for (row_type row = row1; row <= row2; row++) {
7390 retval += tabular.cellInset(row, col)->asString(intoInsets);
7396 ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
7398 LASSERT(stidx <= enidx, return ParagraphList());
7399 ParagraphList retval;
7400 col_type const col1 = tabular.cellColumn(stidx);
7401 col_type const col2 = tabular.cellColumn(enidx);
7402 row_type const row1 = tabular.cellRow(stidx);
7403 row_type const row2 = tabular.cellRow(enidx);
7404 for (col_type col = col1; col <= col2; col++)
7405 for (row_type row = row1; row <= row2; row++)
7406 for (auto const & par : tabular.cellInset(row, col)->paragraphs())
7407 retval.push_back(par);
7412 void InsetTabular::getSelection(Cursor & cur,
7413 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7415 CursorSlice const & beg = cur.selBegin();
7416 CursorSlice const & end = cur.selEnd();
7417 cs = tabular.cellColumn(beg.idx());
7418 ce = tabular.cellColumn(end.idx());
7422 rs = tabular.cellRow(beg.idx());
7423 re = tabular.cellRow(end.idx());
7429 Text * InsetTabular::getText(int idx) const
7431 return size_t(idx) < nargs() ? cell(idx)->getText(0) : nullptr;
7435 bool InsetTabular::isChanged() const
7437 for (idx_type idx = 0; idx < nargs(); ++idx) {
7438 if (cell(idx)->isChanged())
7440 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7442 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7449 void InsetTabular::setChange(Change const & change)
7451 for (idx_type idx = 0; idx < nargs(); ++idx)
7452 cell(idx)->setChange(change);
7456 void InsetTabular::acceptChanges()
7458 for (idx_type idx = 0; idx < nargs(); ++idx)
7459 cell(idx)->acceptChanges();
7460 for (row_type row = 0; row < tabular.nrows(); ++row) {
7461 if (tabular.row_info[row].change.inserted())
7462 tabular.row_info[row].change.setUnchanged();
7463 else if (tabular.row_info[row].change.deleted())
7464 tabular.deleteRow(row, true);
7466 for (col_type col = 0; col < tabular.ncols(); ++col) {
7467 if (tabular.column_info[col].change.inserted())
7468 tabular.column_info[col].change.setUnchanged();
7469 else if (tabular.column_info[col].change.deleted())
7470 tabular.deleteColumn(col, true);
7472 tabular.updateIndexes();
7476 void InsetTabular::rejectChanges()
7478 for (idx_type idx = 0; idx < nargs(); ++idx)
7479 cell(idx)->rejectChanges();
7480 for (row_type row = 0; row < tabular.nrows(); ++row) {
7481 if (tabular.row_info[row].change.deleted())
7482 tabular.row_info[row].change.setUnchanged();
7483 else if (tabular.row_info[row].change.inserted())
7484 tabular.deleteRow(row, true);
7486 for (col_type col = 0; col < tabular.ncols(); ++col) {
7487 if (tabular.column_info[col].change.deleted())
7488 tabular.column_info[col].change.setUnchanged();
7489 else if (tabular.column_info[col].change.inserted())
7490 tabular.deleteColumn(col, true);
7492 tabular.updateIndexes();
7496 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7498 return tabular.getPWidth(cell).zero();
7502 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7505 if (buf.length() <= 0)
7510 col_type maxCols = 1;
7511 size_t const len = buf.length();
7515 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7523 maxCols = max(cols, maxCols);
7529 maxCols = max(cols, maxCols);
7535 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7536 loctab = paste_tabular.get();
7537 dirtyTabularStack(true);
7540 cell = bv.cursor().idx();
7541 ocol = tabular.cellColumn(cell);
7542 row = tabular.cellRow(cell);
7546 idx_type cells = loctab->numberofcells;
7549 rows = loctab->nrows();
7550 col_type columns = loctab->ncols();
7553 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7559 // append column if necessary
7560 if (cols == columns) {
7561 loctab->appendColumn(cols - 1);
7562 columns = loctab->ncols();
7563 cells = loctab->numberofcells;
7566 if (cols < columns) {
7567 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7568 Font const font = bv.textMetrics(&inset->text()).
7570 inset->setText(buf.substr(op, p - op), font,
7571 buffer().params().track_changes);
7577 // we can only set this if we are not too far right
7578 if (cols < columns) {
7579 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7580 Font const font = bv.textMetrics(&inset->text()).
7582 inset->setText(buf.substr(op, p - op), font,
7583 buffer().params().track_changes);
7587 // append row if necessary
7588 if (row == rows && p < len - 1) {
7589 loctab->appendRow(row - 1);
7590 rows = loctab->nrows();
7591 cells = loctab->numberofcells;
7594 cell = loctab->cellIndex(row, cols);
7600 // check for the last cell if there is no trailing '\n'
7601 if (cell < cells && op < len) {
7602 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7603 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7604 inset->setText(buf.substr(op, len - op), font,
7605 buffer().params().track_changes);
7611 void InsetTabular::addPreview(DocIterator const & inset_pos,
7612 PreviewLoader & loader) const
7614 DocIterator cell_pos = inset_pos;
7616 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7617 for (row_type r = 0; r < tabular.nrows(); ++r) {
7618 for (col_type c = 0; c < tabular.ncols(); ++c) {
7619 cell_pos.top().idx() = tabular.cellIndex(r, c);
7620 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7626 bool InsetTabular::completionSupported(Cursor const & cur) const
7628 Cursor const & bvCur = cur.bv().cursor();
7629 if (&bvCur.inset() != this)
7631 return cur.text()->completionSupported(cur);
7635 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7637 return completionSupported(cur);
7641 bool InsetTabular::automaticInlineCompletion() const
7643 return lyxrc.completion_inline_text;
7647 bool InsetTabular::automaticPopupCompletion() const
7649 return lyxrc.completion_popup_text;
7653 bool InsetTabular::showCompletionCursor() const
7655 return lyxrc.completion_cursor_text &&
7656 (lyxrc.completion_inline_text || lyxrc.completion_popup_text);
7660 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7662 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : nullptr;
7666 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7668 if (!completionSupported(cur))
7670 return cur.text()->completionPrefix(cur);
7674 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7676 if (!completionSupported(cur))
7679 return cur.text()->insertCompletion(cur, s, finished);
7683 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7684 Dimension & dim) const
7686 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7687 tm.completionPosAndDim(cur, x, y, dim);
7691 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7693 istringstream data(in);
7695 lex.setStream(data);
7702 if (!lex || token != "tabular") {
7703 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7707 // This is part of the inset proper that is usually swallowed
7708 // by Buffer::readInset
7710 if (!lex || token != "Tabular") {
7711 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7719 string InsetTabular::params2string(InsetTabular const & inset)
7722 data << "tabular" << ' ';
7724 data << "\\end_inset\n";
7729 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7731 for (col_type c = 0; c < tabular.ncols(); ++c) {
7732 for (row_type r = 0; r < tabular.nrows(); ++r) {
7733 if (!tabular.isPartOfMultiColumn(r,c) &&
7734 !tabular.isPartOfMultiRow(r,c))
7737 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7738 ParagraphList::iterator it = parlist.begin();
7739 ParagraphList::iterator const en = parlist.end();
7740 for (; it != en; ++it)
7741 it->setLayout(dc.plainLayout());