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/convert.h"
61 #include "support/debug.h"
62 #include "support/docstream.h"
63 #include "support/FileName.h"
64 #include "support/gettext.h"
65 #include "support/lassert.h"
66 #include "support/lstrings.h"
67 #include "support/unique_ptr.h"
76 using namespace lyx::support;
82 using cap::dirtyTabularStack;
83 using cap::tabularStackDirty;
85 using graphics::PreviewLoader;
87 using frontend::Painter;
88 using frontend::Clipboard;
90 namespace Alert = frontend::Alert;
95 int const ADD_TO_HEIGHT = 2; // in cell
96 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
97 int const default_line_space = 10; // ?
98 int const WIDTH_OF_LINE = 5; // space between double lines
102 unique_ptr<Tabular> paste_tabular;
105 struct TabularFeature {
106 Tabular::Feature action;
112 TabularFeature tabularFeature[] =
114 // the SET/UNSET actions are used by the table dialog,
115 // the TOGGLE actions by the table toolbar buttons
116 // FIXME: these values have been hardcoded in InsetMathGrid and other
118 { Tabular::APPEND_ROW, "append-row", false },
119 { Tabular::APPEND_COLUMN, "append-column", false },
120 { Tabular::DELETE_ROW, "delete-row", false },
121 { Tabular::DELETE_COLUMN, "delete-column", false },
122 { Tabular::COPY_ROW, "copy-row", false },
123 { Tabular::COPY_COLUMN, "copy-column", false },
124 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
125 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
126 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
127 { Tabular::MOVE_ROW_UP, "move-row-up", false },
128 { Tabular::SET_LINE_TOP, "set-line-top", true },
129 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
130 { Tabular::SET_LTRIM_TOP, "set-ltrim-top", true },
131 { Tabular::SET_LTRIM_BOTTOM, "set-ltrim-bottom", true },
132 { Tabular::SET_RTRIM_TOP, "set-rtrim-top", true },
133 { Tabular::SET_RTRIM_BOTTOM, "set-rtrim-bottom", true },
134 { Tabular::SET_LINE_LEFT, "set-line-left", true },
135 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
136 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
137 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
138 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
139 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
140 { Tabular::TOGGLE_LTRIM_TOP, "toggle-ltrim-top", false },
141 { Tabular::TOGGLE_LTRIM_BOTTOM, "toggle-ltrim-bottom", false },
142 { Tabular::TOGGLE_RTRIM_TOP, "toggle-rtrim-top", false },
143 { Tabular::TOGGLE_RTRIM_BOTTOM, "toggle-rtrim-bottom", false },
144 { Tabular::ALIGN_LEFT, "align-left", false },
145 { Tabular::ALIGN_RIGHT, "align-right", false },
146 { Tabular::ALIGN_CENTER, "align-center", false },
147 { Tabular::ALIGN_BLOCK, "align-block", false },
148 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
149 { Tabular::VALIGN_TOP, "valign-top", false },
150 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
151 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
152 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
153 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
154 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
155 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
156 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
157 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
158 { Tabular::MULTICOLUMN, "multicolumn", false },
159 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
160 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
161 { Tabular::MULTIROW, "multirow", false },
162 { Tabular::SET_MULTIROW, "set-multirow", false },
163 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
164 { Tabular::SET_MROFFSET, "set-mroffset", true },
165 { Tabular::SET_ALL_LINES, "set-all-lines", false },
166 { Tabular::RESET_FORMAL_DEFAULT, "reset-formal-default", false },
167 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
168 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
169 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
170 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
171 { Tabular::SET_PWIDTH, "set-pwidth", true },
172 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
173 { Tabular::TOGGLE_VARWIDTH_COLUMN, "toggle-varwidth-column", true },
174 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
175 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
176 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
177 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
178 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
179 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
180 { Tabular::SET_USEBOX, "set-usebox", true },
181 { Tabular::SET_LTHEAD, "set-lthead", true },
182 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
183 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
184 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
185 { Tabular::SET_LTFOOT, "set-ltfoot", true },
186 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
187 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
188 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
189 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
190 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
191 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
192 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
193 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
194 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
195 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
196 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
197 { Tabular::SET_BOOKTABS, "set-booktabs", false },
198 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
199 { Tabular::SET_TOP_SPACE, "set-top-space", true },
200 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
201 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
202 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
203 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
204 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
205 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
206 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
207 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
208 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
209 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
210 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
211 { Tabular::SET_INNER_LINES, "set-inner-lines", false },
212 { Tabular::LAST_ACTION, "", false }
216 string const tostr(LyXAlignment const & num)
221 case LYX_ALIGN_BLOCK:
225 case LYX_ALIGN_CENTER:
227 case LYX_ALIGN_RIGHT:
229 case LYX_ALIGN_LAYOUT:
231 case LYX_ALIGN_SPECIAL:
233 case LYX_ALIGN_DECIMAL:
240 string const tostr(Tabular::HAlignment const & num)
243 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
245 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
247 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
254 string const tostr(Tabular::VAlignment const & num)
257 case Tabular::LYX_VALIGN_TOP:
259 case Tabular::LYX_VALIGN_MIDDLE:
261 case Tabular::LYX_VALIGN_BOTTOM:
268 string const tostr(Tabular::BoxType const & num)
271 case Tabular::BOX_NONE:
273 case Tabular::BOX_PARBOX:
275 case Tabular::BOX_MINIPAGE:
277 case Tabular::BOX_VARWIDTH:
284 // I would have liked a fromstr template a lot better. (Lgb)
285 bool string2type(string const & str, LyXAlignment & num)
288 num = LYX_ALIGN_NONE;
289 else if (str == "block")
290 num = LYX_ALIGN_BLOCK;
291 else if (str == "left")
292 num = LYX_ALIGN_LEFT;
293 else if (str == "center")
294 num = LYX_ALIGN_CENTER;
295 else if (str == "right")
296 num = LYX_ALIGN_RIGHT;
297 else if (str == "decimal")
298 num = LYX_ALIGN_DECIMAL;
305 bool string2type(string const & str, Tabular::HAlignment & num)
308 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
309 else if (str == "center" )
310 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
311 else if (str == "right")
312 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
319 bool string2type(string const & str, Tabular::VAlignment & num)
322 num = Tabular::LYX_VALIGN_TOP;
323 else if (str == "middle" )
324 num = Tabular::LYX_VALIGN_MIDDLE;
325 else if (str == "bottom")
326 num = Tabular::LYX_VALIGN_BOTTOM;
333 bool string2type(string const & str, Tabular::BoxType & num)
336 num = Tabular::BOX_NONE;
337 else if (str == "parbox")
338 num = Tabular::BOX_PARBOX;
339 else if (str == "minipage")
340 num = Tabular::BOX_MINIPAGE;
341 else if (str == "varwidth")
342 num = Tabular::BOX_VARWIDTH;
349 bool string2type(string const & str, bool & num)
353 else if (str == "false")
361 bool getTokenValue(string const & str, char const * token, string & ret)
364 size_t token_length = strlen(token);
365 size_t pos = str.find(token);
367 if (pos == string::npos || pos + token_length + 1 >= str.length()
368 || str[pos + token_length] != '=')
370 pos += token_length + 1;
372 if (ch != '"' && ch != '\'') { // only read till next space
376 while (pos < str.length() - 1 && str[++pos] != ch)
383 bool getTokenValue(string const & str, char const * token, docstring & ret)
386 bool const success = getTokenValue(str, token, tmp);
387 ret = from_utf8(tmp);
392 bool getTokenValue(string const & str, char const * token, int & num)
396 if (!getTokenValue(str, token, tmp))
398 num = convert<int>(tmp);
403 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
406 return getTokenValue(str, token, tmp) && string2type(tmp, num);
410 bool getTokenValue(string const & str, char const * token,
411 Tabular::HAlignment & num)
414 return getTokenValue(str, token, tmp) && string2type(tmp, num);
418 bool getTokenValue(string const & str, char const * token,
419 Tabular::VAlignment & num)
422 return getTokenValue(str, token, tmp) && string2type(tmp, num);
426 bool getTokenValue(string const & str, char const * token,
427 Tabular::BoxType & num)
430 return getTokenValue(str, token, tmp) && string2type(tmp, num);
434 bool getTokenValue(string const & str, char const * token, bool & flag)
436 // set the flag always to false as this should be the default for bools
437 // not in the file-format.
440 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
444 bool getTokenValue(string const & str, char const * token, Length & len)
446 // set the length to be zero() as default as this it should be if not
447 // in the file format.
450 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
454 bool getTokenValue(string const & str, char const * token, Change & change, BufferParams & bp)
456 // set the change to be Change() as default as this it should be if not
457 // in the file format.
460 if (getTokenValue(str, token, tmp)) {
461 vector<string> const changedata = getVectorFromString(tmp, " ");
462 if (changedata.size() != 3) {
463 Alert::warning(_("Change tracking data incomplete"),
464 _("Change tracking information for tabular row/column "
465 "is incomplete. I will ignore this."));
468 BufferParams::AuthorMap const & am = bp.author_map_;
469 int aid = convert<int>(changedata[1]);
470 if (am.find(aid) == am.end()) {
471 // FIXME Use ErrorList
472 Alert::warning(_("Change tracking author index missing"),
473 bformat(_("A change tracking author information for index "
474 "%1$d is missing. This can happen after a wrong "
475 "merge by a version control system. In this case, "
476 "either fix the merge, or have this information "
477 "missing until the corresponding tracked changes "
478 "are merged or this user edits the file again.\n"),
480 bp.addAuthor(Author(aid));
482 istringstream is(changedata[2]);
485 if (changedata[0] == "inserted") {
486 change = Change(Change::INSERTED, am.find(aid)->second, ct);
488 } else if (changedata[0] == "deleted") {
489 change = Change(Change::DELETED, am.find(aid)->second, ct);
497 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
502 if (!getTokenValue(str, token, tmp))
504 if (tmp == "default") {
508 return isValidLength(tmp, &len);
512 void l_getline(istream & is, string & str)
515 while (str.empty()) {
517 if (!str.empty() && str[str.length() - 1] == '\r')
518 str.erase(str.length() - 1);
523 string const write_attribute(string const & name, T const & t)
525 string const s = tostr(t);
526 return s.empty() ? s : " " + name + "=\"" + s + "\"";
530 string const write_attribute(string const & name, string const & t)
532 return t.empty() ? t : " " + name + "=\"" + t + "\"";
537 string const write_attribute(string const & name, docstring const & t)
539 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
544 string const write_attribute(string const & name, bool const & b)
546 // we write only true attribute values so we remove a bit of the
547 // file format bloat for tabulars.
548 return b ? write_attribute(name, convert<string>(b)) : string();
553 string const write_attribute(string const & name, int const & i)
555 // we write only true attribute values so we remove a bit of the
556 // file format bloat for tabulars.
557 return i ? write_attribute(name, convert<string>(i)) : string();
562 string const write_attribute(string const & name, idx_type const & i)
564 // we write only true attribute values so we remove a bit of the
565 // file format bloat for tabulars.
566 return i ? write_attribute(name, convert<string>(i)) : string();
571 string const write_attribute(string const & name, Length const & value)
573 // we write only the value if we really have one same reason as above.
574 return value.zero() ? string() : write_attribute(name, value.asString());
577 string const write_attribute(string const & name, Change const & change, BufferParams const & bp)
579 odocstringstream ods;
580 if (change.inserted())
581 ods << from_ascii("inserted");
582 else if (change.deleted())
583 ods << from_ascii("deleted");
584 if (change.changed()) {
585 ods << " " << bp.authors().get(change.author).bufferId()
586 << " " << change.changetime;
587 return write_attribute(name, ods.str());
595 string const featureAsString(Tabular::Feature action)
597 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
598 if (tabularFeature[i].action == action)
599 return tabularFeature[i].feature;
605 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
607 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
608 for (; dit; dit.forwardChar())
609 if (dit.inTexted() && dit.depth() == 1
610 && dit.paragraph().find(align_d, false, false, dit.pos()))
617 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
619 InsetTableCell tail = InsetTableCell(head);
620 DocIterator const dit = separatorPos(&head, align_d);
621 hassep = static_cast<bool>(dit);
623 pos_type const psize = head.paragraphs().front().size();
624 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
625 tail.paragraphs().front().eraseChars(0,
626 dit.pos() < psize ? dit.pos() + 1 : psize, false);
633 /////////////////////////////////////////////////////////////////////
637 /////////////////////////////////////////////////////////////////////
640 Tabular::CellData::CellData(Buffer * buf)
643 multicolumn(Tabular::CELL_NORMAL),
644 multirow(Tabular::CELL_NORMAL),
645 alignment(LYX_ALIGN_CENTER),
646 valignment(LYX_VALIGN_TOP),
654 top_line_rtrimmed(false),
655 top_line_ltrimmed(false),
656 bottom_line_rtrimmed(false),
657 bottom_line_ltrimmed(false),
660 inset(new InsetTableCell(buf))
662 inset->setBuffer(*buf);
666 Tabular::CellData::CellData(CellData const & cs)
669 multicolumn(cs.multicolumn),
670 multirow(cs.multirow),
671 mroffset(cs.mroffset),
672 alignment(cs.alignment),
673 valignment(cs.valignment),
674 decimal_hoffset(cs.decimal_hoffset),
675 decimal_width(cs.decimal_width),
677 top_line(cs.top_line),
678 bottom_line(cs.bottom_line),
679 left_line(cs.left_line),
680 right_line(cs.right_line),
681 top_line_rtrimmed(cs.top_line_rtrimmed),
682 top_line_ltrimmed(cs.top_line_ltrimmed),
683 bottom_line_rtrimmed(cs.bottom_line_rtrimmed),
684 bottom_line_ltrimmed(cs.bottom_line_ltrimmed),
687 align_special(cs.align_special),
689 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
693 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
699 multicolumn = cs.multicolumn;
700 multirow = cs.multirow;
701 mroffset = cs.mroffset;
702 alignment = cs.alignment;
703 valignment = cs.valignment;
704 decimal_hoffset = cs.decimal_hoffset;
705 decimal_width = cs.decimal_width;
706 voffset = cs.voffset;
707 top_line = cs.top_line;
708 bottom_line = cs.bottom_line;
709 left_line = cs.left_line;
710 right_line = cs.right_line;
711 top_line_rtrimmed = cs.top_line_rtrimmed;
712 top_line_ltrimmed = cs.top_line_ltrimmed;
713 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
714 bottom_line_ltrimmed = cs.bottom_line_ltrimmed;
717 align_special = cs.align_special;
718 p_width = cs.p_width;
719 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
723 Tabular::RowData::RowData()
726 top_space_default(false),
727 bottom_space_default(false),
728 interline_space_default(false),
739 Tabular::ColumnData::ColumnData()
740 : alignment(LYX_ALIGN_CENTER),
741 valignment(LYX_VALIGN_TOP),
749 Tabular::ltType::ltType()
750 : set(false), topDL(false),
756 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
758 init(buffer, rows_arg, columns_arg);
762 void Tabular::setBuffer(Buffer & buffer)
765 for (row_type i = 0; i < nrows(); ++i)
766 for (col_type j = 0; j < ncols(); ++j)
767 cell_info[i][j].inset->setBuffer(*buffer_);
771 // activates all lines and sets all widths to 0
772 void Tabular::init(Buffer * buf, row_type rows_arg,
773 col_type columns_arg)
776 row_info = row_vector(rows_arg);
777 column_info = column_vector(columns_arg);
778 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
779 row_info.reserve(10);
780 column_info.reserve(10);
781 cell_info.reserve(100);
783 is_long_tabular = false;
784 tabular_valignment = LYX_VALIGN_MIDDLE;
785 tabular_width = Length();
786 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
788 use_booktabs = false;
789 // set silly default lines
790 for (row_type r = 0; r < nrows(); ++r)
791 for (col_type c = 0; c < ncols(); ++c) {
792 cell_info[r][c].inset->setBuffer(*buffer_);
793 cell_info[r][c].top_line = true;
794 cell_info[r][c].left_line = true;
795 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
796 cell_info[r][c].right_line = c == ncols() - 1;
801 void Tabular::deleteRow(row_type const row, bool const force)
803 // Not allowed to delete last row
807 bool const ct = force ? false : buffer().params().track_changes;
809 for (col_type c = 0; c < ncols(); ++c) {
810 // Care about multirow cells
811 if (row + 1 < nrows() &&
812 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
813 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
814 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
818 row_info[row].change.setDeleted();
820 row_info.erase(row_info.begin() + row);
821 cell_info.erase(cell_info.begin() + row);
827 void Tabular::copyRow(row_type const row)
829 insertRow(row, true);
833 void Tabular::appendRow(row_type row)
835 insertRow(row, false);
839 void Tabular::insertRow(row_type const row, bool copy)
841 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
842 cell_info.insert(cell_info.begin() + row + 1,
843 cell_vector(0, CellData(buffer_)));
845 for (col_type c = 0; c < ncols(); ++c) {
846 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
847 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
848 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
849 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
853 for (col_type c = 0; c < ncols(); ++c) {
854 if (isPartOfMultiRow(row, c))
856 // inherit line settings
857 idx_type const i = cellIndex(row + 1, c);
858 idx_type const j = cellIndex(row, c);
859 setLeftLine(i, leftLine(j));
860 setRightLine(i, rightLine(j));
861 setTopLine(i, topLine(j));
862 if (topLine(j) && bottomLine(j)) {
863 setBottomLine(i, true);
864 setBottomLine(j, false);
867 if (buffer().params().track_changes) {
868 row_info[row + 1].change.setInserted();
874 void Tabular::moveColumn(col_type col, ColDirection direction)
876 if (direction == Tabular::LEFT)
879 std::swap(column_info[col], column_info[col + 1]);
881 for (row_type r = 0; r < nrows(); ++r) {
882 std::swap(cell_info[r][col], cell_info[r][col + 1]);
883 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
884 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
886 idx_type const i = cellIndex(r, col);
887 idx_type const j = cellIndex(r, col + 1);
888 if (buffer().params().track_changes) {
889 cellInfo(i).inset->setChange(Change(Change::INSERTED));
890 cellInfo(j).inset->setChange(Change(Change::INSERTED));
897 void Tabular::moveRow(row_type row, RowDirection direction)
899 if (direction == Tabular::UP)
902 std::swap(row_info[row], row_info[row + 1]);
904 for (col_type c = 0; c < ncols(); ++c) {
905 std::swap(cell_info[row][c], cell_info[row + 1][c]);
906 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
907 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
909 idx_type const i = cellIndex(row, c);
910 idx_type const j = cellIndex(row + 1, c);
911 if (buffer().params().track_changes) {
912 cellInfo(i).inset->setChange(Change(Change::INSERTED));
913 cellInfo(j).inset->setChange(Change(Change::INSERTED));
920 void Tabular::deleteColumn(col_type const col, bool const force)
922 // Not allowed to delete last column
926 bool const ct = force ? false : buffer().params().track_changes;
928 for (row_type r = 0; r < nrows(); ++r) {
929 // Care about multicolumn cells
930 if (col + 1 < ncols() &&
931 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
932 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
933 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
936 cell_info[r].erase(cell_info[r].begin() + col);
939 column_info[col].change.setDeleted();
941 column_info.erase(column_info.begin() + col);
946 void Tabular::copyColumn(col_type const col)
948 insertColumn(col, true);
952 void Tabular::appendColumn(col_type col)
954 insertColumn(col, false);
958 void Tabular::insertColumn(col_type const col, bool copy)
960 bool const ct = buffer().params().track_changes;
961 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
963 for (row_type r = 0; r < nrows(); ++r) {
964 cell_info[r].insert(cell_info[r].begin() + col + 1,
965 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
966 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
967 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
970 for (row_type r = 0; r < nrows(); ++r) {
971 // inherit line settings
972 idx_type const i = cellIndex(r, col + 1);
973 idx_type const j = cellIndex(r, col);
974 setBottomLine(i, bottomLine(j));
975 setTopLine(i, topLine(j));
976 setLeftLine(i, leftLine(j));
977 setRightLine(i, rightLine(j));
978 if (rightLine(i) && rightLine(j)) {
979 setRightLine(j, false);
983 column_info[col + 1].change.setInserted();
989 void Tabular::updateIndexes()
994 for (row_type row = 0; row < nrows(); ++row)
995 for (col_type column = 0; column < ncols(); ++column) {
996 if (!isPartOfMultiColumn(row, column)
997 && !isPartOfMultiRow(row, column))
999 if (isPartOfMultiRow(row, column))
1000 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1002 cell_info[row][column].cellno = numberofcells - 1;
1005 rowofcell.resize(numberofcells);
1006 columnofcell.resize(numberofcells);
1008 // reset column and row of cells and update their width, alignment and ct status
1009 for (row_type row = 0; row < nrows(); ++row) {
1010 for (col_type column = 0; column < ncols(); ++column) {
1011 if (isPartOfMultiColumn(row, column)) {
1012 cell_info[row][column].inset->toggleMultiCol(true);
1015 cell_info[row][column].inset->toggleMultiCol(false);
1016 // columnofcell needs to be called before setting width and alignment
1017 // multirow cells inherit the width from the column width
1018 if (!isPartOfMultiRow(row, column)) {
1019 columnofcell[i] = column;
1022 setFixedWidth(row, column);
1023 if (isPartOfMultiRow(row, column)) {
1024 cell_info[row][column].inset->toggleMultiRow(true);
1027 cell_info[row][column].inset->toggleMultiRow(false);
1028 cell_info[row][column].inset->setContentAlignment(
1029 getAlignment(cellIndex(row, column)));
1030 if (buffer().params().track_changes) {
1031 if (row_info[row].change.changed())
1032 cell_info[row][column].inset->setChange(row_info[row].change);
1033 if (column_info[column].change.changed())
1034 cell_info[row][column].inset->setChange(column_info[column].change);
1042 idx_type Tabular::numberOfCellsInRow(row_type const row) const
1044 idx_type result = 0;
1045 for (col_type c = 0; c < ncols(); ++c)
1046 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1052 bool Tabular::topLine(idx_type const cell) const
1054 return cellInfo(cell).top_line;
1058 bool Tabular::bottomLine(idx_type const cell) const
1060 return cellInfo(cell).bottom_line;
1064 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1066 if (use_booktabs && !ignore_bt)
1068 return cellInfo(cell).left_line;
1072 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1074 if (use_booktabs && !ignore_bt)
1076 return cellInfo(cell).right_line;
1080 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1083 return make_pair(false, false);
1084 return make_pair(cellInfo(cell).top_line_ltrimmed,
1085 cellInfo(cell).top_line_rtrimmed);
1089 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1092 return make_pair(false, false);
1093 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1094 cellInfo(cell).bottom_line_rtrimmed);
1098 int Tabular::interRowSpace(row_type row) const
1100 if (!row || row >= nrows())
1103 int const interline_space = row_info[row - 1].interline_space_default ?
1104 default_line_space :
1105 row_info[row - 1].interline_space.inPixels(width());
1106 if (rowTopLine(row) && rowBottomLine(row - 1))
1107 return interline_space + WIDTH_OF_LINE;
1108 return interline_space;
1112 int Tabular::interColumnSpace(idx_type cell) const
1114 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1115 if (rightLine(cell) && nextcol < ncols()
1116 && leftLine(cellIndex(cellRow(cell), nextcol)))
1117 return WIDTH_OF_LINE;
1122 int Tabular::cellWidth(idx_type cell) const
1125 col_type const span = columnSpan(cell);
1126 col_type const col = cellColumn(cell);
1127 for(col_type c = col; c < col + span ; ++c)
1128 w += column_info[c].width;
1133 int Tabular::cellHeight(idx_type cell) const
1135 row_type const span = rowSpan(cell);
1136 row_type const row = cellRow(cell);
1138 for(row_type r = row; r < row + span ; ++r) {
1139 h += rowAscent(r) + rowDescent(r);
1140 if (r != row + span - 1)
1141 h += interRowSpace(r + 1);
1148 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1150 vector<int> max_dwidth(ncols(), 0);
1151 // collect max. fixed width of column
1152 map<col_type, int> max_pwidth;
1153 // collect max. variable width of column
1154 map<col_type, int> max_width;
1156 for(col_type c = 0; c < ncols(); ++c)
1157 for(row_type r = 0; r < nrows(); ++r) {
1158 idx_type const i = cellIndex(r, c);
1159 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1160 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1161 if (!getPWidth(i).zero())
1162 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1163 else if (!column_info[c].varwidth)
1164 max_width[c] = max(max_width[c], cell_info[r][c].width);
1167 // If we have a fixed tabular width, we take this into account
1168 Length tab_width = tabular_width;
1169 bool const tabularx = hasVarwidthColumn();
1170 if (tabularx && tab_width.zero())
1171 // If no tabular width is specified with X columns,
1172 // we use 100% colwidth
1173 tab_width = Length(100, Length::PCW);
1175 if (!tab_width.zero()) {
1176 restwidth = mi.base.inPixels(tab_width);
1177 // Subtract the fixed widths from the table width
1178 for (auto const & w : max_pwidth)
1179 restwidth -= w.second;
1182 // If we have a fixed width, distribute the available table width
1183 // (minus the fixed widths) to the variable-width columns
1185 int restcols = ncols() - max_pwidth.size();
1186 if ((restwidth > 0) && (restcols != 0))
1187 vcolwidth = restwidth / restcols;
1189 // Now consider that some variable width columns exceed the vcolwidth
1190 if (vcolwidth > 0) {
1191 bool changed = false;
1192 for (auto const & w : max_width) {
1193 if (tabularx || w.second > vcolwidth) {
1195 restwidth -= w.second;
1199 if (changed && restwidth > 0)
1200 vcolwidth = restwidth / restcols;
1203 bool update = false;
1204 // for each col get max of single col cells
1205 for(col_type c = 0; c < ncols(); ++c) {
1207 for(row_type r = 0; r < nrows(); ++r) {
1208 idx_type const i = cellIndex(r, c);
1209 if (columnSpan(i) == 1) {
1210 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1211 && cell_info[r][c].decimal_width != 0)
1212 new_width = max(new_width, cellInfo(i).width
1213 + max_dwidth[c] - cellInfo(i).decimal_width);
1214 else if (getPWidth(i).zero() && vcolwidth > 0) {
1215 if (tabularx && !column_info[c].varwidth)
1216 new_width = max(new_width, cellInfo(i).width);
1218 new_width = vcolwidth;
1220 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1222 new_width = max(new_width, cellInfo(i).width);
1226 if (column_info[c].width != new_width) {
1227 column_info[c].width = new_width;
1228 // Do not trigger update when no space is left for variable
1229 // columns, as this will loop
1230 update = tab_width.zero() || restwidth > 0;
1233 // update col widths to fit merged cells
1234 for(col_type c = 0; c < ncols(); ++c)
1235 for(row_type r = 0; r < nrows(); ++r) {
1236 idx_type const i = cellIndex(r, c);
1237 int const span = columnSpan(i);
1238 if (span == 1 || c > cellColumn(i))
1242 for(col_type j = c; j < c + span ; ++j)
1243 old_width += column_info[j].width;
1245 if (cellInfo(i).width > old_width) {
1246 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1247 // Do not trigger update when no space is left for variable
1248 // columns, as this will loop
1249 update = tab_width.zero() || restwidth > 0;
1257 int Tabular::width() const
1260 for (col_type c = 0; c < ncols(); ++c)
1261 width += column_info[c].width;
1266 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1269 col_type const col = cellColumn(cell);
1270 // set alignment for the whole row if we are not in a multicolumn cell,
1271 // exclude possible multicolumn cells in the row
1272 if (!isMultiColumn(cell)) {
1273 for (row_type r = 0; r < nrows(); ++r) {
1274 // only if the column has no width the multirow inherits the
1275 // alignment of the column, otherwise it is left aligned
1276 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1277 && !isMultiColumn(cellIndex(r, col))) {
1278 cell_info[r][col].alignment = align;
1279 cell_info[r][col].inset->setContentAlignment(align);
1281 if ((isMultiRow(cellIndex(r, col)) && has_width)
1282 && !isMultiColumn(cellIndex(r, col))) {
1283 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1284 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1287 column_info[col].alignment = align;
1288 docstring & dpoint = column_info[col].decimal_point;
1289 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1290 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1291 dpoint = tlang->decimalSeparator();
1294 cellInfo(cell).alignment = align;
1295 cellInset(cell)->setContentAlignment(align);
1300 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1303 if (!isMultiColumn(cell) || onlycolumn)
1304 column_info[cellColumn(cell)].valignment = align;
1306 cellInfo(cell).valignment = align;
1313 * Allow line and paragraph breaks for fixed width multicol/multirow cells
1314 * or disallow them, merge cell paragraphs and reset layout to standard
1315 * for variable width multicol cells.
1317 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1318 bool const fixedWidth, bool const multicol,
1319 bool const multirow)
1321 inset->toggleFixedWidth(fixedWidth);
1322 if (!multirow && (fixedWidth || !multicol))
1325 // merge all paragraphs to one
1326 BufferParams const & bp = cur.bv().buffer().params();
1327 while (inset->paragraphs().size() > 1)
1328 mergeParagraph(bp, inset->paragraphs(), 0);
1330 // This is relevant for multirows
1335 ParagraphList::iterator pit = inset->paragraphs().begin();
1336 for (; pit != inset->paragraphs().end(); ++pit) {
1337 for (pos_type j = 0; j != pit->size(); ++j) {
1338 if (pit->isNewline(j))
1339 pit->eraseChar(j, bp.track_changes);
1345 // undo information has already been recorded
1346 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1347 bp.documentClass().plainLayoutName());
1354 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1355 Length const & width)
1357 col_type const c = cellColumn(cell);
1359 column_info[c].p_width = width;
1360 // reset the vertical alignment to top if the fixed width
1361 // is removed or zero because only fixed width columns can
1362 // have a vertical alignment
1363 if (column_info[c].p_width.zero())
1364 column_info[c].valignment = LYX_VALIGN_TOP;
1365 for (row_type r = 0; r < nrows(); ++r) {
1366 idx_type const cidx = cellIndex(r, c);
1367 // because of multicolumns
1368 toggleFixedWidth(cur, cellInset(cidx).get(),
1369 !getPWidth(cidx).zero(), isMultiColumn(cidx),
1371 if (isMultiRow(cidx))
1372 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1374 // cur can become invalid after paragraphs were merged
1379 bool Tabular::setFixedWidth(row_type r, col_type c)
1381 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1382 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1383 || (multicol && !cell_info[r][c].p_width.zero());
1384 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1389 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1390 Length const & width)
1392 if (!isMultiColumn(cell))
1395 cellInfo(cell).p_width = width;
1396 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1397 isMultiColumn(cell), isMultiRow(cell));
1398 // cur can become invalid after paragraphs were merged
1404 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1406 column_info[cellColumn(cell)].varwidth = varwidth;
1411 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1413 cellInfo(cell).mroffset = mroffset;
1418 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1419 Tabular::Feature what)
1421 if (what == SET_SPECIAL_MULTICOLUMN)
1422 cellInfo(cell).align_special = special;
1424 column_info[cellColumn(cell)].align_special = special;
1428 void Tabular::setTopLine(idx_type i, bool line)
1430 cellInfo(i).top_line = line;
1434 void Tabular::setBottomLine(idx_type i, bool line)
1436 cellInfo(i).bottom_line = line;
1440 void Tabular::setTopLineLTrim(idx_type i, bool val)
1442 cellInfo(i).top_line_ltrimmed = val;
1446 void Tabular::setTopLineRTrim(idx_type i, bool val)
1448 cellInfo(i).top_line_rtrimmed = val;
1452 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1454 cellInfo(i).bottom_line_ltrimmed = val;
1458 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1460 cellInfo(i).bottom_line_rtrimmed = val;
1464 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1466 setTopLineLTrim(i, trim.first);
1467 setTopLineRTrim(i, trim.second);
1470 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1472 setBottomLineLTrim(i, trim.first);
1473 setBottomLineRTrim(i, trim.second);
1476 void Tabular::setLeftLine(idx_type cell, bool line)
1478 cellInfo(cell).left_line = line;
1482 void Tabular::setRightLine(idx_type cell, bool line)
1484 cellInfo(cell).right_line = line;
1487 bool Tabular::rowTopLine(row_type r) const
1489 bool all_rows_set = true;
1490 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1491 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1492 return all_rows_set;
1496 bool Tabular::rowBottomLine(row_type r) const
1498 bool all_rows_set = true;
1499 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1500 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1501 return all_rows_set;
1505 bool Tabular::columnLeftLine(col_type c) const
1512 for (row_type r = 0; r < nrows(); ++r) {
1513 idx_type const i = cellIndex(r, c);
1514 if (c == cellColumn(i)) {
1516 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1517 if (cellInfo(i).left_line || right)
1521 return 2 * nrows_left >= total;
1525 bool Tabular::columnRightLine(col_type c) const
1530 int nrows_right = 0;
1532 for (row_type r = 0; r < nrows(); ++r) {
1533 idx_type i = cellIndex(r, c);
1534 if (c == cellColumn(i) + columnSpan(i) - 1) {
1536 bool left = (c + 1 < ncols()
1537 && cellInfo(cellIndex(r, c + 1)).left_line)
1538 || c + 1 == ncols();
1539 if (cellInfo(i).right_line && left)
1543 return 2 * nrows_right >= total;
1547 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1549 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1550 return cellInfo(cell).alignment;
1552 return column_info[cellColumn(cell)].alignment;
1557 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1559 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1560 return cellInfo(cell).valignment;
1561 return column_info[cellColumn(cell)].valignment;
1565 int Tabular::offsetVAlignment() const
1567 // for top-alignment the first horizontal table line must be exactly at
1568 // the position of the base line of the surrounding text line
1569 // for bottom alignment, the same is for the last table line
1570 int offset_valign = 0;
1571 switch (tabular_valignment) {
1572 case Tabular::LYX_VALIGN_BOTTOM:
1573 offset_valign = rowAscent(0) - height();
1575 case Tabular::LYX_VALIGN_MIDDLE:
1576 offset_valign = (- height()) / 2 + rowAscent(0);
1578 case Tabular::LYX_VALIGN_TOP:
1579 offset_valign = rowAscent(0);
1582 return offset_valign;
1586 Length const Tabular::getPWidth(idx_type cell) const
1588 if (isMultiColumn(cell))
1589 return cellInfo(cell).p_width;
1590 return column_info[cellColumn(cell)].p_width;
1594 Length const Tabular::getMROffset(idx_type cell) const
1596 return cellInfo(cell).mroffset;
1600 int Tabular::textHOffset(idx_type cell) const
1602 // the LaTeX Way :-(
1603 int x = WIDTH_OF_LINE;
1605 int const w = cellWidth(cell) - cellInfo(cell).width;
1607 switch (getAlignment(cell)) {
1608 case LYX_ALIGN_CENTER:
1611 case LYX_ALIGN_RIGHT:
1614 case LYX_ALIGN_DECIMAL: {
1615 // we center when no decimal point
1616 if (cellInfo(cell).decimal_width == 0) {
1620 col_type const c = cellColumn(cell);
1621 int max_dhoffset = 0;
1622 for(row_type r = 0; r < row_info.size() ; ++r) {
1623 idx_type const i = cellIndex(r, c);
1624 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1625 && cellInfo(i).decimal_width != 0)
1626 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1628 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1631 // LYX_ALIGN_LEFT: nothing :-)
1639 int Tabular::textVOffset(idx_type cell) const
1641 int voffset = cellInfo(cell).voffset;
1642 if (isMultiRow(cell)) {
1643 row_type const row = cellRow(cell);
1644 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1650 idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1653 idx_type const numcells = numberOfCellsInRow(row);
1654 // we check against numcells to make sure we do not crash if all the
1655 // cells are multirow (bug #7535), but in that case our return value
1656 // is really invalid, i.e., it is NOT the first cell in the row. but
1657 // i do not know what to do here. (rgh)
1658 while (c < numcells - 1
1659 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1660 || (ct && column_info[c].change.deleted())))
1662 return cell_info[row][c].cellno;
1666 idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1668 col_type c = ncols() - 1;
1669 // of course we check against 0 so we don't crash. but we have the same
1670 // problem as in the previous routine: if all the cells are part of a
1671 // multirow or part of a multi column, then our return value is invalid.
1673 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1674 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1675 || (ct && column_info[c].change.deleted())))
1677 return cell_info[row][c].cellno;
1681 row_type Tabular::getFirstRow(bool const ct) const
1686 // exclude deleted rows if ct == true
1687 while (r < nrows() && row_info[r].change.deleted())
1693 row_type Tabular::getLastRow(bool const ct) const
1695 row_type r = nrows() - 1;
1698 // exclude deleted rows if ct == true
1699 while (r > 0 && row_info[r].change.deleted())
1705 row_type Tabular::cellRow(idx_type cell) const
1707 if (cell >= numberofcells)
1711 return rowofcell[cell];
1715 col_type Tabular::cellColumn(idx_type cell) const
1717 if (cell >= numberofcells)
1721 return columnofcell[cell];
1725 void Tabular::write(ostream & os) const
1729 << write_attribute("version", 3)
1730 << write_attribute("rows", nrows())
1731 << write_attribute("columns", ncols())
1733 // global longtable options
1735 << write_attribute("rotate", rotate)
1736 << write_attribute("booktabs", use_booktabs)
1737 << write_attribute("islongtable", is_long_tabular)
1738 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1739 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1740 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1741 << write_attribute("headTopDL", endhead.topDL)
1742 << write_attribute("headBottomDL", endhead.bottomDL)
1743 << write_attribute("footTopDL", endfoot.topDL)
1744 << write_attribute("footBottomDL", endfoot.bottomDL)
1745 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1746 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1747 << write_attribute("lastFootEmpty", endlastfoot.empty);
1748 // longtables cannot be aligned vertically
1749 if (!is_long_tabular)
1750 os << write_attribute("tabularvalignment", tabular_valignment);
1751 os << write_attribute("tabularwidth", tabular_width);
1752 if (is_long_tabular)
1753 os << write_attribute("longtabularalignment", longtabular_alignment);
1755 for (col_type c = 0; c < ncols(); ++c) {
1757 << write_attribute("alignment", column_info[c].alignment);
1758 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1759 os << write_attribute("decimal_point", column_info[c].decimal_point);
1760 os << write_attribute("change", column_info[c].change, buffer().params())
1761 << write_attribute("valignment", column_info[c].valignment)
1762 << write_attribute("width", column_info[c].p_width.asString())
1763 << write_attribute("varwidth", column_info[c].varwidth)
1764 << write_attribute("special", column_info[c].align_special)
1767 for (row_type r = 0; r < nrows(); ++r) {
1768 static const string def("default");
1770 if (row_info[r].top_space_default)
1771 os << write_attribute("topspace", def);
1773 os << write_attribute("topspace", row_info[r].top_space);
1774 if (row_info[r].bottom_space_default)
1775 os << write_attribute("bottomspace", def);
1777 os << write_attribute("bottomspace", row_info[r].bottom_space);
1778 if (row_info[r].interline_space_default)
1779 os << write_attribute("interlinespace", def);
1781 os << write_attribute("interlinespace", row_info[r].interline_space);
1782 os << write_attribute("change", row_info[r].change, buffer().params())
1783 << write_attribute("endhead", row_info[r].endhead)
1784 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1785 << write_attribute("endfoot", row_info[r].endfoot)
1786 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1787 << write_attribute("newpage", row_info[r].newpage)
1788 << write_attribute("caption", row_info[r].caption)
1790 for (col_type c = 0; c < ncols(); ++c) {
1792 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1793 << write_attribute("multirow", cell_info[r][c].multirow)
1794 << write_attribute("mroffset", cell_info[r][c].mroffset)
1795 << write_attribute("alignment", cell_info[r][c].alignment)
1796 << write_attribute("valignment", cell_info[r][c].valignment)
1797 << write_attribute("topline", cell_info[r][c].top_line)
1798 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1799 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1800 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1801 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1802 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1803 << write_attribute("leftline", cell_info[r][c].left_line)
1804 << write_attribute("rightline", cell_info[r][c].right_line)
1805 << write_attribute("rotate", cell_info[r][c].rotate)
1806 << write_attribute("usebox", cell_info[r][c].usebox)
1807 << write_attribute("width", cell_info[r][c].p_width)
1808 << write_attribute("special", cell_info[r][c].align_special)
1810 os << "\\begin_inset ";
1811 cell_info[r][c].inset->write(os);
1812 os << "\n\\end_inset\n"
1814 // FIXME This can be removed again once the mystery
1815 // crash has been resolved.
1820 os << "</lyxtabular>\n";
1824 void Tabular::read(Lexer & lex)
1827 istream & is = lex.getStream();
1829 l_getline(is, line);
1830 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1831 LASSERT(false, return);
1835 if (!getTokenValue(line, "version", version))
1837 LATTEST(version >= 2);
1840 if (!getTokenValue(line, "rows", rows_arg))
1843 if (!getTokenValue(line, "columns", columns_arg))
1845 init(buffer_, rows_arg, columns_arg);
1846 l_getline(is, line);
1847 if (!prefixIs(line, "<features")) {
1848 lyxerr << "Wrong tabular format (expected <features ...> got"
1849 << line << ')' << endl;
1852 getTokenValue(line, "rotate", rotate);
1853 getTokenValue(line, "booktabs", use_booktabs);
1854 getTokenValue(line, "islongtable", is_long_tabular);
1855 getTokenValue(line, "tabularvalignment", tabular_valignment);
1856 getTokenValue(line, "tabularwidth", tabular_width);
1857 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1858 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1859 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1860 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1861 getTokenValue(line, "headTopDL", endhead.topDL);
1862 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1863 getTokenValue(line, "footTopDL", endfoot.topDL);
1864 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1865 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1866 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1867 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1869 for (col_type c = 0; c < ncols(); ++c) {
1871 if (!prefixIs(line,"<column")) {
1872 lyxerr << "Wrong tabular format (expected <column ...> got"
1873 << line << ')' << endl;
1876 getTokenValue(line, "alignment", column_info[c].alignment);
1877 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1878 getTokenValue(line, "valignment", column_info[c].valignment);
1879 getTokenValue(line, "width", column_info[c].p_width);
1880 getTokenValue(line, "special", column_info[c].align_special);
1881 getTokenValue(line, "varwidth", column_info[c].varwidth);
1882 getTokenValue(line, "change", column_info[c].change, buffer().params());
1885 for (row_type i = 0; i < nrows(); ++i) {
1886 l_getline(is, line);
1887 if (!prefixIs(line, "<row")) {
1888 lyxerr << "Wrong tabular format (expected <row ...> got"
1889 << line << ')' << endl;
1892 getTokenValue(line, "topspace", row_info[i].top_space,
1893 row_info[i].top_space_default);
1894 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1895 row_info[i].bottom_space_default);
1896 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1897 row_info[i].interline_space_default);
1898 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1899 getTokenValue(line, "endhead", row_info[i].endhead);
1900 getTokenValue(line, "endfoot", row_info[i].endfoot);
1901 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1902 getTokenValue(line, "newpage", row_info[i].newpage);
1903 getTokenValue(line, "caption", row_info[i].caption);
1904 getTokenValue(line, "change", row_info[i].change, buffer().params());
1905 for (col_type j = 0; j < ncols(); ++j) {
1906 l_getline(is, line);
1907 if (!prefixIs(line, "<cell")) {
1908 lyxerr << "Wrong tabular format (expected <cell ...> got"
1909 << line << ')' << endl;
1912 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1913 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1914 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1915 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1916 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1917 getTokenValue(line, "topline", cell_info[i][j].top_line);
1918 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1919 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1920 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1921 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1922 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1923 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1924 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1925 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1926 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1927 getTokenValue(line, "width", cell_info[i][j].p_width);
1929 getTokenValue(line, "special", cell_info[i][j].align_special);
1930 l_getline(is, line);
1931 if (prefixIs(line, "\\begin_inset")) {
1932 cell_info[i][j].inset->setBuffer(*buffer_);
1933 cell_info[i][j].inset->read(lex);
1934 l_getline(is, line);
1936 if (!prefixIs(line, "</cell>")) {
1937 lyxerr << "Wrong tabular format (expected </cell> got"
1938 << line << ')' << endl;
1942 l_getline(is, line);
1943 if (!prefixIs(line, "</row>")) {
1944 lyxerr << "Wrong tabular format (expected </row> got"
1945 << line << ')' << endl;
1949 while (!prefixIs(line, "</lyxtabular>")) {
1950 l_getline(is, line);
1956 bool Tabular::isMultiColumn(idx_type cell) const
1958 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1959 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1963 bool Tabular::hasMultiColumn(col_type c) const
1965 for (row_type r = 0; r < nrows(); ++r) {
1966 if (isMultiColumn(cellIndex(r, c)))
1973 bool Tabular::hasVarwidthColumn() const
1975 for (col_type c = 0; c < ncols(); ++c) {
1976 if (column_info[c].varwidth)
1983 bool Tabular::isVTypeColumn(col_type c) const
1985 for (row_type r = 0; r < nrows(); ++r) {
1986 idx_type idx = cellIndex(r, c);
1987 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1994 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1996 return cell_info[cellRow(cell)][cellColumn(cell)];
2000 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2002 return cell_info[cellRow(cell)][cellColumn(cell)];
2006 idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2007 bool const right_border)
2009 idx_type const col = cellColumn(cell);
2010 idx_type const row = cellRow(cell);
2011 for (idx_type i = 0; i < number; ++i)
2012 unsetMultiRow(cellIndex(row, col + i));
2014 // unsetting of multirow may have invalidated cell index
2015 cell = cellIndex(row, col);
2016 CellData & cs = cellInfo(cell);
2017 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2018 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2019 cs.alignment = column_info[col].alignment;
2020 setRightLine(cell, right_border);
2021 // non-fixed width multicolumns cannot have multiple paragraphs
2022 if (getPWidth(cell).zero()) {
2023 toggleFixedWidth(cur, cellInset(cell).get(),
2024 !getPWidth(cell).zero(), isMultiColumn(cell),
2026 // cur can become invalid after paragraphs were merged
2030 idx_type lastcell = cellIndex(row, col + number - 1);
2031 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2032 CellData & cs1 = cellInfo(cell + i);
2033 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2034 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2042 bool Tabular::isMultiRow(idx_type cell) const
2044 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2045 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2048 bool Tabular::hasMultiRow(row_type r) const
2050 for (col_type c = 0; c < ncols(); ++c) {
2051 if (isMultiRow(cellIndex(r, c)))
2057 idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2058 bool const bottom_border,
2059 LyXAlignment const halign)
2061 idx_type const col = cellColumn(cell);
2062 idx_type const row = cellRow(cell);
2063 for (idx_type i = 0; i < number; ++i)
2064 unsetMultiColumn(cellIndex(row + i, col));
2066 // unsetting of multirow may have invalidated cell index
2067 cell = cellIndex(row, col);
2068 CellData & cs = cellInfo(cell);
2069 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2070 cs.valignment = LYX_VALIGN_MIDDLE;
2071 // the horizontal alignment of multirow cells can only
2072 // be changed for the whole table row,
2073 // support changing this only for the multirow cell can be done via
2075 if (getPWidth(cell).zero())
2076 cs.alignment = halign;
2078 cs.alignment = LYX_ALIGN_LEFT;
2080 // Multirows cannot have multiple paragraphs
2081 if (getPWidth(cell).zero()) {
2082 toggleFixedWidth(cur, cellInset(cell).get(),
2083 !getPWidth(cell).zero(),
2084 isMultiColumn(cell), isMultiRow(cell));
2085 // cur can become invalid after paragraphs were merged
2089 // set the bottom line of the last selected cell
2090 setBottomLine(cell, bottom_border);
2092 for (idx_type i = 1; i < number; ++i) {
2093 CellData & cs1 = cell_info[row + i][col];
2094 cs1.multirow = CELL_PART_OF_MULTIROW;
2095 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2103 idx_type Tabular::columnSpan(idx_type cell) const
2105 row_type const row = cellRow(cell);
2106 col_type const col = cellColumn(cell);
2108 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2115 idx_type Tabular::rowSpan(idx_type cell) const
2117 col_type const column = cellColumn(cell);
2118 col_type row = cellRow(cell) + 1;
2119 while (row < nrows() && isPartOfMultiRow(row, column))
2122 return row - cellRow(cell);
2126 void Tabular::unsetMultiColumn(idx_type cell)
2128 if (!isMultiColumn(cell))
2131 row_type const row = cellRow(cell);
2132 col_type const col = cellColumn(cell);
2133 row_type const span = columnSpan(cell);
2134 for (col_type c = 0; c < span; ++c) {
2135 // in the table dialog the lines are set in every case
2136 // when unsetting a multicolumn this leads to an additional right
2137 // line for every cell that was part of the former multicolumn cell,
2138 // except if the cell is in the last column
2139 // therefore remove this line
2140 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2141 && (col + c) < (col + span - 1))
2142 cell_info[row][col + c].right_line = false;
2143 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2149 void Tabular::unsetMultiRow(idx_type cell)
2151 if (!isMultiRow(cell))
2154 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2155 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2156 row_type const row = cellRow(cell);
2157 col_type const col = cellColumn(cell);
2158 row_type const span = rowSpan(cell);
2159 for (row_type r = 0; r < span; ++r)
2160 cell_info[row + r][col].multirow = CELL_NORMAL;
2165 void Tabular::setRotateCell(idx_type cell, int value)
2167 cellInfo(cell).rotate = value;
2171 int Tabular::getRotateCell(idx_type cell) const
2173 return cellInfo(cell).rotate;
2177 bool Tabular::needRotating() const
2179 if (rotate && !is_long_tabular)
2181 for (row_type r = 0; r < nrows(); ++r)
2182 for (col_type c = 0; c < ncols(); ++c)
2183 if (cell_info[r][c].rotate != 0)
2189 bool Tabular::isLastCell(idx_type cell) const
2191 return cell + 1 >= numberofcells;
2195 idx_type Tabular::cellAbove(idx_type cell) const
2197 if (cellRow(cell) == 0)
2200 col_type const col = cellColumn(cell);
2201 row_type r = cellRow(cell) - 1;
2202 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2205 return cell_info[r][col].cellno;
2209 idx_type Tabular::cellBelow(idx_type cell) const
2211 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2212 if (nextrow < nrows())
2213 return cell_info[nextrow][cellColumn(cell)].cellno;
2218 idx_type Tabular::cellIndex(row_type row, col_type column) const
2220 LASSERT(column != npos && column < ncols(), column = 0);
2221 LASSERT(row != npos && row < nrows(), row = 0);
2222 return cell_info[row][column].cellno;
2226 void Tabular::setUsebox(idx_type cell, BoxType type)
2228 cellInfo(cell).usebox = type;
2232 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2234 if (getRotateCell(cell) == 0
2235 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2236 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2238 if (cellInfo(cell).usebox > 1)
2239 return cellInfo(cell).usebox;
2240 return useBox(cell);
2245 // This are functions used for the longtable support
2247 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2253 row_info[row].endfirsthead = flag;
2257 row_info[row].endhead = flag;
2262 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2265 hd.set = haveLTHead();
2266 return row_info[row].endhead;
2270 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2273 hd.set = haveLTFirstHead();
2274 return row_info[row].endfirsthead;
2278 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2284 row_info[row].endlastfoot = flag;
2288 row_info[row].endfoot = flag;
2293 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2296 fd.set = haveLTFoot();
2297 return row_info[row].endfoot;
2301 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2304 fd.set = haveLTLastFoot();
2305 return row_info[row].endlastfoot;
2309 void Tabular::setLTNewPage(row_type row, bool what)
2311 row_info[row].newpage = what;
2315 bool Tabular::getLTNewPage(row_type row) const
2317 return row_info[row].newpage;
2321 bool Tabular::haveLTHead(bool withcaptions) const
2323 if (!is_long_tabular)
2325 for (row_type i = 0; i < nrows(); ++i)
2326 if (row_info[i].endhead &&
2327 (withcaptions || !row_info[i].caption))
2333 bool Tabular::haveLTFirstHead(bool withcaptions) const
2335 if (!is_long_tabular || endfirsthead.empty)
2337 for (row_type r = 0; r < nrows(); ++r)
2338 if (row_info[r].endfirsthead &&
2339 (withcaptions || !row_info[r].caption))
2345 bool Tabular::haveLTFoot(bool withcaptions) const
2347 if (!is_long_tabular)
2349 for (row_type r = 0; r < nrows(); ++r)
2350 if (row_info[r].endfoot &&
2351 (withcaptions || !row_info[r].caption))
2357 bool Tabular::haveLTLastFoot(bool withcaptions) const
2359 if (!is_long_tabular || endlastfoot.empty)
2361 for (row_type r = 0; r < nrows(); ++r)
2362 if (row_info[r].endlastfoot &&
2363 (withcaptions || !row_info[r].caption))
2369 idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2371 idx_type i = getFirstCellInRow(row);
2373 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2374 setTopLine(i, false);
2375 setBottomLine(i, false);
2376 setLeftLine(i, false);
2377 setRightLine(i, false);
2378 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2379 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2380 setLTHead(row, true, endfirsthead, true);
2381 row_info[row].endfirsthead = true;
2384 unsetMultiColumn(i);
2385 // When unsetting a caption row, also all existing
2386 // captions in this row must be dissolved.
2388 row_info[row].caption = what;
2393 bool Tabular::ltCaption(row_type row) const
2395 return row_info[row].caption;
2399 bool Tabular::haveLTCaption(CaptionType captiontype) const
2401 if (!is_long_tabular)
2403 for (row_type r = 0; r < nrows(); ++r) {
2404 if (row_info[r].caption) {
2405 switch (captiontype) {
2406 case CAPTION_FIRSTHEAD:
2407 if (row_info[r].endfirsthead)
2411 if (row_info[r].endhead)
2415 if (row_info[r].endfoot)
2418 case CAPTION_LASTFOOT:
2419 if (row_info[r].endlastfoot)
2431 // end longtable support functions
2433 void Tabular::setRowAscent(row_type row, int height)
2435 if (row >= nrows() || row_info[row].ascent == height)
2437 row_info[row].ascent = height;
2441 void Tabular::setRowDescent(row_type row, int height)
2443 if (row >= nrows() || row_info[row].descent == height)
2445 row_info[row].descent = height;
2449 int Tabular::rowAscent(row_type row) const
2451 LASSERT(row < nrows(), row = 0);
2452 return row_info[row].ascent;
2456 int Tabular::rowDescent(row_type row) const
2458 LASSERT(row < nrows(), row = 0);
2459 return row_info[row].descent;
2463 int Tabular::height() const
2466 for (row_type row = 0; row < nrows(); ++row)
2467 height += rowAscent(row) + rowDescent(row) +
2473 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2475 LASSERT(row < nrows(), return false);
2476 LASSERT(column < ncols(), return false);
2477 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2481 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2483 LASSERT(row < nrows(), return false);
2484 LASSERT(column < ncols(), return false);
2485 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2489 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns,
2490 list<col_type> logical_columns) const
2492 // we only output complete row lines and the 1st row here, the rest
2493 // is done in Tabular::TeXBottomHLine(...)
2495 // get for each column the topline (if any)
2496 map<col_type, bool> topline, topltrims, toprtrims;
2498 bool have_trims = false;
2499 for (auto const & c : columns) {
2500 topline[c] = topLine(cellIndex(row, c));
2501 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2502 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2503 // If cell is part of a multirow and not the first cell of the
2504 // multirow, no line must be drawn.
2506 if (isMultiRow(cellIndex(row, c))
2507 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2509 topltrims[c] = false;
2510 toprtrims[c] = false;
2512 // copy trimming to multicolumn parts
2513 if (isPartOfMultiColumn(row, c)) {
2514 topltrims[c] = topltrims[c-1];
2515 toprtrims[c] = toprtrims[c-1];
2517 if (topline.find(c) != topline.end() && topline.find(c)->second)
2519 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2520 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2524 // do nothing if empty first row, or incomplete row line after
2525 row_type first = getFirstRow(!buffer().params().output_changes);
2526 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2529 // Is this the actual first row (excluding longtable caption row)?
2530 bool const realfirstrow = (row == first
2531 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2533 // only output complete row lines and the 1st row's clines
2534 if (nset == columns.size() && !have_trims) {
2536 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2540 } else if (realfirstrow || have_trims) {
2541 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2543 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2544 std::list<col_type>::const_iterator it2 = columns.begin();
2545 // We need to iterate over the logical columns here, but take care for
2547 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2552 if (topline.find(c)->second) {
2553 col_type offset = 0;
2554 for (col_type j = 0 ; j < c; ++j)
2555 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2557 // If the two iterators differ, we are in bidi with swapped columns
2558 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2559 while (isPartOfMultiColumn(row, c))
2562 if (topltrims.find(c) != topltrims.end()
2563 && topltrims.find(c)->second)
2565 col_type cstart = c;
2566 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2567 if (isMultiColumn(cellIndex(row, c))
2568 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2570 if (c > cstart && topltrims.find(c) != topltrims.end()
2571 && topltrims.find(c)->second) {
2572 if (!isPartOfMultiColumn(row, c))
2575 } else if (toprtrims.find(c) != toprtrims.end()
2576 && toprtrims.find(c)->second)
2580 for (col_type j = cstart ; j < c ; ++j)
2581 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2583 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2584 if (toprtrims.find(c) != toprtrims.end()
2585 && toprtrims.find(c)->second)
2590 os << "(" << trim << ")";
2591 if (firstcol > lastcol)
2592 // This can happen with bidi (swapped columns)
2593 os << "{" << lastcol << '-' << firstcol << "}";
2595 os << "{" << firstcol << '-' << lastcol << "}";
2596 if (c == columns.size() - 1)
2606 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns,
2607 list<col_type> logical_columns) const
2609 // we output bottomlines of row r and the toplines of row r+1
2610 // if the latter do not span the whole tabular
2612 // get the bottomlines of row r, and toplines in next row
2613 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2614 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2615 bool nextrowset = true;
2616 for (auto const & c : columns) {
2617 idx_type const idx = cellIndex(row, c);
2618 bottomline[c] = bottomLine(cellIndex(row, c));
2619 bottomltrims[c] = bottomLineTrim(idx).first;
2620 bottomrtrims[c] = bottomLineTrim(idx).second;
2621 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2622 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2623 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2624 // If cell is part of a multirow and not the last cell of the
2625 // multirow, no line must be drawn.
2627 if (isMultiRow(cellIndex(row, c))
2628 && isMultiRow(cellIndex(row + 1, c))
2629 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2630 bottomline[c] = false;
2632 bottomltrims[c] = false;
2633 bottomrtrims[c] = false;
2634 topltrims[c] = false;
2635 toprtrims[c] = false;
2637 // copy trimming in multicolumn parts
2638 if (isPartOfMultiColumn(row, c)) {
2639 topltrims[c] = topltrims[c-1];
2640 toprtrims[c] = toprtrims[c-1];
2641 bottomltrims[c] = bottomltrims[c-1];
2642 bottomrtrims[c] = bottomrtrims[c-1];
2645 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2648 // combine this row's bottom lines and next row's toplines if necessary
2650 bool have_trims = false;
2651 for (auto const & c : columns) {
2653 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2654 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2655 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2656 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2657 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2658 if (bottomline.find(c)->second)
2660 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2661 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2665 // do nothing if empty, OR incomplete row line with a topline in next row
2666 if (nset == 0 || (nextrowset && nset != columns.size()))
2669 if (nset == columns.size() && !have_trims) {
2671 os << (lastrow ? "\\bottomrule" : "\\midrule");
2675 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2677 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2678 std::list<col_type>::const_iterator it2 = columns.begin();
2679 // We need to iterate over the logical columns here, but take care for
2681 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2686 if (bottomline.find(c)->second) {
2687 col_type offset = 0;
2688 for (col_type j = 0 ; j < c; ++j)
2689 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2691 // If the two iterators differ, we are in bidi with swapped columns
2692 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2693 while (isPartOfMultiColumn(row, c))
2696 if (bottomltrims.find(c) != bottomltrims.end()
2697 && bottomltrims.find(c)->second)
2699 col_type cstart = c;
2700 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2701 if (isMultiColumn(cellIndex(row, c))
2703 && isPartOfMultiColumn(row, c + 1))
2706 && bottomltrims.find(c) != bottomltrims.end()
2707 && bottomltrims.find(c)->second) {
2708 if (!isPartOfMultiColumn(row, c))
2711 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2712 && bottomrtrims.find(c)->second)
2716 for (col_type j = cstart ; j < c ; ++j)
2717 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2719 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2720 if (bottomrtrims.find(c) != bottomrtrims.end()
2721 && bottomrtrims.find(c)->second)
2726 os << "(" << trim << ")";
2727 if (firstcol > lastcol)
2728 // This can happen with bidi (swapped columns)
2729 os << "{" << lastcol << '-' << firstcol << "}";
2731 os << "{" << firstcol << '-' << lastcol << "}";
2732 if (c == columns.size() - 1)
2742 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2743 bool & ismulticol, bool & ismultirow,
2744 bool const bidi) const
2746 row_type const r = cellRow(cell);
2747 if (is_long_tabular && row_info[r].caption)
2750 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2751 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2752 // figure out how to set the lines
2753 // we always set double lines to the right of the cell
2754 // or left in bidi RTL, respectively.
2755 col_type const c = cellColumn(cell);
2756 col_type const nextcol = c + columnSpan(cell);
2757 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2758 bool colright = columnRightLine(c);
2759 bool colleft = columnLeftLine(c);
2760 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2761 bool nextcellleft = nextcol < ncols()
2762 && leftLine(cellIndex(r, nextcol));
2763 bool coldouble = colright && nextcolleft;
2764 bool celldouble = rightLine(cell) && nextcellleft;
2766 ismulticol = (isMultiColumn(cell)
2767 || (c == 0 && colleft != leftLine(cell))
2768 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2769 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2770 || (coldouble != celldouble))
2773 // we center in multicol when no decimal point
2775 docstring const align_d = column_info[c].decimal_point;
2776 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2777 bool const nosep = !dit;
2778 ismulticol |= nosep;
2779 celldouble &= nosep;
2782 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2783 int latexcolspan = columnSpan(cell);
2784 for (col_type col = c; col < c + columnSpan(cell); ++col)
2785 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2789 os << "\\multicolumn{" << latexcolspan << "}{";
2790 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2791 || (!bidi && c == 0 && leftLine(cell))))
2793 if (bidi && celldouble)
2794 // add extra vertical line if we want a double one
2796 if (!cellInfo(cell).align_special.empty()) {
2797 os << cellInfo(cell).align_special;
2799 if (!getPWidth(cell).zero()) {
2801 case LYX_ALIGN_LEFT:
2802 os << ">{\\raggedright}";
2804 case LYX_ALIGN_RIGHT:
2805 os << ">{\\raggedleft}";
2807 case LYX_ALIGN_CENTER:
2808 os << ">{\\centering}";
2814 case LYX_VALIGN_TOP:
2817 case LYX_VALIGN_MIDDLE:
2820 case LYX_VALIGN_BOTTOM:
2825 << from_ascii(getPWidth(cell).asLatexString())
2829 case LYX_ALIGN_LEFT:
2832 case LYX_ALIGN_RIGHT:
2839 } // end if else !getPWidth
2840 } // end if else !cellinfo_of_cell
2841 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2843 if (!bidi && celldouble)
2844 // add extra vertical line if we want a double one
2847 } // end if ismulticol
2849 // we only need code for the first multirow cell
2850 ismultirow = isMultiRow(cell);
2852 os << "\\multirow{" << rowSpan(cell) << "}{";
2853 if (!getPWidth(cell).zero())
2854 os << from_ascii(getPWidth(cell).asLatexString());
2856 // we need to set a default value
2859 if (!getMROffset(cell).zero())
2860 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2862 } // end if ismultirow
2864 if (getRotateCell(cell) != 0)
2865 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2867 if (getUsebox(cell) == BOX_PARBOX) {
2870 case LYX_VALIGN_TOP:
2873 case LYX_VALIGN_MIDDLE:
2876 case LYX_VALIGN_BOTTOM:
2880 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2882 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2883 os << "\\begin{minipage}[";
2885 case LYX_VALIGN_TOP:
2888 case LYX_VALIGN_MIDDLE:
2891 case LYX_VALIGN_BOTTOM:
2895 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2897 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2898 os << "\\begin{varwidth}[";
2900 case LYX_VALIGN_TOP:
2903 case LYX_VALIGN_MIDDLE:
2906 case LYX_VALIGN_BOTTOM:
2910 os << "]{\\linewidth}\n";
2915 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2916 bool ismulticol, bool ismultirow) const
2918 row_type const r = cellRow(cell);
2919 if (is_long_tabular && row_info[r].caption)
2923 if (getUsebox(cell) == BOX_PARBOX)
2925 else if (getUsebox(cell) == BOX_MINIPAGE)
2926 os << breakln << "\\end{minipage}";
2927 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2928 os << breakln << "\\end{varwidth}";
2929 if (getRotateCell(cell) != 0)
2930 os << breakln << "\\end{turn}";
2938 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2939 OutputParams const & runparams,
2940 list<col_type> columns,
2941 list<col_type> logical_columns) const
2943 if (!is_long_tabular)
2947 // output caption which is in no header or footer
2948 if (haveLTCaption()) {
2949 for (row_type r = 0; r < nrows(); ++r) {
2950 if (row_info[r].caption &&
2951 !row_info[r].endfirsthead && !row_info[r].endhead &&
2952 !row_info[r].endfoot && !row_info[r].endlastfoot)
2953 TeXRow(os, r, runparams, columns, logical_columns);
2956 // output first header info
2957 if (haveLTFirstHead()) {
2958 if (endfirsthead.topDL)
2960 for (row_type r = 0; r < nrows(); ++r) {
2961 if (row_info[r].endfirsthead)
2962 TeXRow(os, r, runparams, columns, logical_columns);
2964 if (endfirsthead.bottomDL)
2966 os << "\\endfirsthead\n";
2968 // output header info
2970 if (endfirsthead.empty && !haveLTFirstHead())
2971 os << "\\endfirsthead\n";
2974 for (row_type r = 0; r < nrows(); ++r) {
2975 if (row_info[r].endhead)
2976 TeXRow(os, r, runparams, columns, logical_columns);
2978 if (endhead.bottomDL)
2980 os << "\\endhead\n";
2982 // output footer info
2986 for (row_type r = 0; r < nrows(); ++r) {
2987 if (row_info[r].endfoot)
2988 TeXRow(os, r, runparams, columns, logical_columns);
2990 if (endfoot.bottomDL)
2992 os << "\\endfoot\n";
2993 if (endlastfoot.empty && !haveLTLastFoot())
2994 os << "\\endlastfoot\n";
2996 // output lastfooter info
2997 if (haveLTLastFoot()) {
2998 if (endlastfoot.topDL)
3000 for (row_type r = 0; r < nrows(); ++r) {
3001 if (row_info[r].endlastfoot)
3002 TeXRow(os, r, runparams, columns, logical_columns);
3004 if (endlastfoot.bottomDL)
3006 os << "\\endlastfoot\n";
3011 bool Tabular::isValidRow(row_type row) const
3013 if (!is_long_tabular)
3015 return !row_info[row].endhead && !row_info[row].endfirsthead
3016 && !row_info[row].endfoot && !row_info[row].endlastfoot
3017 && !row_info[row].caption;
3021 void Tabular::TeXRow(otexstream & os, row_type row,
3022 OutputParams const & runparams,
3023 list<col_type> columns, list<col_type> logical_columns) const
3025 //output the top line
3026 TeXTopHLine(os, row, columns, logical_columns);
3028 if (row_info[row].top_space_default) {
3030 os << "\\addlinespace\n";
3032 os << "\\noalign{\\vskip\\doublerulesep}\n";
3033 } else if(!row_info[row].top_space.zero()) {
3035 os << "\\addlinespace["
3036 << from_ascii(row_info[row].top_space.asLatexString())
3039 os << "\\noalign{\\vskip"
3040 << from_ascii(row_info[row].top_space.asLatexString())
3044 bool ismulticol = false;
3045 bool ismultirow = false;
3047 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3048 // Luabibdi (used by LuaTeX) behaves like classic
3049 bool const bidi_rtl =
3050 runparams.local_font->isRightToLeft()
3051 && runparams.useBidiPackage();
3052 bool const ct = !buffer().params().output_changes;
3054 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3056 for (auto const & c : columns) {
3057 if (isPartOfMultiColumn(row, c))
3060 idx_type cell = cellIndex(row, c);
3062 if (isPartOfMultiRow(row, c)
3063 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3064 if (cell != lastcell)
3069 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3070 InsetTableCell const * inset = cellInset(cell);
3072 Paragraph const & par = inset->paragraphs().front();
3074 os.texrow().forceStart(par.id(), 0);
3076 bool rtl = par.isRTL(buffer().params())
3078 && getPWidth(cell).zero()
3079 && !runparams.use_polyglossia;
3083 par.getParLanguage(buffer().params())->lang();
3084 if (lang == "farsi")
3086 else if (lang == "arabic_arabi")
3088 // currently, remaining RTL languages are
3089 // arabic_arabtex and hebrew
3093 // pass to the OutputParams that we are in a cell and
3094 // which alignment we have set.
3095 // InsetNewline needs this context information.
3096 OutputParams newrp(runparams);
3097 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3098 ? OutputParams::PLAIN
3099 : OutputParams::ALIGNED;
3101 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3102 // copy cell and split in 2
3103 InsetTableCell head = InsetTableCell(*cellInset(cell));
3104 head.setBuffer(const_cast<Buffer &>(buffer()));
3105 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3107 dit.push_back(CursorSlice(head));
3108 head.setMacrocontextPositionRecursive(dit);
3109 bool hassep = false;
3110 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3112 tail.setBuffer(head.buffer());
3114 dit.push_back(CursorSlice(tail));
3115 tail.setMacrocontextPositionRecursive(dit);
3119 tail.latex(os, newrp);
3122 head.latex(os, newrp);
3124 head.latex(os, newrp);
3127 tail.latex(os, newrp);
3130 } else if (ltCaption(row)) {
3131 // Inside longtable caption rows, we must only output the caption inset
3132 // with its content and omit anything outside of that (see #10791)
3133 InsetTableCell & tc_inset = *const_cast<InsetTableCell *>(inset);
3134 for (Inset const & it : tc_inset) {
3135 if (it.lyxCode() != CAPTION_CODE)
3137 it.latex(os, runparams);
3140 } else if (!isPartOfMultiRow(row, c)) {
3141 if (!runparams.nice)
3142 os.texrow().start(par.id(), 0);
3143 inset->latex(os, newrp);
3146 runparams.encoding = newrp.encoding;
3150 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3151 if (cell != lastcell) { // not last cell in row
3158 os << "\\tabularnewline";
3159 if (row_info[row].bottom_space_default) {
3161 os << "\\addlinespace";
3163 os << "[\\doublerulesep]";
3164 } else if (!row_info[row].bottom_space.zero()) {
3166 os << "\\addlinespace";
3168 << from_ascii(row_info[row].bottom_space.asLatexString())
3173 //output the bottom line
3174 TeXBottomHLine(os, row, columns, logical_columns);
3176 if (row_info[row].interline_space_default) {
3178 os << "\\addlinespace\n";
3180 os << "\\noalign{\\vskip\\doublerulesep}\n";
3181 } else if (!row_info[row].interline_space.zero()) {
3183 os << "\\addlinespace["
3184 << from_ascii(row_info[row].interline_space.asLatexString())
3187 os << "\\noalign{\\vskip"
3188 << from_ascii(row_info[row].interline_space.asLatexString())
3194 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3196 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3197 && !hasVarwidthColumn();
3198 bool const is_xltabular = is_long_tabular
3199 && (hasVarwidthColumn() || !tabular_width.zero());
3200 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3202 //+---------------------------------------------------------------------
3203 //+ first the opening preamble +
3204 //+---------------------------------------------------------------------
3207 if (!TexRow::isNone(pos))
3208 os.texrow().start(pos);
3211 if (is_long_tabular)
3212 os << "\\begin{landscape}\n";
3214 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3217 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3218 // order for RTL (#9686). Thus we use this list.
3219 bool const bidi_rtl =
3220 runparams.local_font->isRightToLeft()
3221 && runparams.useBidiPackage();
3222 list<col_type> columns;
3223 list<col_type> logical_columns;
3224 for (col_type cl = 0; cl < ncols(); ++cl) {
3225 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3228 columns.push_front(cl);
3230 columns.push_back(cl);
3231 // for some calculations, we need the logical (non-swapped)
3232 // columns also in bidi.
3233 logical_columns.push_back(cl);
3236 // If we use \cline or \cmidrule, we need to locally de-activate
3237 // the - character when using languages that activate it (e.g., Czech, Slovak).
3238 bool deactivate_chars = false;
3239 if ((runparams.use_babel || runparams.use_polyglossia)
3240 && contains(runparams.active_chars, '-')) {
3241 bool have_clines = false;
3242 // Check if we use \cline or \cmidrule
3243 for (row_type row = 0; row < nrows(); ++row) {
3244 col_type bset = 0, tset = 0;
3245 for (auto const & c : columns) {
3246 idx_type const idx = cellIndex(row, c);
3247 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3248 || topLineTrim(idx).first || topLineTrim(idx).second) {
3252 if (bottomLine(cellIndex(row, c)))
3254 if (topLine(cellIndex(row, c)))
3257 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3263 deactivate_chars = true;
3264 os << "\\begingroup\n"
3265 << "\\catcode`\\-=12\n";
3269 if (is_long_tabular) {
3271 os << "\\begin{xltabular}";
3273 os << "\\begin{longtable}";
3274 switch (longtabular_alignment) {
3275 case LYX_LONGTABULAR_ALIGN_LEFT:
3278 case LYX_LONGTABULAR_ALIGN_CENTER:
3281 case LYX_LONGTABULAR_ALIGN_RIGHT:
3286 if (tabular_width.zero())
3287 os << "{" << from_ascii("\\columnwidth") << "}";
3289 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3292 if (is_tabular_star)
3293 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3294 else if (hasVarwidthColumn()) {
3295 os << "\\begin{tabularx}{";
3296 if (tabular_width.zero())
3297 os << from_ascii("\\columnwidth") << "}";
3299 os << from_ascii(tabular_width.asLatexString()) << "}";
3301 os << "\\begin{tabular}";
3302 switch (tabular_valignment) {
3303 case LYX_VALIGN_TOP:
3306 case LYX_VALIGN_MIDDLE:
3308 case LYX_VALIGN_BOTTOM:
3316 if (is_tabular_star)
3317 os << "@{\\extracolsep{\\fill}}";
3319 for (auto const & c : columns) {
3320 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3322 if (!column_info[c].align_special.empty()) {
3323 os << column_info[c].align_special;
3325 if (!column_info[c].p_width.zero()) {
3326 bool decimal = false;
3327 switch (column_info[c].alignment) {
3328 case LYX_ALIGN_LEFT:
3329 os << ">{\\raggedright}";
3331 case LYX_ALIGN_RIGHT:
3332 os << ">{\\raggedleft}";
3334 case LYX_ALIGN_CENTER:
3335 os << ">{\\centering}";
3337 case LYX_ALIGN_NONE:
3338 case LYX_ALIGN_BLOCK:
3339 case LYX_ALIGN_LAYOUT:
3340 case LYX_ALIGN_SPECIAL:
3342 case LYX_ALIGN_DECIMAL: {
3344 os << ">{\\raggedright}";
3346 os << ">{\\raggedleft}";
3353 switch (column_info[c].valignment) {
3354 case LYX_VALIGN_TOP:
3355 // this is the default
3357 case LYX_VALIGN_MIDDLE:
3360 case LYX_VALIGN_BOTTOM:
3366 // Fixed-width cells with alignment at decimal separator
3367 // are output as two cells of half the width with the decimal
3368 // separator as column sep. This effectively puts the content
3369 // centered, which differs from the normal decimal sep alignment
3370 // and is not ideal, but we cannot do better ATM (see #9568).
3371 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3373 docstring const halffixedwith =
3374 from_ascii(Length(column_info[c].p_width.value() / 2,
3375 column_info[c].p_width.unit()).asLatexString());
3379 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3386 << from_ascii(column_info[c].p_width.asLatexString())
3388 } else if (column_info[c].varwidth) {
3389 switch (column_info[c].alignment) {
3390 case LYX_ALIGN_LEFT:
3391 os << ">{\\raggedright\\arraybackslash}";
3393 case LYX_ALIGN_RIGHT:
3394 os << ">{\\raggedleft\\arraybackslash}";
3396 case LYX_ALIGN_CENTER:
3397 os << ">{\\centering\\arraybackslash}";
3399 case LYX_ALIGN_NONE:
3400 case LYX_ALIGN_BLOCK:
3401 case LYX_ALIGN_LAYOUT:
3402 case LYX_ALIGN_SPECIAL:
3403 case LYX_ALIGN_DECIMAL:
3407 } else if (isVTypeColumn(c)) {
3408 switch (column_info[c].alignment) {
3409 case LYX_ALIGN_LEFT:
3410 os << ">{\\raggedright}";
3412 case LYX_ALIGN_RIGHT:
3413 os << ">{\\raggedleft}";
3415 case LYX_ALIGN_CENTER:
3416 os << ">{\\centering}";
3418 case LYX_ALIGN_NONE:
3419 case LYX_ALIGN_BLOCK:
3420 case LYX_ALIGN_LAYOUT:
3421 case LYX_ALIGN_SPECIAL:
3422 case LYX_ALIGN_DECIMAL:
3425 os << "V{\\linewidth}";
3427 switch (column_info[c].alignment) {
3428 case LYX_ALIGN_LEFT:
3431 case LYX_ALIGN_RIGHT:
3434 case LYX_ALIGN_DECIMAL:
3435 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3441 } // end if else !column_info[i].p_width
3442 } // end if else !column_info[i].align_special
3443 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3448 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3450 //+---------------------------------------------------------------------
3451 //+ the single row and columns (cells) +
3452 //+---------------------------------------------------------------------
3454 for (row_type r = 0; r < nrows(); ++r) {
3455 if (!buffer().params().output_changes && row_info[r].change.deleted())
3457 if (isValidRow(r)) {
3458 TeXRow(os, r, runparams, columns, logical_columns);
3459 if (is_long_tabular && row_info[r].newpage)
3460 os << "\\newpage\n";
3464 //+---------------------------------------------------------------------
3465 //+ the closing of the tabular +
3466 //+---------------------------------------------------------------------
3468 if (is_long_tabular) {
3470 os << "\\end{xltabular}";
3472 os << "\\end{longtable}";
3474 if (is_tabular_star)
3475 os << "\\end{tabular*}";
3476 else if (hasVarwidthColumn())
3477 os << "\\end{tabularx}";
3479 os << "\\end{tabular}";
3482 if (deactivate_chars)
3484 os << "\n\\endgroup\n";
3487 if (is_long_tabular)
3488 os << breakln << "\\end{landscape}";
3490 os << breakln << "\\end{turn}";
3493 if (!TexRow::isNone(pos))
3494 os.texrow().start(pos);
3498 void Tabular::docbookRow(XMLStream & xs, row_type row,
3499 OutputParams const & runparams, bool header) const
3501 switch (buffer().params().docbook_table_output) {
3502 case BufferParams::HTMLTable:
3503 docbookRowAsHTML(xs, row, runparams, header);
3505 case BufferParams::CALSTable:
3506 docbookRowAsCALS(xs, row, runparams);
3512 void Tabular::docbookRowAsHTML(XMLStream & xs, row_type row,
3513 OutputParams const & runparams, bool header) const
3515 string const celltag = header ? "th" : "td";
3516 idx_type cell = getFirstCellInRow(row);
3518 xs << xml::StartTag("tr");
3520 for (col_type c = 0; c < ncols(); ++c) {
3521 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3526 Length const cwidth = column_info[c].p_width;
3527 if (!cwidth.zero()) {
3528 string const hwidth = cwidth.asHTMLString();
3529 attr << "style =\"width: " << hwidth << ";\" ";
3533 switch (getAlignment(cell)) {
3534 case LYX_ALIGN_BLOCK:
3537 case LYX_ALIGN_DECIMAL: {
3538 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3539 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3542 case LYX_ALIGN_LEFT:
3545 case LYX_ALIGN_RIGHT:
3553 attr << " valign='";
3554 switch (getVAlignment(cell)) {
3555 case LYX_VALIGN_TOP:
3558 case LYX_VALIGN_BOTTOM:
3561 case LYX_VALIGN_MIDDLE:
3566 if (isMultiColumn(cell))
3567 attr << " colspan='" << columnSpan(cell) << "'";
3568 else if (isMultiRow(cell))
3569 attr << " rowspan='" << rowSpan(cell) << "'";
3571 xs << xml::StartTag(celltag, attr.str(), true);
3572 cellInset(cell)->docbook(xs, runparams);
3573 xs << xml::EndTag(celltag);
3577 xs << xml::EndTag("tr");
3582 void Tabular::docbookRowAsCALS(XMLStream & xs, row_type row,
3583 OutputParams const & runparams) const
3585 idx_type cell = getFirstCellInRow(row);
3587 xs << xml::StartTag("row");
3589 for (col_type c = 0; c < ncols(); ++c) {
3590 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3596 switch (getAlignment(cell)) {
3597 case LYX_ALIGN_BLOCK:
3600 case LYX_ALIGN_DECIMAL: {
3601 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3602 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3605 case LYX_ALIGN_LEFT:
3608 case LYX_ALIGN_RIGHT:
3617 attr << " valign='";
3618 switch (getVAlignment(cell)) {
3619 case LYX_VALIGN_TOP:
3622 case LYX_VALIGN_BOTTOM:
3625 case LYX_VALIGN_MIDDLE:
3630 if (isMultiColumn(cell))
3631 attr << " colspan='" << columnSpan(cell) << "'";
3632 else if (isMultiRow(cell))
3633 attr << " rowspan='" << rowSpan(cell) << "'";
3635 attr << " colname='c" << (c + 1) << "'"; // Column numbering starts at 1.
3637 // All cases where there should be a line *below* this row.
3638 if (row_info[row].bottom_space_default)
3639 attr << " rowsep='1'";
3641 xs << xml::StartTag("entry", attr.str(), true);
3642 cellInset(cell)->docbook(xs, runparams);
3643 xs << xml::EndTag("entry");
3647 xs << xml::EndTag("row");
3652 void Tabular::docbook(XMLStream & xs, OutputParams const & runparams) const
3656 // Some tables are inline. Likely limitation: cannot output a table within a table; is that really a limitation?
3657 if (!runparams.docbook_in_table) { // Check on the *outer* set of parameters, so that the table can be closed
3658 // properly at the end of this function.
3659 xs << xml::StartTag("informaltable");
3663 // "Formal" tables have a title and use the tag <table>; the distinction with <informaltable> is done outside.
3664 // HTML has the caption first with titles forbidden, and CALS has a title first.
3665 if (haveLTCaption()) {
3666 std::string tag = ((buffer().params().docbook_table_output) == BufferParams::HTMLTable) ? "caption" : "title";
3668 xs << xml::StartTag(tag);
3669 for (row_type r = 0; r < nrows(); ++r)
3670 if (row_info[r].caption)
3671 docbookRow(xs, r, runparams);
3672 xs << xml::EndTag(tag);
3676 // CALS header: describe all columns in this table. For names, take 'c' then the ID of the column.
3677 // Start at one, as is customary with CALS!
3678 if (buffer().params().docbook_table_output == BufferParams::CALSTable) {
3679 for (col_type c = 0; c < ncols(); ++c) {
3680 std::stringstream attr;
3681 attr << "colnum='" << (c + 1) << "' ";
3682 attr << "colname='c" << (c + 1) << "' ";
3683 Length const cwidth = column_info[c].p_width;
3685 attr << "colwidth='" << cwidth.asHTMLString() << "' ";
3686 attr << "rowheader='norowheader'"; // Last attribute, hence no space at the end.
3688 xs << xml::CompTag("colspec", attr.str());
3693 // Output the header of the table. For both HTML and CALS, this is surrounded by a thead.
3694 bool const havefirsthead = haveLTFirstHead(false);
3695 // if we have a first head, then we are going to ignore the
3696 // headers for the additional pages, since there aren't any
3697 // in DocBook. this test accomplishes that.
3698 bool const havehead = !havefirsthead && haveLTHead(false);
3699 if (havehead || havefirsthead) {
3700 xs << xml::StartTag("thead") << xml::CR();
3701 for (row_type r = 0; r < nrows(); ++r) {
3702 if (((havefirsthead && row_info[r].endfirsthead) ||
3703 (havehead && row_info[r].endhead)) &&
3704 !row_info[r].caption) {
3705 docbookRow(xs, r, runparams, true); // TODO: HTML vs CALS
3708 xs << xml::EndTag("thead");
3712 // Output the footer of the table. For both HTML and CALS, this is surrounded by a tfoot and output just after
3713 // the header (and before the body).
3714 bool const havelastfoot = haveLTLastFoot(false);
3716 bool const havefoot = !havelastfoot && haveLTFoot(false);
3717 if (havefoot || havelastfoot) {
3718 xs << xml::StartTag("tfoot") << xml::CR();
3719 for (row_type r = 0; r < nrows(); ++r) {
3720 if (((havelastfoot && row_info[r].endlastfoot) ||
3721 (havefoot && row_info[r].endfoot)) &&
3722 !row_info[r].caption) {
3723 docbookRow(xs, r, runparams); // TODO: HTML vs CALS
3726 xs << xml::EndTag("tfoot");
3730 // Output the main part of the table. The tbody container is mandatory for CALS, but optional for HTML (only if
3731 // there is no header and no footer). It never hurts to have it, though.
3732 xs << xml::StartTag("tbody");
3734 for (row_type r = 0; r < nrows(); ++r)
3736 docbookRow(xs, r, runparams);
3737 xs << xml::EndTag("tbody");
3740 // If this method started the table tag, also make it close it.
3741 if (!runparams.docbook_in_table) {
3742 xs << xml::EndTag("informaltable");
3748 docstring Tabular::xhtmlRow(XMLStream & xs, row_type row,
3749 OutputParams const & runparams, bool header) const
3752 string const celltag = header ? "th" : "td";
3753 idx_type cell = getFirstCellInRow(row);
3755 xs << xml::StartTag("tr");
3756 for (col_type c = 0; c < ncols(); ++c) {
3757 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3762 Length const cwidth = column_info[c].p_width;
3763 if (!cwidth.zero()) {
3764 string const hwidth = cwidth.asHTMLString();
3765 attr << "style =\"width: " << hwidth << ";\" ";
3769 switch (getAlignment(cell)) {
3770 case LYX_ALIGN_LEFT:
3773 case LYX_ALIGN_RIGHT:
3781 attr << " valign='";
3782 switch (getVAlignment(cell)) {
3783 case LYX_VALIGN_TOP:
3786 case LYX_VALIGN_BOTTOM:
3789 case LYX_VALIGN_MIDDLE:
3794 if (isMultiColumn(cell))
3795 attr << " colspan='" << columnSpan(cell) << "'";
3796 else if (isMultiRow(cell))
3797 attr << " rowspan='" << rowSpan(cell) << "'";
3799 xs << xml::StartTag(celltag, attr.str(), true) << xml::CR();
3800 ret += cellInset(cell)->xhtml(xs, runparams);
3801 xs << xml::EndTag(celltag) << xml::CR();
3804 xs << xml::EndTag("tr");
3809 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3813 if (is_long_tabular) {
3814 // we'll wrap it in a div, so as to deal with alignment
3816 switch (longtabular_alignment) {
3817 case LYX_LONGTABULAR_ALIGN_LEFT:
3820 case LYX_LONGTABULAR_ALIGN_CENTER:
3823 case LYX_LONGTABULAR_ALIGN_RIGHT:
3827 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
3829 // The caption flag wins over head/foot
3830 if (haveLTCaption()) {
3831 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
3833 for (row_type r = 0; r < nrows(); ++r)
3834 if (row_info[r].caption)
3835 ret += xhtmlRow(xs, r, runparams);
3836 xs << xml::EndTag("div");
3841 xs << xml::StartTag("table");
3844 // output header info
3845 bool const havefirsthead = haveLTFirstHead(false);
3846 // if we have a first head, then we are going to ignore the
3847 // headers for the additional pages, since there aren't any
3848 // in XHTML. this test accomplishes that.
3849 bool const havehead = !havefirsthead && haveLTHead(false);
3850 if (havehead || havefirsthead) {
3851 xs << xml::StartTag("thead");
3853 for (row_type r = 0; r < nrows(); ++r) {
3854 if (((havefirsthead && row_info[r].endfirsthead) ||
3855 (havehead && row_info[r].endhead)) &&
3856 !row_info[r].caption) {
3857 ret += xhtmlRow(xs, r, runparams, true);
3860 xs << xml::EndTag("thead");
3863 // output footer info
3864 bool const havelastfoot = haveLTLastFoot(false);
3866 bool const havefoot = !havelastfoot && haveLTFoot(false);
3867 if (havefoot || havelastfoot) {
3868 xs << xml::StartTag("tfoot") << xml::CR();
3869 for (row_type r = 0; r < nrows(); ++r) {
3870 if (((havelastfoot && row_info[r].endlastfoot) ||
3871 (havefoot && row_info[r].endfoot)) &&
3872 !row_info[r].caption) {
3873 ret += xhtmlRow(xs, r, runparams);
3876 xs << xml::EndTag("tfoot");
3880 xs << xml::StartTag("tbody") << xml::CR();
3881 for (row_type r = 0; r < nrows(); ++r)
3883 ret += xhtmlRow(xs, r, runparams);
3884 xs << xml::EndTag("tbody");
3886 xs << xml::EndTag("table");
3888 if (is_long_tabular) {
3889 xs << xml::EndTag("div");
3896 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3897 vector<unsigned int> const & clen) const
3899 idx_type const fcell = getFirstCellInRow(row);
3900 idx_type const n = numberOfCellsInRow(row) + fcell;
3903 for (idx_type i = fcell; i < n; ++i) {
3913 for (idx_type i = fcell; i < n; ++i) {
3924 col_type column = cellColumn(i);
3925 int len = clen[column];
3926 while (column < ncols() - 1
3927 && isPartOfMultiColumn(row, ++column))
3928 len += clen[column] + 4;
3929 os << docstring(len, ch);
3944 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3945 vector<unsigned int> const & clen) const
3947 idx_type const fcell = getFirstCellInRow(row);
3948 idx_type const n = numberOfCellsInRow(row) + fcell;
3951 for (idx_type i = fcell; i < n; ++i) {
3952 if (bottomLine(i)) {
3961 for (idx_type i = fcell; i < n; ++i) {
3962 if (bottomLine(i)) {
3972 col_type column = cellColumn(i);
3973 int len = clen[column];
3974 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3975 len += clen[column] + 4;
3976 os << docstring(len, ch);
3977 if (bottomLine(i)) {
3991 void Tabular::plaintextPrintCell(odocstringstream & os,
3992 OutputParams const & runparams,
3993 idx_type cell, row_type row, col_type column,
3994 vector<unsigned int> const & clen,
3995 bool onlydata, size_t max_length) const
3997 odocstringstream sstr;
3998 cellInset(cell)->plaintext(sstr, runparams, max_length);
4010 unsigned int len1 = sstr.str().length();
4011 unsigned int len2 = clen[column];
4012 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4013 len2 += clen[column] + 4;
4016 switch (getAlignment(cell)) {
4018 case LYX_ALIGN_LEFT:
4021 case LYX_ALIGN_RIGHT:
4025 case LYX_ALIGN_CENTER:
4031 os << docstring(len1, ' ') << sstr.str()
4032 << docstring(len2, ' ');
4034 if (rightLine(cell))
4041 void Tabular::plaintext(odocstringstream & os,
4042 OutputParams const & runparams, int const depth,
4043 bool onlydata, char_type delim, size_t max_length) const
4045 // first calculate the width of the single columns
4046 vector<unsigned int> clen(ncols());
4049 // first all non multicolumn cells!
4050 for (col_type c = 0; c < ncols(); ++c) {
4052 for (row_type r = 0; r < nrows(); ++r) {
4053 idx_type cell = cellIndex(r, c);
4054 if (isMultiColumn(cell))
4056 odocstringstream sstr;
4057 cellInset(cell)->plaintext(sstr, runparams, max_length);
4058 if (clen[c] < sstr.str().length())
4059 clen[c] = sstr.str().length();
4062 // then all multicolumn cells!
4063 for (col_type c = 0; c < ncols(); ++c) {
4064 for (row_type r = 0; r < nrows(); ++r) {
4065 idx_type cell = cellIndex(r, c);
4066 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
4068 odocstringstream sstr;
4069 cellInset(cell)->plaintext(sstr, runparams, max_length);
4070 int len = int(sstr.str().length());
4071 idx_type const n = columnSpan(cell);
4072 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
4074 if (len > int(clen[c + n - 1]))
4075 clen[c + n - 1] = len;
4080 for (row_type r = 0; r < nrows(); ++r) {
4081 if (!onlydata && plaintextTopHLine(os, r, clen))
4082 os << docstring(depth * 2, ' ');
4083 for (col_type c = 0; c < ncols(); ++c) {
4084 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
4086 if (onlydata && c > 0)
4087 // we don't use operator<< for single UCS4 character.
4088 // see explanation in docstream.h
4090 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
4092 if (os.str().size() > max_length)
4097 os << docstring(depth * 2, ' ');
4098 if (plaintextBottomHLine(os, r, clen))
4099 os << docstring(depth * 2, ' ');
4101 if (os.str().size() > max_length)
4107 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4109 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4113 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4115 return cell_info[row][column].inset;
4119 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4121 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4125 void Tabular::setCellInset(row_type row, col_type column,
4126 shared_ptr<InsetTableCell> ins)
4128 CellData & cd = cell_info[row][column];
4133 void Tabular::validate(LaTeXFeatures & features) const
4135 features.require("NeedTabularnewline");
4137 features.require("booktabs");
4138 if (is_long_tabular && !hasVarwidthColumn()) {
4139 if (tabular_width.zero())
4140 features.require("longtable");
4142 features.require("xltabular");
4144 if (rotate && is_long_tabular)
4145 features.require("lscape");
4147 features.require("rotating");
4148 if (hasVarwidthColumn()) {
4149 if (is_long_tabular)
4150 features.require("xltabular");
4152 features.require("tabularx");
4154 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4155 if (isMultiRow(cell))
4156 features.require("multirow");
4157 if (getUsebox(cell) == BOX_VARWIDTH)
4158 features.require("varwidth");
4159 if (getVAlignment(cell) != LYX_VALIGN_TOP
4160 || !getPWidth(cell).zero()
4161 || isVTypeColumn(cellColumn(cell)))
4162 features.require("array");
4163 // Tell footnote that we need a savenote
4164 // environment in non-long tables or
4165 // longtable headers/footers
4166 else if (!is_long_tabular && !features.inFloat())
4167 features.saveNoteEnv("tabular");
4168 else if (!isValidRow(cellRow(cell)))
4169 features.saveNoteEnv("longtable");
4171 cellInset(cell)->validate(features);
4172 features.saveNoteEnv(string());
4177 Tabular::BoxType Tabular::useBox(idx_type cell) const
4179 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4180 if (parlist.size() > 1)
4181 return BOX_VARWIDTH;
4183 ParagraphList::const_iterator cit = parlist.begin();
4184 ParagraphList::const_iterator end = parlist.end();
4186 for (; cit != end; ++cit)
4187 for (int i = 0; i < cit->size(); ++i)
4188 if (cit->isNewline(i) || cit->layout().isEnvironment())
4189 return BOX_VARWIDTH;
4195 /////////////////////////////////////////////////////////////////////
4199 /////////////////////////////////////////////////////////////////////
4201 InsetTableCell::InsetTableCell(Buffer * buf)
4202 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
4203 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4206 bool InsetTableCell::forcePlainLayout(idx_type) const
4208 return isMultiRow || (isMultiColumn && !isFixedWidth);
4212 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4214 return isFixedWidth;
4218 bool InsetTableCell::forceLocalFontSwitch() const
4224 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4225 FuncStatus & status) const
4227 bool enabled = true;
4228 switch (cmd.action()) {
4229 case LFUN_INSET_SPLIT:
4230 case LFUN_INSET_DISSOLVE:
4233 case LFUN_MATH_DISPLAY:
4234 if (!hasFixedWidth()) {
4239 return InsetText::getStatus(cur, cmd, status);
4241 status.setEnabled(enabled);
4245 docstring InsetTableCell::asString(bool intoInsets)
4248 if (paragraphs().empty())
4250 ParagraphList::const_iterator it = paragraphs().begin();
4251 ParagraphList::const_iterator en = paragraphs().end();
4253 for (; it != en; ++it) {
4258 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4264 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4265 UpdateType utype, TocBackend & backend) const
4267 InsetText::iterateForToc(di, output_active, utype, backend);
4271 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4274 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4275 return InsetText::xhtml(xs, rp);
4279 void InsetTableCell::docbook(XMLStream & xs, OutputParams const & runparams) const
4281 InsetText::docbook(xs, runparams);
4285 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4287 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4288 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4290 // Hand font through to contained lyxtext:
4291 tm.font_.fontInfo() = mi.base.font;
4292 mi.base.textwidth -= horiz_offset;
4294 // This can happen when a layout has a left and right margin,
4295 // and the view is made very narrow. We can't do better than
4296 // to draw it partly out of view (bug 5890).
4297 if (mi.base.textwidth < 1)
4298 mi.base.textwidth = 1;
4300 // We tell metrics here not to expand on multiple pars
4301 // This is the difference to InsetText::Metrics
4302 if (hasFixedWidth())
4303 tm.metrics(mi, dim, mi.base.textwidth, false);
4305 tm.metrics(mi, dim, 0, false);
4306 mi.base.textwidth += horiz_offset;
4307 dim.asc += topOffset(mi.base.bv);
4308 dim.des += bottomOffset(mi.base.bv);
4309 dim.wid += horiz_offset;
4313 /////////////////////////////////////////////////////////////////////
4317 /////////////////////////////////////////////////////////////////////
4319 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4321 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4322 rowselect_(false), colselect_(false)
4327 InsetTabular::InsetTabular(InsetTabular const & tab)
4328 : Inset(tab), tabular(tab.tabular),
4329 rowselect_(false), colselect_(false)
4334 InsetTabular::~InsetTabular()
4336 hideDialogs("tabular", this);
4340 void InsetTabular::setBuffer(Buffer & buf)
4342 tabular.setBuffer(buf);
4343 Inset::setBuffer(buf);
4347 bool InsetTabular::insetAllowed(InsetCode code) const
4352 case MATHMACRO_CODE:
4357 return tabular.is_long_tabular;
4365 bool InsetTabular::allowMultiPar() const
4367 for (col_type c = 0; c < tabular.ncols(); ++c) {
4368 for (row_type r = 0; r < tabular.nrows(); ++r) {
4369 if (tabular.cellInset(r,c)->allowMultiPar())
4377 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4379 return tabular.is_long_tabular &&
4380 (newtype == "Standard" || newtype == "Unnumbered");
4384 void InsetTabular::write(ostream & os) const
4386 os << "Tabular" << endl;
4391 string InsetTabular::contextMenu(BufferView const &, int, int) const
4393 // FIXME: depending on the selection state,
4394 // we could offer a different menu.
4395 return cell(0)->contextMenuName() + ";" + contextMenuName();
4399 string InsetTabular::contextMenuName() const
4401 return "context-tabular";
4405 void InsetTabular::read(Lexer & lex)
4407 //bool const old_format = (lex.getString() == "\\LyXTable");
4415 string token = lex.getString();
4416 while (lex && token != "\\end_inset") {
4418 token = lex.getString();
4421 lex.printError("Missing \\end_inset at this point. ");
4425 int InsetTabular::rowFromY(Cursor & cur, int y) const
4427 // top y coordinate of tabular
4428 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4430 for (; r < tabular.nrows() && y > h; ++r)
4431 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4432 + tabular.interRowSpace(r);
4438 int InsetTabular::columnFromX(Cursor & cur, int x) const
4440 // left x coordinate of tabular
4441 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4443 for (; c < tabular.ncols() && x > w; ++c)
4444 w += tabular.cellWidth(c);
4449 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4451 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4452 // mi.base.textwidth << "\n";
4453 LBUFERR(mi.base.bv);
4455 for (row_type r = 0; r < tabular.nrows(); ++r) {
4458 for (col_type c = 0; c < tabular.ncols(); ++c) {
4459 if (tabular.isPartOfMultiColumn(r, c)
4460 || tabular.isPartOfMultiRow(r, c))
4461 // multicolumn or multirow cell, but not first one
4463 idx_type const cell = tabular.cellIndex(r, c);
4466 Length const p_width = tabular.getPWidth(cell);
4467 if (!p_width.zero())
4468 m.base.textwidth = mi.base.inPixels(p_width);
4469 else if (tabular.column_info[c].varwidth)
4470 m.base.textwidth = tabular.column_info[c].width;
4471 tabular.cellInset(cell)->metrics(m, dim0);
4472 if (!p_width.zero() || tabular.column_info[c].varwidth)
4473 dim0.wid = m.base.textwidth;
4474 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4475 + tabular.interColumnSpace(cell);
4477 // FIXME(?): do we need a second metrics call?
4478 TextMetrics const & tm =
4479 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4481 // determine horizontal offset because of decimal align (if necessary)
4482 int decimal_width = 0;
4483 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4484 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4485 tail.setBuffer(tabular.buffer());
4486 // we need to set macrocontext position everywhere
4487 // otherwise we crash with nested insets (e.g. footnotes)
4488 // after decimal point
4489 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4491 dit.push_back(CursorSlice(tail));
4492 tail.setMacrocontextPositionRecursive(dit);
4494 // remove text leading decimal point
4495 docstring const align_d = tabular.column_info[c].decimal_point;
4496 dit = separatorPos(&tail, align_d);
4498 pos_type const psize = tail.paragraphs().front().size();
4500 tail.paragraphs().front().eraseChars(0,
4501 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4503 tail.metrics(m, dim1);
4504 decimal_width = dim1.width();
4507 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4508 tabular.cell_info[r][c].decimal_width = decimal_width;
4510 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4511 // = descent of text in last par + bottomOffset:
4512 int const lastpardes = tm.last().second->descent()
4513 + bottomOffset(mi.base.bv);
4515 switch (tabular.getVAlignment(cell)) {
4516 case Tabular::LYX_VALIGN_TOP:
4518 case Tabular::LYX_VALIGN_MIDDLE:
4519 offset = -(dim0.des - lastpardes)/2;
4521 case Tabular::LYX_VALIGN_BOTTOM:
4522 offset = -(dim0.des - lastpardes);
4525 tabular.cell_info[r][c].voffset = offset;
4526 maxasc = max(maxasc, dim0.asc - offset);
4527 maxdes = max(maxdes, dim0.des + offset);
4529 int const top_space = tabular.row_info[r].top_space_default ?
4530 default_line_space :
4531 mi.base.inPixels(tabular.row_info[r].top_space);
4532 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4533 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4534 default_line_space :
4535 mi.base.inPixels(tabular.row_info[r].bottom_space);
4536 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4539 // We need to recalculate the metrics after column width calculation
4540 // with xtabular (possibly multiple times, so the call is recursive).
4541 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4543 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4544 dim.des = tabular.height() - dim.asc;
4545 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4549 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4551 if (&cur.inset() == this && cur.selection()) {
4552 if (cur.selIsMultiCell()) {
4555 getSelection(cur, rs, re, cs, ce);
4557 idx_type const cell = tabular.cellIndex(row, col);
4558 col_type const cspan = tabular.columnSpan(cell);
4559 row_type const rspan = tabular.rowSpan(cell);
4560 if (col + cspan - 1 >= cs && col <= ce
4561 && row + rspan - 1 >= rs && row <= re)
4564 if (col == tabular.cellColumn(cur.idx())
4565 && row == tabular.cellRow(cur.idx())) {
4566 CursorSlice const & beg = cur.selBegin();
4567 CursorSlice const & end = cur.selEnd();
4569 if ((end.lastpos() > 0 || end.lastpit() > 0)
4570 && end.pos() == end.lastpos() && beg.pos() == 0
4571 && end.pit() == end.lastpit() && beg.pit() == 0)
4579 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4581 x += ADD_TO_TABULAR_WIDTH;
4583 BufferView * bv = pi.base.bv;
4584 Cursor & cur = pi.base.bv->cursor();
4586 // FIXME: As the full background is painted in drawBackground(),
4587 // we have no choice but to do a full repaint for the Text cells.
4588 pi.full_repaint = true;
4590 bool const original_selection_state = pi.selected;
4594 // Save tabular change status
4595 Change tab_change = pi.change;
4597 int yy = y + tabular.offsetVAlignment();
4598 for (row_type r = 0; r < tabular.nrows(); ++r) {
4600 for (col_type c = 0; c < tabular.ncols(); ++c) {
4601 if (tabular.isPartOfMultiColumn(r, c))
4604 idx = tabular.cellIndex(r, c);
4606 if (tabular.isPartOfMultiRow(r, c)) {
4607 nx += tabular.cellWidth(idx);
4611 pi.selected |= isCellSelected(cur, r, c);
4613 // Mark deleted rows/columns
4614 if (tabular.column_info[c].change.changed())
4615 pi.change = tabular.column_info[c].change;
4616 else if (tabular.row_info[r].change.changed())
4617 pi.change = tabular.row_info[r].change;
4619 pi.change = tab_change;
4621 int const cx = nx + tabular.textHOffset(idx);
4622 int const cy = yy + tabular.textVOffset(idx);
4623 // Cache the Inset position.
4624 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4625 cell(idx)->draw(pi, cx, cy);
4626 drawCellLines(pi, nx, yy, r, idx);
4627 nx += tabular.cellWidth(idx);
4628 pi.selected = original_selection_state;
4631 if (r + 1 < tabular.nrows())
4632 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4633 + tabular.interRowSpace(r + 1);
4638 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4640 x += ADD_TO_TABULAR_WIDTH;
4641 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4642 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4643 pi.backgroundColor(this));
4647 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4649 Cursor & cur = pi.base.bv->cursor();
4651 x += ADD_TO_TABULAR_WIDTH;
4653 if (!cur.selection())
4655 if (&cur.inset() != this)
4660 bool const full_cell_selected = isCellSelected(cur,
4661 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4663 if (cur.selIsMultiCell() || full_cell_selected) {
4664 for (row_type r = 0; r < tabular.nrows(); ++r) {
4666 for (col_type c = 0; c < tabular.ncols(); ++c) {
4667 if (tabular.isPartOfMultiColumn(r, c))
4670 idx_type const cell = tabular.cellIndex(r, c);
4672 if (tabular.isPartOfMultiRow(r, c)) {
4673 xx += tabular.cellWidth(cell);
4676 int const w = tabular.cellWidth(cell);
4677 int const h = tabular.cellHeight(cell);
4678 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4679 if (isCellSelected(cur, r, c))
4680 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4683 if (r + 1 < tabular.nrows())
4684 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4685 + tabular.interRowSpace(r + 1);
4689 // FIXME: This code has no effect because InsetTableCell does not handle
4690 // drawSelection other than the trivial implementation in Inset.
4692 // x += cellXPos(cur.idx());
4693 // x += tabular.textHOffset(cur.idx());
4694 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4701 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4702 Color const incol, bool drawline, bool heavy = false)
4704 Color const col = drawline ? incol : Color_tabularonoffline;
4705 if (drawline && lt > 0)
4706 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4707 Painter::line_onoffdash,
4708 Painter::thin_line);
4709 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4710 drawline ? Painter::line_solid : Painter::line_onoffdash,
4711 (heavy ? 2 : 1) * Painter::thin_line);
4712 if (drawline && rt > 0)
4713 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4714 Painter::line_onoffdash,
4715 Painter::thin_line);
4721 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4722 row_type row, idx_type cell) const
4724 y -= tabular.rowAscent(row);
4725 int const w = tabular.cellWidth(cell);
4726 int const h = tabular.cellHeight(cell);
4730 col_type const col = tabular.cellColumn(cell);
4732 // Colour the frame if rows/columns are added or deleted
4733 Color colour = Color_tabularline;
4734 if (tabular.column_info[col].change.changed()
4735 || tabular.row_info[row].change.changed())
4736 colour = InsetTableCell(*tabular.cellInset(cell)).paragraphs().front().lookupChange(0).color();
4739 bool drawline = tabular.topLine(cell)
4740 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4741 bool heavy = tabular.use_booktabs
4742 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4743 && tabular.rowTopLine(row);
4744 if (tabular.topLineTrim(cell).first
4745 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4747 if (tabular.topLineTrim(cell).second
4748 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4750 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4754 drawline = tabular.bottomLine(cell);
4755 row_type const lastrow = tabular.nrows() - 1;
4756 // Consider multi-rows
4758 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4759 && tabular.isPartOfMultiRow(r + 1, col))
4761 heavy = tabular.use_booktabs
4762 && ((row == lastrow && tabular.rowBottomLine(row))
4763 || (r == lastrow && tabular.rowBottomLine(r)));
4764 if (tabular.bottomLineTrim(cell).first)
4766 if (tabular.bottomLineTrim(cell).second)
4768 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4771 drawline = tabular.leftLine(cell)
4772 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4773 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4776 x -= tabular.interColumnSpace(cell);
4777 col_type next_cell_col = col + 1;
4778 while (next_cell_col < tabular.ncols()
4779 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4781 drawline = tabular.rightLine(cell)
4782 || (next_cell_col < tabular.ncols()
4783 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4784 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4788 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4790 //lyxerr << "InsetTabular::edit: " << this << endl;
4794 if (isRightToLeft(cur))
4795 cur.idx() = tabular.getLastCellInRow(0);
4801 if (isRightToLeft(cur))
4802 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4804 cur.idx() = tabular.numberofcells - 1;
4806 cur.pos() = cur.lastpos(); // FIXME crude guess
4808 cur.setCurrentFont();
4809 // FIXME: this accesses the position cache before it is initialized
4810 //cur.bv().fitCursor();
4814 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4816 // In a longtable, tell captions what the current float is
4817 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4818 string const saveflt = cnts.current_float();
4819 if (tabular.is_long_tabular) {
4820 cnts.current_float("table");
4821 // in longtables, we only step the counter once
4822 cnts.step(from_ascii("table"), utype);
4823 cnts.isLongtable(true);
4826 ParIterator it2 = it;
4828 size_t const end = it2.nargs();
4829 for ( ; it2.idx() < end; it2.top().forwardIdx())
4830 buffer().updateBuffer(it2, utype);
4833 if (tabular.is_long_tabular) {
4834 cnts.current_float(saveflt);
4835 cnts.isLongtable(false);
4840 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4841 UpdateType utype, TocBackend & backend) const
4843 DocIterator dit = cpit;
4845 size_t const end = dit.nargs();
4846 for ( ; dit.idx() < end; dit.top().forwardIdx())
4847 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4851 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4853 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4854 return x < x0 || x > x0 + tabular.width();
4858 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4860 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4861 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4862 // there is no margin added vertically to tabular insets.
4863 // However, it works for now.
4864 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4868 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4870 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4874 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4876 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4877 << "\n cur:" << cur);
4878 CursorSlice sl = cur.top();
4879 Cursor & bvcur = cur.bv().cursor();
4881 FuncCode const act = cmd.action();
4885 case LFUN_MOUSE_PRESS: {
4886 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4888 if (hitSelectRow(cur.bv(), cmd.x())) {
4889 row_type r = rowFromY(cur, cmd.y());
4890 cur.idx() = tabular.getFirstCellInRow(r);
4894 cur.idx() = tabular.getLastCellInRow(r);
4895 cur.pit() = cur.lastpit();
4896 cur.pos() = cur.lastpos();
4897 cur.selection(true);
4903 if (hitSelectColumn(cur.bv(), cmd.y())) {
4904 col_type c = columnFromX(cur, cmd.x());
4905 cur.idx() = tabular.cellIndex(0, c);
4909 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4910 cur.pit() = cur.lastpit();
4911 cur.pos() = cur.lastpos();
4912 cur.selection(true);
4917 // do not reset cursor/selection if we have selected
4918 // some cells (bug 2715).
4919 if (cmd.button() == mouse_button::button3
4920 && &bvcur.selBegin().inset() == this
4921 && bvcur.selIsMultiCell())
4924 // Let InsetTableCell do it
4925 cell(cur.idx())->dispatch(cur, cmd);
4928 case LFUN_MOUSE_MOTION:
4929 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4930 if (cmd.button() == mouse_button::button1) {
4931 // only accept motions to places not deeper nested than the real anchor
4932 if (!bvcur.realAnchor().hasPart(cur)) {
4936 // select (additional) row
4938 row_type r = rowFromY(cur, cmd.y());
4939 cur.idx() = tabular.getLastCellInRow(r);
4940 // we need to reset the cursor's pit and pos now, as the old ones
4941 // may no longer be valid.
4944 bvcur.setCursor(cur);
4945 bvcur.selection(true);
4948 // select (additional) column
4950 col_type c = columnFromX(cur, cmd.x());
4951 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4952 // we need to reset the cursor's pit and pos now, as the old ones
4953 // may no longer be valid.
4956 bvcur.setCursor(cur);
4957 bvcur.selection(true);
4960 // only update if selection changes
4961 if (bvcur.idx() == cur.idx() &&
4962 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4963 cur.noScreenUpdate();
4964 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4965 bvcur.setCursor(cur);
4966 bvcur.selection(true);
4967 // if this is a multicell selection, we just set the cursor to
4968 // the beginning of the cell's text.
4969 if (bvcur.selIsMultiCell()) {
4970 bvcur.pit() = bvcur.lastpit();
4971 bvcur.pos() = bvcur.lastpos();
4976 case LFUN_MOUSE_RELEASE:
4981 case LFUN_CELL_BACKWARD:
4983 cur.selection(false);
4986 case LFUN_CELL_FORWARD:
4988 cur.selection(false);
4991 case LFUN_CHAR_FORWARD_SELECT:
4992 case LFUN_CHAR_FORWARD:
4993 case LFUN_CHAR_BACKWARD_SELECT:
4994 case LFUN_CHAR_BACKWARD:
4995 case LFUN_CHAR_RIGHT_SELECT:
4996 case LFUN_CHAR_RIGHT:
4997 case LFUN_CHAR_LEFT_SELECT:
4998 case LFUN_CHAR_LEFT:
4999 case LFUN_WORD_FORWARD:
5000 case LFUN_WORD_FORWARD_SELECT:
5001 case LFUN_WORD_BACKWARD:
5002 case LFUN_WORD_BACKWARD_SELECT:
5003 case LFUN_WORD_RIGHT:
5004 case LFUN_WORD_RIGHT_SELECT:
5005 case LFUN_WORD_LEFT:
5006 case LFUN_WORD_LEFT_SELECT: {
5007 // determine whether we move to next or previous cell, where to enter
5008 // the new cell from, and which command to "finish" (i.e., exit the
5011 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
5012 FuncCode finish_lfun;
5014 if (act == LFUN_CHAR_FORWARD
5015 || act == LFUN_CHAR_FORWARD_SELECT
5016 || act == LFUN_WORD_FORWARD
5017 || act == LFUN_WORD_FORWARD_SELECT) {
5019 finish_lfun = LFUN_FINISHED_FORWARD;
5021 else if (act == LFUN_CHAR_BACKWARD
5022 || act == LFUN_CHAR_BACKWARD_SELECT
5023 || act == LFUN_WORD_BACKWARD
5024 || act == LFUN_WORD_BACKWARD_SELECT) {
5026 finish_lfun = LFUN_FINISHED_BACKWARD;
5028 // LEFT or RIGHT commands --- the interpretation will depend on the
5029 // table's direction.
5031 bool const right = act == LFUN_CHAR_RIGHT
5032 || act == LFUN_CHAR_RIGHT_SELECT
5033 || act == LFUN_WORD_RIGHT
5034 || act == LFUN_WORD_RIGHT_SELECT;
5035 next_cell = isRightToLeft(cur) != right;
5037 if (lyxrc.visual_cursor)
5038 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
5040 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
5043 bool const select = act == LFUN_CHAR_FORWARD_SELECT
5044 || act == LFUN_CHAR_BACKWARD_SELECT
5045 || act == LFUN_CHAR_RIGHT_SELECT
5046 || act == LFUN_CHAR_LEFT_SELECT
5047 || act == LFUN_WORD_FORWARD_SELECT
5048 || act == LFUN_WORD_RIGHT_SELECT
5049 || act == LFUN_WORD_BACKWARD_SELECT
5050 || act == LFUN_WORD_LEFT_SELECT;
5052 // If we have a multicell selection or we're
5053 // not doing some LFUN_*_SELECT thing anyway...
5054 if (!cur.selIsMultiCell() || !select) {
5055 col_type const c = tabular.cellColumn(cur.idx());
5056 row_type const r = tabular.cellRow(cur.idx());
5057 // Are we trying to select the whole cell and is the whole cell
5058 // not yet selected?
5059 bool const select_whole = select && !isCellSelected(cur, r, c) &&
5060 ((next_cell && cur.pit() == cur.lastpit()
5061 && cur.pos() == cur.lastpos())
5062 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
5064 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
5066 // ...try to dispatch to the cell's inset.
5067 cell(cur.idx())->dispatch(cur, cmd);
5069 // When we already have a selection we want to select the whole cell
5070 // before going to the next cell.
5071 if (select_whole && !empty_cell){
5072 getText(cur.idx())->selectAll(cur);
5074 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5078 // FIXME: When we support the selection of an empty cell, remove
5079 // the !empty_cell from this condition. For now we jump to the next
5080 // cell if the current cell is empty.
5081 if (cur.result().dispatched() && !empty_cell)
5085 // move to next/prev cell, as appropriate
5086 // note that we will always do this if we're selecting and we have
5087 // a multicell selection
5088 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
5089 << " cell from: " << int(entry_from));
5091 moveNextCell(cur, entry_from);
5093 movePrevCell(cur, entry_from);
5094 // if we're exiting the table, call the appropriate FINISHED lfun
5095 if (sl == cur.top()) {
5096 cmd = FuncRequest(finish_lfun);
5101 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5106 case LFUN_DOWN_SELECT:
5108 if (!(cur.selection() && cur.selIsMultiCell()))
5109 cell(cur.idx())->dispatch(cur, cmd);
5111 cur.dispatched(); // override the cell's decision
5112 if (sl == cur.top()) {
5113 // if our Text didn't do anything to the cursor
5114 // then we try to put the cursor into the cell below
5115 // setting also the right targetX.
5116 cur.selHandle(act == LFUN_DOWN_SELECT);
5117 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
5118 int const xtarget = cur.targetX();
5119 // WARNING: Once cur.idx() has been reset, the cursor is in
5120 // an inconsistent state until pos() has been set. Be careful
5121 // what you do with it!
5122 cur.idx() = tabular.cellBelow(cur.idx());
5124 TextMetrics const & tm =
5125 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5126 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5127 cur.setCurrentFont();
5130 if (sl == cur.top()) {
5131 // we trick it to go to forward after leaving the
5133 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5136 if (cur.selIsMultiCell()) {
5137 cur.pit() = cur.lastpit();
5138 cur.pos() = cur.lastpos();
5139 cur.setCurrentFont();
5140 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5143 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5146 case LFUN_UP_SELECT:
5148 if (!(cur.selection() && cur.selIsMultiCell()))
5149 cell(cur.idx())->dispatch(cur, cmd);
5150 cur.dispatched(); // override the cell's decision
5151 if (sl == cur.top()) {
5152 // if our Text didn't do anything to the cursor
5153 // then we try to put the cursor into the cell above
5154 // setting also the right targetX.
5155 cur.selHandle(act == LFUN_UP_SELECT);
5156 if (tabular.cellRow(cur.idx()) != 0) {
5157 int const xtarget = cur.targetX();
5158 // WARNING: Once cur.idx() has been reset, the cursor is in
5159 // an inconsistent state until pos() has been set. Be careful
5160 // what you do with it!
5161 cur.idx() = tabular.cellAbove(cur.idx());
5162 cur.pit() = cur.lastpit();
5163 Text const * text = cell(cur.idx())->getText(0);
5164 TextMetrics const & tm = cur.bv().textMetrics(text);
5165 ParagraphMetrics const & pm =
5166 tm.parMetrics(cur.lastpit());
5167 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5168 cur.setCurrentFont();
5171 if (sl == cur.top()) {
5172 cmd = FuncRequest(LFUN_UP);
5175 if (cur.selIsMultiCell()) {
5177 cur.pos() = cur.lastpos();
5178 cur.setCurrentFont();
5179 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5182 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5185 case LFUN_LAYOUT_TABULAR:
5186 cur.bv().showDialog("tabular");
5189 case LFUN_INSET_MODIFY:
5190 // we come from the dialog
5191 if (cmd.getArg(0) == "tabular")
5192 tabularFeatures(cur, cmd.getLongArg(1));
5197 case LFUN_TABULAR_FEATURE:
5198 tabularFeatures(cur, to_utf8(cmd.argument()));
5201 // insert file functions
5202 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5203 case LFUN_FILE_INSERT_PLAINTEXT:
5205 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5206 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5207 FileName(to_utf8(cmd.argument())));
5210 cur.recordUndoInset();
5211 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5212 // content has been replaced,
5213 // so cursor might be invalid
5214 cur.pos() = cur.lastpos();
5215 cur.pit() = cur.lastpit();
5216 bvcur.setCursor(cur);
5223 if (cur.selIsMultiCell()) {
5224 if (copySelection(cur)) {
5225 cur.recordUndoInset();
5229 cell(cur.idx())->dispatch(cur, cmd);
5232 case LFUN_SELF_INSERT:
5233 if (cur.selIsMultiCell()) {
5234 cur.recordUndoInset();
5236 BufferView * bv = &cur.bv();
5237 docstring::const_iterator cit = cmd.argument().begin();
5238 docstring::const_iterator const end = cmd.argument().end();
5239 for (; cit != end; ++cit)
5240 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5243 bv->bookmarkEditPosition();
5245 cell(cur.idx())->dispatch(cur, cmd);
5248 case LFUN_CHAR_DELETE_BACKWARD:
5249 case LFUN_CHAR_DELETE_FORWARD:
5250 if (cur.selIsMultiCell()) {
5251 cur.recordUndoInset();
5254 cell(cur.idx())->dispatch(cur, cmd);
5257 case LFUN_CLIPBOARD_PASTE:
5258 case LFUN_PRIMARY_SELECTION_PASTE: {
5259 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5260 theClipboard().getAsText(Clipboard::PlainTextType) :
5261 theSelection().get();
5264 // pass to InsertPlaintextString, but
5265 // only if we have multi-cell content
5266 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5267 cur.recordUndoInset();
5268 if (insertPlaintextString(cur.bv(), clip, false)) {
5269 // content has been replaced,
5270 // so cursor might be invalid
5271 cur.pos() = cur.lastpos();
5272 cur.pit() = cur.lastpit();
5273 bvcur.setCursor(cur);
5277 // Let the cell handle normal text
5278 cell(cur.idx())->dispatch(cur, cmd);
5283 if (!tabularStackDirty()) {
5284 // Check if we have plain text or HTML with rows/columns.
5285 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5286 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5287 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5288 docstring const clip =
5289 theClipboard().getAsText(Clipboard::AnyTextType);
5290 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5291 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5292 doDispatch(cur, ncmd);
5296 else if (theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5297 // This might be tabular data from another LyX instance. Check!
5298 docstring const clip =
5299 theClipboard().getAsText(Clipboard::PlainTextType);
5300 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5301 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5302 doDispatch(cur, ncmd);
5306 if (!cur.selIsMultiCell())
5307 cell(cur.idx())->dispatch(cur, cmd);
5310 if (theClipboard().isInternal()) {
5311 cur.recordUndoInset();
5312 pasteClipboard(cur);
5316 case LFUN_CHANGE_ACCEPT:
5317 case LFUN_CHANGE_REJECT:
5318 case LFUN_FONT_EMPH:
5319 case LFUN_FONT_BOLD:
5320 case LFUN_FONT_BOLDSYMBOL:
5321 case LFUN_FONT_ROMAN:
5322 case LFUN_FONT_NOUN:
5323 case LFUN_FONT_ITAL:
5324 case LFUN_FONT_FRAK:
5325 case LFUN_FONT_TYPEWRITER:
5326 case LFUN_FONT_SANS:
5327 case LFUN_TEXTSTYLE_APPLY:
5328 case LFUN_TEXTSTYLE_UPDATE:
5329 case LFUN_FONT_SIZE:
5330 case LFUN_FONT_UNDERLINE:
5331 case LFUN_FONT_STRIKEOUT:
5332 case LFUN_FONT_CROSSOUT:
5333 case LFUN_FONT_UNDERUNDERLINE:
5334 case LFUN_FONT_UNDERWAVE:
5336 case LFUN_PARAGRAPH_PARAMS_APPLY:
5337 case LFUN_PARAGRAPH_PARAMS:
5338 case LFUN_WORD_CAPITALIZE:
5339 case LFUN_WORD_UPCASE:
5340 case LFUN_WORD_LOWCASE:
5341 case LFUN_CHARS_TRANSPOSE: {
5342 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5343 if (cur.selIsMultiCell()) {
5346 getSelection(cur, rs, re, cs, ce);
5347 Cursor tmpcur = cur;
5348 for (row_type r = rs; r <= re; ++r) {
5349 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5350 // whole row selected
5351 if (act == LFUN_CHANGE_ACCEPT) {
5352 if (tabular.row_info[r].change.inserted())
5353 tabular.row_info[r].change.setUnchanged();
5354 else if (tabular.row_info[r].change.deleted()) {
5355 tabular.deleteRow(r, true);
5360 if (tabular.row_info[r].change.deleted())
5361 tabular.row_info[r].change.setUnchanged();
5362 else if (tabular.row_info[r].change.inserted()) {
5363 tabular.deleteRow(r, true);
5369 for (col_type c = cs; c <= ce; ++c) {
5370 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5371 // whole col selected
5372 if (act == LFUN_CHANGE_ACCEPT) {
5373 if (tabular.column_info[c].change.inserted())
5374 tabular.column_info[c].change.setUnchanged();
5375 else if (tabular.column_info[c].change.deleted()) {
5376 tabular.deleteColumn(c, true);
5381 if (tabular.column_info[c].change.deleted())
5382 tabular.column_info[c].change.setUnchanged();
5383 else if (tabular.column_info[c].change.inserted()) {
5384 tabular.deleteColumn(c, true);
5390 // cursor follows cell:
5391 tmpcur.idx() = tabular.cellIndex(r, c);
5392 // select this cell only:
5395 tmpcur.resetAnchor();
5396 tmpcur.pit() = tmpcur.lastpit();
5397 tmpcur.pos() = tmpcur.top().lastpos();
5398 tmpcur.setCursor(tmpcur);
5399 tmpcur.setSelection();
5400 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5404 tabular.updateIndexes();
5405 // cursor might be invalid
5407 // change bar might need to be redrawn
5408 cur.screenUpdateFlags(Update::Force);
5409 cur.forceBufferUpdate();
5413 cell(cur.idx())->dispatch(cur, cmd);
5418 case LFUN_CHANGE_NEXT:
5419 case LFUN_CHANGE_PREVIOUS: {
5420 // BufferView::dispatch has already moved the cursor, we just
5421 // need to select here if we have a changed row or column
5422 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5424 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5428 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5429 cur.pit() = cur.lastpit();
5430 cur.pos() = cur.lastpos();
5431 cur.selection(true);
5435 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5437 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5441 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5442 cur.pit() = cur.lastpit();
5443 cur.pos() = cur.lastpos();
5444 cur.selection(true);
5451 case LFUN_INSET_SETTINGS:
5452 // relay this lfun to Inset, not to the cell.
5453 Inset::doDispatch(cur, cmd);
5457 // we try to handle this event in the insets dispatch function.
5458 cell(cur.idx())->dispatch(cur, cmd);
5464 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5465 string const & argument, FuncStatus & status) const
5468 int action = Tabular::LAST_ACTION;
5470 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5471 if (tabularFeature[i].feature == s) {
5472 action = tabularFeature[i].action;
5476 if (action == Tabular::LAST_ACTION) {
5478 status.setUnknown(true);
5482 row_type sel_row_start = 0;
5483 row_type sel_row_end = 0;
5484 col_type sel_col_start = 0;
5485 col_type sel_col_end = 0;
5486 Tabular::ltType dummyltt;
5489 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5492 case Tabular::SET_PWIDTH:
5493 case Tabular::SET_MPWIDTH:
5494 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5495 case Tabular::SET_SPECIAL_COLUMN:
5496 case Tabular::SET_SPECIAL_MULTICOLUMN:
5497 case Tabular::APPEND_ROW:
5498 case Tabular::APPEND_COLUMN:
5499 case Tabular::DELETE_ROW:
5500 case Tabular::DELETE_COLUMN:
5501 case Tabular::COPY_ROW:
5502 case Tabular::COPY_COLUMN:
5503 case Tabular::SET_TOP_SPACE:
5504 case Tabular::SET_BOTTOM_SPACE:
5505 case Tabular::SET_INTERLINE_SPACE:
5509 case Tabular::SET_TABULAR_WIDTH:
5510 status.setEnabled(!tabular.rotate
5511 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5514 case Tabular::MOVE_COLUMN_RIGHT:
5515 case Tabular::MOVE_COLUMN_LEFT:
5516 case Tabular::MOVE_ROW_DOWN:
5517 case Tabular::MOVE_ROW_UP: {
5518 if (cur.selection()) {
5519 status.message(_("Selections not supported."));
5520 status.setEnabled(false);
5524 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
5525 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
5526 (action == Tabular::MOVE_COLUMN_LEFT &&
5527 tabular.cellColumn(cur.idx()) == 0) ||
5528 (action == Tabular::MOVE_ROW_DOWN &&
5529 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
5530 (action == Tabular::MOVE_ROW_UP &&
5531 tabular.cellRow(cur.idx()) == 0)) {
5532 status.setEnabled(false);
5536 if (action == Tabular::MOVE_COLUMN_RIGHT ||
5537 action == Tabular::MOVE_COLUMN_LEFT) {
5538 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
5539 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
5540 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
5541 status.message(_("Multi-column in current or"
5542 " destination column."));
5543 status.setEnabled(false);
5548 if (action == Tabular::MOVE_ROW_DOWN ||
5549 action == Tabular::MOVE_ROW_UP) {
5550 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
5551 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
5552 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
5553 status.message(_("Multi-row in current or"
5554 " destination row."));
5555 status.setEnabled(false);
5560 status.setEnabled(true);
5564 case Tabular::SET_DECIMAL_POINT:
5566 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5569 case Tabular::SET_MULTICOLUMN:
5570 case Tabular::UNSET_MULTICOLUMN:
5571 case Tabular::MULTICOLUMN:
5572 // If a row is set as longtable caption, it must not be allowed
5573 // to unset that this row is a multicolumn.
5574 status.setEnabled(sel_row_start == sel_row_end
5575 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5576 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5579 case Tabular::SET_MULTIROW:
5580 case Tabular::UNSET_MULTIROW:
5581 case Tabular::MULTIROW:
5582 // If a row is set as longtable caption, it must not be allowed
5583 // to unset that this row is a multirow.
5584 status.setEnabled(sel_col_start == sel_col_end
5585 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5586 status.setOnOff(tabular.isMultiRow(cur.idx()));
5589 case Tabular::SET_ALL_LINES:
5590 case Tabular::UNSET_ALL_LINES:
5591 case Tabular::SET_INNER_LINES:
5592 case Tabular::SET_BORDER_LINES:
5593 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5596 case Tabular::RESET_FORMAL_DEFAULT:
5597 status.setEnabled(tabular.use_booktabs);
5600 case Tabular::SET_LINE_TOP:
5601 case Tabular::SET_LINE_BOTTOM:
5602 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5605 case Tabular::SET_LINE_LEFT:
5606 case Tabular::SET_LINE_RIGHT:
5607 status.setEnabled(!tabular.use_booktabs
5608 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5611 case Tabular::SET_LTRIM_TOP:
5612 case Tabular::SET_RTRIM_TOP:
5613 status.setEnabled(tabular.use_booktabs
5614 && tabular.cellRow(cur.idx()) != 0
5615 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5618 case Tabular::SET_LTRIM_BOTTOM:
5619 case Tabular::SET_RTRIM_BOTTOM:
5620 status.setEnabled(tabular.use_booktabs
5621 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5622 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5625 case Tabular::TOGGLE_LINE_TOP:
5626 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5627 status.setOnOff(tabular.topLine(cur.idx()));
5630 case Tabular::TOGGLE_LINE_BOTTOM:
5631 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5632 status.setOnOff(tabular.bottomLine(cur.idx()));
5635 case Tabular::TOGGLE_LINE_LEFT:
5636 status.setEnabled(!tabular.use_booktabs
5637 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5638 status.setOnOff(tabular.leftLine(cur.idx()));
5641 case Tabular::TOGGLE_LINE_RIGHT:
5642 status.setEnabled(!tabular.use_booktabs
5643 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5644 status.setOnOff(tabular.rightLine(cur.idx()));
5647 case Tabular::TOGGLE_LTRIM_TOP:
5648 status.setEnabled(tabular.use_booktabs
5649 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5650 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5653 case Tabular::TOGGLE_RTRIM_TOP:
5654 status.setEnabled(tabular.use_booktabs
5655 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5656 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5659 case Tabular::TOGGLE_LTRIM_BOTTOM:
5660 status.setEnabled(tabular.use_booktabs
5661 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5662 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5665 case Tabular::TOGGLE_RTRIM_BOTTOM:
5666 status.setEnabled(tabular.use_booktabs
5667 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5668 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5671 // multirow cells only inherit the alignment of the column if the column has
5672 // no width, otherwise they are left-aligned
5673 // therefore allow always left but right and center only if there is no width
5674 case Tabular::M_ALIGN_LEFT:
5677 case Tabular::ALIGN_LEFT:
5678 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5681 case Tabular::M_ALIGN_RIGHT:
5684 case Tabular::ALIGN_RIGHT:
5685 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5686 && !tabular.getPWidth(cur.idx()).zero()));
5687 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5690 case Tabular::M_ALIGN_CENTER:
5693 case Tabular::ALIGN_CENTER:
5694 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5695 && !tabular.getPWidth(cur.idx()).zero()));
5696 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5699 case Tabular::ALIGN_BLOCK:
5700 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5701 && !tabular.isMultiRow(cur.idx()));
5702 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5705 case Tabular::ALIGN_DECIMAL:
5706 status.setEnabled(!tabular.isMultiRow(cur.idx())
5707 && !tabular.isMultiColumn(cur.idx()));
5708 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5711 case Tabular::M_VALIGN_TOP:
5714 case Tabular::VALIGN_TOP:
5715 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5716 && !tabular.isMultiRow(cur.idx()));
5718 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5721 case Tabular::M_VALIGN_BOTTOM:
5724 case Tabular::VALIGN_BOTTOM:
5725 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5726 && !tabular.isMultiRow(cur.idx()));
5728 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5731 case Tabular::M_VALIGN_MIDDLE:
5734 case Tabular::VALIGN_MIDDLE:
5735 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5736 && !tabular.isMultiRow(cur.idx()));
5738 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5741 case Tabular::SET_LONGTABULAR:
5742 case Tabular::TOGGLE_LONGTABULAR:
5743 // setting as longtable is not allowed when table is inside a float
5744 if (cur.innerInsetOfType(FLOAT_CODE) != nullptr
5745 || cur.innerInsetOfType(WRAP_CODE) != nullptr)
5746 status.setEnabled(false);
5748 status.setEnabled(true);
5749 status.setOnOff(tabular.is_long_tabular);
5752 case Tabular::UNSET_LONGTABULAR:
5753 status.setOnOff(!tabular.is_long_tabular);
5756 case Tabular::TOGGLE_ROTATE_TABULAR:
5757 case Tabular::SET_ROTATE_TABULAR:
5758 status.setOnOff(tabular.rotate != 0);
5761 case Tabular::TABULAR_VALIGN_TOP:
5762 status.setEnabled(tabular.tabular_width.zero());
5763 status.setOnOff(tabular.tabular_valignment
5764 == Tabular::LYX_VALIGN_TOP);
5766 case Tabular::TABULAR_VALIGN_MIDDLE:
5767 status.setEnabled(tabular.tabular_width.zero());
5768 status.setOnOff(tabular.tabular_valignment
5769 == Tabular::LYX_VALIGN_MIDDLE);
5771 case Tabular::TABULAR_VALIGN_BOTTOM:
5772 status.setEnabled(tabular.tabular_width.zero());
5773 status.setOnOff(tabular.tabular_valignment
5774 == Tabular::LYX_VALIGN_BOTTOM);
5777 case Tabular::LONGTABULAR_ALIGN_LEFT:
5778 status.setOnOff(tabular.longtabular_alignment
5779 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5781 case Tabular::LONGTABULAR_ALIGN_CENTER:
5782 status.setOnOff(tabular.longtabular_alignment
5783 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5785 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5786 status.setOnOff(tabular.longtabular_alignment
5787 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5790 case Tabular::UNSET_ROTATE_TABULAR:
5791 status.setOnOff(tabular.rotate == 0);
5794 case Tabular::TOGGLE_ROTATE_CELL:
5795 case Tabular::SET_ROTATE_CELL:
5796 status.setOnOff(!oneCellHasRotationState(false,
5797 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5800 case Tabular::UNSET_ROTATE_CELL:
5801 status.setOnOff(!oneCellHasRotationState(true,
5802 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5805 case Tabular::SET_USEBOX:
5806 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5809 // every row can only be one thing:
5810 // either a footer or header
5811 case Tabular::SET_LTFIRSTHEAD:
5812 status.setEnabled(sel_row_start == sel_row_end);
5813 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5816 case Tabular::UNSET_LTFIRSTHEAD:
5817 status.setEnabled(sel_row_start == sel_row_end);
5818 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5821 case Tabular::SET_LTHEAD:
5822 status.setEnabled(sel_row_start == sel_row_end);
5823 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5826 case Tabular::UNSET_LTHEAD:
5827 status.setEnabled(sel_row_start == sel_row_end);
5828 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5831 case Tabular::SET_LTFOOT:
5832 status.setEnabled(sel_row_start == sel_row_end);
5833 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5836 case Tabular::UNSET_LTFOOT:
5837 status.setEnabled(sel_row_start == sel_row_end);
5838 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5841 case Tabular::SET_LTLASTFOOT:
5842 status.setEnabled(sel_row_start == sel_row_end);
5843 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5846 case Tabular::UNSET_LTLASTFOOT:
5847 status.setEnabled(sel_row_start == sel_row_end);
5848 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5851 case Tabular::SET_LTNEWPAGE:
5852 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5854 case Tabular::UNSET_LTNEWPAGE:
5855 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5858 // only one row in head/firsthead/foot/lasthead can be the caption
5859 // and a multirow cannot be set as caption
5860 case Tabular::SET_LTCAPTION:
5861 status.setEnabled(sel_row_start == sel_row_end
5862 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5863 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5864 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5865 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5866 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5867 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5868 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5869 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5870 && !tabular.isMultiRow(sel_row_start));
5871 status.setOnOff(tabular.ltCaption(sel_row_start));
5874 case Tabular::UNSET_LTCAPTION:
5875 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5878 case Tabular::TOGGLE_LTCAPTION:
5879 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5880 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5881 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5882 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5883 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5884 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5885 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5886 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5887 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5888 status.setOnOff(tabular.ltCaption(sel_row_start));
5891 case Tabular::TOGGLE_BOOKTABS:
5892 case Tabular::SET_BOOKTABS:
5893 status.setOnOff(tabular.use_booktabs);
5896 case Tabular::UNSET_BOOKTABS:
5897 status.setOnOff(!tabular.use_booktabs);
5902 status.setEnabled(false);
5909 // function sets an object as defined in FuncStatus.h:
5910 // states OK, Unknown, Disabled, On, Off.
5911 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5912 FuncStatus & status) const
5914 switch (cmd.action()) {
5915 case LFUN_INSET_MODIFY:
5916 if (cmd.getArg(0) != "tabular")
5918 if (cmd.getArg(1) == "for-dialog") {
5919 // The dialog is asking the status of a command
5920 if (&cur.inset() != this)
5922 string action = cmd.getArg(2);
5923 string arg = cmd.getLongArg(3);
5924 return getFeatureStatus(cur, action, arg, status);
5926 // We always enable the lfun if it is coming from the dialog
5927 // because the dialog makes sure all the settings are valid,
5928 // even though the first argument might not be valid now.
5929 status.setEnabled(true);
5933 case LFUN_TABULAR_FEATURE: {
5934 if (&cur.inset() != this)
5936 string action = cmd.getArg(0);
5937 string arg = cmd.getLongArg(1);
5938 return getFeatureStatus(cur, action, arg, status);
5941 case LFUN_CAPTION_INSERT: {
5942 // caption is only allowed in caption cell of longtable
5943 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5944 status.setEnabled(false);
5947 // only standard caption is allowed
5948 string arg = cmd.getArg(0);
5949 if (!arg.empty() && arg != "Standard") {
5950 status.setEnabled(false);
5953 // check if there is already a caption
5954 bool have_caption = false;
5955 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5956 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5957 ParagraphList::const_iterator pend = itc.paragraphs().end();
5958 for (; pit != pend; ++pit) {
5959 InsetList::const_iterator it = pit->insetList().begin();
5960 InsetList::const_iterator end = pit->insetList().end();
5961 for (; it != end; ++it) {
5962 if (it->inset->lyxCode() == CAPTION_CODE) {
5963 have_caption = true;
5968 status.setEnabled(!have_caption);
5972 // These are only enabled inside tabular
5973 case LFUN_CELL_BACKWARD:
5974 case LFUN_CELL_FORWARD:
5975 status.setEnabled(true);
5978 // disable these with multiple cells selected
5979 case LFUN_INSET_INSERT:
5980 case LFUN_TABULAR_INSERT:
5981 case LFUN_TABULAR_STYLE_INSERT:
5982 case LFUN_FLEX_INSERT:
5983 case LFUN_FLOAT_INSERT:
5984 case LFUN_FLOAT_WIDE_INSERT:
5985 case LFUN_FOOTNOTE_INSERT:
5986 case LFUN_MARGINALNOTE_INSERT:
5987 case LFUN_MATH_INSERT:
5988 case LFUN_MATH_MODE:
5989 case LFUN_MATH_MUTATE:
5990 case LFUN_MATH_DISPLAY:
5991 case LFUN_NOTE_INSERT:
5992 case LFUN_ARGUMENT_INSERT:
5993 case LFUN_BOX_INSERT:
5994 case LFUN_BRANCH_INSERT:
5995 case LFUN_PHANTOM_INSERT:
5996 case LFUN_WRAP_INSERT:
5997 case LFUN_PREVIEW_INSERT:
5998 case LFUN_ERT_INSERT: {
5999 if (cur.selIsMultiCell()) {
6000 status.setEnabled(false);
6003 return cell(cur.idx())->getStatus(cur, cmd, status);
6006 case LFUN_CHANGE_ACCEPT:
6007 case LFUN_CHANGE_REJECT: {
6008 if (cur.selIsMultiCell()) {
6011 getSelection(cur, rs, re, cs, ce);
6012 for (row_type r = rs; r <= re; ++r) {
6013 if (tabular.row_info[r].change.changed()) {
6014 status.setEnabled(true);
6017 for (col_type c = cs; c <= ce; ++c) {
6018 if (tabular.column_info[c].change.changed()) {
6019 status.setEnabled(true);
6025 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
6026 status.setEnabled(true);
6029 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
6030 status.setEnabled(true);
6034 return cell(cur.idx())->getStatus(cur, cmd, status);
6037 // disable in non-fixed-width cells
6038 case LFUN_PARAGRAPH_BREAK:
6039 // multirow does not allow paragraph breaks
6040 if (tabular.isMultiRow(cur.idx())) {
6041 status.setEnabled(false);
6045 case LFUN_NEWLINE_INSERT:
6046 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
6047 && tabular.getPWidth(cur.idx()).zero()) {
6048 status.setEnabled(false);
6051 return cell(cur.idx())->getStatus(cur, cmd, status);
6053 case LFUN_NEWPAGE_INSERT:
6054 status.setEnabled(false);
6058 if (tabularStackDirty() && theClipboard().isInternal()) {
6059 if (cur.selIsMultiCell()) {
6062 getSelection(cur, rs, re, cs, ce);
6063 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
6064 && paste_tabular->nrows() == re - rs + 1)
6065 status.setEnabled(true);
6067 status.setEnabled(false);
6068 status.message(_("Selection size should match clipboard content."));
6071 status.setEnabled(true);
6074 return cell(cur.idx())->getStatus(cur, cmd, status);
6076 case LFUN_INSET_SETTINGS:
6077 // relay this lfun to Inset, not to the cell.
6078 return Inset::getStatus(cur, cmd, status);
6081 // we try to handle this event in the insets dispatch function.
6082 return cell(cur.idx())->getStatus(cur, cmd, status);
6088 Inset::RowFlags InsetTabular::rowFlags() const
6090 if (tabular.is_long_tabular) {
6091 switch (tabular.longtabular_alignment) {
6092 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
6093 return Display | AlignLeft;
6094 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
6096 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
6097 return Display | AlignRight;
6106 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
6108 tabular.latex(os, runparams);
6112 int InsetTabular::plaintext(odocstringstream & os,
6113 OutputParams const & runparams, size_t max_length) const
6115 os << '\n'; // output table on a new line
6116 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
6117 tabular.plaintext(os, runparams, dp, false, 0, max_length);
6118 return PLAINTEXT_NEWLINE;
6122 void InsetTabular::docbook(XMLStream & xs, OutputParams const & runparams) const
6124 tabular.docbook(xs, runparams);
6128 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6130 return tabular.xhtml(xs, rp);
6134 void InsetTabular::validate(LaTeXFeatures & features) const
6136 tabular.validate(features);
6137 features.useInsetLayout(getLayout());
6141 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6143 return tabular.cellInset(idx);
6147 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6149 return tabular.cellInset(idx);
6153 void InsetTabular::cursorPos(BufferView const & bv,
6154 CursorSlice const & sl, bool boundary, int & x, int & y) const
6156 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6158 // y offset correction
6159 y += cellYPos(sl.idx());
6160 y += tabular.textVOffset(sl.idx());
6161 y += tabular.offsetVAlignment();
6163 // x offset correction
6164 x += cellXPos(sl.idx());
6165 x += tabular.textHOffset(sl.idx());
6166 x += ADD_TO_TABULAR_WIDTH;
6170 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6174 Inset const & inset = *tabular.cellInset(cell);
6175 Point o = bv.coordCache().getInsets().xy(&inset);
6176 int const xbeg = o.x_ - tabular.textHOffset(cell);
6177 int const xend = xbeg + tabular.cellWidth(cell);
6178 row_type const row = tabular.cellRow(cell);
6179 int const ybeg = o.y_ - tabular.rowAscent(row)
6180 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6181 int const yend = ybeg + tabular.cellHeight(cell);
6193 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6194 // << " ybeg=" << ybeg << " yend=" << yend
6195 // << " xx=" << xx << " yy=" << yy
6196 // << " dist=" << xx + yy << endl;
6201 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6203 //lyxerr << "InsetTabular::editXY: " << this << endl;
6205 cur.idx() = getNearestCell(cur.bv(), x, y);
6206 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6210 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6212 cur.idx() = getNearestCell(cur.bv(), x, y);
6213 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6217 idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6219 idx_type idx_min = 0;
6220 int dist_min = numeric_limits<int>::max();
6221 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6222 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6223 int const d = dist(bv, i, x, y);
6234 int InsetTabular::cellYPos(idx_type const cell) const
6236 row_type row = tabular.cellRow(cell);
6238 for (row_type r = 0; r < row; ++r)
6239 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6240 + tabular.interRowSpace(r + 1);
6245 int InsetTabular::cellXPos(idx_type const cell) const
6247 col_type col = tabular.cellColumn(cell);
6249 for (col_type c = 0; c < col; ++c)
6250 lx += tabular.column_info[c].width;
6255 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6257 row_type const row = tabular.cellRow(cur.idx());
6258 col_type const col = tabular.cellColumn(cur.idx());
6260 if (isRightToLeft(cur)) {
6261 if (tabular.cellColumn(cur.idx()) == 0) {
6262 if (row == tabular.nrows() - 1)
6264 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6269 cur.idx() = tabular.getLastCellInRow(row - 1);
6271 cur.idx() = tabular.cellIndex(row, col - 1);
6274 if (tabular.isLastCell(cur.idx()))
6276 if (cur.idx() == tabular.getLastCellInRow(row))
6277 cur.idx() = tabular.cellIndex(row + 1, 0);
6279 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6280 cur.idx() = tabular.cellIndex(row, colnextcell);
6284 cur.boundary(false);
6286 if (cur.selIsMultiCell()) {
6287 cur.pit() = cur.lastpit();
6288 cur.pos() = cur.lastpos();
6295 // in visual mode, place cursor at extreme left or right
6297 switch(entry_from) {
6299 case ENTRY_DIRECTION_RIGHT:
6300 cur.posVisToRowExtremity(false /* !left */);
6302 case ENTRY_DIRECTION_LEFT:
6303 cur.posVisToRowExtremity(true /* left */);
6305 case ENTRY_DIRECTION_IGNORE:
6306 // nothing to do in this case
6310 cur.setCurrentFont();
6314 void InsetTabular::movePrevCell(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 (cur.idx() == tabular.getLastCellInRow(row)) {
6323 cur.idx() = tabular.getFirstCellInRow(row);
6324 cur.idx() = tabular.cellAbove(cur.idx());
6326 if (tabular.isLastCell(cur.idx()))
6328 if (cur.idx() == tabular.getLastCellInRow(row))
6329 cur.idx() = tabular.cellIndex(row + 1, 0);
6331 cur.idx() = tabular.cellIndex(row, col + 1);
6334 if (cur.idx() == 0) // first cell
6337 cur.idx() = tabular.getLastCellInRow(row - 1);
6339 cur.idx() = tabular.cellIndex(row, col - 1);
6342 if (cur.selIsMultiCell()) {
6343 cur.pit() = cur.lastpit();
6344 cur.pos() = cur.lastpos();
6348 cur.pit() = cur.lastpit();
6349 cur.pos() = cur.lastpos();
6351 // in visual mode, place cursor at extreme left or right
6353 switch(entry_from) {
6355 case ENTRY_DIRECTION_RIGHT:
6356 cur.posVisToRowExtremity(false /* !left */);
6358 case ENTRY_DIRECTION_LEFT:
6359 cur.posVisToRowExtremity(true /* left */);
6361 case ENTRY_DIRECTION_IGNORE:
6362 // nothing to do in this case
6366 cur.setCurrentFont();
6370 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6372 cur.recordUndoInset(this);
6374 istringstream is(argument);
6375 // limit the size of strings we read to avoid memory problems
6379 size_t safe_guard = 0;
6384 if (safe_guard > 1000) {
6385 LYXERR0("parameter max count reached!");
6389 Tabular::Feature action = Tabular::LAST_ACTION;
6392 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6393 if (s != tabularFeature[i].feature)
6395 action = tabularFeature[i].action;
6398 if (action == Tabular::LAST_ACTION) {
6399 LYXERR0("Feature not found " << s);
6403 if (tabularFeature[i].need_value)
6405 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6406 tabularFeatures(cur, action, val);
6411 static void checkLongtableSpecial(Tabular::ltType & ltt,
6412 string const & special, bool & flag)
6414 if (special == "dl_above") {
6417 } else if (special == "dl_below") {
6418 ltt.bottomDL = flag;
6420 } else if (special == "empty") {
6430 bool InsetTabular::oneCellHasRotationState(bool rotated,
6431 row_type row_start, row_type row_end,
6432 col_type col_start, col_type col_end) const
6434 for (row_type r = row_start; r <= row_end; ++r)
6435 for (col_type c = col_start; c <= col_end; ++c)
6437 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6440 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6447 void InsetTabular::tabularFeatures(Cursor & cur,
6448 Tabular::Feature feature, string const & value)
6450 col_type sel_col_start;
6451 col_type sel_col_end;
6452 row_type sel_row_start;
6453 row_type sel_row_end;
6454 bool setLines = false;
6455 bool setLinesInnerOnly = false;
6456 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6457 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6461 case Tabular::M_ALIGN_LEFT:
6462 case Tabular::ALIGN_LEFT:
6463 setAlign = LYX_ALIGN_LEFT;
6466 case Tabular::M_ALIGN_RIGHT:
6467 case Tabular::ALIGN_RIGHT:
6468 setAlign = LYX_ALIGN_RIGHT;
6471 case Tabular::M_ALIGN_CENTER:
6472 case Tabular::ALIGN_CENTER:
6473 setAlign = LYX_ALIGN_CENTER;
6476 case Tabular::ALIGN_BLOCK:
6477 setAlign = LYX_ALIGN_BLOCK;
6480 case Tabular::ALIGN_DECIMAL:
6481 setAlign = LYX_ALIGN_DECIMAL;
6484 case Tabular::M_VALIGN_TOP:
6485 case Tabular::VALIGN_TOP:
6486 setVAlign = Tabular::LYX_VALIGN_TOP;
6489 case Tabular::M_VALIGN_BOTTOM:
6490 case Tabular::VALIGN_BOTTOM:
6491 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6494 case Tabular::M_VALIGN_MIDDLE:
6495 case Tabular::VALIGN_MIDDLE:
6496 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6503 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6504 row_type const row = tabular.cellRow(cur.idx());
6505 col_type const column = tabular.cellColumn(cur.idx());
6507 Tabular::ltType ltt;
6511 case Tabular::SET_TABULAR_WIDTH:
6512 tabular.setTabularWidth(Length(value));
6515 case Tabular::SET_PWIDTH: {
6516 Length const len(value);
6517 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6518 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6520 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6521 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6526 case Tabular::SET_MPWIDTH:
6527 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6530 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6531 bool const varwidth = value == "on";
6532 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6533 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6537 case Tabular::SET_MROFFSET:
6538 tabular.setMROffset(cur, cur.idx(), Length(value));
6541 case Tabular::SET_SPECIAL_COLUMN:
6542 case Tabular::SET_SPECIAL_MULTICOLUMN:
6543 if (value == "none")
6544 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6546 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6549 case Tabular::APPEND_ROW:
6550 // append the row into the tabular
6551 tabular.appendRow(row);
6554 case Tabular::APPEND_COLUMN:
6555 // append the column into the tabular
6556 tabular.appendColumn(column);
6557 cur.idx() = tabular.cellIndex(row, column);
6560 case Tabular::DELETE_ROW:
6561 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6562 for (col_type c = 0; c < tabular.ncols(); c++) {
6563 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6564 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6565 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6566 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6570 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6571 tabular.deleteRow(sel_row_start);
6572 if (sel_row_start >= tabular.nrows())
6574 cur.idx() = tabular.cellIndex(sel_row_start, column);
6577 cur.selection(false);
6580 case Tabular::DELETE_COLUMN:
6581 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6582 for (row_type r = 0; r < tabular.nrows(); r++)
6583 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6584 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6587 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6588 for (row_type r = 0; r < tabular.nrows(); r++)
6589 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6590 tabular.leftLine(tabular.cellIndex(r, 0)));
6593 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6594 tabular.deleteColumn(sel_col_start);
6595 if (sel_col_start >= tabular.ncols())
6597 cur.idx() = tabular.cellIndex(row, sel_col_start);
6600 cur.selection(false);
6603 case Tabular::COPY_ROW:
6604 tabular.copyRow(row);
6607 case Tabular::COPY_COLUMN:
6608 tabular.copyColumn(column);
6609 cur.idx() = tabular.cellIndex(row, column);
6612 case Tabular::MOVE_COLUMN_RIGHT:
6613 tabular.moveColumn(column, Tabular::RIGHT);
6614 cur.idx() = tabular.cellIndex(row, column + 1);
6617 case Tabular::MOVE_COLUMN_LEFT:
6618 tabular.moveColumn(column, Tabular::LEFT);
6619 cur.idx() = tabular.cellIndex(row, column - 1);
6622 case Tabular::MOVE_ROW_DOWN:
6623 tabular.moveRow(row, Tabular::DOWN);
6624 cur.idx() = tabular.cellIndex(row + 1, column);
6627 case Tabular::MOVE_ROW_UP:
6628 tabular.moveRow(row, Tabular::UP);
6629 cur.idx() = tabular.cellIndex(row - 1, column);
6632 case Tabular::SET_LINE_TOP:
6633 case Tabular::TOGGLE_LINE_TOP: {
6634 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6635 ? (value == "true") : !tabular.topLine(cur.idx());
6636 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6637 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6638 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6642 case Tabular::SET_LINE_BOTTOM:
6643 case Tabular::TOGGLE_LINE_BOTTOM: {
6644 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6645 ? (value == "true") : !tabular.bottomLine(cur.idx());
6646 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6647 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6648 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6652 case Tabular::SET_LTRIM_TOP:
6653 case Tabular::TOGGLE_LTRIM_TOP: {
6654 bool l = (feature == Tabular::SET_LTRIM_TOP)
6655 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6656 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6657 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6658 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6662 case Tabular::SET_RTRIM_TOP:
6663 case Tabular::TOGGLE_RTRIM_TOP: {
6664 bool l = (feature == Tabular::SET_RTRIM_TOP)
6665 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6666 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6667 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6668 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6672 case Tabular::SET_LTRIM_BOTTOM:
6673 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6674 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6675 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6676 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6677 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6678 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6682 case Tabular::SET_RTRIM_BOTTOM:
6683 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6684 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6685 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6686 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6687 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6688 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6692 case Tabular::SET_LINE_LEFT:
6693 case Tabular::TOGGLE_LINE_LEFT: {
6694 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6695 ? (value == "true") : !tabular.leftLine(cur.idx());
6696 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6697 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6698 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6702 case Tabular::SET_LINE_RIGHT:
6703 case Tabular::TOGGLE_LINE_RIGHT: {
6704 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6705 ? (value == "true") : !tabular.rightLine(cur.idx());
6706 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6707 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6708 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6712 case Tabular::M_ALIGN_LEFT:
6713 case Tabular::M_ALIGN_RIGHT:
6714 case Tabular::M_ALIGN_CENTER:
6715 case Tabular::ALIGN_LEFT:
6716 case Tabular::ALIGN_RIGHT:
6717 case Tabular::ALIGN_CENTER:
6718 case Tabular::ALIGN_BLOCK:
6719 case Tabular::ALIGN_DECIMAL:
6720 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6721 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6722 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6723 !tabular.getPWidth(c).zero());
6726 case Tabular::M_VALIGN_TOP:
6727 case Tabular::M_VALIGN_BOTTOM:
6728 case Tabular::M_VALIGN_MIDDLE:
6731 case Tabular::VALIGN_TOP:
6732 case Tabular::VALIGN_BOTTOM:
6733 case Tabular::VALIGN_MIDDLE:
6734 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6735 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6736 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6739 case Tabular::SET_MULTICOLUMN: {
6740 if (!cur.selection()) {
6741 // just multicol for one single cell
6742 // check whether we are completely in a multicol
6743 if (!tabular.isMultiColumn(cur.idx()))
6744 tabular.setMultiColumn(cur, cur.idx(), 1,
6745 tabular.rightLine(cur.idx()));
6748 // we have a selection so this means we just add all these
6749 // cells to form a multicolumn cell
6750 idx_type const s_start = cur.selBegin().idx();
6751 row_type const col_start = tabular.cellColumn(s_start);
6752 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6753 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6754 tabular.rightLine(cur.selEnd().idx()));
6757 cur.selection(false);
6761 case Tabular::UNSET_MULTICOLUMN: {
6762 if (!cur.selection()) {
6763 if (tabular.isMultiColumn(cur.idx()))
6764 tabular.unsetMultiColumn(cur.idx());
6769 case Tabular::MULTICOLUMN: {
6770 if (!cur.selection()) {
6771 if (tabular.isMultiColumn(cur.idx()))
6772 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6774 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6778 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6779 row_type const r = sel_row_start;
6780 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6781 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6784 // If the selection contains at least one singlecol cell
6785 // or multiple multicol cells,
6786 // we assume the user will merge is to a single multicol
6788 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6790 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6794 case Tabular::SET_MULTIROW: {
6795 if (!cur.selection()) {
6796 // just multirow for one single cell
6797 // check whether we are completely in a multirow
6798 if (!tabular.isMultiRow(cur.idx()))
6799 tabular.setMultiRow(cur, cur.idx(), 1,
6800 tabular.bottomLine(cur.idx()),
6801 tabular.getAlignment(cur.idx()));
6804 // we have a selection so this means we just add all this
6805 // cells to form a multirow cell
6806 idx_type const s_start = cur.selBegin().idx();
6807 row_type const row_start = tabular.cellRow(s_start);
6808 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6809 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6810 tabular.bottomLine(cur.selEnd().idx()),
6811 tabular.getAlignment(cur.selEnd().idx()));
6814 cur.selection(false);
6818 case Tabular::UNSET_MULTIROW: {
6819 if (!cur.selection()) {
6820 if (tabular.isMultiRow(cur.idx()))
6821 tabular.unsetMultiRow(cur.idx());
6826 case Tabular::MULTIROW: {
6827 if (!cur.selection()) {
6828 if (tabular.isMultiRow(cur.idx()))
6829 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6831 tabularFeatures(cur, Tabular::SET_MULTIROW);
6835 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6836 col_type const c = sel_col_start;
6837 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6838 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6841 // If the selection contains at least one singlerow cell
6842 // or multiple multirow cells,
6843 // we assume the user will merge is to a single multirow
6845 tabularFeatures(cur, Tabular::SET_MULTIROW);
6847 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6851 case Tabular::SET_INNER_LINES:
6852 setLinesInnerOnly = true;
6854 case Tabular::SET_ALL_LINES:
6857 case Tabular::UNSET_ALL_LINES:
6858 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6859 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6860 idx_type const cell = tabular.cellIndex(r, c);
6861 if (!setLinesInnerOnly || r != sel_row_start)
6862 tabular.setTopLine(cell, setLines);
6863 if ((!setLinesInnerOnly || r != sel_row_end)
6864 && (!setLines || r == sel_row_end))
6865 tabular.setBottomLine(cell, setLines);
6866 if ((!setLinesInnerOnly || c != sel_col_end)
6867 && (!setLines || c == sel_col_end))
6868 tabular.setRightLine(cell, setLines);
6869 if ((!setLinesInnerOnly || c != sel_col_start))
6870 tabular.setLeftLine(cell, setLines);
6874 case Tabular::RESET_FORMAL_DEFAULT:
6875 for (row_type r = 0; r < tabular.nrows(); ++r) {
6876 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6877 for (col_type c = 0; c < tabular.ncols(); ++c) {
6878 idx_type const cell = tabular.cellIndex(r, c);
6879 tabular.setTopLine(cell, head_or_foot);
6880 tabular.setBottomLine(cell, head_or_foot);
6885 case Tabular::SET_BORDER_LINES:
6886 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6887 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6888 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6890 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6891 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6892 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6896 case Tabular::TOGGLE_LONGTABULAR:
6897 if (tabular.is_long_tabular)
6898 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6900 tabular.is_long_tabular = true;
6903 case Tabular::SET_LONGTABULAR:
6904 tabular.is_long_tabular = true;
6907 case Tabular::UNSET_LONGTABULAR:
6908 for (row_type r = 0; r < tabular.nrows(); ++r) {
6909 if (tabular.ltCaption(r)) {
6910 cur.idx() = tabular.cellIndex(r, 0);
6913 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6916 tabular.is_long_tabular = false;
6919 case Tabular::SET_ROTATE_TABULAR:
6920 tabular.rotate = convert<int>(value);
6923 case Tabular::UNSET_ROTATE_TABULAR:
6927 case Tabular::TOGGLE_ROTATE_TABULAR:
6928 // when pressing the rotate button we default to 90° rotation
6929 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6932 case Tabular::TABULAR_VALIGN_TOP:
6933 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6936 case Tabular::TABULAR_VALIGN_MIDDLE:
6937 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6940 case Tabular::TABULAR_VALIGN_BOTTOM:
6941 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6944 case Tabular::LONGTABULAR_ALIGN_LEFT:
6945 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6948 case Tabular::LONGTABULAR_ALIGN_CENTER:
6949 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6952 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6953 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6956 case Tabular::SET_ROTATE_CELL:
6957 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6958 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6959 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6962 case Tabular::UNSET_ROTATE_CELL:
6963 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6964 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6965 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6968 case Tabular::TOGGLE_ROTATE_CELL:
6970 bool oneNotRotated = oneCellHasRotationState(false,
6971 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6973 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6974 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6975 // when pressing the rotate cell button we default to 90° rotation
6977 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6979 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6984 case Tabular::SET_USEBOX: {
6985 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6986 if (val == tabular.getUsebox(cur.idx()))
6987 val = Tabular::BOX_NONE;
6988 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6989 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6990 tabular.setUsebox(tabular.cellIndex(r, c), val);
6994 case Tabular::UNSET_LTFIRSTHEAD:
6997 case Tabular::SET_LTFIRSTHEAD:
6998 tabular.getRowOfLTFirstHead(row, ltt);
6999 checkLongtableSpecial(ltt, value, flag);
7000 tabular.setLTHead(row, flag, ltt, true);
7003 case Tabular::UNSET_LTHEAD:
7006 case Tabular::SET_LTHEAD:
7007 tabular.getRowOfLTHead(row, ltt);
7008 checkLongtableSpecial(ltt, value, flag);
7009 tabular.setLTHead(row, flag, ltt, false);
7012 case Tabular::UNSET_LTFOOT:
7015 case Tabular::SET_LTFOOT:
7016 tabular.getRowOfLTFoot(row, ltt);
7017 checkLongtableSpecial(ltt, value, flag);
7018 tabular.setLTFoot(row, flag, ltt, false);
7021 case Tabular::UNSET_LTLASTFOOT:
7024 case Tabular::SET_LTLASTFOOT:
7025 tabular.getRowOfLTLastFoot(row, ltt);
7026 checkLongtableSpecial(ltt, value, flag);
7027 tabular.setLTFoot(row, flag, ltt, true);
7030 case Tabular::UNSET_LTNEWPAGE:
7033 case Tabular::SET_LTNEWPAGE:
7034 tabular.setLTNewPage(row, flag);
7037 case Tabular::SET_LTCAPTION: {
7038 if (tabular.ltCaption(row))
7040 cur.idx() = tabular.setLTCaption(cur, row, true);
7043 cur.selection(false);
7044 // If a row is set as caption, then also insert
7045 // a caption. Otherwise the LaTeX output is broken.
7046 // Select cell if it is non-empty
7047 if (cur.lastpos() > 0 || cur.lastpit() > 0)
7048 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
7049 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
7053 case Tabular::UNSET_LTCAPTION: {
7054 if (!tabular.ltCaption(row))
7056 cur.idx() = tabular.setLTCaption(cur, row, false);
7059 cur.selection(false);
7060 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
7061 if (lyx::getStatus(fr).enabled())
7066 case Tabular::TOGGLE_LTCAPTION: {
7067 if (tabular.ltCaption(row))
7068 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
7070 tabularFeatures(cur, Tabular::SET_LTCAPTION);
7074 case Tabular::TOGGLE_BOOKTABS:
7075 tabular.use_booktabs = !tabular.use_booktabs;
7078 case Tabular::SET_BOOKTABS:
7079 tabular.use_booktabs = true;
7082 case Tabular::UNSET_BOOKTABS:
7083 tabular.use_booktabs = false;
7086 case Tabular::SET_TOP_SPACE: {
7088 if (value == "default")
7089 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7090 tabular.row_info[r].top_space_default = true;
7091 else if (value == "none")
7092 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7093 tabular.row_info[r].top_space_default = false;
7094 tabular.row_info[r].top_space = len;
7096 else if (isValidLength(value, &len))
7097 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7098 tabular.row_info[r].top_space_default = false;
7099 tabular.row_info[r].top_space = len;
7104 case Tabular::SET_BOTTOM_SPACE: {
7106 if (value == "default")
7107 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7108 tabular.row_info[r].bottom_space_default = true;
7109 else if (value == "none")
7110 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7111 tabular.row_info[r].bottom_space_default = false;
7112 tabular.row_info[r].bottom_space = len;
7114 else if (isValidLength(value, &len))
7115 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7116 tabular.row_info[r].bottom_space_default = false;
7117 tabular.row_info[r].bottom_space = len;
7122 case Tabular::SET_INTERLINE_SPACE: {
7124 if (value == "default")
7125 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7126 tabular.row_info[r].interline_space_default = true;
7127 else if (value == "none")
7128 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7129 tabular.row_info[r].interline_space_default = false;
7130 tabular.row_info[r].interline_space = len;
7132 else if (isValidLength(value, &len))
7133 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7134 tabular.row_info[r].interline_space_default = false;
7135 tabular.row_info[r].interline_space = len;
7140 case Tabular::SET_DECIMAL_POINT:
7141 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7142 tabular.column_info[c].decimal_point = from_utf8(value);
7145 // dummy stuff just to avoid warnings
7146 case Tabular::LAST_ACTION:
7152 bool InsetTabular::copySelection(Cursor & cur)
7154 if (!cur.selection())
7159 getSelection(cur, rs, re, cs, ce);
7161 paste_tabular.reset(new Tabular(tabular));
7163 for (row_type r = 0; r < rs; ++r)
7164 paste_tabular->deleteRow(0, true);
7166 row_type const rows = re - rs + 1;
7167 while (paste_tabular->nrows() > rows)
7168 paste_tabular->deleteRow(rows, true);
7170 for (col_type c = 0; c < cs; ++c)
7171 paste_tabular->deleteColumn(0, true);
7173 col_type const columns = ce - cs + 1;
7174 while (paste_tabular->ncols() > columns)
7175 paste_tabular->deleteColumn(columns, true);
7177 paste_tabular->setBuffer(tabular.buffer());
7179 odocstringstream os;
7180 OutputParams const runparams(nullptr);
7181 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7182 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7183 cap::copySelection(cur, os.str());
7185 // mark tabular stack dirty
7186 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7187 // when we (hopefully) have a one-for-all paste mechanism.
7188 // This must be called after cap::copySelection.
7189 dirtyTabularStack(true);
7195 bool InsetTabular::pasteClipboard(Cursor & cur)
7199 col_type actcol = tabular.cellColumn(cur.idx());
7200 row_type actrow = tabular.cellRow(cur.idx());
7202 if (cur.selIsMultiCell()) {
7205 getSelection(cur, actrow, re, actcol, ce);
7208 col_type const oldncols = tabular.ncols();
7209 for (row_type r1 = 0, r2 = actrow; r1 < paste_tabular->nrows(); ++r1, ++r2) {
7210 // Append rows if needed
7211 if (r2 == tabular.nrows())
7212 tabular.insertRow(r2 - 1, false);
7213 for (col_type c1 = 0, c2 = actcol; c1 < paste_tabular->ncols(); ++c1, ++c2) {
7214 // Append columns if needed
7215 if (c2 == tabular.ncols())
7216 tabular.insertColumn(c2 - 1, false);
7217 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7218 tabular.isPartOfMultiColumn(r2, c2))
7220 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7224 if (tabular.isPartOfMultiColumn(r2, c2)) {
7228 shared_ptr<InsetTableCell> inset(
7229 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7230 tabular.setCellInset(r2, c2, inset);
7231 // FIXME?: why do we need to do this explicitly? (EL)
7232 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7234 if (!lyxrc.ct_markup_copied) {
7235 // do not paste deleted text
7236 inset->acceptChanges();
7237 inset->setChange(Change(buffer().params().track_changes ?
7238 Change::INSERTED : Change::UNCHANGED));
7244 // amend cursor position if cols have been appended
7245 cur.idx() += actrow * (tabular.ncols() - oldncols);
7250 void InsetTabular::cutSelection(Cursor & cur)
7252 if (!cur.selection())
7257 getSelection(cur, rs, re, cs, ce);
7258 for (row_type r = rs; r <= re; ++r) {
7259 for (col_type c = cs; c <= ce; ++c) {
7260 shared_ptr<InsetTableCell> t
7261 = cell(tabular.cellIndex(r, c));
7262 if (buffer().params().track_changes)
7263 // FIXME: Change tracking (MG)
7264 t->setChange(Change(Change::DELETED));
7270 // cursor position might be invalid now
7271 if (cur.pit() > cur.lastpit())
7272 cur.pit() = cur.lastpit();
7273 if (cur.pos() > cur.lastpos())
7274 cur.pos() = cur.lastpos();
7275 cur.clearSelection();
7279 bool InsetTabular::isRightToLeft(Cursor & cur) const
7281 // LASSERT: It might be better to abandon this Buffer.
7282 LASSERT(cur.depth() > 1, return false);
7283 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7284 pos_type const parentpos = cur[cur.depth() - 2].pos();
7285 return parentpar.getFontSettings(buffer().params(),
7286 parentpos).language()->rightToLeft();
7290 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7293 LASSERT(stidx <= enidx, return docstring());
7295 col_type const col1 = tabular.cellColumn(stidx);
7296 col_type const col2 = tabular.cellColumn(enidx);
7297 row_type const row1 = tabular.cellRow(stidx);
7298 row_type const row2 = tabular.cellRow(enidx);
7300 for (col_type col = col1; col <= col2; col++)
7301 for (row_type row = row1; row <= row2; row++) {
7306 retval += tabular.cellInset(row, col)->asString(intoInsets);
7312 ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
7314 LASSERT(stidx <= enidx, return ParagraphList());
7315 ParagraphList retval;
7316 col_type const col1 = tabular.cellColumn(stidx);
7317 col_type const col2 = tabular.cellColumn(enidx);
7318 row_type const row1 = tabular.cellRow(stidx);
7319 row_type const row2 = tabular.cellRow(enidx);
7320 for (col_type col = col1; col <= col2; col++)
7321 for (row_type row = row1; row <= row2; row++)
7322 for (auto const & par : tabular.cellInset(row, col)->paragraphs())
7323 retval.push_back(par);
7328 void InsetTabular::getSelection(Cursor & cur,
7329 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7331 CursorSlice const & beg = cur.selBegin();
7332 CursorSlice const & end = cur.selEnd();
7333 cs = tabular.cellColumn(beg.idx());
7334 ce = tabular.cellColumn(end.idx());
7338 rs = tabular.cellRow(beg.idx());
7339 re = tabular.cellRow(end.idx());
7345 Text * InsetTabular::getText(int idx) const
7347 return size_t(idx) < nargs() ? cell(idx)->getText(0) : nullptr;
7351 bool InsetTabular::isChanged() const
7353 for (idx_type idx = 0; idx < nargs(); ++idx) {
7354 if (cell(idx)->isChanged())
7356 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7358 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7365 void InsetTabular::setChange(Change const & change)
7367 for (idx_type idx = 0; idx < nargs(); ++idx)
7368 cell(idx)->setChange(change);
7372 void InsetTabular::acceptChanges()
7374 for (idx_type idx = 0; idx < nargs(); ++idx)
7375 cell(idx)->acceptChanges();
7376 for (row_type row = 0; row < tabular.nrows(); ++row) {
7377 if (tabular.row_info[row].change.inserted())
7378 tabular.row_info[row].change.setUnchanged();
7379 else if (tabular.row_info[row].change.deleted())
7380 tabular.deleteRow(row, true);
7382 for (col_type col = 0; col < tabular.ncols(); ++col) {
7383 if (tabular.column_info[col].change.inserted())
7384 tabular.column_info[col].change.setUnchanged();
7385 else if (tabular.column_info[col].change.deleted())
7386 tabular.deleteColumn(col, true);
7388 tabular.updateIndexes();
7392 void InsetTabular::rejectChanges()
7394 for (idx_type idx = 0; idx < nargs(); ++idx)
7395 cell(idx)->rejectChanges();
7396 for (row_type row = 0; row < tabular.nrows(); ++row) {
7397 if (tabular.row_info[row].change.deleted())
7398 tabular.row_info[row].change.setUnchanged();
7399 else if (tabular.row_info[row].change.inserted())
7400 tabular.deleteRow(row, true);
7402 for (col_type col = 0; col < tabular.ncols(); ++col) {
7403 if (tabular.column_info[col].change.deleted())
7404 tabular.column_info[col].change.setUnchanged();
7405 else if (tabular.column_info[col].change.inserted())
7406 tabular.deleteColumn(col, true);
7408 tabular.updateIndexes();
7412 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7414 return tabular.getPWidth(cell).zero();
7418 bool InsetTabular::forcePlainLayout(idx_type cell) const
7420 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
7424 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7427 if (buf.length() <= 0)
7432 col_type maxCols = 1;
7433 size_t const len = buf.length();
7437 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7445 maxCols = max(cols, maxCols);
7451 maxCols = max(cols, maxCols);
7457 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7458 loctab = paste_tabular.get();
7459 dirtyTabularStack(true);
7462 cell = bv.cursor().idx();
7463 ocol = tabular.cellColumn(cell);
7464 row = tabular.cellRow(cell);
7468 idx_type cells = loctab->numberofcells;
7471 rows = loctab->nrows();
7472 col_type columns = loctab->ncols();
7475 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7481 // append column if necessary
7482 if (cols == columns) {
7483 loctab->appendColumn(cols - 1);
7484 columns = loctab->ncols();
7485 cells = loctab->numberofcells;
7488 if (cols < columns) {
7489 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7490 Font const font = bv.textMetrics(&inset->text()).
7492 inset->setText(buf.substr(op, p - op), font,
7493 buffer().params().track_changes);
7499 // we can only set this if we are not too far right
7500 if (cols < columns) {
7501 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7502 Font const font = bv.textMetrics(&inset->text()).
7504 inset->setText(buf.substr(op, p - op), font,
7505 buffer().params().track_changes);
7509 // append row if necessary
7510 if (row == rows && p < len - 1) {
7511 loctab->appendRow(row - 1);
7512 rows = loctab->nrows();
7513 cells = loctab->numberofcells;
7516 cell = loctab->cellIndex(row, cols);
7522 // check for the last cell if there is no trailing '\n'
7523 if (cell < cells && op < len) {
7524 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7525 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7526 inset->setText(buf.substr(op, len - op), font,
7527 buffer().params().track_changes);
7533 void InsetTabular::addPreview(DocIterator const & inset_pos,
7534 PreviewLoader & loader) const
7536 DocIterator cell_pos = inset_pos;
7538 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7539 for (row_type r = 0; r < tabular.nrows(); ++r) {
7540 for (col_type c = 0; c < tabular.ncols(); ++c) {
7541 cell_pos.top().idx() = tabular.cellIndex(r, c);
7542 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7548 bool InsetTabular::completionSupported(Cursor const & cur) const
7550 Cursor const & bvCur = cur.bv().cursor();
7551 if (&bvCur.inset() != this)
7553 return cur.text()->completionSupported(cur);
7557 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7559 return completionSupported(cur);
7563 bool InsetTabular::automaticInlineCompletion() const
7565 return lyxrc.completion_inline_text;
7569 bool InsetTabular::automaticPopupCompletion() const
7571 return lyxrc.completion_popup_text;
7575 bool InsetTabular::showCompletionCursor() const
7577 return lyxrc.completion_cursor_text;
7581 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7583 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : nullptr;
7587 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7589 if (!completionSupported(cur))
7591 return cur.text()->completionPrefix(cur);
7595 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7597 if (!completionSupported(cur))
7600 return cur.text()->insertCompletion(cur, s, finished);
7604 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7605 Dimension & dim) const
7607 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7608 tm.completionPosAndDim(cur, x, y, dim);
7612 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7614 istringstream data(in);
7616 lex.setStream(data);
7623 if (!lex || token != "tabular") {
7624 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7628 // This is part of the inset proper that is usually swallowed
7629 // by Buffer::readInset
7631 if (!lex || token != "Tabular") {
7632 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7640 string InsetTabular::params2string(InsetTabular const & inset)
7643 data << "tabular" << ' ';
7645 data << "\\end_inset\n";
7650 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7652 for (col_type c = 0; c < tabular.ncols(); ++c) {
7653 for (row_type r = 0; r < tabular.nrows(); ++r) {
7654 if (!tabular.isPartOfMultiColumn(r,c) &&
7655 !tabular.isPartOfMultiRow(r,c))
7658 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7659 ParagraphList::iterator it = parlist.begin();
7660 ParagraphList::iterator const en = parlist.end();
7661 for (; it != en; ++it)
7662 it->setLayout(dc.plainLayout());