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"
46 #include "output_xhtml.h"
47 #include "Paragraph.h"
48 #include "ParagraphParameters.h"
49 #include "ParIterator.h"
51 #include "texstream.h"
52 #include "TextClass.h"
53 #include "TextMetrics.h"
55 #include "frontends/Application.h"
56 #include "frontends/alert.h"
57 #include "frontends/Clipboard.h"
58 #include "frontends/Painter.h"
59 #include "frontends/Selection.h"
61 #include "support/convert.h"
62 #include "support/debug.h"
63 #include "support/docstream.h"
64 #include "support/FileName.h"
65 #include "support/gettext.h"
66 #include "support/lassert.h"
67 #include "support/lstrings.h"
68 #include "support/unique_ptr.h"
77 using namespace lyx::support;
83 using cap::dirtyTabularStack;
84 using cap::tabularStackDirty;
86 using graphics::PreviewLoader;
88 using frontend::Painter;
89 using frontend::Clipboard;
91 namespace Alert = frontend::Alert;
96 int const ADD_TO_HEIGHT = 2; // in cell
97 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
98 int const default_line_space = 10; // ?
99 int const WIDTH_OF_LINE = 5; // space between double lines
103 unique_ptr<Tabular> paste_tabular;
106 struct TabularFeature {
107 Tabular::Feature action;
113 TabularFeature tabularFeature[] =
115 // the SET/UNSET actions are used by the table dialog,
116 // the TOGGLE actions by the table toolbar buttons
117 // FIXME: these values have been hardcoded in InsetMathGrid and other
119 { Tabular::APPEND_ROW, "append-row", false },
120 { Tabular::APPEND_COLUMN, "append-column", false },
121 { Tabular::DELETE_ROW, "delete-row", false },
122 { Tabular::DELETE_COLUMN, "delete-column", false },
123 { Tabular::COPY_ROW, "copy-row", false },
124 { Tabular::COPY_COLUMN, "copy-column", false },
125 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
126 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
127 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
128 { Tabular::MOVE_ROW_UP, "move-row-up", false },
129 { Tabular::SET_LINE_TOP, "set-line-top", true },
130 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
131 { Tabular::SET_LTRIM_TOP, "set-ltrim-top", true },
132 { Tabular::SET_LTRIM_BOTTOM, "set-ltrim-bottom", true },
133 { Tabular::SET_RTRIM_TOP, "set-rtrim-top", true },
134 { Tabular::SET_RTRIM_BOTTOM, "set-rtrim-bottom", true },
135 { Tabular::SET_LINE_LEFT, "set-line-left", true },
136 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
137 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
138 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
139 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
140 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
141 { Tabular::TOGGLE_LTRIM_TOP, "toggle-ltrim-top", false },
142 { Tabular::TOGGLE_LTRIM_BOTTOM, "toggle-ltrim-bottom", false },
143 { Tabular::TOGGLE_RTRIM_TOP, "toggle-rtrim-top", false },
144 { Tabular::TOGGLE_RTRIM_BOTTOM, "toggle-rtrim-bottom", false },
145 { Tabular::ALIGN_LEFT, "align-left", false },
146 { Tabular::ALIGN_RIGHT, "align-right", false },
147 { Tabular::ALIGN_CENTER, "align-center", false },
148 { Tabular::ALIGN_BLOCK, "align-block", false },
149 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
150 { Tabular::VALIGN_TOP, "valign-top", false },
151 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
152 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
153 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
154 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
155 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
156 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
157 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
158 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
159 { Tabular::MULTICOLUMN, "multicolumn", false },
160 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
161 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
162 { Tabular::MULTIROW, "multirow", false },
163 { Tabular::SET_MULTIROW, "set-multirow", false },
164 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
165 { Tabular::SET_MROFFSET, "set-mroffset", true },
166 { Tabular::SET_ALL_LINES, "set-all-lines", false },
167 { Tabular::RESET_FORMAL_DEFAULT, "reset-formal-default", false },
168 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
169 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
170 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
171 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
172 { Tabular::SET_PWIDTH, "set-pwidth", true },
173 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
174 { Tabular::TOGGLE_VARWIDTH_COLUMN, "toggle-varwidth-column", true },
175 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
176 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
177 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
178 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
179 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
180 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
181 { Tabular::SET_USEBOX, "set-usebox", true },
182 { Tabular::SET_LTHEAD, "set-lthead", true },
183 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
184 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
185 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
186 { Tabular::SET_LTFOOT, "set-ltfoot", true },
187 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
188 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
189 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
190 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
191 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
192 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
193 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
194 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
195 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
196 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
197 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
198 { Tabular::SET_BOOKTABS, "set-booktabs", false },
199 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
200 { Tabular::SET_TOP_SPACE, "set-top-space", true },
201 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
202 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
203 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
204 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
205 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
206 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
207 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
208 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
209 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
210 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
211 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
212 { Tabular::SET_INNER_LINES, "set-inner-lines", false },
213 { Tabular::LAST_ACTION, "", false }
217 string const tostr(LyXAlignment const & num)
222 case LYX_ALIGN_BLOCK:
226 case LYX_ALIGN_CENTER:
228 case LYX_ALIGN_RIGHT:
230 case LYX_ALIGN_LAYOUT:
232 case LYX_ALIGN_SPECIAL:
234 case LYX_ALIGN_DECIMAL:
241 string const tostr(Tabular::HAlignment const & num)
244 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
246 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
248 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
255 string const tostr(Tabular::VAlignment const & num)
258 case Tabular::LYX_VALIGN_TOP:
260 case Tabular::LYX_VALIGN_MIDDLE:
262 case Tabular::LYX_VALIGN_BOTTOM:
269 string const tostr(Tabular::BoxType const & num)
272 case Tabular::BOX_NONE:
274 case Tabular::BOX_PARBOX:
276 case Tabular::BOX_MINIPAGE:
278 case Tabular::BOX_VARWIDTH:
285 // I would have liked a fromstr template a lot better. (Lgb)
286 bool string2type(string const & str, LyXAlignment & num)
289 num = LYX_ALIGN_NONE;
290 else if (str == "block")
291 num = LYX_ALIGN_BLOCK;
292 else if (str == "left")
293 num = LYX_ALIGN_LEFT;
294 else if (str == "center")
295 num = LYX_ALIGN_CENTER;
296 else if (str == "right")
297 num = LYX_ALIGN_RIGHT;
298 else if (str == "decimal")
299 num = LYX_ALIGN_DECIMAL;
306 bool string2type(string const & str, Tabular::HAlignment & num)
309 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
310 else if (str == "center" )
311 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
312 else if (str == "right")
313 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
320 bool string2type(string const & str, Tabular::VAlignment & num)
323 num = Tabular::LYX_VALIGN_TOP;
324 else if (str == "middle" )
325 num = Tabular::LYX_VALIGN_MIDDLE;
326 else if (str == "bottom")
327 num = Tabular::LYX_VALIGN_BOTTOM;
334 bool string2type(string const & str, Tabular::BoxType & num)
337 num = Tabular::BOX_NONE;
338 else if (str == "parbox")
339 num = Tabular::BOX_PARBOX;
340 else if (str == "minipage")
341 num = Tabular::BOX_MINIPAGE;
342 else if (str == "varwidth")
343 num = Tabular::BOX_VARWIDTH;
350 bool string2type(string const & str, bool & num)
354 else if (str == "false")
362 bool getTokenValue(string const & str, char const * token, string & ret)
365 size_t token_length = strlen(token);
366 size_t pos = str.find(token);
368 if (pos == string::npos || pos + token_length + 1 >= str.length()
369 || str[pos + token_length] != '=')
371 pos += token_length + 1;
373 if (ch != '"' && ch != '\'') { // only read till next space
377 while (pos < str.length() - 1 && str[++pos] != ch)
384 bool getTokenValue(string const & str, char const * token, docstring & ret)
387 bool const success = getTokenValue(str, token, tmp);
388 ret = from_utf8(tmp);
393 bool getTokenValue(string const & str, char const * token, int & num)
397 if (!getTokenValue(str, token, tmp))
399 num = convert<int>(tmp);
404 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
407 return getTokenValue(str, token, tmp) && string2type(tmp, num);
411 bool getTokenValue(string const & str, char const * token,
412 Tabular::HAlignment & num)
415 return getTokenValue(str, token, tmp) && string2type(tmp, num);
419 bool getTokenValue(string const & str, char const * token,
420 Tabular::VAlignment & num)
423 return getTokenValue(str, token, tmp) && string2type(tmp, num);
427 bool getTokenValue(string const & str, char const * token,
428 Tabular::BoxType & num)
431 return getTokenValue(str, token, tmp) && string2type(tmp, num);
435 bool getTokenValue(string const & str, char const * token, bool & flag)
437 // set the flag always to false as this should be the default for bools
438 // not in the file-format.
441 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
445 bool getTokenValue(string const & str, char const * token, Length & len)
447 // set the length to be zero() as default as this it should be if not
448 // in the file format.
451 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
455 bool getTokenValue(string const & str, char const * token, Change & change, BufferParams & bp)
457 // set the change to be Change() as default as this it should be if not
458 // in the file format.
461 if (getTokenValue(str, token, tmp)) {
462 vector<string> const changedata = getVectorFromString(tmp, " ");
463 if (changedata.size() != 3) {
464 Alert::warning(_("Change tracking data incomplete"),
465 _("Change tracking information for tabular row/column "
466 "is incomplete. I will ignore this."));
469 BufferParams::AuthorMap const & am = bp.author_map_;
470 int aid = convert<int>(changedata[1]);
471 if (am.find(aid) == am.end()) {
472 // FIXME Use ErrorList
473 Alert::warning(_("Change tracking author index missing"),
474 bformat(_("A change tracking author information for index "
475 "%1$d is missing. This can happen after a wrong "
476 "merge by a version control system. In this case, "
477 "either fix the merge, or have this information "
478 "missing until the corresponding tracked changes "
479 "are merged or this user edits the file again.\n"),
481 bp.addAuthor(Author(aid));
483 istringstream is(changedata[2]);
486 if (changedata[0] == "inserted") {
487 change = Change(Change::INSERTED, am.find(aid)->second, ct);
489 } else if (changedata[0] == "deleted") {
490 change = Change(Change::DELETED, am.find(aid)->second, ct);
498 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
503 if (!getTokenValue(str, token, tmp))
505 if (tmp == "default") {
509 return isValidLength(tmp, &len);
513 void l_getline(istream & is, string & str)
516 while (str.empty()) {
518 if (!str.empty() && str[str.length() - 1] == '\r')
519 str.erase(str.length() - 1);
524 string const write_attribute(string const & name, T const & t)
526 string const s = tostr(t);
527 return s.empty() ? s : " " + name + "=\"" + s + "\"";
531 string const write_attribute(string const & name, string const & t)
533 return t.empty() ? t : " " + name + "=\"" + t + "\"";
538 string const write_attribute(string const & name, docstring const & t)
540 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
545 string const write_attribute(string const & name, bool const & b)
547 // we write only true attribute values so we remove a bit of the
548 // file format bloat for tabulars.
549 return b ? write_attribute(name, convert<string>(b)) : string();
554 string const write_attribute(string const & name, int const & i)
556 // we write only true attribute values so we remove a bit of the
557 // file format bloat for tabulars.
558 return i ? write_attribute(name, convert<string>(i)) : string();
563 string const write_attribute(string const & name, Tabular::idx_type const & i)
565 // we write only true attribute values so we remove a bit of the
566 // file format bloat for tabulars.
567 return i ? write_attribute(name, convert<string>(i)) : string();
572 string const write_attribute(string const & name, Length const & value)
574 // we write only the value if we really have one same reason as above.
575 return value.zero() ? string() : write_attribute(name, value.asString());
578 string const write_attribute(string const & name, Change const & change, BufferParams const & bp)
580 odocstringstream ods;
581 if (change.inserted())
582 ods << from_ascii("inserted");
583 else if (change.deleted())
584 ods << from_ascii("deleted");
585 if (change.changed()) {
586 ods << " " << bp.authors().get(change.author).bufferId()
587 << " " << change.changetime;
588 return write_attribute(name, ods.str());
596 string const featureAsString(Tabular::Feature action)
598 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
599 if (tabularFeature[i].action == action)
600 return tabularFeature[i].feature;
606 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
608 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
609 for (; dit; dit.forwardChar())
610 if (dit.inTexted() && dit.depth() == 1
611 && dit.paragraph().find(align_d, false, false, dit.pos()))
618 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
620 InsetTableCell tail = InsetTableCell(head);
621 DocIterator const dit = separatorPos(&head, align_d);
624 pos_type const psize = head.paragraphs().front().size();
625 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
626 tail.paragraphs().front().eraseChars(0,
627 dit.pos() < psize ? dit.pos() + 1 : psize, false);
634 /////////////////////////////////////////////////////////////////////
638 /////////////////////////////////////////////////////////////////////
641 Tabular::CellData::CellData(Buffer * buf)
644 multicolumn(Tabular::CELL_NORMAL),
645 multirow(Tabular::CELL_NORMAL),
646 alignment(LYX_ALIGN_CENTER),
647 valignment(LYX_VALIGN_TOP),
655 top_line_rtrimmed(false),
656 top_line_ltrimmed(false),
657 bottom_line_rtrimmed(false),
658 bottom_line_ltrimmed(false),
661 inset(new InsetTableCell(buf))
663 inset->setBuffer(*buf);
667 Tabular::CellData::CellData(CellData const & cs)
670 multicolumn(cs.multicolumn),
671 multirow(cs.multirow),
672 mroffset(cs.mroffset),
673 alignment(cs.alignment),
674 valignment(cs.valignment),
675 decimal_hoffset(cs.decimal_hoffset),
676 decimal_width(cs.decimal_width),
678 top_line(cs.top_line),
679 bottom_line(cs.bottom_line),
680 left_line(cs.left_line),
681 right_line(cs.right_line),
682 top_line_rtrimmed(cs.top_line_rtrimmed),
683 top_line_ltrimmed(cs.top_line_ltrimmed),
684 bottom_line_rtrimmed(cs.bottom_line_rtrimmed),
685 bottom_line_ltrimmed(cs.bottom_line_ltrimmed),
688 align_special(cs.align_special),
690 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
694 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
700 multicolumn = cs.multicolumn;
701 multirow = cs.multirow;
702 mroffset = cs.mroffset;
703 alignment = cs.alignment;
704 valignment = cs.valignment;
705 decimal_hoffset = cs.decimal_hoffset;
706 decimal_width = cs.decimal_width;
707 voffset = cs.voffset;
708 top_line = cs.top_line;
709 bottom_line = cs.bottom_line;
710 left_line = cs.left_line;
711 right_line = cs.right_line;
712 top_line_rtrimmed = cs.top_line_rtrimmed;
713 top_line_ltrimmed = cs.top_line_ltrimmed;
714 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
715 bottom_line_ltrimmed = cs.bottom_line_ltrimmed;
718 align_special = cs.align_special;
719 p_width = cs.p_width;
720 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
724 Tabular::RowData::RowData()
727 top_space_default(false),
728 bottom_space_default(false),
729 interline_space_default(false),
740 Tabular::ColumnData::ColumnData()
741 : alignment(LYX_ALIGN_CENTER),
742 valignment(LYX_VALIGN_TOP),
750 Tabular::ltType::ltType()
751 : set(false), topDL(false),
757 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
759 init(buffer, rows_arg, columns_arg);
763 void Tabular::setBuffer(Buffer & buffer)
766 for (row_type i = 0; i < nrows(); ++i)
767 for (col_type j = 0; j < ncols(); ++j)
768 cell_info[i][j].inset->setBuffer(*buffer_);
772 // activates all lines and sets all widths to 0
773 void Tabular::init(Buffer * buf, row_type rows_arg,
774 col_type columns_arg)
777 row_info = row_vector(rows_arg);
778 column_info = column_vector(columns_arg);
779 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
780 row_info.reserve(10);
781 column_info.reserve(10);
782 cell_info.reserve(100);
784 is_long_tabular = false;
785 tabular_valignment = LYX_VALIGN_MIDDLE;
786 tabular_width = Length();
787 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
789 use_booktabs = false;
790 // set silly default lines
791 for (row_type r = 0; r < nrows(); ++r)
792 for (col_type c = 0; c < ncols(); ++c) {
793 cell_info[r][c].inset->setBuffer(*buffer_);
794 cell_info[r][c].top_line = true;
795 cell_info[r][c].left_line = true;
796 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
797 cell_info[r][c].right_line = c == ncols() - 1;
802 void Tabular::deleteRow(row_type const row, bool const force)
804 // Not allowed to delete last row
808 bool const ct = force ? false : buffer().params().track_changes;
810 for (col_type c = 0; c < ncols(); ++c) {
811 // Care about multirow cells
812 if (row + 1 < nrows() &&
813 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
814 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
815 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
819 row_info[row].change.setDeleted();
821 row_info.erase(row_info.begin() + row);
822 cell_info.erase(cell_info.begin() + row);
828 void Tabular::copyRow(row_type const row)
830 insertRow(row, true);
834 void Tabular::appendRow(row_type row)
836 insertRow(row, false);
840 void Tabular::insertRow(row_type const row, bool copy)
842 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
843 cell_info.insert(cell_info.begin() + row + 1,
844 cell_vector(0, CellData(buffer_)));
846 for (col_type c = 0; c < ncols(); ++c) {
847 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
848 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
849 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
850 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
854 for (col_type c = 0; c < ncols(); ++c) {
855 if (isPartOfMultiRow(row, c))
857 // inherit line settings
858 idx_type const i = cellIndex(row + 1, c);
859 idx_type const j = cellIndex(row, c);
860 setLeftLine(i, leftLine(j));
861 setRightLine(i, rightLine(j));
862 setTopLine(i, topLine(j));
863 if (topLine(j) && bottomLine(j)) {
864 setBottomLine(i, true);
865 setBottomLine(j, false);
868 if (buffer().params().track_changes) {
869 row_info[row + 1].change.setInserted();
875 void Tabular::moveColumn(col_type col, ColDirection direction)
877 if (direction == Tabular::LEFT)
880 std::swap(column_info[col], column_info[col + 1]);
882 for (row_type r = 0; r < nrows(); ++r) {
883 std::swap(cell_info[r][col], cell_info[r][col + 1]);
884 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
885 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
887 idx_type const i = cellIndex(r, col);
888 idx_type const j = cellIndex(r, col + 1);
889 if (buffer().params().track_changes) {
890 cellInfo(i).inset->setChange(Change(Change::INSERTED));
891 cellInfo(j).inset->setChange(Change(Change::INSERTED));
898 void Tabular::moveRow(row_type row, RowDirection direction)
900 if (direction == Tabular::UP)
903 std::swap(row_info[row], row_info[row + 1]);
905 for (col_type c = 0; c < ncols(); ++c) {
906 std::swap(cell_info[row][c], cell_info[row + 1][c]);
907 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
908 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
910 idx_type const i = cellIndex(row, c);
911 idx_type const j = cellIndex(row + 1, c);
912 if (buffer().params().track_changes) {
913 cellInfo(i).inset->setChange(Change(Change::INSERTED));
914 cellInfo(j).inset->setChange(Change(Change::INSERTED));
921 void Tabular::deleteColumn(col_type const col, bool const force)
923 // Not allowed to delete last column
927 bool const ct = force ? false : buffer().params().track_changes;
929 for (row_type r = 0; r < nrows(); ++r) {
930 // Care about multicolumn cells
931 if (col + 1 < ncols() &&
932 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
933 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
934 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
937 cell_info[r].erase(cell_info[r].begin() + col);
940 column_info[col].change.setDeleted();
942 column_info.erase(column_info.begin() + col);
947 void Tabular::copyColumn(col_type const col)
949 insertColumn(col, true);
953 void Tabular::appendColumn(col_type col)
955 insertColumn(col, false);
959 void Tabular::insertColumn(col_type const col, bool copy)
961 bool const ct = buffer().params().track_changes;
962 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
964 for (row_type r = 0; r < nrows(); ++r) {
965 cell_info[r].insert(cell_info[r].begin() + col + 1,
966 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
967 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
968 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
971 for (row_type r = 0; r < nrows(); ++r) {
972 // inherit line settings
973 idx_type const i = cellIndex(r, col + 1);
974 idx_type const j = cellIndex(r, col);
975 setBottomLine(i, bottomLine(j));
976 setTopLine(i, topLine(j));
977 setLeftLine(i, leftLine(j));
978 setRightLine(i, rightLine(j));
979 if (rightLine(i) && rightLine(j)) {
980 setRightLine(j, false);
984 column_info[col + 1].change.setInserted();
990 void Tabular::updateIndexes()
995 for (row_type row = 0; row < nrows(); ++row)
996 for (col_type column = 0; column < ncols(); ++column) {
997 if (!isPartOfMultiColumn(row, column)
998 && !isPartOfMultiRow(row, column))
1000 if (isPartOfMultiRow(row, column))
1001 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1003 cell_info[row][column].cellno = numberofcells - 1;
1006 rowofcell.resize(numberofcells);
1007 columnofcell.resize(numberofcells);
1009 // reset column and row of cells and update their width, alignment and ct status
1010 for (row_type row = 0; row < nrows(); ++row) {
1011 for (col_type column = 0; column < ncols(); ++column) {
1012 if (isPartOfMultiColumn(row, column)) {
1013 cell_info[row][column].inset->toggleMultiCol(true);
1016 cell_info[row][column].inset->toggleMultiCol(false);
1017 // columnofcell needs to be called before setting width and alignment
1018 // multirow cells inherit the width from the column width
1019 if (!isPartOfMultiRow(row, column)) {
1020 columnofcell[i] = column;
1023 setFixedWidth(row, column);
1024 if (isPartOfMultiRow(row, column)) {
1025 cell_info[row][column].inset->toggleMultiRow(true);
1028 cell_info[row][column].inset->toggleMultiRow(false);
1029 cell_info[row][column].inset->setContentAlignment(
1030 getAlignment(cellIndex(row, column)));
1031 if (buffer().params().track_changes) {
1032 if (row_info[row].change.changed())
1033 cell_info[row][column].inset->setChange(row_info[row].change);
1034 if (column_info[column].change.changed())
1035 cell_info[row][column].inset->setChange(column_info[column].change);
1043 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
1045 idx_type result = 0;
1046 for (col_type c = 0; c < ncols(); ++c)
1047 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1053 bool Tabular::topLine(idx_type const cell) const
1055 return cellInfo(cell).top_line;
1059 bool Tabular::bottomLine(idx_type const cell) const
1061 return cellInfo(cell).bottom_line;
1065 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1067 if (use_booktabs && !ignore_bt)
1069 return cellInfo(cell).left_line;
1073 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1075 if (use_booktabs && !ignore_bt)
1077 return cellInfo(cell).right_line;
1081 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1084 return make_pair(false, false);
1085 return make_pair(cellInfo(cell).top_line_ltrimmed,
1086 cellInfo(cell).top_line_rtrimmed);
1090 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1093 return make_pair(false, false);
1094 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1095 cellInfo(cell).bottom_line_rtrimmed);
1099 int Tabular::interRowSpace(row_type row) const
1101 if (!row || row >= nrows())
1104 int const interline_space = row_info[row - 1].interline_space_default ?
1105 default_line_space :
1106 row_info[row - 1].interline_space.inPixels(width());
1107 if (rowTopLine(row) && rowBottomLine(row - 1))
1108 return interline_space + WIDTH_OF_LINE;
1109 return interline_space;
1113 int Tabular::interColumnSpace(idx_type cell) const
1115 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1116 if (rightLine(cell) && nextcol < ncols()
1117 && leftLine(cellIndex(cellRow(cell), nextcol)))
1118 return WIDTH_OF_LINE;
1123 int Tabular::cellWidth(idx_type cell) const
1126 col_type const span = columnSpan(cell);
1127 col_type const col = cellColumn(cell);
1128 for(col_type c = col; c < col + span ; ++c)
1129 w += column_info[c].width;
1134 int Tabular::cellHeight(idx_type cell) const
1136 row_type const span = rowSpan(cell);
1137 row_type const row = cellRow(cell);
1139 for(row_type r = row; r < row + span ; ++r) {
1140 h += rowAscent(r) + rowDescent(r);
1141 if (r != row + span - 1)
1142 h += interRowSpace(r + 1);
1149 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1151 vector<int> max_dwidth(ncols(), 0);
1152 // collect max. fixed width of column
1153 map<col_type, int> max_pwidth;
1154 // collect max. variable width of column
1155 map<col_type, int> max_width;
1157 for(col_type c = 0; c < ncols(); ++c)
1158 for(row_type r = 0; r < nrows(); ++r) {
1159 idx_type const i = cellIndex(r, c);
1160 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1161 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1162 if (!getPWidth(i).zero())
1163 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1164 else if (!column_info[c].varwidth)
1165 max_width[c] = max(max_width[c], cell_info[r][c].width);
1168 // If we have a fixed tabular width, we take this into account
1169 Length tab_width = tabular_width;
1170 bool const tabularx = hasVarwidthColumn();
1171 if (tabularx && tab_width.zero())
1172 // If no tabular width is specified with X columns,
1173 // we use 100% colwidth
1174 tab_width = Length(100, Length::PCW);
1176 if (!tab_width.zero()) {
1177 restwidth = mi.base.inPixels(tab_width);
1178 // Subtract the fixed widths from the table width
1179 for (auto const w : max_pwidth)
1180 restwidth -= w.second;
1183 // If we have a fixed width, distribute the available table width
1184 // (minus the fixed widths) to the variable-width columns
1186 int restcols = ncols() - max_pwidth.size();
1187 if ((restwidth > 0) && (restcols != 0))
1188 vcolwidth = restwidth / restcols;
1190 // Now consider that some variable width columns exceed the vcolwidth
1191 if (vcolwidth > 0) {
1192 bool changed = false;
1193 for (auto const w : max_width) {
1194 if (tabularx || w.second > vcolwidth) {
1196 restwidth -= w.second;
1200 if (changed && restwidth > 0)
1201 vcolwidth = restwidth / restcols;
1204 bool update = false;
1205 // for each col get max of single col cells
1206 for(col_type c = 0; c < ncols(); ++c) {
1208 for(row_type r = 0; r < nrows(); ++r) {
1209 idx_type const i = cellIndex(r, c);
1210 if (columnSpan(i) == 1) {
1211 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1212 && cell_info[r][c].decimal_width != 0)
1213 new_width = max(new_width, cellInfo(i).width
1214 + max_dwidth[c] - cellInfo(i).decimal_width);
1215 else if (getPWidth(i).zero() && vcolwidth > 0) {
1216 if (tabularx && !column_info[c].varwidth)
1217 new_width = max(new_width, cellInfo(i).width);
1219 new_width = vcolwidth;
1221 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1223 new_width = max(new_width, cellInfo(i).width);
1227 if (column_info[c].width != new_width) {
1228 column_info[c].width = new_width;
1229 // Do not trigger update when no space is left for variable
1230 // columns, as this will loop
1231 update = tab_width.zero() || restwidth > 0;
1234 // update col widths to fit merged cells
1235 for(col_type c = 0; c < ncols(); ++c)
1236 for(row_type r = 0; r < nrows(); ++r) {
1237 idx_type const i = cellIndex(r, c);
1238 int const span = columnSpan(i);
1239 if (span == 1 || c > cellColumn(i))
1243 for(col_type j = c; j < c + span ; ++j)
1244 old_width += column_info[j].width;
1246 if (cellInfo(i).width > old_width) {
1247 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1248 // Do not trigger update when no space is left for variable
1249 // columns, as this will loop
1250 update = tab_width.zero() || restwidth > 0;
1258 int Tabular::width() const
1261 for (col_type c = 0; c < ncols(); ++c)
1262 width += column_info[c].width;
1267 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1270 col_type const col = cellColumn(cell);
1271 // set alignment for the whole row if we are not in a multicolumn cell,
1272 // exclude possible multicolumn cells in the row
1273 if (!isMultiColumn(cell)) {
1274 for (row_type r = 0; r < nrows(); ++r) {
1275 // only if the column has no width the multirow inherits the
1276 // alignment of the column, otherwise it is left aligned
1277 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1278 && !isMultiColumn(cellIndex(r, col))) {
1279 cell_info[r][col].alignment = align;
1280 cell_info[r][col].inset->setContentAlignment(align);
1282 if ((isMultiRow(cellIndex(r, col)) && has_width)
1283 && !isMultiColumn(cellIndex(r, col))) {
1284 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1285 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1288 column_info[col].alignment = align;
1289 docstring & dpoint = column_info[col].decimal_point;
1290 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1291 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1292 dpoint = tlang->decimalSeparator();
1295 cellInfo(cell).alignment = align;
1296 cellInset(cell)->setContentAlignment(align);
1301 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1304 if (!isMultiColumn(cell) || onlycolumn)
1305 column_info[cellColumn(cell)].valignment = align;
1307 cellInfo(cell).valignment = align;
1314 * Allow line and paragraph breaks for fixed width multicol/multirow cells
1315 * or disallow them, merge cell paragraphs and reset layout to standard
1316 * for variable width multicol cells.
1318 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1319 bool const fixedWidth, bool const multicol,
1320 bool const multirow)
1322 inset->toggleFixedWidth(fixedWidth);
1323 if (!multirow && (fixedWidth || !multicol))
1326 // merge all paragraphs to one
1327 BufferParams const & bp = cur.bv().buffer().params();
1328 while (inset->paragraphs().size() > 1)
1329 mergeParagraph(bp, inset->paragraphs(), 0);
1331 // This is relevant for multirows
1336 ParagraphList::iterator pit = inset->paragraphs().begin();
1337 for (; pit != inset->paragraphs().end(); ++pit) {
1338 for (pos_type j = 0; j != pit->size(); ++j) {
1339 if (pit->isNewline(j))
1340 pit->eraseChar(j, bp.track_changes);
1346 // undo information has already been recorded
1347 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1348 bp.documentClass().plainLayoutName());
1355 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1356 Length const & width)
1358 col_type const c = cellColumn(cell);
1360 column_info[c].p_width = width;
1361 // reset the vertical alignment to top if the fixed width
1362 // is removed or zero because only fixed width columns can
1363 // have a vertical alignment
1364 if (column_info[c].p_width.zero())
1365 column_info[c].valignment = LYX_VALIGN_TOP;
1366 for (row_type r = 0; r < nrows(); ++r) {
1367 idx_type const cidx = cellIndex(r, c);
1368 // because of multicolumns
1369 toggleFixedWidth(cur, cellInset(cidx).get(),
1370 !getPWidth(cidx).zero(), isMultiColumn(cidx),
1372 if (isMultiRow(cidx))
1373 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1375 // cur can become invalid after paragraphs were merged
1380 bool Tabular::setFixedWidth(row_type r, col_type c)
1382 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1383 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1384 || (multicol && !cell_info[r][c].p_width.zero());
1385 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1390 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1391 Length const & width)
1393 if (!isMultiColumn(cell))
1396 cellInfo(cell).p_width = width;
1397 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1398 isMultiColumn(cell), isMultiRow(cell));
1399 // cur can become invalid after paragraphs were merged
1405 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1407 column_info[cellColumn(cell)].varwidth = varwidth;
1412 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1414 cellInfo(cell).mroffset = mroffset;
1419 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1420 Tabular::Feature what)
1422 if (what == SET_SPECIAL_MULTICOLUMN)
1423 cellInfo(cell).align_special = special;
1425 column_info[cellColumn(cell)].align_special = special;
1429 void Tabular::setTopLine(idx_type i, bool line)
1431 cellInfo(i).top_line = line;
1435 void Tabular::setBottomLine(idx_type i, bool line)
1437 cellInfo(i).bottom_line = line;
1441 void Tabular::setTopLineLTrim(idx_type i, bool val)
1443 cellInfo(i).top_line_ltrimmed = val;
1447 void Tabular::setTopLineRTrim(idx_type i, bool val)
1449 cellInfo(i).top_line_rtrimmed = val;
1453 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1455 cellInfo(i).bottom_line_ltrimmed = val;
1459 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1461 cellInfo(i).bottom_line_rtrimmed = val;
1465 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1467 setTopLineLTrim(i, trim.first);
1468 setTopLineRTrim(i, trim.second);
1471 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1473 setBottomLineLTrim(i, trim.first);
1474 setBottomLineRTrim(i, trim.second);
1477 void Tabular::setLeftLine(idx_type cell, bool line)
1479 cellInfo(cell).left_line = line;
1483 void Tabular::setRightLine(idx_type cell, bool line)
1485 cellInfo(cell).right_line = line;
1488 bool Tabular::rowTopLine(row_type r) const
1490 bool all_rows_set = true;
1491 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1492 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1493 return all_rows_set;
1497 bool Tabular::rowBottomLine(row_type r) const
1499 bool all_rows_set = true;
1500 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1501 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1502 return all_rows_set;
1506 bool Tabular::columnLeftLine(col_type c) const
1513 for (row_type r = 0; r < nrows(); ++r) {
1514 idx_type const i = cellIndex(r, c);
1515 if (c == cellColumn(i)) {
1517 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1518 if (cellInfo(i).left_line || right)
1522 return 2 * nrows_left >= total;
1526 bool Tabular::columnRightLine(col_type c) const
1531 int nrows_right = 0;
1533 for (row_type r = 0; r < nrows(); ++r) {
1534 idx_type i = cellIndex(r, c);
1535 if (c == cellColumn(i) + columnSpan(i) - 1) {
1537 bool left = (c + 1 < ncols()
1538 && cellInfo(cellIndex(r, c + 1)).left_line)
1539 || c + 1 == ncols();
1540 if (cellInfo(i).right_line && left)
1544 return 2 * nrows_right >= total;
1548 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1550 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1551 return cellInfo(cell).alignment;
1553 return column_info[cellColumn(cell)].alignment;
1558 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1560 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1561 return cellInfo(cell).valignment;
1562 return column_info[cellColumn(cell)].valignment;
1566 int Tabular::offsetVAlignment() const
1568 // for top-alignment the first horizontal table line must be exactly at
1569 // the position of the base line of the surrounding text line
1570 // for bottom alignment, the same is for the last table line
1571 int offset_valign = 0;
1572 switch (tabular_valignment) {
1573 case Tabular::LYX_VALIGN_BOTTOM:
1574 offset_valign = rowAscent(0) - height();
1576 case Tabular::LYX_VALIGN_MIDDLE:
1577 offset_valign = (- height()) / 2 + rowAscent(0);
1579 case Tabular::LYX_VALIGN_TOP:
1580 offset_valign = rowAscent(0);
1583 return offset_valign;
1587 Length const Tabular::getPWidth(idx_type cell) const
1589 if (isMultiColumn(cell))
1590 return cellInfo(cell).p_width;
1591 return column_info[cellColumn(cell)].p_width;
1595 Length const Tabular::getMROffset(idx_type cell) const
1597 return cellInfo(cell).mroffset;
1601 int Tabular::textHOffset(idx_type cell) const
1603 // the LaTeX Way :-(
1604 int x = WIDTH_OF_LINE;
1606 int const w = cellWidth(cell) - cellInfo(cell).width;
1608 switch (getAlignment(cell)) {
1609 case LYX_ALIGN_CENTER:
1612 case LYX_ALIGN_RIGHT:
1615 case LYX_ALIGN_DECIMAL: {
1616 // we center when no decimal point
1617 if (cellInfo(cell).decimal_width == 0) {
1621 col_type const c = cellColumn(cell);
1622 int max_dhoffset = 0;
1623 for(row_type r = 0; r < row_info.size() ; ++r) {
1624 idx_type const i = cellIndex(r, c);
1625 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1626 && cellInfo(i).decimal_width != 0)
1627 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1629 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1632 // LYX_ALIGN_LEFT: nothing :-)
1640 int Tabular::textVOffset(idx_type cell) const
1642 int voffset = cellInfo(cell).voffset;
1643 if (isMultiRow(cell)) {
1644 row_type const row = cellRow(cell);
1645 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1651 Tabular::idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1654 idx_type const numcells = numberOfCellsInRow(row);
1655 // we check against numcells to make sure we do not crash if all the
1656 // cells are multirow (bug #7535), but in that case our return value
1657 // is really invalid, i.e., it is NOT the first cell in the row. but
1658 // i do not know what to do here. (rgh)
1659 while (c < numcells - 1
1660 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1661 || (ct && column_info[c].change.deleted())))
1663 return cell_info[row][c].cellno;
1667 Tabular::idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1669 col_type c = ncols() - 1;
1670 // of course we check against 0 so we don't crash. but we have the same
1671 // problem as in the previous routine: if all the cells are part of a
1672 // multirow or part of a multi column, then our return value is invalid.
1674 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1675 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1676 || (ct && column_info[c].change.deleted())))
1678 return cell_info[row][c].cellno;
1682 Tabular::row_type Tabular::getFirstRow(bool const ct) const
1687 // exclude deleted rows if ct == true
1688 while (r < nrows() && row_info[r].change.deleted())
1694 Tabular::row_type Tabular::getLastRow(bool const ct) const
1696 row_type r = nrows() - 1;
1699 // exclude deleted rows if ct == true
1700 while (r > 0 && row_info[r].change.deleted())
1706 Tabular::row_type Tabular::cellRow(idx_type cell) const
1708 if (cell >= numberofcells)
1712 return rowofcell[cell];
1716 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1718 if (cell >= numberofcells)
1722 return columnofcell[cell];
1726 void Tabular::write(ostream & os) const
1730 << write_attribute("version", 3)
1731 << write_attribute("rows", nrows())
1732 << write_attribute("columns", ncols())
1734 // global longtable options
1736 << write_attribute("rotate", rotate)
1737 << write_attribute("booktabs", use_booktabs)
1738 << write_attribute("islongtable", is_long_tabular)
1739 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1740 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1741 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1742 << write_attribute("headTopDL", endhead.topDL)
1743 << write_attribute("headBottomDL", endhead.bottomDL)
1744 << write_attribute("footTopDL", endfoot.topDL)
1745 << write_attribute("footBottomDL", endfoot.bottomDL)
1746 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1747 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1748 << write_attribute("lastFootEmpty", endlastfoot.empty);
1749 // longtables cannot be aligned vertically
1750 if (!is_long_tabular)
1751 os << write_attribute("tabularvalignment", tabular_valignment);
1752 os << write_attribute("tabularwidth", tabular_width);
1753 if (is_long_tabular)
1754 os << write_attribute("longtabularalignment", longtabular_alignment);
1756 for (col_type c = 0; c < ncols(); ++c) {
1758 << write_attribute("alignment", column_info[c].alignment);
1759 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1760 os << write_attribute("decimal_point", column_info[c].decimal_point);
1761 os << write_attribute("change", column_info[c].change, buffer().params())
1762 << write_attribute("valignment", column_info[c].valignment)
1763 << write_attribute("width", column_info[c].p_width.asString())
1764 << write_attribute("varwidth", column_info[c].varwidth)
1765 << write_attribute("special", column_info[c].align_special)
1768 for (row_type r = 0; r < nrows(); ++r) {
1769 static const string def("default");
1771 if (row_info[r].top_space_default)
1772 os << write_attribute("topspace", def);
1774 os << write_attribute("topspace", row_info[r].top_space);
1775 if (row_info[r].bottom_space_default)
1776 os << write_attribute("bottomspace", def);
1778 os << write_attribute("bottomspace", row_info[r].bottom_space);
1779 if (row_info[r].interline_space_default)
1780 os << write_attribute("interlinespace", def);
1782 os << write_attribute("interlinespace", row_info[r].interline_space);
1783 os << write_attribute("change", row_info[r].change, buffer().params())
1784 << write_attribute("endhead", row_info[r].endhead)
1785 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1786 << write_attribute("endfoot", row_info[r].endfoot)
1787 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1788 << write_attribute("newpage", row_info[r].newpage)
1789 << write_attribute("caption", row_info[r].caption)
1791 for (col_type c = 0; c < ncols(); ++c) {
1793 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1794 << write_attribute("multirow", cell_info[r][c].multirow)
1795 << write_attribute("mroffset", cell_info[r][c].mroffset)
1796 << write_attribute("alignment", cell_info[r][c].alignment)
1797 << write_attribute("valignment", cell_info[r][c].valignment)
1798 << write_attribute("topline", cell_info[r][c].top_line)
1799 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1800 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1801 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1802 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1803 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1804 << write_attribute("leftline", cell_info[r][c].left_line)
1805 << write_attribute("rightline", cell_info[r][c].right_line)
1806 << write_attribute("rotate", cell_info[r][c].rotate)
1807 << write_attribute("usebox", cell_info[r][c].usebox)
1808 << write_attribute("width", cell_info[r][c].p_width)
1809 << write_attribute("special", cell_info[r][c].align_special)
1811 os << "\\begin_inset ";
1812 cell_info[r][c].inset->write(os);
1813 os << "\n\\end_inset\n"
1815 // FIXME This can be removed again once the mystery
1816 // crash has been resolved.
1821 os << "</lyxtabular>\n";
1825 void Tabular::read(Lexer & lex)
1828 istream & is = lex.getStream();
1830 l_getline(is, line);
1831 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1832 LASSERT(false, return);
1836 if (!getTokenValue(line, "version", version))
1838 LATTEST(version >= 2);
1841 if (!getTokenValue(line, "rows", rows_arg))
1844 if (!getTokenValue(line, "columns", columns_arg))
1846 init(buffer_, rows_arg, columns_arg);
1847 l_getline(is, line);
1848 if (!prefixIs(line, "<features")) {
1849 lyxerr << "Wrong tabular format (expected <features ...> got"
1850 << line << ')' << endl;
1853 getTokenValue(line, "rotate", rotate);
1854 getTokenValue(line, "booktabs", use_booktabs);
1855 getTokenValue(line, "islongtable", is_long_tabular);
1856 getTokenValue(line, "tabularvalignment", tabular_valignment);
1857 getTokenValue(line, "tabularwidth", tabular_width);
1858 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1859 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1860 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1861 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1862 getTokenValue(line, "headTopDL", endhead.topDL);
1863 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1864 getTokenValue(line, "footTopDL", endfoot.topDL);
1865 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1866 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1867 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1868 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1870 for (col_type c = 0; c < ncols(); ++c) {
1872 if (!prefixIs(line,"<column")) {
1873 lyxerr << "Wrong tabular format (expected <column ...> got"
1874 << line << ')' << endl;
1877 getTokenValue(line, "alignment", column_info[c].alignment);
1878 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1879 getTokenValue(line, "valignment", column_info[c].valignment);
1880 getTokenValue(line, "width", column_info[c].p_width);
1881 getTokenValue(line, "special", column_info[c].align_special);
1882 getTokenValue(line, "varwidth", column_info[c].varwidth);
1883 getTokenValue(line, "change", column_info[c].change, buffer().params());
1886 for (row_type i = 0; i < nrows(); ++i) {
1887 l_getline(is, line);
1888 if (!prefixIs(line, "<row")) {
1889 lyxerr << "Wrong tabular format (expected <row ...> got"
1890 << line << ')' << endl;
1893 getTokenValue(line, "topspace", row_info[i].top_space,
1894 row_info[i].top_space_default);
1895 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1896 row_info[i].bottom_space_default);
1897 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1898 row_info[i].interline_space_default);
1899 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1900 getTokenValue(line, "endhead", row_info[i].endhead);
1901 getTokenValue(line, "endfoot", row_info[i].endfoot);
1902 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1903 getTokenValue(line, "newpage", row_info[i].newpage);
1904 getTokenValue(line, "caption", row_info[i].caption);
1905 getTokenValue(line, "change", row_info[i].change, buffer().params());
1906 for (col_type j = 0; j < ncols(); ++j) {
1907 l_getline(is, line);
1908 if (!prefixIs(line, "<cell")) {
1909 lyxerr << "Wrong tabular format (expected <cell ...> got"
1910 << line << ')' << endl;
1913 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1914 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1915 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1916 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1917 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1918 getTokenValue(line, "topline", cell_info[i][j].top_line);
1919 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1920 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1921 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1922 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1923 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1924 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1925 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1926 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1927 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1928 getTokenValue(line, "width", cell_info[i][j].p_width);
1930 getTokenValue(line, "special", cell_info[i][j].align_special);
1931 l_getline(is, line);
1932 if (prefixIs(line, "\\begin_inset")) {
1933 cell_info[i][j].inset->setBuffer(*buffer_);
1934 cell_info[i][j].inset->read(lex);
1935 l_getline(is, line);
1937 if (!prefixIs(line, "</cell>")) {
1938 lyxerr << "Wrong tabular format (expected </cell> got"
1939 << line << ')' << endl;
1943 l_getline(is, line);
1944 if (!prefixIs(line, "</row>")) {
1945 lyxerr << "Wrong tabular format (expected </row> got"
1946 << line << ')' << endl;
1950 while (!prefixIs(line, "</lyxtabular>")) {
1951 l_getline(is, line);
1957 bool Tabular::isMultiColumn(idx_type cell) const
1959 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1960 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1964 bool Tabular::hasMultiColumn(col_type c) const
1966 for (row_type r = 0; r < nrows(); ++r) {
1967 if (isMultiColumn(cellIndex(r, c)))
1974 bool Tabular::hasVarwidthColumn() const
1976 for (col_type c = 0; c < ncols(); ++c) {
1977 if (column_info[c].varwidth)
1984 bool Tabular::isVTypeColumn(col_type c) const
1986 for (row_type r = 0; r < nrows(); ++r) {
1987 idx_type idx = cellIndex(r, c);
1988 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1995 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1997 return cell_info[cellRow(cell)][cellColumn(cell)];
2001 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2003 return cell_info[cellRow(cell)][cellColumn(cell)];
2007 Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2008 bool const right_border)
2010 idx_type const col = cellColumn(cell);
2011 idx_type const row = cellRow(cell);
2012 for (idx_type i = 0; i < number; ++i)
2013 unsetMultiRow(cellIndex(row, col + i));
2015 // unsetting of multirow may have invalidated cell index
2016 cell = cellIndex(row, col);
2017 CellData & cs = cellInfo(cell);
2018 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2019 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2020 cs.alignment = column_info[col].alignment;
2021 setRightLine(cell, right_border);
2022 // non-fixed width multicolumns cannot have multiple paragraphs
2023 if (getPWidth(cell).zero()) {
2024 toggleFixedWidth(cur, cellInset(cell).get(),
2025 !getPWidth(cell).zero(), isMultiColumn(cell),
2027 // cur can become invalid after paragraphs were merged
2031 idx_type lastcell = cellIndex(row, col + number - 1);
2032 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2033 CellData & cs1 = cellInfo(cell + i);
2034 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2035 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2043 bool Tabular::isMultiRow(idx_type cell) const
2045 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2046 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2049 bool Tabular::hasMultiRow(row_type r) const
2051 for (col_type c = 0; c < ncols(); ++c) {
2052 if (isMultiRow(cellIndex(r, c)))
2058 Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2059 bool const bottom_border,
2060 LyXAlignment const halign)
2062 idx_type const col = cellColumn(cell);
2063 idx_type const row = cellRow(cell);
2064 for (idx_type i = 0; i < number; ++i)
2065 unsetMultiColumn(cellIndex(row + i, col));
2067 // unsetting of multirow may have invalidated cell index
2068 cell = cellIndex(row, col);
2069 CellData & cs = cellInfo(cell);
2070 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2071 cs.valignment = LYX_VALIGN_MIDDLE;
2072 // the horizontal alignment of multirow cells can only
2073 // be changed for the whole table row,
2074 // support changing this only for the multirow cell can be done via
2076 if (getPWidth(cell).zero())
2077 cs.alignment = halign;
2079 cs.alignment = LYX_ALIGN_LEFT;
2081 // Multirows cannot have multiple paragraphs
2082 if (getPWidth(cell).zero()) {
2083 toggleFixedWidth(cur, cellInset(cell).get(),
2084 !getPWidth(cell).zero(),
2085 isMultiColumn(cell), isMultiRow(cell));
2086 // cur can become invalid after paragraphs were merged
2090 // set the bottom line of the last selected cell
2091 setBottomLine(cell, bottom_border);
2093 for (idx_type i = 1; i < number; ++i) {
2094 CellData & cs1 = cell_info[row + i][col];
2095 cs1.multirow = CELL_PART_OF_MULTIROW;
2096 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2104 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
2106 row_type const row = cellRow(cell);
2107 col_type const col = cellColumn(cell);
2109 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2116 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
2118 col_type const column = cellColumn(cell);
2119 col_type row = cellRow(cell) + 1;
2120 while (row < nrows() && isPartOfMultiRow(row, column))
2123 return row - cellRow(cell);
2127 void Tabular::unsetMultiColumn(idx_type cell)
2129 if (!isMultiColumn(cell))
2132 row_type const row = cellRow(cell);
2133 col_type const col = cellColumn(cell);
2134 row_type const span = columnSpan(cell);
2135 for (col_type c = 0; c < span; ++c) {
2136 // in the table dialog the lines are set in every case
2137 // when unsetting a multicolumn this leads to an additional right
2138 // line for every cell that was part of the former multicolumn cell,
2139 // except if the cell is in the last column
2140 // therefore remove this line
2141 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2142 && (col + c) < (col + span - 1))
2143 cell_info[row][col + c].right_line = false;
2144 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2150 void Tabular::unsetMultiRow(idx_type cell)
2152 if (!isMultiRow(cell))
2155 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2156 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2157 row_type const row = cellRow(cell);
2158 col_type const col = cellColumn(cell);
2159 row_type const span = rowSpan(cell);
2160 for (row_type r = 0; r < span; ++r)
2161 cell_info[row + r][col].multirow = CELL_NORMAL;
2166 void Tabular::setRotateCell(idx_type cell, int value)
2168 cellInfo(cell).rotate = value;
2172 int Tabular::getRotateCell(idx_type cell) const
2174 return cellInfo(cell).rotate;
2178 bool Tabular::needRotating() const
2180 if (rotate && !is_long_tabular)
2182 for (row_type r = 0; r < nrows(); ++r)
2183 for (col_type c = 0; c < ncols(); ++c)
2184 if (cell_info[r][c].rotate != 0)
2190 bool Tabular::isLastCell(idx_type cell) const
2192 if (cell + 1 < numberofcells)
2198 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2200 if (cellRow(cell) == 0)
2203 col_type const col = cellColumn(cell);
2204 row_type r = cellRow(cell) - 1;
2205 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2208 return cell_info[r][col].cellno;
2212 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2214 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2215 if (nextrow < nrows())
2216 return cell_info[nextrow][cellColumn(cell)].cellno;
2221 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2223 LASSERT(column != npos && column < ncols(), column = 0);
2224 LASSERT(row != npos && row < nrows(), row = 0);
2225 return cell_info[row][column].cellno;
2229 void Tabular::setUsebox(idx_type cell, BoxType type)
2231 cellInfo(cell).usebox = type;
2235 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2237 if (getRotateCell(cell) == 0
2238 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2239 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2241 if (cellInfo(cell).usebox > 1)
2242 return cellInfo(cell).usebox;
2243 return useBox(cell);
2248 // This are functions used for the longtable support
2250 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2256 row_info[row].endfirsthead = flag;
2260 row_info[row].endhead = flag;
2265 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2268 hd.set = haveLTHead();
2269 return row_info[row].endhead;
2273 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2276 hd.set = haveLTFirstHead();
2277 return row_info[row].endfirsthead;
2281 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2287 row_info[row].endlastfoot = flag;
2291 row_info[row].endfoot = flag;
2296 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2299 fd.set = haveLTFoot();
2300 return row_info[row].endfoot;
2304 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2307 fd.set = haveLTLastFoot();
2308 return row_info[row].endlastfoot;
2312 void Tabular::setLTNewPage(row_type row, bool what)
2314 row_info[row].newpage = what;
2318 bool Tabular::getLTNewPage(row_type row) const
2320 return row_info[row].newpage;
2324 bool Tabular::haveLTHead(bool withcaptions) const
2326 if (!is_long_tabular)
2328 for (row_type i = 0; i < nrows(); ++i)
2329 if (row_info[i].endhead &&
2330 (withcaptions || !row_info[i].caption))
2336 bool Tabular::haveLTFirstHead(bool withcaptions) const
2338 if (!is_long_tabular || endfirsthead.empty)
2340 for (row_type r = 0; r < nrows(); ++r)
2341 if (row_info[r].endfirsthead &&
2342 (withcaptions || !row_info[r].caption))
2348 bool Tabular::haveLTFoot(bool withcaptions) const
2350 if (!is_long_tabular)
2352 for (row_type r = 0; r < nrows(); ++r)
2353 if (row_info[r].endfoot &&
2354 (withcaptions || !row_info[r].caption))
2360 bool Tabular::haveLTLastFoot(bool withcaptions) const
2362 if (!is_long_tabular || endlastfoot.empty)
2364 for (row_type r = 0; r < nrows(); ++r)
2365 if (row_info[r].endlastfoot &&
2366 (withcaptions || !row_info[r].caption))
2372 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2374 idx_type i = getFirstCellInRow(row);
2376 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2377 setTopLine(i, false);
2378 setBottomLine(i, false);
2379 setLeftLine(i, false);
2380 setRightLine(i, false);
2381 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2382 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2383 setLTHead(row, true, endfirsthead, true);
2384 row_info[row].endfirsthead = true;
2387 unsetMultiColumn(i);
2388 // When unsetting a caption row, also all existing
2389 // captions in this row must be dissolved.
2391 row_info[row].caption = what;
2396 bool Tabular::ltCaption(row_type row) const
2398 return row_info[row].caption;
2402 bool Tabular::haveLTCaption(CaptionType captiontype) const
2404 if (!is_long_tabular)
2406 for (row_type r = 0; r < nrows(); ++r) {
2407 if (row_info[r].caption) {
2408 switch (captiontype) {
2409 case CAPTION_FIRSTHEAD:
2410 if (row_info[r].endfirsthead)
2414 if (row_info[r].endhead)
2418 if (row_info[r].endfoot)
2421 case CAPTION_LASTFOOT:
2422 if (row_info[r].endlastfoot)
2434 // end longtable support functions
2436 void Tabular::setRowAscent(row_type row, int height)
2438 if (row >= nrows() || row_info[row].ascent == height)
2440 row_info[row].ascent = height;
2444 void Tabular::setRowDescent(row_type row, int height)
2446 if (row >= nrows() || row_info[row].descent == height)
2448 row_info[row].descent = height;
2452 int Tabular::rowAscent(row_type row) const
2454 LASSERT(row < nrows(), row = 0);
2455 return row_info[row].ascent;
2459 int Tabular::rowDescent(row_type row) const
2461 LASSERT(row < nrows(), row = 0);
2462 return row_info[row].descent;
2466 int Tabular::height() const
2469 for (row_type row = 0; row < nrows(); ++row)
2470 height += rowAscent(row) + rowDescent(row) +
2476 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2478 LASSERT(row < nrows(), return false);
2479 LASSERT(column < ncols(), return false);
2480 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2484 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2486 LASSERT(row < nrows(), return false);
2487 LASSERT(column < ncols(), return false);
2488 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2492 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns,
2493 list<col_type> logical_columns) const
2495 // we only output complete row lines and the 1st row here, the rest
2496 // is done in Tabular::TeXBottomHLine(...)
2498 // get for each column the topline (if any)
2499 map<col_type, bool> topline, topltrims, toprtrims;
2501 bool have_trims = false;
2502 for (auto const & c : columns) {
2503 topline[c] = topLine(cellIndex(row, c));
2504 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2505 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2506 // If cell is part of a multirow and not the first cell of the
2507 // multirow, no line must be drawn.
2509 if (isMultiRow(cellIndex(row, c))
2510 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2512 topltrims[c] = false;
2513 toprtrims[c] = false;
2515 // copy trimming to multicolumn parts
2516 if (isPartOfMultiColumn(row, c)) {
2517 topltrims[c] = topltrims[c-1];
2518 toprtrims[c] = toprtrims[c-1];
2520 if (topline.find(c) != topline.end() && topline.find(c)->second)
2522 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2523 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2527 // do nothing if empty first row, or incomplete row line after
2528 row_type first = getFirstRow(!buffer().params().output_changes);
2529 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2532 // Is this the actual first row (excluding longtable caption row)?
2533 bool const realfirstrow = (row == first
2534 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2536 // only output complete row lines and the 1st row's clines
2537 if (nset == columns.size() && !have_trims) {
2539 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2543 } else if (realfirstrow || have_trims) {
2544 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2546 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2547 std::list<col_type>::const_iterator it2 = columns.begin();
2548 // We need to iterate over the logical columns here, but take care for
2550 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2555 if (topline.find(c)->second) {
2556 col_type offset = 0;
2557 for (col_type j = 0 ; j < c; ++j)
2558 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2560 // If the two iterators differ, we are in bidi with swapped columns
2561 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2562 while (isPartOfMultiColumn(row, c))
2565 if (topltrims.find(c) != topltrims.end()
2566 && topltrims.find(c)->second)
2568 col_type cstart = c;
2569 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2570 if (isMultiColumn(cellIndex(row, c))
2571 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2573 if (c > cstart && topltrims.find(c) != topltrims.end()
2574 && topltrims.find(c)->second) {
2575 if (!isPartOfMultiColumn(row, c))
2578 } else if (toprtrims.find(c) != toprtrims.end()
2579 && toprtrims.find(c)->second)
2583 for (col_type j = cstart ; j < c ; ++j)
2584 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2586 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2587 if (toprtrims.find(c) != toprtrims.end()
2588 && toprtrims.find(c)->second)
2593 os << "(" << trim << ")";
2594 if (firstcol > lastcol)
2595 // This can happen with bidi (swapped columns)
2596 os << "{" << lastcol << '-' << firstcol << "}";
2598 os << "{" << firstcol << '-' << lastcol << "}";
2599 if (c == columns.size() - 1)
2609 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns,
2610 list<col_type> logical_columns) const
2612 // we output bottomlines of row r and the toplines of row r+1
2613 // if the latter do not span the whole tabular
2615 // get the bottomlines of row r, and toplines in next row
2616 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2617 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2618 bool nextrowset = true;
2619 for (auto const & c : columns) {
2620 idx_type const idx = cellIndex(row, c);
2621 bottomline[c] = bottomLine(cellIndex(row, c));
2622 bottomltrims[c] = bottomLineTrim(idx).first;
2623 bottomrtrims[c] = bottomLineTrim(idx).second;
2624 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2625 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2626 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2627 // If cell is part of a multirow and not the last cell of the
2628 // multirow, no line must be drawn.
2630 if (isMultiRow(cellIndex(row, c))
2631 && isMultiRow(cellIndex(row + 1, c))
2632 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2633 bottomline[c] = false;
2635 bottomltrims[c] = false;
2636 bottomrtrims[c] = false;
2637 topltrims[c] = false;
2638 toprtrims[c] = false;
2640 // copy trimming in multicolumn parts
2641 if (isPartOfMultiColumn(row, c)) {
2642 topltrims[c] = topltrims[c-1];
2643 toprtrims[c] = toprtrims[c-1];
2644 bottomltrims[c] = bottomltrims[c-1];
2645 bottomrtrims[c] = bottomrtrims[c-1];
2648 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2651 // combine this row's bottom lines and next row's toplines if necessary
2653 bool have_trims = false;
2654 for (auto const & c : columns) {
2656 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2657 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2658 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2659 bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2660 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2661 if (bottomline.find(c)->second)
2663 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2664 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2668 // do nothing if empty, OR incomplete row line with a topline in next row
2669 if (nset == 0 || (nextrowset && nset != columns.size()))
2672 if (nset == columns.size() && !have_trims) {
2674 os << (lastrow ? "\\bottomrule" : "\\midrule");
2678 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2680 std::list<col_type>::const_iterator it1 = logical_columns.begin();
2681 std::list<col_type>::const_iterator it2 = columns.begin();
2682 // We need to iterate over the logical columns here, but take care for
2684 for (; it1 != logical_columns.end() && it2 != columns.end(); ++it1, ++it2) {
2689 if (bottomline.find(c)->second) {
2690 col_type offset = 0;
2691 for (col_type j = 0 ; j < c; ++j)
2692 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2694 // If the two iterators differ, we are in bidi with swapped columns
2695 col_type firstcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2696 while (isPartOfMultiColumn(row, c))
2699 if (bottomltrims.find(c) != bottomltrims.end()
2700 && bottomltrims.find(c)->second)
2702 col_type cstart = c;
2703 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2704 if (isMultiColumn(cellIndex(row, c))
2706 && isPartOfMultiColumn(row, c + 1))
2709 && bottomltrims.find(c) != bottomltrims.end()
2710 && bottomltrims.find(c)->second) {
2711 if (!isPartOfMultiColumn(row, c))
2714 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2715 && bottomrtrims.find(c)->second)
2719 for (col_type j = cstart ; j < c ; ++j)
2720 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2722 col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
2723 if (bottomrtrims.find(c) != bottomrtrims.end()
2724 && bottomrtrims.find(c)->second)
2729 os << "(" << trim << ")";
2730 if (firstcol > lastcol)
2731 // This can happen with bidi (swapped columns)
2732 os << "{" << lastcol << '-' << firstcol << "}";
2734 os << "{" << firstcol << '-' << lastcol << "}";
2735 if (c == columns.size() - 1)
2745 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2746 bool & ismulticol, bool & ismultirow,
2747 bool const bidi) const
2749 row_type const r = cellRow(cell);
2750 if (is_long_tabular && row_info[r].caption)
2753 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2754 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2755 // figure out how to set the lines
2756 // we always set double lines to the right of the cell
2757 // or left in bidi RTL, respectively.
2758 col_type const c = cellColumn(cell);
2759 col_type const nextcol = c + columnSpan(cell);
2760 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2761 bool colright = columnRightLine(c);
2762 bool colleft = columnLeftLine(c);
2763 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2764 bool nextcellleft = nextcol < ncols()
2765 && leftLine(cellIndex(r, nextcol));
2766 bool coldouble = colright && nextcolleft;
2767 bool celldouble = rightLine(cell) && nextcellleft;
2769 ismulticol = (isMultiColumn(cell)
2770 || (c == 0 && colleft != leftLine(cell))
2771 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2772 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2773 || (coldouble != celldouble))
2776 // we center in multicol when no decimal point
2778 docstring const align_d = column_info[c].decimal_point;
2779 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2780 bool const nosep = !dit;
2781 ismulticol |= nosep;
2782 celldouble &= nosep;
2785 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2786 int latexcolspan = columnSpan(cell);
2787 for (col_type col = c; col < c + columnSpan(cell); ++col)
2788 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2792 os << "\\multicolumn{" << latexcolspan << "}{";
2793 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2794 || (!bidi && c == 0 && leftLine(cell))))
2796 if (bidi && celldouble)
2797 // add extra vertical line if we want a double one
2799 if (!cellInfo(cell).align_special.empty()) {
2800 os << cellInfo(cell).align_special;
2802 if (!getPWidth(cell).zero()) {
2804 case LYX_ALIGN_LEFT:
2805 os << ">{\\raggedright}";
2807 case LYX_ALIGN_RIGHT:
2808 os << ">{\\raggedleft}";
2810 case LYX_ALIGN_CENTER:
2811 os << ">{\\centering}";
2817 case LYX_VALIGN_TOP:
2820 case LYX_VALIGN_MIDDLE:
2823 case LYX_VALIGN_BOTTOM:
2828 << from_ascii(getPWidth(cell).asLatexString())
2832 case LYX_ALIGN_LEFT:
2835 case LYX_ALIGN_RIGHT:
2842 } // end if else !getPWidth
2843 } // end if else !cellinfo_of_cell
2844 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2846 if (!bidi && celldouble)
2847 // add extra vertical line if we want a double one
2850 } // end if ismulticol
2852 // we only need code for the first multirow cell
2853 ismultirow = isMultiRow(cell);
2855 os << "\\multirow{" << rowSpan(cell) << "}{";
2856 if (!getPWidth(cell).zero())
2857 os << from_ascii(getPWidth(cell).asLatexString());
2859 // we need to set a default value
2862 if (!getMROffset(cell).zero())
2863 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2865 } // end if ismultirow
2867 if (getRotateCell(cell) != 0)
2868 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2870 if (getUsebox(cell) == BOX_PARBOX) {
2873 case LYX_VALIGN_TOP:
2876 case LYX_VALIGN_MIDDLE:
2879 case LYX_VALIGN_BOTTOM:
2883 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2885 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2886 os << "\\begin{minipage}[";
2888 case LYX_VALIGN_TOP:
2891 case LYX_VALIGN_MIDDLE:
2894 case LYX_VALIGN_BOTTOM:
2898 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2900 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2901 os << "\\begin{varwidth}[";
2903 case LYX_VALIGN_TOP:
2906 case LYX_VALIGN_MIDDLE:
2909 case LYX_VALIGN_BOTTOM:
2913 os << "]{\\linewidth}\n";
2918 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2919 bool ismulticol, bool ismultirow) const
2921 row_type const r = cellRow(cell);
2922 if (is_long_tabular && row_info[r].caption)
2926 if (getUsebox(cell) == BOX_PARBOX)
2928 else if (getUsebox(cell) == BOX_MINIPAGE)
2929 os << breakln << "\\end{minipage}";
2930 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2931 os << breakln << "\\end{varwidth}";
2932 if (getRotateCell(cell) != 0)
2933 os << breakln << "\\end{turn}";
2941 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2942 OutputParams const & runparams,
2943 list<col_type> columns,
2944 list<col_type> logical_columns) const
2946 if (!is_long_tabular)
2950 // output caption which is in no header or footer
2951 if (haveLTCaption()) {
2952 for (row_type r = 0; r < nrows(); ++r) {
2953 if (row_info[r].caption &&
2954 !row_info[r].endfirsthead && !row_info[r].endhead &&
2955 !row_info[r].endfoot && !row_info[r].endlastfoot)
2956 TeXRow(os, r, runparams, columns, logical_columns);
2959 // output first header info
2960 if (haveLTFirstHead()) {
2961 if (endfirsthead.topDL)
2963 for (row_type r = 0; r < nrows(); ++r) {
2964 if (row_info[r].endfirsthead)
2965 TeXRow(os, r, runparams, columns, logical_columns);
2967 if (endfirsthead.bottomDL)
2969 os << "\\endfirsthead\n";
2971 // output header info
2973 if (endfirsthead.empty && !haveLTFirstHead())
2974 os << "\\endfirsthead\n";
2977 for (row_type r = 0; r < nrows(); ++r) {
2978 if (row_info[r].endhead)
2979 TeXRow(os, r, runparams, columns, logical_columns);
2981 if (endhead.bottomDL)
2983 os << "\\endhead\n";
2985 // output footer info
2989 for (row_type r = 0; r < nrows(); ++r) {
2990 if (row_info[r].endfoot)
2991 TeXRow(os, r, runparams, columns, logical_columns);
2993 if (endfoot.bottomDL)
2995 os << "\\endfoot\n";
2996 if (endlastfoot.empty && !haveLTLastFoot())
2997 os << "\\endlastfoot\n";
2999 // output lastfooter info
3000 if (haveLTLastFoot()) {
3001 if (endlastfoot.topDL)
3003 for (row_type r = 0; r < nrows(); ++r) {
3004 if (row_info[r].endlastfoot)
3005 TeXRow(os, r, runparams, columns, logical_columns);
3007 if (endlastfoot.bottomDL)
3009 os << "\\endlastfoot\n";
3014 bool Tabular::isValidRow(row_type row) const
3016 if (!is_long_tabular)
3018 return !row_info[row].endhead && !row_info[row].endfirsthead
3019 && !row_info[row].endfoot && !row_info[row].endlastfoot
3020 && !row_info[row].caption;
3024 void Tabular::TeXRow(otexstream & os, row_type row,
3025 OutputParams const & runparams,
3026 list<col_type> columns, list<col_type> logical_columns) const
3028 //output the top line
3029 TeXTopHLine(os, row, columns, logical_columns);
3031 if (row_info[row].top_space_default) {
3033 os << "\\addlinespace\n";
3035 os << "\\noalign{\\vskip\\doublerulesep}\n";
3036 } else if(!row_info[row].top_space.zero()) {
3038 os << "\\addlinespace["
3039 << from_ascii(row_info[row].top_space.asLatexString())
3042 os << "\\noalign{\\vskip"
3043 << from_ascii(row_info[row].top_space.asLatexString())
3047 bool ismulticol = false;
3048 bool ismultirow = false;
3050 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3051 // Luabibdi (used by LuaTeX) behaves like classic
3052 bool const bidi_rtl =
3053 runparams.local_font->isRightToLeft()
3054 && runparams.useBidiPackage();
3055 bool const ct = !buffer().params().output_changes;
3057 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3059 for (auto const & c : columns) {
3060 if (isPartOfMultiColumn(row, c))
3063 idx_type cell = cellIndex(row, c);
3065 if (isPartOfMultiRow(row, c)
3066 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3067 if (cell != lastcell)
3072 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3073 InsetTableCell const * inset = cellInset(cell);
3075 Paragraph const & par = inset->paragraphs().front();
3077 os.texrow().forceStart(par.id(), 0);
3079 bool rtl = par.isRTL(buffer().params())
3081 && getPWidth(cell).zero()
3082 && !runparams.use_polyglossia;
3086 par.getParLanguage(buffer().params())->lang();
3087 if (lang == "farsi")
3089 else if (lang == "arabic_arabi")
3091 // currently, remaining RTL languages are
3092 // arabic_arabtex and hebrew
3096 // pass to the OutputParams that we are in a cell and
3097 // which alignment we have set.
3098 // InsetNewline needs this context information.
3099 OutputParams newrp(runparams);
3100 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3101 ? OutputParams::PLAIN
3102 : OutputParams::ALIGNED;
3104 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3105 // copy cell and split in 2
3106 InsetTableCell head = InsetTableCell(*cellInset(cell));
3107 head.setBuffer(const_cast<Buffer &>(buffer()));
3108 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3110 dit.push_back(CursorSlice(head));
3111 head.setMacrocontextPositionRecursive(dit);
3112 bool hassep = false;
3113 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3115 tail.setBuffer(head.buffer());
3117 dit.push_back(CursorSlice(tail));
3118 tail.setMacrocontextPositionRecursive(dit);
3122 tail.latex(os, newrp);
3125 head.latex(os, newrp);
3127 head.latex(os, newrp);
3130 tail.latex(os, newrp);
3133 } else if (ltCaption(row)) {
3134 // Inside longtable caption rows, we must only output the caption inset
3135 // with its content and omit anything outside of that (see #10791)
3136 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
3137 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
3138 for (; it != i_end; ++it) {
3139 if (it->lyxCode() != CAPTION_CODE)
3141 it->latex(os, runparams);
3144 } else if (!isPartOfMultiRow(row, c)) {
3145 if (!runparams.nice)
3146 os.texrow().start(par.id(), 0);
3147 inset->latex(os, newrp);
3150 runparams.encoding = newrp.encoding;
3154 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3155 if (cell != lastcell) { // not last cell in row
3162 os << "\\tabularnewline";
3163 if (row_info[row].bottom_space_default) {
3165 os << "\\addlinespace";
3167 os << "[\\doublerulesep]";
3168 } else if (!row_info[row].bottom_space.zero()) {
3170 os << "\\addlinespace";
3172 << from_ascii(row_info[row].bottom_space.asLatexString())
3177 //output the bottom line
3178 TeXBottomHLine(os, row, columns, logical_columns);
3180 if (row_info[row].interline_space_default) {
3182 os << "\\addlinespace\n";
3184 os << "\\noalign{\\vskip\\doublerulesep}\n";
3185 } else if (!row_info[row].interline_space.zero()) {
3187 os << "\\addlinespace["
3188 << from_ascii(row_info[row].interline_space.asLatexString())
3191 os << "\\noalign{\\vskip"
3192 << from_ascii(row_info[row].interline_space.asLatexString())
3198 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3200 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3201 && !hasVarwidthColumn();
3202 bool const is_xltabular = is_long_tabular
3203 && (hasVarwidthColumn() || !tabular_width.zero());
3204 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3206 //+---------------------------------------------------------------------
3207 //+ first the opening preamble +
3208 //+---------------------------------------------------------------------
3211 if (!TexRow::isNone(pos))
3212 os.texrow().start(pos);
3215 if (is_long_tabular)
3216 os << "\\begin{landscape}\n";
3218 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3221 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3222 // order for RTL (#9686). Thus we use this list.
3223 bool const bidi_rtl =
3224 runparams.local_font->isRightToLeft()
3225 && runparams.useBidiPackage();
3226 list<col_type> columns;
3227 list<col_type> logical_columns;
3228 for (col_type cl = 0; cl < ncols(); ++cl) {
3229 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3232 columns.push_front(cl);
3234 columns.push_back(cl);
3235 // for some calculations, we need the logical (non-swapped)
3236 // columns also in bidi.
3237 logical_columns.push_back(cl);
3240 // If we use \cline or \cmidrule, we need to locally de-activate
3241 // the - character when using languages that activate it (e.g., Czech, Slovak).
3242 bool deactivate_chars = false;
3243 if ((runparams.use_babel || runparams.use_polyglossia)
3244 && contains(runparams.active_chars, '-')) {
3245 bool have_clines = false;
3246 // Check if we use \cline or \cmidrule
3247 for (row_type row = 0; row < nrows(); ++row) {
3248 col_type bset = 0, tset = 0;
3249 for (auto const & c : columns) {
3250 idx_type const idx = cellIndex(row, c);
3251 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3252 || topLineTrim(idx).first || topLineTrim(idx).second) {
3256 if (bottomLine(cellIndex(row, c)))
3258 if (topLine(cellIndex(row, c)))
3261 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3267 deactivate_chars = true;
3268 os << "\\begingroup\n"
3269 << "\\catcode`\\-=12\n";
3273 if (is_long_tabular) {
3275 os << "\\begin{xltabular}";
3277 os << "\\begin{longtable}";
3278 switch (longtabular_alignment) {
3279 case LYX_LONGTABULAR_ALIGN_LEFT:
3282 case LYX_LONGTABULAR_ALIGN_CENTER:
3285 case LYX_LONGTABULAR_ALIGN_RIGHT:
3290 if (tabular_width.zero())
3291 os << "{" << from_ascii("\\columnwidth") << "}";
3293 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3296 if (is_tabular_star)
3297 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3298 else if (hasVarwidthColumn()) {
3299 os << "\\begin{tabularx}{";
3300 if (tabular_width.zero())
3301 os << from_ascii("\\columnwidth") << "}";
3303 os << from_ascii(tabular_width.asLatexString()) << "}";
3305 os << "\\begin{tabular}";
3306 switch (tabular_valignment) {
3307 case LYX_VALIGN_TOP:
3310 case LYX_VALIGN_MIDDLE:
3312 case LYX_VALIGN_BOTTOM:
3320 if (is_tabular_star)
3321 os << "@{\\extracolsep{\\fill}}";
3323 for (auto const & c : columns) {
3324 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3326 if (!column_info[c].align_special.empty()) {
3327 os << column_info[c].align_special;
3329 if (!column_info[c].p_width.zero()) {
3330 bool decimal = false;
3331 switch (column_info[c].alignment) {
3332 case LYX_ALIGN_LEFT:
3333 os << ">{\\raggedright}";
3335 case LYX_ALIGN_RIGHT:
3336 os << ">{\\raggedleft}";
3338 case LYX_ALIGN_CENTER:
3339 os << ">{\\centering}";
3341 case LYX_ALIGN_NONE:
3342 case LYX_ALIGN_BLOCK:
3343 case LYX_ALIGN_LAYOUT:
3344 case LYX_ALIGN_SPECIAL:
3346 case LYX_ALIGN_DECIMAL: {
3348 os << ">{\\raggedright}";
3350 os << ">{\\raggedleft}";
3357 switch (column_info[c].valignment) {
3358 case LYX_VALIGN_TOP:
3359 // this is the default
3361 case LYX_VALIGN_MIDDLE:
3364 case LYX_VALIGN_BOTTOM:
3370 // Fixed-width cells with alignment at decimal separator
3371 // are output as two cells of half the width with the decimal
3372 // separator as column sep. This effectively puts the content
3373 // centered, which differs from the normal decimal sep alignment
3374 // and is not ideal, but we cannot do better ATM (see #9568).
3375 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3377 docstring const halffixedwith =
3378 from_ascii(Length(column_info[c].p_width.value() / 2,
3379 column_info[c].p_width.unit()).asLatexString());
3383 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3390 << from_ascii(column_info[c].p_width.asLatexString())
3392 } else if (column_info[c].varwidth) {
3393 switch (column_info[c].alignment) {
3394 case LYX_ALIGN_LEFT:
3395 os << ">{\\raggedright\\arraybackslash}";
3397 case LYX_ALIGN_RIGHT:
3398 os << ">{\\raggedleft\\arraybackslash}";
3400 case LYX_ALIGN_CENTER:
3401 os << ">{\\centering\\arraybackslash}";
3403 case LYX_ALIGN_NONE:
3404 case LYX_ALIGN_BLOCK:
3405 case LYX_ALIGN_LAYOUT:
3406 case LYX_ALIGN_SPECIAL:
3407 case LYX_ALIGN_DECIMAL:
3411 } else if (isVTypeColumn(c)) {
3412 switch (column_info[c].alignment) {
3413 case LYX_ALIGN_LEFT:
3414 os << ">{\\raggedright}";
3416 case LYX_ALIGN_RIGHT:
3417 os << ">{\\raggedleft}";
3419 case LYX_ALIGN_CENTER:
3420 os << ">{\\centering}";
3422 case LYX_ALIGN_NONE:
3423 case LYX_ALIGN_BLOCK:
3424 case LYX_ALIGN_LAYOUT:
3425 case LYX_ALIGN_SPECIAL:
3426 case LYX_ALIGN_DECIMAL:
3429 os << "V{\\linewidth}";
3431 switch (column_info[c].alignment) {
3432 case LYX_ALIGN_LEFT:
3435 case LYX_ALIGN_RIGHT:
3438 case LYX_ALIGN_DECIMAL:
3439 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3445 } // end if else !column_info[i].p_width
3446 } // end if else !column_info[i].align_special
3447 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3452 TeXLongtableHeaderFooter(os, runparams, columns, logical_columns);
3454 //+---------------------------------------------------------------------
3455 //+ the single row and columns (cells) +
3456 //+---------------------------------------------------------------------
3458 for (row_type r = 0; r < nrows(); ++r) {
3459 if (!buffer().params().output_changes && row_info[r].change.deleted())
3461 if (isValidRow(r)) {
3462 TeXRow(os, r, runparams, columns, logical_columns);
3463 if (is_long_tabular && row_info[r].newpage)
3464 os << "\\newpage\n";
3468 //+---------------------------------------------------------------------
3469 //+ the closing of the tabular +
3470 //+---------------------------------------------------------------------
3472 if (is_long_tabular) {
3474 os << "\\end{xltabular}";
3476 os << "\\end{longtable}";
3478 if (is_tabular_star)
3479 os << "\\end{tabular*}";
3480 else if (hasVarwidthColumn())
3481 os << "\\end{tabularx}";
3483 os << "\\end{tabular}";
3486 if (deactivate_chars)
3488 os << "\n\\endgroup\n";
3491 if (is_long_tabular)
3492 os << breakln << "\\end{landscape}";
3494 os << breakln << "\\end{turn}";
3497 if (!TexRow::isNone(pos))
3498 os.texrow().start(pos);
3502 void Tabular::docbookRow(XMLStream & xs, row_type row,
3503 OutputParams const & runparams, bool header) const
3505 switch (buffer().params().docbook_table_output) {
3506 case BufferParams::HTMLTable:
3507 docbookRowAsHTML(xs, row, runparams, header);
3509 case BufferParams::CALSTable:
3510 docbookRowAsCALS(xs, row, runparams);
3516 void Tabular::docbookRowAsHTML(XMLStream & xs, row_type row,
3517 OutputParams const & runparams, bool header) const
3519 string const celltag = header ? "th" : "td";
3520 idx_type cell = getFirstCellInRow(row);
3522 xs << xml::StartTag("tr");
3524 for (col_type c = 0; c < ncols(); ++c) {
3525 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3530 Length const cwidth = column_info[c].p_width;
3531 if (!cwidth.zero()) {
3532 string const hwidth = cwidth.asHTMLString();
3533 attr << "style =\"width: " << hwidth << ";\" ";
3537 switch (getAlignment(cell)) {
3538 case LYX_ALIGN_BLOCK:
3541 case LYX_ALIGN_DECIMAL: {
3542 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3543 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3546 case LYX_ALIGN_LEFT:
3549 case LYX_ALIGN_RIGHT:
3557 attr << " valign='";
3558 switch (getVAlignment(cell)) {
3559 case LYX_VALIGN_TOP:
3562 case LYX_VALIGN_BOTTOM:
3565 case LYX_VALIGN_MIDDLE:
3570 if (isMultiColumn(cell))
3571 attr << " colspan='" << columnSpan(cell) << "'";
3572 else if (isMultiRow(cell))
3573 attr << " rowspan='" << rowSpan(cell) << "'";
3575 xs << xml::StartTag(celltag, attr.str(), true);
3576 cellInset(cell)->docbook(xs, runparams);
3577 xs << xml::EndTag(celltag);
3581 xs << xml::EndTag("tr");
3586 void Tabular::docbookRowAsCALS(XMLStream & xs, row_type row,
3587 OutputParams const & runparams) const
3589 idx_type cell = getFirstCellInRow(row);
3591 xs << xml::StartTag("row");
3593 for (col_type c = 0; c < ncols(); ++c) {
3594 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3600 switch (getAlignment(cell)) {
3601 case LYX_ALIGN_BLOCK:
3604 case LYX_ALIGN_DECIMAL: {
3605 Language const *tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
3606 attr << "char' char='" << to_utf8(tlang->decimalSeparator());
3609 case LYX_ALIGN_LEFT:
3612 case LYX_ALIGN_RIGHT:
3621 attr << " valign='";
3622 switch (getVAlignment(cell)) {
3623 case LYX_VALIGN_TOP:
3626 case LYX_VALIGN_BOTTOM:
3629 case LYX_VALIGN_MIDDLE:
3634 if (isMultiColumn(cell))
3635 attr << " colspan='" << columnSpan(cell) << "'";
3636 else if (isMultiRow(cell))
3637 attr << " rowspan='" << rowSpan(cell) << "'";
3639 attr << " colname='c" << (c + 1) << "'"; // Column numbering starts at 1.
3641 // All cases where there should be a line *below* this row.
3642 if (row_info[row].bottom_space_default)
3643 attr << " rowsep='1'";
3645 xs << xml::StartTag("entry", attr.str(), true);
3646 cellInset(cell)->docbook(xs, runparams);
3647 xs << xml::EndTag("entry");
3651 xs << xml::EndTag("row");
3656 void Tabular::docbook(XMLStream & xs, OutputParams const & runparams) const
3660 // Some tables are inline. Likely limitation: cannot output a table within a table; is that really a limitation?
3661 if (!runparams.docbook_in_table) { // Check on the *outer* set of parameters, so that the table can be closed
3662 // properly at the end of this function.
3663 xs << xml::StartTag("informaltable");
3667 // "Formal" tables have a title and use the tag <table>; the distinction with <informaltable> is done outside.
3668 // HTML has the caption first with titles forbidden, and CALS has a title first.
3669 if (haveLTCaption()) {
3670 std::string tag = ((buffer().params().docbook_table_output) == BufferParams::HTMLTable) ? "caption" : "title";
3672 xs << xml::StartTag(tag);
3673 for (row_type r = 0; r < nrows(); ++r)
3674 if (row_info[r].caption)
3675 docbookRow(xs, r, runparams);
3676 xs << xml::EndTag(tag);
3680 // CALS header: describe all columns in this table. For names, take 'c' then the ID of the column.
3681 // Start at one, as is customary with CALS!
3682 if (buffer().params().docbook_table_output == BufferParams::CALSTable) {
3683 for (col_type c = 0; c < ncols(); ++c) {
3684 std::stringstream attr;
3685 attr << "colnum='" << (c + 1) << "' ";
3686 attr << "colname='c" << (c + 1) << "' ";
3687 Length const cwidth = column_info[c].p_width;
3689 attr << "colwidth='" << cwidth.asHTMLString() << "' ";
3690 attr << "rowheader='norowheader'"; // Last attribute, hence no space at the end.
3692 xs << xml::CompTag("colspec", attr.str());
3697 // Output the header of the table. For both HTML and CALS, this is surrounded by a thead.
3698 bool const havefirsthead = haveLTFirstHead(false);
3699 // if we have a first head, then we are going to ignore the
3700 // headers for the additional pages, since there aren't any
3701 // in DocBook. this test accomplishes that.
3702 bool const havehead = !havefirsthead && haveLTHead(false);
3703 if (havehead || havefirsthead) {
3704 xs << xml::StartTag("thead") << xml::CR();
3705 for (row_type r = 0; r < nrows(); ++r) {
3706 if (((havefirsthead && row_info[r].endfirsthead) ||
3707 (havehead && row_info[r].endhead)) &&
3708 !row_info[r].caption) {
3709 docbookRow(xs, r, runparams, true); // TODO: HTML vs CALS
3712 xs << xml::EndTag("thead");
3716 // Output the footer of the table. For both HTML and CALS, this is surrounded by a tfoot and output just after
3717 // the header (and before the body).
3718 bool const havelastfoot = haveLTLastFoot(false);
3720 bool const havefoot = !havelastfoot && haveLTFoot(false);
3721 if (havefoot || havelastfoot) {
3722 xs << xml::StartTag("tfoot") << xml::CR();
3723 for (row_type r = 0; r < nrows(); ++r) {
3724 if (((havelastfoot && row_info[r].endlastfoot) ||
3725 (havefoot && row_info[r].endfoot)) &&
3726 !row_info[r].caption) {
3727 docbookRow(xs, r, runparams); // TODO: HTML vs CALS
3730 xs << xml::EndTag("tfoot");
3734 // Output the main part of the table. The tbody container is mandatory for CALS, but optional for HTML (only if
3735 // there is no header and no footer). It never hurts to have it, though.
3736 xs << xml::StartTag("tbody");
3738 for (row_type r = 0; r < nrows(); ++r)
3740 docbookRow(xs, r, runparams);
3741 xs << xml::EndTag("tbody");
3744 // If this method started the table tag, also make it close it.
3745 if (!runparams.docbook_in_table) {
3746 xs << xml::EndTag("informaltable");
3752 docstring Tabular::xhtmlRow(XMLStream & xs, row_type row,
3753 OutputParams const & runparams, bool header) const
3756 string const celltag = header ? "th" : "td";
3757 idx_type cell = getFirstCellInRow(row);
3759 xs << xml::StartTag("tr");
3760 for (col_type c = 0; c < ncols(); ++c) {
3761 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3766 Length const cwidth = column_info[c].p_width;
3767 if (!cwidth.zero()) {
3768 string const hwidth = cwidth.asHTMLString();
3769 attr << "style =\"width: " << hwidth << ";\" ";
3773 switch (getAlignment(cell)) {
3774 case LYX_ALIGN_LEFT:
3777 case LYX_ALIGN_RIGHT:
3785 attr << " valign='";
3786 switch (getVAlignment(cell)) {
3787 case LYX_VALIGN_TOP:
3790 case LYX_VALIGN_BOTTOM:
3793 case LYX_VALIGN_MIDDLE:
3798 if (isMultiColumn(cell))
3799 attr << " colspan='" << columnSpan(cell) << "'";
3800 else if (isMultiRow(cell))
3801 attr << " rowspan='" << rowSpan(cell) << "'";
3803 xs << xml::StartTag(celltag, attr.str(), true) << xml::CR();
3804 ret += cellInset(cell)->xhtml(xs, runparams);
3805 xs << xml::EndTag(celltag) << xml::CR();
3808 xs << xml::EndTag("tr");
3813 docstring Tabular::xhtml(XMLStream & xs, OutputParams const & runparams) const
3817 if (is_long_tabular) {
3818 // we'll wrap it in a div, so as to deal with alignment
3820 switch (longtabular_alignment) {
3821 case LYX_LONGTABULAR_ALIGN_LEFT:
3824 case LYX_LONGTABULAR_ALIGN_CENTER:
3827 case LYX_LONGTABULAR_ALIGN_RIGHT:
3831 xs << xml::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
3833 // The caption flag wins over head/foot
3834 if (haveLTCaption()) {
3835 xs << xml::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
3837 for (row_type r = 0; r < nrows(); ++r)
3838 if (row_info[r].caption)
3839 ret += xhtmlRow(xs, r, runparams);
3840 xs << xml::EndTag("div");
3845 xs << xml::StartTag("table");
3848 // output header info
3849 bool const havefirsthead = haveLTFirstHead(false);
3850 // if we have a first head, then we are going to ignore the
3851 // headers for the additional pages, since there aren't any
3852 // in XHTML. this test accomplishes that.
3853 bool const havehead = !havefirsthead && haveLTHead(false);
3854 if (havehead || havefirsthead) {
3855 xs << xml::StartTag("thead");
3857 for (row_type r = 0; r < nrows(); ++r) {
3858 if (((havefirsthead && row_info[r].endfirsthead) ||
3859 (havehead && row_info[r].endhead)) &&
3860 !row_info[r].caption) {
3861 ret += xhtmlRow(xs, r, runparams, true);
3864 xs << xml::EndTag("thead");
3867 // output footer info
3868 bool const havelastfoot = haveLTLastFoot(false);
3870 bool const havefoot = !havelastfoot && haveLTFoot(false);
3871 if (havefoot || havelastfoot) {
3872 xs << xml::StartTag("tfoot") << xml::CR();
3873 for (row_type r = 0; r < nrows(); ++r) {
3874 if (((havelastfoot && row_info[r].endlastfoot) ||
3875 (havefoot && row_info[r].endfoot)) &&
3876 !row_info[r].caption) {
3877 ret += xhtmlRow(xs, r, runparams);
3880 xs << xml::EndTag("tfoot");
3884 xs << xml::StartTag("tbody") << xml::CR();
3885 for (row_type r = 0; r < nrows(); ++r)
3887 ret += xhtmlRow(xs, r, runparams);
3888 xs << xml::EndTag("tbody");
3890 xs << xml::EndTag("table");
3892 if (is_long_tabular) {
3893 xs << xml::EndTag("div");
3900 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3901 vector<unsigned int> const & clen) const
3903 idx_type const fcell = getFirstCellInRow(row);
3904 idx_type const n = numberOfCellsInRow(row) + fcell;
3907 for (idx_type i = fcell; i < n; ++i) {
3917 for (idx_type i = fcell; i < n; ++i) {
3928 col_type column = cellColumn(i);
3929 int len = clen[column];
3930 while (column < ncols() - 1
3931 && isPartOfMultiColumn(row, ++column))
3932 len += clen[column] + 4;
3933 os << docstring(len, ch);
3948 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3949 vector<unsigned int> const & clen) const
3951 idx_type const fcell = getFirstCellInRow(row);
3952 idx_type const n = numberOfCellsInRow(row) + fcell;
3955 for (idx_type i = fcell; i < n; ++i) {
3956 if (bottomLine(i)) {
3965 for (idx_type i = fcell; i < n; ++i) {
3966 if (bottomLine(i)) {
3976 col_type column = cellColumn(i);
3977 int len = clen[column];
3978 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3979 len += clen[column] + 4;
3980 os << docstring(len, ch);
3981 if (bottomLine(i)) {
3995 void Tabular::plaintextPrintCell(odocstringstream & os,
3996 OutputParams const & runparams,
3997 idx_type cell, row_type row, col_type column,
3998 vector<unsigned int> const & clen,
3999 bool onlydata, size_t max_length) const
4001 odocstringstream sstr;
4002 cellInset(cell)->plaintext(sstr, runparams, max_length);
4014 unsigned int len1 = sstr.str().length();
4015 unsigned int len2 = clen[column];
4016 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
4017 len2 += clen[column] + 4;
4020 switch (getAlignment(cell)) {
4022 case LYX_ALIGN_LEFT:
4025 case LYX_ALIGN_RIGHT:
4029 case LYX_ALIGN_CENTER:
4035 os << docstring(len1, ' ') << sstr.str()
4036 << docstring(len2, ' ');
4038 if (rightLine(cell))
4045 void Tabular::plaintext(odocstringstream & os,
4046 OutputParams const & runparams, int const depth,
4047 bool onlydata, char_type delim, size_t max_length) const
4049 // first calculate the width of the single columns
4050 vector<unsigned int> clen(ncols());
4053 // first all non multicolumn cells!
4054 for (col_type c = 0; c < ncols(); ++c) {
4056 for (row_type r = 0; r < nrows(); ++r) {
4057 idx_type cell = cellIndex(r, c);
4058 if (isMultiColumn(cell))
4060 odocstringstream sstr;
4061 cellInset(cell)->plaintext(sstr, runparams, max_length);
4062 if (clen[c] < sstr.str().length())
4063 clen[c] = sstr.str().length();
4066 // then all multicolumn cells!
4067 for (col_type c = 0; c < ncols(); ++c) {
4068 for (row_type r = 0; r < nrows(); ++r) {
4069 idx_type cell = cellIndex(r, c);
4070 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
4072 odocstringstream sstr;
4073 cellInset(cell)->plaintext(sstr, runparams, max_length);
4074 int len = int(sstr.str().length());
4075 idx_type const n = columnSpan(cell);
4076 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
4078 if (len > int(clen[c + n - 1]))
4079 clen[c + n - 1] = len;
4084 for (row_type r = 0; r < nrows(); ++r) {
4085 if (!onlydata && plaintextTopHLine(os, r, clen))
4086 os << docstring(depth * 2, ' ');
4087 for (col_type c = 0; c < ncols(); ++c) {
4088 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
4090 if (onlydata && c > 0)
4091 // we don't use operator<< for single UCS4 character.
4092 // see explanation in docstream.h
4094 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
4096 if (os.str().size() > max_length)
4101 os << docstring(depth * 2, ' ');
4102 if (plaintextBottomHLine(os, r, clen))
4103 os << docstring(depth * 2, ' ');
4105 if (os.str().size() > max_length)
4111 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
4113 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
4117 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
4119 return cell_info[row][column].inset;
4123 InsetTableCell const * Tabular::cellInset(idx_type cell) const
4125 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
4129 void Tabular::setCellInset(row_type row, col_type column,
4130 shared_ptr<InsetTableCell> ins)
4132 CellData & cd = cell_info[row][column];
4137 void Tabular::validate(LaTeXFeatures & features) const
4139 features.require("NeedTabularnewline");
4141 features.require("booktabs");
4142 if (is_long_tabular && !hasVarwidthColumn()) {
4143 if (tabular_width.zero())
4144 features.require("longtable");
4146 features.require("xltabular");
4148 if (rotate && is_long_tabular)
4149 features.require("lscape");
4151 features.require("rotating");
4152 if (hasVarwidthColumn()) {
4153 if (is_long_tabular)
4154 features.require("xltabular");
4156 features.require("tabularx");
4158 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4159 if (isMultiRow(cell))
4160 features.require("multirow");
4161 if (getUsebox(cell) == BOX_VARWIDTH)
4162 features.require("varwidth");
4163 if (getVAlignment(cell) != LYX_VALIGN_TOP
4164 || !getPWidth(cell).zero()
4165 || isVTypeColumn(cellColumn(cell)))
4166 features.require("array");
4167 // Tell footnote that we need a savenote
4168 // environment in non-long tables or
4169 // longtable headers/footers
4170 else if (!is_long_tabular && !features.inFloat())
4171 features.saveNoteEnv("tabular");
4172 else if (!isValidRow(cellRow(cell)))
4173 features.saveNoteEnv("longtable");
4175 cellInset(cell)->validate(features);
4176 features.saveNoteEnv(string());
4181 Tabular::BoxType Tabular::useBox(idx_type cell) const
4183 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4184 if (parlist.size() > 1)
4185 return BOX_VARWIDTH;
4187 ParagraphList::const_iterator cit = parlist.begin();
4188 ParagraphList::const_iterator end = parlist.end();
4190 for (; cit != end; ++cit)
4191 for (int i = 0; i < cit->size(); ++i)
4192 if (cit->isNewline(i) || cit->layout().isEnvironment())
4193 return BOX_VARWIDTH;
4199 /////////////////////////////////////////////////////////////////////
4203 /////////////////////////////////////////////////////////////////////
4205 InsetTableCell::InsetTableCell(Buffer * buf)
4206 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
4207 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4211 bool InsetTableCell::forcePlainLayout(idx_type) const
4213 return isMultiRow || (isMultiColumn && !isFixedWidth);
4217 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4219 return isFixedWidth;
4223 bool InsetTableCell::forceLocalFontSwitch() const
4229 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4230 FuncStatus & status) const
4232 bool enabled = true;
4233 switch (cmd.action()) {
4234 case LFUN_INSET_DISSOLVE:
4237 case LFUN_MATH_DISPLAY:
4238 if (!hasFixedWidth()) {
4243 return InsetText::getStatus(cur, cmd, status);
4245 status.setEnabled(enabled);
4249 docstring InsetTableCell::asString(bool intoInsets)
4252 if (paragraphs().empty())
4254 ParagraphList::const_iterator it = paragraphs().begin();
4255 ParagraphList::const_iterator en = paragraphs().end();
4257 for (; it != en; ++it) {
4262 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4268 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4269 UpdateType utype, TocBackend & backend) const
4271 InsetText::iterateForToc(di, output_active, utype, backend);
4275 docstring InsetTableCell::xhtml(XMLStream & xs, OutputParams const & rp) const
4278 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4279 return InsetText::xhtml(xs, rp);
4283 void InsetTableCell::docbook(XMLStream & xs, OutputParams const & runparams) const
4285 InsetText::docbook(xs, runparams);
4289 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4291 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4292 int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
4294 // Hand font through to contained lyxtext:
4295 tm.font_.fontInfo() = mi.base.font;
4296 mi.base.textwidth -= horiz_offset;
4298 // This can happen when a layout has a left and right margin,
4299 // and the view is made very narrow. We can't do better than
4300 // to draw it partly out of view (bug 5890).
4301 if (mi.base.textwidth < 1)
4302 mi.base.textwidth = 1;
4304 // We tell metrics here not to expand on multiple pars
4305 // This is the difference to InsetText::Metrics
4306 if (hasFixedWidth())
4307 tm.metrics(mi, dim, mi.base.textwidth, false);
4309 tm.metrics(mi, dim, 0, false);
4310 mi.base.textwidth += horiz_offset;
4311 dim.asc += topOffset(mi.base.bv);
4312 dim.des += bottomOffset(mi.base.bv);
4313 dim.wid += horiz_offset;
4317 /////////////////////////////////////////////////////////////////////
4321 /////////////////////////////////////////////////////////////////////
4323 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4325 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4326 rowselect_(false), colselect_(false)
4331 InsetTabular::InsetTabular(InsetTabular const & tab)
4332 : Inset(tab), tabular(tab.tabular),
4333 rowselect_(false), colselect_(false)
4338 InsetTabular::~InsetTabular()
4340 hideDialogs("tabular", this);
4344 void InsetTabular::setBuffer(Buffer & buf)
4346 tabular.setBuffer(buf);
4347 Inset::setBuffer(buf);
4351 bool InsetTabular::insetAllowed(InsetCode code) const
4356 case MATHMACRO_CODE:
4361 return tabular.is_long_tabular;
4369 bool InsetTabular::allowMultiPar() const
4371 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
4372 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
4373 if (tabular.cellInset(r,c)->allowMultiPar())
4381 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4383 return tabular.is_long_tabular &&
4384 (newtype == "Standard" || newtype == "Unnumbered");
4388 void InsetTabular::write(ostream & os) const
4390 os << "Tabular" << endl;
4395 string InsetTabular::contextMenu(BufferView const &, int, int) const
4397 // FIXME: depending on the selection state,
4398 // we could offer a different menu.
4399 return cell(0)->contextMenuName() + ";" + contextMenuName();
4403 string InsetTabular::contextMenuName() const
4405 return "context-tabular";
4409 void InsetTabular::read(Lexer & lex)
4411 //bool const old_format = (lex.getString() == "\\LyXTable");
4419 string token = lex.getString();
4420 while (lex && token != "\\end_inset") {
4422 token = lex.getString();
4425 lex.printError("Missing \\end_inset at this point. ");
4429 int InsetTabular::rowFromY(Cursor & cur, int y) const
4431 // top y coordinate of tabular
4432 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4434 for (; r < tabular.nrows() && y > h; ++r)
4435 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4436 + tabular.interRowSpace(r);
4442 int InsetTabular::columnFromX(Cursor & cur, int x) const
4444 // left x coordinate of tabular
4445 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4447 for (; c < tabular.ncols() && x > w; ++c)
4448 w += tabular.cellWidth(c);
4453 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4455 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4456 // mi.base.textwidth << "\n";
4457 LBUFERR(mi.base.bv);
4459 for (row_type r = 0; r < tabular.nrows(); ++r) {
4462 for (col_type c = 0; c < tabular.ncols(); ++c) {
4463 if (tabular.isPartOfMultiColumn(r, c)
4464 || tabular.isPartOfMultiRow(r, c))
4465 // multicolumn or multirow cell, but not first one
4467 idx_type const cell = tabular.cellIndex(r, c);
4470 Length const p_width = tabular.getPWidth(cell);
4471 if (!p_width.zero())
4472 m.base.textwidth = mi.base.inPixels(p_width);
4473 else if (tabular.column_info[c].varwidth)
4474 m.base.textwidth = tabular.column_info[c].width;
4475 tabular.cellInset(cell)->metrics(m, dim0);
4476 if (!p_width.zero() || tabular.column_info[c].varwidth)
4477 dim0.wid = m.base.textwidth;
4478 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4479 + tabular.interColumnSpace(cell);
4481 // FIXME(?): do we need a second metrics call?
4482 TextMetrics const & tm =
4483 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4485 // determine horizontal offset because of decimal align (if necessary)
4486 int decimal_width = 0;
4487 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4488 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4489 tail.setBuffer(tabular.buffer());
4490 // we need to set macrocontext position everywhere
4491 // otherwise we crash with nested insets (e.g. footnotes)
4492 // after decimal point
4493 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4495 dit.push_back(CursorSlice(tail));
4496 tail.setMacrocontextPositionRecursive(dit);
4498 // remove text leading decimal point
4499 docstring const align_d = tabular.column_info[c].decimal_point;
4500 dit = separatorPos(&tail, align_d);
4502 pos_type const psize = tail.paragraphs().front().size();
4504 tail.paragraphs().front().eraseChars(0,
4505 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4507 tail.metrics(m, dim1);
4508 decimal_width = dim1.width();
4511 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4512 tabular.cell_info[r][c].decimal_width = decimal_width;
4514 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4515 // = descent of text in last par + bottomOffset:
4516 int const lastpardes = tm.last().second->descent()
4517 + bottomOffset(mi.base.bv);
4519 switch (tabular.getVAlignment(cell)) {
4520 case Tabular::LYX_VALIGN_TOP:
4522 case Tabular::LYX_VALIGN_MIDDLE:
4523 offset = -(dim0.des - lastpardes)/2;
4525 case Tabular::LYX_VALIGN_BOTTOM:
4526 offset = -(dim0.des - lastpardes);
4529 tabular.cell_info[r][c].voffset = offset;
4530 maxasc = max(maxasc, dim0.asc - offset);
4531 maxdes = max(maxdes, dim0.des + offset);
4533 int const top_space = tabular.row_info[r].top_space_default ?
4534 default_line_space :
4535 mi.base.inPixels(tabular.row_info[r].top_space);
4536 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4537 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4538 default_line_space :
4539 mi.base.inPixels(tabular.row_info[r].bottom_space);
4540 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4543 // We need to recalculate the metrics after column width calculation
4544 // with xtabular (possibly multiple times, so the call is recursive).
4545 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4547 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4548 dim.des = tabular.height() - dim.asc;
4549 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4553 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4555 if (&cur.inset() == this && cur.selection()) {
4556 if (cur.selIsMultiCell()) {
4559 getSelection(cur, rs, re, cs, ce);
4561 idx_type const cell = tabular.cellIndex(row, col);
4562 col_type const cspan = tabular.columnSpan(cell);
4563 row_type const rspan = tabular.rowSpan(cell);
4564 if (col + cspan - 1 >= cs && col <= ce
4565 && row + rspan - 1 >= rs && row <= re)
4568 if (col == tabular.cellColumn(cur.idx())
4569 && row == tabular.cellRow(cur.idx())) {
4570 CursorSlice const & beg = cur.selBegin();
4571 CursorSlice const & end = cur.selEnd();
4573 if ((end.lastpos() > 0 || end.lastpit() > 0)
4574 && end.pos() == end.lastpos() && beg.pos() == 0
4575 && end.pit() == end.lastpit() && beg.pit() == 0)
4583 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4585 x += ADD_TO_TABULAR_WIDTH;
4587 BufferView * bv = pi.base.bv;
4588 Cursor & cur = pi.base.bv->cursor();
4590 // FIXME: As the full background is painted in drawBackground(),
4591 // we have no choice but to do a full repaint for the Text cells.
4592 pi.full_repaint = true;
4594 bool const original_selection_state = pi.selected;
4598 // Save tabular change status
4599 Change tab_change = pi.change;
4601 int yy = y + tabular.offsetVAlignment();
4602 for (row_type r = 0; r < tabular.nrows(); ++r) {
4604 for (col_type c = 0; c < tabular.ncols(); ++c) {
4605 if (tabular.isPartOfMultiColumn(r, c))
4608 idx = tabular.cellIndex(r, c);
4610 if (tabular.isPartOfMultiRow(r, c)) {
4611 nx += tabular.cellWidth(idx);
4615 pi.selected |= isCellSelected(cur, r, c);
4617 // Mark deleted rows/columns
4618 if (tabular.column_info[c].change.changed())
4619 pi.change = tabular.column_info[c].change;
4620 else if (tabular.row_info[r].change.changed())
4621 pi.change = tabular.row_info[r].change;
4623 pi.change = tab_change;
4625 int const cx = nx + tabular.textHOffset(idx);
4626 int const cy = yy + tabular.textVOffset(idx);
4627 // Cache the Inset position.
4628 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4629 cell(idx)->draw(pi, cx, cy);
4630 drawCellLines(pi, nx, yy, r, idx);
4631 nx += tabular.cellWidth(idx);
4632 pi.selected = original_selection_state;
4635 if (r + 1 < tabular.nrows())
4636 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4637 + tabular.interRowSpace(r + 1);
4642 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4644 x += ADD_TO_TABULAR_WIDTH;
4645 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4646 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4647 pi.backgroundColor(this));
4651 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4653 Cursor & cur = pi.base.bv->cursor();
4655 x += ADD_TO_TABULAR_WIDTH;
4657 if (!cur.selection())
4659 if (&cur.inset() != this)
4664 bool const full_cell_selected = isCellSelected(cur,
4665 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4667 if (cur.selIsMultiCell() || full_cell_selected) {
4668 for (row_type r = 0; r < tabular.nrows(); ++r) {
4670 for (col_type c = 0; c < tabular.ncols(); ++c) {
4671 if (tabular.isPartOfMultiColumn(r, c))
4674 idx_type const cell = tabular.cellIndex(r, c);
4676 if (tabular.isPartOfMultiRow(r, c)) {
4677 xx += tabular.cellWidth(cell);
4680 int const w = tabular.cellWidth(cell);
4681 int const h = tabular.cellHeight(cell);
4682 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4683 if (isCellSelected(cur, r, c))
4684 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4687 if (r + 1 < tabular.nrows())
4688 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4689 + tabular.interRowSpace(r + 1);
4693 // FIXME: This code has no effect because InsetTableCell does not handle
4694 // drawSelection other than the trivial implementation in Inset.
4696 // x += cellXPos(cur.idx());
4697 // x += tabular.textHOffset(cur.idx());
4698 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4705 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4706 Color const incol, bool drawline, bool heavy = false)
4708 Color const col = drawline ? incol : Color_tabularonoffline;
4709 if (drawline && lt > 0)
4710 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4711 Painter::line_onoffdash,
4712 Painter::thin_line);
4713 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4714 drawline ? Painter::line_solid : Painter::line_onoffdash,
4715 (heavy ? 2 : 1) * Painter::thin_line);
4716 if (drawline && rt > 0)
4717 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4718 Painter::line_onoffdash,
4719 Painter::thin_line);
4725 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4726 row_type row, idx_type cell) const
4728 y -= tabular.rowAscent(row);
4729 int const w = tabular.cellWidth(cell);
4730 int const h = tabular.cellHeight(cell);
4734 col_type const col = tabular.cellColumn(cell);
4736 // Colour the frame if rows/columns are added or deleted
4737 Color colour = Color_tabularline;
4738 if (tabular.column_info[col].change.changed()
4739 || tabular.row_info[row].change.changed())
4740 colour = InsetTableCell(*tabular.cellInset(cell)).paragraphs().front().lookupChange(0).color();
4743 bool drawline = tabular.topLine(cell)
4744 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4745 bool heavy = tabular.use_booktabs
4746 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4747 && tabular.rowTopLine(row);
4748 if (tabular.topLineTrim(cell).first
4749 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4751 if (tabular.topLineTrim(cell).second
4752 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4754 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4758 drawline = tabular.bottomLine(cell);
4759 row_type const lastrow = tabular.nrows() - 1;
4760 // Consider multi-rows
4762 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4763 && tabular.isPartOfMultiRow(r + 1, col))
4765 heavy = tabular.use_booktabs
4766 && ((row == lastrow && tabular.rowBottomLine(row))
4767 || (r == lastrow && tabular.rowBottomLine(r)));
4768 if (tabular.bottomLineTrim(cell).first)
4770 if (tabular.bottomLineTrim(cell).second)
4772 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4775 drawline = tabular.leftLine(cell)
4776 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4777 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4780 x -= tabular.interColumnSpace(cell);
4781 col_type next_cell_col = col + 1;
4782 while (next_cell_col < tabular.ncols()
4783 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4785 drawline = tabular.rightLine(cell)
4786 || (next_cell_col < tabular.ncols()
4787 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4788 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4792 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4794 //lyxerr << "InsetTabular::edit: " << this << endl;
4798 if (isRightToLeft(cur))
4799 cur.idx() = tabular.getLastCellInRow(0);
4805 if (isRightToLeft(cur))
4806 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4808 cur.idx() = tabular.numberofcells - 1;
4810 cur.pos() = cur.lastpos(); // FIXME crude guess
4812 cur.setCurrentFont();
4813 // FIXME: this accesses the position cache before it is initialized
4814 //cur.bv().fitCursor();
4818 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/)
4820 // In a longtable, tell captions what the current float is
4821 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4822 string const saveflt = cnts.current_float();
4823 if (tabular.is_long_tabular) {
4824 cnts.current_float("table");
4825 // in longtables, we only step the counter once
4826 cnts.step(from_ascii("table"), utype);
4827 cnts.isLongtable(true);
4830 ParIterator it2 = it;
4832 size_t const end = it2.nargs();
4833 for ( ; it2.idx() < end; it2.top().forwardIdx())
4834 buffer().updateBuffer(it2, utype);
4837 if (tabular.is_long_tabular) {
4838 cnts.current_float(saveflt);
4839 cnts.isLongtable(false);
4844 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4845 UpdateType utype, TocBackend & backend) const
4847 DocIterator dit = cpit;
4849 size_t const end = dit.nargs();
4850 for ( ; dit.idx() < end; dit.top().forwardIdx())
4851 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4855 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4857 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4858 return x < x0 || x > x0 + tabular.width();
4862 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4864 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4865 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4866 // there is no margin added vertically to tabular insets.
4867 // However, it works for now.
4868 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4872 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4874 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4878 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4880 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4881 << "\n cur:" << cur);
4882 CursorSlice sl = cur.top();
4883 Cursor & bvcur = cur.bv().cursor();
4885 FuncCode const act = cmd.action();
4889 case LFUN_MOUSE_PRESS: {
4890 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4892 if (hitSelectRow(cur.bv(), cmd.x())) {
4893 row_type r = rowFromY(cur, cmd.y());
4894 cur.idx() = tabular.getFirstCellInRow(r);
4898 cur.idx() = tabular.getLastCellInRow(r);
4899 cur.pit() = cur.lastpit();
4900 cur.pos() = cur.lastpos();
4901 cur.selection(true);
4907 if (hitSelectColumn(cur.bv(), cmd.y())) {
4908 col_type c = columnFromX(cur, cmd.x());
4909 cur.idx() = tabular.cellIndex(0, c);
4913 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4914 cur.pit() = cur.lastpit();
4915 cur.pos() = cur.lastpos();
4916 cur.selection(true);
4921 // do not reset cursor/selection if we have selected
4922 // some cells (bug 2715).
4923 if (cmd.button() == mouse_button::button3
4924 && &bvcur.selBegin().inset() == this
4925 && bvcur.selIsMultiCell())
4928 // Let InsetTableCell do it
4929 cell(cur.idx())->dispatch(cur, cmd);
4932 case LFUN_MOUSE_MOTION:
4933 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4934 if (cmd.button() == mouse_button::button1) {
4935 // only accept motions to places not deeper nested than the real anchor
4936 if (!bvcur.realAnchor().hasPart(cur)) {
4940 // select (additional) row
4942 row_type r = rowFromY(cur, cmd.y());
4943 cur.idx() = tabular.getLastCellInRow(r);
4944 // we need to reset the cursor's pit and pos now, as the old ones
4945 // may no longer be valid.
4948 bvcur.setCursor(cur);
4949 bvcur.selection(true);
4952 // select (additional) column
4954 col_type c = columnFromX(cur, cmd.x());
4955 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4956 // we need to reset the cursor's pit and pos now, as the old ones
4957 // may no longer be valid.
4960 bvcur.setCursor(cur);
4961 bvcur.selection(true);
4964 // only update if selection changes
4965 if (bvcur.idx() == cur.idx() &&
4966 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4967 cur.noScreenUpdate();
4968 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4969 bvcur.setCursor(cur);
4970 bvcur.selection(true);
4971 // if this is a multicell selection, we just set the cursor to
4972 // the beginning of the cell's text.
4973 if (bvcur.selIsMultiCell()) {
4974 bvcur.pit() = bvcur.lastpit();
4975 bvcur.pos() = bvcur.lastpos();
4980 case LFUN_MOUSE_RELEASE:
4985 case LFUN_CELL_BACKWARD:
4987 cur.selection(false);
4990 case LFUN_CELL_FORWARD:
4992 cur.selection(false);
4995 case LFUN_CHAR_FORWARD_SELECT:
4996 case LFUN_CHAR_FORWARD:
4997 case LFUN_CHAR_BACKWARD_SELECT:
4998 case LFUN_CHAR_BACKWARD:
4999 case LFUN_CHAR_RIGHT_SELECT:
5000 case LFUN_CHAR_RIGHT:
5001 case LFUN_CHAR_LEFT_SELECT:
5002 case LFUN_CHAR_LEFT:
5003 case LFUN_WORD_FORWARD:
5004 case LFUN_WORD_FORWARD_SELECT:
5005 case LFUN_WORD_BACKWARD:
5006 case LFUN_WORD_BACKWARD_SELECT:
5007 case LFUN_WORD_RIGHT:
5008 case LFUN_WORD_RIGHT_SELECT:
5009 case LFUN_WORD_LEFT:
5010 case LFUN_WORD_LEFT_SELECT: {
5011 // determine whether we move to next or previous cell, where to enter
5012 // the new cell from, and which command to "finish" (i.e., exit the
5015 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
5016 FuncCode finish_lfun;
5018 if (act == LFUN_CHAR_FORWARD
5019 || act == LFUN_CHAR_FORWARD_SELECT
5020 || act == LFUN_WORD_FORWARD
5021 || act == LFUN_WORD_FORWARD_SELECT) {
5023 finish_lfun = LFUN_FINISHED_FORWARD;
5025 else if (act == LFUN_CHAR_BACKWARD
5026 || act == LFUN_CHAR_BACKWARD_SELECT
5027 || act == LFUN_WORD_BACKWARD
5028 || act == LFUN_WORD_BACKWARD_SELECT) {
5030 finish_lfun = LFUN_FINISHED_BACKWARD;
5032 // LEFT or RIGHT commands --- the interpretation will depend on the
5033 // table's direction.
5035 bool const right = act == LFUN_CHAR_RIGHT
5036 || act == LFUN_CHAR_RIGHT_SELECT
5037 || act == LFUN_WORD_RIGHT
5038 || act == LFUN_WORD_RIGHT_SELECT;
5039 next_cell = isRightToLeft(cur) != right;
5041 if (lyxrc.visual_cursor)
5042 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
5044 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
5047 bool const select = act == LFUN_CHAR_FORWARD_SELECT
5048 || act == LFUN_CHAR_BACKWARD_SELECT
5049 || act == LFUN_CHAR_RIGHT_SELECT
5050 || act == LFUN_CHAR_LEFT_SELECT
5051 || act == LFUN_WORD_FORWARD_SELECT
5052 || act == LFUN_WORD_RIGHT_SELECT
5053 || act == LFUN_WORD_BACKWARD_SELECT
5054 || act == LFUN_WORD_LEFT_SELECT;
5056 // If we have a multicell selection or we're
5057 // not doing some LFUN_*_SELECT thing anyway...
5058 if (!cur.selIsMultiCell() || !select) {
5059 col_type const c = tabular.cellColumn(cur.idx());
5060 row_type const r = tabular.cellRow(cur.idx());
5061 // Are we trying to select the whole cell and is the whole cell
5062 // not yet selected?
5063 bool const select_whole = select && !isCellSelected(cur, r, c) &&
5064 ((next_cell && cur.pit() == cur.lastpit()
5065 && cur.pos() == cur.lastpos())
5066 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
5068 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
5070 // ...try to dispatch to the cell's inset.
5071 cell(cur.idx())->dispatch(cur, cmd);
5073 // When we already have a selection we want to select the whole cell
5074 // before going to the next cell.
5075 if (select_whole && !empty_cell){
5076 getText(cur.idx())->selectAll(cur);
5078 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5082 // FIXME: When we support the selection of an empty cell, remove
5083 // the !empty_cell from this condition. For now we jump to the next
5084 // cell if the current cell is empty.
5085 if (cur.result().dispatched() && !empty_cell)
5089 // move to next/prev cell, as appropriate
5090 // note that we will always do this if we're selecting and we have
5091 // a multicell selection
5092 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
5093 << " cell from: " << int(entry_from));
5095 moveNextCell(cur, entry_from);
5097 movePrevCell(cur, entry_from);
5098 // if we're exiting the table, call the appropriate FINISHED lfun
5099 if (sl == cur.top()) {
5100 cmd = FuncRequest(finish_lfun);
5105 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5110 case LFUN_DOWN_SELECT:
5112 if (!(cur.selection() && cur.selIsMultiCell()))
5113 cell(cur.idx())->dispatch(cur, cmd);
5115 cur.dispatched(); // override the cell's decision
5116 if (sl == cur.top()) {
5117 // if our Text didn't do anything to the cursor
5118 // then we try to put the cursor into the cell below
5119 // setting also the right targetX.
5120 cur.selHandle(act == LFUN_DOWN_SELECT);
5121 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
5122 int const xtarget = cur.targetX();
5123 // WARNING: Once cur.idx() has been reset, the cursor is in
5124 // an inconsistent state until pos() has been set. Be careful
5125 // what you do with it!
5126 cur.idx() = tabular.cellBelow(cur.idx());
5128 TextMetrics const & tm =
5129 cur.bv().textMetrics(cell(cur.idx())->getText(0));
5130 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
5131 cur.setCurrentFont();
5134 if (sl == cur.top()) {
5135 // we trick it to go to forward after leaving the
5137 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
5140 if (cur.selIsMultiCell()) {
5141 cur.pit() = cur.lastpit();
5142 cur.pos() = cur.lastpos();
5143 cur.setCurrentFont();
5144 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5147 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5150 case LFUN_UP_SELECT:
5152 if (!(cur.selection() && cur.selIsMultiCell()))
5153 cell(cur.idx())->dispatch(cur, cmd);
5154 cur.dispatched(); // override the cell's decision
5155 if (sl == cur.top()) {
5156 // if our Text didn't do anything to the cursor
5157 // then we try to put the cursor into the cell above
5158 // setting also the right targetX.
5159 cur.selHandle(act == LFUN_UP_SELECT);
5160 if (tabular.cellRow(cur.idx()) != 0) {
5161 int const xtarget = cur.targetX();
5162 // WARNING: Once cur.idx() has been reset, the cursor is in
5163 // an inconsistent state until pos() has been set. Be careful
5164 // what you do with it!
5165 cur.idx() = tabular.cellAbove(cur.idx());
5166 cur.pit() = cur.lastpit();
5167 Text const * text = cell(cur.idx())->getText(0);
5168 TextMetrics const & tm = cur.bv().textMetrics(text);
5169 ParagraphMetrics const & pm =
5170 tm.parMetrics(cur.lastpit());
5171 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5172 cur.setCurrentFont();
5175 if (sl == cur.top()) {
5176 cmd = FuncRequest(LFUN_UP);
5179 if (cur.selIsMultiCell()) {
5181 cur.pos() = cur.lastpos();
5182 cur.setCurrentFont();
5183 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5186 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5189 case LFUN_LAYOUT_TABULAR:
5190 cur.bv().showDialog("tabular");
5193 case LFUN_INSET_MODIFY:
5194 // we come from the dialog
5195 if (cmd.getArg(0) == "tabular")
5196 tabularFeatures(cur, cmd.getLongArg(1));
5201 case LFUN_TABULAR_FEATURE:
5202 tabularFeatures(cur, to_utf8(cmd.argument()));
5205 // insert file functions
5206 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5207 case LFUN_FILE_INSERT_PLAINTEXT:
5209 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5210 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5211 FileName(to_utf8(cmd.argument())));
5214 cur.recordUndoInset();
5215 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5216 // content has been replaced,
5217 // so cursor might be invalid
5218 cur.pos() = cur.lastpos();
5219 cur.pit() = cur.lastpit();
5220 bvcur.setCursor(cur);
5227 if (cur.selIsMultiCell()) {
5228 if (copySelection(cur)) {
5229 cur.recordUndoInset();
5233 cell(cur.idx())->dispatch(cur, cmd);
5236 case LFUN_SELF_INSERT:
5237 if (cur.selIsMultiCell()) {
5238 cur.recordUndoInset();
5240 BufferView * bv = &cur.bv();
5241 docstring::const_iterator cit = cmd.argument().begin();
5242 docstring::const_iterator const end = cmd.argument().end();
5243 for (; cit != end; ++cit)
5244 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5247 bv->bookmarkEditPosition();
5249 cell(cur.idx())->dispatch(cur, cmd);
5252 case LFUN_CHAR_DELETE_BACKWARD:
5253 case LFUN_CHAR_DELETE_FORWARD:
5254 if (cur.selIsMultiCell()) {
5255 cur.recordUndoInset();
5258 cell(cur.idx())->dispatch(cur, cmd);
5262 if (!cur.selection())
5264 if (cur.selIsMultiCell()) {
5268 cell(cur.idx())->dispatch(cur, cmd);
5271 case LFUN_CLIPBOARD_PASTE:
5272 case LFUN_PRIMARY_SELECTION_PASTE: {
5273 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5274 theClipboard().getAsText(Clipboard::PlainTextType) :
5275 theSelection().get();
5278 // pass to InsertPlaintextString, but
5279 // only if we have multi-cell content
5280 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5281 cur.recordUndoInset();
5282 if (insertPlaintextString(cur.bv(), clip, false)) {
5283 // content has been replaced,
5284 // so cursor might be invalid
5285 cur.pos() = cur.lastpos();
5286 cur.pit() = cur.lastpit();
5287 bvcur.setCursor(cur);
5291 // Let the cell handle normal text
5292 cell(cur.idx())->dispatch(cur, cmd);
5297 if (!tabularStackDirty()) {
5298 // Check if we have plain text or HTML with rows/columns.
5299 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5300 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5301 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5302 docstring const clip =
5303 theClipboard().getAsText(Clipboard::AnyTextType);
5304 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5305 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5306 doDispatch(cur, ncmd);
5310 else if (theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5311 // This might be tabular data from another LyX instance. Check!
5312 docstring const clip =
5313 theClipboard().getAsText(Clipboard::PlainTextType);
5314 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5315 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5316 doDispatch(cur, ncmd);
5320 if (!cur.selIsMultiCell())
5321 cell(cur.idx())->dispatch(cur, cmd);
5324 if (theClipboard().isInternal()) {
5325 cur.recordUndoInset();
5326 pasteClipboard(cur);
5330 case LFUN_CHANGE_ACCEPT:
5331 case LFUN_CHANGE_REJECT:
5332 case LFUN_FONT_EMPH:
5333 case LFUN_FONT_BOLD:
5334 case LFUN_FONT_BOLDSYMBOL:
5335 case LFUN_FONT_ROMAN:
5336 case LFUN_FONT_NOUN:
5337 case LFUN_FONT_ITAL:
5338 case LFUN_FONT_FRAK:
5339 case LFUN_FONT_TYPEWRITER:
5340 case LFUN_FONT_SANS:
5341 case LFUN_TEXTSTYLE_APPLY:
5342 case LFUN_TEXTSTYLE_UPDATE:
5343 case LFUN_FONT_SIZE:
5344 case LFUN_FONT_UNDERLINE:
5345 case LFUN_FONT_STRIKEOUT:
5346 case LFUN_FONT_CROSSOUT:
5347 case LFUN_FONT_UNDERUNDERLINE:
5348 case LFUN_FONT_UNDERWAVE:
5350 case LFUN_PARAGRAPH_PARAMS_APPLY:
5351 case LFUN_PARAGRAPH_PARAMS:
5352 case LFUN_WORD_CAPITALIZE:
5353 case LFUN_WORD_UPCASE:
5354 case LFUN_WORD_LOWCASE:
5355 case LFUN_CHARS_TRANSPOSE: {
5356 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5357 if (cur.selIsMultiCell()) {
5360 getSelection(cur, rs, re, cs, ce);
5361 Cursor tmpcur = cur;
5362 for (row_type r = rs; r <= re; ++r) {
5363 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5364 // whole row selected
5365 if (act == LFUN_CHANGE_ACCEPT) {
5366 if (tabular.row_info[r].change.inserted())
5367 tabular.row_info[r].change.setUnchanged();
5368 else if (tabular.row_info[r].change.deleted()) {
5369 tabular.deleteRow(r, true);
5374 if (tabular.row_info[r].change.deleted())
5375 tabular.row_info[r].change.setUnchanged();
5376 else if (tabular.row_info[r].change.inserted()) {
5377 tabular.deleteRow(r, true);
5383 for (col_type c = cs; c <= ce; ++c) {
5384 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5385 // whole col selected
5386 if (act == LFUN_CHANGE_ACCEPT) {
5387 if (tabular.column_info[c].change.inserted())
5388 tabular.column_info[c].change.setUnchanged();
5389 else if (tabular.column_info[c].change.deleted()) {
5390 tabular.deleteColumn(c, true);
5395 if (tabular.column_info[c].change.deleted())
5396 tabular.column_info[c].change.setUnchanged();
5397 else if (tabular.column_info[c].change.inserted()) {
5398 tabular.deleteColumn(c, true);
5404 // cursor follows cell:
5405 tmpcur.idx() = tabular.cellIndex(r, c);
5406 // select this cell only:
5409 tmpcur.resetAnchor();
5410 tmpcur.pit() = tmpcur.lastpit();
5411 tmpcur.pos() = tmpcur.top().lastpos();
5412 tmpcur.setCursor(tmpcur);
5413 tmpcur.setSelection();
5414 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5418 tabular.updateIndexes();
5419 // cursor might be invalid
5421 // change bar might need to be redrawn
5422 cur.screenUpdateFlags(Update::Force);
5423 cur.forceBufferUpdate();
5427 cell(cur.idx())->dispatch(cur, cmd);
5432 case LFUN_CHANGE_NEXT:
5433 case LFUN_CHANGE_PREVIOUS: {
5434 // BufferView::dispatch has already moved the cursor, we just
5435 // need to select here if we have a changed row or column
5436 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5438 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5442 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5443 cur.pit() = cur.lastpit();
5444 cur.pos() = cur.lastpos();
5445 cur.selection(true);
5449 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5451 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5455 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5456 cur.pit() = cur.lastpit();
5457 cur.pos() = cur.lastpos();
5458 cur.selection(true);
5465 case LFUN_INSET_SETTINGS:
5466 // relay this lfun to Inset, not to the cell.
5467 Inset::doDispatch(cur, cmd);
5471 // we try to handle this event in the insets dispatch function.
5472 cell(cur.idx())->dispatch(cur, cmd);
5478 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5479 string const & argument, FuncStatus & status) const
5482 int action = Tabular::LAST_ACTION;
5484 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5485 if (tabularFeature[i].feature == s) {
5486 action = tabularFeature[i].action;
5490 if (action == Tabular::LAST_ACTION) {
5492 status.setUnknown(true);
5496 row_type sel_row_start = 0;
5497 row_type sel_row_end = 0;
5498 col_type sel_col_start = 0;
5499 col_type sel_col_end = 0;
5500 Tabular::ltType dummyltt;
5503 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5506 case Tabular::SET_PWIDTH:
5507 case Tabular::SET_MPWIDTH:
5508 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5509 case Tabular::SET_SPECIAL_COLUMN:
5510 case Tabular::SET_SPECIAL_MULTICOLUMN:
5511 case Tabular::APPEND_ROW:
5512 case Tabular::APPEND_COLUMN:
5513 case Tabular::DELETE_ROW:
5514 case Tabular::DELETE_COLUMN:
5515 case Tabular::COPY_ROW:
5516 case Tabular::COPY_COLUMN:
5517 case Tabular::SET_TOP_SPACE:
5518 case Tabular::SET_BOTTOM_SPACE:
5519 case Tabular::SET_INTERLINE_SPACE:
5523 case Tabular::SET_TABULAR_WIDTH:
5524 status.setEnabled(!tabular.rotate
5525 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5528 case Tabular::MOVE_COLUMN_RIGHT:
5529 case Tabular::MOVE_COLUMN_LEFT:
5530 case Tabular::MOVE_ROW_DOWN:
5531 case Tabular::MOVE_ROW_UP: {
5532 if (cur.selection()) {
5533 status.message(_("Selections not supported."));
5534 status.setEnabled(false);
5538 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
5539 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
5540 (action == Tabular::MOVE_COLUMN_LEFT &&
5541 tabular.cellColumn(cur.idx()) == 0) ||
5542 (action == Tabular::MOVE_ROW_DOWN &&
5543 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
5544 (action == Tabular::MOVE_ROW_UP &&
5545 tabular.cellRow(cur.idx()) == 0)) {
5546 status.setEnabled(false);
5550 if (action == Tabular::MOVE_COLUMN_RIGHT ||
5551 action == Tabular::MOVE_COLUMN_LEFT) {
5552 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
5553 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
5554 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
5555 status.message(_("Multi-column in current or"
5556 " destination column."));
5557 status.setEnabled(false);
5562 if (action == Tabular::MOVE_ROW_DOWN ||
5563 action == Tabular::MOVE_ROW_UP) {
5564 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
5565 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
5566 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
5567 status.message(_("Multi-row in current or"
5568 " destination row."));
5569 status.setEnabled(false);
5574 status.setEnabled(true);
5578 case Tabular::SET_DECIMAL_POINT:
5580 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5583 case Tabular::SET_MULTICOLUMN:
5584 case Tabular::UNSET_MULTICOLUMN:
5585 case Tabular::MULTICOLUMN:
5586 // If a row is set as longtable caption, it must not be allowed
5587 // to unset that this row is a multicolumn.
5588 status.setEnabled(sel_row_start == sel_row_end
5589 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5590 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5593 case Tabular::SET_MULTIROW:
5594 case Tabular::UNSET_MULTIROW:
5595 case Tabular::MULTIROW:
5596 // If a row is set as longtable caption, it must not be allowed
5597 // to unset that this row is a multirow.
5598 status.setEnabled(sel_col_start == sel_col_end
5599 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5600 status.setOnOff(tabular.isMultiRow(cur.idx()));
5603 case Tabular::SET_ALL_LINES:
5604 case Tabular::UNSET_ALL_LINES:
5605 case Tabular::SET_INNER_LINES:
5606 case Tabular::SET_BORDER_LINES:
5607 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5610 case Tabular::RESET_FORMAL_DEFAULT:
5611 status.setEnabled(tabular.use_booktabs);
5614 case Tabular::SET_LINE_TOP:
5615 case Tabular::SET_LINE_BOTTOM:
5616 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5619 case Tabular::SET_LINE_LEFT:
5620 case Tabular::SET_LINE_RIGHT:
5621 status.setEnabled(!tabular.use_booktabs
5622 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5625 case Tabular::SET_LTRIM_TOP:
5626 case Tabular::SET_RTRIM_TOP:
5627 status.setEnabled(tabular.use_booktabs
5628 && tabular.cellRow(cur.idx()) != 0
5629 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5632 case Tabular::SET_LTRIM_BOTTOM:
5633 case Tabular::SET_RTRIM_BOTTOM:
5634 status.setEnabled(tabular.use_booktabs
5635 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5636 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5639 case Tabular::TOGGLE_LINE_TOP:
5640 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5641 status.setOnOff(tabular.topLine(cur.idx()));
5644 case Tabular::TOGGLE_LINE_BOTTOM:
5645 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5646 status.setOnOff(tabular.bottomLine(cur.idx()));
5649 case Tabular::TOGGLE_LINE_LEFT:
5650 status.setEnabled(!tabular.use_booktabs
5651 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5652 status.setOnOff(tabular.leftLine(cur.idx()));
5655 case Tabular::TOGGLE_LINE_RIGHT:
5656 status.setEnabled(!tabular.use_booktabs
5657 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5658 status.setOnOff(tabular.rightLine(cur.idx()));
5661 case Tabular::TOGGLE_LTRIM_TOP:
5662 status.setEnabled(tabular.use_booktabs
5663 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5664 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5667 case Tabular::TOGGLE_RTRIM_TOP:
5668 status.setEnabled(tabular.use_booktabs
5669 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5670 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5673 case Tabular::TOGGLE_LTRIM_BOTTOM:
5674 status.setEnabled(tabular.use_booktabs
5675 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5676 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5679 case Tabular::TOGGLE_RTRIM_BOTTOM:
5680 status.setEnabled(tabular.use_booktabs
5681 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5682 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5685 // multirow cells only inherit the alignment of the column if the column has
5686 // no width, otherwise they are left-aligned
5687 // therefore allow always left but right and center only if there is no width
5688 case Tabular::M_ALIGN_LEFT:
5691 case Tabular::ALIGN_LEFT:
5692 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5695 case Tabular::M_ALIGN_RIGHT:
5698 case Tabular::ALIGN_RIGHT:
5699 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5700 && !tabular.getPWidth(cur.idx()).zero()));
5701 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5704 case Tabular::M_ALIGN_CENTER:
5707 case Tabular::ALIGN_CENTER:
5708 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5709 && !tabular.getPWidth(cur.idx()).zero()));
5710 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5713 case Tabular::ALIGN_BLOCK:
5714 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5715 && !tabular.isMultiRow(cur.idx()));
5716 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5719 case Tabular::ALIGN_DECIMAL:
5720 status.setEnabled(!tabular.isMultiRow(cur.idx())
5721 && !tabular.isMultiColumn(cur.idx()));
5722 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5725 case Tabular::M_VALIGN_TOP:
5728 case Tabular::VALIGN_TOP:
5729 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5730 && !tabular.isMultiRow(cur.idx()));
5732 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5735 case Tabular::M_VALIGN_BOTTOM:
5738 case Tabular::VALIGN_BOTTOM:
5739 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5740 && !tabular.isMultiRow(cur.idx()));
5742 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5745 case Tabular::M_VALIGN_MIDDLE:
5748 case Tabular::VALIGN_MIDDLE:
5749 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5750 && !tabular.isMultiRow(cur.idx()));
5752 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5755 case Tabular::SET_LONGTABULAR:
5756 case Tabular::TOGGLE_LONGTABULAR:
5757 // setting as longtable is not allowed when table is inside a float
5758 if (cur.innerInsetOfType(FLOAT_CODE) != 0
5759 || cur.innerInsetOfType(WRAP_CODE) != 0)
5760 status.setEnabled(false);
5762 status.setEnabled(true);
5763 status.setOnOff(tabular.is_long_tabular);
5766 case Tabular::UNSET_LONGTABULAR:
5767 status.setOnOff(!tabular.is_long_tabular);
5770 case Tabular::TOGGLE_ROTATE_TABULAR:
5771 case Tabular::SET_ROTATE_TABULAR:
5772 status.setOnOff(tabular.rotate != 0);
5775 case Tabular::TABULAR_VALIGN_TOP:
5776 status.setEnabled(tabular.tabular_width.zero());
5777 status.setOnOff(tabular.tabular_valignment
5778 == Tabular::LYX_VALIGN_TOP);
5780 case Tabular::TABULAR_VALIGN_MIDDLE:
5781 status.setEnabled(tabular.tabular_width.zero());
5782 status.setOnOff(tabular.tabular_valignment
5783 == Tabular::LYX_VALIGN_MIDDLE);
5785 case Tabular::TABULAR_VALIGN_BOTTOM:
5786 status.setEnabled(tabular.tabular_width.zero());
5787 status.setOnOff(tabular.tabular_valignment
5788 == Tabular::LYX_VALIGN_BOTTOM);
5791 case Tabular::LONGTABULAR_ALIGN_LEFT:
5792 status.setOnOff(tabular.longtabular_alignment
5793 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5795 case Tabular::LONGTABULAR_ALIGN_CENTER:
5796 status.setOnOff(tabular.longtabular_alignment
5797 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5799 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5800 status.setOnOff(tabular.longtabular_alignment
5801 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5804 case Tabular::UNSET_ROTATE_TABULAR:
5805 status.setOnOff(tabular.rotate == 0);
5808 case Tabular::TOGGLE_ROTATE_CELL:
5809 case Tabular::SET_ROTATE_CELL:
5810 status.setOnOff(!oneCellHasRotationState(false,
5811 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5814 case Tabular::UNSET_ROTATE_CELL:
5815 status.setOnOff(!oneCellHasRotationState(true,
5816 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5819 case Tabular::SET_USEBOX:
5820 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5823 // every row can only be one thing:
5824 // either a footer or header
5825 case Tabular::SET_LTFIRSTHEAD:
5826 status.setEnabled(sel_row_start == sel_row_end);
5827 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5830 case Tabular::UNSET_LTFIRSTHEAD:
5831 status.setEnabled(sel_row_start == sel_row_end);
5832 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5835 case Tabular::SET_LTHEAD:
5836 status.setEnabled(sel_row_start == sel_row_end);
5837 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5840 case Tabular::UNSET_LTHEAD:
5841 status.setEnabled(sel_row_start == sel_row_end);
5842 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5845 case Tabular::SET_LTFOOT:
5846 status.setEnabled(sel_row_start == sel_row_end);
5847 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5850 case Tabular::UNSET_LTFOOT:
5851 status.setEnabled(sel_row_start == sel_row_end);
5852 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5855 case Tabular::SET_LTLASTFOOT:
5856 status.setEnabled(sel_row_start == sel_row_end);
5857 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5860 case Tabular::UNSET_LTLASTFOOT:
5861 status.setEnabled(sel_row_start == sel_row_end);
5862 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5865 case Tabular::SET_LTNEWPAGE:
5866 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5868 case Tabular::UNSET_LTNEWPAGE:
5869 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5872 // only one row in head/firsthead/foot/lasthead can be the caption
5873 // and a multirow cannot be set as caption
5874 case Tabular::SET_LTCAPTION:
5875 status.setEnabled(sel_row_start == sel_row_end
5876 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5877 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5878 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5879 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5880 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5881 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5882 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5883 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5884 && !tabular.isMultiRow(sel_row_start));
5885 status.setOnOff(tabular.ltCaption(sel_row_start));
5888 case Tabular::UNSET_LTCAPTION:
5889 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5892 case Tabular::TOGGLE_LTCAPTION:
5893 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5894 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5895 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5896 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5897 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5898 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5899 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5900 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5901 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5902 status.setOnOff(tabular.ltCaption(sel_row_start));
5905 case Tabular::TOGGLE_BOOKTABS:
5906 case Tabular::SET_BOOKTABS:
5907 status.setOnOff(tabular.use_booktabs);
5910 case Tabular::UNSET_BOOKTABS:
5911 status.setOnOff(!tabular.use_booktabs);
5916 status.setEnabled(false);
5923 // function sets an object as defined in FuncStatus.h:
5924 // states OK, Unknown, Disabled, On, Off.
5925 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5926 FuncStatus & status) const
5928 switch (cmd.action()) {
5929 case LFUN_INSET_MODIFY:
5930 if (cmd.getArg(0) != "tabular")
5932 if (cmd.getArg(1) == "for-dialog") {
5933 // The dialog is asking the status of a command
5934 if (&cur.inset() != this)
5936 string action = cmd.getArg(2);
5937 string arg = cmd.getLongArg(3);
5938 return getFeatureStatus(cur, action, arg, status);
5940 // We always enable the lfun if it is coming from the dialog
5941 // because the dialog makes sure all the settings are valid,
5942 // even though the first argument might not be valid now.
5943 status.setEnabled(true);
5947 case LFUN_TABULAR_FEATURE: {
5948 if (&cur.inset() != this)
5950 string action = cmd.getArg(0);
5951 string arg = cmd.getLongArg(1);
5952 return getFeatureStatus(cur, action, arg, status);
5955 case LFUN_CAPTION_INSERT: {
5956 // caption is only allowed in caption cell of longtable
5957 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5958 status.setEnabled(false);
5961 // only standard caption is allowed
5962 string arg = cmd.getArg(0);
5963 if (!arg.empty() && arg != "Standard") {
5964 status.setEnabled(false);
5967 // check if there is already a caption
5968 bool have_caption = false;
5969 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5970 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5971 ParagraphList::const_iterator pend = itc.paragraphs().end();
5972 for (; pit != pend; ++pit) {
5973 InsetList::const_iterator it = pit->insetList().begin();
5974 InsetList::const_iterator end = pit->insetList().end();
5975 for (; it != end; ++it) {
5976 if (it->inset->lyxCode() == CAPTION_CODE) {
5977 have_caption = true;
5982 status.setEnabled(!have_caption);
5986 // These are only enabled inside tabular
5987 case LFUN_CELL_BACKWARD:
5988 case LFUN_CELL_FORWARD:
5989 status.setEnabled(true);
5992 // disable these with multiple cells selected
5993 case LFUN_INSET_INSERT:
5994 case LFUN_TABULAR_INSERT:
5995 case LFUN_TABULAR_STYLE_INSERT:
5996 case LFUN_FLEX_INSERT:
5997 case LFUN_FLOAT_INSERT:
5998 case LFUN_FLOAT_WIDE_INSERT:
5999 case LFUN_FOOTNOTE_INSERT:
6000 case LFUN_MARGINALNOTE_INSERT:
6001 case LFUN_MATH_INSERT:
6002 case LFUN_MATH_MODE:
6003 case LFUN_MATH_MUTATE:
6004 case LFUN_MATH_DISPLAY:
6005 case LFUN_NOTE_INSERT:
6006 case LFUN_ARGUMENT_INSERT:
6007 case LFUN_BOX_INSERT:
6008 case LFUN_BRANCH_INSERT:
6009 case LFUN_PHANTOM_INSERT:
6010 case LFUN_WRAP_INSERT:
6011 case LFUN_PREVIEW_INSERT:
6012 case LFUN_ERT_INSERT: {
6013 if (cur.selIsMultiCell()) {
6014 status.setEnabled(false);
6017 return cell(cur.idx())->getStatus(cur, cmd, status);
6020 case LFUN_CHANGE_ACCEPT:
6021 case LFUN_CHANGE_REJECT: {
6022 if (cur.selIsMultiCell()) {
6025 getSelection(cur, rs, re, cs, ce);
6026 for (row_type r = rs; r <= re; ++r) {
6027 if (tabular.row_info[r].change.changed()) {
6028 status.setEnabled(true);
6031 for (col_type c = cs; c <= ce; ++c) {
6032 if (tabular.column_info[c].change.changed()) {
6033 status.setEnabled(true);
6039 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
6040 status.setEnabled(true);
6043 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
6044 status.setEnabled(true);
6048 return cell(cur.idx())->getStatus(cur, cmd, status);
6051 // disable in non-fixed-width cells
6052 case LFUN_PARAGRAPH_BREAK:
6053 // multirow does not allow paragraph breaks
6054 if (tabular.isMultiRow(cur.idx())) {
6055 status.setEnabled(false);
6059 case LFUN_NEWLINE_INSERT:
6060 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
6061 && tabular.getPWidth(cur.idx()).zero()) {
6062 status.setEnabled(false);
6065 return cell(cur.idx())->getStatus(cur, cmd, status);
6067 case LFUN_NEWPAGE_INSERT:
6068 status.setEnabled(false);
6072 if (tabularStackDirty() && theClipboard().isInternal()) {
6073 if (cur.selIsMultiCell()) {
6076 getSelection(cur, rs, re, cs, ce);
6077 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
6078 && paste_tabular->nrows() == re - rs + 1)
6079 status.setEnabled(true);
6081 status.setEnabled(false);
6082 status.message(_("Selection size should match clipboard content."));
6085 status.setEnabled(true);
6088 return cell(cur.idx())->getStatus(cur, cmd, status);
6090 case LFUN_INSET_SETTINGS:
6091 // relay this lfun to Inset, not to the cell.
6092 return Inset::getStatus(cur, cmd, status);
6095 // we try to handle this event in the insets dispatch function.
6096 return cell(cur.idx())->getStatus(cur, cmd, status);
6102 Inset::RowFlags InsetTabular::rowFlags() const
6104 if (tabular.is_long_tabular) {
6105 switch (tabular.longtabular_alignment) {
6106 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
6107 return Display | AlignLeft;
6108 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
6110 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
6111 return Display | AlignRight;
6120 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
6122 tabular.latex(os, runparams);
6126 int InsetTabular::plaintext(odocstringstream & os,
6127 OutputParams const & runparams, size_t max_length) const
6129 os << '\n'; // output table on a new line
6130 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
6131 tabular.plaintext(os, runparams, dp, false, 0, max_length);
6132 return PLAINTEXT_NEWLINE;
6136 void InsetTabular::docbook(XMLStream & xs, OutputParams const & runparams) const
6138 tabular.docbook(xs, runparams);
6142 docstring InsetTabular::xhtml(XMLStream & xs, OutputParams const & rp) const
6144 return tabular.xhtml(xs, rp);
6148 void InsetTabular::validate(LaTeXFeatures & features) const
6150 tabular.validate(features);
6151 features.useInsetLayout(getLayout());
6155 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6157 return tabular.cellInset(idx);
6161 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6163 return tabular.cellInset(idx);
6167 void InsetTabular::cursorPos(BufferView const & bv,
6168 CursorSlice const & sl, bool boundary, int & x, int & y) const
6170 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6172 // y offset correction
6173 y += cellYPos(sl.idx());
6174 y += tabular.textVOffset(sl.idx());
6175 y += tabular.offsetVAlignment();
6177 // x offset correction
6178 x += cellXPos(sl.idx());
6179 x += tabular.textHOffset(sl.idx());
6180 x += ADD_TO_TABULAR_WIDTH;
6184 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6188 Inset const & inset = *tabular.cellInset(cell);
6189 Point o = bv.coordCache().getInsets().xy(&inset);
6190 int const xbeg = o.x_ - tabular.textHOffset(cell);
6191 int const xend = xbeg + tabular.cellWidth(cell);
6192 row_type const row = tabular.cellRow(cell);
6193 int const ybeg = o.y_ - tabular.rowAscent(row)
6194 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6195 int const yend = ybeg + tabular.cellHeight(cell);
6207 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6208 // << " ybeg=" << ybeg << " yend=" << yend
6209 // << " xx=" << xx << " yy=" << yy
6210 // << " dist=" << xx + yy << endl;
6215 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6217 //lyxerr << "InsetTabular::editXY: " << this << endl;
6219 cur.idx() = getNearestCell(cur.bv(), x, y);
6220 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6224 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6226 cur.idx() = getNearestCell(cur.bv(), x, y);
6227 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6231 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6233 idx_type idx_min = 0;
6234 int dist_min = numeric_limits<int>::max();
6235 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6236 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6237 int const d = dist(bv, i, x, y);
6248 int InsetTabular::cellYPos(idx_type const cell) const
6250 row_type row = tabular.cellRow(cell);
6252 for (row_type r = 0; r < row; ++r)
6253 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6254 + tabular.interRowSpace(r + 1);
6259 int InsetTabular::cellXPos(idx_type const cell) const
6261 col_type col = tabular.cellColumn(cell);
6263 for (col_type c = 0; c < col; ++c)
6264 lx += tabular.column_info[c].width;
6269 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6271 row_type const row = tabular.cellRow(cur.idx());
6272 col_type const col = tabular.cellColumn(cur.idx());
6274 if (isRightToLeft(cur)) {
6275 if (tabular.cellColumn(cur.idx()) == 0) {
6276 if (row == tabular.nrows() - 1)
6278 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6283 cur.idx() = tabular.getLastCellInRow(row - 1);
6285 cur.idx() = tabular.cellIndex(row, col - 1);
6288 if (tabular.isLastCell(cur.idx()))
6290 if (cur.idx() == tabular.getLastCellInRow(row))
6291 cur.idx() = tabular.cellIndex(row + 1, 0);
6293 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6294 cur.idx() = tabular.cellIndex(row, colnextcell);
6298 cur.boundary(false);
6300 if (cur.selIsMultiCell()) {
6301 cur.pit() = cur.lastpit();
6302 cur.pos() = cur.lastpos();
6309 // in visual mode, place cursor at extreme left or right
6311 switch(entry_from) {
6313 case ENTRY_DIRECTION_RIGHT:
6314 cur.posVisToRowExtremity(false /* !left */);
6316 case ENTRY_DIRECTION_LEFT:
6317 cur.posVisToRowExtremity(true /* left */);
6319 case ENTRY_DIRECTION_IGNORE:
6320 // nothing to do in this case
6324 cur.setCurrentFont();
6328 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6330 row_type const row = tabular.cellRow(cur.idx());
6331 col_type const col = tabular.cellColumn(cur.idx());
6333 if (isRightToLeft(cur)) {
6334 if (cur.idx() == tabular.getLastCellInRow(row)) {
6337 cur.idx() = tabular.getFirstCellInRow(row);
6338 cur.idx() = tabular.cellAbove(cur.idx());
6340 if (tabular.isLastCell(cur.idx()))
6342 if (cur.idx() == tabular.getLastCellInRow(row))
6343 cur.idx() = tabular.cellIndex(row + 1, 0);
6345 cur.idx() = tabular.cellIndex(row, col + 1);
6348 if (cur.idx() == 0) // first cell
6351 cur.idx() = tabular.getLastCellInRow(row - 1);
6353 cur.idx() = tabular.cellIndex(row, col - 1);
6356 if (cur.selIsMultiCell()) {
6357 cur.pit() = cur.lastpit();
6358 cur.pos() = cur.lastpos();
6362 cur.pit() = cur.lastpit();
6363 cur.pos() = cur.lastpos();
6365 // in visual mode, place cursor at extreme left or right
6367 switch(entry_from) {
6369 case ENTRY_DIRECTION_RIGHT:
6370 cur.posVisToRowExtremity(false /* !left */);
6372 case ENTRY_DIRECTION_LEFT:
6373 cur.posVisToRowExtremity(true /* left */);
6375 case ENTRY_DIRECTION_IGNORE:
6376 // nothing to do in this case
6380 cur.setCurrentFont();
6384 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6386 cur.recordUndoInset(this);
6388 istringstream is(argument);
6389 // limit the size of strings we read to avoid memory problems
6393 size_t safe_guard = 0;
6398 if (safe_guard > 1000) {
6399 LYXERR0("parameter max count reached!");
6403 Tabular::Feature action = Tabular::LAST_ACTION;
6406 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6407 if (s != tabularFeature[i].feature)
6409 action = tabularFeature[i].action;
6412 if (action == Tabular::LAST_ACTION) {
6413 LYXERR0("Feature not found " << s);
6417 if (tabularFeature[i].need_value)
6419 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6420 tabularFeatures(cur, action, val);
6425 static void checkLongtableSpecial(Tabular::ltType & ltt,
6426 string const & special, bool & flag)
6428 if (special == "dl_above") {
6431 } else if (special == "dl_below") {
6432 ltt.bottomDL = flag;
6434 } else if (special == "empty") {
6444 bool InsetTabular::oneCellHasRotationState(bool rotated,
6445 row_type row_start, row_type row_end,
6446 col_type col_start, col_type col_end) const
6448 for (row_type r = row_start; r <= row_end; ++r)
6449 for (col_type c = col_start; c <= col_end; ++c)
6451 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6454 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6461 void InsetTabular::tabularFeatures(Cursor & cur,
6462 Tabular::Feature feature, string const & value)
6464 col_type sel_col_start;
6465 col_type sel_col_end;
6466 row_type sel_row_start;
6467 row_type sel_row_end;
6468 bool setLines = false;
6469 bool setLinesInnerOnly = false;
6470 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6471 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6475 case Tabular::M_ALIGN_LEFT:
6476 case Tabular::ALIGN_LEFT:
6477 setAlign = LYX_ALIGN_LEFT;
6480 case Tabular::M_ALIGN_RIGHT:
6481 case Tabular::ALIGN_RIGHT:
6482 setAlign = LYX_ALIGN_RIGHT;
6485 case Tabular::M_ALIGN_CENTER:
6486 case Tabular::ALIGN_CENTER:
6487 setAlign = LYX_ALIGN_CENTER;
6490 case Tabular::ALIGN_BLOCK:
6491 setAlign = LYX_ALIGN_BLOCK;
6494 case Tabular::ALIGN_DECIMAL:
6495 setAlign = LYX_ALIGN_DECIMAL;
6498 case Tabular::M_VALIGN_TOP:
6499 case Tabular::VALIGN_TOP:
6500 setVAlign = Tabular::LYX_VALIGN_TOP;
6503 case Tabular::M_VALIGN_BOTTOM:
6504 case Tabular::VALIGN_BOTTOM:
6505 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6508 case Tabular::M_VALIGN_MIDDLE:
6509 case Tabular::VALIGN_MIDDLE:
6510 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6517 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6518 row_type const row = tabular.cellRow(cur.idx());
6519 col_type const column = tabular.cellColumn(cur.idx());
6521 Tabular::ltType ltt;
6525 case Tabular::SET_TABULAR_WIDTH:
6526 tabular.setTabularWidth(Length(value));
6529 case Tabular::SET_PWIDTH: {
6530 Length const len(value);
6531 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6532 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6534 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6535 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6540 case Tabular::SET_MPWIDTH:
6541 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6544 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6545 bool const varwidth = value == "on";
6546 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6547 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6551 case Tabular::SET_MROFFSET:
6552 tabular.setMROffset(cur, cur.idx(), Length(value));
6555 case Tabular::SET_SPECIAL_COLUMN:
6556 case Tabular::SET_SPECIAL_MULTICOLUMN:
6557 if (value == "none")
6558 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6560 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6563 case Tabular::APPEND_ROW:
6564 // append the row into the tabular
6565 tabular.appendRow(row);
6568 case Tabular::APPEND_COLUMN:
6569 // append the column into the tabular
6570 tabular.appendColumn(column);
6571 cur.idx() = tabular.cellIndex(row, column);
6574 case Tabular::DELETE_ROW:
6575 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6576 for (col_type c = 0; c < tabular.ncols(); c++) {
6577 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6578 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6579 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6580 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6584 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6585 tabular.deleteRow(sel_row_start);
6586 if (sel_row_start >= tabular.nrows())
6588 cur.idx() = tabular.cellIndex(sel_row_start, column);
6591 cur.selection(false);
6594 case Tabular::DELETE_COLUMN:
6595 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6596 for (row_type r = 0; r < tabular.nrows(); r++)
6597 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6598 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6601 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6602 for (row_type r = 0; r < tabular.nrows(); r++)
6603 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6604 tabular.leftLine(tabular.cellIndex(r, 0)));
6607 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6608 tabular.deleteColumn(sel_col_start);
6609 if (sel_col_start >= tabular.ncols())
6611 cur.idx() = tabular.cellIndex(row, sel_col_start);
6614 cur.selection(false);
6617 case Tabular::COPY_ROW:
6618 tabular.copyRow(row);
6621 case Tabular::COPY_COLUMN:
6622 tabular.copyColumn(column);
6623 cur.idx() = tabular.cellIndex(row, column);
6626 case Tabular::MOVE_COLUMN_RIGHT:
6627 tabular.moveColumn(column, Tabular::RIGHT);
6628 cur.idx() = tabular.cellIndex(row, column + 1);
6631 case Tabular::MOVE_COLUMN_LEFT:
6632 tabular.moveColumn(column, Tabular::LEFT);
6633 cur.idx() = tabular.cellIndex(row, column - 1);
6636 case Tabular::MOVE_ROW_DOWN:
6637 tabular.moveRow(row, Tabular::DOWN);
6638 cur.idx() = tabular.cellIndex(row + 1, column);
6641 case Tabular::MOVE_ROW_UP:
6642 tabular.moveRow(row, Tabular::UP);
6643 cur.idx() = tabular.cellIndex(row - 1, column);
6646 case Tabular::SET_LINE_TOP:
6647 case Tabular::TOGGLE_LINE_TOP: {
6648 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6649 ? (value == "true") : !tabular.topLine(cur.idx());
6650 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6651 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6652 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6656 case Tabular::SET_LINE_BOTTOM:
6657 case Tabular::TOGGLE_LINE_BOTTOM: {
6658 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6659 ? (value == "true") : !tabular.bottomLine(cur.idx());
6660 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6661 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6662 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6666 case Tabular::SET_LTRIM_TOP:
6667 case Tabular::TOGGLE_LTRIM_TOP: {
6668 bool l = (feature == Tabular::SET_LTRIM_TOP)
6669 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6670 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6671 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6672 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6676 case Tabular::SET_RTRIM_TOP:
6677 case Tabular::TOGGLE_RTRIM_TOP: {
6678 bool l = (feature == Tabular::SET_RTRIM_TOP)
6679 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6680 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6681 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6682 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6686 case Tabular::SET_LTRIM_BOTTOM:
6687 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6688 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6689 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6690 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6691 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6692 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6696 case Tabular::SET_RTRIM_BOTTOM:
6697 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6698 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6699 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6700 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6701 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6702 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6706 case Tabular::SET_LINE_LEFT:
6707 case Tabular::TOGGLE_LINE_LEFT: {
6708 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6709 ? (value == "true") : !tabular.leftLine(cur.idx());
6710 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6711 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6712 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6716 case Tabular::SET_LINE_RIGHT:
6717 case Tabular::TOGGLE_LINE_RIGHT: {
6718 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6719 ? (value == "true") : !tabular.rightLine(cur.idx());
6720 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6721 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6722 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6726 case Tabular::M_ALIGN_LEFT:
6727 case Tabular::M_ALIGN_RIGHT:
6728 case Tabular::M_ALIGN_CENTER:
6729 case Tabular::ALIGN_LEFT:
6730 case Tabular::ALIGN_RIGHT:
6731 case Tabular::ALIGN_CENTER:
6732 case Tabular::ALIGN_BLOCK:
6733 case Tabular::ALIGN_DECIMAL:
6734 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6735 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6736 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6737 !tabular.getPWidth(c).zero());
6740 case Tabular::M_VALIGN_TOP:
6741 case Tabular::M_VALIGN_BOTTOM:
6742 case Tabular::M_VALIGN_MIDDLE:
6745 case Tabular::VALIGN_TOP:
6746 case Tabular::VALIGN_BOTTOM:
6747 case Tabular::VALIGN_MIDDLE:
6748 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6749 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6750 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6753 case Tabular::SET_MULTICOLUMN: {
6754 if (!cur.selection()) {
6755 // just multicol for one single cell
6756 // check whether we are completely in a multicol
6757 if (!tabular.isMultiColumn(cur.idx()))
6758 tabular.setMultiColumn(cur, cur.idx(), 1,
6759 tabular.rightLine(cur.idx()));
6762 // we have a selection so this means we just add all these
6763 // cells to form a multicolumn cell
6764 idx_type const s_start = cur.selBegin().idx();
6765 row_type const col_start = tabular.cellColumn(s_start);
6766 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6767 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6768 tabular.rightLine(cur.selEnd().idx()));
6771 cur.selection(false);
6775 case Tabular::UNSET_MULTICOLUMN: {
6776 if (!cur.selection()) {
6777 if (tabular.isMultiColumn(cur.idx()))
6778 tabular.unsetMultiColumn(cur.idx());
6783 case Tabular::MULTICOLUMN: {
6784 if (!cur.selection()) {
6785 if (tabular.isMultiColumn(cur.idx()))
6786 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6788 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6792 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6793 row_type const r = sel_row_start;
6794 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6795 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6798 // If the selection contains at least one singlecol cell
6799 // or multiple multicol cells,
6800 // we assume the user will merge is to a single multicol
6802 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6804 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6808 case Tabular::SET_MULTIROW: {
6809 if (!cur.selection()) {
6810 // just multirow for one single cell
6811 // check whether we are completely in a multirow
6812 if (!tabular.isMultiRow(cur.idx()))
6813 tabular.setMultiRow(cur, cur.idx(), 1,
6814 tabular.bottomLine(cur.idx()),
6815 tabular.getAlignment(cur.idx()));
6818 // we have a selection so this means we just add all this
6819 // cells to form a multirow cell
6820 idx_type const s_start = cur.selBegin().idx();
6821 row_type const row_start = tabular.cellRow(s_start);
6822 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6823 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6824 tabular.bottomLine(cur.selEnd().idx()),
6825 tabular.getAlignment(cur.selEnd().idx()));
6828 cur.selection(false);
6832 case Tabular::UNSET_MULTIROW: {
6833 if (!cur.selection()) {
6834 if (tabular.isMultiRow(cur.idx()))
6835 tabular.unsetMultiRow(cur.idx());
6840 case Tabular::MULTIROW: {
6841 if (!cur.selection()) {
6842 if (tabular.isMultiRow(cur.idx()))
6843 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6845 tabularFeatures(cur, Tabular::SET_MULTIROW);
6849 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6850 col_type const c = sel_col_start;
6851 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6852 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6855 // If the selection contains at least one singlerow cell
6856 // or multiple multirow cells,
6857 // we assume the user will merge is to a single multirow
6859 tabularFeatures(cur, Tabular::SET_MULTIROW);
6861 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6865 case Tabular::SET_INNER_LINES:
6866 setLinesInnerOnly = true;
6868 case Tabular::SET_ALL_LINES:
6871 case Tabular::UNSET_ALL_LINES:
6872 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6873 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6874 idx_type const cell = tabular.cellIndex(r, c);
6875 if (!setLinesInnerOnly || r != sel_row_start)
6876 tabular.setTopLine(cell, setLines);
6877 if ((!setLinesInnerOnly || r != sel_row_end)
6878 && (!setLines || r == sel_row_end))
6879 tabular.setBottomLine(cell, setLines);
6880 if ((!setLinesInnerOnly || c != sel_col_end)
6881 && (!setLines || c == sel_col_end))
6882 tabular.setRightLine(cell, setLines);
6883 if ((!setLinesInnerOnly || c != sel_col_start))
6884 tabular.setLeftLine(cell, setLines);
6888 case Tabular::RESET_FORMAL_DEFAULT:
6889 for (row_type r = 0; r < tabular.nrows(); ++r) {
6890 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6891 for (col_type c = 0; c < tabular.ncols(); ++c) {
6892 idx_type const cell = tabular.cellIndex(r, c);
6893 tabular.setTopLine(cell, head_or_foot);
6894 tabular.setBottomLine(cell, head_or_foot);
6899 case Tabular::SET_BORDER_LINES:
6900 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6901 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6902 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6904 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6905 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6906 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6910 case Tabular::TOGGLE_LONGTABULAR:
6911 if (tabular.is_long_tabular)
6912 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6914 tabular.is_long_tabular = true;
6917 case Tabular::SET_LONGTABULAR:
6918 tabular.is_long_tabular = true;
6921 case Tabular::UNSET_LONGTABULAR:
6922 for (row_type r = 0; r < tabular.nrows(); ++r) {
6923 if (tabular.ltCaption(r)) {
6924 cur.idx() = tabular.cellIndex(r, 0);
6927 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6930 tabular.is_long_tabular = false;
6933 case Tabular::SET_ROTATE_TABULAR:
6934 tabular.rotate = convert<int>(value);
6937 case Tabular::UNSET_ROTATE_TABULAR:
6941 case Tabular::TOGGLE_ROTATE_TABULAR:
6942 // when pressing the rotate button we default to 90° rotation
6943 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6946 case Tabular::TABULAR_VALIGN_TOP:
6947 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6950 case Tabular::TABULAR_VALIGN_MIDDLE:
6951 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6954 case Tabular::TABULAR_VALIGN_BOTTOM:
6955 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6958 case Tabular::LONGTABULAR_ALIGN_LEFT:
6959 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6962 case Tabular::LONGTABULAR_ALIGN_CENTER:
6963 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6966 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6967 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6970 case Tabular::SET_ROTATE_CELL:
6971 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6972 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6973 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6976 case Tabular::UNSET_ROTATE_CELL:
6977 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6978 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6979 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6982 case Tabular::TOGGLE_ROTATE_CELL:
6984 bool oneNotRotated = oneCellHasRotationState(false,
6985 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6987 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6988 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6989 // when pressing the rotate cell button we default to 90° rotation
6991 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6993 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6998 case Tabular::SET_USEBOX: {
6999 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
7000 if (val == tabular.getUsebox(cur.idx()))
7001 val = Tabular::BOX_NONE;
7002 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7003 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7004 tabular.setUsebox(tabular.cellIndex(r, c), val);
7008 case Tabular::UNSET_LTFIRSTHEAD:
7011 case Tabular::SET_LTFIRSTHEAD:
7012 tabular.getRowOfLTFirstHead(row, ltt);
7013 checkLongtableSpecial(ltt, value, flag);
7014 tabular.setLTHead(row, flag, ltt, true);
7017 case Tabular::UNSET_LTHEAD:
7020 case Tabular::SET_LTHEAD:
7021 tabular.getRowOfLTHead(row, ltt);
7022 checkLongtableSpecial(ltt, value, flag);
7023 tabular.setLTHead(row, flag, ltt, false);
7026 case Tabular::UNSET_LTFOOT:
7029 case Tabular::SET_LTFOOT:
7030 tabular.getRowOfLTFoot(row, ltt);
7031 checkLongtableSpecial(ltt, value, flag);
7032 tabular.setLTFoot(row, flag, ltt, false);
7035 case Tabular::UNSET_LTLASTFOOT:
7038 case Tabular::SET_LTLASTFOOT:
7039 tabular.getRowOfLTLastFoot(row, ltt);
7040 checkLongtableSpecial(ltt, value, flag);
7041 tabular.setLTFoot(row, flag, ltt, true);
7044 case Tabular::UNSET_LTNEWPAGE:
7047 case Tabular::SET_LTNEWPAGE:
7048 tabular.setLTNewPage(row, flag);
7051 case Tabular::SET_LTCAPTION: {
7052 if (tabular.ltCaption(row))
7054 cur.idx() = tabular.setLTCaption(cur, row, true);
7057 cur.selection(false);
7058 // If a row is set as caption, then also insert
7059 // a caption. Otherwise the LaTeX output is broken.
7060 // Select cell if it is non-empty
7061 if (cur.lastpos() > 0 || cur.lastpit() > 0)
7062 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
7063 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
7067 case Tabular::UNSET_LTCAPTION: {
7068 if (!tabular.ltCaption(row))
7070 cur.idx() = tabular.setLTCaption(cur, row, false);
7073 cur.selection(false);
7074 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
7075 if (lyx::getStatus(fr).enabled())
7080 case Tabular::TOGGLE_LTCAPTION: {
7081 if (tabular.ltCaption(row))
7082 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
7084 tabularFeatures(cur, Tabular::SET_LTCAPTION);
7088 case Tabular::TOGGLE_BOOKTABS:
7089 tabular.use_booktabs = !tabular.use_booktabs;
7092 case Tabular::SET_BOOKTABS:
7093 tabular.use_booktabs = true;
7096 case Tabular::UNSET_BOOKTABS:
7097 tabular.use_booktabs = false;
7100 case Tabular::SET_TOP_SPACE: {
7102 if (value == "default")
7103 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7104 tabular.row_info[r].top_space_default = true;
7105 else if (value == "none")
7106 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7107 tabular.row_info[r].top_space_default = false;
7108 tabular.row_info[r].top_space = len;
7110 else if (isValidLength(value, &len))
7111 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7112 tabular.row_info[r].top_space_default = false;
7113 tabular.row_info[r].top_space = len;
7118 case Tabular::SET_BOTTOM_SPACE: {
7120 if (value == "default")
7121 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7122 tabular.row_info[r].bottom_space_default = true;
7123 else if (value == "none")
7124 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7125 tabular.row_info[r].bottom_space_default = false;
7126 tabular.row_info[r].bottom_space = len;
7128 else if (isValidLength(value, &len))
7129 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7130 tabular.row_info[r].bottom_space_default = false;
7131 tabular.row_info[r].bottom_space = len;
7136 case Tabular::SET_INTERLINE_SPACE: {
7138 if (value == "default")
7139 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
7140 tabular.row_info[r].interline_space_default = true;
7141 else if (value == "none")
7142 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7143 tabular.row_info[r].interline_space_default = false;
7144 tabular.row_info[r].interline_space = len;
7146 else if (isValidLength(value, &len))
7147 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
7148 tabular.row_info[r].interline_space_default = false;
7149 tabular.row_info[r].interline_space = len;
7154 case Tabular::SET_DECIMAL_POINT:
7155 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7156 tabular.column_info[c].decimal_point = from_utf8(value);
7159 // dummy stuff just to avoid warnings
7160 case Tabular::LAST_ACTION:
7166 bool InsetTabular::copySelection(Cursor & cur)
7168 if (!cur.selection())
7173 getSelection(cur, rs, re, cs, ce);
7175 paste_tabular.reset(new Tabular(tabular));
7177 for (row_type r = 0; r < rs; ++r)
7178 paste_tabular->deleteRow(0, true);
7180 row_type const rows = re - rs + 1;
7181 while (paste_tabular->nrows() > rows)
7182 paste_tabular->deleteRow(rows, true);
7184 for (col_type c = 0; c < cs; ++c)
7185 paste_tabular->deleteColumn(0, true);
7187 col_type const columns = ce - cs + 1;
7188 while (paste_tabular->ncols() > columns)
7189 paste_tabular->deleteColumn(columns, true);
7191 paste_tabular->setBuffer(tabular.buffer());
7193 odocstringstream os;
7194 OutputParams const runparams(0);
7195 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7196 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7197 cap::copySelection(cur, os.str());
7199 // mark tabular stack dirty
7200 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7201 // when we (hopefully) have a one-for-all paste mechanism.
7202 // This must be called after cap::copySelection.
7203 dirtyTabularStack(true);
7209 bool InsetTabular::pasteClipboard(Cursor & cur)
7213 col_type actcol = tabular.cellColumn(cur.idx());
7214 row_type actrow = tabular.cellRow(cur.idx());
7216 if (cur.selIsMultiCell()) {
7219 getSelection(cur, actrow, re, actcol, ce);
7222 col_type const oldncols = tabular.ncols();
7223 for (row_type r1 = 0, r2 = actrow; r1 < paste_tabular->nrows(); ++r1, ++r2) {
7224 // Append rows if needed
7225 if (r2 == tabular.nrows())
7226 tabular.insertRow(r2 - 1, false);
7227 for (col_type c1 = 0, c2 = actcol; c1 < paste_tabular->ncols(); ++c1, ++c2) {
7228 // Append columns if needed
7229 if (c2 == tabular.ncols())
7230 tabular.insertColumn(c2 - 1, false);
7231 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7232 tabular.isPartOfMultiColumn(r2, c2))
7234 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7238 if (tabular.isPartOfMultiColumn(r2, c2)) {
7242 shared_ptr<InsetTableCell> inset(
7243 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7244 tabular.setCellInset(r2, c2, inset);
7245 // FIXME?: why do we need to do this explicitly? (EL)
7246 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7248 if (!lyxrc.ct_markup_copied) {
7249 // do not paste deleted text
7250 inset->acceptChanges();
7251 inset->setChange(Change(buffer().params().track_changes ?
7252 Change::INSERTED : Change::UNCHANGED));
7258 // amend cursor position if cols have been appended
7259 cur.idx() += actrow * (tabular.ncols() - oldncols);
7264 void InsetTabular::cutSelection(Cursor & cur)
7266 if (!cur.selection())
7271 getSelection(cur, rs, re, cs, ce);
7272 for (row_type r = rs; r <= re; ++r) {
7273 for (col_type c = cs; c <= ce; ++c) {
7274 shared_ptr<InsetTableCell> t
7275 = cell(tabular.cellIndex(r, c));
7276 if (buffer().params().track_changes)
7277 // FIXME: Change tracking (MG)
7278 t->setChange(Change(Change::DELETED));
7284 // cursor position might be invalid now
7285 if (cur.pit() > cur.lastpit())
7286 cur.pit() = cur.lastpit();
7287 if (cur.pos() > cur.lastpos())
7288 cur.pos() = cur.lastpos();
7289 cur.clearSelection();
7293 bool InsetTabular::isRightToLeft(Cursor & cur) const
7295 // LASSERT: It might be better to abandon this Buffer.
7296 LASSERT(cur.depth() > 1, return false);
7297 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7298 pos_type const parentpos = cur[cur.depth() - 2].pos();
7299 return parentpar.getFontSettings(buffer().params(),
7300 parentpos).language()->rightToLeft();
7304 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7307 LASSERT(stidx <= enidx, return docstring());
7309 col_type const col1 = tabular.cellColumn(stidx);
7310 col_type const col2 = tabular.cellColumn(enidx);
7311 row_type const row1 = tabular.cellRow(stidx);
7312 row_type const row2 = tabular.cellRow(enidx);
7314 for (col_type col = col1; col <= col2; col++)
7315 for (row_type row = row1; row <= row2; row++) {
7320 retval += tabular.cellInset(row, col)->asString(intoInsets);
7326 ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
7328 LASSERT(stidx <= enidx, return ParagraphList());
7329 ParagraphList retval;
7330 col_type const col1 = tabular.cellColumn(stidx);
7331 col_type const col2 = tabular.cellColumn(enidx);
7332 row_type const row1 = tabular.cellRow(stidx);
7333 row_type const row2 = tabular.cellRow(enidx);
7334 for (col_type col = col1; col <= col2; col++)
7335 for (row_type row = row1; row <= row2; row++)
7336 for (auto par : tabular.cellInset(row, col)->paragraphs())
7337 retval.push_back(par);
7342 void InsetTabular::getSelection(Cursor & cur,
7343 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7345 CursorSlice const & beg = cur.selBegin();
7346 CursorSlice const & end = cur.selEnd();
7347 cs = tabular.cellColumn(beg.idx());
7348 ce = tabular.cellColumn(end.idx());
7352 rs = tabular.cellRow(beg.idx());
7353 re = tabular.cellRow(end.idx());
7359 Text * InsetTabular::getText(int idx) const
7361 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
7365 bool InsetTabular::isChanged() const
7367 for (idx_type idx = 0; idx < nargs(); ++idx) {
7368 if (cell(idx)->isChanged())
7370 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7372 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7379 void InsetTabular::setChange(Change const & change)
7381 for (idx_type idx = 0; idx < nargs(); ++idx)
7382 cell(idx)->setChange(change);
7386 void InsetTabular::acceptChanges()
7388 for (idx_type idx = 0; idx < nargs(); ++idx)
7389 cell(idx)->acceptChanges();
7390 for (row_type row = 0; row < tabular.nrows(); ++row) {
7391 if (tabular.row_info[row].change.inserted())
7392 tabular.row_info[row].change.setUnchanged();
7393 else if (tabular.row_info[row].change.deleted())
7394 tabular.deleteRow(row, true);
7396 for (col_type col = 0; col < tabular.ncols(); ++col) {
7397 if (tabular.column_info[col].change.inserted())
7398 tabular.column_info[col].change.setUnchanged();
7399 else if (tabular.column_info[col].change.deleted())
7400 tabular.deleteColumn(col, true);
7402 tabular.updateIndexes();
7406 void InsetTabular::rejectChanges()
7408 for (idx_type idx = 0; idx < nargs(); ++idx)
7409 cell(idx)->rejectChanges();
7410 for (row_type row = 0; row < tabular.nrows(); ++row) {
7411 if (tabular.row_info[row].change.deleted())
7412 tabular.row_info[row].change.setUnchanged();
7413 else if (tabular.row_info[row].change.inserted())
7414 tabular.deleteRow(row, true);
7416 for (col_type col = 0; col < tabular.ncols(); ++col) {
7417 if (tabular.column_info[col].change.deleted())
7418 tabular.column_info[col].change.setUnchanged();
7419 else if (tabular.column_info[col].change.inserted())
7420 tabular.deleteColumn(col, true);
7422 tabular.updateIndexes();
7426 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7428 return tabular.getPWidth(cell).zero();
7432 bool InsetTabular::forcePlainLayout(idx_type cell) const
7434 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
7438 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7441 if (buf.length() <= 0)
7446 col_type maxCols = 1;
7447 size_t const len = buf.length();
7451 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7459 maxCols = max(cols, maxCols);
7465 maxCols = max(cols, maxCols);
7471 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7472 loctab = paste_tabular.get();
7473 dirtyTabularStack(true);
7476 cell = bv.cursor().idx();
7477 ocol = tabular.cellColumn(cell);
7478 row = tabular.cellRow(cell);
7482 idx_type cells = loctab->numberofcells;
7485 rows = loctab->nrows();
7486 col_type columns = loctab->ncols();
7489 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7495 // append column if necessary
7496 if (cols == columns) {
7497 loctab->appendColumn(cols - 1);
7498 columns = loctab->ncols();
7499 cells = loctab->numberofcells;
7502 if (cols < columns) {
7503 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7504 Font const font = bv.textMetrics(&inset->text()).
7506 inset->setText(buf.substr(op, p - op), font,
7507 buffer().params().track_changes);
7513 // we can only set this if we are not too far right
7514 if (cols < columns) {
7515 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7516 Font const font = bv.textMetrics(&inset->text()).
7518 inset->setText(buf.substr(op, p - op), font,
7519 buffer().params().track_changes);
7523 // append row if necessary
7524 if (row == rows && p < len - 1) {
7525 loctab->appendRow(row - 1);
7526 rows = loctab->nrows();
7527 cells = loctab->numberofcells;
7530 cell = loctab->cellIndex(row, cols);
7536 // check for the last cell if there is no trailing '\n'
7537 if (cell < cells && op < len) {
7538 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7539 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7540 inset->setText(buf.substr(op, len - op), font,
7541 buffer().params().track_changes);
7547 void InsetTabular::addPreview(DocIterator const & inset_pos,
7548 PreviewLoader & loader) const
7550 DocIterator cell_pos = inset_pos;
7552 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7553 for (row_type r = 0; r < tabular.nrows(); ++r) {
7554 for (col_type c = 0; c < tabular.ncols(); ++c) {
7555 cell_pos.top().idx() = tabular.cellIndex(r, c);
7556 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7562 bool InsetTabular::completionSupported(Cursor const & cur) const
7564 Cursor const & bvCur = cur.bv().cursor();
7565 if (&bvCur.inset() != this)
7567 return cur.text()->completionSupported(cur);
7571 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7573 return completionSupported(cur);
7577 bool InsetTabular::automaticInlineCompletion() const
7579 return lyxrc.completion_inline_text;
7583 bool InsetTabular::automaticPopupCompletion() const
7585 return lyxrc.completion_popup_text;
7589 bool InsetTabular::showCompletionCursor() const
7591 return lyxrc.completion_cursor_text;
7595 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7597 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
7601 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7603 if (!completionSupported(cur))
7605 return cur.text()->completionPrefix(cur);
7609 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7611 if (!completionSupported(cur))
7614 return cur.text()->insertCompletion(cur, s, finished);
7618 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7619 Dimension & dim) const
7621 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7622 tm.completionPosAndDim(cur, x, y, dim);
7626 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7628 istringstream data(in);
7630 lex.setStream(data);
7637 if (!lex || token != "tabular") {
7638 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7642 // This is part of the inset proper that is usually swallowed
7643 // by Buffer::readInset
7645 if (!lex || token != "Tabular") {
7646 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7654 string InsetTabular::params2string(InsetTabular const & inset)
7657 data << "tabular" << ' ';
7659 data << "\\end_inset\n";
7664 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7666 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
7667 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
7668 if (!tabular.isPartOfMultiColumn(r,c) &&
7669 !tabular.isPartOfMultiRow(r,c))
7672 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7673 ParagraphList::iterator it = parlist.begin();
7674 ParagraphList::iterator const en = parlist.end();
7675 for (; it != en; ++it)
7676 it->setLayout(dc.plainLayout());