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::RESET_FORMAL_DEFAULT, "reset-formal-default", false },
168 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
169 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
170 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
171 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
172 { Tabular::SET_PWIDTH, "set-pwidth", true },
173 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
174 { Tabular::TOGGLE_VARWIDTH_COLUMN, "toggle-varwidth-column", true },
175 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
176 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
177 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
178 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
179 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
180 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
181 { Tabular::SET_USEBOX, "set-usebox", true },
182 { Tabular::SET_LTHEAD, "set-lthead", true },
183 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
184 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
185 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
186 { Tabular::SET_LTFOOT, "set-ltfoot", true },
187 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
188 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
189 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
190 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
191 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
192 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
193 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
194 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
195 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
196 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
197 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
198 { Tabular::SET_BOOKTABS, "set-booktabs", false },
199 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
200 { Tabular::SET_TOP_SPACE, "set-top-space", true },
201 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
202 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
203 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
204 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
205 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
206 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
207 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
208 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
209 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
210 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
211 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
212 { Tabular::SET_INNER_LINES, "set-inner-lines", false },
213 { Tabular::LAST_ACTION, "", false }
217 string const tostr(LyXAlignment const & num)
222 case LYX_ALIGN_BLOCK:
226 case LYX_ALIGN_CENTER:
228 case LYX_ALIGN_RIGHT:
230 case LYX_ALIGN_LAYOUT:
232 case LYX_ALIGN_SPECIAL:
234 case LYX_ALIGN_DECIMAL:
241 string const tostr(Tabular::HAlignment const & num)
244 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
246 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
248 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
255 string const tostr(Tabular::VAlignment const & num)
258 case Tabular::LYX_VALIGN_TOP:
260 case Tabular::LYX_VALIGN_MIDDLE:
262 case Tabular::LYX_VALIGN_BOTTOM:
269 string const tostr(Tabular::BoxType const & num)
272 case Tabular::BOX_NONE:
274 case Tabular::BOX_PARBOX:
276 case Tabular::BOX_MINIPAGE:
278 case Tabular::BOX_VARWIDTH:
285 // I would have liked a fromstr template a lot better. (Lgb)
286 bool string2type(string const & str, LyXAlignment & num)
289 num = LYX_ALIGN_NONE;
290 else if (str == "block")
291 num = LYX_ALIGN_BLOCK;
292 else if (str == "left")
293 num = LYX_ALIGN_LEFT;
294 else if (str == "center")
295 num = LYX_ALIGN_CENTER;
296 else if (str == "right")
297 num = LYX_ALIGN_RIGHT;
298 else if (str == "decimal")
299 num = LYX_ALIGN_DECIMAL;
306 bool string2type(string const & str, Tabular::HAlignment & num)
309 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
310 else if (str == "center" )
311 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
312 else if (str == "right")
313 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
320 bool string2type(string const & str, Tabular::VAlignment & num)
323 num = Tabular::LYX_VALIGN_TOP;
324 else if (str == "middle" )
325 num = Tabular::LYX_VALIGN_MIDDLE;
326 else if (str == "bottom")
327 num = Tabular::LYX_VALIGN_BOTTOM;
334 bool string2type(string const & str, Tabular::BoxType & num)
337 num = Tabular::BOX_NONE;
338 else if (str == "parbox")
339 num = Tabular::BOX_PARBOX;
340 else if (str == "minipage")
341 num = Tabular::BOX_MINIPAGE;
342 else if (str == "varwidth")
343 num = Tabular::BOX_VARWIDTH;
350 bool string2type(string const & str, bool & num)
354 else if (str == "false")
362 bool getTokenValue(string const & str, char const * token, string & ret)
365 size_t token_length = strlen(token);
366 size_t pos = str.find(token);
368 if (pos == string::npos || pos + token_length + 1 >= str.length()
369 || str[pos + token_length] != '=')
371 pos += token_length + 1;
373 if (ch != '"' && ch != '\'') { // only read till next space
377 while (pos < str.length() - 1 && str[++pos] != ch)
384 bool getTokenValue(string const & str, char const * token, docstring & ret)
387 bool const success = getTokenValue(str, token, tmp);
388 ret = from_utf8(tmp);
393 bool getTokenValue(string const & str, char const * token, int & num)
397 if (!getTokenValue(str, token, tmp))
399 num = convert<int>(tmp);
404 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
407 return getTokenValue(str, token, tmp) && string2type(tmp, num);
411 bool getTokenValue(string const & str, char const * token,
412 Tabular::HAlignment & num)
415 return getTokenValue(str, token, tmp) && string2type(tmp, num);
419 bool getTokenValue(string const & str, char const * token,
420 Tabular::VAlignment & num)
423 return getTokenValue(str, token, tmp) && string2type(tmp, num);
427 bool getTokenValue(string const & str, char const * token,
428 Tabular::BoxType & num)
431 return getTokenValue(str, token, tmp) && string2type(tmp, num);
435 bool getTokenValue(string const & str, char const * token, bool & flag)
437 // set the flag always to false as this should be the default for bools
438 // not in the file-format.
441 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
445 bool getTokenValue(string const & str, char const * token, Length & len)
447 // set the length to be zero() as default as this it should be if not
448 // in the file format.
451 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
455 bool getTokenValue(string const & str, char const * token, Change & change, BufferParams & bp)
457 // set the change to be Change() as default as this it should be if not
458 // in the file format.
461 if (getTokenValue(str, token, tmp)) {
462 vector<string> const changedata = getVectorFromString(tmp, " ");
463 if (changedata.size() != 3) {
464 Alert::warning(_("Change tracking data incomplete"),
465 _("Change tracking information for tabular row/column "
466 "is incomplete. I will ignore this."));
469 BufferParams::AuthorMap const & am = bp.author_map_;
470 int aid = convert<int>(changedata[1]);
471 if (am.find(aid) == am.end()) {
472 // FIXME Use ErrorList
473 Alert::warning(_("Change tracking author index missing"),
474 bformat(_("A change tracking author information for index "
475 "%1$d is missing. This can happen after a wrong "
476 "merge by a version control system. In this case, "
477 "either fix the merge, or have this information "
478 "missing until the corresponding tracked changes "
479 "are merged or this user edits the file again.\n"),
481 bp.addAuthor(Author(aid));
483 istringstream is(changedata[2]);
486 if (changedata[0] == "inserted") {
487 change = Change(Change::INSERTED, am.find(aid)->second, ct);
489 } else if (changedata[0] == "deleted") {
490 change = Change(Change::DELETED, am.find(aid)->second, ct);
498 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
503 if (!getTokenValue(str, token, tmp))
505 if (tmp == "default") {
509 return isValidLength(tmp, &len);
513 void l_getline(istream & is, string & str)
516 while (str.empty()) {
518 if (!str.empty() && str[str.length() - 1] == '\r')
519 str.erase(str.length() - 1);
524 string const write_attribute(string const & name, T const & t)
526 string const s = tostr(t);
527 return s.empty() ? s : " " + name + "=\"" + s + "\"";
531 string const write_attribute(string const & name, string const & t)
533 return t.empty() ? t : " " + name + "=\"" + t + "\"";
538 string const write_attribute(string const & name, docstring const & t)
540 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
545 string const write_attribute(string const & name, bool const & b)
547 // we write only true attribute values so we remove a bit of the
548 // file format bloat for tabulars.
549 return b ? write_attribute(name, convert<string>(b)) : string();
554 string const write_attribute(string const & name, int const & i)
556 // we write only true attribute values so we remove a bit of the
557 // file format bloat for tabulars.
558 return i ? write_attribute(name, convert<string>(i)) : string();
563 string const write_attribute(string const & name, idx_type const & i)
565 // we write only true attribute values so we remove a bit of the
566 // file format bloat for tabulars.
567 return i ? write_attribute(name, convert<string>(i)) : string();
572 string const write_attribute(string const & name, Length const & value)
574 // we write only the value if we really have one same reason as above.
575 return value.zero() ? string() : write_attribute(name, value.asString());
578 string const write_attribute(string const & name, Change const & change, BufferParams const & bp)
580 odocstringstream ods;
581 if (change.inserted())
582 ods << from_ascii("inserted");
583 else if (change.deleted())
584 ods << from_ascii("deleted");
585 if (change.changed()) {
586 ods << " " << bp.authors().get(change.author).bufferId()
587 << " " << change.changetime;
588 return write_attribute(name, ods.str());
596 string const featureAsString(Tabular::Feature action)
598 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
599 if (tabularFeature[i].action == action)
600 return tabularFeature[i].feature;
606 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
608 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
609 for (; dit; dit.forwardChar())
610 if (dit.inTexted() && dit.depth() == 1
611 && dit.paragraph().find(align_d, false, false, dit.pos()))
618 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
620 InsetTableCell tail = head;
621 DocIterator const dit = separatorPos(&head, align_d);
622 hassep = static_cast<bool>(dit);
624 pos_type const psize = head.paragraphs().front().size();
625 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
626 tail.paragraphs().front().eraseChars(0,
627 dit.pos() < psize ? dit.pos() + 1 : psize, false);
634 /////////////////////////////////////////////////////////////////////
638 /////////////////////////////////////////////////////////////////////
641 Tabular::CellData::CellData(Buffer * buf)
644 multicolumn(Tabular::CELL_NORMAL),
645 multirow(Tabular::CELL_NORMAL),
646 alignment(LYX_ALIGN_CENTER),
647 valignment(LYX_VALIGN_TOP),
655 top_line_rtrimmed(false),
656 top_line_ltrimmed(false),
657 bottom_line_rtrimmed(false),
658 bottom_line_ltrimmed(false),
661 inset(new InsetTableCell(buf))
663 inset->setBuffer(*buf);
667 Tabular::CellData::CellData(CellData const & cs)
670 multicolumn(cs.multicolumn),
671 multirow(cs.multirow),
672 mroffset(cs.mroffset),
673 alignment(cs.alignment),
674 valignment(cs.valignment),
675 decimal_hoffset(cs.decimal_hoffset),
676 decimal_width(cs.decimal_width),
678 top_line(cs.top_line),
679 bottom_line(cs.bottom_line),
680 left_line(cs.left_line),
681 right_line(cs.right_line),
682 top_line_rtrimmed(cs.top_line_rtrimmed),
683 top_line_ltrimmed(cs.top_line_ltrimmed),
684 bottom_line_rtrimmed(cs.bottom_line_rtrimmed),
685 bottom_line_ltrimmed(cs.bottom_line_ltrimmed),
688 align_special(cs.align_special),
690 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
694 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
700 multicolumn = cs.multicolumn;
701 multirow = cs.multirow;
702 mroffset = cs.mroffset;
703 alignment = cs.alignment;
704 valignment = cs.valignment;
705 decimal_hoffset = cs.decimal_hoffset;
706 decimal_width = cs.decimal_width;
707 voffset = cs.voffset;
708 top_line = cs.top_line;
709 bottom_line = cs.bottom_line;
710 left_line = cs.left_line;
711 right_line = cs.right_line;
712 top_line_rtrimmed = cs.top_line_rtrimmed;
713 top_line_ltrimmed = cs.top_line_ltrimmed;
714 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
715 bottom_line_ltrimmed = cs.bottom_line_ltrimmed;
718 align_special = cs.align_special;
719 p_width = cs.p_width;
720 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
724 Tabular::RowData::RowData()
727 top_space_default(false),
728 bottom_space_default(false),
729 interline_space_default(false),
740 Tabular::ColumnData::ColumnData()
741 : alignment(LYX_ALIGN_CENTER),
742 valignment(LYX_VALIGN_TOP),
750 Tabular::ltType::ltType()
751 : set(false), topDL(false),
757 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
759 init(buffer, rows_arg, columns_arg);
763 void Tabular::setBuffer(Buffer & buffer)
766 for (row_type i = 0; i < nrows(); ++i)
767 for (col_type j = 0; j < ncols(); ++j)
768 cell_info[i][j].inset->setBuffer(*buffer_);
772 // activates all lines and sets all widths to 0
773 void Tabular::init(Buffer * buf, row_type rows_arg,
774 col_type columns_arg)
777 row_info = row_vector(rows_arg);
778 column_info = column_vector(columns_arg);
779 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
780 row_info.reserve(10);
781 column_info.reserve(10);
782 cell_info.reserve(100);
784 is_long_tabular = false;
785 tabular_valignment = LYX_VALIGN_MIDDLE;
786 tabular_width = Length();
787 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
789 use_booktabs = false;
790 // set silly default lines
791 for (row_type r = 0; r < nrows(); ++r)
792 for (col_type c = 0; c < ncols(); ++c) {
793 cell_info[r][c].inset->setBuffer(*buffer_);
794 cell_info[r][c].top_line = true;
795 cell_info[r][c].left_line = true;
796 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
797 cell_info[r][c].right_line = c == ncols() - 1;
802 void Tabular::deleteRow(row_type const row, bool const force)
804 // Not allowed to delete last row
808 bool const ct = force ? false : buffer().params().track_changes;
810 for (col_type c = 0; c < ncols(); ++c) {
811 // Care about multirow cells
812 if (row + 1 < nrows() &&
813 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
814 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
815 cell_info[row + 1][c] = cell_info[row][c];
819 row_info[row].change.setDeleted();
821 row_info.erase(row_info.begin() + row);
822 cell_info.erase(cell_info.begin() + row);
828 void Tabular::copyRow(row_type const row)
830 insertRow(row, true);
834 void Tabular::appendRow(row_type row)
836 insertRow(row, false);
840 void Tabular::insertRow(row_type const row, bool copy)
842 row_info.insert(row_info.begin() + row + 1, row_info[row]);
843 cell_info.insert(cell_info.begin() + row + 1,
844 cell_vector(0, CellData(buffer_)));
846 for (col_type c = 0; c < ncols(); ++c) {
847 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
848 copy ? cell_info[row][c] : CellData(buffer_));
849 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
850 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
854 for (col_type c = 0; c < ncols(); ++c) {
855 if (isPartOfMultiRow(row, c))
857 // inherit line settings
858 idx_type const i = cellIndex(row + 1, c);
859 idx_type const j = cellIndex(row, c);
860 setLeftLine(i, leftLine(j));
861 setRightLine(i, rightLine(j));
862 setTopLine(i, topLine(j));
863 if (topLine(j) && bottomLine(j)) {
864 setBottomLine(i, true);
865 setBottomLine(j, false);
868 if (buffer().params().track_changes) {
869 row_info[row + 1].change.setInserted();
875 void Tabular::moveColumn(col_type col_start, col_type col_end,
876 ColDirection direction)
878 if (direction == Tabular::LEFT) {
879 for (col_type col = col_start; col <= col_end; ++col) {
880 std::swap(column_info[col - 1], column_info[col]);
881 for (row_type r = 0; r < nrows(); ++r) {
882 std::swap(cell_info[r][col - 1], cell_info[r][col]);
883 std::swap(cell_info[r][col - 1].left_line, cell_info[r][col].left_line);
884 std::swap(cell_info[r][col - 1].right_line, cell_info[r][col].right_line);
886 if (buffer().params().track_changes) {
887 idx_type const i = cellIndex(r, col - 1);
888 idx_type const j = cellIndex(r, col);
889 cellInfo(i).inset->setChange(Change(Change::INSERTED));
890 cellInfo(j).inset->setChange(Change(Change::INSERTED));
898 for (col_type col = col_end; col >= col_start; --col) {
899 std::swap(column_info[col], column_info[col + 1]);
900 for (row_type r = 0; r < nrows(); ++r) {
901 std::swap(cell_info[r][col], cell_info[r][col + 1]);
902 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
903 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
905 if (buffer().params().track_changes) {
906 idx_type const i = cellIndex(r, col);
907 idx_type const j = cellIndex(r, col + 1);
908 cellInfo(i).inset->setChange(Change(Change::INSERTED));
909 cellInfo(j).inset->setChange(Change(Change::INSERTED));
920 void Tabular::moveRow(row_type row_start, row_type row_end, RowDirection direction)
922 if (direction == Tabular::UP) {
923 for (row_type row = row_start; row <= row_end; ++row) {
924 std::swap(row_info[row - 1], row_info[row]);
925 for (col_type c = 0; c < ncols(); ++c) {
926 std::swap(cell_info[row - 1][c], cell_info[row][c]);
927 std::swap(cell_info[row - 1][c].top_line, cell_info[row][c].top_line);
928 std::swap(cell_info[row - 1][c].bottom_line, cell_info[row][c].bottom_line);
930 idx_type const i = cellIndex(row - 1, c);
931 idx_type const j = cellIndex(row, c);
932 if (buffer().params().track_changes) {
933 cellInfo(i).inset->setChange(Change(Change::INSERTED));
934 cellInfo(j).inset->setChange(Change(Change::INSERTED));
942 for (row_type row = row_end; row >= row_start; --row) {
943 std::swap(row_info[row], row_info[row + 1]);
944 for (col_type c = 0; c < ncols(); ++c) {
945 std::swap(cell_info[row][c], cell_info[row + 1][c]);
946 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
947 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
949 idx_type const i = cellIndex(row, c);
950 idx_type const j = cellIndex(row + 1, c);
951 if (buffer().params().track_changes) {
952 cellInfo(i).inset->setChange(Change(Change::INSERTED));
953 cellInfo(j).inset->setChange(Change(Change::INSERTED));
964 void Tabular::deleteColumn(col_type const col, bool const force)
966 // Not allowed to delete last column
970 bool const ct = force ? false : buffer().params().track_changes;
972 for (row_type r = 0; r < nrows(); ++r) {
973 // Care about multicolumn cells
974 if (col + 1 < ncols() &&
975 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
976 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
977 cell_info[r][col + 1] = cell_info[r][col];
980 cell_info[r].erase(cell_info[r].begin() + col);
983 column_info[col].change.setDeleted();
985 column_info.erase(column_info.begin() + col);
990 void Tabular::copyColumn(col_type const col)
992 insertColumn(col, true);
996 void Tabular::appendColumn(col_type col)
998 insertColumn(col, false);
1002 void Tabular::insertColumn(col_type const col, bool copy)
1004 bool const ct = buffer().params().track_changes;
1005 column_info.insert(column_info.begin() + col + 1, column_info[col]);
1007 for (row_type r = 0; r < nrows(); ++r) {
1008 cell_info[r].insert(cell_info[r].begin() + col + 1,
1009 copy ? cell_info[r][col] : CellData(buffer_));
1010 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
1011 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
1014 for (row_type r = 0; r < nrows(); ++r) {
1015 // inherit line settings
1016 idx_type const i = cellIndex(r, col + 1);
1017 idx_type const j = cellIndex(r, col);
1018 setBottomLine(i, bottomLine(j));
1019 setTopLine(i, topLine(j));
1020 setLeftLine(i, leftLine(j));
1021 setRightLine(i, rightLine(j));
1022 if (rightLine(i) && rightLine(j)) {
1023 setRightLine(j, false);
1027 column_info[col + 1].change.setInserted();
1033 void Tabular::updateIndexes()
1035 setBuffer(buffer());
1037 // reset cell number
1038 for (row_type row = 0; row < nrows(); ++row)
1039 for (col_type column = 0; column < ncols(); ++column) {
1040 if (!isPartOfMultiColumn(row, column)
1041 && !isPartOfMultiRow(row, column))
1043 if (isPartOfMultiRow(row, column))
1044 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1046 cell_info[row][column].cellno = numberofcells - 1;
1049 rowofcell.resize(numberofcells);
1050 columnofcell.resize(numberofcells);
1052 // reset column and row of cells and update their width, alignment and ct status
1053 for (row_type row = 0; row < nrows(); ++row) {
1054 for (col_type column = 0; column < ncols(); ++column) {
1055 if (isPartOfMultiColumn(row, column)) {
1056 cell_info[row][column].inset->toggleMultiCol(true);
1059 cell_info[row][column].inset->toggleMultiCol(false);
1060 // columnofcell needs to be called before setting width and alignment
1061 // multirow cells inherit the width from the column width
1062 if (!isPartOfMultiRow(row, column)) {
1063 columnofcell[i] = column;
1066 setFixedWidth(row, column);
1067 if (isPartOfMultiRow(row, column)) {
1068 cell_info[row][column].inset->toggleMultiRow(true);
1071 cell_info[row][column].inset->toggleMultiRow(false);
1072 cell_info[row][column].inset->setContentAlignment(
1073 getAlignment(cellIndex(row, column)));
1074 if (buffer().params().track_changes) {
1075 if (row_info[row].change.changed())
1076 cell_info[row][column].inset->setChange(row_info[row].change);
1077 if (column_info[column].change.changed())
1078 cell_info[row][column].inset->setChange(column_info[column].change);
1086 idx_type Tabular::numberOfCellsInRow(row_type const row) const
1088 idx_type result = 0;
1089 for (col_type c = 0; c < ncols(); ++c)
1090 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1096 bool Tabular::topLine(idx_type const cell) const
1098 return cellInfo(cell).top_line;
1102 bool Tabular::bottomLine(idx_type const cell) const
1104 return cellInfo(cell).bottom_line;
1108 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1110 if (use_booktabs && !ignore_bt)
1112 return cellInfo(cell).left_line;
1116 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1118 if (use_booktabs && !ignore_bt)
1120 return cellInfo(cell).right_line;
1124 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1127 return make_pair(false, false);
1128 return make_pair(cellInfo(cell).top_line_ltrimmed,
1129 cellInfo(cell).top_line_rtrimmed);
1133 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1136 return make_pair(false, false);
1137 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1138 cellInfo(cell).bottom_line_rtrimmed);
1142 int Tabular::interRowSpace(row_type row) const
1144 if (!row || row >= nrows())
1147 int const interline_space = row_info[row - 1].interline_space_default ?
1148 default_line_space :
1149 row_info[row - 1].interline_space.inPixels(width());
1150 if (rowTopLine(row) && rowBottomLine(row - 1))
1151 return interline_space + WIDTH_OF_LINE;
1152 return interline_space;
1156 int Tabular::interColumnSpace(idx_type cell) const
1158 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1159 if (rightLine(cell) && nextcol < ncols()
1160 && leftLine(cellIndex(cellRow(cell), nextcol)))
1161 return WIDTH_OF_LINE;
1166 int Tabular::cellWidth(idx_type cell) const
1169 col_type const span = columnSpan(cell);
1170 col_type const col = cellColumn(cell);
1171 for(col_type c = col; c < col + span ; ++c)
1172 w += column_info[c].width;
1177 int Tabular::cellHeight(idx_type cell) const
1179 row_type const span = rowSpan(cell);
1180 row_type const row = cellRow(cell);
1182 for(row_type r = row; r < row + span ; ++r) {
1183 h += rowAscent(r) + rowDescent(r);
1184 if (r != row + span - 1)
1185 h += interRowSpace(r + 1);
1192 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1194 vector<int> max_dwidth(ncols(), 0);
1195 // collect max. fixed width of column
1196 map<col_type, int> max_pwidth;
1197 // collect max. variable width of column
1198 map<col_type, int> max_width;
1200 for(col_type c = 0; c < ncols(); ++c)
1201 for(row_type r = 0; r < nrows(); ++r) {
1202 idx_type const i = cellIndex(r, c);
1203 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1204 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1205 if (!getPWidth(i).zero())
1206 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1207 else if (!column_info[c].varwidth)
1208 max_width[c] = max(max_width[c], cell_info[r][c].width);
1211 // If we have a fixed tabular width, we take this into account
1212 Length tab_width = tabular_width;
1213 bool const tabularx = hasVarwidthColumn();
1214 if (tabularx && tab_width.zero())
1215 // If no tabular width is specified with X columns,
1216 // we use 100% colwidth
1217 tab_width = Length(100, Length::PCW);
1219 if (!tab_width.zero()) {
1220 restwidth = mi.base.inPixels(tab_width);
1221 // Subtract the fixed widths from the table width
1222 for (auto const & w : max_pwidth)
1223 restwidth -= w.second;
1226 // If we have a fixed width, distribute the available table width
1227 // (minus the fixed widths) to the variable-width columns
1229 int restcols = ncols() - max_pwidth.size();
1230 if ((restwidth > 0) && (restcols != 0))
1231 vcolwidth = restwidth / restcols;
1233 // Now consider that some variable width columns exceed the vcolwidth
1234 if (vcolwidth > 0) {
1235 bool changed = false;
1236 for (auto const & w : max_width) {
1237 if (tabularx || w.second > vcolwidth) {
1239 restwidth -= w.second;
1243 if (changed && restwidth > 0)
1244 vcolwidth = restwidth / restcols;
1247 bool update = false;
1248 // for each col get max of single col cells
1249 for(col_type c = 0; c < ncols(); ++c) {
1251 for(row_type r = 0; r < nrows(); ++r) {
1252 idx_type const i = cellIndex(r, c);
1253 if (columnSpan(i) == 1) {
1254 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1255 && cell_info[r][c].decimal_width != 0)
1256 new_width = max(new_width, cellInfo(i).width
1257 + max_dwidth[c] - cellInfo(i).decimal_width);
1258 else if (getPWidth(i).zero() && vcolwidth > 0) {
1259 if (tabularx && !column_info[c].varwidth)
1260 new_width = max(new_width, cellInfo(i).width);
1262 new_width = vcolwidth;
1264 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1266 new_width = max(new_width, cellInfo(i).width);
1270 if (column_info[c].width != new_width) {
1271 column_info[c].width = new_width;
1272 // Do not trigger update when no space is left for variable
1273 // columns, as this will loop
1274 update = tab_width.zero() || restwidth > 0;
1277 // update col widths to fit merged cells
1278 for(col_type c = 0; c < ncols(); ++c)
1279 for(row_type r = 0; r < nrows(); ++r) {
1280 idx_type const i = cellIndex(r, c);
1281 int const span = columnSpan(i);
1282 if (span == 1 || c > cellColumn(i))
1286 for(col_type j = c; j < c + span ; ++j)
1287 old_width += column_info[j].width;
1289 if (cellInfo(i).width > old_width) {
1290 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1291 // Do not trigger update when no space is left for variable
1292 // columns, as this will loop
1293 update = tab_width.zero() || restwidth > 0;
1301 int Tabular::width() const
1304 for (col_type c = 0; c < ncols(); ++c)
1305 width += column_info[c].width;
1310 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1313 col_type const col = cellColumn(cell);
1314 // set alignment for the whole row if we are not in a multicolumn cell,
1315 // exclude possible multicolumn cells in the row
1316 if (!isMultiColumn(cell)) {
1317 for (row_type r = 0; r < nrows(); ++r) {
1318 // only if the column has no width the multirow inherits the
1319 // alignment of the column, otherwise it is left aligned
1320 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1321 && !isMultiColumn(cellIndex(r, col))) {
1322 cell_info[r][col].alignment = align;
1323 cell_info[r][col].inset->setContentAlignment(align);
1325 if ((isMultiRow(cellIndex(r, col)) && has_width)
1326 && !isMultiColumn(cellIndex(r, col))) {
1327 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1328 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1331 column_info[col].alignment = align;
1332 docstring & dpoint = column_info[col].decimal_point;
1333 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1334 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1335 dpoint = tlang->decimalSeparator();
1338 cellInfo(cell).alignment = align;
1339 cellInset(cell)->setContentAlignment(align);
1344 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1347 if (!isMultiColumn(cell) || onlycolumn)
1348 column_info[cellColumn(cell)].valignment = align;
1350 cellInfo(cell).valignment = align;
1357 * Allow line and paragraph breaks for fixed width multirow cells
1358 * or disallow them, merge cell paragraphs and reset layout to standard
1359 * for variable width multicol cells.
1361 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1362 bool const fixedWidth, bool const multirow)
1364 inset->toggleFixedWidth(fixedWidth);
1368 BufferParams const & bp = cur.bv().buffer().params();
1371 // undo information has already been recorded
1372 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1373 bp.documentClass().plainLayoutName());
1380 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1381 Length const & width)
1383 col_type const c = cellColumn(cell);
1385 column_info[c].p_width = width;
1386 // reset the vertical alignment to top if the fixed width
1387 // is removed or zero because only fixed width columns can
1388 // have a vertical alignment
1389 if (column_info[c].p_width.zero())
1390 column_info[c].valignment = LYX_VALIGN_TOP;
1391 for (row_type r = 0; r < nrows(); ++r) {
1392 idx_type const cidx = cellIndex(r, c);
1393 // because of multicolumns
1394 toggleFixedWidth(cur, cellInset(cidx).get(),
1395 !getPWidth(cidx).zero(), isMultiRow(cidx));
1396 if (isMultiRow(cidx))
1397 setAlignment(cidx, column_info[c].alignment,
1398 !column_info[c].p_width.zero());
1400 // cur can become invalid after paragraphs were merged
1405 bool Tabular::setFixedWidth(row_type r, col_type c)
1407 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1408 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1409 || (multicol && !cell_info[r][c].p_width.zero());
1410 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1415 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1416 Length const & width)
1418 if (!isMultiColumn(cell))
1421 cellInfo(cell).p_width = width;
1422 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1424 // cur can become invalid after paragraphs were merged
1430 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1432 column_info[cellColumn(cell)].varwidth = varwidth;
1433 cellInset(cell).get()->toggleVarWidth(varwidth);
1438 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1440 cellInfo(cell).mroffset = mroffset;
1445 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1446 Tabular::Feature what)
1448 if (what == SET_SPECIAL_MULTICOLUMN)
1449 cellInfo(cell).align_special = special;
1451 column_info[cellColumn(cell)].align_special = special;
1455 void Tabular::setTopLine(idx_type i, bool line)
1457 cellInfo(i).top_line = line;
1461 void Tabular::setBottomLine(idx_type i, bool line)
1463 cellInfo(i).bottom_line = line;
1467 void Tabular::setTopLineLTrim(idx_type i, bool val)
1469 cellInfo(i).top_line_ltrimmed = val;
1473 void Tabular::setTopLineRTrim(idx_type i, bool val)
1475 cellInfo(i).top_line_rtrimmed = val;
1479 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1481 cellInfo(i).bottom_line_ltrimmed = val;
1485 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1487 cellInfo(i).bottom_line_rtrimmed = val;
1491 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1493 setTopLineLTrim(i, trim.first);
1494 setTopLineRTrim(i, trim.second);
1497 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1499 setBottomLineLTrim(i, trim.first);
1500 setBottomLineRTrim(i, trim.second);
1503 void Tabular::setLeftLine(idx_type cell, bool line)
1505 cellInfo(cell).left_line = line;
1509 void Tabular::setRightLine(idx_type cell, bool line)
1511 cellInfo(cell).right_line = line;
1514 bool Tabular::rowTopLine(row_type r) const
1516 bool all_rows_set = true;
1517 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1518 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1519 return all_rows_set;
1523 bool Tabular::rowBottomLine(row_type r) const
1525 bool all_rows_set = true;
1526 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1527 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1528 return all_rows_set;
1532 bool Tabular::columnLeftLine(col_type c) const
1539 for (row_type r = 0; r < nrows(); ++r) {
1540 idx_type const i = cellIndex(r, c);
1541 if (c == cellColumn(i)) {
1543 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1544 if (cellInfo(i).left_line || right)
1548 return 2 * nrows_left >= total;
1552 bool Tabular::columnRightLine(col_type c) const
1557 int nrows_right = 0;
1559 for (row_type r = 0; r < nrows(); ++r) {
1560 idx_type i = cellIndex(r, c);
1561 if (c == cellColumn(i) + columnSpan(i) - 1) {
1563 bool left = (c + 1 < ncols()
1564 && cellInfo(cellIndex(r, c + 1)).left_line)
1565 || c + 1 == ncols();
1566 if (cellInfo(i).right_line && left)
1570 return 2 * nrows_right >= total;
1574 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1576 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1577 return cellInfo(cell).alignment;
1579 return column_info[cellColumn(cell)].alignment;
1584 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1586 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1587 return cellInfo(cell).valignment;
1588 return column_info[cellColumn(cell)].valignment;
1592 int Tabular::offsetVAlignment() const
1594 // for top-alignment the first horizontal table line must be exactly at
1595 // the position of the base line of the surrounding text line
1596 // for bottom alignment, the same is for the last table line
1597 int offset_valign = 0;
1598 switch (tabular_valignment) {
1599 case Tabular::LYX_VALIGN_BOTTOM:
1600 offset_valign = rowAscent(0) - height();
1602 case Tabular::LYX_VALIGN_MIDDLE:
1603 offset_valign = (- height()) / 2 + rowAscent(0);
1605 case Tabular::LYX_VALIGN_TOP:
1606 offset_valign = rowAscent(0);
1609 return offset_valign;
1613 Length const Tabular::getPWidth(idx_type cell) const
1615 if (isMultiColumn(cell))
1616 return cellInfo(cell).p_width;
1617 return column_info[cellColumn(cell)].p_width;
1621 Length const Tabular::getMROffset(idx_type cell) const
1623 return cellInfo(cell).mroffset;
1627 int Tabular::textHOffset(idx_type cell) const
1629 // the LaTeX Way :-(
1630 int x = WIDTH_OF_LINE;
1632 int const w = cellWidth(cell) - cellInfo(cell).width;
1634 switch (getAlignment(cell)) {
1635 case LYX_ALIGN_CENTER:
1638 case LYX_ALIGN_RIGHT:
1641 case LYX_ALIGN_DECIMAL: {
1642 // we center when no decimal point
1643 if (cellInfo(cell).decimal_width == 0) {
1647 col_type const c = cellColumn(cell);
1648 int max_dhoffset = 0;
1649 for(row_type r = 0; r < row_info.size() ; ++r) {
1650 idx_type const i = cellIndex(r, c);
1651 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1652 && cellInfo(i).decimal_width != 0)
1653 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1655 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1658 // LYX_ALIGN_LEFT: nothing :-)
1666 int Tabular::textVOffset(idx_type cell) const
1668 int voffset = cellInfo(cell).voffset;
1669 if (isMultiRow(cell)) {
1670 row_type const row = cellRow(cell);
1671 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1677 idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1680 idx_type const numcells = numberOfCellsInRow(row);
1681 // we check against numcells to make sure we do not crash if all the
1682 // cells are multirow (bug #7535), but in that case our return value
1683 // is really invalid, i.e., it is NOT the first cell in the row. but
1684 // i do not know what to do here. (rgh)
1685 while (c < numcells - 1
1686 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1687 || (ct && column_info[c].change.deleted())))
1689 return cell_info[row][c].cellno;
1693 idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1695 col_type c = ncols() - 1;
1696 // of course we check against 0 so we don't crash. but we have the same
1697 // problem as in the previous routine: if all the cells are part of a
1698 // multirow or part of a multi column, then our return value is invalid.
1700 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1701 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1702 || (ct && column_info[c].change.deleted())))
1704 return cell_info[row][c].cellno;
1708 row_type Tabular::getFirstRow(bool const ct) const
1713 // exclude deleted rows if ct == true
1714 while (r < nrows() && row_info[r].change.deleted())
1720 row_type Tabular::getLastRow(bool const ct) const
1722 row_type r = nrows() - 1;
1725 // exclude deleted rows if ct == true
1726 while (r > 0 && row_info[r].change.deleted())
1732 row_type Tabular::cellRow(idx_type cell) const
1734 if (cell >= numberofcells)
1738 return rowofcell[cell];
1742 col_type Tabular::cellColumn(idx_type cell) const
1744 if (cell >= numberofcells)
1748 return columnofcell[cell];
1752 void Tabular::write(ostream & os) const
1756 << write_attribute("version", 3)
1757 << write_attribute("rows", nrows())
1758 << write_attribute("columns", ncols())
1760 // global longtable options
1762 << write_attribute("rotate", rotate)
1763 << write_attribute("booktabs", use_booktabs)
1764 << write_attribute("islongtable", is_long_tabular)
1765 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1766 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1767 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1768 << write_attribute("headTopDL", endhead.topDL)
1769 << write_attribute("headBottomDL", endhead.bottomDL)
1770 << write_attribute("footTopDL", endfoot.topDL)
1771 << write_attribute("footBottomDL", endfoot.bottomDL)
1772 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1773 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1774 << write_attribute("lastFootEmpty", endlastfoot.empty);
1775 // longtables cannot be aligned vertically
1776 if (!is_long_tabular)
1777 os << write_attribute("tabularvalignment", tabular_valignment);
1778 os << write_attribute("tabularwidth", tabular_width);
1779 if (is_long_tabular)
1780 os << write_attribute("longtabularalignment", longtabular_alignment);
1782 for (col_type c = 0; c < ncols(); ++c) {
1784 << write_attribute("alignment", column_info[c].alignment);
1785 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1786 os << write_attribute("decimal_point", column_info[c].decimal_point);
1787 os << write_attribute("change", column_info[c].change, buffer().params())
1788 << write_attribute("valignment", column_info[c].valignment)
1789 << write_attribute("width", column_info[c].p_width.asString())
1790 << write_attribute("varwidth", column_info[c].varwidth)
1791 << write_attribute("special", column_info[c].align_special)
1794 for (row_type r = 0; r < nrows(); ++r) {
1795 static const string def("default");
1797 if (row_info[r].top_space_default)
1798 os << write_attribute("topspace", def);
1800 os << write_attribute("topspace", row_info[r].top_space);
1801 if (row_info[r].bottom_space_default)
1802 os << write_attribute("bottomspace", def);
1804 os << write_attribute("bottomspace", row_info[r].bottom_space);
1805 if (row_info[r].interline_space_default)
1806 os << write_attribute("interlinespace", def);
1808 os << write_attribute("interlinespace", row_info[r].interline_space);
1809 os << write_attribute("change", row_info[r].change, buffer().params())
1810 << write_attribute("endhead", row_info[r].endhead)
1811 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1812 << write_attribute("endfoot", row_info[r].endfoot)
1813 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1814 << write_attribute("newpage", row_info[r].newpage)
1815 << write_attribute("caption", row_info[r].caption)
1817 for (col_type c = 0; c < ncols(); ++c) {
1819 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1820 << write_attribute("multirow", cell_info[r][c].multirow)
1821 << write_attribute("mroffset", cell_info[r][c].mroffset)
1822 << write_attribute("alignment", cell_info[r][c].alignment)
1823 << write_attribute("valignment", cell_info[r][c].valignment)
1824 << write_attribute("topline", cell_info[r][c].top_line)
1825 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1826 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1827 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1828 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1829 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1830 << write_attribute("leftline", cell_info[r][c].left_line)
1831 << write_attribute("rightline", cell_info[r][c].right_line)
1832 << write_attribute("rotate", cell_info[r][c].rotate)
1833 << write_attribute("usebox", cell_info[r][c].usebox)
1834 << write_attribute("width", cell_info[r][c].p_width)
1835 << write_attribute("special", cell_info[r][c].align_special)
1837 os << "\\begin_inset ";
1838 cell_info[r][c].inset->write(os);
1839 os << "\n\\end_inset\n"
1841 // FIXME This can be removed again once the mystery
1842 // crash has been resolved.
1847 os << "</lyxtabular>\n";
1851 void Tabular::read(Lexer & lex)
1854 istream & is = lex.getStream();
1856 l_getline(is, line);
1857 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1858 LASSERT(false, return);
1862 if (!getTokenValue(line, "version", version))
1864 LATTEST(version >= 2);
1867 if (!getTokenValue(line, "rows", rows_arg))
1870 if (!getTokenValue(line, "columns", columns_arg))
1872 init(buffer_, rows_arg, columns_arg);
1873 l_getline(is, line);
1874 if (!prefixIs(line, "<features")) {
1875 lyxerr << "Wrong tabular format (expected <features ...> got"
1876 << line << ')' << endl;
1879 getTokenValue(line, "rotate", rotate);
1880 getTokenValue(line, "booktabs", use_booktabs);
1881 getTokenValue(line, "islongtable", is_long_tabular);
1882 getTokenValue(line, "tabularvalignment", tabular_valignment);
1883 getTokenValue(line, "tabularwidth", tabular_width);
1884 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1885 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1886 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1887 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1888 getTokenValue(line, "headTopDL", endhead.topDL);
1889 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1890 getTokenValue(line, "footTopDL", endfoot.topDL);
1891 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1892 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1893 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1894 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1896 for (col_type c = 0; c < ncols(); ++c) {
1898 if (!prefixIs(line,"<column")) {
1899 lyxerr << "Wrong tabular format (expected <column ...> got"
1900 << line << ')' << endl;
1903 getTokenValue(line, "alignment", column_info[c].alignment);
1904 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1905 getTokenValue(line, "valignment", column_info[c].valignment);
1906 getTokenValue(line, "width", column_info[c].p_width);
1907 getTokenValue(line, "special", column_info[c].align_special);
1908 getTokenValue(line, "varwidth", column_info[c].varwidth);
1909 getTokenValue(line, "change", column_info[c].change, buffer().params());
1912 for (row_type i = 0; i < nrows(); ++i) {
1913 l_getline(is, line);
1914 if (!prefixIs(line, "<row")) {
1915 lyxerr << "Wrong tabular format (expected <row ...> got"
1916 << line << ')' << endl;
1919 getTokenValue(line, "topspace", row_info[i].top_space,
1920 row_info[i].top_space_default);
1921 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1922 row_info[i].bottom_space_default);
1923 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1924 row_info[i].interline_space_default);
1925 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1926 getTokenValue(line, "endhead", row_info[i].endhead);
1927 getTokenValue(line, "endfoot", row_info[i].endfoot);
1928 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1929 getTokenValue(line, "newpage", row_info[i].newpage);
1930 getTokenValue(line, "caption", row_info[i].caption);
1931 getTokenValue(line, "change", row_info[i].change, buffer().params());
1932 for (col_type j = 0; j < ncols(); ++j) {
1933 l_getline(is, line);
1934 if (!prefixIs(line, "<cell")) {
1935 lyxerr << "Wrong tabular format (expected <cell ...> got"
1936 << line << ')' << endl;
1939 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1940 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1941 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1942 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1943 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1944 getTokenValue(line, "topline", cell_info[i][j].top_line);
1945 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1946 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1947 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1948 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1949 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1950 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1951 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1952 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1953 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1954 getTokenValue(line, "width", cell_info[i][j].p_width);
1956 getTokenValue(line, "special", cell_info[i][j].align_special);
1957 l_getline(is, line);
1958 if (prefixIs(line, "\\begin_inset")) {
1959 cell_info[i][j].inset->setBuffer(*buffer_);
1960 cell_info[i][j].inset->read(lex);
1961 l_getline(is, line);
1963 if (!prefixIs(line, "</cell>")) {
1964 lyxerr << "Wrong tabular format (expected </cell> got"
1965 << line << ')' << endl;
1969 l_getline(is, line);
1970 if (!prefixIs(line, "</row>")) {
1971 lyxerr << "Wrong tabular format (expected </row> got"
1972 << line << ')' << endl;
1976 while (!prefixIs(line, "</lyxtabular>")) {
1977 l_getline(is, line);
1983 bool Tabular::isMultiColumn(idx_type cell) const
1985 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1986 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1990 bool Tabular::hasMultiColumn(col_type c) const
1992 for (row_type r = 0; r < nrows(); ++r) {
1993 if (isMultiColumn(cellIndex(r, c)))
2000 bool Tabular::hasVarwidthColumn() const
2002 for (col_type c = 0; c < ncols(); ++c) {
2003 if (column_info[c].varwidth)
2010 bool Tabular::isVTypeColumn(col_type c) const
2012 for (row_type r = 0; r < nrows(); ++r) {
2013 idx_type idx = cellIndex(r, c);
2014 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH
2015 && getAlignment(idx) == LYX_ALIGN_LEFT
2016 && getVAlignment(idx) == LYX_VALIGN_TOP)
2023 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
2025 return cell_info[cellRow(cell)][cellColumn(cell)];
2029 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2031 return cell_info[cellRow(cell)][cellColumn(cell)];
2035 idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2036 bool const right_border)
2038 idx_type const col = cellColumn(cell);
2039 idx_type const row = cellRow(cell);
2040 for (idx_type i = 0; i < number; ++i)
2041 unsetMultiRow(cellIndex(row, col + i));
2043 // unsetting of multirow may have invalidated cell index
2044 cell = cellIndex(row, col);
2045 CellData & cs = cellInfo(cell);
2046 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2047 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2048 cs.alignment = column_info[col].alignment;
2049 setRightLine(cell, right_border);
2050 // non-fixed width multicolumns cannot have multiple paragraphs
2051 if (getPWidth(cell).zero()) {
2052 toggleFixedWidth(cur, cellInset(cell).get(),
2053 !getPWidth(cell).zero(), isMultiRow(cell));
2054 // cur can become invalid after paragraphs were merged
2058 idx_type lastcell = cellIndex(row, col + number - 1);
2059 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2060 CellData & cs1 = cellInfo(cell + i);
2061 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2062 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2070 bool Tabular::isMultiRow(idx_type cell) const
2072 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2073 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2076 bool Tabular::hasMultiRow(row_type r) const
2078 for (col_type c = 0; c < ncols(); ++c) {
2079 if (isMultiRow(cellIndex(r, c)))
2085 idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2086 bool const bottom_border,
2087 LyXAlignment const halign)
2089 idx_type const col = cellColumn(cell);
2090 idx_type const row = cellRow(cell);
2091 for (idx_type i = 0; i < number; ++i)
2092 unsetMultiColumn(cellIndex(row + i, col));
2094 // unsetting of multirow may have invalidated cell index
2095 cell = cellIndex(row, col);
2096 CellData & cs = cellInfo(cell);
2097 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2098 cs.valignment = LYX_VALIGN_MIDDLE;
2099 // the horizontal alignment of multirow cells can only
2100 // be changed for the whole table row,
2101 // support changing this only for the multirow cell can be done via
2103 if (getPWidth(cell).zero())
2104 cs.alignment = halign;
2106 cs.alignment = LYX_ALIGN_LEFT;
2108 // Multirows cannot have multiple paragraphs
2109 if (getPWidth(cell).zero()) {
2110 toggleFixedWidth(cur, cellInset(cell).get(),
2111 !getPWidth(cell).zero(),
2113 // cur can become invalid after paragraphs were merged
2117 // set the bottom line of the last selected cell
2118 setBottomLine(cell, bottom_border);
2120 for (idx_type i = 1; i < number; ++i) {
2121 CellData & cs1 = cell_info[row + i][col];
2122 cs1.multirow = CELL_PART_OF_MULTIROW;
2123 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2131 idx_type Tabular::columnSpan(idx_type cell) const
2133 row_type const row = cellRow(cell);
2134 col_type const col = cellColumn(cell);
2136 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2143 idx_type Tabular::rowSpan(idx_type cell) const
2145 col_type const column = cellColumn(cell);
2146 col_type row = cellRow(cell) + 1;
2147 while (row < nrows() && isPartOfMultiRow(row, column))
2150 return row - cellRow(cell);
2154 void Tabular::unsetMultiColumn(idx_type cell)
2156 if (!isMultiColumn(cell))
2159 row_type const row = cellRow(cell);
2160 col_type const col = cellColumn(cell);
2161 row_type const span = columnSpan(cell);
2162 for (col_type c = 0; c < span; ++c) {
2163 // in the table dialog the lines are set in every case
2164 // when unsetting a multicolumn this leads to an additional right
2165 // line for every cell that was part of the former multicolumn cell,
2166 // except if the cell is in the last column
2167 // therefore remove this line
2168 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2169 && (col + c) < (col + span - 1))
2170 cell_info[row][col + c].right_line = false;
2171 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2177 void Tabular::unsetMultiRow(idx_type cell)
2179 if (!isMultiRow(cell))
2182 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2183 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2184 row_type const row = cellRow(cell);
2185 col_type const col = cellColumn(cell);
2186 row_type const span = rowSpan(cell);
2187 for (row_type r = 0; r < span; ++r)
2188 cell_info[row + r][col].multirow = CELL_NORMAL;
2193 void Tabular::setRotateCell(idx_type cell, int value)
2195 cellInfo(cell).rotate = value;
2199 int Tabular::getRotateCell(idx_type cell) const
2201 return cellInfo(cell).rotate;
2205 bool Tabular::needRotating() const
2207 if (rotate && !is_long_tabular)
2209 for (row_type r = 0; r < nrows(); ++r)
2210 for (col_type c = 0; c < ncols(); ++c)
2211 if (cell_info[r][c].rotate != 0)
2217 bool Tabular::isLastCell(idx_type cell) const
2219 return cell + 1 >= numberofcells;
2223 idx_type Tabular::cellAbove(idx_type cell) const
2225 if (cellRow(cell) == 0)
2228 col_type const col = cellColumn(cell);
2229 row_type r = cellRow(cell) - 1;
2230 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2233 return cell_info[r][col].cellno;
2237 idx_type Tabular::cellBelow(idx_type cell) const
2239 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2240 if (nextrow < nrows())
2241 return cell_info[nextrow][cellColumn(cell)].cellno;
2246 idx_type Tabular::cellIndex(row_type row, col_type column) const
2248 LASSERT(column != npos && column < ncols(), column = 0);
2249 LASSERT(row != npos && row < nrows(), row = 0);
2250 return cell_info[row][column].cellno;
2254 void Tabular::setUsebox(idx_type cell, BoxType type)
2256 cellInfo(cell).usebox = type;
2260 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2262 if (getRotateCell(cell) == 0
2263 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2264 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2266 if (cellInfo(cell).usebox > 1)
2267 return cellInfo(cell).usebox;
2268 return useBox(cell);
2273 // This are functions used for the longtable support
2275 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2281 row_info[row].endfirsthead = flag;
2285 row_info[row].endhead = flag;
2290 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2293 hd.set = haveLTHead();
2294 return row_info[row].endhead;
2298 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2301 hd.set = haveLTFirstHead();
2302 return row_info[row].endfirsthead;
2306 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2312 row_info[row].endlastfoot = flag;
2316 row_info[row].endfoot = flag;
2321 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2324 fd.set = haveLTFoot();
2325 return row_info[row].endfoot;
2329 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2332 fd.set = haveLTLastFoot();
2333 return row_info[row].endlastfoot;
2337 void Tabular::setLTNewPage(row_type row, bool what)
2339 row_info[row].newpage = what;
2343 bool Tabular::getLTNewPage(row_type row) const
2345 return row_info[row].newpage;
2349 bool Tabular::haveLTHead(bool withcaptions) const
2351 if (!is_long_tabular)
2353 for (row_type i = 0; i < nrows(); ++i)
2354 if (row_info[i].endhead &&
2355 (withcaptions || !row_info[i].caption))
2361 bool Tabular::haveLTFirstHead(bool withcaptions) const
2363 if (!is_long_tabular || endfirsthead.empty)
2365 for (row_type r = 0; r < nrows(); ++r)
2366 if (row_info[r].endfirsthead &&
2367 (withcaptions || !row_info[r].caption))
2373 bool Tabular::haveLTFoot(bool withcaptions) const
2375 if (!is_long_tabular)
2377 for (row_type r = 0; r < nrows(); ++r)
2378 if (row_info[r].endfoot &&
2379 (withcaptions || !row_info[r].caption))
2385 bool Tabular::haveLTLastFoot(bool withcaptions) const
2387 if (!is_long_tabular || endlastfoot.empty)
2389 for (row_type r = 0; r < nrows(); ++r)
2390 if (row_info[r].endlastfoot &&
2391 (withcaptions || !row_info[r].caption))
2397 idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2399 idx_type i = getFirstCellInRow(row);
2401 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2402 setTopLine(i, false);
2403 setBottomLine(i, false);
2404 setLeftLine(i, false);
2405 setRightLine(i, false);
2406 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2407 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2408 setLTHead(row, true, endfirsthead, true);
2409 row_info[row].endfirsthead = true;
2412 unsetMultiColumn(i);
2413 // When unsetting a caption row, also all existing
2414 // captions in this row must be dissolved.
2416 row_info[row].caption = what;
2421 bool Tabular::ltCaption(row_type row) const
2423 return row_info[row].caption;
2427 bool Tabular::haveLTCaption(CaptionType captiontype) const
2429 if (!is_long_tabular)
2431 for (row_type r = 0; r < nrows(); ++r) {
2432 if (row_info[r].caption) {
2433 switch (captiontype) {
2434 case CAPTION_FIRSTHEAD:
2435 if (row_info[r].endfirsthead)
2439 if (row_info[r].endhead)
2443 if (row_info[r].endfoot)
2446 case CAPTION_LASTFOOT:
2447 if (row_info[r].endlastfoot)
2459 // end longtable support functions
2461 void Tabular::setRowAscent(row_type row, int height)
2463 if (row >= nrows() || row_info[row].ascent == height)
2465 row_info[row].ascent = height;
2469 void Tabular::setRowDescent(row_type row, int height)
2471 if (row >= nrows() || row_info[row].descent == height)
2473 row_info[row].descent = height;
2477 int Tabular::rowAscent(row_type row) const
2479 LASSERT(row < nrows(), row = 0);
2480 return row_info[row].ascent;
2484 int Tabular::rowDescent(row_type row) const
2486 LASSERT(row < nrows(), row = 0);
2487 return row_info[row].descent;
2491 int Tabular::height() const
2494 for (row_type row = 0; row < nrows(); ++row)
2495 height += rowAscent(row) + rowDescent(row) +
2501 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2503 LASSERT(row < nrows(), return false);
2504 LASSERT(column < ncols(), return false);
2505 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2509 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2511 LASSERT(row < nrows(), return false);
2512 LASSERT(column < ncols(), return false);
2513 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2517 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns,
2518 list<col_type> logical_columns) const
2520 // we only output complete row lines and the 1st row here, the rest
2521 // is done in Tabular::TeXBottomHLine(...)
2523 // get for each column the topline (if any)
2524 map<col_type, bool> topline, topltrims, toprtrims;
2526 bool have_trims = false;
2527 for (auto const & c : columns) {
2528 topline[c] = topLine(cellIndex(row, c));
2529 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2530 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2531 // If cell is part of a multirow and not the first cell of the
2532 // multirow, no line must be drawn.
2534 if (isMultiRow(cellIndex(row, c))
2535 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2537 topltrims[c] = false;
2538 toprtrims[c] = false;
2540 // copy trimming to multicolumn parts
2541 if (isPartOfMultiColumn(row, c)) {
2542 topltrims[c] = topltrims[c-1];
2543 toprtrims[c] = toprtrims[c-1];
2545 if (topline.find(c) != topline.end() && topline.find(c)->second)
2547 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2548 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2552 // do nothing if empty first row, or incomplete row line after
2553 row_type first = getFirstRow(!buffer().params().output_changes);
2554 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2557 // Is this the actual first row (excluding longtable caption row)?
2558 bool const realfirstrow = (row == first
2559 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2561 // only output complete row lines and the 1st row's clines
2562 if (nset == columns.size() && !have_trims) {
2564 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2568 } else if (realfirstrow || have_trims) {
2569 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2571 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2572 std::list<col_type>::const_iterator it2 = columns.begin();
2573 // We need to iterate over the logical columns here, but take care for
2575 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2580 if (topline.find(c)->second) {
2581 col_type offset = 0;
2582 for (col_type j = 0 ; j < c; ++j)
2583 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2585 // If the two iterators differ, we are in bidi with swapped columns
2586 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2587 while (isPartOfMultiColumn(row, c))
2590 if (topltrims.find(c) != topltrims.end()
2591 && topltrims.find(c)->second)
2593 col_type cstart = c;
2594 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2595 if (isMultiColumn(cellIndex(row, c))
2596 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2598 if (c > cstart && topltrims.find(c) != topltrims.end()
2599 && topltrims.find(c)->second) {
2600 if (!isPartOfMultiColumn(row, c))
2603 } else if (toprtrims.find(c) != toprtrims.end()
2604 && toprtrims.find(c)->second)
2608 for (col_type j = cstart ; j <= c ; ++j)
2609 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2611 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2612 if (toprtrims.find(c) != toprtrims.end()
2613 && toprtrims.find(c)->second)
2618 os << "(" << trim << ")";
2619 if (firstcol > lastcol)
2620 // This can happen with bidi (swapped columns)
2621 os << "{" << lastcol << '-' << firstcol << "}";
2623 os << "{" << firstcol << '-' << lastcol << "}";
2624 if (c == columns.size() - 1)
2634 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns,
2635 list<col_type> logical_columns) const
2637 // we output bottomlines of row r and the toplines of row r+1
2638 // if the latter do not span the whole tabular
2640 // get the bottomlines of row r, and toplines in next row
2641 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2642 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2643 bool nextrowset = true;
2644 for (auto const & c : columns) {
2645 idx_type const idx = cellIndex(row, c);
2646 bottomline[c] = bottomLine(cellIndex(row, c));
2647 bottomltrims[c] = bottomLineTrim(idx).first;
2648 bottomrtrims[c] = bottomLineTrim(idx).second;
2649 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2650 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2651 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2652 // If cell is part of a multirow and not the last cell of the
2653 // multirow, no line must be drawn.
2655 if (isMultiRow(cellIndex(row, c))
2656 && isMultiRow(cellIndex(row + 1, c))
2657 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2658 bottomline[c] = false;
2660 bottomltrims[c] = false;
2661 bottomrtrims[c] = false;
2662 topltrims[c] = false;
2663 toprtrims[c] = false;
2665 // copy trimming in multicolumn parts
2666 if (isPartOfMultiColumn(row, c)) {
2667 topltrims[c] = topltrims[c-1];
2668 toprtrims[c] = toprtrims[c-1];
2669 bottomltrims[c] = bottomltrims[c-1];
2670 bottomrtrims[c] = bottomrtrims[c-1];
2673 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2676 // combine this row's bottom lines and next row's toplines if necessary
2678 bool have_trims = false;
2679 for (auto const & c : columns) {
2681 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2682 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2683 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2684 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2685 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2686 if (bottomline.find(c)->second)
2688 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2689 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2693 // do nothing if empty, OR incomplete row line with a topline in next row
2694 if (nset == 0 || (nextrowset && nset != columns.size()))
2697 if (nset == columns.size() && !have_trims) {
2699 os << (lastrow ? "\\bottomrule" : "\\midrule");
2703 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2705 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2706 std::list<col_type>::const_iterator it2 = columns.begin();
2707 // We need to iterate over the logical columns here, but take care for
2709 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2714 if (bottomline.find(c)->second) {
2715 col_type offset = 0;
2716 for (col_type j = 0 ; j < c; ++j)
2717 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2719 // If the two iterators differ, we are in bidi with swapped columns
2720 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2721 while (isPartOfMultiColumn(row, c))
2724 if (bottomltrims.find(c) != bottomltrims.end()
2725 && bottomltrims.find(c)->second)
2727 col_type cstart = c;
2728 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2729 if (isMultiColumn(cellIndex(row, c))
2731 && isPartOfMultiColumn(row, c + 1))
2734 && bottomltrims.find(c) != bottomltrims.end()
2735 && bottomltrims.find(c)->second) {
2736 if (!isPartOfMultiColumn(row, c))
2739 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2740 && bottomrtrims.find(c)->second)
2744 for (col_type j = cstart ; j <= c ; ++j)
2745 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2747 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2748 if (bottomrtrims.find(c) != bottomrtrims.end()
2749 && bottomrtrims.find(c)->second)
2754 os << "(" << trim << ")";
2755 if (firstcol > lastcol)
2756 // This can happen with bidi (swapped columns)
2757 os << "{" << lastcol << '-' << firstcol << "}";
2759 os << "{" << firstcol << '-' << lastcol << "}";
2760 if (c == columns.size() - 1)
2770 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2771 bool & ismulticol, bool & ismultirow,
2772 bool const bidi) const
2774 row_type const r = cellRow(cell);
2775 if (is_long_tabular && row_info[r].caption)
2778 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2779 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2780 // figure out how to set the lines
2781 // we always set double lines to the right of the cell
2782 // or left in bidi RTL, respectively.
2783 col_type const c = cellColumn(cell);
2784 col_type const nextcol = c + columnSpan(cell);
2785 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2786 bool colright = columnRightLine(c);
2787 bool colleft = columnLeftLine(c);
2788 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2789 bool nextcellleft = nextcol < ncols()
2790 && leftLine(cellIndex(r, nextcol));
2791 bool coldouble = colright && nextcolleft;
2792 bool celldouble = rightLine(cell) && nextcellleft;
2794 ismulticol = (isMultiColumn(cell)
2795 || (c == 0 && colleft != leftLine(cell))
2796 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2797 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2798 || (coldouble != celldouble))
2801 // we center in multicol when no decimal point
2803 docstring const align_d = column_info[c].decimal_point;
2804 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2805 bool const nosep = !dit;
2806 ismulticol |= nosep;
2807 celldouble &= nosep;
2810 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2811 int latexcolspan = columnSpan(cell);
2812 for (col_type col = c; col < c + columnSpan(cell); ++col)
2813 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2817 os << "\\multicolumn{" << latexcolspan << "}{";
2818 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2819 || (!bidi && c == 0 && leftLine(cell))))
2821 if (bidi && celldouble)
2822 // add extra vertical line if we want a double one
2824 if (!cellInfo(cell).align_special.empty()) {
2825 os << cellInfo(cell).align_special;
2827 if (!getPWidth(cell).zero()) {
2829 case LYX_ALIGN_LEFT:
2830 os << ">{\\raggedright}";
2832 case LYX_ALIGN_RIGHT:
2833 os << ">{\\raggedleft}";
2835 case LYX_ALIGN_CENTER:
2836 os << ">{\\centering}";
2842 case LYX_VALIGN_TOP:
2845 case LYX_VALIGN_MIDDLE:
2848 case LYX_VALIGN_BOTTOM:
2853 << from_ascii(getPWidth(cell).asLatexString())
2856 if ((getRotateCell(cell) == 0 && useBox(cell) == BOX_VARWIDTH
2857 && align == LYX_ALIGN_LEFT))
2858 os << "V{\\linewidth}";
2861 case LYX_ALIGN_LEFT:
2864 case LYX_ALIGN_RIGHT:
2872 } // end if else !getPWidth
2873 } // end if else !cellinfo_of_cell
2874 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2876 if (!bidi && celldouble)
2877 // add extra vertical line if we want a double one
2880 } // end if ismulticol
2882 // we only need code for the first multirow cell
2883 ismultirow = isMultiRow(cell);
2885 os << "\\multirow{" << rowSpan(cell) << "}{";
2886 if (!getPWidth(cell).zero())
2887 os << from_ascii(getPWidth(cell).asLatexString());
2889 if (column_info[c].varwidth)
2890 // this inherits varwidth size
2893 // we need to set a default value
2897 if (!getMROffset(cell).zero())
2898 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2900 } // end if ismultirow
2902 if (getRotateCell(cell) != 0)
2903 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2905 if (getUsebox(cell) == BOX_PARBOX) {
2908 case LYX_VALIGN_TOP:
2911 case LYX_VALIGN_MIDDLE:
2914 case LYX_VALIGN_BOTTOM:
2918 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2920 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2921 os << "\\begin{minipage}[";
2923 case LYX_VALIGN_TOP:
2926 case LYX_VALIGN_MIDDLE:
2929 case LYX_VALIGN_BOTTOM:
2933 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2935 } else if (getUsebox(cell) == BOX_VARWIDTH
2936 && (getRotateCell(cell) != 0 || align != LYX_ALIGN_LEFT
2937 || valign != LYX_VALIGN_TOP)) {
2938 os << "\\begin{cellvarwidth}[";
2940 case LYX_VALIGN_TOP:
2943 case LYX_VALIGN_MIDDLE:
2946 case LYX_VALIGN_BOTTOM:
2952 case LYX_ALIGN_RIGHT:
2953 os << "\\raggedleft\n";
2955 case LYX_ALIGN_CENTER:
2956 os << "\\centering\n";
2958 case LYX_ALIGN_LEFT:
2959 //os << "\\narrowragged\n";
2961 case LYX_ALIGN_BLOCK:
2962 case LYX_ALIGN_DECIMAL:
2963 case LYX_ALIGN_NONE:
2964 case LYX_ALIGN_LAYOUT:
2965 case LYX_ALIGN_SPECIAL:
2972 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2973 bool ismulticol, bool ismultirow) const
2975 row_type const r = cellRow(cell);
2976 if (is_long_tabular && row_info[r].caption)
2980 if (getUsebox(cell) == BOX_PARBOX)
2982 else if (getUsebox(cell) == BOX_MINIPAGE)
2983 os << breakln << "\\end{minipage}";
2984 else if (getUsebox(cell) == BOX_VARWIDTH
2985 && (getRotateCell(cell) != 0 || getAlignment(cell) != LYX_ALIGN_LEFT
2986 || getVAlignment(cell) != LYX_VALIGN_TOP))
2987 os << breakln << "\\end{cellvarwidth}";
2988 if (getRotateCell(cell) != 0)
2989 os << breakln << "\\end{turn}";
2997 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2998 OutputParams const & runparams,
2999 list<col_type> columns,
3000 list<col_type> logical_columns) const
3002 if (!is_long_tabular)
3006 // output caption which is in no header or footer
3007 if (haveLTCaption()) {
3008 for (row_type r = 0; r < nrows(); ++r) {
3009 if (row_info[r].caption &&
3010 !row_info[r].endfirsthead && !row_info[r].endhead &&
3011 !row_info[r].endfoot && !row_info[r].endlastfoot)
3012 TeXRow(os, r, runparams, columns, logical_columns);
3015 // output first header info
3016 if (haveLTFirstHead()) {
3017 if (endfirsthead.topDL)
3019 for (row_type r = 0; r < nrows(); ++r) {
3020 if (row_info[r].endfirsthead)
3021 TeXRow(os, r, runparams, columns, logical_columns);
3023 if (endfirsthead.bottomDL)
3025 os << "\\endfirsthead\n";
3027 // output header info
3029 if (endfirsthead.empty && !haveLTFirstHead())
3030 os << "\\endfirsthead\n";
3033 for (row_type r = 0; r < nrows(); ++r) {
3034 if (row_info[r].endhead)
3035 TeXRow(os, r, runparams, columns, logical_columns);
3037 if (endhead.bottomDL)
3039 os << "\\endhead\n";
3041 // output footer info
3045 for (row_type r = 0; r < nrows(); ++r) {
3046 if (row_info[r].endfoot)
3047 TeXRow(os, r, runparams, columns, logical_columns);
3049 if (endfoot.bottomDL)
3051 os << "\\endfoot\n";
3052 if (endlastfoot.empty && !haveLTLastFoot())
3053 os << "\\endlastfoot\n";
3055 // output lastfooter info
3056 if (haveLTLastFoot()) {
3057 if (endlastfoot.topDL)
3059 for (row_type r = 0; r < nrows(); ++r) {
3060 if (row_info[r].endlastfoot)
3061 TeXRow(os, r, runparams, columns, logical_columns);
3063 if (endlastfoot.bottomDL)
3065 os << "\\endlastfoot\n";
3070 bool Tabular::isValidRow(row_type row) const
3072 if (!is_long_tabular)
3074 return !row_info[row].endhead && !row_info[row].endfirsthead
3075 && !row_info[row].endfoot && !row_info[row].endlastfoot
3076 && !row_info[row].caption;
3080 void Tabular::TeXRow(otexstream & os, row_type row,
3081 OutputParams const & runparams,
3082 list<col_type> columns, list<col_type> logical_columns) const
3084 //output the top line
3085 TeXTopHLine(os, row, columns, logical_columns);
3087 if (row_info[row].top_space_default) {
3089 os << "\\addlinespace\n";
3091 os << "\\noalign{\\vskip\\doublerulesep}\n";
3092 } else if(!row_info[row].top_space.zero()) {
3094 os << "\\addlinespace["
3095 << from_ascii(row_info[row].top_space.asLatexString())
3098 os << "\\noalign{\\vskip"
3099 << from_ascii(row_info[row].top_space.asLatexString())
3103 bool ismulticol = false;
3104 bool ismultirow = false;
3106 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3107 // Luabibdi (used by LuaTeX) behaves like classic
3108 bool const bidi_rtl =
3109 runparams.local_font->isRightToLeft()
3110 && runparams.useBidiPackage();
3111 bool const ct = !buffer().params().output_changes;
3113 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3115 for (auto const & c : columns) {
3116 if (isPartOfMultiColumn(row, c))
3119 idx_type cell = cellIndex(row, c);
3121 if (isPartOfMultiRow(row, c)
3122 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3123 if (cell != lastcell)
3128 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3129 InsetTableCell const * inset = cellInset(cell);
3131 Paragraph const & par = inset->paragraphs().front();
3133 os.texrow().forceStart(par.id(), 0);
3135 bool rtl = par.isRTL(buffer().params())
3137 && getPWidth(cell).zero()
3138 && !runparams.use_polyglossia;
3142 par.getParLanguage(buffer().params())->lang();
3143 if (lang == "farsi")
3145 else if (lang == "arabic_arabi")
3147 // currently, remaining RTL languages are
3148 // arabic_arabtex and hebrew
3152 // pass to the OutputParams that we are in a cell and
3153 // which alignment we have set.
3154 // InsetNewline needs this context information.
3155 OutputParams newrp(runparams);
3156 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3157 ? OutputParams::PLAIN
3158 : OutputParams::ALIGNED;
3160 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3161 // copy cell and split in 2
3162 InsetTableCell head = InsetTableCell(*cellInset(cell));
3163 head.setBuffer(const_cast<Buffer &>(buffer()));
3164 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3166 dit.push_back(CursorSlice(head));
3167 head.setMacrocontextPositionRecursive(dit);
3168 bool hassep = false;
3169 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3171 tail.setBuffer(head.buffer());
3173 dit.push_back(CursorSlice(tail));
3174 tail.setMacrocontextPositionRecursive(dit);
3178 tail.latex(os, newrp);
3181 head.latex(os, newrp);
3183 head.latex(os, newrp);
3186 tail.latex(os, newrp);
3189 } else if (ltCaption(row)) {
3190 // Inside longtable caption rows, we must only output the caption inset
3191 // with its content and omit anything outside of that (see #10791)
3192 InsetTableCell & tc_inset = *const_cast<InsetTableCell *>(inset);
3193 for (Inset const & it : tc_inset) {
3194 if (it.lyxCode() != CAPTION_CODE)
3196 it.latex(os, runparams);
3199 } else if (!isPartOfMultiRow(row, c)) {
3200 if (!runparams.nice)
3201 os.texrow().start(par.id(), 0);
3202 if (isMultiRow(cell) && !LaTeXFeatures::isAvailable("multirow-2021/01/29"))
3203 newrp.isNonLong = true;
3204 inset->latex(os, newrp);
3207 runparams.encoding = newrp.encoding;
3211 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3212 if (cell != lastcell) { // not last cell in row
3219 os << "\\tabularnewline";
3220 if (row_info[row].bottom_space_default) {
3222 os << "\\addlinespace";
3224 os << "[\\doublerulesep]";
3225 } else if (!row_info[row].bottom_space.zero()) {
3227 os << "\\addlinespace";
3229 << from_ascii(row_info[row].bottom_space.asLatexString())
3234 //output the bottom line
3235 TeXBottomHLine(os, row, columns, logical_columns);
3237 if (row_info[row].interline_space_default) {
3239 os << "\\addlinespace\n";
3241 os << "\\noalign{\\vskip\\doublerulesep}\n";
3242 } else if (!row_info[row].interline_space.zero()) {
3244 os << "\\addlinespace["
3245 << from_ascii(row_info[row].interline_space.asLatexString())
3248 os << "\\noalign{\\vskip"
3249 << from_ascii(row_info[row].interline_space.asLatexString())
3255 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3257 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3258 && !hasVarwidthColumn();
3259 bool const is_xltabular = is_long_tabular
3260 && (hasVarwidthColumn() || !tabular_width.zero());
3261 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3263 //+---------------------------------------------------------------------
3264 //+ first the opening preamble +
3265 //+---------------------------------------------------------------------
3268 if (!TexRow::isNone(pos))
3269 os.texrow().start(pos);
3272 if (is_long_tabular)
3273 os << "\\begin{landscape}\n";
3275 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3278 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3279 // order for RTL (#9686). Thus we use this list.
3280 bool const bidi_rtl =
3281 runparams.local_font->isRightToLeft()
3282 && runparams.useBidiPackage();
3283 list<col_type> columns;
3284 list<col_type> logical_columns;
3285 for (col_type cl = 0; cl < ncols(); ++cl) {
3286 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3289 columns.push_front(cl);
3291 columns.push_back(cl);
3292 // for some calculations, we need the logical (non-swapped)
3293 // columns also in bidi.
3294 logical_columns.push_back(cl);
3297 // If we use \cline or \cmidrule, we need to locally de-activate
3298 // the - character when using languages that activate it (e.g., Czech, Slovak).
3299 bool deactivate_chars = false;
3300 if ((runparams.use_babel || runparams.use_polyglossia)
3301 && contains(runparams.active_chars, '-')) {
3302 bool have_clines = false;
3303 // Check if we use \cline or \cmidrule
3304 for (row_type row = 0; row < nrows(); ++row) {
3305 col_type bset = 0, tset = 0;
3306 for (auto const & c : columns) {
3307 idx_type const idx = cellIndex(row, c);
3308 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3309 || topLineTrim(idx).first || topLineTrim(idx).second) {
3313 if (bottomLine(cellIndex(row, c)))
3315 if (topLine(cellIndex(row, c)))
3318 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3324 deactivate_chars = true;
3325 os << "\\begingroup\n"
3326 << "\\catcode`\\-=12\n";
3330 if (is_long_tabular) {
3332 os << "\\begin{xltabular}";
3334 os << "\\begin{longtable}";
3335 switch (longtabular_alignment) {
3336 case LYX_LONGTABULAR_ALIGN_LEFT:
3339 case LYX_LONGTABULAR_ALIGN_CENTER:
3342 case LYX_LONGTABULAR_ALIGN_RIGHT:
3347 if (tabular_width.zero())
3348 os << "{" << from_ascii("\\columnwidth") << "}";
3350 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3353 if (is_tabular_star)
3354 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3355 else if (hasVarwidthColumn()) {
3356 os << "\\begin{tabularx}{";
3357 if (tabular_width.zero())
3358 os << from_ascii("\\columnwidth") << "}";
3360 os << from_ascii(tabular_width.asLatexString()) << "}";
3362 os << "\\begin{tabular}";
3363 switch (tabular_valignment) {
3364 case LYX_VALIGN_TOP:
3367 case LYX_VALIGN_MIDDLE:
3369 case LYX_VALIGN_BOTTOM:
3377 if (is_tabular_star)
3378 os << "@{\\extracolsep{\\fill}}";
3380 for (auto const & c : columns) {
3381 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3383 if (!column_info[c].align_special.empty()) {
3384 os << column_info[c].align_special;
3386 if (!column_info[c].p_width.zero()) {
3387 bool decimal = false;
3388 switch (column_info[c].alignment) {
3389 case LYX_ALIGN_LEFT:
3390 os << ">{\\raggedright}";
3392 case LYX_ALIGN_RIGHT:
3393 os << ">{\\raggedleft}";
3395 case LYX_ALIGN_CENTER:
3396 os << ">{\\centering}";
3398 case LYX_ALIGN_NONE:
3399 case LYX_ALIGN_BLOCK:
3400 case LYX_ALIGN_LAYOUT:
3401 case LYX_ALIGN_SPECIAL:
3403 case LYX_ALIGN_DECIMAL: {
3405 os << ">{\\raggedright}";
3407 os << ">{\\raggedleft}";
3414 switch (column_info[c].valignment) {
3415 case LYX_VALIGN_TOP:
3416 // this is the default
3418 case LYX_VALIGN_MIDDLE:
3421 case LYX_VALIGN_BOTTOM:
3427 // Fixed-width cells with alignment at decimal separator
3428 // are output as two cells of half the width with the decimal
3429 // separator as column sep. This effectively puts the content
3430 // centered, which differs from the normal decimal sep alignment
3431 // and is not ideal, but we cannot do better ATM (see #9568).
3432 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3434 docstring const halffixedwith =
3435 from_ascii(Length(column_info[c].p_width.value() / 2,
3436 column_info[c].p_width.unit()).asLatexString());
3440 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3447 << from_ascii(column_info[c].p_width.asLatexString())
3449 } else if (column_info[c].varwidth) {
3450 switch (column_info[c].alignment) {
3451 case LYX_ALIGN_LEFT:
3452 os << ">{\\raggedright\\arraybackslash}";
3454 case LYX_ALIGN_RIGHT:
3455 os << ">{\\raggedleft\\arraybackslash}";
3457 case LYX_ALIGN_CENTER:
3458 os << ">{\\centering\\arraybackslash}";
3460 case LYX_ALIGN_NONE:
3461 case LYX_ALIGN_BLOCK:
3462 case LYX_ALIGN_LAYOUT:
3463 case LYX_ALIGN_SPECIAL:
3464 case LYX_ALIGN_DECIMAL:
3468 } else if (isVTypeColumn(c))
3469 os << "V{\\linewidth}";
3471 switch (column_info[c].alignment) {
3472 case LYX_ALIGN_LEFT:
3475 case LYX_ALIGN_RIGHT:
3478 case LYX_ALIGN_DECIMAL:
3479 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3485 } // end if else !column_info[i].p_width
3486 } // end if else !column_info[i].align_special
3487 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3492 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3494 //+---------------------------------------------------------------------
3495 //+ the single row and columns (cells) +
3496 //+---------------------------------------------------------------------
3498 for (row_type r = 0; r < nrows(); ++r) {
3499 if (!buffer().params().output_changes && row_info[r].change.deleted())
3501 if (isValidRow(r)) {
3502 TeXRow(os, r, runparams, columns, logical_columns);
3503 if (is_long_tabular && row_info[r].newpage)
3504 os << "\\newpage\n";
3508 //+---------------------------------------------------------------------
3509 //+ the closing of the tabular +
3510 //+---------------------------------------------------------------------
3512 if (is_long_tabular) {
3514 os << "\\end{xltabular}";
3516 os << "\\end{longtable}";
3518 if (is_tabular_star)
3519 os << "\\end{tabular*}";
3520 else if (hasVarwidthColumn())
3521 os << "\\end{tabularx}";
3523 os << "\\end{tabular}";
3526 if (deactivate_chars)
3528 os << "\n\\endgroup\n";
3531 if (is_long_tabular)
3532 os << breakln << "\\end{landscape}";
3534 os << breakln << "\\end{turn}";
3537 if (!TexRow::isNone(pos))
3538 os.texrow().start(pos);
3542 void Tabular::docbookRow(XMLStream & xs, row_type row,
3543 OutputParams const & runparams, bool header) const
3545 switch (buffer().params().docbook_table_output) {
3546 case BufferParams::HTMLTable:
3547 docbookRowAsHTML(xs, row, runparams, header);
3549 case BufferParams::CALSTable:
3550 docbookRowAsCALS(xs, row, runparams);
3556 void Tabular::docbookRowAsHTML(XMLStream & xs, row_type row,
3557 OutputParams const & runparams, bool header) const
3559 string const celltag = header ? "th" : "td";
3560 idx_type cell = getFirstCellInRow(row);
3562 xs << xml::StartTag("tr");
3564 for (col_type c = 0; c < ncols(); ++c) {
3565 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3570 Length const cwidth = column_info[c].p_width;
3571 if (!cwidth.zero()) {
3572 string const hwidth = cwidth.asHTMLString();
3573 attr << "style=\"width: " << hwidth << ";\" ";
3577 switch (getAlignment(cell)) {
3578 case LYX_ALIGN_BLOCK:
3581 case LYX_ALIGN_DECIMAL: {
3582 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3583 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3586 case LYX_ALIGN_LEFT:
3589 case LYX_ALIGN_RIGHT:
3597 attr << " valign='";
3598 switch (getVAlignment(cell)) {
3599 case LYX_VALIGN_TOP:
3602 case LYX_VALIGN_BOTTOM:
3605 case LYX_VALIGN_MIDDLE:
3610 if (isMultiColumn(cell))
3611 attr << " colspan='" << columnSpan(cell) << "'";
3612 else if (isMultiRow(cell))
3613 attr << " rowspan='" << rowSpan(cell) << "'";
3615 OutputParams rp = runparams;
3616 rp.docbook_in_par = false;
3617 rp.docbook_force_pars = true;
3618 xs << xml::StartTag(celltag, attr.str(), true);
3619 cellInset(cell)->docbook(xs, rp);
3620 xs << xml::EndTag(celltag);
3624 xs << xml::EndTag("tr");
3629 void Tabular::docbookRowAsCALS(XMLStream & xs, row_type row,
3630 OutputParams const & runparams) const
3632 idx_type cell = getFirstCellInRow(row);
3634 xs << xml::StartTag("row");
3636 for (col_type c = 0; c < ncols(); ++c) {
3637 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3643 switch (getAlignment(cell)) {
3644 case LYX_ALIGN_BLOCK:
3647 case LYX_ALIGN_DECIMAL: {
3648 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3649 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3652 case LYX_ALIGN_LEFT:
3655 case LYX_ALIGN_RIGHT:
3664 attr << " valign='";
3665 switch (getVAlignment(cell)) {
3666 case LYX_VALIGN_TOP:
3669 case LYX_VALIGN_BOTTOM:
3672 case LYX_VALIGN_MIDDLE:
3677 if (isMultiColumn(cell))
3678 attr << " colspan='" << columnSpan(cell) << "'";
3679 else if (isMultiRow(cell))
3680 attr << " rowspan='" << rowSpan(cell) << "'";
3682 attr << " colname='c" << (c + 1) << "'"; // Column numbering starts at 1.
3684 // All cases where there should be a line *below* this row.
3685 if (row_info[row].bottom_space_default)
3686 attr << " rowsep='1'";
3688 xs << xml::StartTag("entry", attr.str(), true);
3689 cellInset(cell)->docbook(xs, runparams);
3690 xs << xml::EndTag("entry");
3694 xs << xml::EndTag("row");
3699 void Tabular::docbook(XMLStream & xs, OutputParams const & runparams) const
3703 // Some tables are inline. Likely limitation: cannot output a table within a table; is that really a limitation?
3704 if (!runparams.docbook_in_table) { // Check on the *outer* set of parameters, so that the table can be closed
3705 // properly at the end of this function.
3706 xs << xml::StartTag("informaltable");
3710 // "Formal" tables have a title and use the tag <table>; the distinction with <informaltable> is done outside.
3711 // HTML has the caption first with titles forbidden, and CALS has a title first.
3712 if (haveLTCaption()) {
3713 std::string tag = ((buffer().params().docbook_table_output) == BufferParams::HTMLTable) ? "caption" : "title";
3715 xs << xml::StartTag(tag);
3716 for (row_type r = 0; r < nrows(); ++r)
3717 if (row_info[r].caption)
3718 docbookRow(xs, r, runparams);
3719 xs << xml::EndTag(tag);
3723 // CALS header: describe all columns in this table. For names, take 'c' then the ID of the column.
3724 // Start at one, as is customary with CALS!
3725 if (buffer().params().docbook_table_output == BufferParams::CALSTable) {
3726 for (col_type c = 0; c < ncols(); ++c) {
3727 std::stringstream attr;
3728 attr << "colnum='" << (c + 1) << "' ";
3729 attr << "colname='c" << (c + 1) << "' ";
3730 Length const cwidth = column_info[c].p_width;
3732 attr << "colwidth='" << cwidth.asHTMLString() << "' ";
3733 attr << "rowheader='norowheader'"; // Last attribute, hence no space at the end.
3735 xs << xml::CompTag("colspec", attr.str());
3740 // Output the header of the table. For both HTML and CALS, this is surrounded by a thead.
3741 bool const havefirsthead = haveLTFirstHead(false);
3742 // if we have a first head, then we are going to ignore the
3743 // headers for the additional pages, since there aren't any
3744 // in DocBook. this test accomplishes that.
3745 bool const havehead = !havefirsthead && haveLTHead(false);
3746 if (havehead || havefirsthead) {
3747 xs << xml::StartTag("thead") << xml::CR();
3748 for (row_type r = 0; r < nrows(); ++r) {
3749 if (((havefirsthead && row_info[r].endfirsthead) ||
3750 (havehead && row_info[r].endhead)) &&
3751 !row_info[r].caption) {
3752 docbookRow(xs, r, runparams, true);
3755 xs << xml::EndTag("thead");
3759 // Output the footer of the table. For both HTML and CALS, this is surrounded by a tfoot and output just after
3760 // the header (and before the body).
3761 bool const havelastfoot = haveLTLastFoot(false);
3763 bool const havefoot = !havelastfoot && haveLTFoot(false);
3764 if (havefoot || havelastfoot) {
3765 xs << xml::StartTag("tfoot") << xml::CR();
3766 for (row_type r = 0; r < nrows(); ++r) {
3767 if (((havelastfoot && row_info[r].endlastfoot) ||
3768 (havefoot && row_info[r].endfoot)) &&
3769 !row_info[r].caption) {
3770 docbookRow(xs, r, runparams); // TODO: HTML vs CALS
3773 xs << xml::EndTag("tfoot");
3777 // Output the main part of the table. The tbody container is mandatory for CALS, but optional for HTML (only if
3778 // there is no header and no footer). It never hurts to have it, though.
3779 xs << xml::StartTag("tbody");
3781 for (row_type r = 0; r < nrows(); ++r)
3783 docbookRow(xs, r, runparams);
3784 xs << xml::EndTag("tbody");
3787 // If this method started the table tag, also make it close it.
3788 if (!runparams.docbook_in_table) {
3789 xs << xml::EndTag("informaltable");
3795 docstring Tabular::xhtmlRow(XMLStream & xs, row_type row,
3796 OutputParams const & runparams, bool header) const
3799 string const celltag = header ? "th" : "td";
3800 idx_type cell = getFirstCellInRow(row);
3802 xs << xml::StartTag("tr");
3803 for (col_type c = 0; c < ncols(); ++c) {
3804 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3809 Length const cwidth = column_info[c].p_width;
3810 if (!cwidth.zero()) {
3811 string const hwidth = cwidth.asHTMLString();
3812 attr << "style=\"width: " << hwidth << ";\" ";
3816 switch (getAlignment(cell)) {
3817 case LYX_ALIGN_LEFT:
3820 case LYX_ALIGN_RIGHT:
3828 attr << " valign='";
3829 switch (getVAlignment(cell)) {
3830 case LYX_VALIGN_TOP:
3833 case LYX_VALIGN_BOTTOM:
3836 case LYX_VALIGN_MIDDLE:
3841 if (isMultiColumn(cell))
3842 attr << " colspan='" << columnSpan(cell) << "'";
3843 else if (isMultiRow(cell))
3844 attr << " rowspan='" << rowSpan(cell) << "'";
3846 xs << xml::StartTag(celltag, attr.str(), true) << xml::CR();
3847 ret += cellInset(cell)->xhtml(xs, runparams);
3848 xs << xml::EndTag(celltag) << xml::CR();
3851 xs << xml::EndTag("tr");
3856 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3860 if (is_long_tabular) {
3861 // we'll wrap it in a div, so as to deal with alignment
3863 switch (longtabular_alignment) {
3864 case LYX_LONGTABULAR_ALIGN_LEFT:
3867 case LYX_LONGTABULAR_ALIGN_CENTER:
3870 case LYX_LONGTABULAR_ALIGN_RIGHT:
3874 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
3876 // The caption flag wins over head/foot
3877 if (haveLTCaption()) {
3878 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
3880 for (row_type r = 0; r < nrows(); ++r)
3881 if (row_info[r].caption)
3882 ret += xhtmlRow(xs, r, runparams);
3883 xs << xml::EndTag("div");
3888 xs << xml::StartTag("table");
3891 // output header info
3892 bool const havefirsthead = haveLTFirstHead(false);
3893 // if we have a first head, then we are going to ignore the
3894 // headers for the additional pages, since there aren't any
3895 // in XHTML. this test accomplishes that.
3896 bool const havehead = !havefirsthead && haveLTHead(false);
3897 if (havehead || havefirsthead) {
3898 xs << xml::StartTag("thead");
3900 for (row_type r = 0; r < nrows(); ++r) {
3901 if (((havefirsthead && row_info[r].endfirsthead) ||
3902 (havehead && row_info[r].endhead)) &&
3903 !row_info[r].caption) {
3904 ret += xhtmlRow(xs, r, runparams, true);
3907 xs << xml::EndTag("thead");
3910 // output footer info
3911 bool const havelastfoot = haveLTLastFoot(false);
3913 bool const havefoot = !havelastfoot && haveLTFoot(false);
3914 if (havefoot || havelastfoot) {
3915 xs << xml::StartTag("tfoot") << xml::CR();
3916 for (row_type r = 0; r < nrows(); ++r) {
3917 if (((havelastfoot && row_info[r].endlastfoot) ||
3918 (havefoot && row_info[r].endfoot)) &&
3919 !row_info[r].caption) {
3920 ret += xhtmlRow(xs, r, runparams);
3923 xs << xml::EndTag("tfoot");
3927 xs << xml::StartTag("tbody") << xml::CR();
3928 for (row_type r = 0; r < nrows(); ++r)
3930 ret += xhtmlRow(xs, r, runparams);
3931 xs << xml::EndTag("tbody");
3933 xs << xml::EndTag("table");
3935 if (is_long_tabular) {
3936 xs << xml::EndTag("div");
3943 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3944 vector<unsigned int> const & clen) const
3946 idx_type const fcell = getFirstCellInRow(row);
3947 idx_type const n = numberOfCellsInRow(row) + fcell;
3950 for (idx_type i = fcell; i < n; ++i) {
3960 for (idx_type i = fcell; i < n; ++i) {
3971 col_type column = cellColumn(i);
3972 int len = clen[column];
3973 while (column < ncols() - 1
3974 && isPartOfMultiColumn(row, ++column))
3975 len += clen[column] + 4;
3976 os << docstring(len, ch);
3991 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3992 vector<unsigned int> const & clen) const
3994 idx_type const fcell = getFirstCellInRow(row);
3995 idx_type const n = numberOfCellsInRow(row) + fcell;
3998 for (idx_type i = fcell; i < n; ++i) {
3999 if (bottomLine(i)) {
4008 for (idx_type i = fcell; i < n; ++i) {
4009 if (bottomLine(i)) {
4019 col_type column = cellColumn(i);
4020 int len = clen[column];
4021 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4022 len += clen[column] + 4;
4023 os << docstring(len, ch);
4024 if (bottomLine(i)) {
4038 void Tabular::plaintextPrintCell(odocstringstream & os,
4039 OutputParams const & runparams,
4040 idx_type cell, row_type row, col_type column,
4041 vector<unsigned int> const & clen,
4042 bool onlydata, size_t max_length) const
4044 odocstringstream sstr;
4045 cellInset(cell)->plaintext(sstr, runparams, max_length);
4057 unsigned int len1 = sstr.str().length();
4058 unsigned int len2 = clen[column];
4059 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4060 len2 += clen[column] + 4;
4063 switch (getAlignment(cell)) {
4065 case LYX_ALIGN_LEFT:
4068 case LYX_ALIGN_RIGHT:
4072 case LYX_ALIGN_CENTER:
4078 os << docstring(len1, ' ') << sstr.str()
4079 << docstring(len2, ' ');
4081 if (rightLine(cell))
4088 void Tabular::plaintext(odocstringstream & os,
4089 OutputParams const & runparams, int const depth,
4090 bool onlydata, char_type delim, size_t max_length) const
4092 // first calculate the width of the single columns
4093 vector<unsigned int> clen(ncols());
4096 // first all non multicolumn cells!
4097 for (col_type c = 0; c < ncols(); ++c) {
4099 for (row_type r = 0; r < nrows(); ++r) {
4100 idx_type cell = cellIndex(r, c);
4101 if (isMultiColumn(cell))
4103 odocstringstream sstr;
4104 cellInset(cell)->plaintext(sstr, runparams, max_length);
4105 if (clen[c] < sstr.str().length())
4106 clen[c] = sstr.str().length();
4109 // then all multicolumn cells!
4110 for (col_type c = 0; c < ncols(); ++c) {
4111 for (row_type r = 0; r < nrows(); ++r) {
4112 idx_type cell = cellIndex(r, c);
4113 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
4115 odocstringstream sstr;
4116 cellInset(cell)->plaintext(sstr, runparams, max_length);
4117 int len = int(sstr.str().length());
4118 idx_type const n = columnSpan(cell);
4119 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
4121 if (len > int(clen[c + n - 1]))
4122 clen[c + n - 1] = len;
4127 for (row_type r = 0; r < nrows(); ++r) {
4128 if (!onlydata && plaintextTopHLine(os, r, clen))
4129 os << docstring(depth * 2, ' ');
4130 for (col_type c = 0; c < ncols(); ++c) {
4131 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
4133 if (onlydata && c > 0)
4134 // we don't use operator<< for single UCS4 character.
4135 // see explanation in docstream.h
4137 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
4139 if (os.str().size() > max_length)
4144 os << docstring(depth * 2, ' ');
4145 if (plaintextBottomHLine(os, r, clen))
4146 os << docstring(depth * 2, ' ');
4148 if (os.str().size() > max_length)
4154 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4156 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4160 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4162 return cell_info[row][column].inset;
4166 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4168 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4172 void Tabular::setCellInset(row_type row, col_type column,
4173 shared_ptr<InsetTableCell> ins)
4175 CellData & cd = cell_info[row][column];
4180 void Tabular::validate(LaTeXFeatures & features) const
4182 features.require("NeedTabularnewline");
4184 features.require("booktabs");
4185 if (is_long_tabular && !hasVarwidthColumn()) {
4186 if (tabular_width.zero())
4187 features.require("longtable");
4189 features.require("xltabular");
4191 if (rotate && is_long_tabular)
4192 features.require("lscape");
4194 features.require("rotating");
4195 if (hasVarwidthColumn()) {
4196 if (is_long_tabular)
4197 features.require("xltabular");
4199 features.require("tabularx");
4201 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4202 if (isMultiRow(cell))
4203 features.require("multirow");
4204 if (getUsebox(cell) == BOX_VARWIDTH) {
4205 features.require("varwidth");
4206 features.require("cellvarwidth");
4208 if (getVAlignment(cell) != LYX_VALIGN_TOP
4209 || !getPWidth(cell).zero()
4210 || isVTypeColumn(cellColumn(cell)))
4211 features.require("array");
4212 // Tell footnote that we need a savenote
4213 // environment in non-long tables or
4214 // longtable headers/footers
4215 else if (!is_long_tabular && !features.inFloat())
4216 features.saveNoteEnv("tabular");
4217 else if (!isValidRow(cellRow(cell)))
4218 features.saveNoteEnv("longtable");
4220 cellInset(cell)->validate(features);
4221 features.saveNoteEnv(string());
4226 Tabular::BoxType Tabular::useBox(idx_type cell) const
4228 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4229 if (parlist.size() > 1)
4230 return BOX_VARWIDTH;
4232 ParagraphList::const_iterator cit = parlist.begin();
4233 ParagraphList::const_iterator end = parlist.end();
4235 for (; cit != end; ++cit)
4236 for (int i = 0; i < cit->size(); ++i)
4237 if (cit->isNewline(i) || cit->layout().isEnvironment())
4238 return BOX_VARWIDTH;
4244 /////////////////////////////////////////////////////////////////////
4248 /////////////////////////////////////////////////////////////////////
4250 InsetTableCell::InsetTableCell(Buffer * buf)
4251 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false), isVarwidth(false),
4252 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4255 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4257 return isFixedWidth;
4261 bool InsetTableCell::forceLocalFontSwitch() const
4267 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4268 FuncStatus & status) const
4270 bool enabled = true;
4271 switch (cmd.action()) {
4272 case LFUN_INSET_SPLIT:
4273 case LFUN_INSET_DISSOLVE:
4276 case LFUN_MATH_DISPLAY:
4277 if (!hasFixedWidth()) {
4282 return InsetText::getStatus(cur, cmd, status);
4284 status.setEnabled(enabled);
4288 docstring InsetTableCell::asString(bool intoInsets)
4291 if (paragraphs().empty())
4293 ParagraphList::const_iterator it = paragraphs().begin();
4294 ParagraphList::const_iterator en = paragraphs().end();
4296 for (; it != en; ++it) {
4301 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4307 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4308 UpdateType utype, TocBackend & backend) const
4310 InsetText::iterateForToc(di, output_active, utype, backend);
4314 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4317 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4318 return InsetText::xhtml(xs, rp);
4322 void InsetTableCell::docbook(XMLStream & xs, OutputParams const & runparams) const
4324 InsetText::docbook(xs, runparams);
4328 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4330 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4331 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4333 // Hand font through to contained lyxtext:
4334 tm.font_.fontInfo() = mi.base.font;
4335 mi.base.textwidth -= horiz_offset;
4337 // This can happen when a layout has a left and right margin,
4338 // and the view is made very narrow. We can't do better than
4339 // to draw it partly out of view (bug 5890).
4340 if (mi.base.textwidth < 1)
4341 mi.base.textwidth = 1;
4343 // We tell metrics here not to expand on multiple pars
4344 // This is the difference to InsetText::Metrics
4345 Changer changetight = changeVar(mi.tight_insets, true);
4346 if (hasFixedWidth())
4347 tm.metrics(mi, dim, mi.base.textwidth);
4349 tm.metrics(mi, dim, 0);
4350 mi.base.textwidth += horiz_offset;
4351 dim.asc += topOffset(mi.base.bv);
4352 dim.des += bottomOffset(mi.base.bv);
4353 dim.wid += horiz_offset;
4357 /////////////////////////////////////////////////////////////////////
4361 /////////////////////////////////////////////////////////////////////
4363 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4365 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4366 rowselect_(false), colselect_(false)
4371 InsetTabular::InsetTabular(InsetTabular const & tab)
4372 : Inset(tab), tabular(tab.tabular),
4373 rowselect_(false), colselect_(false)
4378 InsetTabular::~InsetTabular()
4380 hideDialogs("tabular", this);
4384 void InsetTabular::setBuffer(Buffer & buf)
4386 tabular.setBuffer(buf);
4387 Inset::setBuffer(buf);
4391 bool InsetTabular::insetAllowed(InsetCode code) const
4396 case MATHMACRO_CODE:
4401 return tabular.is_long_tabular;
4409 bool InsetTabular::allowMultiPar() const
4411 for (col_type c = 0; c < tabular.ncols(); ++c) {
4412 for (row_type r = 0; r < tabular.nrows(); ++r) {
4413 if (tabular.cellInset(r,c)->allowMultiPar())
4421 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4423 return tabular.is_long_tabular &&
4424 (newtype == "Standard" || newtype == "Unnumbered");
4428 void InsetTabular::write(ostream & os) const
4430 os << "Tabular" << endl;
4435 string InsetTabular::contextMenu(BufferView const &, int, int) const
4437 // FIXME: depending on the selection state,
4438 // we could offer a different menu.
4439 return cell(0)->contextMenuName() + ";" + contextMenuName();
4443 string InsetTabular::contextMenuName() const
4445 return "context-tabular";
4449 void InsetTabular::read(Lexer & lex)
4451 //bool const old_format = (lex.getString() == "\\LyXTable");
4459 string token = lex.getString();
4460 while (lex && token != "\\end_inset") {
4462 token = lex.getString();
4465 lex.printError("Missing \\end_inset at this point. ");
4469 int InsetTabular::rowFromY(Cursor & cur, int y) const
4471 // top y coordinate of tabular
4472 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4474 for (; r < tabular.nrows() && y > h; ++r)
4475 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4476 + tabular.interRowSpace(r);
4482 int InsetTabular::columnFromX(Cursor & cur, int x) const
4484 // left x coordinate of tabular
4485 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4487 for (; c < tabular.ncols() && x > w; ++c)
4488 w += tabular.cellWidth(c);
4493 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4495 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4496 // mi.base.textwidth << "\n";
4497 LBUFERR(mi.base.bv);
4499 for (row_type r = 0; r < tabular.nrows(); ++r) {
4502 for (col_type c = 0; c < tabular.ncols(); ++c) {
4503 if (tabular.isPartOfMultiColumn(r, c)
4504 || tabular.isPartOfMultiRow(r, c))
4505 // multicolumn or multirow cell, but not first one
4507 idx_type const cell = tabular.cellIndex(r, c);
4510 Length const p_width = tabular.getPWidth(cell);
4511 if (!p_width.zero())
4512 m.base.textwidth = mi.base.inPixels(p_width);
4513 else if (tabular.column_info[c].varwidth)
4514 m.base.textwidth = tabular.column_info[c].width;
4515 tabular.cellInset(cell)->metrics(m, dim0);
4516 if (!p_width.zero() || tabular.column_info[c].varwidth)
4517 dim0.wid = m.base.textwidth;
4518 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4519 + tabular.interColumnSpace(cell);
4521 // FIXME(?): do we need a second metrics call?
4522 TextMetrics const & tm =
4523 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4525 // determine horizontal offset because of decimal align (if necessary)
4526 int decimal_width = 0;
4527 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4528 InsetTableCell tail = *tabular.cellInset(cell);
4529 tail.setBuffer(tabular.buffer());
4530 // we need to set macrocontext position everywhere
4531 // otherwise we crash with nested insets (e.g. footnotes)
4532 // after decimal point
4533 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4535 dit.push_back(CursorSlice(tail));
4536 tail.setMacrocontextPositionRecursive(dit);
4538 // remove text leading decimal point
4539 docstring const align_d = tabular.column_info[c].decimal_point;
4540 dit = separatorPos(&tail, align_d);
4542 pos_type const psize = tail.paragraphs().front().size();
4544 tail.paragraphs().front().eraseChars(0,
4545 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4547 tail.metrics(m, dim1);
4548 decimal_width = dim1.width();
4551 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4552 tabular.cell_info[r][c].decimal_width = decimal_width;
4554 // with LYX_VALIGN_BOTTOM the descent is relative to the last
4555 // row of the last par (note that the par might have multile rows!)
4556 // = descent of text in last row + bottomOffset:
4557 int const lastpardes = tm.last().second->rows().back().descent()
4558 + bottomOffset(mi.base.bv);
4560 switch (tabular.getVAlignment(cell)) {
4561 case Tabular::LYX_VALIGN_TOP:
4563 case Tabular::LYX_VALIGN_MIDDLE:
4564 offset = -(dim0.des - lastpardes)/2;
4566 case Tabular::LYX_VALIGN_BOTTOM:
4567 offset = -(dim0.des - lastpardes);
4570 tabular.cell_info[r][c].voffset = offset;
4571 maxasc = max(maxasc, dim0.asc - offset);
4572 maxdes = max(maxdes, dim0.des + offset);
4574 int const top_space = tabular.row_info[r].top_space_default ?
4575 default_line_space :
4576 mi.base.inPixels(tabular.row_info[r].top_space);
4577 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4578 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4579 default_line_space :
4580 mi.base.inPixels(tabular.row_info[r].bottom_space);
4581 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4584 // We need to recalculate the metrics after column width calculation
4585 // with xtabular (possibly multiple times, so the call is recursive).
4586 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4588 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4589 dim.des = tabular.height() - dim.asc;
4590 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4594 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4596 if (&cur.inset() == this && cur.selection()) {
4597 if (cur.selIsMultiCell()) {
4600 getSelection(cur, rs, re, cs, ce);
4602 idx_type const cell = tabular.cellIndex(row, col);
4603 col_type const cspan = tabular.columnSpan(cell);
4604 row_type const rspan = tabular.rowSpan(cell);
4605 if (col + cspan - 1 >= cs && col <= ce
4606 && row + rspan - 1 >= rs && row <= re)
4609 if (col == tabular.cellColumn(cur.idx())
4610 && row == tabular.cellRow(cur.idx())) {
4611 CursorSlice const & beg = cur.selBegin();
4612 CursorSlice const & end = cur.selEnd();
4614 if ((end.lastpos() > 0 || end.lastpit() > 0)
4615 && end.pos() == end.lastpos() && beg.pos() == 0
4616 && end.pit() == end.lastpit() && beg.pit() == 0)
4624 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4626 x += ADD_TO_TABULAR_WIDTH;
4628 BufferView * bv = pi.base.bv;
4629 Cursor & cur = pi.base.bv->cursor();
4631 // FIXME: As the full background is painted in drawBackground(),
4632 // we have no choice but to do a full repaint for the Text cells.
4633 pi.full_repaint = true;
4635 bool const original_selection_state = pi.selected;
4639 // Save tabular change status
4640 Change tab_change = pi.change;
4642 int yy = y + tabular.offsetVAlignment();
4643 for (row_type r = 0; r < tabular.nrows(); ++r) {
4645 for (col_type c = 0; c < tabular.ncols(); ++c) {
4646 if (tabular.isPartOfMultiColumn(r, c))
4649 idx = tabular.cellIndex(r, c);
4651 if (tabular.isPartOfMultiRow(r, c)) {
4652 nx += tabular.cellWidth(idx);
4656 pi.selected |= isCellSelected(cur, r, c);
4658 // Mark deleted rows/columns
4659 if (tabular.column_info[c].change.changed())
4660 pi.change = tabular.column_info[c].change;
4661 else if (tabular.row_info[r].change.changed())
4662 pi.change = tabular.row_info[r].change;
4664 pi.change = tab_change;
4666 int const cx = nx + tabular.textHOffset(idx);
4667 int const cy = yy + tabular.textVOffset(idx);
4668 // Cache the Inset position.
4669 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4670 cell(idx)->draw(pi, cx, cy);
4671 drawCellLines(pi, nx, yy, r, idx);
4672 nx += tabular.cellWidth(idx);
4673 pi.selected = original_selection_state;
4676 if (r + 1 < tabular.nrows())
4677 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4678 + tabular.interRowSpace(r + 1);
4683 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4685 x += ADD_TO_TABULAR_WIDTH;
4686 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4687 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4688 pi.backgroundColor(this));
4692 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4694 Cursor & cur = pi.base.bv->cursor();
4696 x += ADD_TO_TABULAR_WIDTH;
4698 if (!cur.selection())
4700 if (&cur.inset() != this)
4705 bool const full_cell_selected = isCellSelected(cur,
4706 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4708 if (cur.selIsMultiCell() || full_cell_selected) {
4709 for (row_type r = 0; r < tabular.nrows(); ++r) {
4711 for (col_type c = 0; c < tabular.ncols(); ++c) {
4712 if (tabular.isPartOfMultiColumn(r, c))
4715 idx_type const cell = tabular.cellIndex(r, c);
4717 if (tabular.isPartOfMultiRow(r, c)) {
4718 xx += tabular.cellWidth(cell);
4721 int const w = tabular.cellWidth(cell);
4722 int const h = tabular.cellHeight(cell);
4723 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4724 if (isCellSelected(cur, r, c))
4725 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4728 if (r + 1 < tabular.nrows())
4729 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4730 + tabular.interRowSpace(r + 1);
4734 // FIXME: This code has no effect because InsetTableCell does not handle
4735 // drawSelection other than the trivial implementation in Inset.
4737 // x += cellXPos(cur.idx());
4738 // x += tabular.textHOffset(cur.idx());
4739 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4746 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4747 Color const incol, bool drawline, bool heavy = false)
4749 Color const col = drawline ? incol : Color_tabularonoffline;
4750 if (drawline && lt > 0)
4751 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4752 Painter::line_onoffdash,
4753 Painter::thin_line);
4754 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4755 drawline ? Painter::line_solid : Painter::line_onoffdash,
4756 (heavy ? 2 : 1) * Painter::thin_line);
4757 if (drawline && rt > 0)
4758 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4759 Painter::line_onoffdash,
4760 Painter::thin_line);
4766 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4767 row_type row, idx_type cell) const
4769 y -= tabular.rowAscent(row);
4770 int const w = tabular.cellWidth(cell);
4771 int const h = tabular.cellHeight(cell);
4775 col_type const col = tabular.cellColumn(cell);
4777 // Colour the frame if rows/columns are added or deleted
4778 Color colour = Color_tabularline;
4779 if (tabular.column_info[col].change.changed()
4780 || tabular.row_info[row].change.changed())
4781 colour = tabular.cellInset(cell)->paragraphs().front().lookupChange(0).color();
4784 bool drawline = tabular.topLine(cell)
4785 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4786 bool heavy = tabular.use_booktabs
4787 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4788 && tabular.rowTopLine(row);
4789 if (tabular.topLineTrim(cell).first
4790 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4792 if (tabular.topLineTrim(cell).second
4793 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4795 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4799 drawline = tabular.bottomLine(cell);
4800 row_type const lastrow = tabular.nrows() - 1;
4801 // Consider multi-rows
4803 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4804 && tabular.isPartOfMultiRow(r + 1, col))
4806 heavy = tabular.use_booktabs
4807 && ((row == lastrow && tabular.rowBottomLine(row))
4808 || (r == lastrow && tabular.rowBottomLine(r)));
4809 if (tabular.bottomLineTrim(cell).first)
4811 if (tabular.bottomLineTrim(cell).second)
4813 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4816 drawline = tabular.leftLine(cell)
4817 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4818 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4821 x -= tabular.interColumnSpace(cell);
4822 col_type next_cell_col = col + 1;
4823 while (next_cell_col < tabular.ncols()
4824 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4826 drawline = tabular.rightLine(cell)
4827 || (next_cell_col < tabular.ncols()
4828 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4829 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4833 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4835 //lyxerr << "InsetTabular::edit: " << this << endl;
4839 if (isRightToLeft(cur))
4840 cur.idx() = tabular.getLastCellInRow(0);
4846 if (isRightToLeft(cur))
4847 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4849 cur.idx() = tabular.numberofcells - 1;
4851 cur.pos() = cur.lastpos(); // FIXME crude guess
4853 cur.setCurrentFont();
4854 // FIXME: this accesses the position cache before it is initialized
4855 //cur.bv().fitCursor();
4859 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4861 // In a longtable, tell captions what the current float is
4862 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4863 string const saveflt = cnts.current_float();
4864 if (tabular.is_long_tabular) {
4865 cnts.current_float("table");
4866 // in longtables, we only step the counter once
4867 cnts.step(from_ascii("table"), utype);
4868 cnts.isLongtable(true);
4871 ParIterator it2 = it;
4873 size_t const end = it2.nargs();
4874 for ( ; it2.idx() < end; it2.top().forwardIdx())
4875 buffer().updateBuffer(it2, utype);
4878 if (tabular.is_long_tabular) {
4879 cnts.current_float(saveflt);
4880 cnts.isLongtable(false);
4885 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4886 UpdateType utype, TocBackend & backend) const
4888 DocIterator dit = cpit;
4890 size_t const end = dit.nargs();
4891 for ( ; dit.idx() < end; dit.top().forwardIdx())
4892 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4896 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4898 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4899 return x < x0 || x > x0 + tabular.width();
4903 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4905 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4906 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4907 // there is no margin added vertically to tabular insets.
4908 // However, it works for now.
4909 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4913 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4915 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4919 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4921 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4922 << "\n cur:" << cur);
4923 CursorSlice sl = cur.top();
4924 Cursor & bvcur = cur.bv().cursor();
4926 FuncCode const act = cmd.action();
4930 case LFUN_MOUSE_PRESS: {
4931 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4933 if (hitSelectRow(cur.bv(), cmd.x())) {
4934 row_type r = rowFromY(cur, cmd.y());
4935 cur.idx() = tabular.getFirstCellInRow(r);
4939 cur.idx() = tabular.getLastCellInRow(r);
4940 cur.pit() = cur.lastpit();
4941 cur.pos() = cur.lastpos();
4942 cur.selection(true);
4948 if (hitSelectColumn(cur.bv(), cmd.y())) {
4949 col_type c = columnFromX(cur, cmd.x());
4950 cur.idx() = tabular.cellIndex(0, c);
4954 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4955 cur.pit() = cur.lastpit();
4956 cur.pos() = cur.lastpos();
4957 cur.selection(true);
4962 // do not reset cursor/selection if we have selected
4963 // some cells (bug 2715).
4964 if (cmd.button() == mouse_button::button3
4965 && &bvcur.selBegin().inset() == this
4966 && bvcur.selIsMultiCell())
4969 // Let InsetTableCell do it
4970 cell(cur.idx())->dispatch(cur, cmd);
4973 case LFUN_MOUSE_MOTION:
4974 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4975 if (cmd.button() == mouse_button::button1) {
4976 // only accept motions to places not deeper nested than the real anchor
4977 if (!bvcur.realAnchor().hasPart(cur)) {
4981 // select (additional) row
4983 row_type r = rowFromY(cur, cmd.y());
4984 cur.idx() = tabular.getLastCellInRow(r);
4985 // we need to reset the cursor's pit and pos now, as the old ones
4986 // may no longer be valid.
4989 bvcur.setCursor(cur);
4990 bvcur.selection(true);
4993 // select (additional) column
4995 col_type c = columnFromX(cur, cmd.x());
4996 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4997 // we need to reset the cursor's pit and pos now, as the old ones
4998 // may no longer be valid.
5001 bvcur.setCursor(cur);
5002 bvcur.selection(true);
5005 // only update if selection changes
5006 if (bvcur.idx() == cur.idx() &&
5007 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
5008 cur.noScreenUpdate();
5009 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
5010 bvcur.setCursor(cur);
5011 bvcur.selection(true);
5012 // if this is a multicell selection, we just set the cursor to
5013 // the beginning of the cell's text.
5014 if (bvcur.selIsMultiCell()) {
5015 bvcur.pit() = bvcur.lastpit();
5016 bvcur.pos() = bvcur.lastpos();
5021 case LFUN_MOUSE_RELEASE:
5026 case LFUN_CELL_BACKWARD:
5028 cur.selection(false);
5031 case LFUN_CELL_FORWARD:
5033 cur.selection(false);
5036 case LFUN_CHAR_FORWARD_SELECT:
5037 case LFUN_CHAR_FORWARD:
5038 case LFUN_CHAR_BACKWARD_SELECT:
5039 case LFUN_CHAR_BACKWARD:
5040 case LFUN_CHAR_RIGHT_SELECT:
5041 case LFUN_CHAR_RIGHT:
5042 case LFUN_CHAR_LEFT_SELECT:
5043 case LFUN_CHAR_LEFT:
5044 case LFUN_WORD_FORWARD:
5045 case LFUN_WORD_FORWARD_SELECT:
5046 case LFUN_WORD_BACKWARD:
5047 case LFUN_WORD_BACKWARD_SELECT:
5048 case LFUN_WORD_RIGHT:
5049 case LFUN_WORD_RIGHT_SELECT:
5050 case LFUN_WORD_LEFT:
5051 case LFUN_WORD_LEFT_SELECT: {
5052 // determine whether we move to next or previous cell, where to enter
5053 // the new cell from, and which command to "finish" (i.e., exit the
5056 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
5057 FuncCode finish_lfun;
5059 if (act == LFUN_CHAR_FORWARD
5060 || act == LFUN_CHAR_FORWARD_SELECT
5061 || act == LFUN_WORD_FORWARD
5062 || act == LFUN_WORD_FORWARD_SELECT) {
5064 finish_lfun = LFUN_FINISHED_FORWARD;
5066 else if (act == LFUN_CHAR_BACKWARD
5067 || act == LFUN_CHAR_BACKWARD_SELECT
5068 || act == LFUN_WORD_BACKWARD
5069 || act == LFUN_WORD_BACKWARD_SELECT) {
5071 finish_lfun = LFUN_FINISHED_BACKWARD;
5073 // LEFT or RIGHT commands --- the interpretation will depend on the
5074 // table's direction.
5076 bool const right = act == LFUN_CHAR_RIGHT
5077 || act == LFUN_CHAR_RIGHT_SELECT
5078 || act == LFUN_WORD_RIGHT
5079 || act == LFUN_WORD_RIGHT_SELECT;
5080 next_cell = isRightToLeft(cur) != right;
5082 if (lyxrc.visual_cursor)
5083 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
5085 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
5088 bool const select = act == LFUN_CHAR_FORWARD_SELECT
5089 || act == LFUN_CHAR_BACKWARD_SELECT
5090 || act == LFUN_CHAR_RIGHT_SELECT
5091 || act == LFUN_CHAR_LEFT_SELECT
5092 || act == LFUN_WORD_FORWARD_SELECT
5093 || act == LFUN_WORD_RIGHT_SELECT
5094 || act == LFUN_WORD_BACKWARD_SELECT
5095 || act == LFUN_WORD_LEFT_SELECT;
5097 // If we have a multicell selection or we're
5098 // not doing some LFUN_*_SELECT thing anyway...
5099 if (!cur.selIsMultiCell() || !select) {
5100 col_type const c = tabular.cellColumn(cur.idx());
5101 row_type const r = tabular.cellRow(cur.idx());
5102 // Are we trying to select the whole cell and is the whole cell
5103 // not yet selected?
5104 bool const select_whole = select && !isCellSelected(cur, r, c) &&
5105 ((next_cell && cur.pit() == cur.lastpit()
5106 && cur.pos() == cur.lastpos())
5107 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
5109 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
5111 // ...try to dispatch to the cell's inset.
5112 cell(cur.idx())->dispatch(cur, cmd);
5114 // When we already have a selection we want to select the whole cell
5115 // before going to the next cell.
5116 if (select_whole && !empty_cell){
5117 getText(cur.idx())->selectAll(cur);
5119 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5123 // FIXME: When we support the selection of an empty cell, remove
5124 // the !empty_cell from this condition. For now we jump to the next
5125 // cell if the current cell is empty.
5126 if (cur.result().dispatched() && !empty_cell)
5130 // move to next/prev cell, as appropriate
5131 // note that we will always do this if we're selecting and we have
5132 // a multicell selection
5133 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
5134 << " cell from: " << int(entry_from));
5136 moveNextCell(cur, entry_from);
5138 movePrevCell(cur, entry_from);
5139 // if we're exiting the table, call the appropriate FINISHED lfun
5140 if (sl == cur.top()) {
5141 cmd = FuncRequest(finish_lfun);
5146 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5151 case LFUN_DOWN_SELECT:
5153 if (!(cur.selection() && cur.selIsMultiCell()))
5154 cell(cur.idx())->dispatch(cur, cmd);
5156 cur.dispatched(); // override the cell's decision
5157 if (sl == cur.top()) {
5158 // if our Text didn't do anything to the cursor
5159 // then we try to put the cursor into the cell below
5160 // setting also the right targetX.
5161 cur.selHandle(act == LFUN_DOWN_SELECT);
5162 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
5163 int const xtarget = cur.targetX();
5164 // WARNING: Once cur.idx() has been reset, the cursor is in
5165 // an inconsistent state until pos() has been set. Be careful
5166 // what you do with it!
5167 cur.idx() = tabular.cellBelow(cur.idx());
5169 TextMetrics const & tm =
5170 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5171 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5172 cur.setCurrentFont();
5175 if (sl == cur.top()) {
5176 // we trick it to go to forward after leaving the
5178 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5181 if (cur.selIsMultiCell()) {
5182 cur.pit() = cur.lastpit();
5183 cur.pos() = cur.lastpos();
5184 cur.setCurrentFont();
5185 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5188 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5191 case LFUN_UP_SELECT:
5193 if (!(cur.selection() && cur.selIsMultiCell()))
5194 cell(cur.idx())->dispatch(cur, cmd);
5195 cur.dispatched(); // override the cell's decision
5196 if (sl == cur.top()) {
5197 // if our Text didn't do anything to the cursor
5198 // then we try to put the cursor into the cell above
5199 // setting also the right targetX.
5200 cur.selHandle(act == LFUN_UP_SELECT);
5201 if (tabular.cellRow(cur.idx()) != 0) {
5202 int const xtarget = cur.targetX();
5203 // WARNING: Once cur.idx() has been reset, the cursor is in
5204 // an inconsistent state until pos() has been set. Be careful
5205 // what you do with it!
5206 cur.idx() = tabular.cellAbove(cur.idx());
5207 cur.pit() = cur.lastpit();
5208 Text const * text = cell(cur.idx())->getText(0);
5209 TextMetrics const & tm = cur.bv().textMetrics(text);
5210 ParagraphMetrics const & pm =
5211 tm.parMetrics(cur.lastpit());
5212 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5213 cur.setCurrentFont();
5216 if (sl == cur.top()) {
5217 cmd = FuncRequest(LFUN_UP);
5220 if (cur.selIsMultiCell()) {
5222 cur.pos() = cur.lastpos();
5223 cur.setCurrentFont();
5224 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5227 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5230 case LFUN_LAYOUT_TABULAR:
5231 cur.bv().showDialog("tabular");
5234 case LFUN_INSET_MODIFY:
5235 // we come from the dialog
5236 if (cmd.getArg(0) == "tabular")
5237 tabularFeatures(cur, cmd.getLongArg(1));
5242 case LFUN_TABULAR_FEATURE:
5243 tabularFeatures(cur, to_utf8(cmd.argument()));
5246 // insert file functions
5247 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5248 case LFUN_FILE_INSERT_PLAINTEXT:
5250 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5251 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5252 FileName(to_utf8(cmd.argument())));
5255 cur.recordUndoInset();
5256 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5257 // content has been replaced,
5258 // so cursor might be invalid
5259 cur.pos() = cur.lastpos();
5260 cur.pit() = cur.lastpit();
5261 bvcur.setCursor(cur);
5268 if (cur.selIsMultiCell())
5271 cell(cur.idx())->dispatch(cur, cmd);
5275 if (cur.selIsMultiCell()) {
5276 if (copySelection(cur)) {
5277 cur.recordUndoInset();
5281 cell(cur.idx())->dispatch(cur, cmd);
5284 case LFUN_SELF_INSERT:
5285 if (cur.selIsMultiCell()) {
5286 cur.recordUndoInset();
5288 BufferView * bv = &cur.bv();
5289 docstring::const_iterator cit = cmd.argument().begin();
5290 docstring::const_iterator const end = cmd.argument().end();
5291 for (; cit != end; ++cit)
5292 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5295 bv->bookmarkEditPosition();
5297 cell(cur.idx())->dispatch(cur, cmd);
5300 case LFUN_CHAR_DELETE_BACKWARD:
5301 case LFUN_CHAR_DELETE_FORWARD:
5302 if (cur.selIsMultiCell()) {
5303 cur.recordUndoInset();
5306 cell(cur.idx())->dispatch(cur, cmd);
5309 case LFUN_CLIPBOARD_PASTE:
5310 case LFUN_PRIMARY_SELECTION_PASTE: {
5311 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5312 theClipboard().getAsText(Clipboard::PlainTextType) :
5313 theSelection().get();
5316 // pass to InsertPlaintextString, but
5317 // only if we have multi-cell content
5318 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5319 cur.recordUndoInset();
5320 if (insertPlaintextString(cur.bv(), clip, false)) {
5321 // content has been replaced,
5322 // so cursor might be invalid
5324 bvcur.setCursor(cur);
5328 // Let the cell handle normal text
5329 cell(cur.idx())->dispatch(cur, cmd);
5334 if (!tabularStackDirty()) {
5335 // Check if we have plain text or HTML with rows/columns.
5336 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5337 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5338 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5339 docstring const clip =
5340 theClipboard().getAsText(Clipboard::AnyTextType);
5341 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5342 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5343 doDispatch(cur, ncmd);
5347 else if (!theClipboard().isInternal()
5348 && theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5349 // This might be tabular data from another LyX instance. Check!
5350 docstring const clip =
5351 theClipboard().getAsText(Clipboard::PlainTextType);
5352 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5353 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5354 doDispatch(cur, ncmd);
5358 if (!cur.selIsMultiCell())
5359 cell(cur.idx())->dispatch(cur, cmd);
5362 if (theClipboard().isInternal()) {
5363 cur.recordUndoInset();
5364 pasteClipboard(cur);
5368 case LFUN_CHANGE_ACCEPT:
5369 case LFUN_CHANGE_REJECT:
5370 case LFUN_FONT_EMPH:
5371 case LFUN_FONT_BOLD:
5372 case LFUN_FONT_BOLDSYMBOL:
5373 case LFUN_FONT_ROMAN:
5374 case LFUN_FONT_NOUN:
5375 case LFUN_FONT_ITAL:
5376 case LFUN_FONT_FRAK:
5377 case LFUN_FONT_TYPEWRITER:
5378 case LFUN_FONT_SANS:
5379 case LFUN_TEXTSTYLE_APPLY:
5380 case LFUN_TEXTSTYLE_UPDATE:
5381 case LFUN_FONT_SIZE:
5382 case LFUN_FONT_UNDERLINE:
5383 case LFUN_FONT_STRIKEOUT:
5384 case LFUN_FONT_CROSSOUT:
5385 case LFUN_FONT_UNDERUNDERLINE:
5386 case LFUN_FONT_UNDERWAVE:
5388 case LFUN_PARAGRAPH_PARAMS_APPLY:
5389 case LFUN_PARAGRAPH_PARAMS:
5390 case LFUN_WORD_CAPITALIZE:
5391 case LFUN_WORD_UPCASE:
5392 case LFUN_WORD_LOWCASE:
5393 case LFUN_CHARS_TRANSPOSE: {
5394 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5395 if (cur.selIsMultiCell()) {
5398 getSelection(cur, rs, re, cs, ce);
5399 Cursor tmpcur = cur;
5400 for (row_type r = rs; r <= re; ++r) {
5401 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5402 // whole row selected
5403 if (act == LFUN_CHANGE_ACCEPT) {
5404 if (tabular.row_info[r].change.inserted())
5405 tabular.row_info[r].change.setUnchanged();
5406 else if (tabular.row_info[r].change.deleted()) {
5407 tabular.deleteRow(r, true);
5412 if (tabular.row_info[r].change.deleted())
5413 tabular.row_info[r].change.setUnchanged();
5414 else if (tabular.row_info[r].change.inserted()) {
5415 tabular.deleteRow(r, true);
5421 for (col_type c = cs; c <= ce; ++c) {
5422 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5423 // whole col selected
5424 if (act == LFUN_CHANGE_ACCEPT) {
5425 if (tabular.column_info[c].change.inserted())
5426 tabular.column_info[c].change.setUnchanged();
5427 else if (tabular.column_info[c].change.deleted()) {
5428 tabular.deleteColumn(c, true);
5433 if (tabular.column_info[c].change.deleted())
5434 tabular.column_info[c].change.setUnchanged();
5435 else if (tabular.column_info[c].change.inserted()) {
5436 tabular.deleteColumn(c, true);
5442 // cursor follows cell:
5443 tmpcur.idx() = tabular.cellIndex(r, c);
5444 // select this cell only:
5447 tmpcur.resetAnchor();
5448 tmpcur.pit() = tmpcur.lastpit();
5449 tmpcur.pos() = tmpcur.top().lastpos();
5450 tmpcur.setCursor(tmpcur);
5451 tmpcur.setSelection();
5452 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5456 tabular.updateIndexes();
5457 // cursor might be invalid
5459 // change bar might need to be redrawn
5460 cur.screenUpdateFlags(Update::Force);
5461 cur.forceBufferUpdate();
5465 cell(cur.idx())->dispatch(cur, cmd);
5470 case LFUN_CHANGE_NEXT:
5471 case LFUN_CHANGE_PREVIOUS: {
5472 // BufferView::dispatch has already moved the cursor, we just
5473 // need to select here if we have a changed row or column
5474 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5476 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5480 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5481 cur.pit() = cur.lastpit();
5482 cur.pos() = cur.lastpos();
5483 cur.selection(true);
5487 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5489 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5493 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5494 cur.pit() = cur.lastpit();
5495 cur.pos() = cur.lastpos();
5496 cur.selection(true);
5503 case LFUN_INSET_SETTINGS:
5504 // relay this lfun to Inset, not to the cell.
5505 Inset::doDispatch(cur, cmd);
5509 // we try to handle this event in the insets dispatch function.
5510 cell(cur.idx())->dispatch(cur, cmd);
5516 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5517 string const & argument, FuncStatus & status) const
5520 int action = Tabular::LAST_ACTION;
5522 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5523 if (tabularFeature[i].feature == s) {
5524 action = tabularFeature[i].action;
5528 if (action == Tabular::LAST_ACTION) {
5530 status.setUnknown(true);
5534 row_type sel_row_start = 0;
5535 row_type sel_row_end = 0;
5536 col_type sel_col_start = 0;
5537 col_type sel_col_end = 0;
5538 Tabular::ltType dummyltt;
5541 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5544 case Tabular::SET_PWIDTH:
5545 case Tabular::SET_MPWIDTH:
5546 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5547 case Tabular::SET_SPECIAL_COLUMN:
5548 case Tabular::SET_SPECIAL_MULTICOLUMN:
5549 case Tabular::APPEND_ROW:
5550 case Tabular::APPEND_COLUMN:
5551 case Tabular::COPY_ROW:
5552 case Tabular::COPY_COLUMN:
5553 case Tabular::SET_TOP_SPACE:
5554 case Tabular::SET_BOTTOM_SPACE:
5555 case Tabular::SET_INTERLINE_SPACE:
5559 case Tabular::DELETE_ROW:
5560 status.setEnabled(tabular.nrows() > 1);
5562 case Tabular::DELETE_COLUMN:
5563 status.setEnabled(tabular.ncols() > 1);
5566 case Tabular::SET_TABULAR_WIDTH:
5567 status.setEnabled(!tabular.rotate
5568 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5571 case Tabular::MOVE_COLUMN_RIGHT:
5572 case Tabular::MOVE_COLUMN_LEFT:
5573 case Tabular::MOVE_ROW_DOWN:
5574 case Tabular::MOVE_ROW_UP: {
5577 if (cur.selIsMultiCell())
5578 getSelection(cur, rs, re, cs, ce);
5580 rs = tabular.cellRow(cur.idx());
5582 cs = tabular.cellColumn(cur.idx());
5585 if ((action == Tabular::MOVE_COLUMN_RIGHT
5586 && tabular.ncols() == ce + 1)
5587 || (action == Tabular::MOVE_COLUMN_LEFT && cs == 0)
5588 || (action == Tabular::MOVE_ROW_DOWN
5589 && tabular.nrows() == re + 1)
5590 || (action == Tabular::MOVE_ROW_UP && rs == 0)) {
5591 status.setEnabled(false);
5595 if (action == Tabular::MOVE_COLUMN_RIGHT
5596 || action == Tabular::MOVE_COLUMN_LEFT) {
5597 bool has_multicol = (action == Tabular::MOVE_COLUMN_RIGHT)
5598 ? tabular.hasMultiColumn(ce + 1)
5599 : tabular.hasMultiColumn(cs - 1);
5600 for (col_type c = cs; c <= ce; ++c) {
5601 if (tabular.hasMultiColumn(c)) {
5602 has_multicol = true;
5607 status.message(_("Column movement not supported with multi-columns."));
5608 status.setEnabled(false);
5613 if (action == Tabular::MOVE_ROW_DOWN
5614 || action == Tabular::MOVE_ROW_UP) {
5615 bool has_multirow = (action == Tabular::MOVE_ROW_DOWN)
5616 ? tabular.hasMultiRow(re + 1)
5617 : tabular.hasMultiRow(rs - 1);
5618 for (row_type r = rs; r <= re; ++r) {
5619 if (tabular.hasMultiRow(r)) {
5620 has_multirow = true;
5625 status.message(_("Row movement not supported with multi-rows."));
5626 status.setEnabled(false);
5631 status.setEnabled(true);
5635 case Tabular::SET_DECIMAL_POINT:
5637 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5640 case Tabular::SET_MULTICOLUMN:
5641 case Tabular::UNSET_MULTICOLUMN:
5642 case Tabular::MULTICOLUMN:
5643 // If a row is set as longtable caption, it must not be allowed
5644 // to unset that this row is a multicolumn.
5645 status.setEnabled(sel_row_start == sel_row_end
5646 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5647 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5650 case Tabular::SET_MULTIROW:
5651 case Tabular::UNSET_MULTIROW:
5652 case Tabular::MULTIROW:
5653 // If a row is set as longtable caption, it must not be allowed
5654 // to unset that this row is a multirow.
5655 status.setEnabled(sel_col_start == sel_col_end
5656 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5657 status.setOnOff(tabular.isMultiRow(cur.idx()));
5660 case Tabular::SET_ALL_LINES:
5661 case Tabular::UNSET_ALL_LINES:
5662 case Tabular::SET_INNER_LINES:
5663 case Tabular::SET_BORDER_LINES:
5664 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5667 case Tabular::RESET_FORMAL_DEFAULT:
5668 status.setEnabled(tabular.use_booktabs);
5671 case Tabular::SET_LINE_TOP:
5672 case Tabular::SET_LINE_BOTTOM:
5673 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5676 case Tabular::SET_LINE_LEFT:
5677 case Tabular::SET_LINE_RIGHT:
5678 status.setEnabled(!tabular.use_booktabs
5679 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5682 case Tabular::SET_LTRIM_TOP:
5683 case Tabular::SET_RTRIM_TOP:
5684 status.setEnabled(tabular.use_booktabs
5685 && tabular.cellRow(cur.idx()) != 0
5686 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5689 case Tabular::SET_LTRIM_BOTTOM:
5690 case Tabular::SET_RTRIM_BOTTOM:
5691 status.setEnabled(tabular.use_booktabs
5692 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5693 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5696 case Tabular::TOGGLE_LINE_TOP:
5697 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5698 status.setOnOff(tabular.topLine(cur.idx()));
5701 case Tabular::TOGGLE_LINE_BOTTOM:
5702 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5703 status.setOnOff(tabular.bottomLine(cur.idx()));
5706 case Tabular::TOGGLE_LINE_LEFT:
5707 status.setEnabled(!tabular.use_booktabs
5708 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5709 status.setOnOff(tabular.leftLine(cur.idx()));
5712 case Tabular::TOGGLE_LINE_RIGHT:
5713 status.setEnabled(!tabular.use_booktabs
5714 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5715 status.setOnOff(tabular.rightLine(cur.idx()));
5718 case Tabular::TOGGLE_LTRIM_TOP:
5719 status.setEnabled(tabular.use_booktabs
5720 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5721 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5724 case Tabular::TOGGLE_RTRIM_TOP:
5725 status.setEnabled(tabular.use_booktabs
5726 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5727 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5730 case Tabular::TOGGLE_LTRIM_BOTTOM:
5731 status.setEnabled(tabular.use_booktabs
5732 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5733 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5736 case Tabular::TOGGLE_RTRIM_BOTTOM:
5737 status.setEnabled(tabular.use_booktabs
5738 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5739 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5742 // multirow cells only inherit the alignment of the column if the column has
5743 // no width, otherwise they are left-aligned
5744 // therefore allow always left but right and center only if there is no width
5745 case Tabular::M_ALIGN_LEFT:
5748 case Tabular::ALIGN_LEFT:
5749 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5752 case Tabular::M_ALIGN_RIGHT:
5755 case Tabular::ALIGN_RIGHT:
5756 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5757 && !tabular.getPWidth(cur.idx()).zero()));
5758 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5761 case Tabular::M_ALIGN_CENTER:
5764 case Tabular::ALIGN_CENTER:
5765 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5766 && !tabular.getPWidth(cur.idx()).zero()));
5767 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5770 case Tabular::ALIGN_BLOCK:
5771 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5772 && !tabular.isMultiRow(cur.idx()));
5773 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5776 case Tabular::ALIGN_DECIMAL:
5777 status.setEnabled(!tabular.isMultiRow(cur.idx())
5778 && !tabular.isMultiColumn(cur.idx()));
5779 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5782 case Tabular::M_VALIGN_TOP:
5785 case Tabular::VALIGN_TOP:
5786 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5787 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5789 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5792 case Tabular::M_VALIGN_BOTTOM:
5795 case Tabular::VALIGN_BOTTOM:
5796 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5797 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5799 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5802 case Tabular::M_VALIGN_MIDDLE:
5805 case Tabular::VALIGN_MIDDLE:
5806 status.setEnabled((!tabular.getPWidth(cur.idx()).zero()
5807 || tabular.getUsebox(cur.idx()) == Tabular::BOX_VARWIDTH));
5809 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5812 case Tabular::SET_LONGTABULAR:
5813 case Tabular::TOGGLE_LONGTABULAR:
5814 // setting as longtable is not allowed when table is inside a float
5815 if (cur.innerInsetOfType(FLOAT_CODE) != nullptr
5816 || cur.innerInsetOfType(WRAP_CODE) != nullptr)
5817 status.setEnabled(false);
5819 status.setEnabled(true);
5820 status.setOnOff(tabular.is_long_tabular);
5823 case Tabular::UNSET_LONGTABULAR:
5824 status.setOnOff(!tabular.is_long_tabular);
5827 case Tabular::TOGGLE_ROTATE_TABULAR:
5828 case Tabular::SET_ROTATE_TABULAR:
5829 status.setOnOff(tabular.rotate != 0);
5832 case Tabular::TABULAR_VALIGN_TOP:
5833 status.setEnabled(tabular.tabular_width.zero());
5834 status.setOnOff(tabular.tabular_valignment
5835 == Tabular::LYX_VALIGN_TOP);
5837 case Tabular::TABULAR_VALIGN_MIDDLE:
5838 status.setEnabled(tabular.tabular_width.zero());
5839 status.setOnOff(tabular.tabular_valignment
5840 == Tabular::LYX_VALIGN_MIDDLE);
5842 case Tabular::TABULAR_VALIGN_BOTTOM:
5843 status.setEnabled(tabular.tabular_width.zero());
5844 status.setOnOff(tabular.tabular_valignment
5845 == Tabular::LYX_VALIGN_BOTTOM);
5848 case Tabular::LONGTABULAR_ALIGN_LEFT:
5849 status.setOnOff(tabular.longtabular_alignment
5850 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5852 case Tabular::LONGTABULAR_ALIGN_CENTER:
5853 status.setOnOff(tabular.longtabular_alignment
5854 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5856 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5857 status.setOnOff(tabular.longtabular_alignment
5858 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5861 case Tabular::UNSET_ROTATE_TABULAR:
5862 status.setOnOff(tabular.rotate == 0);
5865 case Tabular::TOGGLE_ROTATE_CELL:
5866 case Tabular::SET_ROTATE_CELL:
5867 status.setOnOff(!oneCellHasRotationState(false,
5868 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5871 case Tabular::UNSET_ROTATE_CELL:
5872 status.setOnOff(!oneCellHasRotationState(true,
5873 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5876 case Tabular::SET_USEBOX:
5877 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5880 // every row can only be one thing:
5881 // either a footer or header
5882 case Tabular::SET_LTFIRSTHEAD:
5883 status.setEnabled(sel_row_start == sel_row_end);
5884 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5887 case Tabular::UNSET_LTFIRSTHEAD:
5888 status.setEnabled(sel_row_start == sel_row_end);
5889 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5892 case Tabular::SET_LTHEAD:
5893 status.setEnabled(sel_row_start == sel_row_end);
5894 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5897 case Tabular::UNSET_LTHEAD:
5898 status.setEnabled(sel_row_start == sel_row_end);
5899 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5902 case Tabular::SET_LTFOOT:
5903 status.setEnabled(sel_row_start == sel_row_end);
5904 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5907 case Tabular::UNSET_LTFOOT:
5908 status.setEnabled(sel_row_start == sel_row_end);
5909 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5912 case Tabular::SET_LTLASTFOOT:
5913 status.setEnabled(sel_row_start == sel_row_end);
5914 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5917 case Tabular::UNSET_LTLASTFOOT:
5918 status.setEnabled(sel_row_start == sel_row_end);
5919 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5922 case Tabular::SET_LTNEWPAGE:
5923 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5925 case Tabular::UNSET_LTNEWPAGE:
5926 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5929 // only one row in head/firsthead/foot/lasthead can be the caption
5930 // and a multirow cannot be set as caption
5931 case Tabular::SET_LTCAPTION:
5932 status.setEnabled(sel_row_start == sel_row_end
5933 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5934 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5935 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5936 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5937 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5938 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5939 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5940 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5941 && !tabular.isMultiRow(sel_row_start));
5942 status.setOnOff(tabular.ltCaption(sel_row_start));
5945 case Tabular::UNSET_LTCAPTION:
5946 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5949 case Tabular::TOGGLE_LTCAPTION:
5950 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5951 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5952 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5953 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5954 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5955 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5956 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5957 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5958 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5959 status.setOnOff(tabular.ltCaption(sel_row_start));
5962 case Tabular::TOGGLE_BOOKTABS:
5963 case Tabular::SET_BOOKTABS:
5964 status.setOnOff(tabular.use_booktabs);
5967 case Tabular::UNSET_BOOKTABS:
5968 status.setOnOff(!tabular.use_booktabs);
5973 status.setEnabled(false);
5980 // function sets an object as defined in FuncStatus.h:
5981 // states OK, Unknown, Disabled, On, Off.
5982 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5983 FuncStatus & status) const
5985 switch (cmd.action()) {
5986 case LFUN_INSET_MODIFY:
5987 if (cmd.getArg(0) != "tabular")
5989 if (cmd.getArg(1) == "for-dialog") {
5990 // The dialog is asking the status of a command
5991 if (&cur.inset() != this)
5993 string action = cmd.getArg(2);
5994 string arg = cmd.getLongArg(3);
5995 return getFeatureStatus(cur, action, arg, status);
5997 // We always enable the lfun if it is coming from the dialog
5998 // because the dialog makes sure all the settings are valid,
5999 // even though the first argument might not be valid now.
6000 status.setEnabled(true);
6004 case LFUN_TABULAR_FEATURE: {
6005 if (&cur.inset() != this)
6007 string action = cmd.getArg(0);
6008 string arg = cmd.getLongArg(1);
6009 return getFeatureStatus(cur, action, arg, status);
6012 case LFUN_CAPTION_INSERT: {
6013 // caption is only allowed in caption cell of longtable
6014 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
6015 status.setEnabled(false);
6018 // only standard caption is allowed
6019 string arg = cmd.getArg(0);
6020 if (!arg.empty() && arg != "Standard") {
6021 status.setEnabled(false);
6024 // check if there is already a caption
6025 bool have_caption = false;
6026 InsetTableCell itc = *tabular.cellInset(cur.idx());
6027 ParagraphList::const_iterator pit = itc.paragraphs().begin();
6028 ParagraphList::const_iterator pend = itc.paragraphs().end();
6029 for (; pit != pend; ++pit) {
6030 InsetList::const_iterator it = pit->insetList().begin();
6031 InsetList::const_iterator end = pit->insetList().end();
6032 for (; it != end; ++it) {
6033 if (it->inset->lyxCode() == CAPTION_CODE) {
6034 have_caption = true;
6039 status.setEnabled(!have_caption);
6043 // These are only enabled inside tabular
6044 case LFUN_CELL_BACKWARD:
6045 case LFUN_CELL_FORWARD:
6046 status.setEnabled(true);
6049 // disable these with multiple cells selected
6050 case LFUN_INSET_INSERT:
6051 case LFUN_TABULAR_INSERT:
6052 case LFUN_TABULAR_STYLE_INSERT:
6053 case LFUN_FLEX_INSERT:
6054 case LFUN_FLOAT_INSERT:
6055 case LFUN_FLOAT_WIDE_INSERT:
6056 case LFUN_FOOTNOTE_INSERT:
6057 case LFUN_MARGINALNOTE_INSERT:
6058 case LFUN_MATH_INSERT:
6059 case LFUN_MATH_MODE:
6060 case LFUN_MATH_MUTATE:
6061 case LFUN_MATH_DISPLAY:
6062 case LFUN_NOTE_INSERT:
6063 case LFUN_ARGUMENT_INSERT:
6064 case LFUN_BOX_INSERT:
6065 case LFUN_BRANCH_INSERT:
6066 case LFUN_PHANTOM_INSERT:
6067 case LFUN_WRAP_INSERT:
6068 case LFUN_PREVIEW_INSERT:
6069 case LFUN_ERT_INSERT: {
6070 if (cur.selIsMultiCell()) {
6071 status.setEnabled(false);
6074 return cell(cur.idx())->getStatus(cur, cmd, status);
6077 case LFUN_CHANGE_ACCEPT:
6078 case LFUN_CHANGE_REJECT: {
6079 if (cur.selIsMultiCell()) {
6082 getSelection(cur, rs, re, cs, ce);
6083 for (row_type r = rs; r <= re; ++r) {
6084 if (tabular.row_info[r].change.changed()) {
6085 status.setEnabled(true);
6088 for (col_type c = cs; c <= ce; ++c) {
6089 if (tabular.column_info[c].change.changed()) {
6090 status.setEnabled(true);
6096 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
6097 status.setEnabled(true);
6100 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
6101 status.setEnabled(true);
6105 return cell(cur.idx())->getStatus(cur, cmd, status);
6108 case LFUN_PARAGRAPH_BREAK:
6109 case LFUN_NEWLINE_INSERT:
6110 return cell(cur.idx())->getStatus(cur, cmd, status);
6112 case LFUN_NEWPAGE_INSERT:
6113 status.setEnabled(false);
6117 if (tabularStackDirty() && theClipboard().isInternal()) {
6118 if (cur.selIsMultiCell()) {
6121 getSelection(cur, rs, re, cs, ce);
6122 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
6123 && paste_tabular->nrows() == re - rs + 1)
6124 status.setEnabled(true);
6126 status.setEnabled(false);
6127 status.message(_("Selection size should match clipboard content."));
6130 status.setEnabled(true);
6133 return cell(cur.idx())->getStatus(cur, cmd, status);
6135 case LFUN_INSET_SETTINGS:
6136 // relay this lfun to Inset, not to the cell.
6137 return Inset::getStatus(cur, cmd, status);
6140 // we try to handle this event in the insets dispatch function.
6141 return cell(cur.idx())->getStatus(cur, cmd, status);
6147 Inset::RowFlags InsetTabular::rowFlags() const
6149 if (tabular.is_long_tabular) {
6150 switch (tabular.longtabular_alignment) {
6151 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
6152 return Display | AlignLeft;
6153 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
6155 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
6156 return Display | AlignRight;
6165 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
6167 tabular.latex(os, runparams);
6171 int InsetTabular::plaintext(odocstringstream & os,
6172 OutputParams const & runparams, size_t max_length) const
6174 os << '\n'; // output table on a new line
6175 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
6176 tabular.plaintext(os, runparams, dp, false, 0, max_length);
6177 return PLAINTEXT_NEWLINE;
6181 void InsetTabular::docbook(XMLStream & xs, OutputParams const & runparams) const
6183 tabular.docbook(xs, runparams);
6187 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6189 return tabular.xhtml(xs, rp);
6193 void InsetTabular::validate(LaTeXFeatures & features) const
6195 tabular.validate(features);
6196 features.useInsetLayout(getLayout());
6200 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6202 return tabular.cellInset(idx);
6206 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6208 return tabular.cellInset(idx);
6212 void InsetTabular::cursorPos(BufferView const & bv,
6213 CursorSlice const & sl, bool boundary, int & x, int & y) const
6215 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6217 // y offset correction
6218 y += cellYPos(sl.idx());
6219 y += tabular.textVOffset(sl.idx());
6220 y += tabular.offsetVAlignment();
6222 // x offset correction
6223 x += cellXPos(sl.idx());
6224 x += tabular.textHOffset(sl.idx());
6225 x += ADD_TO_TABULAR_WIDTH;
6229 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6233 Inset const & inset = *tabular.cellInset(cell);
6234 Point o = bv.coordCache().getInsets().xy(&inset);
6235 int const xbeg = o.x_ - tabular.textHOffset(cell);
6236 int const xend = xbeg + tabular.cellWidth(cell);
6237 row_type const row = tabular.cellRow(cell);
6238 int const ybeg = o.y_ - tabular.rowAscent(row)
6239 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6240 int const yend = ybeg + tabular.cellHeight(cell);
6252 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6253 // << " ybeg=" << ybeg << " yend=" << yend
6254 // << " xx=" << xx << " yy=" << yy
6255 // << " dist=" << xx + yy << endl;
6260 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6262 //lyxerr << "InsetTabular::editXY: " << this << endl;
6264 cur.idx() = getNearestCell(cur.bv(), x, y);
6265 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6269 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6271 cur.idx() = getNearestCell(cur.bv(), x, y);
6272 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6276 idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6278 idx_type idx_min = 0;
6279 int dist_min = numeric_limits<int>::max();
6280 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6281 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6282 int const d = dist(bv, i, x, y);
6293 int InsetTabular::cellYPos(idx_type const cell) const
6295 row_type row = tabular.cellRow(cell);
6297 for (row_type r = 0; r < row; ++r)
6298 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6299 + tabular.interRowSpace(r + 1);
6304 int InsetTabular::cellXPos(idx_type const cell) const
6306 col_type col = tabular.cellColumn(cell);
6308 for (col_type c = 0; c < col; ++c)
6309 lx += tabular.column_info[c].width;
6314 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6316 row_type const row = tabular.cellRow(cur.idx());
6317 col_type const col = tabular.cellColumn(cur.idx());
6319 if (isRightToLeft(cur)) {
6320 if (tabular.cellColumn(cur.idx()) == 0) {
6321 if (row == tabular.nrows() - 1)
6323 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6328 cur.idx() = tabular.getLastCellInRow(row - 1);
6330 cur.idx() = tabular.cellIndex(row, col - 1);
6333 if (tabular.isLastCell(cur.idx()))
6335 if (cur.idx() == tabular.getLastCellInRow(row))
6336 cur.idx() = tabular.cellIndex(row + 1, 0);
6338 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6339 cur.idx() = tabular.cellIndex(row, colnextcell);
6343 cur.boundary(false);
6345 if (cur.selIsMultiCell()) {
6346 cur.pit() = cur.lastpit();
6347 cur.pos() = cur.lastpos();
6354 // in visual mode, place cursor at extreme left or right
6356 switch(entry_from) {
6358 case ENTRY_DIRECTION_RIGHT:
6359 cur.posVisToRowExtremity(false /* !left */);
6361 case ENTRY_DIRECTION_LEFT:
6362 cur.posVisToRowExtremity(true /* left */);
6364 case ENTRY_DIRECTION_IGNORE:
6365 // nothing to do in this case
6369 cur.setCurrentFont();
6373 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6375 row_type const row = tabular.cellRow(cur.idx());
6376 col_type const col = tabular.cellColumn(cur.idx());
6378 if (isRightToLeft(cur)) {
6379 if (cur.idx() == tabular.getLastCellInRow(row)) {
6382 cur.idx() = tabular.getFirstCellInRow(row);
6383 cur.idx() = tabular.cellAbove(cur.idx());
6385 if (tabular.isLastCell(cur.idx()))
6387 if (cur.idx() == tabular.getLastCellInRow(row))
6388 cur.idx() = tabular.cellIndex(row + 1, 0);
6390 cur.idx() = tabular.cellIndex(row, col + 1);
6393 if (cur.idx() == 0) // first cell
6396 cur.idx() = tabular.getLastCellInRow(row - 1);
6398 cur.idx() = tabular.cellIndex(row, col - 1);
6401 if (cur.selIsMultiCell()) {
6402 cur.pit() = cur.lastpit();
6403 cur.pos() = cur.lastpos();
6407 cur.pit() = cur.lastpit();
6408 cur.pos() = cur.lastpos();
6410 // in visual mode, place cursor at extreme left or right
6412 switch(entry_from) {
6414 case ENTRY_DIRECTION_RIGHT:
6415 cur.posVisToRowExtremity(false /* !left */);
6417 case ENTRY_DIRECTION_LEFT:
6418 cur.posVisToRowExtremity(true /* left */);
6420 case ENTRY_DIRECTION_IGNORE:
6421 // nothing to do in this case
6425 cur.setCurrentFont();
6429 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6431 cur.recordUndoInset(this);
6433 istringstream is(argument);
6434 // limit the size of strings we read to avoid memory problems
6438 size_t safe_guard = 0;
6443 if (safe_guard > 1000) {
6444 LYXERR0("parameter max count reached!");
6448 Tabular::Feature action = Tabular::LAST_ACTION;
6451 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6452 if (s != tabularFeature[i].feature)
6454 action = tabularFeature[i].action;
6457 if (action == Tabular::LAST_ACTION) {
6458 LYXERR0("Feature not found " << s);
6462 if (tabularFeature[i].need_value)
6464 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6465 tabularFeatures(cur, action, val);
6470 static void checkLongtableSpecial(Tabular::ltType & ltt,
6471 string const & special, bool & flag)
6473 if (special == "dl_above") {
6476 } else if (special == "dl_below") {
6477 ltt.bottomDL = flag;
6479 } else if (special == "empty") {
6489 bool InsetTabular::oneCellHasRotationState(bool rotated,
6490 row_type row_start, row_type row_end,
6491 col_type col_start, col_type col_end) const
6493 for (row_type r = row_start; r <= row_end; ++r)
6494 for (col_type c = col_start; c <= col_end; ++c)
6496 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6499 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6506 void InsetTabular::tabularFeatures(Cursor & cur,
6507 Tabular::Feature feature, string const & value)
6509 col_type sel_col_start;
6510 col_type sel_col_end;
6511 row_type sel_row_start;
6512 row_type sel_row_end;
6513 bool setLines = false;
6514 bool setLinesInnerOnly = false;
6515 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6516 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6520 case Tabular::M_ALIGN_LEFT:
6521 case Tabular::ALIGN_LEFT:
6522 setAlign = LYX_ALIGN_LEFT;
6525 case Tabular::M_ALIGN_RIGHT:
6526 case Tabular::ALIGN_RIGHT:
6527 setAlign = LYX_ALIGN_RIGHT;
6530 case Tabular::M_ALIGN_CENTER:
6531 case Tabular::ALIGN_CENTER:
6532 setAlign = LYX_ALIGN_CENTER;
6535 case Tabular::ALIGN_BLOCK:
6536 setAlign = LYX_ALIGN_BLOCK;
6539 case Tabular::ALIGN_DECIMAL:
6540 setAlign = LYX_ALIGN_DECIMAL;
6543 case Tabular::M_VALIGN_TOP:
6544 case Tabular::VALIGN_TOP:
6545 setVAlign = Tabular::LYX_VALIGN_TOP;
6548 case Tabular::M_VALIGN_BOTTOM:
6549 case Tabular::VALIGN_BOTTOM:
6550 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6553 case Tabular::M_VALIGN_MIDDLE:
6554 case Tabular::VALIGN_MIDDLE:
6555 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6562 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6563 row_type const row = tabular.cellRow(cur.idx());
6564 col_type const column = tabular.cellColumn(cur.idx());
6566 Tabular::ltType ltt;
6570 case Tabular::SET_TABULAR_WIDTH:
6571 tabular.setTabularWidth(Length(value));
6574 case Tabular::SET_PWIDTH: {
6575 Length const len(value);
6576 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6577 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6579 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6580 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6585 case Tabular::SET_MPWIDTH:
6586 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6589 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6590 bool const varwidth = value == "on";
6591 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6592 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6596 case Tabular::SET_MROFFSET:
6597 tabular.setMROffset(cur, cur.idx(), Length(value));
6600 case Tabular::SET_SPECIAL_COLUMN:
6601 case Tabular::SET_SPECIAL_MULTICOLUMN:
6602 if (value == "none")
6603 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6605 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6608 case Tabular::APPEND_ROW:
6609 // append the row into the tabular
6610 tabular.appendRow(row);
6613 case Tabular::APPEND_COLUMN:
6614 // append the column into the tabular
6615 tabular.appendColumn(column);
6616 cur.idx() = tabular.cellIndex(row, column);
6619 case Tabular::DELETE_ROW:
6620 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6621 for (col_type c = 0; c < tabular.ncols(); c++) {
6622 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6623 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6624 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6625 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6629 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6630 tabular.deleteRow(sel_row_start);
6631 if (sel_row_start >= tabular.nrows())
6633 cur.idx() = tabular.cellIndex(sel_row_start, column);
6636 cur.selection(false);
6639 case Tabular::DELETE_COLUMN:
6640 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6641 for (row_type r = 0; r < tabular.nrows(); r++)
6642 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6643 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6646 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6647 for (row_type r = 0; r < tabular.nrows(); r++)
6648 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6649 tabular.leftLine(tabular.cellIndex(r, 0)));
6652 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6653 tabular.deleteColumn(sel_col_start);
6654 if (sel_col_start >= tabular.ncols())
6656 cur.idx() = tabular.cellIndex(row, sel_col_start);
6659 cur.selection(false);
6662 case Tabular::COPY_ROW:
6663 tabular.copyRow(row);
6666 case Tabular::COPY_COLUMN:
6667 tabular.copyColumn(column);
6668 cur.idx() = tabular.cellIndex(row, column);
6671 case Tabular::MOVE_COLUMN_RIGHT:
6672 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::RIGHT);
6673 if (cur.selection()) {
6674 cur.selection(false);
6675 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start + 1);
6677 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end + 1);
6680 cur.idx() = tabular.cellIndex(row, column + 1);
6683 case Tabular::MOVE_COLUMN_LEFT:
6684 tabular.moveColumn(sel_col_start, sel_col_end, Tabular::LEFT);
6685 if (cur.selection()) {
6686 cur.selection(false);
6687 cur.idx() = tabular.cellIndex(sel_row_start, sel_col_start - 1);
6689 cur.idx() = tabular.cellIndex(sel_row_end, sel_col_end - 1);
6692 cur.idx() = tabular.cellIndex(row, column - 1);
6695 case Tabular::MOVE_ROW_DOWN:
6696 tabular.moveRow(sel_row_start, sel_row_end, Tabular::DOWN);
6697 if (cur.selection()) {
6698 cur.selection(false);
6699 cur.idx() = tabular.cellIndex(sel_row_start + 1, sel_col_start);
6701 cur.idx() = tabular.cellIndex(sel_row_end + 1, sel_col_end);
6704 cur.idx() = tabular.cellIndex(row + 1, column);
6707 case Tabular::MOVE_ROW_UP:
6708 tabular.moveRow(sel_row_start, sel_row_end, Tabular::UP);
6709 if (cur.selection()) {
6710 cur.selection(false);
6711 cur.idx() = tabular.cellIndex(sel_row_start - 1, sel_col_start);
6713 cur.idx() = tabular.cellIndex(sel_row_end - 1, sel_col_end);
6716 cur.idx() = tabular.cellIndex(row - 1, column);
6719 case Tabular::SET_LINE_TOP:
6720 case Tabular::TOGGLE_LINE_TOP: {
6721 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6722 ? (value == "true") : !tabular.topLine(cur.idx());
6723 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6724 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6725 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6729 case Tabular::SET_LINE_BOTTOM:
6730 case Tabular::TOGGLE_LINE_BOTTOM: {
6731 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6732 ? (value == "true") : !tabular.bottomLine(cur.idx());
6733 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6734 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6735 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6739 case Tabular::SET_LTRIM_TOP:
6740 case Tabular::TOGGLE_LTRIM_TOP: {
6741 bool l = (feature == Tabular::SET_LTRIM_TOP)
6742 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6743 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6744 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6745 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6749 case Tabular::SET_RTRIM_TOP:
6750 case Tabular::TOGGLE_RTRIM_TOP: {
6751 bool l = (feature == Tabular::SET_RTRIM_TOP)
6752 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6753 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6754 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6755 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6759 case Tabular::SET_LTRIM_BOTTOM:
6760 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6761 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6762 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6763 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6764 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6765 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6769 case Tabular::SET_RTRIM_BOTTOM:
6770 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6771 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6772 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6773 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6774 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6775 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6779 case Tabular::SET_LINE_LEFT:
6780 case Tabular::TOGGLE_LINE_LEFT: {
6781 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6782 ? (value == "true") : !tabular.leftLine(cur.idx());
6783 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6784 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6785 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6789 case Tabular::SET_LINE_RIGHT:
6790 case Tabular::TOGGLE_LINE_RIGHT: {
6791 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6792 ? (value == "true") : !tabular.rightLine(cur.idx());
6793 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6794 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6795 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6799 case Tabular::M_ALIGN_LEFT:
6800 case Tabular::M_ALIGN_RIGHT:
6801 case Tabular::M_ALIGN_CENTER:
6802 case Tabular::ALIGN_LEFT:
6803 case Tabular::ALIGN_RIGHT:
6804 case Tabular::ALIGN_CENTER:
6805 case Tabular::ALIGN_BLOCK:
6806 case Tabular::ALIGN_DECIMAL:
6807 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6808 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6809 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6810 !tabular.getPWidth(c).zero());
6813 case Tabular::M_VALIGN_TOP:
6814 case Tabular::M_VALIGN_BOTTOM:
6815 case Tabular::M_VALIGN_MIDDLE:
6818 case Tabular::VALIGN_TOP:
6819 case Tabular::VALIGN_BOTTOM:
6820 case Tabular::VALIGN_MIDDLE:
6821 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6822 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6823 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6826 case Tabular::SET_MULTICOLUMN: {
6827 if (!cur.selection()) {
6828 // just multicol for one single cell
6829 // check whether we are completely in a multicol
6830 if (!tabular.isMultiColumn(cur.idx()))
6831 tabular.setMultiColumn(cur, cur.idx(), 1,
6832 tabular.rightLine(cur.idx()));
6835 // we have a selection so this means we just add all these
6836 // cells to form a multicolumn cell
6837 idx_type const s_start = cur.selBegin().idx();
6838 row_type const col_start = tabular.cellColumn(s_start);
6839 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6840 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6841 tabular.rightLine(cur.selEnd().idx()));
6844 cur.selection(false);
6848 case Tabular::UNSET_MULTICOLUMN: {
6849 if (!cur.selection()) {
6850 if (tabular.isMultiColumn(cur.idx()))
6851 tabular.unsetMultiColumn(cur.idx());
6856 case Tabular::MULTICOLUMN: {
6857 if (!cur.selection()) {
6858 if (tabular.isMultiColumn(cur.idx()))
6859 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6861 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6865 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6866 row_type const r = sel_row_start;
6867 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6868 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6871 // If the selection contains at least one singlecol cell
6872 // or multiple multicol cells,
6873 // we assume the user will merge is to a single multicol
6875 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6877 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6881 case Tabular::SET_MULTIROW: {
6882 if (!cur.selection()) {
6883 // just multirow for one single cell
6884 // check whether we are completely in a multirow
6885 if (!tabular.isMultiRow(cur.idx()))
6886 tabular.setMultiRow(cur, cur.idx(), 1,
6887 tabular.bottomLine(cur.idx()),
6888 tabular.getAlignment(cur.idx()));
6891 // we have a selection so this means we just add all this
6892 // cells to form a multirow cell
6893 idx_type const s_start = cur.selBegin().idx();
6894 row_type const row_start = tabular.cellRow(s_start);
6895 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6896 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6897 tabular.bottomLine(cur.selEnd().idx()),
6898 tabular.getAlignment(cur.selEnd().idx()));
6901 cur.selection(false);
6905 case Tabular::UNSET_MULTIROW: {
6906 if (!cur.selection()) {
6907 if (tabular.isMultiRow(cur.idx()))
6908 tabular.unsetMultiRow(cur.idx());
6913 case Tabular::MULTIROW: {
6914 if (!cur.selection()) {
6915 if (tabular.isMultiRow(cur.idx()))
6916 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6918 tabularFeatures(cur, Tabular::SET_MULTIROW);
6922 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6923 col_type const c = sel_col_start;
6924 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6925 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6928 // If the selection contains at least one singlerow cell
6929 // or multiple multirow cells,
6930 // we assume the user will merge is to a single multirow
6932 tabularFeatures(cur, Tabular::SET_MULTIROW);
6934 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6938 case Tabular::SET_INNER_LINES:
6939 setLinesInnerOnly = true;
6941 case Tabular::SET_ALL_LINES:
6944 case Tabular::UNSET_ALL_LINES:
6945 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6946 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6947 idx_type const cell = tabular.cellIndex(r, c);
6948 if (!setLinesInnerOnly || r != sel_row_start)
6949 tabular.setTopLine(cell, setLines);
6950 if ((!setLinesInnerOnly || r != sel_row_end)
6951 && (!setLines || r == sel_row_end))
6952 tabular.setBottomLine(cell, setLines);
6953 if ((!setLinesInnerOnly || c != sel_col_end)
6954 && (!setLines || c == sel_col_end))
6955 tabular.setRightLine(cell, setLines);
6956 if ((!setLinesInnerOnly || c != sel_col_start))
6957 tabular.setLeftLine(cell, setLines);
6961 case Tabular::RESET_FORMAL_DEFAULT:
6962 for (row_type r = 0; r < tabular.nrows(); ++r) {
6963 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6964 for (col_type c = 0; c < tabular.ncols(); ++c) {
6965 idx_type const cell = tabular.cellIndex(r, c);
6966 tabular.setTopLine(cell, head_or_foot);
6967 tabular.setBottomLine(cell, head_or_foot);
6972 case Tabular::SET_BORDER_LINES:
6973 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6974 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6975 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6977 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6978 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6979 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6983 case Tabular::TOGGLE_LONGTABULAR:
6984 if (tabular.is_long_tabular)
6985 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6987 tabular.is_long_tabular = true;
6990 case Tabular::SET_LONGTABULAR:
6991 tabular.is_long_tabular = true;
6994 case Tabular::UNSET_LONGTABULAR:
6995 for (row_type r = 0; r < tabular.nrows(); ++r) {
6996 if (tabular.ltCaption(r)) {
6997 cur.idx() = tabular.cellIndex(r, 0);
7000 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
7003 tabular.is_long_tabular = false;
7006 case Tabular::SET_ROTATE_TABULAR:
7007 tabular.rotate = convert<int>(value);
7010 case Tabular::UNSET_ROTATE_TABULAR:
7014 case Tabular::TOGGLE_ROTATE_TABULAR:
7015 // when pressing the rotate button we default to 90° rotation
7016 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
7019 case Tabular::TABULAR_VALIGN_TOP:
7020 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
7023 case Tabular::TABULAR_VALIGN_MIDDLE:
7024 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
7027 case Tabular::TABULAR_VALIGN_BOTTOM:
7028 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
7031 case Tabular::LONGTABULAR_ALIGN_LEFT:
7032 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
7035 case Tabular::LONGTABULAR_ALIGN_CENTER:
7036 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
7039 case Tabular::LONGTABULAR_ALIGN_RIGHT:
7040 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
7043 case Tabular::SET_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), convert<int>(value));
7049 case Tabular::UNSET_ROTATE_CELL:
7050 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7051 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7052 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7055 case Tabular::TOGGLE_ROTATE_CELL:
7057 bool oneNotRotated = oneCellHasRotationState(false,
7058 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
7060 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7061 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
7062 // when pressing the rotate cell button we default to 90° rotation
7064 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
7066 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
7071 case Tabular::SET_USEBOX: {
7072 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
7073 if (val == tabular.getUsebox(cur.idx()))
7074 val = Tabular::BOX_NONE;
7075 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7076 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7077 tabular.setUsebox(tabular.cellIndex(r, c), val);
7081 case Tabular::UNSET_LTFIRSTHEAD:
7084 case Tabular::SET_LTFIRSTHEAD:
7085 tabular.getRowOfLTFirstHead(row, ltt);
7086 checkLongtableSpecial(ltt, value, flag);
7087 tabular.setLTHead(row, flag, ltt, true);
7090 case Tabular::UNSET_LTHEAD:
7093 case Tabular::SET_LTHEAD:
7094 tabular.getRowOfLTHead(row, ltt);
7095 checkLongtableSpecial(ltt, value, flag);
7096 tabular.setLTHead(row, flag, ltt, false);
7099 case Tabular::UNSET_LTFOOT:
7102 case Tabular::SET_LTFOOT:
7103 tabular.getRowOfLTFoot(row, ltt);
7104 checkLongtableSpecial(ltt, value, flag);
7105 tabular.setLTFoot(row, flag, ltt, false);
7108 case Tabular::UNSET_LTLASTFOOT:
7111 case Tabular::SET_LTLASTFOOT:
7112 tabular.getRowOfLTLastFoot(row, ltt);
7113 checkLongtableSpecial(ltt, value, flag);
7114 tabular.setLTFoot(row, flag, ltt, true);
7117 case Tabular::UNSET_LTNEWPAGE:
7120 case Tabular::SET_LTNEWPAGE:
7121 tabular.setLTNewPage(row, flag);
7124 case Tabular::SET_LTCAPTION: {
7125 if (tabular.ltCaption(row))
7127 cur.idx() = tabular.setLTCaption(cur, row, true);
7130 cur.selection(false);
7131 // If a row is set as caption, then also insert
7132 // a caption. Otherwise the LaTeX output is broken.
7133 // Select cell if it is non-empty
7134 if (cur.lastpos() > 0 || cur.lastpit() > 0)
7135 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
7136 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
7140 case Tabular::UNSET_LTCAPTION: {
7141 if (!tabular.ltCaption(row))
7143 cur.idx() = tabular.setLTCaption(cur, row, false);
7146 cur.selection(false);
7147 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
7148 if (lyx::getStatus(fr).enabled())
7153 case Tabular::TOGGLE_LTCAPTION: {
7154 if (tabular.ltCaption(row))
7155 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
7157 tabularFeatures(cur, Tabular::SET_LTCAPTION);
7161 case Tabular::TOGGLE_BOOKTABS:
7162 tabular.use_booktabs = !tabular.use_booktabs;
7165 case Tabular::SET_BOOKTABS:
7166 tabular.use_booktabs = true;
7169 case Tabular::UNSET_BOOKTABS:
7170 tabular.use_booktabs = false;
7173 case Tabular::SET_TOP_SPACE: {
7175 if (value == "default")
7176 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7177 tabular.row_info[r].top_space_default = true;
7178 else if (value == "none")
7179 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7180 tabular.row_info[r].top_space_default = false;
7181 tabular.row_info[r].top_space = len;
7183 else if (isValidLength(value, &len))
7184 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7185 tabular.row_info[r].top_space_default = false;
7186 tabular.row_info[r].top_space = len;
7191 case Tabular::SET_BOTTOM_SPACE: {
7193 if (value == "default")
7194 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7195 tabular.row_info[r].bottom_space_default = true;
7196 else if (value == "none")
7197 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7198 tabular.row_info[r].bottom_space_default = false;
7199 tabular.row_info[r].bottom_space = len;
7201 else if (isValidLength(value, &len))
7202 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7203 tabular.row_info[r].bottom_space_default = false;
7204 tabular.row_info[r].bottom_space = len;
7209 case Tabular::SET_INTERLINE_SPACE: {
7211 if (value == "default")
7212 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7213 tabular.row_info[r].interline_space_default = true;
7214 else if (value == "none")
7215 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7216 tabular.row_info[r].interline_space_default = false;
7217 tabular.row_info[r].interline_space = len;
7219 else if (isValidLength(value, &len))
7220 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7221 tabular.row_info[r].interline_space_default = false;
7222 tabular.row_info[r].interline_space = len;
7227 case Tabular::SET_DECIMAL_POINT:
7228 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7229 tabular.column_info[c].decimal_point = from_utf8(value);
7232 // dummy stuff just to avoid warnings
7233 case Tabular::LAST_ACTION:
7239 bool InsetTabular::copySelection(Cursor & cur)
7241 if (!cur.selection())
7246 getSelection(cur, rs, re, cs, ce);
7248 paste_tabular.reset(new Tabular(tabular));
7250 for (row_type r = 0; r < rs; ++r)
7251 paste_tabular->deleteRow(0, true);
7253 row_type const rows = re - rs + 1;
7254 while (paste_tabular->nrows() > rows)
7255 paste_tabular->deleteRow(rows, true);
7257 for (col_type c = 0; c < cs; ++c)
7258 paste_tabular->deleteColumn(0, true);
7260 col_type const columns = ce - cs + 1;
7261 while (paste_tabular->ncols() > columns)
7262 paste_tabular->deleteColumn(columns, true);
7264 paste_tabular->setBuffer(tabular.buffer());
7266 odocstringstream os;
7267 OutputParams const runparams(nullptr);
7268 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7269 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7270 cap::copySelection(cur, os.str());
7272 // mark tabular stack dirty
7273 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7274 // when we (hopefully) have a one-for-all paste mechanism.
7275 // This must be called after cap::copySelection.
7276 dirtyTabularStack(true);
7282 bool InsetTabular::pasteClipboard(Cursor & cur)
7286 col_type actcol = tabular.cellColumn(cur.idx());
7287 row_type actrow = tabular.cellRow(cur.idx());
7289 if (cur.selIsMultiCell()) {
7292 getSelection(cur, actrow, re, actcol, ce);
7295 col_type const oldncols = tabular.ncols();
7296 for (row_type r1 = 0, r2 = actrow; r1 < paste_tabular->nrows(); ++r1, ++r2) {
7297 // Append rows if needed
7298 if (r2 == tabular.nrows())
7299 tabular.insertRow(r2 - 1, false);
7300 for (col_type c1 = 0, c2 = actcol; c1 < paste_tabular->ncols(); ++c1, ++c2) {
7301 // Append columns if needed
7302 if (c2 == tabular.ncols())
7303 tabular.insertColumn(c2 - 1, false);
7304 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7305 tabular.isPartOfMultiColumn(r2, c2))
7307 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7311 if (tabular.isPartOfMultiColumn(r2, c2)) {
7315 shared_ptr<InsetTableCell> inset(
7316 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7317 tabular.setCellInset(r2, c2, inset);
7318 // FIXME?: why do we need to do this explicitly? (EL)
7319 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7321 if (lyxrc.ct_markup_copied) {
7322 // Only change to inserted if ct is active,
7323 // otherwise leave markup as is
7324 if (buffer().params().track_changes)
7325 inset->setChange(Change(Change::INSERTED));
7327 // Resolve all markup to inserted or unchanged
7328 inset->setChange(Change(buffer().params().track_changes ?
7329 Change::INSERTED : Change::UNCHANGED));
7334 // amend cursor position if cols have been appended
7335 cur.idx() += actrow * (tabular.ncols() - oldncols);
7340 void InsetTabular::cutSelection(Cursor & cur)
7342 if (!cur.selection())
7347 getSelection(cur, rs, re, cs, ce);
7348 for (row_type r = rs; r <= re; ++r) {
7349 for (col_type c = cs; c <= ce; ++c) {
7350 shared_ptr<InsetTableCell> t
7351 = cell(tabular.cellIndex(r, c));
7352 if (buffer().params().track_changes)
7353 // FIXME: Change tracking (MG)
7354 t->setChange(Change(Change::DELETED));
7360 // cursor position might be invalid now
7361 if (cur.pit() > cur.lastpit())
7362 cur.pit() = cur.lastpit();
7363 if (cur.pos() > cur.lastpos())
7364 cur.pos() = cur.lastpos();
7365 cur.clearSelection();
7369 bool InsetTabular::isRightToLeft(Cursor & cur) const
7371 // LASSERT: It might be better to abandon this Buffer.
7372 LASSERT(cur.depth() > 1, return false);
7373 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7374 pos_type const parentpos = cur[cur.depth() - 2].pos();
7375 return parentpar.getFontSettings(buffer().params(),
7376 parentpos).language()->rightToLeft();
7380 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7383 LASSERT(stidx <= enidx, return docstring());
7385 col_type const col1 = tabular.cellColumn(stidx);
7386 col_type const col2 = tabular.cellColumn(enidx);
7387 row_type const row1 = tabular.cellRow(stidx);
7388 row_type const row2 = tabular.cellRow(enidx);
7390 for (col_type col = col1; col <= col2; col++)
7391 for (row_type row = row1; row <= row2; row++) {
7396 retval += tabular.cellInset(row, col)->asString(intoInsets);
7402 ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
7404 LASSERT(stidx <= enidx, return ParagraphList());
7405 ParagraphList retval;
7406 col_type const col1 = tabular.cellColumn(stidx);
7407 col_type const col2 = tabular.cellColumn(enidx);
7408 row_type const row1 = tabular.cellRow(stidx);
7409 row_type const row2 = tabular.cellRow(enidx);
7410 for (col_type col = col1; col <= col2; col++)
7411 for (row_type row = row1; row <= row2; row++)
7412 for (auto const & par : tabular.cellInset(row, col)->paragraphs())
7413 retval.push_back(par);
7418 void InsetTabular::getSelection(Cursor & cur,
7419 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7421 CursorSlice const & beg = cur.selBegin();
7422 CursorSlice const & end = cur.selEnd();
7423 cs = tabular.cellColumn(beg.idx());
7424 ce = tabular.cellColumn(end.idx());
7428 rs = tabular.cellRow(beg.idx());
7429 re = tabular.cellRow(end.idx());
7435 Text * InsetTabular::getText(int idx) const
7437 return size_t(idx) < nargs() ? cell(idx)->getText(0) : nullptr;
7441 bool InsetTabular::isChanged() const
7443 for (idx_type idx = 0; idx < nargs(); ++idx) {
7444 if (cell(idx)->isChanged())
7446 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7448 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7455 void InsetTabular::setChange(Change const & change)
7457 for (idx_type idx = 0; idx < nargs(); ++idx)
7458 cell(idx)->setChange(change);
7462 void InsetTabular::acceptChanges()
7464 for (idx_type idx = 0; idx < nargs(); ++idx)
7465 cell(idx)->acceptChanges();
7466 for (row_type row = 0; row < tabular.nrows(); ++row) {
7467 if (tabular.row_info[row].change.inserted())
7468 tabular.row_info[row].change.setUnchanged();
7469 else if (tabular.row_info[row].change.deleted())
7470 tabular.deleteRow(row, true);
7472 for (col_type col = 0; col < tabular.ncols(); ++col) {
7473 if (tabular.column_info[col].change.inserted())
7474 tabular.column_info[col].change.setUnchanged();
7475 else if (tabular.column_info[col].change.deleted())
7476 tabular.deleteColumn(col, true);
7478 tabular.updateIndexes();
7482 void InsetTabular::rejectChanges()
7484 for (idx_type idx = 0; idx < nargs(); ++idx)
7485 cell(idx)->rejectChanges();
7486 for (row_type row = 0; row < tabular.nrows(); ++row) {
7487 if (tabular.row_info[row].change.deleted())
7488 tabular.row_info[row].change.setUnchanged();
7489 else if (tabular.row_info[row].change.inserted())
7490 tabular.deleteRow(row, true);
7492 for (col_type col = 0; col < tabular.ncols(); ++col) {
7493 if (tabular.column_info[col].change.deleted())
7494 tabular.column_info[col].change.setUnchanged();
7495 else if (tabular.column_info[col].change.inserted())
7496 tabular.deleteColumn(col, true);
7498 tabular.updateIndexes();
7502 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7504 return tabular.getPWidth(cell).zero();
7508 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7511 if (buf.length() <= 0)
7516 col_type maxCols = 1;
7517 size_t const len = buf.length();
7521 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7529 maxCols = max(cols, maxCols);
7535 maxCols = max(cols, maxCols);
7541 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7542 loctab = paste_tabular.get();
7543 dirtyTabularStack(true);
7546 cell = bv.cursor().idx();
7547 ocol = tabular.cellColumn(cell);
7548 row = tabular.cellRow(cell);
7552 idx_type cells = loctab->numberofcells;
7555 rows = loctab->nrows();
7556 col_type columns = loctab->ncols();
7559 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7565 // append column if necessary
7566 if (cols == columns) {
7567 loctab->appendColumn(cols - 1);
7568 columns = loctab->ncols();
7569 cells = loctab->numberofcells;
7572 if (cols < columns) {
7573 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7574 Font const font = bv.textMetrics(&inset->text()).
7576 inset->setText(buf.substr(op, p - op), font,
7577 buffer().params().track_changes);
7583 // we can only set this if we are not too far right
7584 if (cols < columns) {
7585 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7586 Font const font = bv.textMetrics(&inset->text()).
7588 inset->setText(buf.substr(op, p - op), font,
7589 buffer().params().track_changes);
7593 // append row if necessary
7594 if (row == rows && p < len - 1) {
7595 loctab->appendRow(row - 1);
7596 rows = loctab->nrows();
7597 cells = loctab->numberofcells;
7600 cell = loctab->cellIndex(row, cols);
7606 // check for the last cell if there is no trailing '\n'
7607 if (cell < cells && op < len) {
7608 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7609 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7610 inset->setText(buf.substr(op, len - op), font,
7611 buffer().params().track_changes);
7617 void InsetTabular::addPreview(DocIterator const & inset_pos,
7618 PreviewLoader & loader) const
7620 DocIterator cell_pos = inset_pos;
7622 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7623 for (row_type r = 0; r < tabular.nrows(); ++r) {
7624 for (col_type c = 0; c < tabular.ncols(); ++c) {
7625 cell_pos.top().idx() = tabular.cellIndex(r, c);
7626 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7632 bool InsetTabular::completionSupported(Cursor const & cur) const
7634 Cursor const & bvCur = cur.bv().cursor();
7635 if (&bvCur.inset() != this)
7637 return cur.text()->completionSupported(cur);
7641 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7643 return completionSupported(cur);
7647 bool InsetTabular::automaticInlineCompletion() const
7649 return lyxrc.completion_inline_text;
7653 bool InsetTabular::automaticPopupCompletion() const
7655 return lyxrc.completion_popup_text;
7659 bool InsetTabular::showCompletionCursor() const
7661 return lyxrc.completion_cursor_text &&
7662 (lyxrc.completion_inline_text || lyxrc.completion_popup_text);
7666 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7668 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : nullptr;
7672 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7674 if (!completionSupported(cur))
7676 return cur.text()->completionPrefix(cur);
7680 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7682 if (!completionSupported(cur))
7685 return cur.text()->insertCompletion(cur, s, finished);
7689 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7690 Dimension & dim) const
7692 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7693 tm.completionPosAndDim(cur, x, y, dim);
7697 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7699 istringstream data(in);
7701 lex.setStream(data);
7708 if (!lex || token != "tabular") {
7709 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7713 // This is part of the inset proper that is usually swallowed
7714 // by Buffer::readInset
7716 if (!lex || token != "Tabular") {
7717 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7725 string InsetTabular::params2string(InsetTabular const & inset)
7728 data << "tabular" << ' ';
7730 data << "\\end_inset\n";
7735 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7737 for (col_type c = 0; c < tabular.ncols(); ++c) {
7738 for (row_type r = 0; r < tabular.nrows(); ++r) {
7739 if (!tabular.isPartOfMultiColumn(r,c) &&
7740 !tabular.isPartOfMultiRow(r,c))
7743 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7744 ParagraphList::iterator it = parlist.begin();
7745 ParagraphList::iterator const en = parlist.end();
7746 for (; it != en; ++it)
7747 it->setLayout(dc.plainLayout());