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"
25 #include "buffer_funcs.h"
27 #include "BufferParams.h"
28 #include "BufferView.h"
29 #include "CoordCache.h"
32 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
36 #include "InsetIterator.h"
37 #include "InsetList.h"
39 #include "LaTeXFeatures.h"
43 #include "MetricsInfo.h"
44 #include "OutputParams.h"
45 #include "output_xhtml.h"
46 #include "Paragraph.h"
47 #include "ParagraphParameters.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, Tabular::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 reson 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);
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 aligment
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 Tabular::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 // Substract 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 Tabular::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 Tabular::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 Tabular::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 Tabular::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 Tabular::row_type Tabular::cellRow(idx_type cell) const
1707 if (cell >= numberofcells)
1711 return rowofcell[cell];
1715 Tabular::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 Tabular::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 Tabular::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 Tabular::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 Tabular::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 if (cell + 1 < numberofcells)
2197 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2199 if (cellRow(cell) == 0)
2202 col_type const col = cellColumn(cell);
2203 row_type r = cellRow(cell) - 1;
2204 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2207 return cell_info[r][col].cellno;
2211 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2213 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2214 if (nextrow < nrows())
2215 return cell_info[nextrow][cellColumn(cell)].cellno;
2220 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2222 LASSERT(column != npos && column < ncols(), column = 0);
2223 LASSERT(row != npos && row < nrows(), row = 0);
2224 return cell_info[row][column].cellno;
2228 void Tabular::setUsebox(idx_type cell, BoxType type)
2230 cellInfo(cell).usebox = type;
2234 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2236 if (getRotateCell(cell) == 0
2237 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2238 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2240 if (cellInfo(cell).usebox > 1)
2241 return cellInfo(cell).usebox;
2242 return useBox(cell);
2247 // This are functions used for the longtable support
2249 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2255 row_info[row].endfirsthead = flag;
2259 row_info[row].endhead = flag;
2264 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2267 hd.set = haveLTHead();
2268 return row_info[row].endhead;
2272 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2275 hd.set = haveLTFirstHead();
2276 return row_info[row].endfirsthead;
2280 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2286 row_info[row].endlastfoot = flag;
2290 row_info[row].endfoot = flag;
2295 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2298 fd.set = haveLTFoot();
2299 return row_info[row].endfoot;
2303 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2306 fd.set = haveLTLastFoot();
2307 return row_info[row].endlastfoot;
2311 void Tabular::setLTNewPage(row_type row, bool what)
2313 row_info[row].newpage = what;
2317 bool Tabular::getLTNewPage(row_type row) const
2319 return row_info[row].newpage;
2323 bool Tabular::haveLTHead(bool withcaptions) const
2325 if (!is_long_tabular)
2327 for (row_type i = 0; i < nrows(); ++i)
2328 if (row_info[i].endhead &&
2329 (withcaptions || !row_info[i].caption))
2335 bool Tabular::haveLTFirstHead(bool withcaptions) const
2337 if (!is_long_tabular || endfirsthead.empty)
2339 for (row_type r = 0; r < nrows(); ++r)
2340 if (row_info[r].endfirsthead &&
2341 (withcaptions || !row_info[r].caption))
2347 bool Tabular::haveLTFoot(bool withcaptions) const
2349 if (!is_long_tabular)
2351 for (row_type r = 0; r < nrows(); ++r)
2352 if (row_info[r].endfoot &&
2353 (withcaptions || !row_info[r].caption))
2359 bool Tabular::haveLTLastFoot(bool withcaptions) const
2361 if (!is_long_tabular || endlastfoot.empty)
2363 for (row_type r = 0; r < nrows(); ++r)
2364 if (row_info[r].endlastfoot &&
2365 (withcaptions || !row_info[r].caption))
2371 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2373 idx_type i = getFirstCellInRow(row);
2375 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2376 setTopLine(i, false);
2377 setBottomLine(i, false);
2378 setLeftLine(i, false);
2379 setRightLine(i, false);
2380 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2381 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2382 setLTHead(row, true, endfirsthead, true);
2383 row_info[row].endfirsthead = true;
2386 unsetMultiColumn(i);
2387 // When unsetting a caption row, also all existing
2388 // captions in this row must be dissolved.
2390 row_info[row].caption = what;
2395 bool Tabular::ltCaption(row_type row) const
2397 return row_info[row].caption;
2401 bool Tabular::haveLTCaption(CaptionType captiontype) const
2403 if (!is_long_tabular)
2405 for (row_type r = 0; r < nrows(); ++r) {
2406 if (row_info[r].caption) {
2407 switch (captiontype) {
2408 case CAPTION_FIRSTHEAD:
2409 if (row_info[r].endfirsthead)
2413 if (row_info[r].endhead)
2417 if (row_info[r].endfoot)
2420 case CAPTION_LASTFOOT:
2421 if (row_info[r].endlastfoot)
2433 // end longtable support functions
2435 void Tabular::setRowAscent(row_type row, int height)
2437 if (row >= nrows() || row_info[row].ascent == height)
2439 row_info[row].ascent = height;
2443 void Tabular::setRowDescent(row_type row, int height)
2445 if (row >= nrows() || row_info[row].descent == height)
2447 row_info[row].descent = height;
2451 int Tabular::rowAscent(row_type row) const
2453 LASSERT(row < nrows(), row = 0);
2454 return row_info[row].ascent;
2458 int Tabular::rowDescent(row_type row) const
2460 LASSERT(row < nrows(), row = 0);
2461 return row_info[row].descent;
2465 int Tabular::height() const
2468 for (row_type row = 0; row < nrows(); ++row)
2469 height += rowAscent(row) + rowDescent(row) +
2475 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2477 LASSERT(row < nrows(), return false);
2478 LASSERT(column < ncols(), return false);
2479 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2483 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2485 LASSERT(row < nrows(), return false);
2486 LASSERT(column < ncols(), return false);
2487 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2491 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns,
2492 list<col_type> logical_columns) const
2494 // we only output complete row lines and the 1st row here, the rest
2495 // is done in Tabular::TeXBottomHLine(...)
2497 // get for each column the topline (if any)
2498 map<col_type, bool> topline, topltrims, toprtrims;
2500 bool have_trims = false;
2501 for (auto const & c : columns) {
2502 topline[c] = topLine(cellIndex(row, c));
2503 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2504 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2505 // If cell is part of a multirow and not the first cell of the
2506 // multirow, no line must be drawn.
2508 if (isMultiRow(cellIndex(row, c))
2509 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2511 topltrims[c] = false;
2512 toprtrims[c] = false;
2514 // copy trimming to multicolumn parts
2515 if (isPartOfMultiColumn(row, c)) {
2516 topltrims[c] = topltrims[c-1];
2517 toprtrims[c] = toprtrims[c-1];
2519 if (topline.find(c) != topline.end() && topline.find(c)->second)
2521 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2522 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2526 // do nothing if empty first row, or incomplete row line after
2527 row_type first = getFirstRow(!buffer().params().output_changes);
2528 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2531 // Is this the actual first row (excluding longtable caption row)?
2532 bool const realfirstrow = (row == first
2533 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2535 // only output complete row lines and the 1st row's clines
2536 if (nset == columns.size() && !have_trims) {
2538 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2542 } else if (realfirstrow || have_trims) {
2543 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2545 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2546 std::list<col_type>::const_iterator it2 = columns.begin();
2547 // We need to iterate over the logical columns here, but take care for
2549 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2554 if (topline.find(c)->second) {
2555 col_type offset = 0;
2556 for (col_type j = 0 ; j < c; ++j)
2557 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2559 // If the two iterators differ, we are in bidi with swapped columns
2560 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2561 while (isPartOfMultiColumn(row, c))
2564 if (topltrims.find(c) != topltrims.end()
2565 && topltrims.find(c)->second)
2567 col_type cstart = c;
2568 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2569 if (isMultiColumn(cellIndex(row, c))
2570 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2572 if (c > cstart && topltrims.find(c) != topltrims.end()
2573 && topltrims.find(c)->second) {
2574 if (!isPartOfMultiColumn(row, c))
2577 } else if (toprtrims.find(c) != toprtrims.end()
2578 && toprtrims.find(c)->second)
2582 for (col_type j = cstart ; j < c ; ++j)
2583 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2585 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2586 if (toprtrims.find(c) != toprtrims.end()
2587 && toprtrims.find(c)->second)
2592 os << "(" << trim << ")";
2593 if (firstcol > lastcol)
2594 // This can happen with bidi (swapped columns)
2595 os << "{" << lastcol << '-' << firstcol << "}";
2597 os << "{" << firstcol << '-' << lastcol << "}";
2598 if (c == columns.size() - 1)
2608 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns,
2609 list<col_type> logical_columns) const
2611 // we output bottomlines of row r and the toplines of row r+1
2612 // if the latter do not span the whole tabular
2614 // get the bottomlines of row r, and toplines in next row
2615 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2616 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2617 bool nextrowset = true;
2618 for (auto const & c : columns) {
2619 idx_type const idx = cellIndex(row, c);
2620 bottomline[c] = bottomLine(cellIndex(row, c));
2621 bottomltrims[c] = bottomLineTrim(idx).first;
2622 bottomrtrims[c] = bottomLineTrim(idx).second;
2623 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2624 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2625 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2626 // If cell is part of a multirow and not the last cell of the
2627 // multirow, no line must be drawn.
2629 if (isMultiRow(cellIndex(row, c))
2630 && isMultiRow(cellIndex(row + 1, c))
2631 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2632 bottomline[c] = false;
2634 bottomltrims[c] = false;
2635 bottomrtrims[c] = false;
2636 topltrims[c] = false;
2637 toprtrims[c] = false;
2639 // copy trimming in multicolumn parts
2640 if (isPartOfMultiColumn(row, c)) {
2641 topltrims[c] = topltrims[c-1];
2642 toprtrims[c] = toprtrims[c-1];
2643 bottomltrims[c] = bottomltrims[c-1];
2644 bottomrtrims[c] = bottomrtrims[c-1];
2647 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2650 // combine this row's bottom lines and next row's toplines if necessary
2652 bool have_trims = false;
2653 for (auto const & c : columns) {
2655 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2656 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2657 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2658 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2659 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2660 if (bottomline.find(c)->second)
2662 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2663 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2667 // do nothing if empty, OR incomplete row line with a topline in next row
2668 if (nset == 0 || (nextrowset && nset != columns.size()))
2671 if (nset == columns.size() && !have_trims) {
2673 os << (lastrow ? "\\bottomrule" : "\\midrule");
2677 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2679 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2680 std::list<col_type>::const_iterator it2 = columns.begin();
2681 // We need to iterate over the logical columns here, but take care for
2683 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2688 if (bottomline.find(c)->second) {
2689 col_type offset = 0;
2690 for (col_type j = 0 ; j < c; ++j)
2691 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2693 // If the two iterators differ, we are in bidi with swapped columns
2694 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2695 while (isPartOfMultiColumn(row, c))
2698 if (bottomltrims.find(c) != bottomltrims.end()
2699 && bottomltrims.find(c)->second)
2701 col_type cstart = c;
2702 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2703 if (isMultiColumn(cellIndex(row, c))
2705 && isPartOfMultiColumn(row, c + 1))
2708 && bottomltrims.find(c) != bottomltrims.end()
2709 && bottomltrims.find(c)->second) {
2710 if (!isPartOfMultiColumn(row, c))
2713 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2714 && bottomrtrims.find(c)->second)
2718 for (col_type j = cstart ; j < c ; ++j)
2719 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2721 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2722 if (bottomrtrims.find(c) != bottomrtrims.end()
2723 && bottomrtrims.find(c)->second)
2728 os << "(" << trim << ")";
2729 if (firstcol > lastcol)
2730 // This can happen with bidi (swapped columns)
2731 os << "{" << lastcol << '-' << firstcol << "}";
2733 os << "{" << firstcol << '-' << lastcol << "}";
2734 if (c == columns.size() - 1)
2744 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2745 bool & ismulticol, bool & ismultirow,
2746 bool const bidi) const
2748 row_type const r = cellRow(cell);
2749 if (is_long_tabular && row_info[r].caption)
2752 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2753 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2754 // figure out how to set the lines
2755 // we always set double lines to the right of the cell
2756 // or left in bidi RTL, respectively.
2757 col_type const c = cellColumn(cell);
2758 col_type const nextcol = c + columnSpan(cell);
2759 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2760 bool colright = columnRightLine(c);
2761 bool colleft = columnLeftLine(c);
2762 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2763 bool nextcellleft = nextcol < ncols()
2764 && leftLine(cellIndex(r, nextcol));
2765 bool coldouble = colright && nextcolleft;
2766 bool celldouble = rightLine(cell) && nextcellleft;
2768 ismulticol = (isMultiColumn(cell)
2769 || (c == 0 && colleft != leftLine(cell))
2770 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2771 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2772 || (coldouble != celldouble))
2775 // we center in multicol when no decimal point
2777 docstring const align_d = column_info[c].decimal_point;
2778 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2779 bool const nosep = !dit;
2780 ismulticol |= nosep;
2781 celldouble &= nosep;
2784 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2785 int latexcolspan = columnSpan(cell);
2786 for (col_type col = c; col < c + columnSpan(cell); ++col)
2787 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2791 os << "\\multicolumn{" << latexcolspan << "}{";
2792 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2793 || (!bidi && c == 0 && leftLine(cell))))
2795 if (bidi && celldouble)
2796 // add extra vertical line if we want a double one
2798 if (!cellInfo(cell).align_special.empty()) {
2799 os << cellInfo(cell).align_special;
2801 if (!getPWidth(cell).zero()) {
2803 case LYX_ALIGN_LEFT:
2804 os << ">{\\raggedright}";
2806 case LYX_ALIGN_RIGHT:
2807 os << ">{\\raggedleft}";
2809 case LYX_ALIGN_CENTER:
2810 os << ">{\\centering}";
2816 case LYX_VALIGN_TOP:
2819 case LYX_VALIGN_MIDDLE:
2822 case LYX_VALIGN_BOTTOM:
2827 << from_ascii(getPWidth(cell).asLatexString())
2831 case LYX_ALIGN_LEFT:
2834 case LYX_ALIGN_RIGHT:
2841 } // end if else !getPWidth
2842 } // end if else !cellinfo_of_cell
2843 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2845 if (!bidi && celldouble)
2846 // add extra vertical line if we want a double one
2849 } // end if ismulticol
2851 // we only need code for the first multirow cell
2852 ismultirow = isMultiRow(cell);
2854 os << "\\multirow{" << rowSpan(cell) << "}{";
2855 if (!getPWidth(cell).zero())
2856 os << from_ascii(getPWidth(cell).asLatexString());
2858 // we need to set a default value
2861 if (!getMROffset(cell).zero())
2862 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2864 } // end if ismultirow
2866 if (getRotateCell(cell) != 0)
2867 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2869 if (getUsebox(cell) == BOX_PARBOX) {
2872 case LYX_VALIGN_TOP:
2875 case LYX_VALIGN_MIDDLE:
2878 case LYX_VALIGN_BOTTOM:
2882 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2884 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2885 os << "\\begin{minipage}[";
2887 case LYX_VALIGN_TOP:
2890 case LYX_VALIGN_MIDDLE:
2893 case LYX_VALIGN_BOTTOM:
2897 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2899 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2900 os << "\\begin{varwidth}[";
2902 case LYX_VALIGN_TOP:
2905 case LYX_VALIGN_MIDDLE:
2908 case LYX_VALIGN_BOTTOM:
2912 os << "]{\\linewidth}\n";
2917 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2918 bool ismulticol, bool ismultirow) const
2920 row_type const r = cellRow(cell);
2921 if (is_long_tabular && row_info[r].caption)
2925 if (getUsebox(cell) == BOX_PARBOX)
2927 else if (getUsebox(cell) == BOX_MINIPAGE)
2928 os << breakln << "\\end{minipage}";
2929 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2930 os << breakln << "\\end{varwidth}";
2931 if (getRotateCell(cell) != 0)
2932 os << breakln << "\\end{turn}";
2940 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2941 OutputParams const & runparams,
2942 list<col_type> columns,
2943 list<col_type> logical_columns) const
2945 if (!is_long_tabular)
2949 // output caption which is in no header or footer
2950 if (haveLTCaption()) {
2951 for (row_type r = 0; r < nrows(); ++r) {
2952 if (row_info[r].caption &&
2953 !row_info[r].endfirsthead && !row_info[r].endhead &&
2954 !row_info[r].endfoot && !row_info[r].endlastfoot)
2955 TeXRow(os, r, runparams, columns, logical_columns);
2958 // output first header info
2959 if (haveLTFirstHead()) {
2960 if (endfirsthead.topDL)
2962 for (row_type r = 0; r < nrows(); ++r) {
2963 if (row_info[r].endfirsthead)
2964 TeXRow(os, r, runparams, columns, logical_columns);
2966 if (endfirsthead.bottomDL)
2968 os << "\\endfirsthead\n";
2970 // output header info
2972 if (endfirsthead.empty && !haveLTFirstHead())
2973 os << "\\endfirsthead\n";
2976 for (row_type r = 0; r < nrows(); ++r) {
2977 if (row_info[r].endhead)
2978 TeXRow(os, r, runparams, columns, logical_columns);
2980 if (endhead.bottomDL)
2982 os << "\\endhead\n";
2984 // output footer info
2988 for (row_type r = 0; r < nrows(); ++r) {
2989 if (row_info[r].endfoot)
2990 TeXRow(os, r, runparams, columns, logical_columns);
2992 if (endfoot.bottomDL)
2994 os << "\\endfoot\n";
2995 if (endlastfoot.empty && !haveLTLastFoot())
2996 os << "\\endlastfoot\n";
2998 // output lastfooter info
2999 if (haveLTLastFoot()) {
3000 if (endlastfoot.topDL)
3002 for (row_type r = 0; r < nrows(); ++r) {
3003 if (row_info[r].endlastfoot)
3004 TeXRow(os, r, runparams, columns, logical_columns);
3006 if (endlastfoot.bottomDL)
3008 os << "\\endlastfoot\n";
3013 bool Tabular::isValidRow(row_type row) const
3015 if (!is_long_tabular)
3017 return !row_info[row].endhead && !row_info[row].endfirsthead
3018 && !row_info[row].endfoot && !row_info[row].endlastfoot
3019 && !row_info[row].caption;
3023 void Tabular::TeXRow(otexstream & os, row_type row,
3024 OutputParams const & runparams,
3025 list<col_type> columns, list<col_type> logical_columns) const
3027 //output the top line
3028 TeXTopHLine(os, row, columns, logical_columns);
3030 if (row_info[row].top_space_default) {
3032 os << "\\addlinespace\n";
3034 os << "\\noalign{\\vskip\\doublerulesep}\n";
3035 } else if(!row_info[row].top_space.zero()) {
3037 os << "\\addlinespace["
3038 << from_ascii(row_info[row].top_space.asLatexString())
3041 os << "\\noalign{\\vskip"
3042 << from_ascii(row_info[row].top_space.asLatexString())
3046 bool ismulticol = false;
3047 bool ismultirow = false;
3049 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3050 // Luabibdi (used by LuaTeX) behaves like classic
3051 bool const bidi_rtl =
3052 runparams.local_font->isRightToLeft()
3053 && runparams.useBidiPackage();
3054 bool const ct = !buffer().params().output_changes;
3056 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3058 for (auto const & c : columns) {
3059 if (isPartOfMultiColumn(row, c))
3062 idx_type cell = cellIndex(row, c);
3064 if (isPartOfMultiRow(row, c)
3065 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3066 if (cell != lastcell)
3071 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3072 InsetTableCell const * inset = cellInset(cell);
3074 Paragraph const & par = inset->paragraphs().front();
3076 os.texrow().forceStart(par.id(), 0);
3078 bool rtl = par.isRTL(buffer().params())
3080 && getPWidth(cell).zero()
3081 && !runparams.use_polyglossia;
3085 par.getParLanguage(buffer().params())->lang();
3086 if (lang == "farsi")
3088 else if (lang == "arabic_arabi")
3090 // currently, remaining RTL languages are
3091 // arabic_arabtex and hebrew
3095 // pass to the OutputParams that we are in a cell and
3096 // which alignment we have set.
3097 // InsetNewline needs this context information.
3098 OutputParams newrp(runparams);
3099 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3100 ? OutputParams::PLAIN
3101 : OutputParams::ALIGNED;
3103 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3104 // copy cell and split in 2
3105 InsetTableCell head = InsetTableCell(*cellInset(cell));
3106 head.setBuffer(const_cast<Buffer &>(buffer()));
3107 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3109 dit.push_back(CursorSlice(head));
3110 head.setMacrocontextPositionRecursive(dit);
3111 bool hassep = false;
3112 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3114 tail.setBuffer(head.buffer());
3116 dit.push_back(CursorSlice(tail));
3117 tail.setMacrocontextPositionRecursive(dit);
3121 tail.latex(os, newrp);
3124 head.latex(os, newrp);
3126 head.latex(os, newrp);
3129 tail.latex(os, newrp);
3132 } else if (ltCaption(row)) {
3133 // Inside longtable caption rows, we must only output the caption inset
3134 // with its content and omit anything outside of that (see #10791)
3135 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
3136 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
3137 for (; it != i_end; ++it) {
3138 if (it->lyxCode() != CAPTION_CODE)
3140 it->latex(os, runparams);
3143 } else if (!isPartOfMultiRow(row, c)) {
3144 if (!runparams.nice)
3145 os.texrow().start(par.id(), 0);
3146 inset->latex(os, newrp);
3149 runparams.encoding = newrp.encoding;
3153 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3154 if (cell != lastcell) { // not last cell in row
3161 os << "\\tabularnewline";
3162 if (row_info[row].bottom_space_default) {
3164 os << "\\addlinespace";
3166 os << "[\\doublerulesep]";
3167 } else if (!row_info[row].bottom_space.zero()) {
3169 os << "\\addlinespace";
3171 << from_ascii(row_info[row].bottom_space.asLatexString())
3176 //output the bottom line
3177 TeXBottomHLine(os, row, columns, logical_columns);
3179 if (row_info[row].interline_space_default) {
3181 os << "\\addlinespace\n";
3183 os << "\\noalign{\\vskip\\doublerulesep}\n";
3184 } else if (!row_info[row].interline_space.zero()) {
3186 os << "\\addlinespace["
3187 << from_ascii(row_info[row].interline_space.asLatexString())
3190 os << "\\noalign{\\vskip"
3191 << from_ascii(row_info[row].interline_space.asLatexString())
3197 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3199 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3200 && !hasVarwidthColumn();
3201 bool const is_xltabular = is_long_tabular
3202 && (hasVarwidthColumn() || !tabular_width.zero());
3203 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3205 //+---------------------------------------------------------------------
3206 //+ first the opening preamble +
3207 //+---------------------------------------------------------------------
3210 if (!TexRow::isNone(pos))
3211 os.texrow().start(pos);
3214 if (is_long_tabular)
3215 os << "\\begin{landscape}\n";
3217 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3220 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3221 // order for RTL (#9686). Thus we use this list.
3222 bool const bidi_rtl =
3223 runparams.local_font->isRightToLeft()
3224 && runparams.useBidiPackage();
3225 list<col_type> columns;
3226 list<col_type> logical_columns;
3227 for (col_type cl = 0; cl < ncols(); ++cl) {
3228 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3231 columns.push_front(cl);
3233 columns.push_back(cl);
3234 // for some calculations, we need the logical (non-swapped)
3235 // columns also in bidi.
3236 logical_columns.push_back(cl);
3239 // If we use \cline or \cmidrule, we need to locally de-activate
3240 // the - character when using languages that activate it (e.g., Czech, Slovak).
3241 bool deactivate_chars = false;
3242 if ((runparams.use_babel || runparams.use_polyglossia)
3243 && contains(runparams.active_chars, '-')) {
3244 bool have_clines = false;
3245 // Check if we use \cline or \cmidrule
3246 for (row_type row = 0; row < nrows(); ++row) {
3247 col_type bset = 0, tset = 0;
3248 for (auto const & c : columns) {
3249 idx_type const idx = cellIndex(row, c);
3250 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3251 || topLineTrim(idx).first || topLineTrim(idx).second) {
3255 if (bottomLine(cellIndex(row, c)))
3257 if (topLine(cellIndex(row, c)))
3260 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3266 deactivate_chars = true;
3267 os << "\\begingroup\n"
3268 << "\\catcode`\\-=12\n";
3272 if (is_long_tabular) {
3274 os << "\\begin{xltabular}";
3276 os << "\\begin{longtable}";
3277 switch (longtabular_alignment) {
3278 case LYX_LONGTABULAR_ALIGN_LEFT:
3281 case LYX_LONGTABULAR_ALIGN_CENTER:
3284 case LYX_LONGTABULAR_ALIGN_RIGHT:
3289 if (tabular_width.zero())
3290 os << "{" << from_ascii("\\columnwidth") << "}";
3292 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3295 if (is_tabular_star)
3296 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3297 else if (hasVarwidthColumn()) {
3298 os << "\\begin{tabularx}{";
3299 if (tabular_width.zero())
3300 os << from_ascii("\\columnwidth") << "}";
3302 os << from_ascii(tabular_width.asLatexString()) << "}";
3304 os << "\\begin{tabular}";
3305 switch (tabular_valignment) {
3306 case LYX_VALIGN_TOP:
3309 case LYX_VALIGN_MIDDLE:
3311 case LYX_VALIGN_BOTTOM:
3319 if (is_tabular_star)
3320 os << "@{\\extracolsep{\\fill}}";
3322 for (auto const & c : columns) {
3323 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3325 if (!column_info[c].align_special.empty()) {
3326 os << column_info[c].align_special;
3328 if (!column_info[c].p_width.zero()) {
3329 bool decimal = false;
3330 switch (column_info[c].alignment) {
3331 case LYX_ALIGN_LEFT:
3332 os << ">{\\raggedright}";
3334 case LYX_ALIGN_RIGHT:
3335 os << ">{\\raggedleft}";
3337 case LYX_ALIGN_CENTER:
3338 os << ">{\\centering}";
3340 case LYX_ALIGN_NONE:
3341 case LYX_ALIGN_BLOCK:
3342 case LYX_ALIGN_LAYOUT:
3343 case LYX_ALIGN_SPECIAL:
3345 case LYX_ALIGN_DECIMAL: {
3347 os << ">{\\raggedright}";
3349 os << ">{\\raggedleft}";
3356 switch (column_info[c].valignment) {
3357 case LYX_VALIGN_TOP:
3358 // this is the default
3360 case LYX_VALIGN_MIDDLE:
3363 case LYX_VALIGN_BOTTOM:
3369 // Fixed-width cells with alignment at decimal separator
3370 // are output as two cells of half the width with the decimal
3371 // separator as column sep. This effectively puts the content
3372 // centered, which differs from the normal decimal sep alignment
3373 // and is not ideal, but we cannot do better ATM (see #9568).
3374 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3376 docstring const halffixedwith =
3377 from_ascii(Length(column_info[c].p_width.value() / 2,
3378 column_info[c].p_width.unit()).asLatexString());
3382 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3389 << from_ascii(column_info[c].p_width.asLatexString())
3391 } else if (column_info[c].varwidth) {
3392 switch (column_info[c].alignment) {
3393 case LYX_ALIGN_LEFT:
3394 os << ">{\\raggedright\\arraybackslash}";
3396 case LYX_ALIGN_RIGHT:
3397 os << ">{\\raggedleft\\arraybackslash}";
3399 case LYX_ALIGN_CENTER:
3400 os << ">{\\centering\\arraybackslash}";
3402 case LYX_ALIGN_NONE:
3403 case LYX_ALIGN_BLOCK:
3404 case LYX_ALIGN_LAYOUT:
3405 case LYX_ALIGN_SPECIAL:
3406 case LYX_ALIGN_DECIMAL:
3410 } else if (isVTypeColumn(c)) {
3411 switch (column_info[c].alignment) {
3412 case LYX_ALIGN_LEFT:
3413 os << ">{\\raggedright}";
3415 case LYX_ALIGN_RIGHT:
3416 os << ">{\\raggedleft}";
3418 case LYX_ALIGN_CENTER:
3419 os << ">{\\centering}";
3421 case LYX_ALIGN_NONE:
3422 case LYX_ALIGN_BLOCK:
3423 case LYX_ALIGN_LAYOUT:
3424 case LYX_ALIGN_SPECIAL:
3425 case LYX_ALIGN_DECIMAL:
3428 os << "V{\\linewidth}";
3430 switch (column_info[c].alignment) {
3431 case LYX_ALIGN_LEFT:
3434 case LYX_ALIGN_RIGHT:
3437 case LYX_ALIGN_DECIMAL:
3438 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3444 } // end if else !column_info[i].p_width
3445 } // end if else !column_info[i].align_special
3446 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3451 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3453 //+---------------------------------------------------------------------
3454 //+ the single row and columns (cells) +
3455 //+---------------------------------------------------------------------
3457 for (row_type r = 0; r < nrows(); ++r) {
3458 if (!buffer().params().output_changes && row_info[r].change.deleted())
3460 if (isValidRow(r)) {
3461 TeXRow(os, r, runparams, columns, logical_columns);
3462 if (is_long_tabular && row_info[r].newpage)
3463 os << "\\newpage\n";
3467 //+---------------------------------------------------------------------
3468 //+ the closing of the tabular +
3469 //+---------------------------------------------------------------------
3471 if (is_long_tabular) {
3473 os << "\\end{xltabular}";
3475 os << "\\end{longtable}";
3477 if (is_tabular_star)
3478 os << "\\end{tabular*}";
3479 else if (hasVarwidthColumn())
3480 os << "\\end{tabularx}";
3482 os << "\\end{tabular}";
3485 if (deactivate_chars)
3487 os << "\n\\endgroup\n";
3490 if (is_long_tabular)
3491 os << breakln << "\\end{landscape}";
3493 os << breakln << "\\end{turn}";
3496 if (!TexRow::isNone(pos))
3497 os.texrow().start(pos);
3501 int Tabular::docbookRow(odocstream & os, row_type row,
3502 OutputParams const & runparams) const
3505 idx_type cell = getFirstCellInRow(row);
3508 for (col_type c = 0; c < ncols(); ++c) {
3509 if (isPartOfMultiColumn(row, c))
3512 os << "<entry align=\"";
3513 switch (getAlignment(cell)) {
3514 case LYX_ALIGN_LEFT:
3517 case LYX_ALIGN_RIGHT:
3525 os << "\" valign=\"";
3526 switch (getVAlignment(cell)) {
3527 case LYX_VALIGN_TOP:
3530 case LYX_VALIGN_BOTTOM:
3533 case LYX_VALIGN_MIDDLE:
3538 if (isMultiColumn(cell)) {
3539 os << " namest=\"col" << c << "\" ";
3540 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3544 ret += cellInset(cell)->docbook(os, runparams);
3553 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3557 //+---------------------------------------------------------------------
3558 //+ first the opening preamble +
3559 //+---------------------------------------------------------------------
3561 os << "<tgroup cols=\"" << ncols()
3562 << "\" colsep=\"1\" rowsep=\"1\">\n";
3564 for (col_type c = 0; c < ncols(); ++c) {
3565 os << "<colspec colname=\"col" << c << "\" align=\"";
3566 switch (column_info[c].alignment) {
3567 case LYX_ALIGN_LEFT:
3570 case LYX_ALIGN_RIGHT:
3578 if (runparams.flavor == OutputParams::XML)
3584 //+---------------------------------------------------------------------
3585 //+ Long Tabular case +
3586 //+---------------------------------------------------------------------
3588 // output caption info
3589 // The caption flag wins over head/foot
3590 if (haveLTCaption()) {
3591 os << "<caption>\n";
3593 for (row_type r = 0; r < nrows(); ++r) {
3594 if (row_info[r].caption) {
3595 ret += docbookRow(os, r, runparams);
3598 os << "</caption>\n";
3601 // output header info
3602 if (haveLTHead(false) || haveLTFirstHead(false)) {
3605 for (row_type r = 0; r < nrows(); ++r) {
3606 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3607 !row_info[r].caption) {
3608 ret += docbookRow(os, r, runparams);
3614 // output footer info
3615 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3618 for (row_type r = 0; r < nrows(); ++r) {
3619 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3620 !row_info[r].caption) {
3621 ret += docbookRow(os, r, runparams);
3628 //+---------------------------------------------------------------------
3629 //+ the single row and columns (cells) +
3630 //+---------------------------------------------------------------------
3634 for (row_type r = 0; r < nrows(); ++r) {
3635 if (isValidRow(r)) {
3636 ret += docbookRow(os, r, runparams);
3641 //+---------------------------------------------------------------------
3642 //+ the closing of the tabular +
3643 //+---------------------------------------------------------------------
3652 docstring Tabular::xhtmlRow(XMLStream & xs, row_type row,
3653 OutputParams const & runparams, bool header) const
3656 string const celltag = header ? "th" : "td";
3657 idx_type cell = getFirstCellInRow(row);
3659 xs << xml::StartTag("tr");
3660 for (col_type c = 0; c < ncols(); ++c) {
3661 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3666 Length const cwidth = column_info[c].p_width;
3667 if (!cwidth.zero()) {
3668 string const hwidth = cwidth.asHTMLString();
3669 attr << "style =\"width: " << hwidth << ";\" ";
3673 switch (getAlignment(cell)) {
3674 case LYX_ALIGN_LEFT:
3677 case LYX_ALIGN_RIGHT:
3685 attr << " valign='";
3686 switch (getVAlignment(cell)) {
3687 case LYX_VALIGN_TOP:
3690 case LYX_VALIGN_BOTTOM:
3693 case LYX_VALIGN_MIDDLE:
3698 if (isMultiColumn(cell))
3699 attr << " colspan='" << columnSpan(cell) << "'";
3700 else if (isMultiRow(cell))
3701 attr << " rowspan='" << rowSpan(cell) << "'";
3703 xs << xml::StartTag(celltag, attr.str(), true) << xml::CR();
3704 ret += cellInset(cell)->xhtml(xs, runparams);
3705 xs << xml::EndTag(celltag) << xml::CR();
3708 xs << xml::EndTag("tr");
3713 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3717 if (is_long_tabular) {
3718 // we'll wrap it in a div, so as to deal with alignment
3720 switch (longtabular_alignment) {
3721 case LYX_LONGTABULAR_ALIGN_LEFT:
3724 case LYX_LONGTABULAR_ALIGN_CENTER:
3727 case LYX_LONGTABULAR_ALIGN_RIGHT:
3731 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3733 // The caption flag wins over head/foot
3734 if (haveLTCaption()) {
3735 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3737 for (row_type r = 0; r < nrows(); ++r)
3738 if (row_info[r].caption)
3739 ret += xhtmlRow(xs, r, runparams);
3740 xs << xml::EndTag("div") << xml::CR();
3744 xs << xml::StartTag("table") << xml::CR();
3746 // output header info
3747 bool const havefirsthead = haveLTFirstHead(false);
3748 // if we have a first head, then we are going to ignore the
3749 // headers for the additional pages, since there aren't any
3750 // in XHTML. this test accomplishes that.
3751 bool const havehead = !havefirsthead && haveLTHead(false);
3752 if (havehead || havefirsthead) {
3753 xs << xml::StartTag("thead") << xml::CR();
3754 for (row_type r = 0; r < nrows(); ++r) {
3755 if (((havefirsthead && row_info[r].endfirsthead) ||
3756 (havehead && row_info[r].endhead)) &&
3757 !row_info[r].caption) {
3758 ret += xhtmlRow(xs, r, runparams, true);
3761 xs << xml::EndTag("thead") << xml::CR();
3763 // output footer info
3764 bool const havelastfoot = haveLTLastFoot(false);
3766 bool const havefoot = !havelastfoot && haveLTFoot(false);
3767 if (havefoot || havelastfoot) {
3768 xs << xml::StartTag("tfoot") << xml::CR();
3769 for (row_type r = 0; r < nrows(); ++r) {
3770 if (((havelastfoot && row_info[r].endlastfoot) ||
3771 (havefoot && row_info[r].endfoot)) &&
3772 !row_info[r].caption) {
3773 ret += xhtmlRow(xs, r, runparams);
3776 xs << xml::EndTag("tfoot") << xml::CR();
3779 xs << xml::StartTag("tbody") << xml::CR();
3780 for (row_type r = 0; r < nrows(); ++r) {
3781 if (isValidRow(r)) {
3782 ret += xhtmlRow(xs, r, runparams);
3785 xs << xml::EndTag("tbody")
3787 << xml::EndTag("table")
3789 if (is_long_tabular)
3790 xs << xml::EndTag("div") << xml::CR();
3795 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3796 vector<unsigned int> const & clen) const
3798 idx_type const fcell = getFirstCellInRow(row);
3799 idx_type const n = numberOfCellsInRow(row) + fcell;
3802 for (idx_type i = fcell; i < n; ++i) {
3812 for (idx_type i = fcell; i < n; ++i) {
3823 col_type column = cellColumn(i);
3824 int len = clen[column];
3825 while (column < ncols() - 1
3826 && isPartOfMultiColumn(row, ++column))
3827 len += clen[column] + 4;
3828 os << docstring(len, ch);
3843 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3844 vector<unsigned int> const & clen) const
3846 idx_type const fcell = getFirstCellInRow(row);
3847 idx_type const n = numberOfCellsInRow(row) + fcell;
3850 for (idx_type i = fcell; i < n; ++i) {
3851 if (bottomLine(i)) {
3860 for (idx_type i = fcell; i < n; ++i) {
3861 if (bottomLine(i)) {
3871 col_type column = cellColumn(i);
3872 int len = clen[column];
3873 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3874 len += clen[column] + 4;
3875 os << docstring(len, ch);
3876 if (bottomLine(i)) {
3890 void Tabular::plaintextPrintCell(odocstringstream & os,
3891 OutputParams const & runparams,
3892 idx_type cell, row_type row, col_type column,
3893 vector<unsigned int> const & clen,
3894 bool onlydata, size_t max_length) const
3896 odocstringstream sstr;
3897 cellInset(cell)->plaintext(sstr, runparams, max_length);
3909 unsigned int len1 = sstr.str().length();
3910 unsigned int len2 = clen[column];
3911 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3912 len2 += clen[column] + 4;
3915 switch (getAlignment(cell)) {
3917 case LYX_ALIGN_LEFT:
3920 case LYX_ALIGN_RIGHT:
3924 case LYX_ALIGN_CENTER:
3930 os << docstring(len1, ' ') << sstr.str()
3931 << docstring(len2, ' ');
3933 if (rightLine(cell))
3940 void Tabular::plaintext(odocstringstream & os,
3941 OutputParams const & runparams, int const depth,
3942 bool onlydata, char_type delim, size_t max_length) const
3944 // first calculate the width of the single columns
3945 vector<unsigned int> clen(ncols());
3948 // first all non multicolumn cells!
3949 for (col_type c = 0; c < ncols(); ++c) {
3951 for (row_type r = 0; r < nrows(); ++r) {
3952 idx_type cell = cellIndex(r, c);
3953 if (isMultiColumn(cell))
3955 odocstringstream sstr;
3956 cellInset(cell)->plaintext(sstr, runparams, max_length);
3957 if (clen[c] < sstr.str().length())
3958 clen[c] = sstr.str().length();
3961 // then all multicolumn cells!
3962 for (col_type c = 0; c < ncols(); ++c) {
3963 for (row_type r = 0; r < nrows(); ++r) {
3964 idx_type cell = cellIndex(r, c);
3965 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3967 odocstringstream sstr;
3968 cellInset(cell)->plaintext(sstr, runparams, max_length);
3969 int len = int(sstr.str().length());
3970 idx_type const n = columnSpan(cell);
3971 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3973 if (len > int(clen[c + n - 1]))
3974 clen[c + n - 1] = len;
3979 for (row_type r = 0; r < nrows(); ++r) {
3980 if (!onlydata && plaintextTopHLine(os, r, clen))
3981 os << docstring(depth * 2, ' ');
3982 for (col_type c = 0; c < ncols(); ++c) {
3983 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3985 if (onlydata && c > 0)
3986 // we don't use operator<< for single UCS4 character.
3987 // see explanation in docstream.h
3989 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3991 if (os.str().size() > max_length)
3996 os << docstring(depth * 2, ' ');
3997 if (plaintextBottomHLine(os, r, clen))
3998 os << docstring(depth * 2, ' ');
4000 if (os.str().size() > max_length)
4006 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4008 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4012 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4014 return cell_info[row][column].inset;
4018 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4020 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4024 void Tabular::setCellInset(row_type row, col_type column,
4025 shared_ptr<InsetTableCell> ins)
4027 CellData & cd = cell_info[row][column];
4032 void Tabular::validate(LaTeXFeatures & features) const
4034 features.require("NeedTabularnewline");
4036 features.require("booktabs");
4037 if (is_long_tabular && !hasVarwidthColumn()) {
4038 if (tabular_width.zero())
4039 features.require("longtable");
4041 features.require("xltabular");
4043 if (rotate && is_long_tabular)
4044 features.require("lscape");
4046 features.require("rotating");
4047 if (hasVarwidthColumn()) {
4048 if (is_long_tabular)
4049 features.require("xltabular");
4051 features.require("tabularx");
4053 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4054 if (isMultiRow(cell))
4055 features.require("multirow");
4056 if (getUsebox(cell) == BOX_VARWIDTH)
4057 features.require("varwidth");
4058 if (getVAlignment(cell) != LYX_VALIGN_TOP
4059 || !getPWidth(cell).zero()
4060 || isVTypeColumn(cellColumn(cell)))
4061 features.require("array");
4062 // Tell footnote that we need a savenote
4063 // environment in non-long tables or
4064 // longtable headers/footers
4065 else if (!is_long_tabular && !features.inFloat())
4066 features.saveNoteEnv("tabular");
4067 else if (!isValidRow(cellRow(cell)))
4068 features.saveNoteEnv("longtable");
4070 cellInset(cell)->validate(features);
4071 features.saveNoteEnv(string());
4076 Tabular::BoxType Tabular::useBox(idx_type cell) const
4078 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4079 if (parlist.size() > 1)
4080 return BOX_VARWIDTH;
4082 ParagraphList::const_iterator cit = parlist.begin();
4083 ParagraphList::const_iterator end = parlist.end();
4085 for (; cit != end; ++cit)
4086 for (int i = 0; i < cit->size(); ++i)
4087 if (cit->isNewline(i) || cit->layout().isEnvironment())
4088 return BOX_VARWIDTH;
4094 /////////////////////////////////////////////////////////////////////
4098 /////////////////////////////////////////////////////////////////////
4100 InsetTableCell::InsetTableCell(Buffer * buf)
4101 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
4102 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4106 bool InsetTableCell::forcePlainLayout(idx_type) const
4108 return isMultiRow || (isMultiColumn && !isFixedWidth);
4112 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4114 return isFixedWidth;
4118 bool InsetTableCell::forceLocalFontSwitch() const
4124 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4125 FuncStatus & status) const
4127 bool enabled = true;
4128 switch (cmd.action()) {
4129 case LFUN_INSET_DISSOLVE:
4132 case LFUN_MATH_DISPLAY:
4133 if (!hasFixedWidth()) {
4138 return InsetText::getStatus(cur, cmd, status);
4140 status.setEnabled(enabled);
4144 docstring InsetTableCell::asString(bool intoInsets)
4147 if (paragraphs().empty())
4149 ParagraphList::const_iterator it = paragraphs().begin();
4150 ParagraphList::const_iterator en = paragraphs().end();
4152 for (; it != en; ++it) {
4157 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4163 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4164 UpdateType utype, TocBackend & backend) const
4166 InsetText::iterateForToc(di, output_active, utype, backend);
4170 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4173 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4174 return InsetText::xhtml(xs, rp);
4178 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4180 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4181 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4183 // Hand font through to contained lyxtext:
4184 tm.font_.fontInfo() = mi.base.font;
4185 mi.base.textwidth -= horiz_offset;
4187 // This can happen when a layout has a left and right margin,
4188 // and the view is made very narrow. We can't do better than
4189 // to draw it partly out of view (bug 5890).
4190 if (mi.base.textwidth < 1)
4191 mi.base.textwidth = 1;
4193 // We tell metrics here not to expand on multiple pars
4194 // This is the difference to InsetText::Metrics
4195 if (hasFixedWidth())
4196 tm.metrics(mi, dim, mi.base.textwidth, false);
4198 tm.metrics(mi, dim, 0, false);
4199 mi.base.textwidth += horiz_offset;
4200 dim.asc += topOffset(mi.base.bv);
4201 dim.des += bottomOffset(mi.base.bv);
4202 dim.wid += horiz_offset;
4206 /////////////////////////////////////////////////////////////////////
4210 /////////////////////////////////////////////////////////////////////
4212 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4214 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4215 rowselect_(false), colselect_(false)
4220 InsetTabular::InsetTabular(InsetTabular const & tab)
4221 : Inset(tab), tabular(tab.tabular),
4222 rowselect_(false), colselect_(false)
4227 InsetTabular::~InsetTabular()
4229 hideDialogs("tabular", this);
4233 void InsetTabular::setBuffer(Buffer & buf)
4235 tabular.setBuffer(buf);
4236 Inset::setBuffer(buf);
4240 bool InsetTabular::insetAllowed(InsetCode code) const
4245 case MATHMACRO_CODE:
4250 return tabular.is_long_tabular;
4258 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4260 return tabular.is_long_tabular &&
4261 (newtype == "Standard" || newtype == "Unnumbered");
4265 void InsetTabular::write(ostream & os) const
4267 os << "Tabular" << endl;
4272 string InsetTabular::contextMenu(BufferView const &, int, int) const
4274 // FIXME: depending on the selection state,
4275 // we could offer a different menu.
4276 return cell(0)->contextMenuName() + ";" + contextMenuName();
4280 string InsetTabular::contextMenuName() const
4282 return "context-tabular";
4286 void InsetTabular::read(Lexer & lex)
4288 //bool const old_format = (lex.getString() == "\\LyXTable");
4296 string token = lex.getString();
4297 while (lex && token != "\\end_inset") {
4299 token = lex.getString();
4302 lex.printError("Missing \\end_inset at this point. ");
4306 int InsetTabular::rowFromY(Cursor & cur, int y) const
4308 // top y coordinate of tabular
4309 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4311 for (; r < tabular.nrows() && y > h; ++r)
4312 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4313 + tabular.interRowSpace(r);
4319 int InsetTabular::columnFromX(Cursor & cur, int x) const
4321 // left x coordinate of tabular
4322 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4324 for (; c < tabular.ncols() && x > w; ++c)
4325 w += tabular.cellWidth(c);
4330 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4332 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4333 // mi.base.textwidth << "\n";
4334 LBUFERR(mi.base.bv);
4336 for (row_type r = 0; r < tabular.nrows(); ++r) {
4339 for (col_type c = 0; c < tabular.ncols(); ++c) {
4340 if (tabular.isPartOfMultiColumn(r, c)
4341 || tabular.isPartOfMultiRow(r, c))
4342 // multicolumn or multirow cell, but not first one
4344 idx_type const cell = tabular.cellIndex(r, c);
4347 Length const p_width = tabular.getPWidth(cell);
4348 if (!p_width.zero())
4349 m.base.textwidth = mi.base.inPixels(p_width);
4350 else if (tabular.column_info[c].varwidth)
4351 m.base.textwidth = tabular.column_info[c].width;
4352 tabular.cellInset(cell)->metrics(m, dim0);
4353 if (!p_width.zero() || tabular.column_info[c].varwidth)
4354 dim0.wid = m.base.textwidth;
4355 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4356 + tabular.interColumnSpace(cell);
4358 // FIXME(?): do we need a second metrics call?
4359 TextMetrics const & tm =
4360 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4362 // determine horizontal offset because of decimal align (if necessary)
4363 int decimal_width = 0;
4364 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4365 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4366 tail.setBuffer(tabular.buffer());
4367 // we need to set macrocontext position everywhere
4368 // otherwise we crash with nested insets (e.g. footnotes)
4369 // after decimal point
4370 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4372 dit.push_back(CursorSlice(tail));
4373 tail.setMacrocontextPositionRecursive(dit);
4375 // remove text leading decimal point
4376 docstring const align_d = tabular.column_info[c].decimal_point;
4377 dit = separatorPos(&tail, align_d);
4379 pos_type const psize = tail.paragraphs().front().size();
4381 tail.paragraphs().front().eraseChars(0,
4382 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4384 tail.metrics(m, dim1);
4385 decimal_width = dim1.width();
4388 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4389 tabular.cell_info[r][c].decimal_width = decimal_width;
4391 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4392 // = descent of text in last par + bottomOffset:
4393 int const lastpardes = tm.last().second->descent()
4394 + bottomOffset(mi.base.bv);
4396 switch (tabular.getVAlignment(cell)) {
4397 case Tabular::LYX_VALIGN_TOP:
4399 case Tabular::LYX_VALIGN_MIDDLE:
4400 offset = -(dim0.des - lastpardes)/2;
4402 case Tabular::LYX_VALIGN_BOTTOM:
4403 offset = -(dim0.des - lastpardes);
4406 tabular.cell_info[r][c].voffset = offset;
4407 maxasc = max(maxasc, dim0.asc - offset);
4408 maxdes = max(maxdes, dim0.des + offset);
4410 int const top_space = tabular.row_info[r].top_space_default ?
4411 default_line_space :
4412 mi.base.inPixels(tabular.row_info[r].top_space);
4413 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4414 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4415 default_line_space :
4416 mi.base.inPixels(tabular.row_info[r].bottom_space);
4417 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4420 // We need to recalculate the metrics after column width calculation
4421 // with xtabular (possibly multiple times, so the call is recursive).
4422 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4424 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4425 dim.des = tabular.height() - dim.asc;
4426 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4430 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4432 if (&cur.inset() == this && cur.selection()) {
4433 if (cur.selIsMultiCell()) {
4436 getSelection(cur, rs, re, cs, ce);
4438 idx_type const cell = tabular.cellIndex(row, col);
4439 col_type const cspan = tabular.columnSpan(cell);
4440 row_type const rspan = tabular.rowSpan(cell);
4441 if (col + cspan - 1 >= cs && col <= ce
4442 && row + rspan - 1 >= rs && row <= re)
4445 if (col == tabular.cellColumn(cur.idx())
4446 && row == tabular.cellRow(cur.idx())) {
4447 CursorSlice const & beg = cur.selBegin();
4448 CursorSlice const & end = cur.selEnd();
4450 if ((end.lastpos() > 0 || end.lastpit() > 0)
4451 && end.pos() == end.lastpos() && beg.pos() == 0
4452 && end.pit() == end.lastpit() && beg.pit() == 0)
4460 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4462 x += ADD_TO_TABULAR_WIDTH;
4464 BufferView * bv = pi.base.bv;
4465 Cursor & cur = pi.base.bv->cursor();
4467 // FIXME: As the full background is painted in drawBackground(),
4468 // we have no choice but to do a full repaint for the Text cells.
4469 pi.full_repaint = true;
4471 bool const original_selection_state = pi.selected;
4475 // Save tabular change status
4476 Change tab_change = pi.change;
4478 int yy = y + tabular.offsetVAlignment();
4479 for (row_type r = 0; r < tabular.nrows(); ++r) {
4481 for (col_type c = 0; c < tabular.ncols(); ++c) {
4482 if (tabular.isPartOfMultiColumn(r, c))
4485 idx = tabular.cellIndex(r, c);
4487 if (tabular.isPartOfMultiRow(r, c)) {
4488 nx += tabular.cellWidth(idx);
4492 pi.selected |= isCellSelected(cur, r, c);
4494 // Mark deleted rows/columns
4495 if (tabular.column_info[c].change.changed())
4496 pi.change = tabular.column_info[c].change;
4497 else if (tabular.row_info[r].change.changed())
4498 pi.change = tabular.row_info[r].change;
4500 pi.change = tab_change;
4502 int const cx = nx + tabular.textHOffset(idx);
4503 int const cy = yy + tabular.textVOffset(idx);
4504 // Cache the Inset position.
4505 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4506 cell(idx)->draw(pi, cx, cy);
4507 drawCellLines(pi, nx, yy, r, idx);
4508 nx += tabular.cellWidth(idx);
4509 pi.selected = original_selection_state;
4512 if (r + 1 < tabular.nrows())
4513 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4514 + tabular.interRowSpace(r + 1);
4519 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4521 x += ADD_TO_TABULAR_WIDTH;
4522 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4523 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4524 pi.backgroundColor(this));
4528 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4530 Cursor & cur = pi.base.bv->cursor();
4532 x += ADD_TO_TABULAR_WIDTH;
4534 if (!cur.selection())
4536 if (&cur.inset() != this)
4541 bool const full_cell_selected = isCellSelected(cur,
4542 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4544 if (cur.selIsMultiCell() || full_cell_selected) {
4545 for (row_type r = 0; r < tabular.nrows(); ++r) {
4547 for (col_type c = 0; c < tabular.ncols(); ++c) {
4548 if (tabular.isPartOfMultiColumn(r, c))
4551 idx_type const cell = tabular.cellIndex(r, c);
4553 if (tabular.isPartOfMultiRow(r, c)) {
4554 xx += tabular.cellWidth(cell);
4557 int const w = tabular.cellWidth(cell);
4558 int const h = tabular.cellHeight(cell);
4559 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4560 if (isCellSelected(cur, r, c))
4561 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4564 if (r + 1 < tabular.nrows())
4565 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4566 + tabular.interRowSpace(r + 1);
4570 // FIXME: This code has no effect because InsetTableCell does not handle
4571 // drawSelection other than the trivial implementation in Inset.
4573 // x += cellXPos(cur.idx());
4574 // x += tabular.textHOffset(cur.idx());
4575 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4582 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4583 Color const incol, bool drawline, bool heavy = false)
4585 Color const col = drawline ? incol : Color_tabularonoffline;
4586 if (drawline && lt > 0)
4587 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4588 Painter::line_onoffdash,
4589 Painter::thin_line);
4590 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4591 drawline ? Painter::line_solid : Painter::line_onoffdash,
4592 (heavy ? 2 : 1) * Painter::thin_line);
4593 if (drawline && rt > 0)
4594 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4595 Painter::line_onoffdash,
4596 Painter::thin_line);
4602 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4603 row_type row, idx_type cell) const
4605 y -= tabular.rowAscent(row);
4606 int const w = tabular.cellWidth(cell);
4607 int const h = tabular.cellHeight(cell);
4611 col_type const col = tabular.cellColumn(cell);
4613 // Colour the frame if rows/columns are added or deleted
4614 Color colour = Color_tabularline;
4615 if (tabular.column_info[col].change.changed()
4616 || tabular.row_info[row].change.changed())
4617 colour = InsetTableCell(*tabular.cellInset(cell)).paragraphs().front().lookupChange(0).color();
4620 bool drawline = tabular.topLine(cell)
4621 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4622 bool heavy = tabular.use_booktabs
4623 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4624 && tabular.rowTopLine(row);
4625 if (tabular.topLineTrim(cell).first
4626 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4628 if (tabular.topLineTrim(cell).second
4629 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4631 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4635 drawline = tabular.bottomLine(cell);
4636 row_type const lastrow = tabular.nrows() - 1;
4637 // Consider multi-rows
4639 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4640 && tabular.isPartOfMultiRow(r + 1, col))
4642 heavy = tabular.use_booktabs
4643 && ((row == lastrow && tabular.rowBottomLine(row))
4644 || (r == lastrow && tabular.rowBottomLine(r)));
4645 if (tabular.bottomLineTrim(cell).first)
4647 if (tabular.bottomLineTrim(cell).second)
4649 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4652 drawline = tabular.leftLine(cell)
4653 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4654 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4657 x -= tabular.interColumnSpace(cell);
4658 col_type next_cell_col = col + 1;
4659 while (next_cell_col < tabular.ncols()
4660 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4662 drawline = tabular.rightLine(cell)
4663 || (next_cell_col < tabular.ncols()
4664 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4665 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4669 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4671 //lyxerr << "InsetTabular::edit: " << this << endl;
4675 if (isRightToLeft(cur))
4676 cur.idx() = tabular.getLastCellInRow(0);
4682 if (isRightToLeft(cur))
4683 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4685 cur.idx() = tabular.numberofcells - 1;
4687 cur.pos() = cur.lastpos(); // FIXME crude guess
4689 cur.setCurrentFont();
4690 // FIXME: this accesses the position cache before it is initialized
4691 //cur.bv().fitCursor();
4695 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4697 // In a longtable, tell captions what the current float is
4698 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4699 string const saveflt = cnts.current_float();
4700 if (tabular.is_long_tabular) {
4701 cnts.current_float("table");
4702 // in longtables, we only step the counter once
4703 cnts.step(from_ascii("table"), utype);
4704 cnts.isLongtable(true);
4707 ParIterator it2 = it;
4709 size_t const end = it2.nargs();
4710 for ( ; it2.idx() < end; it2.top().forwardIdx())
4711 buffer().updateBuffer(it2, utype);
4714 if (tabular.is_long_tabular) {
4715 cnts.current_float(saveflt);
4716 cnts.isLongtable(false);
4721 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4722 UpdateType utype, TocBackend & backend) const
4724 DocIterator dit = cpit;
4726 size_t const end = dit.nargs();
4727 for ( ; dit.idx() < end; dit.top().forwardIdx())
4728 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4732 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4734 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4735 return x < x0 || x > x0 + tabular.width();
4739 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4741 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4742 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4743 // there is no margin added vertically to tabular insets.
4744 // However, it works for now.
4745 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4749 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4751 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4755 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4757 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4758 << "\n cur:" << cur);
4759 CursorSlice sl = cur.top();
4760 Cursor & bvcur = cur.bv().cursor();
4762 FuncCode const act = cmd.action();
4766 case LFUN_MOUSE_PRESS: {
4767 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4769 if (hitSelectRow(cur.bv(), cmd.x())) {
4770 row_type r = rowFromY(cur, cmd.y());
4771 cur.idx() = tabular.getFirstCellInRow(r);
4775 cur.idx() = tabular.getLastCellInRow(r);
4776 cur.pit() = cur.lastpit();
4777 cur.pos() = cur.lastpos();
4778 cur.selection(true);
4784 if (hitSelectColumn(cur.bv(), cmd.y())) {
4785 col_type c = columnFromX(cur, cmd.x());
4786 cur.idx() = tabular.cellIndex(0, c);
4790 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4791 cur.pit() = cur.lastpit();
4792 cur.pos() = cur.lastpos();
4793 cur.selection(true);
4798 // do not reset cursor/selection if we have selected
4799 // some cells (bug 2715).
4800 if (cmd.button() == mouse_button::button3
4801 && &bvcur.selBegin().inset() == this
4802 && bvcur.selIsMultiCell())
4805 // Let InsetTableCell do it
4806 cell(cur.idx())->dispatch(cur, cmd);
4809 case LFUN_MOUSE_MOTION:
4810 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4811 if (cmd.button() == mouse_button::button1) {
4812 // only accept motions to places not deeper nested than the real anchor
4813 if (!bvcur.realAnchor().hasPart(cur)) {
4817 // select (additional) row
4819 row_type r = rowFromY(cur, cmd.y());
4820 cur.idx() = tabular.getLastCellInRow(r);
4821 // we need to reset the cursor's pit and pos now, as the old ones
4822 // may no longer be valid.
4825 bvcur.setCursor(cur);
4826 bvcur.selection(true);
4829 // select (additional) column
4831 col_type c = columnFromX(cur, cmd.x());
4832 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4833 // we need to reset the cursor's pit and pos now, as the old ones
4834 // may no longer be valid.
4837 bvcur.setCursor(cur);
4838 bvcur.selection(true);
4841 // only update if selection changes
4842 if (bvcur.idx() == cur.idx() &&
4843 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4844 cur.noScreenUpdate();
4845 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4846 bvcur.setCursor(cur);
4847 bvcur.selection(true);
4848 // if this is a multicell selection, we just set the cursor to
4849 // the beginning of the cell's text.
4850 if (bvcur.selIsMultiCell()) {
4851 bvcur.pit() = bvcur.lastpit();
4852 bvcur.pos() = bvcur.lastpos();
4857 case LFUN_MOUSE_RELEASE:
4862 case LFUN_CELL_BACKWARD:
4864 cur.selection(false);
4867 case LFUN_CELL_FORWARD:
4869 cur.selection(false);
4872 case LFUN_CHAR_FORWARD_SELECT:
4873 case LFUN_CHAR_FORWARD:
4874 case LFUN_CHAR_BACKWARD_SELECT:
4875 case LFUN_CHAR_BACKWARD:
4876 case LFUN_CHAR_RIGHT_SELECT:
4877 case LFUN_CHAR_RIGHT:
4878 case LFUN_CHAR_LEFT_SELECT:
4879 case LFUN_CHAR_LEFT:
4880 case LFUN_WORD_FORWARD:
4881 case LFUN_WORD_FORWARD_SELECT:
4882 case LFUN_WORD_BACKWARD:
4883 case LFUN_WORD_BACKWARD_SELECT:
4884 case LFUN_WORD_RIGHT:
4885 case LFUN_WORD_RIGHT_SELECT:
4886 case LFUN_WORD_LEFT:
4887 case LFUN_WORD_LEFT_SELECT: {
4888 // determine whether we move to next or previous cell, where to enter
4889 // the new cell from, and which command to "finish" (i.e., exit the
4892 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4893 FuncCode finish_lfun;
4895 if (act == LFUN_CHAR_FORWARD
4896 || act == LFUN_CHAR_FORWARD_SELECT
4897 || act == LFUN_WORD_FORWARD
4898 || act == LFUN_WORD_FORWARD_SELECT) {
4900 finish_lfun = LFUN_FINISHED_FORWARD;
4902 else if (act == LFUN_CHAR_BACKWARD
4903 || act == LFUN_CHAR_BACKWARD_SELECT
4904 || act == LFUN_WORD_BACKWARD
4905 || act == LFUN_WORD_BACKWARD_SELECT) {
4907 finish_lfun = LFUN_FINISHED_BACKWARD;
4909 // LEFT or RIGHT commands --- the interpretation will depend on the
4910 // table's direction.
4912 bool const right = act == LFUN_CHAR_RIGHT
4913 || act == LFUN_CHAR_RIGHT_SELECT
4914 || act == LFUN_WORD_RIGHT
4915 || act == LFUN_WORD_RIGHT_SELECT;
4916 next_cell = isRightToLeft(cur) != right;
4918 if (lyxrc.visual_cursor)
4919 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4921 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4924 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4925 || act == LFUN_CHAR_BACKWARD_SELECT
4926 || act == LFUN_CHAR_RIGHT_SELECT
4927 || act == LFUN_CHAR_LEFT_SELECT
4928 || act == LFUN_WORD_FORWARD_SELECT
4929 || act == LFUN_WORD_RIGHT_SELECT
4930 || act == LFUN_WORD_BACKWARD_SELECT
4931 || act == LFUN_WORD_LEFT_SELECT;
4933 // If we have a multicell selection or we're
4934 // not doing some LFUN_*_SELECT thing anyway...
4935 if (!cur.selIsMultiCell() || !select) {
4936 col_type const c = tabular.cellColumn(cur.idx());
4937 row_type const r = tabular.cellRow(cur.idx());
4938 // Are we trying to select the whole cell and is the whole cell
4939 // not yet selected?
4940 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4941 ((next_cell && cur.pit() == cur.lastpit()
4942 && cur.pos() == cur.lastpos())
4943 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4945 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4947 // ...try to dispatch to the cell's inset.
4948 cell(cur.idx())->dispatch(cur, cmd);
4950 // When we already have a selection we want to select the whole cell
4951 // before going to the next cell.
4952 if (select_whole && !empty_cell){
4953 getText(cur.idx())->selectAll(cur);
4955 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4959 // FIXME: When we support the selection of an empty cell, remove
4960 // the !empty_cell from this condition. For now we jump to the next
4961 // cell if the current cell is empty.
4962 if (cur.result().dispatched() && !empty_cell)
4966 // move to next/prev cell, as appropriate
4967 // note that we will always do this if we're selecting and we have
4968 // a multicell selection
4969 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4970 << " cell from: " << int(entry_from));
4972 moveNextCell(cur, entry_from);
4974 movePrevCell(cur, entry_from);
4975 // if we're exiting the table, call the appropriate FINISHED lfun
4976 if (sl == cur.top()) {
4977 cmd = FuncRequest(finish_lfun);
4982 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4987 case LFUN_DOWN_SELECT:
4989 if (!(cur.selection() && cur.selIsMultiCell()))
4990 cell(cur.idx())->dispatch(cur, cmd);
4992 cur.dispatched(); // override the cell's decision
4993 if (sl == cur.top()) {
4994 // if our Text didn't do anything to the cursor
4995 // then we try to put the cursor into the cell below
4996 // setting also the right targetX.
4997 cur.selHandle(act == LFUN_DOWN_SELECT);
4998 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4999 int const xtarget = cur.targetX();
5000 // WARNING: Once cur.idx() has been reset, the cursor is in
5001 // an inconsistent state until pos() has been set. Be careful
5002 // what you do with it!
5003 cur.idx() = tabular.cellBelow(cur.idx());
5005 TextMetrics const & tm =
5006 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5007 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5008 cur.setCurrentFont();
5011 if (sl == cur.top()) {
5012 // we trick it to go to forward after leaving the
5014 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5017 if (cur.selIsMultiCell()) {
5018 cur.pit() = cur.lastpit();
5019 cur.pos() = cur.lastpos();
5020 cur.setCurrentFont();
5021 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5024 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5027 case LFUN_UP_SELECT:
5029 if (!(cur.selection() && cur.selIsMultiCell()))
5030 cell(cur.idx())->dispatch(cur, cmd);
5031 cur.dispatched(); // override the cell's decision
5032 if (sl == cur.top()) {
5033 // if our Text didn't do anything to the cursor
5034 // then we try to put the cursor into the cell above
5035 // setting also the right targetX.
5036 cur.selHandle(act == LFUN_UP_SELECT);
5037 if (tabular.cellRow(cur.idx()) != 0) {
5038 int const xtarget = cur.targetX();
5039 // WARNING: Once cur.idx() has been reset, the cursor is in
5040 // an inconsistent state until pos() has been set. Be careful
5041 // what you do with it!
5042 cur.idx() = tabular.cellAbove(cur.idx());
5043 cur.pit() = cur.lastpit();
5044 Text const * text = cell(cur.idx())->getText(0);
5045 TextMetrics const & tm = cur.bv().textMetrics(text);
5046 ParagraphMetrics const & pm =
5047 tm.parMetrics(cur.lastpit());
5048 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5049 cur.setCurrentFont();
5052 if (sl == cur.top()) {
5053 cmd = FuncRequest(LFUN_UP);
5056 if (cur.selIsMultiCell()) {
5058 cur.pos() = cur.lastpos();
5059 cur.setCurrentFont();
5060 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5063 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5066 case LFUN_LAYOUT_TABULAR:
5067 cur.bv().showDialog("tabular");
5070 case LFUN_INSET_MODIFY:
5071 // we come from the dialog
5072 if (cmd.getArg(0) == "tabular")
5073 tabularFeatures(cur, cmd.getLongArg(1));
5078 case LFUN_TABULAR_FEATURE:
5079 tabularFeatures(cur, to_utf8(cmd.argument()));
5082 // insert file functions
5083 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5084 case LFUN_FILE_INSERT_PLAINTEXT:
5086 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5087 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5088 FileName(to_utf8(cmd.argument())));
5091 cur.recordUndoInset();
5092 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5093 // content has been replaced,
5094 // so cursor might be invalid
5095 cur.pos() = cur.lastpos();
5096 cur.pit() = cur.lastpit();
5097 bvcur.setCursor(cur);
5104 if (cur.selIsMultiCell()) {
5105 if (copySelection(cur)) {
5106 cur.recordUndoInset();
5110 cell(cur.idx())->dispatch(cur, cmd);
5113 case LFUN_SELF_INSERT:
5114 if (cur.selIsMultiCell()) {
5115 cur.recordUndoInset();
5117 BufferView * bv = &cur.bv();
5118 docstring::const_iterator cit = cmd.argument().begin();
5119 docstring::const_iterator const end = cmd.argument().end();
5120 for (; cit != end; ++cit)
5121 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5124 bv->bookmarkEditPosition();
5126 cell(cur.idx())->dispatch(cur, cmd);
5129 case LFUN_CHAR_DELETE_BACKWARD:
5130 case LFUN_CHAR_DELETE_FORWARD:
5131 if (cur.selIsMultiCell()) {
5132 cur.recordUndoInset();
5135 cell(cur.idx())->dispatch(cur, cmd);
5139 if (!cur.selection())
5141 if (cur.selIsMultiCell()) {
5145 cell(cur.idx())->dispatch(cur, cmd);
5148 case LFUN_CLIPBOARD_PASTE:
5149 case LFUN_PRIMARY_SELECTION_PASTE: {
5150 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5151 theClipboard().getAsText(Clipboard::PlainTextType) :
5152 theSelection().get();
5155 // pass to InsertPlaintextString, but
5156 // only if we have multi-cell content
5157 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5158 cur.recordUndoInset();
5159 if (insertPlaintextString(cur.bv(), clip, false)) {
5160 // content has been replaced,
5161 // so cursor might be invalid
5162 cur.pos() = cur.lastpos();
5163 cur.pit() = cur.lastpit();
5164 bvcur.setCursor(cur);
5168 // Let the cell handle normal text
5169 cell(cur.idx())->dispatch(cur, cmd);
5174 if (!tabularStackDirty()) {
5175 // Check if we have plain text or HTML with rows/columns.
5176 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5177 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5178 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5179 docstring const clip =
5180 theClipboard().getAsText(Clipboard::AnyTextType);
5181 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5182 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5183 doDispatch(cur, ncmd);
5187 if (!cur.selIsMultiCell())
5188 cell(cur.idx())->dispatch(cur, cmd);
5191 if (theClipboard().isInternal()) {
5192 cur.recordUndoInset();
5193 pasteClipboard(cur);
5197 case LFUN_CHANGE_ACCEPT:
5198 case LFUN_CHANGE_REJECT:
5199 case LFUN_FONT_EMPH:
5200 case LFUN_FONT_BOLD:
5201 case LFUN_FONT_BOLDSYMBOL:
5202 case LFUN_FONT_ROMAN:
5203 case LFUN_FONT_NOUN:
5204 case LFUN_FONT_ITAL:
5205 case LFUN_FONT_FRAK:
5206 case LFUN_FONT_TYPEWRITER:
5207 case LFUN_FONT_SANS:
5208 case LFUN_TEXTSTYLE_APPLY:
5209 case LFUN_TEXTSTYLE_UPDATE:
5210 case LFUN_FONT_SIZE:
5211 case LFUN_FONT_UNDERLINE:
5212 case LFUN_FONT_STRIKEOUT:
5213 case LFUN_FONT_CROSSOUT:
5214 case LFUN_FONT_UNDERUNDERLINE:
5215 case LFUN_FONT_UNDERWAVE:
5217 case LFUN_PARAGRAPH_PARAMS_APPLY:
5218 case LFUN_PARAGRAPH_PARAMS:
5219 case LFUN_WORD_CAPITALIZE:
5220 case LFUN_WORD_UPCASE:
5221 case LFUN_WORD_LOWCASE:
5222 case LFUN_CHARS_TRANSPOSE: {
5223 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5224 if (cur.selIsMultiCell()) {
5227 getSelection(cur, rs, re, cs, ce);
5228 Cursor tmpcur = cur;
5229 for (row_type r = rs; r <= re; ++r) {
5230 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5231 // whole row selected
5232 if (act == LFUN_CHANGE_ACCEPT) {
5233 if (tabular.row_info[r].change.inserted())
5234 tabular.row_info[r].change.setUnchanged();
5235 else if (tabular.row_info[r].change.deleted()) {
5236 tabular.deleteRow(r, true);
5241 if (tabular.row_info[r].change.deleted())
5242 tabular.row_info[r].change.setUnchanged();
5243 else if (tabular.row_info[r].change.inserted()) {
5244 tabular.deleteRow(r, true);
5250 for (col_type c = cs; c <= ce; ++c) {
5251 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5252 // whole col selected
5253 if (act == LFUN_CHANGE_ACCEPT) {
5254 if (tabular.column_info[c].change.inserted())
5255 tabular.column_info[c].change.setUnchanged();
5256 else if (tabular.column_info[c].change.deleted()) {
5257 tabular.deleteColumn(c, true);
5262 if (tabular.column_info[c].change.deleted())
5263 tabular.column_info[c].change.setUnchanged();
5264 else if (tabular.column_info[c].change.inserted()) {
5265 tabular.deleteColumn(c, true);
5271 // cursor follows cell:
5272 tmpcur.idx() = tabular.cellIndex(r, c);
5273 // select this cell only:
5276 tmpcur.resetAnchor();
5277 tmpcur.pit() = tmpcur.lastpit();
5278 tmpcur.pos() = tmpcur.top().lastpos();
5279 tmpcur.setCursor(tmpcur);
5280 tmpcur.setSelection();
5281 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5285 tabular.updateIndexes();
5286 // cursor might be invalid
5288 // change bar might need to be redrawn
5289 cur.screenUpdateFlags(Update::Force);
5290 cur.forceBufferUpdate();
5294 cell(cur.idx())->dispatch(cur, cmd);
5299 case LFUN_CHANGE_NEXT:
5300 case LFUN_CHANGE_PREVIOUS: {
5301 // BufferView::dispatch has already moved the cursor, we just
5302 // need to select here if we have a changed row or column
5303 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5305 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5309 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5310 cur.pit() = cur.lastpit();
5311 cur.pos() = cur.lastpos();
5312 cur.selection(true);
5316 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5318 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5322 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5323 cur.pit() = cur.lastpit();
5324 cur.pos() = cur.lastpos();
5325 cur.selection(true);
5332 case LFUN_INSET_SETTINGS:
5333 // relay this lfun to Inset, not to the cell.
5334 Inset::doDispatch(cur, cmd);
5338 // we try to handle this event in the insets dispatch function.
5339 cell(cur.idx())->dispatch(cur, cmd);
5345 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5346 string const & argument, FuncStatus & status) const
5349 int action = Tabular::LAST_ACTION;
5351 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5352 if (tabularFeature[i].feature == s) {
5353 action = tabularFeature[i].action;
5357 if (action == Tabular::LAST_ACTION) {
5359 status.setUnknown(true);
5363 row_type sel_row_start = 0;
5364 row_type sel_row_end = 0;
5365 col_type sel_col_start = 0;
5366 col_type sel_col_end = 0;
5367 Tabular::ltType dummyltt;
5370 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5373 case Tabular::SET_PWIDTH:
5374 case Tabular::SET_MPWIDTH:
5375 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5376 case Tabular::SET_SPECIAL_COLUMN:
5377 case Tabular::SET_SPECIAL_MULTICOLUMN:
5378 case Tabular::APPEND_ROW:
5379 case Tabular::APPEND_COLUMN:
5380 case Tabular::DELETE_ROW:
5381 case Tabular::DELETE_COLUMN:
5382 case Tabular::COPY_ROW:
5383 case Tabular::COPY_COLUMN:
5384 case Tabular::SET_TOP_SPACE:
5385 case Tabular::SET_BOTTOM_SPACE:
5386 case Tabular::SET_INTERLINE_SPACE:
5390 case Tabular::SET_TABULAR_WIDTH:
5391 status.setEnabled(!tabular.rotate
5392 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5395 case Tabular::MOVE_COLUMN_RIGHT:
5396 case Tabular::MOVE_COLUMN_LEFT:
5397 case Tabular::MOVE_ROW_DOWN:
5398 case Tabular::MOVE_ROW_UP: {
5399 if (cur.selection()) {
5400 status.message(_("Selections not supported."));
5401 status.setEnabled(false);
5405 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
5406 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
5407 (action == Tabular::MOVE_COLUMN_LEFT &&
5408 tabular.cellColumn(cur.idx()) == 0) ||
5409 (action == Tabular::MOVE_ROW_DOWN &&
5410 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
5411 (action == Tabular::MOVE_ROW_UP &&
5412 tabular.cellRow(cur.idx()) == 0)) {
5413 status.setEnabled(false);
5417 if (action == Tabular::MOVE_COLUMN_RIGHT ||
5418 action == Tabular::MOVE_COLUMN_LEFT) {
5419 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
5420 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
5421 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
5422 status.message(_("Multi-column in current or"
5423 " destination column."));
5424 status.setEnabled(false);
5429 if (action == Tabular::MOVE_ROW_DOWN ||
5430 action == Tabular::MOVE_ROW_UP) {
5431 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
5432 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
5433 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
5434 status.message(_("Multi-row in current or"
5435 " destination row."));
5436 status.setEnabled(false);
5441 status.setEnabled(true);
5445 case Tabular::SET_DECIMAL_POINT:
5447 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5450 case Tabular::SET_MULTICOLUMN:
5451 case Tabular::UNSET_MULTICOLUMN:
5452 case Tabular::MULTICOLUMN:
5453 // If a row is set as longtable caption, it must not be allowed
5454 // to unset that this row is a multicolumn.
5455 status.setEnabled(sel_row_start == sel_row_end
5456 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5457 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5460 case Tabular::SET_MULTIROW:
5461 case Tabular::UNSET_MULTIROW:
5462 case Tabular::MULTIROW:
5463 // If a row is set as longtable caption, it must not be allowed
5464 // to unset that this row is a multirow.
5465 status.setEnabled(sel_col_start == sel_col_end
5466 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5467 status.setOnOff(tabular.isMultiRow(cur.idx()));
5470 case Tabular::SET_ALL_LINES:
5471 case Tabular::UNSET_ALL_LINES:
5472 case Tabular::SET_INNER_LINES:
5473 case Tabular::SET_BORDER_LINES:
5474 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5477 case Tabular::RESET_FORMAL_DEFAULT:
5478 status.setEnabled(tabular.use_booktabs);
5481 case Tabular::SET_LINE_TOP:
5482 case Tabular::SET_LINE_BOTTOM:
5483 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5486 case Tabular::SET_LINE_LEFT:
5487 case Tabular::SET_LINE_RIGHT:
5488 status.setEnabled(!tabular.use_booktabs
5489 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5492 case Tabular::SET_LTRIM_TOP:
5493 case Tabular::SET_RTRIM_TOP:
5494 status.setEnabled(tabular.use_booktabs
5495 && tabular.cellRow(cur.idx()) != 0
5496 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5499 case Tabular::SET_LTRIM_BOTTOM:
5500 case Tabular::SET_RTRIM_BOTTOM:
5501 status.setEnabled(tabular.use_booktabs
5502 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5503 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5506 case Tabular::TOGGLE_LINE_TOP:
5507 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5508 status.setOnOff(tabular.topLine(cur.idx()));
5511 case Tabular::TOGGLE_LINE_BOTTOM:
5512 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5513 status.setOnOff(tabular.bottomLine(cur.idx()));
5516 case Tabular::TOGGLE_LINE_LEFT:
5517 status.setEnabled(!tabular.use_booktabs
5518 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5519 status.setOnOff(tabular.leftLine(cur.idx()));
5522 case Tabular::TOGGLE_LINE_RIGHT:
5523 status.setEnabled(!tabular.use_booktabs
5524 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5525 status.setOnOff(tabular.rightLine(cur.idx()));
5528 case Tabular::TOGGLE_LTRIM_TOP:
5529 status.setEnabled(tabular.use_booktabs
5530 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5531 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5534 case Tabular::TOGGLE_RTRIM_TOP:
5535 status.setEnabled(tabular.use_booktabs
5536 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5537 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5540 case Tabular::TOGGLE_LTRIM_BOTTOM:
5541 status.setEnabled(tabular.use_booktabs
5542 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5543 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5546 case Tabular::TOGGLE_RTRIM_BOTTOM:
5547 status.setEnabled(tabular.use_booktabs
5548 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5549 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5552 // multirow cells only inherit the alignment of the column if the column has
5553 // no width, otherwise they are left-aligned
5554 // therefore allow always left but right and center only if there is no width
5555 case Tabular::M_ALIGN_LEFT:
5558 case Tabular::ALIGN_LEFT:
5559 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5562 case Tabular::M_ALIGN_RIGHT:
5565 case Tabular::ALIGN_RIGHT:
5566 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5567 && !tabular.getPWidth(cur.idx()).zero()));
5568 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5571 case Tabular::M_ALIGN_CENTER:
5574 case Tabular::ALIGN_CENTER:
5575 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5576 && !tabular.getPWidth(cur.idx()).zero()));
5577 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5580 case Tabular::ALIGN_BLOCK:
5581 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5582 && !tabular.isMultiRow(cur.idx()));
5583 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5586 case Tabular::ALIGN_DECIMAL:
5587 status.setEnabled(!tabular.isMultiRow(cur.idx())
5588 && !tabular.isMultiColumn(cur.idx()));
5589 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5592 case Tabular::M_VALIGN_TOP:
5595 case Tabular::VALIGN_TOP:
5596 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5597 && !tabular.isMultiRow(cur.idx()));
5599 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5602 case Tabular::M_VALIGN_BOTTOM:
5605 case Tabular::VALIGN_BOTTOM:
5606 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5607 && !tabular.isMultiRow(cur.idx()));
5609 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5612 case Tabular::M_VALIGN_MIDDLE:
5615 case Tabular::VALIGN_MIDDLE:
5616 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5617 && !tabular.isMultiRow(cur.idx()));
5619 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5622 case Tabular::SET_LONGTABULAR:
5623 case Tabular::TOGGLE_LONGTABULAR:
5624 // setting as longtable is not allowed when table is inside a float
5625 if (cur.innerInsetOfType(FLOAT_CODE) != 0
5626 || cur.innerInsetOfType(WRAP_CODE) != 0)
5627 status.setEnabled(false);
5629 status.setEnabled(true);
5630 status.setOnOff(tabular.is_long_tabular);
5633 case Tabular::UNSET_LONGTABULAR:
5634 status.setOnOff(!tabular.is_long_tabular);
5637 case Tabular::TOGGLE_ROTATE_TABULAR:
5638 case Tabular::SET_ROTATE_TABULAR:
5639 status.setOnOff(tabular.rotate != 0);
5642 case Tabular::TABULAR_VALIGN_TOP:
5643 status.setEnabled(tabular.tabular_width.zero());
5644 status.setOnOff(tabular.tabular_valignment
5645 == Tabular::LYX_VALIGN_TOP);
5647 case Tabular::TABULAR_VALIGN_MIDDLE:
5648 status.setEnabled(tabular.tabular_width.zero());
5649 status.setOnOff(tabular.tabular_valignment
5650 == Tabular::LYX_VALIGN_MIDDLE);
5652 case Tabular::TABULAR_VALIGN_BOTTOM:
5653 status.setEnabled(tabular.tabular_width.zero());
5654 status.setOnOff(tabular.tabular_valignment
5655 == Tabular::LYX_VALIGN_BOTTOM);
5658 case Tabular::LONGTABULAR_ALIGN_LEFT:
5659 status.setOnOff(tabular.longtabular_alignment
5660 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5662 case Tabular::LONGTABULAR_ALIGN_CENTER:
5663 status.setOnOff(tabular.longtabular_alignment
5664 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5666 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5667 status.setOnOff(tabular.longtabular_alignment
5668 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5671 case Tabular::UNSET_ROTATE_TABULAR:
5672 status.setOnOff(tabular.rotate == 0);
5675 case Tabular::TOGGLE_ROTATE_CELL:
5676 case Tabular::SET_ROTATE_CELL:
5677 status.setOnOff(!oneCellHasRotationState(false,
5678 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5681 case Tabular::UNSET_ROTATE_CELL:
5682 status.setOnOff(!oneCellHasRotationState(true,
5683 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5686 case Tabular::SET_USEBOX:
5687 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5690 // every row can only be one thing:
5691 // either a footer or header
5692 case Tabular::SET_LTFIRSTHEAD:
5693 status.setEnabled(sel_row_start == sel_row_end);
5694 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5697 case Tabular::UNSET_LTFIRSTHEAD:
5698 status.setEnabled(sel_row_start == sel_row_end);
5699 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5702 case Tabular::SET_LTHEAD:
5703 status.setEnabled(sel_row_start == sel_row_end);
5704 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5707 case Tabular::UNSET_LTHEAD:
5708 status.setEnabled(sel_row_start == sel_row_end);
5709 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5712 case Tabular::SET_LTFOOT:
5713 status.setEnabled(sel_row_start == sel_row_end);
5714 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5717 case Tabular::UNSET_LTFOOT:
5718 status.setEnabled(sel_row_start == sel_row_end);
5719 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5722 case Tabular::SET_LTLASTFOOT:
5723 status.setEnabled(sel_row_start == sel_row_end);
5724 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5727 case Tabular::UNSET_LTLASTFOOT:
5728 status.setEnabled(sel_row_start == sel_row_end);
5729 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5732 case Tabular::SET_LTNEWPAGE:
5733 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5735 case Tabular::UNSET_LTNEWPAGE:
5736 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5739 // only one row in head/firsthead/foot/lasthead can be the caption
5740 // and a multirow cannot be set as caption
5741 case Tabular::SET_LTCAPTION:
5742 status.setEnabled(sel_row_start == sel_row_end
5743 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5744 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5745 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5746 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5747 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5748 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5749 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5750 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5751 && !tabular.isMultiRow(sel_row_start));
5752 status.setOnOff(tabular.ltCaption(sel_row_start));
5755 case Tabular::UNSET_LTCAPTION:
5756 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5759 case Tabular::TOGGLE_LTCAPTION:
5760 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5761 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5762 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5763 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5764 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5765 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5766 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5767 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5768 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5769 status.setOnOff(tabular.ltCaption(sel_row_start));
5772 case Tabular::TOGGLE_BOOKTABS:
5773 case Tabular::SET_BOOKTABS:
5774 status.setOnOff(tabular.use_booktabs);
5777 case Tabular::UNSET_BOOKTABS:
5778 status.setOnOff(!tabular.use_booktabs);
5783 status.setEnabled(false);
5790 // function sets an object as defined in FuncStatus.h:
5791 // states OK, Unknown, Disabled, On, Off.
5792 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5793 FuncStatus & status) const
5795 switch (cmd.action()) {
5796 case LFUN_INSET_MODIFY:
5797 if (cmd.getArg(0) != "tabular")
5799 if (cmd.getArg(1) == "for-dialog") {
5800 // The dialog is asking the status of a command
5801 if (&cur.inset() != this)
5803 string action = cmd.getArg(2);
5804 string arg = cmd.getLongArg(3);
5805 return getFeatureStatus(cur, action, arg, status);
5807 // We always enable the lfun if it is coming from the dialog
5808 // because the dialog makes sure all the settings are valid,
5809 // even though the first argument might not be valid now.
5810 status.setEnabled(true);
5814 case LFUN_TABULAR_FEATURE: {
5815 if (&cur.inset() != this)
5817 string action = cmd.getArg(0);
5818 string arg = cmd.getLongArg(1);
5819 return getFeatureStatus(cur, action, arg, status);
5822 case LFUN_CAPTION_INSERT: {
5823 // caption is only allowed in caption cell of longtable
5824 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5825 status.setEnabled(false);
5828 // only standard caption is allowed
5829 string arg = cmd.getArg(0);
5830 if (!arg.empty() && arg != "Standard") {
5831 status.setEnabled(false);
5834 // check if there is already a caption
5835 bool have_caption = false;
5836 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5837 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5838 ParagraphList::const_iterator pend = itc.paragraphs().end();
5839 for (; pit != pend; ++pit) {
5840 InsetList::const_iterator it = pit->insetList().begin();
5841 InsetList::const_iterator end = pit->insetList().end();
5842 for (; it != end; ++it) {
5843 if (it->inset->lyxCode() == CAPTION_CODE) {
5844 have_caption = true;
5849 status.setEnabled(!have_caption);
5853 // These are only enabled inside tabular
5854 case LFUN_CELL_BACKWARD:
5855 case LFUN_CELL_FORWARD:
5856 status.setEnabled(true);
5859 // disable these with multiple cells selected
5860 case LFUN_INSET_INSERT:
5861 case LFUN_TABULAR_INSERT:
5862 case LFUN_TABULAR_STYLE_INSERT:
5863 case LFUN_FLEX_INSERT:
5864 case LFUN_FLOAT_INSERT:
5865 case LFUN_FLOAT_WIDE_INSERT:
5866 case LFUN_FOOTNOTE_INSERT:
5867 case LFUN_MARGINALNOTE_INSERT:
5868 case LFUN_MATH_INSERT:
5869 case LFUN_MATH_MODE:
5870 case LFUN_MATH_MUTATE:
5871 case LFUN_MATH_DISPLAY:
5872 case LFUN_NOTE_INSERT:
5873 case LFUN_ARGUMENT_INSERT:
5874 case LFUN_BOX_INSERT:
5875 case LFUN_BRANCH_INSERT:
5876 case LFUN_PHANTOM_INSERT:
5877 case LFUN_WRAP_INSERT:
5878 case LFUN_PREVIEW_INSERT:
5879 case LFUN_ERT_INSERT: {
5880 if (cur.selIsMultiCell()) {
5881 status.setEnabled(false);
5884 return cell(cur.idx())->getStatus(cur, cmd, status);
5887 case LFUN_CHANGE_ACCEPT:
5888 case LFUN_CHANGE_REJECT: {
5889 if (cur.selIsMultiCell()) {
5892 getSelection(cur, rs, re, cs, ce);
5893 for (row_type r = rs; r <= re; ++r) {
5894 if (tabular.row_info[r].change.changed()) {
5895 status.setEnabled(true);
5898 for (col_type c = cs; c <= ce; ++c) {
5899 if (tabular.column_info[c].change.changed()) {
5900 status.setEnabled(true);
5906 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5907 status.setEnabled(true);
5910 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5911 status.setEnabled(true);
5915 return cell(cur.idx())->getStatus(cur, cmd, status);
5918 // disable in non-fixed-width cells
5919 case LFUN_PARAGRAPH_BREAK:
5920 // multirow does not allow paragraph breaks
5921 if (tabular.isMultiRow(cur.idx())) {
5922 status.setEnabled(false);
5926 case LFUN_NEWLINE_INSERT:
5927 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
5928 && tabular.getPWidth(cur.idx()).zero()) {
5929 status.setEnabled(false);
5932 return cell(cur.idx())->getStatus(cur, cmd, status);
5934 case LFUN_NEWPAGE_INSERT:
5935 status.setEnabled(false);
5939 if (tabularStackDirty() && theClipboard().isInternal()) {
5940 if (cur.selIsMultiCell()) {
5943 getSelection(cur, rs, re, cs, ce);
5944 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5945 && paste_tabular->nrows() == re - rs + 1)
5946 status.setEnabled(true);
5948 status.setEnabled(false);
5949 status.message(_("Selection size should match clipboard content."));
5952 status.setEnabled(true);
5955 return cell(cur.idx())->getStatus(cur, cmd, status);
5957 case LFUN_INSET_SETTINGS:
5958 // relay this lfun to Inset, not to the cell.
5959 return Inset::getStatus(cur, cmd, status);
5962 // we try to handle this event in the insets dispatch function.
5963 return cell(cur.idx())->getStatus(cur, cmd, status);
5969 Inset::DisplayType InsetTabular::display() const
5971 if (tabular.is_long_tabular) {
5972 switch (tabular.longtabular_alignment) {
5973 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5975 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5977 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5987 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5989 tabular.latex(os, runparams);
5993 int InsetTabular::plaintext(odocstringstream & os,
5994 OutputParams const & runparams, size_t max_length) const
5996 os << '\n'; // output table on a new line
5997 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5998 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5999 return PLAINTEXT_NEWLINE;
6003 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
6008 // FIXME: Why not pass a proper DocIterator here?
6010 // if the table is inside a float it doesn't need the informaltable
6011 // wrapper. Search for it.
6012 for (master = owner(); master; master = master->owner())
6013 if (master->lyxCode() == FLOAT_CODE)
6018 os << "<informaltable>";
6021 ret += tabular.docbook(os, runparams);
6023 os << "</informaltable>";
6030 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6032 return tabular.xhtml(xs, rp);
6036 void InsetTabular::validate(LaTeXFeatures & features) const
6038 tabular.validate(features);
6039 features.useInsetLayout(getLayout());
6043 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6045 return tabular.cellInset(idx);
6049 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6051 return tabular.cellInset(idx);
6055 void InsetTabular::cursorPos(BufferView const & bv,
6056 CursorSlice const & sl, bool boundary, int & x, int & y) const
6058 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6060 // y offset correction
6061 y += cellYPos(sl.idx());
6062 y += tabular.textVOffset(sl.idx());
6063 y += tabular.offsetVAlignment();
6065 // x offset correction
6066 x += cellXPos(sl.idx());
6067 x += tabular.textHOffset(sl.idx());
6068 x += ADD_TO_TABULAR_WIDTH;
6072 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6076 Inset const & inset = *tabular.cellInset(cell);
6077 Point o = bv.coordCache().getInsets().xy(&inset);
6078 int const xbeg = o.x_ - tabular.textHOffset(cell);
6079 int const xend = xbeg + tabular.cellWidth(cell);
6080 row_type const row = tabular.cellRow(cell);
6081 int const ybeg = o.y_ - tabular.rowAscent(row)
6082 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6083 int const yend = ybeg + tabular.cellHeight(cell);
6095 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6096 // << " ybeg=" << ybeg << " yend=" << yend
6097 // << " xx=" << xx << " yy=" << yy
6098 // << " dist=" << xx + yy << endl;
6103 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6105 //lyxerr << "InsetTabular::editXY: " << this << endl;
6107 cur.idx() = getNearestCell(cur.bv(), x, y);
6108 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6112 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6114 cur.idx() = getNearestCell(cur.bv(), x, y);
6115 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6119 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6121 idx_type idx_min = 0;
6122 int dist_min = numeric_limits<int>::max();
6123 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6124 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6125 int const d = dist(bv, i, x, y);
6136 int InsetTabular::cellYPos(idx_type const cell) const
6138 row_type row = tabular.cellRow(cell);
6140 for (row_type r = 0; r < row; ++r)
6141 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6142 + tabular.interRowSpace(r + 1);
6147 int InsetTabular::cellXPos(idx_type const cell) const
6149 col_type col = tabular.cellColumn(cell);
6151 for (col_type c = 0; c < col; ++c)
6152 lx += tabular.column_info[c].width;
6157 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6159 row_type const row = tabular.cellRow(cur.idx());
6160 col_type const col = tabular.cellColumn(cur.idx());
6162 if (isRightToLeft(cur)) {
6163 if (tabular.cellColumn(cur.idx()) == 0) {
6164 if (row == tabular.nrows() - 1)
6166 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6171 cur.idx() = tabular.getLastCellInRow(row - 1);
6173 cur.idx() = tabular.cellIndex(row, col - 1);
6176 if (tabular.isLastCell(cur.idx()))
6178 if (cur.idx() == tabular.getLastCellInRow(row))
6179 cur.idx() = tabular.cellIndex(row + 1, 0);
6181 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6182 cur.idx() = tabular.cellIndex(row, colnextcell);
6186 cur.boundary(false);
6188 if (cur.selIsMultiCell()) {
6189 cur.pit() = cur.lastpit();
6190 cur.pos() = cur.lastpos();
6197 // in visual mode, place cursor at extreme left or right
6199 switch(entry_from) {
6201 case ENTRY_DIRECTION_RIGHT:
6202 cur.posVisToRowExtremity(false /* !left */);
6204 case ENTRY_DIRECTION_LEFT:
6205 cur.posVisToRowExtremity(true /* left */);
6207 case ENTRY_DIRECTION_IGNORE:
6208 // nothing to do in this case
6212 cur.setCurrentFont();
6216 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6218 row_type const row = tabular.cellRow(cur.idx());
6219 col_type const col = tabular.cellColumn(cur.idx());
6221 if (isRightToLeft(cur)) {
6222 if (cur.idx() == tabular.getLastCellInRow(row)) {
6225 cur.idx() = tabular.getFirstCellInRow(row);
6226 cur.idx() = tabular.cellAbove(cur.idx());
6228 if (tabular.isLastCell(cur.idx()))
6230 if (cur.idx() == tabular.getLastCellInRow(row))
6231 cur.idx() = tabular.cellIndex(row + 1, 0);
6233 cur.idx() = tabular.cellIndex(row, col + 1);
6236 if (cur.idx() == 0) // first cell
6239 cur.idx() = tabular.getLastCellInRow(row - 1);
6241 cur.idx() = tabular.cellIndex(row, col - 1);
6244 if (cur.selIsMultiCell()) {
6245 cur.pit() = cur.lastpit();
6246 cur.pos() = cur.lastpos();
6250 cur.pit() = cur.lastpit();
6251 cur.pos() = cur.lastpos();
6253 // in visual mode, place cursor at extreme left or right
6255 switch(entry_from) {
6257 case ENTRY_DIRECTION_RIGHT:
6258 cur.posVisToRowExtremity(false /* !left */);
6260 case ENTRY_DIRECTION_LEFT:
6261 cur.posVisToRowExtremity(true /* left */);
6263 case ENTRY_DIRECTION_IGNORE:
6264 // nothing to do in this case
6268 cur.setCurrentFont();
6272 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6274 cur.recordUndoInset(this);
6276 istringstream is(argument);
6277 // limit the size of strings we read to avoid memory problems
6281 size_t safe_guard = 0;
6286 if (safe_guard > 1000) {
6287 LYXERR0("parameter max count reached!");
6291 Tabular::Feature action = Tabular::LAST_ACTION;
6294 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6295 if (s != tabularFeature[i].feature)
6297 action = tabularFeature[i].action;
6300 if (action == Tabular::LAST_ACTION) {
6301 LYXERR0("Feature not found " << s);
6305 if (tabularFeature[i].need_value)
6307 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6308 tabularFeatures(cur, action, val);
6313 static void checkLongtableSpecial(Tabular::ltType & ltt,
6314 string const & special, bool & flag)
6316 if (special == "dl_above") {
6319 } else if (special == "dl_below") {
6320 ltt.bottomDL = flag;
6322 } else if (special == "empty") {
6332 bool InsetTabular::oneCellHasRotationState(bool rotated,
6333 row_type row_start, row_type row_end,
6334 col_type col_start, col_type col_end) const
6336 for (row_type r = row_start; r <= row_end; ++r)
6337 for (col_type c = col_start; c <= col_end; ++c)
6339 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6342 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6349 void InsetTabular::tabularFeatures(Cursor & cur,
6350 Tabular::Feature feature, string const & value)
6352 col_type sel_col_start;
6353 col_type sel_col_end;
6354 row_type sel_row_start;
6355 row_type sel_row_end;
6356 bool setLines = false;
6357 bool setLinesInnerOnly = false;
6358 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6359 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6363 case Tabular::M_ALIGN_LEFT:
6364 case Tabular::ALIGN_LEFT:
6365 setAlign = LYX_ALIGN_LEFT;
6368 case Tabular::M_ALIGN_RIGHT:
6369 case Tabular::ALIGN_RIGHT:
6370 setAlign = LYX_ALIGN_RIGHT;
6373 case Tabular::M_ALIGN_CENTER:
6374 case Tabular::ALIGN_CENTER:
6375 setAlign = LYX_ALIGN_CENTER;
6378 case Tabular::ALIGN_BLOCK:
6379 setAlign = LYX_ALIGN_BLOCK;
6382 case Tabular::ALIGN_DECIMAL:
6383 setAlign = LYX_ALIGN_DECIMAL;
6386 case Tabular::M_VALIGN_TOP:
6387 case Tabular::VALIGN_TOP:
6388 setVAlign = Tabular::LYX_VALIGN_TOP;
6391 case Tabular::M_VALIGN_BOTTOM:
6392 case Tabular::VALIGN_BOTTOM:
6393 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6396 case Tabular::M_VALIGN_MIDDLE:
6397 case Tabular::VALIGN_MIDDLE:
6398 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6405 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6406 row_type const row = tabular.cellRow(cur.idx());
6407 col_type const column = tabular.cellColumn(cur.idx());
6409 Tabular::ltType ltt;
6413 case Tabular::SET_TABULAR_WIDTH:
6414 tabular.setTabularWidth(Length(value));
6417 case Tabular::SET_PWIDTH: {
6418 Length const len(value);
6419 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6420 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6422 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6423 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6428 case Tabular::SET_MPWIDTH:
6429 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6432 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6433 bool const varwidth = value == "on";
6434 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6435 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6439 case Tabular::SET_MROFFSET:
6440 tabular.setMROffset(cur, cur.idx(), Length(value));
6443 case Tabular::SET_SPECIAL_COLUMN:
6444 case Tabular::SET_SPECIAL_MULTICOLUMN:
6445 if (value == "none")
6446 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6448 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6451 case Tabular::APPEND_ROW:
6452 // append the row into the tabular
6453 tabular.appendRow(row);
6456 case Tabular::APPEND_COLUMN:
6457 // append the column into the tabular
6458 tabular.appendColumn(column);
6459 cur.idx() = tabular.cellIndex(row, column);
6462 case Tabular::DELETE_ROW:
6463 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6464 for (col_type c = 0; c < tabular.ncols(); c++) {
6465 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6466 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6467 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6468 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6472 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6473 tabular.deleteRow(sel_row_start);
6474 if (sel_row_start >= tabular.nrows())
6476 cur.idx() = tabular.cellIndex(sel_row_start, column);
6479 cur.selection(false);
6482 case Tabular::DELETE_COLUMN:
6483 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6484 for (row_type r = 0; r < tabular.nrows(); r++)
6485 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6486 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6489 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6490 for (row_type r = 0; r < tabular.nrows(); r++)
6491 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6492 tabular.leftLine(tabular.cellIndex(r, 0)));
6495 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6496 tabular.deleteColumn(sel_col_start);
6497 if (sel_col_start >= tabular.ncols())
6499 cur.idx() = tabular.cellIndex(row, sel_col_start);
6502 cur.selection(false);
6505 case Tabular::COPY_ROW:
6506 tabular.copyRow(row);
6509 case Tabular::COPY_COLUMN:
6510 tabular.copyColumn(column);
6511 cur.idx() = tabular.cellIndex(row, column);
6514 case Tabular::MOVE_COLUMN_RIGHT:
6515 tabular.moveColumn(column, Tabular::RIGHT);
6516 cur.idx() = tabular.cellIndex(row, column + 1);
6519 case Tabular::MOVE_COLUMN_LEFT:
6520 tabular.moveColumn(column, Tabular::LEFT);
6521 cur.idx() = tabular.cellIndex(row, column - 1);
6524 case Tabular::MOVE_ROW_DOWN:
6525 tabular.moveRow(row, Tabular::DOWN);
6526 cur.idx() = tabular.cellIndex(row + 1, column);
6529 case Tabular::MOVE_ROW_UP:
6530 tabular.moveRow(row, Tabular::UP);
6531 cur.idx() = tabular.cellIndex(row - 1, column);
6534 case Tabular::SET_LINE_TOP:
6535 case Tabular::TOGGLE_LINE_TOP: {
6536 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6537 ? (value == "true") : !tabular.topLine(cur.idx());
6538 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6539 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6540 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6544 case Tabular::SET_LINE_BOTTOM:
6545 case Tabular::TOGGLE_LINE_BOTTOM: {
6546 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6547 ? (value == "true") : !tabular.bottomLine(cur.idx());
6548 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6549 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6550 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6554 case Tabular::SET_LTRIM_TOP:
6555 case Tabular::TOGGLE_LTRIM_TOP: {
6556 bool l = (feature == Tabular::SET_LTRIM_TOP)
6557 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6558 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6559 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6560 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6564 case Tabular::SET_RTRIM_TOP:
6565 case Tabular::TOGGLE_RTRIM_TOP: {
6566 bool l = (feature == Tabular::SET_RTRIM_TOP)
6567 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6568 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6569 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6570 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6574 case Tabular::SET_LTRIM_BOTTOM:
6575 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6576 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6577 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6578 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6579 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6580 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6584 case Tabular::SET_RTRIM_BOTTOM:
6585 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6586 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6587 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6588 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6589 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6590 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6594 case Tabular::SET_LINE_LEFT:
6595 case Tabular::TOGGLE_LINE_LEFT: {
6596 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6597 ? (value == "true") : !tabular.leftLine(cur.idx());
6598 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6599 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6600 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6604 case Tabular::SET_LINE_RIGHT:
6605 case Tabular::TOGGLE_LINE_RIGHT: {
6606 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6607 ? (value == "true") : !tabular.rightLine(cur.idx());
6608 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6609 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6610 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6614 case Tabular::M_ALIGN_LEFT:
6615 case Tabular::M_ALIGN_RIGHT:
6616 case Tabular::M_ALIGN_CENTER:
6617 case Tabular::ALIGN_LEFT:
6618 case Tabular::ALIGN_RIGHT:
6619 case Tabular::ALIGN_CENTER:
6620 case Tabular::ALIGN_BLOCK:
6621 case Tabular::ALIGN_DECIMAL:
6622 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6623 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6624 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6625 !tabular.getPWidth(c).zero());
6628 case Tabular::M_VALIGN_TOP:
6629 case Tabular::M_VALIGN_BOTTOM:
6630 case Tabular::M_VALIGN_MIDDLE:
6633 case Tabular::VALIGN_TOP:
6634 case Tabular::VALIGN_BOTTOM:
6635 case Tabular::VALIGN_MIDDLE:
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.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6641 case Tabular::SET_MULTICOLUMN: {
6642 if (!cur.selection()) {
6643 // just multicol for one single cell
6644 // check whether we are completely in a multicol
6645 if (!tabular.isMultiColumn(cur.idx()))
6646 tabular.setMultiColumn(cur, cur.idx(), 1,
6647 tabular.rightLine(cur.idx()));
6650 // we have a selection so this means we just add all these
6651 // cells to form a multicolumn cell
6652 idx_type const s_start = cur.selBegin().idx();
6653 row_type const col_start = tabular.cellColumn(s_start);
6654 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6655 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6656 tabular.rightLine(cur.selEnd().idx()));
6659 cur.selection(false);
6663 case Tabular::UNSET_MULTICOLUMN: {
6664 if (!cur.selection()) {
6665 if (tabular.isMultiColumn(cur.idx()))
6666 tabular.unsetMultiColumn(cur.idx());
6671 case Tabular::MULTICOLUMN: {
6672 if (!cur.selection()) {
6673 if (tabular.isMultiColumn(cur.idx()))
6674 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6676 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6680 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6681 row_type const r = sel_row_start;
6682 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6683 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6686 // If the selection contains at least one singlecol cell
6687 // or multiple multicol cells,
6688 // we assume the user will merge is to a single multicol
6690 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6692 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6696 case Tabular::SET_MULTIROW: {
6697 if (!cur.selection()) {
6698 // just multirow for one single cell
6699 // check whether we are completely in a multirow
6700 if (!tabular.isMultiRow(cur.idx()))
6701 tabular.setMultiRow(cur, cur.idx(), 1,
6702 tabular.bottomLine(cur.idx()),
6703 tabular.getAlignment(cur.idx()));
6706 // we have a selection so this means we just add all this
6707 // cells to form a multirow cell
6708 idx_type const s_start = cur.selBegin().idx();
6709 row_type const row_start = tabular.cellRow(s_start);
6710 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6711 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6712 tabular.bottomLine(cur.selEnd().idx()),
6713 tabular.getAlignment(cur.selEnd().idx()));
6716 cur.selection(false);
6720 case Tabular::UNSET_MULTIROW: {
6721 if (!cur.selection()) {
6722 if (tabular.isMultiRow(cur.idx()))
6723 tabular.unsetMultiRow(cur.idx());
6728 case Tabular::MULTIROW: {
6729 if (!cur.selection()) {
6730 if (tabular.isMultiRow(cur.idx()))
6731 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6733 tabularFeatures(cur, Tabular::SET_MULTIROW);
6737 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6738 col_type const c = sel_col_start;
6739 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6740 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6743 // If the selection contains at least one singlerow cell
6744 // or multiple multirow cells,
6745 // we assume the user will merge is to a single multirow
6747 tabularFeatures(cur, Tabular::SET_MULTIROW);
6749 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6753 case Tabular::SET_INNER_LINES:
6754 setLinesInnerOnly = true;
6756 case Tabular::SET_ALL_LINES:
6759 case Tabular::UNSET_ALL_LINES:
6760 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6761 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6762 idx_type const cell = tabular.cellIndex(r, c);
6763 if (!setLinesInnerOnly || r != sel_row_start)
6764 tabular.setTopLine(cell, setLines);
6765 if ((!setLinesInnerOnly || r != sel_row_end)
6766 && (!setLines || r == sel_row_end))
6767 tabular.setBottomLine(cell, setLines);
6768 if ((!setLinesInnerOnly || c != sel_col_end)
6769 && (!setLines || c == sel_col_end))
6770 tabular.setRightLine(cell, setLines);
6771 if ((!setLinesInnerOnly || c != sel_col_start))
6772 tabular.setLeftLine(cell, setLines);
6776 case Tabular::RESET_FORMAL_DEFAULT:
6777 for (row_type r = 0; r < tabular.nrows(); ++r) {
6778 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6779 for (col_type c = 0; c < tabular.ncols(); ++c) {
6780 idx_type const cell = tabular.cellIndex(r, c);
6781 tabular.setTopLine(cell, head_or_foot);
6782 tabular.setBottomLine(cell, head_or_foot);
6787 case Tabular::SET_BORDER_LINES:
6788 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6789 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6790 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6792 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6793 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6794 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6798 case Tabular::TOGGLE_LONGTABULAR:
6799 if (tabular.is_long_tabular)
6800 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6802 tabular.is_long_tabular = true;
6805 case Tabular::SET_LONGTABULAR:
6806 tabular.is_long_tabular = true;
6809 case Tabular::UNSET_LONGTABULAR:
6810 for (row_type r = 0; r < tabular.nrows(); ++r) {
6811 if (tabular.ltCaption(r)) {
6812 cur.idx() = tabular.cellIndex(r, 0);
6815 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6818 tabular.is_long_tabular = false;
6821 case Tabular::SET_ROTATE_TABULAR:
6822 tabular.rotate = convert<int>(value);
6825 case Tabular::UNSET_ROTATE_TABULAR:
6829 case Tabular::TOGGLE_ROTATE_TABULAR:
6830 // when pressing the rotate button we default to 90° rotation
6831 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6834 case Tabular::TABULAR_VALIGN_TOP:
6835 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6838 case Tabular::TABULAR_VALIGN_MIDDLE:
6839 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6842 case Tabular::TABULAR_VALIGN_BOTTOM:
6843 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6846 case Tabular::LONGTABULAR_ALIGN_LEFT:
6847 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6850 case Tabular::LONGTABULAR_ALIGN_CENTER:
6851 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6854 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6855 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6858 case Tabular::SET_ROTATE_CELL:
6859 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6860 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6861 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6864 case Tabular::UNSET_ROTATE_CELL:
6865 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6866 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6867 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6870 case Tabular::TOGGLE_ROTATE_CELL:
6872 bool oneNotRotated = oneCellHasRotationState(false,
6873 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6875 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6876 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6877 // when pressing the rotate cell button we default to 90° rotation
6879 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6881 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6886 case Tabular::SET_USEBOX: {
6887 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6888 if (val == tabular.getUsebox(cur.idx()))
6889 val = Tabular::BOX_NONE;
6890 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6891 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6892 tabular.setUsebox(tabular.cellIndex(r, c), val);
6896 case Tabular::UNSET_LTFIRSTHEAD:
6899 case Tabular::SET_LTFIRSTHEAD:
6900 tabular.getRowOfLTFirstHead(row, ltt);
6901 checkLongtableSpecial(ltt, value, flag);
6902 tabular.setLTHead(row, flag, ltt, true);
6905 case Tabular::UNSET_LTHEAD:
6908 case Tabular::SET_LTHEAD:
6909 tabular.getRowOfLTHead(row, ltt);
6910 checkLongtableSpecial(ltt, value, flag);
6911 tabular.setLTHead(row, flag, ltt, false);
6914 case Tabular::UNSET_LTFOOT:
6917 case Tabular::SET_LTFOOT:
6918 tabular.getRowOfLTFoot(row, ltt);
6919 checkLongtableSpecial(ltt, value, flag);
6920 tabular.setLTFoot(row, flag, ltt, false);
6923 case Tabular::UNSET_LTLASTFOOT:
6926 case Tabular::SET_LTLASTFOOT:
6927 tabular.getRowOfLTLastFoot(row, ltt);
6928 checkLongtableSpecial(ltt, value, flag);
6929 tabular.setLTFoot(row, flag, ltt, true);
6932 case Tabular::UNSET_LTNEWPAGE:
6935 case Tabular::SET_LTNEWPAGE:
6936 tabular.setLTNewPage(row, flag);
6939 case Tabular::SET_LTCAPTION: {
6940 if (tabular.ltCaption(row))
6942 cur.idx() = tabular.setLTCaption(cur, row, true);
6945 cur.selection(false);
6946 // If a row is set as caption, then also insert
6947 // a caption. Otherwise the LaTeX output is broken.
6948 // Select cell if it is non-empty
6949 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6950 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6951 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6955 case Tabular::UNSET_LTCAPTION: {
6956 if (!tabular.ltCaption(row))
6958 cur.idx() = tabular.setLTCaption(cur, row, false);
6961 cur.selection(false);
6962 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6963 if (lyx::getStatus(fr).enabled())
6968 case Tabular::TOGGLE_LTCAPTION: {
6969 if (tabular.ltCaption(row))
6970 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6972 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6976 case Tabular::TOGGLE_BOOKTABS:
6977 tabular.use_booktabs = !tabular.use_booktabs;
6980 case Tabular::SET_BOOKTABS:
6981 tabular.use_booktabs = true;
6984 case Tabular::UNSET_BOOKTABS:
6985 tabular.use_booktabs = false;
6988 case Tabular::SET_TOP_SPACE: {
6990 if (value == "default")
6991 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6992 tabular.row_info[r].top_space_default = true;
6993 else if (value == "none")
6994 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6995 tabular.row_info[r].top_space_default = false;
6996 tabular.row_info[r].top_space = len;
6998 else if (isValidLength(value, &len))
6999 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7000 tabular.row_info[r].top_space_default = false;
7001 tabular.row_info[r].top_space = len;
7006 case Tabular::SET_BOTTOM_SPACE: {
7008 if (value == "default")
7009 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7010 tabular.row_info[r].bottom_space_default = true;
7011 else if (value == "none")
7012 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7013 tabular.row_info[r].bottom_space_default = false;
7014 tabular.row_info[r].bottom_space = len;
7016 else if (isValidLength(value, &len))
7017 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7018 tabular.row_info[r].bottom_space_default = false;
7019 tabular.row_info[r].bottom_space = len;
7024 case Tabular::SET_INTERLINE_SPACE: {
7026 if (value == "default")
7027 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7028 tabular.row_info[r].interline_space_default = true;
7029 else if (value == "none")
7030 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7031 tabular.row_info[r].interline_space_default = false;
7032 tabular.row_info[r].interline_space = len;
7034 else if (isValidLength(value, &len))
7035 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7036 tabular.row_info[r].interline_space_default = false;
7037 tabular.row_info[r].interline_space = len;
7042 case Tabular::SET_DECIMAL_POINT:
7043 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7044 tabular.column_info[c].decimal_point = from_utf8(value);
7047 // dummy stuff just to avoid warnings
7048 case Tabular::LAST_ACTION:
7054 bool InsetTabular::copySelection(Cursor & cur)
7056 if (!cur.selection())
7061 getSelection(cur, rs, re, cs, ce);
7063 paste_tabular.reset(new Tabular(tabular));
7065 for (row_type r = 0; r < rs; ++r)
7066 paste_tabular->deleteRow(0, true);
7068 row_type const rows = re - rs + 1;
7069 while (paste_tabular->nrows() > rows)
7070 paste_tabular->deleteRow(rows, true);
7072 for (col_type c = 0; c < cs; ++c)
7073 paste_tabular->deleteColumn(0, true);
7075 col_type const columns = ce - cs + 1;
7076 while (paste_tabular->ncols() > columns)
7077 paste_tabular->deleteColumn(columns, true);
7079 paste_tabular->setBuffer(tabular.buffer());
7081 odocstringstream os;
7082 OutputParams const runparams(0);
7083 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7084 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7085 cap::copySelection(cur, os.str());
7087 // mark tabular stack dirty
7088 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7089 // when we (hopefully) have a one-for-all paste mechanism.
7090 // This must be called after cap::copySelection.
7091 dirtyTabularStack(true);
7097 bool InsetTabular::pasteClipboard(Cursor & cur)
7101 col_type actcol = tabular.cellColumn(cur.idx());
7102 row_type actrow = tabular.cellRow(cur.idx());
7104 if (cur.selIsMultiCell()) {
7107 getSelection(cur, actrow, re, actcol, ce);
7110 for (row_type r1 = 0, r2 = actrow;
7111 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
7113 for (col_type c1 = 0, c2 = actcol;
7114 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
7116 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7117 tabular.isPartOfMultiColumn(r2, c2))
7119 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7123 if (tabular.isPartOfMultiColumn(r2, c2)) {
7127 shared_ptr<InsetTableCell> inset(
7128 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7129 tabular.setCellInset(r2, c2, inset);
7130 // FIXME?: why do we need to do this explicitly? (EL)
7131 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7133 if (!lyxrc.ct_markup_copied) {
7134 // do not paste deleted text
7135 inset->acceptChanges();
7136 inset->setChange(Change(buffer().params().track_changes ?
7137 Change::INSERTED : Change::UNCHANGED));
7147 void InsetTabular::cutSelection(Cursor & cur)
7149 if (!cur.selection())
7154 getSelection(cur, rs, re, cs, ce);
7155 for (row_type r = rs; r <= re; ++r) {
7156 for (col_type c = cs; c <= ce; ++c) {
7157 shared_ptr<InsetTableCell> t
7158 = cell(tabular.cellIndex(r, c));
7159 if (buffer().params().track_changes)
7160 // FIXME: Change tracking (MG)
7161 t->setChange(Change(Change::DELETED));
7167 // cursor position might be invalid now
7168 if (cur.pit() > cur.lastpit())
7169 cur.pit() = cur.lastpit();
7170 if (cur.pos() > cur.lastpos())
7171 cur.pos() = cur.lastpos();
7172 cur.clearSelection();
7176 bool InsetTabular::isRightToLeft(Cursor & cur) const
7178 // LASSERT: It might be better to abandon this Buffer.
7179 LASSERT(cur.depth() > 1, return false);
7180 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7181 pos_type const parentpos = cur[cur.depth() - 2].pos();
7182 return parentpar.getFontSettings(buffer().params(),
7183 parentpos).language()->rightToLeft();
7187 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7190 LASSERT(stidx <= enidx, return docstring());
7192 col_type const col1 = tabular.cellColumn(stidx);
7193 col_type const col2 = tabular.cellColumn(enidx);
7194 row_type const row1 = tabular.cellRow(stidx);
7195 row_type const row2 = tabular.cellRow(enidx);
7197 for (col_type col = col1; col <= col2; col++)
7198 for (row_type row = row1; row <= row2; row++) {
7203 retval += tabular.cellInset(row, col)->asString(intoInsets);
7209 void InsetTabular::getSelection(Cursor & cur,
7210 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7212 CursorSlice const & beg = cur.selBegin();
7213 CursorSlice const & end = cur.selEnd();
7214 cs = tabular.cellColumn(beg.idx());
7215 ce = tabular.cellColumn(end.idx());
7219 rs = tabular.cellRow(beg.idx());
7220 re = tabular.cellRow(end.idx());
7226 Text * InsetTabular::getText(int idx) const
7228 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
7232 bool InsetTabular::isChanged() const
7234 for (idx_type idx = 0; idx < nargs(); ++idx) {
7235 if (cell(idx)->isChanged())
7237 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7239 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7246 void InsetTabular::setChange(Change const & change)
7248 for (idx_type idx = 0; idx < nargs(); ++idx)
7249 cell(idx)->setChange(change);
7253 void InsetTabular::acceptChanges()
7255 for (idx_type idx = 0; idx < nargs(); ++idx)
7256 cell(idx)->acceptChanges();
7257 for (row_type row = 0; row < tabular.nrows(); ++row) {
7258 if (tabular.row_info[row].change.inserted())
7259 tabular.row_info[row].change.setUnchanged();
7260 else if (tabular.row_info[row].change.deleted())
7261 tabular.deleteRow(row, true);
7263 for (col_type col = 0; col < tabular.ncols(); ++col) {
7264 if (tabular.column_info[col].change.inserted())
7265 tabular.column_info[col].change.setUnchanged();
7266 else if (tabular.column_info[col].change.deleted())
7267 tabular.deleteColumn(col, true);
7269 tabular.updateIndexes();
7273 void InsetTabular::rejectChanges()
7275 for (idx_type idx = 0; idx < nargs(); ++idx)
7276 cell(idx)->rejectChanges();
7277 for (row_type row = 0; row < tabular.nrows(); ++row) {
7278 if (tabular.row_info[row].change.deleted())
7279 tabular.row_info[row].change.setUnchanged();
7280 else if (tabular.row_info[row].change.inserted())
7281 tabular.deleteRow(row, true);
7283 for (col_type col = 0; col < tabular.ncols(); ++col) {
7284 if (tabular.column_info[col].change.deleted())
7285 tabular.column_info[col].change.setUnchanged();
7286 else if (tabular.column_info[col].change.inserted())
7287 tabular.deleteColumn(col, true);
7289 tabular.updateIndexes();
7293 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7295 return tabular.getPWidth(cell).zero();
7299 bool InsetTabular::forcePlainLayout(idx_type cell) const
7301 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
7305 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7308 if (buf.length() <= 0)
7313 col_type maxCols = 1;
7314 size_t const len = buf.length();
7318 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7326 maxCols = max(cols, maxCols);
7332 maxCols = max(cols, maxCols);
7338 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7339 loctab = paste_tabular.get();
7340 dirtyTabularStack(true);
7343 cell = bv.cursor().idx();
7344 ocol = tabular.cellColumn(cell);
7345 row = tabular.cellRow(cell);
7349 idx_type cells = loctab->numberofcells;
7352 rows = loctab->nrows();
7353 col_type columns = loctab->ncols();
7356 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7362 // append column if necessary
7363 if (cols == columns) {
7364 loctab->appendColumn(cols - 1);
7365 columns = loctab->ncols();
7366 cells = loctab->numberofcells;
7369 if (cols < columns) {
7370 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7371 Font const font = bv.textMetrics(&inset->text()).
7373 inset->setText(buf.substr(op, p - op), font,
7374 buffer().params().track_changes);
7380 // we can only set this if we are not too far right
7381 if (cols < columns) {
7382 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7383 Font const font = bv.textMetrics(&inset->text()).
7385 inset->setText(buf.substr(op, p - op), font,
7386 buffer().params().track_changes);
7390 // append row if necessary
7391 if (row == rows && p < len - 1) {
7392 loctab->appendRow(row - 1);
7393 rows = loctab->nrows();
7394 cells = loctab->numberofcells;
7397 cell = loctab->cellIndex(row, cols);
7403 // check for the last cell if there is no trailing '\n'
7404 if (cell < cells && op < len) {
7405 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7406 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7407 inset->setText(buf.substr(op, len - op), font,
7408 buffer().params().track_changes);
7414 void InsetTabular::addPreview(DocIterator const & inset_pos,
7415 PreviewLoader & loader) const
7417 DocIterator cell_pos = inset_pos;
7419 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7420 for (row_type r = 0; r < tabular.nrows(); ++r) {
7421 for (col_type c = 0; c < tabular.ncols(); ++c) {
7422 cell_pos.top().idx() = tabular.cellIndex(r, c);
7423 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7429 bool InsetTabular::completionSupported(Cursor const & cur) const
7431 Cursor const & bvCur = cur.bv().cursor();
7432 if (&bvCur.inset() != this)
7434 return cur.text()->completionSupported(cur);
7438 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7440 return completionSupported(cur);
7444 bool InsetTabular::automaticInlineCompletion() const
7446 return lyxrc.completion_inline_text;
7450 bool InsetTabular::automaticPopupCompletion() const
7452 return lyxrc.completion_popup_text;
7456 bool InsetTabular::showCompletionCursor() const
7458 return lyxrc.completion_cursor_text;
7462 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7464 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
7468 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7470 if (!completionSupported(cur))
7472 return cur.text()->completionPrefix(cur);
7476 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7478 if (!completionSupported(cur))
7481 return cur.text()->insertCompletion(cur, s, finished);
7485 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7486 Dimension & dim) const
7488 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7489 tm.completionPosAndDim(cur, x, y, dim);
7493 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7495 istringstream data(in);
7497 lex.setStream(data);
7504 if (!lex || token != "tabular") {
7505 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7509 // This is part of the inset proper that is usually swallowed
7510 // by Buffer::readInset
7512 if (!lex || token != "Tabular") {
7513 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7521 string InsetTabular::params2string(InsetTabular const & inset)
7524 data << "tabular" << ' ';
7526 data << "\\end_inset\n";
7531 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7533 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
7534 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
7535 if (!tabular.isPartOfMultiColumn(r,c) &&
7536 !tabular.isPartOfMultiRow(r,c))
7539 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7540 ParagraphList::iterator it = parlist.begin();
7541 ParagraphList::iterator const en = parlist.end();
7542 for (; it != en; ++it)
7543 it->setLayout(dc.plainLayout());