2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
16 * \author Scott Kostyshak
18 * Full author contact details are available in file CREDITS.
23 #include "InsetTabular.h"
25 #include "buffer_funcs.h"
27 #include "BufferParams.h"
28 #include "BufferView.h"
29 #include "CoordCache.h"
32 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
36 #include "InsetIterator.h"
37 #include "InsetList.h"
39 #include "LaTeXFeatures.h"
43 #include "MetricsInfo.h"
44 #include "OutputParams.h"
45 #include "output_xhtml.h"
46 #include "Paragraph.h"
47 #include "ParagraphParameters.h"
48 #include "ParIterator.h"
50 #include "texstream.h"
51 #include "TextClass.h"
52 #include "TextMetrics.h"
54 #include "frontends/Application.h"
55 #include "frontends/alert.h"
56 #include "frontends/Clipboard.h"
57 #include "frontends/Painter.h"
58 #include "frontends/Selection.h"
60 #include "support/convert.h"
61 #include "support/debug.h"
62 #include "support/docstream.h"
63 #include "support/FileName.h"
64 #include "support/gettext.h"
65 #include "support/lassert.h"
66 #include "support/lstrings.h"
67 #include "support/unique_ptr.h"
76 using namespace lyx::support;
82 using cap::dirtyTabularStack;
83 using cap::tabularStackDirty;
85 using graphics::PreviewLoader;
87 using frontend::Painter;
88 using frontend::Clipboard;
90 namespace Alert = frontend::Alert;
95 int const ADD_TO_HEIGHT = 2; // in cell
96 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
97 int const default_line_space = 10; // ?
98 int const WIDTH_OF_LINE = 5; // space between double lines
102 unique_ptr<Tabular> paste_tabular;
105 struct TabularFeature {
106 Tabular::Feature action;
112 TabularFeature tabularFeature[] =
114 // the SET/UNSET actions are used by the table dialog,
115 // the TOGGLE actions by the table toolbar buttons
116 // FIXME: these values have been hardcoded in InsetMathGrid and other
118 { Tabular::APPEND_ROW, "append-row", false },
119 { Tabular::APPEND_COLUMN, "append-column", false },
120 { Tabular::DELETE_ROW, "delete-row", false },
121 { Tabular::DELETE_COLUMN, "delete-column", false },
122 { Tabular::COPY_ROW, "copy-row", false },
123 { Tabular::COPY_COLUMN, "copy-column", false },
124 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
125 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
126 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
127 { Tabular::MOVE_ROW_UP, "move-row-up", false },
128 { Tabular::SET_LINE_TOP, "set-line-top", true },
129 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
130 { Tabular::SET_LTRIM_TOP, "set-ltrim-top", true },
131 { Tabular::SET_LTRIM_BOTTOM, "set-ltrim-bottom", true },
132 { Tabular::SET_RTRIM_TOP, "set-rtrim-top", true },
133 { Tabular::SET_RTRIM_BOTTOM, "set-rtrim-bottom", true },
134 { Tabular::SET_LINE_LEFT, "set-line-left", true },
135 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
136 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
137 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
138 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
139 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
140 { Tabular::TOGGLE_LTRIM_TOP, "toggle-ltrim-top", false },
141 { Tabular::TOGGLE_LTRIM_BOTTOM, "toggle-ltrim-bottom", false },
142 { Tabular::TOGGLE_RTRIM_TOP, "toggle-rtrim-top", false },
143 { Tabular::TOGGLE_RTRIM_BOTTOM, "toggle-rtrim-bottom", false },
144 { Tabular::ALIGN_LEFT, "align-left", false },
145 { Tabular::ALIGN_RIGHT, "align-right", false },
146 { Tabular::ALIGN_CENTER, "align-center", false },
147 { Tabular::ALIGN_BLOCK, "align-block", false },
148 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
149 { Tabular::VALIGN_TOP, "valign-top", false },
150 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
151 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
152 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
153 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
154 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
155 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
156 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
157 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
158 { Tabular::MULTICOLUMN, "multicolumn", false },
159 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
160 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
161 { Tabular::MULTIROW, "multirow", false },
162 { Tabular::SET_MULTIROW, "set-multirow", false },
163 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
164 { Tabular::SET_MROFFSET, "set-mroffset", true },
165 { Tabular::SET_ALL_LINES, "set-all-lines", false },
166 { Tabular::RESET_FORMAL_DEFAULT, "reset-formal-default", false },
167 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
168 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
169 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
170 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
171 { Tabular::SET_PWIDTH, "set-pwidth", true },
172 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
173 { Tabular::TOGGLE_VARWIDTH_COLUMN, "toggle-varwidth-column", true },
174 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
175 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
176 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
177 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
178 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
179 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
180 { Tabular::SET_USEBOX, "set-usebox", true },
181 { Tabular::SET_LTHEAD, "set-lthead", true },
182 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
183 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
184 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
185 { Tabular::SET_LTFOOT, "set-ltfoot", true },
186 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
187 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
188 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
189 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
190 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
191 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
192 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
193 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
194 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
195 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
196 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
197 { Tabular::SET_BOOKTABS, "set-booktabs", false },
198 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
199 { Tabular::SET_TOP_SPACE, "set-top-space", true },
200 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
201 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
202 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
203 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
204 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
205 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
206 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
207 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
208 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
209 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
210 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
211 { Tabular::SET_INNER_LINES, "set-inner-lines", false },
212 { Tabular::LAST_ACTION, "", false }
216 string const tostr(LyXAlignment const & num)
221 case LYX_ALIGN_BLOCK:
225 case LYX_ALIGN_CENTER:
227 case LYX_ALIGN_RIGHT:
229 case LYX_ALIGN_LAYOUT:
231 case LYX_ALIGN_SPECIAL:
233 case LYX_ALIGN_DECIMAL:
240 string const tostr(Tabular::HAlignment const & num)
243 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
245 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
247 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
254 string const tostr(Tabular::VAlignment const & num)
257 case Tabular::LYX_VALIGN_TOP:
259 case Tabular::LYX_VALIGN_MIDDLE:
261 case Tabular::LYX_VALIGN_BOTTOM:
268 string const tostr(Tabular::BoxType const & num)
271 case Tabular::BOX_NONE:
273 case Tabular::BOX_PARBOX:
275 case Tabular::BOX_MINIPAGE:
277 case Tabular::BOX_VARWIDTH:
284 // I would have liked a fromstr template a lot better. (Lgb)
285 bool string2type(string const & str, LyXAlignment & num)
288 num = LYX_ALIGN_NONE;
289 else if (str == "block")
290 num = LYX_ALIGN_BLOCK;
291 else if (str == "left")
292 num = LYX_ALIGN_LEFT;
293 else if (str == "center")
294 num = LYX_ALIGN_CENTER;
295 else if (str == "right")
296 num = LYX_ALIGN_RIGHT;
297 else if (str == "decimal")
298 num = LYX_ALIGN_DECIMAL;
305 bool string2type(string const & str, Tabular::HAlignment & num)
308 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
309 else if (str == "center" )
310 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
311 else if (str == "right")
312 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
319 bool string2type(string const & str, Tabular::VAlignment & num)
322 num = Tabular::LYX_VALIGN_TOP;
323 else if (str == "middle" )
324 num = Tabular::LYX_VALIGN_MIDDLE;
325 else if (str == "bottom")
326 num = Tabular::LYX_VALIGN_BOTTOM;
333 bool string2type(string const & str, Tabular::BoxType & num)
336 num = Tabular::BOX_NONE;
337 else if (str == "parbox")
338 num = Tabular::BOX_PARBOX;
339 else if (str == "minipage")
340 num = Tabular::BOX_MINIPAGE;
341 else if (str == "varwidth")
342 num = Tabular::BOX_VARWIDTH;
349 bool string2type(string const & str, bool & num)
353 else if (str == "false")
361 bool getTokenValue(string const & str, char const * token, string & ret)
364 size_t token_length = strlen(token);
365 size_t pos = str.find(token);
367 if (pos == string::npos || pos + token_length + 1 >= str.length()
368 || str[pos + token_length] != '=')
370 pos += token_length + 1;
372 if (ch != '"' && ch != '\'') { // only read till next space
376 while (pos < str.length() - 1 && str[++pos] != ch)
383 bool getTokenValue(string const & str, char const * token, docstring & ret)
386 bool const success = getTokenValue(str, token, tmp);
387 ret = from_utf8(tmp);
392 bool getTokenValue(string const & str, char const * token, int & num)
396 if (!getTokenValue(str, token, tmp))
398 num = convert<int>(tmp);
403 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
406 return getTokenValue(str, token, tmp) && string2type(tmp, num);
410 bool getTokenValue(string const & str, char const * token,
411 Tabular::HAlignment & num)
414 return getTokenValue(str, token, tmp) && string2type(tmp, num);
418 bool getTokenValue(string const & str, char const * token,
419 Tabular::VAlignment & num)
422 return getTokenValue(str, token, tmp) && string2type(tmp, num);
426 bool getTokenValue(string const & str, char const * token,
427 Tabular::BoxType & num)
430 return getTokenValue(str, token, tmp) && string2type(tmp, num);
434 bool getTokenValue(string const & str, char const * token, bool & flag)
436 // set the flag always to false as this should be the default for bools
437 // not in the file-format.
440 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
444 bool getTokenValue(string const & str, char const * token, Length & len)
446 // set the length to be zero() as default as this it should be if not
447 // in the file format.
450 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
454 bool getTokenValue(string const & str, char const * token, Change & change, BufferParams bp)
456 // set the change to be Change() as default as this it should be if not
457 // in the file format.
460 if (getTokenValue(str, token, tmp)) {
461 vector<string> const changedata = getVectorFromString(tmp, " ");
462 if (changedata.size() != 3) {
463 Alert::warning(_("Change tracking data incomplete"),
464 _("Change tracking information for tabular row/column "
465 "is incomplete. I will ignore this.\n"));
468 BufferParams::AuthorMap const & am = bp.author_map_;
469 int aid = convert<int>(changedata[1]);
470 if (am.find(aid) == am.end()) {
471 // FIXME Use ErrorList
472 Alert::warning(_("Change tracking author index missing"),
473 bformat(_("A change tracking author information for index "
474 "%1$d is missing. This can happen after a wrong "
475 "merge by a version control system. In this case, "
476 "either fix the merge, or have this information "
477 "missing until the corresponding tracked changes "
478 "are merged or this user edits the file again.\n"),
480 bp.addAuthor(Author(aid));
482 istringstream is(changedata[2]);
485 if (changedata[0] == "inserted") {
486 change = Change(Change::INSERTED, am.find(aid)->second, ct);
488 } else if (changedata[0] == "deleted") {
489 change = Change(Change::DELETED, am.find(aid)->second, ct);
497 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
502 if (!getTokenValue(str, token, tmp))
504 if (tmp == "default") {
508 return isValidLength(tmp, &len);
512 void l_getline(istream & is, string & str)
515 while (str.empty()) {
517 if (!str.empty() && str[str.length() - 1] == '\r')
518 str.erase(str.length() - 1);
523 string const write_attribute(string const & name, T const & t)
525 string const s = tostr(t);
526 return s.empty() ? s : " " + name + "=\"" + s + "\"";
530 string const write_attribute(string const & name, string const & t)
532 return t.empty() ? t : " " + name + "=\"" + t + "\"";
537 string const write_attribute(string const & name, docstring const & t)
539 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
544 string const write_attribute(string const & name, bool const & b)
546 // we write only true attribute values so we remove a bit of the
547 // file format bloat for tabulars.
548 return b ? write_attribute(name, convert<string>(b)) : string();
553 string const write_attribute(string const & name, int const & i)
555 // we write only true attribute values so we remove a bit of the
556 // file format bloat for tabulars.
557 return i ? write_attribute(name, convert<string>(i)) : string();
562 string const write_attribute(string const & name, Tabular::idx_type const & i)
564 // we write only true attribute values so we remove a bit of the
565 // file format bloat for tabulars.
566 return i ? write_attribute(name, convert<string>(i)) : string();
571 string const write_attribute(string const & name, Length const & value)
573 // we write only the value if we really have one same reson as above.
574 return value.zero() ? string() : write_attribute(name, value.asString());
577 string const write_attribute(string const & name, Change const & change, BufferParams const bp)
579 odocstringstream ods;
580 if (change.inserted())
581 ods << from_ascii("inserted");
582 else if (change.deleted())
583 ods << from_ascii("deleted");
584 if (change.changed()) {
585 ods << " " << bp.authors().get(change.author).bufferId()
586 << " " << change.changetime;
587 return write_attribute(name, ods.str());
595 string const featureAsString(Tabular::Feature action)
597 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
598 if (tabularFeature[i].action == action)
599 return tabularFeature[i].feature;
605 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
607 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
608 for (; dit; dit.forwardChar())
609 if (dit.inTexted() && dit.depth() == 1
610 && dit.paragraph().find(align_d, false, false, dit.pos()))
617 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
619 InsetTableCell tail = InsetTableCell(head);
620 DocIterator const dit = separatorPos(&head, align_d);
623 pit_type const psize = head.paragraphs().front().size();
624 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
625 tail.paragraphs().front().eraseChars(0,
626 dit.pos() < psize ? dit.pos() + 1 : psize, false);
633 /////////////////////////////////////////////////////////////////////
637 /////////////////////////////////////////////////////////////////////
640 Tabular::CellData::CellData(Buffer * buf)
643 multicolumn(Tabular::CELL_NORMAL),
644 multirow(Tabular::CELL_NORMAL),
645 alignment(LYX_ALIGN_CENTER),
646 valignment(LYX_VALIGN_TOP),
654 top_line_rtrimmed(false),
655 top_line_ltrimmed(false),
656 bottom_line_rtrimmed(false),
657 bottom_line_ltrimmed(false),
660 inset(new InsetTableCell(buf))
662 inset->setBuffer(*buf);
666 Tabular::CellData::CellData(CellData const & cs)
669 multicolumn(cs.multicolumn),
670 multirow(cs.multirow),
671 mroffset(cs.mroffset),
672 alignment(cs.alignment),
673 valignment(cs.valignment),
674 decimal_hoffset(cs.decimal_hoffset),
675 decimal_width(cs.decimal_width),
677 top_line(cs.top_line),
678 bottom_line(cs.bottom_line),
679 left_line(cs.left_line),
680 right_line(cs.right_line),
681 top_line_rtrimmed(cs.top_line_rtrimmed),
682 top_line_ltrimmed(cs.top_line_ltrimmed),
683 bottom_line_rtrimmed(cs.bottom_line_rtrimmed),
684 bottom_line_ltrimmed(cs.bottom_line_ltrimmed),
687 align_special(cs.align_special),
689 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
693 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
699 multicolumn = cs.multicolumn;
700 multirow = cs.multirow;
701 mroffset = cs.mroffset;
702 alignment = cs.alignment;
703 valignment = cs.valignment;
704 decimal_hoffset = cs.decimal_hoffset;
705 decimal_width = cs.decimal_width;
706 voffset = cs.voffset;
707 top_line = cs.top_line;
708 bottom_line = cs.bottom_line;
709 left_line = cs.left_line;
710 right_line = cs.right_line;
711 top_line_rtrimmed = cs.top_line_rtrimmed;
712 top_line_ltrimmed = cs.top_line_ltrimmed;
713 bottom_line_rtrimmed = cs.bottom_line_rtrimmed;
714 bottom_line_ltrimmed = cs.bottom_line_ltrimmed;
717 align_special = cs.align_special;
718 p_width = cs.p_width;
719 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
723 Tabular::RowData::RowData()
726 top_space_default(false),
727 bottom_space_default(false),
728 interline_space_default(false),
739 Tabular::ColumnData::ColumnData()
740 : alignment(LYX_ALIGN_CENTER),
741 valignment(LYX_VALIGN_TOP),
749 Tabular::ltType::ltType()
750 : set(false), topDL(false),
756 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
758 init(buffer, rows_arg, columns_arg);
762 void Tabular::setBuffer(Buffer & buffer)
765 for (row_type i = 0; i < nrows(); ++i)
766 for (col_type j = 0; j < ncols(); ++j)
767 cell_info[i][j].inset->setBuffer(*buffer_);
771 // activates all lines and sets all widths to 0
772 void Tabular::init(Buffer * buf, row_type rows_arg,
773 col_type columns_arg)
776 row_info = row_vector(rows_arg);
777 column_info = column_vector(columns_arg);
778 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
779 row_info.reserve(10);
780 column_info.reserve(10);
781 cell_info.reserve(100);
783 is_long_tabular = false;
784 tabular_valignment = LYX_VALIGN_MIDDLE;
785 tabular_width = Length();
786 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
788 use_booktabs = false;
789 // set silly default lines
790 for (row_type r = 0; r < nrows(); ++r)
791 for (col_type c = 0; c < ncols(); ++c) {
792 cell_info[r][c].inset->setBuffer(*buffer_);
793 cell_info[r][c].top_line = true;
794 cell_info[r][c].left_line = true;
795 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
796 cell_info[r][c].right_line = c == ncols() - 1;
801 void Tabular::deleteRow(row_type const row, bool const force)
803 // Not allowed to delete last row
807 bool const ct = force ? false : buffer().params().track_changes;
809 for (col_type c = 0; c < ncols(); ++c) {
810 // Care about multirow cells
811 if (row + 1 < nrows() &&
812 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
813 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
814 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
818 row_info[row].change.setDeleted();
820 row_info.erase(row_info.begin() + row);
821 cell_info.erase(cell_info.begin() + row);
827 void Tabular::copyRow(row_type const row)
829 insertRow(row, true);
833 void Tabular::appendRow(row_type row)
835 insertRow(row, false);
839 void Tabular::insertRow(row_type const row, bool copy)
841 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
842 cell_info.insert(cell_info.begin() + row + 1,
843 cell_vector(0, CellData(buffer_)));
845 for (col_type c = 0; c < ncols(); ++c) {
846 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
847 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
848 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
849 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
853 for (col_type c = 0; c < ncols(); ++c) {
854 if (isPartOfMultiRow(row, c))
856 // inherit line settings
857 idx_type const i = cellIndex(row + 1, c);
858 idx_type const j = cellIndex(row, c);
859 setLeftLine(i, leftLine(j));
860 setRightLine(i, rightLine(j));
861 setTopLine(i, topLine(j));
862 if (topLine(j) && bottomLine(j)) {
863 setBottomLine(i, true);
864 setBottomLine(j, false);
867 if (buffer().params().track_changes) {
868 row_info[row + 1].change.setInserted();
874 void Tabular::moveColumn(col_type col, ColDirection direction)
876 if (direction == Tabular::LEFT)
879 std::swap(column_info[col], column_info[col + 1]);
881 for (row_type r = 0; r < nrows(); ++r) {
882 std::swap(cell_info[r][col], cell_info[r][col + 1]);
883 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
884 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
886 idx_type const i = cellIndex(r, col);
887 idx_type const j = cellIndex(r, col + 1);
888 if (buffer().params().track_changes) {
889 cellInfo(i).inset->setChange(Change(Change::INSERTED));
890 cellInfo(j).inset->setChange(Change(Change::INSERTED));
897 void Tabular::moveRow(row_type row, RowDirection direction)
899 if (direction == Tabular::UP)
902 std::swap(row_info[row], row_info[row + 1]);
904 for (col_type c = 0; c < ncols(); ++c) {
905 std::swap(cell_info[row][c], cell_info[row + 1][c]);
906 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
907 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
909 idx_type const i = cellIndex(row, c);
910 idx_type const j = cellIndex(row + 1, c);
911 if (buffer().params().track_changes) {
912 cellInfo(i).inset->setChange(Change(Change::INSERTED));
913 cellInfo(j).inset->setChange(Change(Change::INSERTED));
920 void Tabular::deleteColumn(col_type const col, bool const force)
922 // Not allowed to delete last column
926 bool const ct = force ? false : buffer().params().track_changes;
928 for (row_type r = 0; r < nrows(); ++r) {
929 // Care about multicolumn cells
930 if (col + 1 < ncols() &&
931 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
932 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
933 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
936 cell_info[r].erase(cell_info[r].begin() + col);
939 column_info[col].change.setDeleted();
941 column_info.erase(column_info.begin() + col);
946 void Tabular::copyColumn(col_type const col)
948 insertColumn(col, true);
952 void Tabular::appendColumn(col_type col)
954 insertColumn(col, false);
958 void Tabular::insertColumn(col_type const col, bool copy)
960 bool const ct = buffer().params().track_changes;
961 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
963 for (row_type r = 0; r < nrows(); ++r) {
964 cell_info[r].insert(cell_info[r].begin() + col + 1,
965 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
966 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
967 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
970 for (row_type r = 0; r < nrows(); ++r) {
971 // inherit line settings
972 idx_type const i = cellIndex(r, col + 1);
973 idx_type const j = cellIndex(r, col);
974 setBottomLine(i, bottomLine(j));
975 setTopLine(i, topLine(j));
976 setLeftLine(i, leftLine(j));
977 setRightLine(i, rightLine(j));
978 if (rightLine(i) && rightLine(j)) {
979 setRightLine(j, false);
983 column_info[col + 1].change.setInserted();
989 void Tabular::updateIndexes()
994 for (row_type row = 0; row < nrows(); ++row)
995 for (col_type column = 0; column < ncols(); ++column) {
996 if (!isPartOfMultiColumn(row, column)
997 && !isPartOfMultiRow(row, column))
999 if (isPartOfMultiRow(row, column))
1000 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
1002 cell_info[row][column].cellno = numberofcells - 1;
1005 rowofcell.resize(numberofcells);
1006 columnofcell.resize(numberofcells);
1008 // reset column and row of cells and update their width, alignment and ct status
1009 for (row_type row = 0; row < nrows(); ++row) {
1010 for (col_type column = 0; column < ncols(); ++column) {
1011 if (isPartOfMultiColumn(row, column)) {
1012 cell_info[row][column].inset->toggleMultiCol(true);
1015 cell_info[row][column].inset->toggleMultiCol(false);
1016 // columnofcell needs to be called before setting width and aligment
1017 // multirow cells inherit the width from the column width
1018 if (!isPartOfMultiRow(row, column)) {
1019 columnofcell[i] = column;
1022 setFixedWidth(row, column);
1023 if (isPartOfMultiRow(row, column)) {
1024 cell_info[row][column].inset->toggleMultiRow(true);
1027 cell_info[row][column].inset->toggleMultiRow(false);
1028 cell_info[row][column].inset->setContentAlignment(
1029 getAlignment(cellIndex(row, column)));
1030 if (buffer().params().track_changes) {
1031 if (row_info[row].change.changed())
1032 cell_info[row][column].inset->setChange(row_info[row].change);
1033 if (column_info[column].change.changed())
1034 cell_info[row][column].inset->setChange(column_info[column].change);
1042 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
1044 idx_type result = 0;
1045 for (col_type c = 0; c < ncols(); ++c)
1046 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
1052 bool Tabular::topLine(idx_type const cell) const
1054 return cellInfo(cell).top_line;
1058 bool Tabular::bottomLine(idx_type const cell) const
1060 return cellInfo(cell).bottom_line;
1064 bool Tabular::leftLine(idx_type cell, bool const ignore_bt) const
1066 if (use_booktabs && !ignore_bt)
1068 return cellInfo(cell).left_line;
1072 bool Tabular::rightLine(idx_type cell, bool const ignore_bt) const
1074 if (use_booktabs && !ignore_bt)
1076 return cellInfo(cell).right_line;
1080 pair<bool, bool> Tabular::topLineTrim(idx_type const cell) const
1083 return make_pair(false, false);
1084 return make_pair(cellInfo(cell).top_line_ltrimmed,
1085 cellInfo(cell).top_line_rtrimmed);
1089 pair<bool, bool> Tabular::bottomLineTrim(idx_type const cell) const
1092 return make_pair(false, false);
1093 return make_pair(cellInfo(cell).bottom_line_ltrimmed,
1094 cellInfo(cell).bottom_line_rtrimmed);
1098 int Tabular::interRowSpace(row_type row) const
1100 if (!row || row >= nrows())
1103 int const interline_space = row_info[row - 1].interline_space_default ?
1104 default_line_space :
1105 row_info[row - 1].interline_space.inPixels(width());
1106 if (rowTopLine(row) && rowBottomLine(row - 1))
1107 return interline_space + WIDTH_OF_LINE;
1108 return interline_space;
1112 int Tabular::interColumnSpace(idx_type cell) const
1114 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
1115 if (rightLine(cell) && nextcol < ncols()
1116 && leftLine(cellIndex(cellRow(cell), nextcol)))
1117 return WIDTH_OF_LINE;
1122 int Tabular::cellWidth(idx_type cell) const
1125 col_type const span = columnSpan(cell);
1126 col_type const col = cellColumn(cell);
1127 for(col_type c = col; c < col + span ; ++c)
1128 w += column_info[c].width;
1133 int Tabular::cellHeight(idx_type cell) const
1135 row_type const span = rowSpan(cell);
1136 row_type const row = cellRow(cell);
1138 for(row_type r = row; r < row + span ; ++r) {
1139 h += rowAscent(r) + rowDescent(r);
1140 if (r != row + span - 1)
1141 h += interRowSpace(r + 1);
1148 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1150 vector<int> max_dwidth(ncols(), 0);
1151 // collect max. fixed width of column
1152 map<col_type, int> max_pwidth;
1153 // collect max. variable width of column
1154 map<col_type, int> max_width;
1156 for(col_type c = 0; c < ncols(); ++c)
1157 for(row_type r = 0; r < nrows(); ++r) {
1158 idx_type const i = cellIndex(r, c);
1159 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1160 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1161 if (!getPWidth(i).zero())
1162 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1163 else if (!column_info[c].varwidth)
1164 max_width[c] = max(max_width[c], cell_info[r][c].width);
1167 // If we have a fixed tabular width, we take this into account
1168 Length tab_width = tabular_width;
1169 bool const tabularx = hasVarwidthColumn();
1170 if (tabularx && tab_width.zero())
1171 // If no tabular width is specified with X columns,
1172 // we use 100% colwidth
1173 tab_width = Length(100, Length::PCW);
1175 if (!tab_width.zero()) {
1176 restwidth = mi.base.inPixels(tab_width);
1177 // Substract the fixed widths from the table width
1178 for (auto const w : max_pwidth)
1179 restwidth -= w.second;
1182 // If we have a fixed width, distribute the available table width
1183 // (minus the fixed widths) to the variable-width columns
1185 int restcols = ncols() - max_pwidth.size();
1186 if ((restwidth > 0) && (restcols != 0))
1187 vcolwidth = restwidth / restcols;
1189 // Now consider that some variable width columns exceed the vcolwidth
1190 if (vcolwidth > 0) {
1191 bool changed = false;
1192 for (auto const w : max_width) {
1193 if (tabularx || w.second > vcolwidth) {
1195 restwidth -= w.second;
1199 if (changed && restwidth > 0)
1200 vcolwidth = restwidth / restcols;
1203 bool update = false;
1204 // for each col get max of single col cells
1205 for(col_type c = 0; c < ncols(); ++c) {
1207 for(row_type r = 0; r < nrows(); ++r) {
1208 idx_type const i = cellIndex(r, c);
1209 if (columnSpan(i) == 1) {
1210 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1211 && cell_info[r][c].decimal_width != 0)
1212 new_width = max(new_width, cellInfo(i).width
1213 + max_dwidth[c] - cellInfo(i).decimal_width);
1214 else if (getPWidth(i).zero() && vcolwidth > 0) {
1215 if (tabularx && !column_info[c].varwidth)
1216 new_width = max(new_width, cellInfo(i).width);
1218 new_width = vcolwidth;
1220 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1222 new_width = max(new_width, cellInfo(i).width);
1226 if (column_info[c].width != new_width) {
1227 column_info[c].width = new_width;
1228 // Do not trigger update when no space is left for variable
1229 // columns, as this will loop
1230 update = tab_width.zero() || restwidth > 0;
1233 // update col widths to fit merged cells
1234 for(col_type c = 0; c < ncols(); ++c)
1235 for(row_type r = 0; r < nrows(); ++r) {
1236 idx_type const i = cellIndex(r, c);
1237 int const span = columnSpan(i);
1238 if (span == 1 || c > cellColumn(i))
1242 for(col_type j = c; j < c + span ; ++j)
1243 old_width += column_info[j].width;
1245 if (cellInfo(i).width > old_width) {
1246 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1247 // Do not trigger update when no space is left for variable
1248 // columns, as this will loop
1249 update = tab_width.zero() || restwidth > 0;
1257 int Tabular::width() const
1260 for (col_type c = 0; c < ncols(); ++c)
1261 width += column_info[c].width;
1266 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1269 col_type const col = cellColumn(cell);
1270 // set alignment for the whole row if we are not in a multicolumn cell,
1271 // exclude possible multicolumn cells in the row
1272 if (!isMultiColumn(cell)) {
1273 for (row_type r = 0; r < nrows(); ++r) {
1274 // only if the column has no width the multirow inherits the
1275 // alignment of the column, otherwise it is left aligned
1276 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1277 && !isMultiColumn(cellIndex(r, col))) {
1278 cell_info[r][col].alignment = align;
1279 cell_info[r][col].inset->setContentAlignment(align);
1281 if ((isMultiRow(cellIndex(r, col)) && has_width)
1282 && !isMultiColumn(cellIndex(r, col))) {
1283 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1284 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1287 column_info[col].alignment = align;
1288 docstring & dpoint = column_info[col].decimal_point;
1289 if (align == LYX_ALIGN_DECIMAL && dpoint.empty()) {
1290 Language const * tlang = buffer().paragraphs().front().getParLanguage(buffer().params());
1291 dpoint = tlang->decimalSeparator();
1294 cellInfo(cell).alignment = align;
1295 cellInset(cell)->setContentAlignment(align);
1300 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1303 if (!isMultiColumn(cell) || onlycolumn)
1304 column_info[cellColumn(cell)].valignment = align;
1306 cellInfo(cell).valignment = align;
1313 * Allow line and paragraph breaks for fixed width multicol/multirow cells
1314 * or disallow them, merge cell paragraphs and reset layout to standard
1315 * for variable width multicol cells.
1317 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1318 bool const fixedWidth, bool const multicol,
1319 bool const multirow)
1321 inset->toggleFixedWidth(fixedWidth);
1322 if (!multirow && (fixedWidth || !multicol))
1325 // merge all paragraphs to one
1326 BufferParams const & bp = cur.bv().buffer().params();
1327 while (inset->paragraphs().size() > 1)
1328 mergeParagraph(bp, inset->paragraphs(), 0);
1330 // This is relevant for multirows
1335 ParagraphList::iterator pit = inset->paragraphs().begin();
1336 for (; pit != inset->paragraphs().end(); ++pit) {
1337 for (pos_type j = 0; j != pit->size(); ++j) {
1338 if (pit->isNewline(j))
1339 pit->eraseChar(j, bp.track_changes);
1345 // undo information has already been recorded
1346 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1347 bp.documentClass().plainLayoutName());
1354 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1355 Length const & width)
1357 col_type const c = cellColumn(cell);
1359 column_info[c].p_width = width;
1360 // reset the vertical alignment to top if the fixed width
1361 // is removed or zero because only fixed width columns can
1362 // have a vertical alignment
1363 if (column_info[c].p_width.zero())
1364 column_info[c].valignment = LYX_VALIGN_TOP;
1365 for (row_type r = 0; r < nrows(); ++r) {
1366 idx_type const cidx = cellIndex(r, c);
1367 // because of multicolumns
1368 toggleFixedWidth(cur, cellInset(cidx).get(),
1369 !getPWidth(cidx).zero(), isMultiColumn(cidx),
1371 if (isMultiRow(cidx))
1372 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1374 // cur can become invalid after paragraphs were merged
1379 bool Tabular::setFixedWidth(row_type r, col_type c)
1381 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1382 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1383 || (multicol && !cell_info[r][c].p_width.zero());
1384 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1389 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1390 Length const & width)
1392 if (!isMultiColumn(cell))
1395 cellInfo(cell).p_width = width;
1396 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1397 isMultiColumn(cell), isMultiRow(cell));
1398 // cur can become invalid after paragraphs were merged
1404 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1406 column_info[cellColumn(cell)].varwidth = varwidth;
1411 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1413 cellInfo(cell).mroffset = mroffset;
1418 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1419 Tabular::Feature what)
1421 if (what == SET_SPECIAL_MULTICOLUMN)
1422 cellInfo(cell).align_special = special;
1424 column_info[cellColumn(cell)].align_special = special;
1428 void Tabular::setTopLine(idx_type i, bool line)
1430 cellInfo(i).top_line = line;
1434 void Tabular::setBottomLine(idx_type i, bool line)
1436 cellInfo(i).bottom_line = line;
1440 void Tabular::setTopLineLTrim(idx_type i, bool val)
1442 cellInfo(i).top_line_ltrimmed = val;
1446 void Tabular::setTopLineRTrim(idx_type i, bool val)
1448 cellInfo(i).top_line_rtrimmed = val;
1452 void Tabular::setBottomLineLTrim(idx_type i, bool val)
1454 cellInfo(i).bottom_line_ltrimmed = val;
1458 void Tabular::setBottomLineRTrim(idx_type i, bool val)
1460 cellInfo(i).bottom_line_rtrimmed = val;
1464 void Tabular::setTopLineTrim(idx_type i, pair<bool, bool> trim)
1466 setTopLineLTrim(i, trim.first);
1467 setTopLineRTrim(i, trim.second);
1470 void Tabular::setBottomLineTrim(idx_type i, pair<bool, bool> trim)
1472 setBottomLineLTrim(i, trim.first);
1473 setBottomLineRTrim(i, trim.second);
1476 void Tabular::setLeftLine(idx_type cell, bool line)
1478 cellInfo(cell).left_line = line;
1482 void Tabular::setRightLine(idx_type cell, bool line)
1484 cellInfo(cell).right_line = line;
1487 bool Tabular::rowTopLine(row_type r) const
1489 bool all_rows_set = true;
1490 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1491 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1492 return all_rows_set;
1496 bool Tabular::rowBottomLine(row_type r) const
1498 bool all_rows_set = true;
1499 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1500 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1501 return all_rows_set;
1505 bool Tabular::columnLeftLine(col_type c) const
1512 for (row_type r = 0; r < nrows(); ++r) {
1513 idx_type const i = cellIndex(r, c);
1514 if (c == cellColumn(i)) {
1516 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1517 if (cellInfo(i).left_line || right)
1521 return 2 * nrows_left >= total;
1525 bool Tabular::columnRightLine(col_type c) const
1530 int nrows_right = 0;
1532 for (row_type r = 0; r < nrows(); ++r) {
1533 idx_type i = cellIndex(r, c);
1534 if (c == cellColumn(i) + columnSpan(i) - 1) {
1536 bool left = (c + 1 < ncols()
1537 && cellInfo(cellIndex(r, c + 1)).left_line)
1538 || c + 1 == ncols();
1539 if (cellInfo(i).right_line && left)
1543 return 2 * nrows_right >= total;
1547 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1549 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1550 return cellInfo(cell).alignment;
1552 return column_info[cellColumn(cell)].alignment;
1557 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1559 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1560 return cellInfo(cell).valignment;
1561 return column_info[cellColumn(cell)].valignment;
1565 int Tabular::offsetVAlignment() const
1567 // for top-alignment the first horizontal table line must be exactly at
1568 // the position of the base line of the surrounding text line
1569 // for bottom alignment, the same is for the last table line
1570 int offset_valign = 0;
1571 switch (tabular_valignment) {
1572 case Tabular::LYX_VALIGN_BOTTOM:
1573 offset_valign = rowAscent(0) - height();
1575 case Tabular::LYX_VALIGN_MIDDLE:
1576 offset_valign = (- height()) / 2 + rowAscent(0);
1578 case Tabular::LYX_VALIGN_TOP:
1579 offset_valign = rowAscent(0);
1582 return offset_valign;
1586 Length const Tabular::getPWidth(idx_type cell) const
1588 if (isMultiColumn(cell))
1589 return cellInfo(cell).p_width;
1590 return column_info[cellColumn(cell)].p_width;
1594 Length const Tabular::getMROffset(idx_type cell) const
1596 return cellInfo(cell).mroffset;
1600 int Tabular::textHOffset(idx_type cell) const
1602 // the LaTeX Way :-(
1603 int x = WIDTH_OF_LINE;
1605 int const w = cellWidth(cell) - cellInfo(cell).width;
1607 switch (getAlignment(cell)) {
1608 case LYX_ALIGN_CENTER:
1611 case LYX_ALIGN_RIGHT:
1614 case LYX_ALIGN_DECIMAL: {
1615 // we center when no decimal point
1616 if (cellInfo(cell).decimal_width == 0) {
1620 col_type const c = cellColumn(cell);
1621 int max_dhoffset = 0;
1622 for(row_type r = 0; r < row_info.size() ; ++r) {
1623 idx_type const i = cellIndex(r, c);
1624 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1625 && cellInfo(i).decimal_width != 0)
1626 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1628 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1631 // LYX_ALIGN_LEFT: nothing :-)
1639 int Tabular::textVOffset(idx_type cell) const
1641 int voffset = cellInfo(cell).voffset;
1642 if (isMultiRow(cell)) {
1643 row_type const row = cellRow(cell);
1644 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1650 Tabular::idx_type Tabular::getFirstCellInRow(row_type row, bool const ct) const
1653 idx_type const numcells = numberOfCellsInRow(row);
1654 // we check against numcells to make sure we do not crash if all the
1655 // cells are multirow (bug #7535), but in that case our return value
1656 // is really invalid, i.e., it is NOT the first cell in the row. but
1657 // i do not know what to do here. (rgh)
1658 while (c < numcells - 1
1659 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1660 || (ct && column_info[c].change.deleted())))
1662 return cell_info[row][c].cellno;
1666 Tabular::idx_type Tabular::getLastCellInRow(row_type row, bool const ct) const
1668 col_type c = ncols() - 1;
1669 // of course we check against 0 so we don't crash. but we have the same
1670 // problem as in the previous routine: if all the cells are part of a
1671 // multirow or part of a multi column, then our return value is invalid.
1673 && ((cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1674 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1675 || (ct && column_info[c].change.deleted())))
1677 return cell_info[row][c].cellno;
1681 Tabular::row_type Tabular::getFirstRow(bool const ct) const
1686 // exclude deleted rows if ct == true
1687 while (r < nrows() && row_info[r].change.deleted())
1693 Tabular::row_type Tabular::getLastRow(bool const ct) const
1695 row_type r = nrows() - 1;
1698 // exclude deleted rows if ct == true
1699 while (r > 0 && row_info[r].change.deleted())
1705 Tabular::row_type Tabular::cellRow(idx_type cell) const
1707 if (cell >= numberofcells)
1711 return rowofcell[cell];
1715 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1717 if (cell >= numberofcells)
1721 return columnofcell[cell];
1725 void Tabular::write(ostream & os) const
1729 << write_attribute("version", 3)
1730 << write_attribute("rows", nrows())
1731 << write_attribute("columns", ncols())
1733 // global longtable options
1735 << write_attribute("rotate", rotate)
1736 << write_attribute("booktabs", use_booktabs)
1737 << write_attribute("islongtable", is_long_tabular)
1738 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1739 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1740 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1741 << write_attribute("headTopDL", endhead.topDL)
1742 << write_attribute("headBottomDL", endhead.bottomDL)
1743 << write_attribute("footTopDL", endfoot.topDL)
1744 << write_attribute("footBottomDL", endfoot.bottomDL)
1745 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1746 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1747 << write_attribute("lastFootEmpty", endlastfoot.empty);
1748 // longtables cannot be aligned vertically
1749 if (!is_long_tabular)
1750 os << write_attribute("tabularvalignment", tabular_valignment);
1751 os << write_attribute("tabularwidth", tabular_width);
1752 if (is_long_tabular)
1753 os << write_attribute("longtabularalignment", longtabular_alignment);
1755 for (col_type c = 0; c < ncols(); ++c) {
1757 << write_attribute("alignment", column_info[c].alignment);
1758 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1759 os << write_attribute("decimal_point", column_info[c].decimal_point);
1760 os << write_attribute("change", column_info[c].change, buffer().params())
1761 << write_attribute("valignment", column_info[c].valignment)
1762 << write_attribute("width", column_info[c].p_width.asString())
1763 << write_attribute("varwidth", column_info[c].varwidth)
1764 << write_attribute("special", column_info[c].align_special)
1767 for (row_type r = 0; r < nrows(); ++r) {
1768 static const string def("default");
1770 if (row_info[r].top_space_default)
1771 os << write_attribute("topspace", def);
1773 os << write_attribute("topspace", row_info[r].top_space);
1774 if (row_info[r].bottom_space_default)
1775 os << write_attribute("bottomspace", def);
1777 os << write_attribute("bottomspace", row_info[r].bottom_space);
1778 if (row_info[r].interline_space_default)
1779 os << write_attribute("interlinespace", def);
1781 os << write_attribute("interlinespace", row_info[r].interline_space);
1782 os << write_attribute("change", row_info[r].change, buffer().params())
1783 << write_attribute("endhead", row_info[r].endhead)
1784 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1785 << write_attribute("endfoot", row_info[r].endfoot)
1786 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1787 << write_attribute("newpage", row_info[r].newpage)
1788 << write_attribute("caption", row_info[r].caption)
1790 for (col_type c = 0; c < ncols(); ++c) {
1792 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1793 << write_attribute("multirow", cell_info[r][c].multirow)
1794 << write_attribute("mroffset", cell_info[r][c].mroffset)
1795 << write_attribute("alignment", cell_info[r][c].alignment)
1796 << write_attribute("valignment", cell_info[r][c].valignment)
1797 << write_attribute("topline", cell_info[r][c].top_line)
1798 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1799 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1800 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1801 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1802 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1803 << write_attribute("leftline", cell_info[r][c].left_line)
1804 << write_attribute("rightline", cell_info[r][c].right_line)
1805 << write_attribute("rotate", cell_info[r][c].rotate)
1806 << write_attribute("usebox", cell_info[r][c].usebox)
1807 << write_attribute("width", cell_info[r][c].p_width)
1808 << write_attribute("special", cell_info[r][c].align_special)
1810 os << "\\begin_inset ";
1811 cell_info[r][c].inset->write(os);
1812 os << "\n\\end_inset\n"
1814 // FIXME This can be removed again once the mystery
1815 // crash has been resolved.
1820 os << "</lyxtabular>\n";
1824 void Tabular::read(Lexer & lex)
1827 istream & is = lex.getStream();
1829 l_getline(is, line);
1830 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1831 LASSERT(false, return);
1835 if (!getTokenValue(line, "version", version))
1837 LATTEST(version >= 2);
1840 if (!getTokenValue(line, "rows", rows_arg))
1843 if (!getTokenValue(line, "columns", columns_arg))
1845 init(buffer_, rows_arg, columns_arg);
1846 l_getline(is, line);
1847 if (!prefixIs(line, "<features")) {
1848 lyxerr << "Wrong tabular format (expected <features ...> got"
1849 << line << ')' << endl;
1852 getTokenValue(line, "rotate", rotate);
1853 getTokenValue(line, "booktabs", use_booktabs);
1854 getTokenValue(line, "islongtable", is_long_tabular);
1855 getTokenValue(line, "tabularvalignment", tabular_valignment);
1856 getTokenValue(line, "tabularwidth", tabular_width);
1857 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1858 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1859 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1860 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1861 getTokenValue(line, "headTopDL", endhead.topDL);
1862 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1863 getTokenValue(line, "footTopDL", endfoot.topDL);
1864 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1865 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1866 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1867 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1869 for (col_type c = 0; c < ncols(); ++c) {
1871 if (!prefixIs(line,"<column")) {
1872 lyxerr << "Wrong tabular format (expected <column ...> got"
1873 << line << ')' << endl;
1876 getTokenValue(line, "alignment", column_info[c].alignment);
1877 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1878 getTokenValue(line, "valignment", column_info[c].valignment);
1879 getTokenValue(line, "width", column_info[c].p_width);
1880 getTokenValue(line, "special", column_info[c].align_special);
1881 getTokenValue(line, "varwidth", column_info[c].varwidth);
1882 getTokenValue(line, "change", column_info[c].change, buffer().params());
1885 for (row_type i = 0; i < nrows(); ++i) {
1886 l_getline(is, line);
1887 if (!prefixIs(line, "<row")) {
1888 lyxerr << "Wrong tabular format (expected <row ...> got"
1889 << line << ')' << endl;
1892 getTokenValue(line, "topspace", row_info[i].top_space,
1893 row_info[i].top_space_default);
1894 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1895 row_info[i].bottom_space_default);
1896 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1897 row_info[i].interline_space_default);
1898 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1899 getTokenValue(line, "endhead", row_info[i].endhead);
1900 getTokenValue(line, "endfoot", row_info[i].endfoot);
1901 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1902 getTokenValue(line, "newpage", row_info[i].newpage);
1903 getTokenValue(line, "caption", row_info[i].caption);
1904 getTokenValue(line, "change", row_info[i].change, buffer().params());
1905 for (col_type j = 0; j < ncols(); ++j) {
1906 l_getline(is, line);
1907 if (!prefixIs(line, "<cell")) {
1908 lyxerr << "Wrong tabular format (expected <cell ...> got"
1909 << line << ')' << endl;
1912 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1913 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1914 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1915 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1916 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1917 getTokenValue(line, "topline", cell_info[i][j].top_line);
1918 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1919 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1920 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1921 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1922 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1923 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1924 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1925 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1926 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1927 getTokenValue(line, "width", cell_info[i][j].p_width);
1929 getTokenValue(line, "special", cell_info[i][j].align_special);
1930 l_getline(is, line);
1931 if (prefixIs(line, "\\begin_inset")) {
1932 cell_info[i][j].inset->setBuffer(*buffer_);
1933 cell_info[i][j].inset->read(lex);
1934 l_getline(is, line);
1936 if (!prefixIs(line, "</cell>")) {
1937 lyxerr << "Wrong tabular format (expected </cell> got"
1938 << line << ')' << endl;
1942 l_getline(is, line);
1943 if (!prefixIs(line, "</row>")) {
1944 lyxerr << "Wrong tabular format (expected </row> got"
1945 << line << ')' << endl;
1949 while (!prefixIs(line, "</lyxtabular>")) {
1950 l_getline(is, line);
1956 bool Tabular::isMultiColumn(idx_type cell) const
1958 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1959 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1963 bool Tabular::hasMultiColumn(col_type c) const
1965 for (row_type r = 0; r < nrows(); ++r) {
1966 if (isMultiColumn(cellIndex(r, c)))
1973 bool Tabular::hasVarwidthColumn() const
1975 for (col_type c = 0; c < ncols(); ++c) {
1976 if (column_info[c].varwidth)
1983 bool Tabular::isVTypeColumn(col_type c) const
1985 for (row_type r = 0; r < nrows(); ++r) {
1986 idx_type idx = cellIndex(r, c);
1987 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1994 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1996 return cell_info[cellRow(cell)][cellColumn(cell)];
2000 Tabular::CellData & Tabular::cellInfo(idx_type cell)
2002 return cell_info[cellRow(cell)][cellColumn(cell)];
2006 Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
2007 bool const right_border)
2009 idx_type const col = cellColumn(cell);
2010 idx_type const row = cellRow(cell);
2011 for (idx_type i = 0; i < number; ++i)
2012 unsetMultiRow(cellIndex(row, col + i));
2014 // unsetting of multirow may have invalidated cell index
2015 cell = cellIndex(row, col);
2016 CellData & cs = cellInfo(cell);
2017 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2018 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
2019 cs.alignment = column_info[col].alignment;
2020 setRightLine(cell, right_border);
2021 // non-fixed width multicolumns cannot have multiple paragraphs
2022 if (getPWidth(cell).zero()) {
2023 toggleFixedWidth(cur, cellInset(cell).get(),
2024 !getPWidth(cell).zero(), isMultiColumn(cell),
2026 // cur can become invalid after paragraphs were merged
2030 idx_type lastcell = cellIndex(row, col + number - 1);
2031 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2032 CellData & cs1 = cellInfo(cell + i);
2033 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2034 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2042 bool Tabular::isMultiRow(idx_type cell) const
2044 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2045 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2048 bool Tabular::hasMultiRow(row_type r) const
2050 for (col_type c = 0; c < ncols(); ++c) {
2051 if (isMultiRow(cellIndex(r, c)))
2057 Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2058 bool const bottom_border,
2059 LyXAlignment const halign)
2061 idx_type const col = cellColumn(cell);
2062 idx_type const row = cellRow(cell);
2063 for (idx_type i = 0; i < number; ++i)
2064 unsetMultiColumn(cellIndex(row + i, col));
2066 // unsetting of multirow may have invalidated cell index
2067 cell = cellIndex(row, col);
2068 CellData & cs = cellInfo(cell);
2069 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2070 cs.valignment = LYX_VALIGN_MIDDLE;
2071 // the horizontal alignment of multirow cells can only
2072 // be changed for the whole table row,
2073 // support changing this only for the multirow cell can be done via
2075 if (getPWidth(cell).zero())
2076 cs.alignment = halign;
2078 cs.alignment = LYX_ALIGN_LEFT;
2080 // Multirows cannot have multiple paragraphs
2081 if (getPWidth(cell).zero()) {
2082 toggleFixedWidth(cur, cellInset(cell).get(),
2083 !getPWidth(cell).zero(),
2084 isMultiColumn(cell), isMultiRow(cell));
2085 // cur can become invalid after paragraphs were merged
2089 // set the bottom line of the last selected cell
2090 setBottomLine(cell, bottom_border);
2092 for (idx_type i = 1; i < number; ++i) {
2093 CellData & cs1 = cell_info[row + i][col];
2094 cs1.multirow = CELL_PART_OF_MULTIROW;
2095 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2103 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
2105 row_type const row = cellRow(cell);
2106 col_type const col = cellColumn(cell);
2108 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2115 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
2117 col_type const column = cellColumn(cell);
2118 col_type row = cellRow(cell) + 1;
2119 while (row < nrows() && isPartOfMultiRow(row, column))
2122 return row - cellRow(cell);
2126 void Tabular::unsetMultiColumn(idx_type cell)
2128 if (!isMultiColumn(cell))
2131 row_type const row = cellRow(cell);
2132 col_type const col = cellColumn(cell);
2133 row_type const span = columnSpan(cell);
2134 for (col_type c = 0; c < span; ++c) {
2135 // in the table dialog the lines are set in every case
2136 // when unsetting a multicolumn this leads to an additional right
2137 // line for every cell that was part of the former multicolumn cell,
2138 // except if the cell is in the last column
2139 // therefore remove this line
2140 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2141 && (col + c) < (col + span - 1))
2142 cell_info[row][col + c].right_line = false;
2143 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2149 void Tabular::unsetMultiRow(idx_type cell)
2151 if (!isMultiRow(cell))
2154 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2155 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2156 row_type const row = cellRow(cell);
2157 col_type const col = cellColumn(cell);
2158 row_type const span = rowSpan(cell);
2159 for (row_type r = 0; r < span; ++r)
2160 cell_info[row + r][col].multirow = CELL_NORMAL;
2165 void Tabular::setRotateCell(idx_type cell, int value)
2167 cellInfo(cell).rotate = value;
2171 int Tabular::getRotateCell(idx_type cell) const
2173 return cellInfo(cell).rotate;
2177 bool Tabular::needRotating() const
2179 if (rotate && !is_long_tabular)
2181 for (row_type r = 0; r < nrows(); ++r)
2182 for (col_type c = 0; c < ncols(); ++c)
2183 if (cell_info[r][c].rotate != 0)
2189 bool Tabular::isLastCell(idx_type cell) const
2191 if (cell + 1 < numberofcells)
2197 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2199 if (cellRow(cell) == 0)
2202 col_type const col = cellColumn(cell);
2203 row_type r = cellRow(cell) - 1;
2204 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2207 return cell_info[r][col].cellno;
2211 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2213 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2214 if (nextrow < nrows())
2215 return cell_info[nextrow][cellColumn(cell)].cellno;
2220 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2222 LASSERT(column != npos && column < ncols(), column = 0);
2223 LASSERT(row != npos && row < nrows(), row = 0);
2224 return cell_info[row][column].cellno;
2228 void Tabular::setUsebox(idx_type cell, BoxType type)
2230 cellInfo(cell).usebox = type;
2234 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2236 if (getRotateCell(cell) == 0
2237 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2238 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2240 if (cellInfo(cell).usebox > 1)
2241 return cellInfo(cell).usebox;
2242 return useBox(cell);
2247 // This are functions used for the longtable support
2249 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2255 row_info[row].endfirsthead = flag;
2259 row_info[row].endhead = flag;
2264 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2267 hd.set = haveLTHead();
2268 return row_info[row].endhead;
2272 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2275 hd.set = haveLTFirstHead();
2276 return row_info[row].endfirsthead;
2280 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2286 row_info[row].endlastfoot = flag;
2290 row_info[row].endfoot = flag;
2295 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2298 fd.set = haveLTFoot();
2299 return row_info[row].endfoot;
2303 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2306 fd.set = haveLTLastFoot();
2307 return row_info[row].endlastfoot;
2311 void Tabular::setLTNewPage(row_type row, bool what)
2313 row_info[row].newpage = what;
2317 bool Tabular::getLTNewPage(row_type row) const
2319 return row_info[row].newpage;
2323 bool Tabular::haveLTHead(bool withcaptions) const
2325 if (!is_long_tabular)
2327 for (row_type i = 0; i < nrows(); ++i)
2328 if (row_info[i].endhead &&
2329 (withcaptions || !row_info[i].caption))
2335 bool Tabular::haveLTFirstHead(bool withcaptions) const
2337 if (!is_long_tabular || endfirsthead.empty)
2339 for (row_type r = 0; r < nrows(); ++r)
2340 if (row_info[r].endfirsthead &&
2341 (withcaptions || !row_info[r].caption))
2347 bool Tabular::haveLTFoot(bool withcaptions) const
2349 if (!is_long_tabular)
2351 for (row_type r = 0; r < nrows(); ++r)
2352 if (row_info[r].endfoot &&
2353 (withcaptions || !row_info[r].caption))
2359 bool Tabular::haveLTLastFoot(bool withcaptions) const
2361 if (!is_long_tabular || endlastfoot.empty)
2363 for (row_type r = 0; r < nrows(); ++r)
2364 if (row_info[r].endlastfoot &&
2365 (withcaptions || !row_info[r].caption))
2371 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2373 idx_type i = getFirstCellInRow(row);
2375 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2376 setTopLine(i, false);
2377 setBottomLine(i, false);
2378 setLeftLine(i, false);
2379 setRightLine(i, false);
2380 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2381 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2382 setLTHead(row, true, endfirsthead, true);
2383 row_info[row].endfirsthead = true;
2386 unsetMultiColumn(i);
2387 // When unsetting a caption row, also all existing
2388 // captions in this row must be dissolved.
2390 row_info[row].caption = what;
2395 bool Tabular::ltCaption(row_type row) const
2397 return row_info[row].caption;
2401 bool Tabular::haveLTCaption(CaptionType captiontype) const
2403 if (!is_long_tabular)
2405 for (row_type r = 0; r < nrows(); ++r) {
2406 if (row_info[r].caption) {
2407 switch (captiontype) {
2408 case CAPTION_FIRSTHEAD:
2409 if (row_info[r].endfirsthead)
2413 if (row_info[r].endhead)
2417 if (row_info[r].endfoot)
2420 case CAPTION_LASTFOOT:
2421 if (row_info[r].endlastfoot)
2433 // end longtable support functions
2435 void Tabular::setRowAscent(row_type row, int height)
2437 if (row >= nrows() || row_info[row].ascent == height)
2439 row_info[row].ascent = height;
2443 void Tabular::setRowDescent(row_type row, int height)
2445 if (row >= nrows() || row_info[row].descent == height)
2447 row_info[row].descent = height;
2451 int Tabular::rowAscent(row_type row) const
2453 LASSERT(row < nrows(), row = 0);
2454 return row_info[row].ascent;
2458 int Tabular::rowDescent(row_type row) const
2460 LASSERT(row < nrows(), row = 0);
2461 return row_info[row].descent;
2465 int Tabular::height() const
2468 for (row_type row = 0; row < nrows(); ++row)
2469 height += rowAscent(row) + rowDescent(row) +
2475 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2477 LASSERT(row < nrows(), return false);
2478 LASSERT(column < ncols(), return false);
2479 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2483 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2485 LASSERT(row < nrows(), return false);
2486 LASSERT(column < ncols(), return false);
2487 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2491 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns) const
2493 // we only output complete row lines and the 1st row here, the rest
2494 // is done in Tabular::TeXBottomHLine(...)
2496 // get for each column the topline (if any)
2497 map<col_type, bool> topline, topltrims, toprtrims;
2499 bool have_trims = false;
2500 for (auto const & c : columns) {
2501 topline[c] = topLine(cellIndex(row, c));
2502 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2503 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2504 // If cell is part of a multirow and not the first cell of the
2505 // multirow, no line must be drawn.
2507 if (isMultiRow(cellIndex(row, c))
2508 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2510 topltrims[c] = false;
2511 toprtrims[c] = false;
2513 // copy trimming to multicolumn parts
2514 if (isPartOfMultiColumn(row, c)) {
2515 topltrims[c] = topltrims[c-1];
2516 toprtrims[c] = toprtrims[c-1];
2518 if (topline.find(c) != topline.end() && topline.find(c)->second)
2520 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2521 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2525 // do nothing if empty first row, or incomplete row line after
2526 row_type first = getFirstRow(!buffer().params().output_changes);
2527 if ((row == first && nset == 0) || (row > first && nset != columns.size()))
2530 // Is this the actual first row (excluding longtable caption row)?
2531 bool const realfirstrow = (row == first
2532 || (is_long_tabular && row == first + 1 && ltCaption(first)));
2534 // only output complete row lines and the 1st row's clines
2535 if (nset == columns.size() && !have_trims) {
2537 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2541 } else if (realfirstrow || have_trims) {
2542 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2544 for (auto & cl : columns) {
2548 if (topline.find(c)->second) {
2549 col_type offset = 0;
2550 for (col_type j = 0 ; j < c; ++j)
2551 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2553 string const firstcol = convert<string>(c + 1 + offset);
2554 while (isPartOfMultiColumn(row, c))
2557 if (topltrims.find(c) != topltrims.end()
2558 && topltrims.find(c)->second)
2560 col_type cstart = c;
2561 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2562 if (isMultiColumn(cellIndex(row, c))
2563 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2565 if (c > cstart && topltrims.find(c) != topltrims.end()
2566 && topltrims.find(c)->second) {
2567 if (!isPartOfMultiColumn(row, c))
2570 } else if (toprtrims.find(c) != toprtrims.end()
2571 && toprtrims.find(c)->second)
2575 for (col_type j = cstart ; j < c ; ++j)
2576 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2578 col_type const lastcol = c + 1 + offset;
2579 if (toprtrims.find(c) != toprtrims.end()
2580 && toprtrims.find(c)->second)
2585 os << "(" << trim << ")";
2586 os << "{" << firstcol << '-' << lastcol << "}";
2587 if (c == columns.size() - 1)
2597 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns) const
2599 // we output bottomlines of row r and the toplines of row r+1
2600 // if the latter do not span the whole tabular
2602 // get the bottomlines of row r, and toplines in next row
2603 bool lastrow = row == getLastRow(!buffer().params().output_changes);
2604 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2605 bool nextrowset = true;
2606 for (auto const & c : columns) {
2607 idx_type const idx = cellIndex(row, c);
2608 bottomline[c] = bottomLine(cellIndex(row, c));
2609 bottomltrims[c] = bottomLineTrim(idx).first;
2610 bottomrtrims[c] = bottomLineTrim(idx).second;
2611 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2612 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2613 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2614 // If cell is part of a multirow and not the last cell of the
2615 // multirow, no line must be drawn.
2617 if (isMultiRow(cellIndex(row, c))
2618 && isMultiRow(cellIndex(row + 1, c))
2619 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2620 bottomline[c] = false;
2622 bottomltrims[c] = false;
2623 bottomrtrims[c] = false;
2624 topltrims[c] = false;
2625 toprtrims[c] = false;
2627 // copy trimming in multicolumn parts
2628 if (isPartOfMultiColumn(row, c)) {
2629 topltrims[c] = topltrims[c-1];
2630 toprtrims[c] = toprtrims[c-1];
2631 bottomltrims[c] = bottomltrims[c-1];
2632 bottomrtrims[c] = bottomrtrims[c-1];
2635 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2638 // combine this row's bottom lines and next row's toplines if necessary
2640 bool have_trims = false;
2641 for (auto const & c : columns) {
2643 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2644 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2645 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2646 bottomrtrims[c] =(bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2647 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2648 if (bottomline.find(c)->second)
2650 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2651 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2655 // do nothing if empty, OR incomplete row line with a topline in next row
2656 if (nset == 0 || (nextrowset && nset != columns.size()))
2659 if (nset == columns.size() && !have_trims) {
2661 os << (lastrow ? "\\bottomrule" : "\\midrule");
2665 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2667 for (auto & cl : columns) {
2671 if (bottomline.find(c)->second) {
2672 col_type offset = 0;
2673 for (col_type j = 0 ; j < c; ++j)
2674 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2676 string const firstcol = convert<string>(c + 1 + offset);
2677 while (isPartOfMultiColumn(row, c))
2680 if (bottomltrims.find(c) != bottomltrims.end()
2681 && bottomltrims.find(c)->second)
2683 col_type cstart = c;
2684 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2685 if (isMultiColumn(cellIndex(row, c))
2687 && isPartOfMultiColumn(row, c + 1))
2690 && bottomltrims.find(c) != bottomltrims.end()
2691 && bottomltrims.find(c)->second) {
2692 if (!isPartOfMultiColumn(row, c))
2695 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2696 && bottomrtrims.find(c)->second)
2700 for (col_type j = cstart ; j < c ; ++j)
2701 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2703 col_type const lastcol = c + 1 + offset;
2704 if (bottomrtrims.find(c) != bottomrtrims.end()
2705 && bottomrtrims.find(c)->second)
2710 os << "(" << trim << ")";
2711 os << "{" << firstcol << '-' << lastcol << "}";
2712 if (c == columns.size() - 1)
2722 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2723 bool & ismulticol, bool & ismultirow,
2724 bool const bidi) const
2726 row_type const r = cellRow(cell);
2727 if (is_long_tabular && row_info[r].caption)
2730 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2731 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2732 // figure out how to set the lines
2733 // we always set double lines to the right of the cell
2734 // or left in bidi RTL, respectively.
2735 col_type const c = cellColumn(cell);
2736 col_type const nextcol = c + columnSpan(cell);
2737 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2738 bool colright = columnRightLine(c);
2739 bool colleft = columnLeftLine(c);
2740 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2741 bool nextcellleft = nextcol < ncols()
2742 && leftLine(cellIndex(r, nextcol));
2743 bool coldouble = colright && nextcolleft;
2744 bool celldouble = rightLine(cell) && nextcellleft;
2746 ismulticol = (isMultiColumn(cell)
2747 || (c == 0 && colleft != leftLine(cell))
2748 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2749 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2750 || (coldouble != celldouble))
2753 // we center in multicol when no decimal point
2755 docstring const align_d = column_info[c].decimal_point;
2756 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2757 bool const nosep = !dit;
2758 ismulticol |= nosep;
2759 celldouble &= nosep;
2762 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2763 int latexcolspan = columnSpan(cell);
2764 for (col_type col = c; col < c + columnSpan(cell); ++col)
2765 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2769 os << "\\multicolumn{" << latexcolspan << "}{";
2770 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2771 || (!bidi && c == 0 && leftLine(cell))))
2773 if (bidi && celldouble)
2774 // add extra vertical line if we want a double one
2776 if (!cellInfo(cell).align_special.empty()) {
2777 os << cellInfo(cell).align_special;
2779 if (!getPWidth(cell).zero()) {
2781 case LYX_ALIGN_LEFT:
2782 os << ">{\\raggedright}";
2784 case LYX_ALIGN_RIGHT:
2785 os << ">{\\raggedleft}";
2787 case LYX_ALIGN_CENTER:
2788 os << ">{\\centering}";
2794 case LYX_VALIGN_TOP:
2797 case LYX_VALIGN_MIDDLE:
2800 case LYX_VALIGN_BOTTOM:
2805 << from_ascii(getPWidth(cell).asLatexString())
2809 case LYX_ALIGN_LEFT:
2812 case LYX_ALIGN_RIGHT:
2819 } // end if else !getPWidth
2820 } // end if else !cellinfo_of_cell
2821 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2823 if (!bidi && celldouble)
2824 // add extra vertical line if we want a double one
2827 } // end if ismulticol
2829 // we only need code for the first multirow cell
2830 ismultirow = isMultiRow(cell);
2832 os << "\\multirow{" << rowSpan(cell) << "}{";
2833 if (!getPWidth(cell).zero())
2834 os << from_ascii(getPWidth(cell).asLatexString());
2836 // we need to set a default value
2839 if (!getMROffset(cell).zero())
2840 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2842 } // end if ismultirow
2844 if (getRotateCell(cell) != 0)
2845 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2847 if (getUsebox(cell) == BOX_PARBOX) {
2850 case LYX_VALIGN_TOP:
2853 case LYX_VALIGN_MIDDLE:
2856 case LYX_VALIGN_BOTTOM:
2860 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2862 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2863 os << "\\begin{minipage}[";
2865 case LYX_VALIGN_TOP:
2868 case LYX_VALIGN_MIDDLE:
2871 case LYX_VALIGN_BOTTOM:
2875 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2877 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2878 os << "\\begin{varwidth}[";
2880 case LYX_VALIGN_TOP:
2883 case LYX_VALIGN_MIDDLE:
2886 case LYX_VALIGN_BOTTOM:
2890 os << "]{\\linewidth}\n";
2895 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2896 bool ismulticol, bool ismultirow) const
2898 row_type const r = cellRow(cell);
2899 if (is_long_tabular && row_info[r].caption)
2903 if (getUsebox(cell) == BOX_PARBOX)
2905 else if (getUsebox(cell) == BOX_MINIPAGE)
2906 os << breakln << "\\end{minipage}";
2907 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2908 os << breakln << "\\end{varwidth}";
2909 if (getRotateCell(cell) != 0)
2910 os << breakln << "\\end{turn}";
2918 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2919 OutputParams const & runparams,
2920 list<col_type> columns) const
2922 if (!is_long_tabular)
2926 // output caption which is in no header or footer
2927 if (haveLTCaption()) {
2928 for (row_type r = 0; r < nrows(); ++r) {
2929 if (row_info[r].caption &&
2930 !row_info[r].endfirsthead && !row_info[r].endhead &&
2931 !row_info[r].endfoot && !row_info[r].endlastfoot)
2932 TeXRow(os, r, runparams, columns);
2935 // output first header info
2936 if (haveLTFirstHead()) {
2937 if (endfirsthead.topDL)
2939 for (row_type r = 0; r < nrows(); ++r) {
2940 if (row_info[r].endfirsthead)
2941 TeXRow(os, r, runparams, columns);
2943 if (endfirsthead.bottomDL)
2945 os << "\\endfirsthead\n";
2947 // output header info
2949 if (endfirsthead.empty && !haveLTFirstHead())
2950 os << "\\endfirsthead\n";
2953 for (row_type r = 0; r < nrows(); ++r) {
2954 if (row_info[r].endhead)
2955 TeXRow(os, r, runparams, columns);
2957 if (endhead.bottomDL)
2959 os << "\\endhead\n";
2961 // output footer info
2965 for (row_type r = 0; r < nrows(); ++r) {
2966 if (row_info[r].endfoot)
2967 TeXRow(os, r, runparams, columns);
2969 if (endfoot.bottomDL)
2971 os << "\\endfoot\n";
2972 if (endlastfoot.empty && !haveLTLastFoot())
2973 os << "\\endlastfoot\n";
2975 // output lastfooter info
2976 if (haveLTLastFoot()) {
2977 if (endlastfoot.topDL)
2979 for (row_type r = 0; r < nrows(); ++r) {
2980 if (row_info[r].endlastfoot)
2981 TeXRow(os, r, runparams, columns);
2983 if (endlastfoot.bottomDL)
2985 os << "\\endlastfoot\n";
2990 bool Tabular::isValidRow(row_type row) const
2992 if (!is_long_tabular)
2994 return !row_info[row].endhead && !row_info[row].endfirsthead
2995 && !row_info[row].endfoot && !row_info[row].endlastfoot
2996 && !row_info[row].caption;
3000 void Tabular::TeXRow(otexstream & os, row_type row,
3001 OutputParams const & runparams,
3002 list<col_type> columns) const
3004 idx_type cell = cellIndex(row, 0);
3006 //output the top line
3007 TeXTopHLine(os, row, columns);
3009 if (row_info[row].top_space_default) {
3011 os << "\\addlinespace\n";
3013 os << "\\noalign{\\vskip\\doublerulesep}\n";
3014 } else if(!row_info[row].top_space.zero()) {
3016 os << "\\addlinespace["
3017 << from_ascii(row_info[row].top_space.asLatexString())
3020 os << "\\noalign{\\vskip"
3021 << from_ascii(row_info[row].top_space.asLatexString())
3025 bool ismulticol = false;
3026 bool ismultirow = false;
3028 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3029 // Luabibdi (used by LuaTeX) behaves like classic
3030 bool const bidi_rtl =
3031 runparams.local_font->isRightToLeft()
3032 && runparams.useBidiPackage();
3033 bool const ct = !buffer().params().output_changes;
3035 bidi_rtl ? getFirstCellInRow(row, ct) : getLastCellInRow(row, ct);
3037 for (auto const & c : columns) {
3038 if (isPartOfMultiColumn(row, c))
3041 cell = cellIndex(row, c);
3043 if (isPartOfMultiRow(row, c)
3044 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3045 if (cell != lastcell)
3050 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3051 InsetTableCell const * inset = cellInset(cell);
3053 Paragraph const & par = inset->paragraphs().front();
3055 os.texrow().forceStart(par.id(), 0);
3057 bool rtl = par.isRTL(buffer().params())
3059 && getPWidth(cell).zero()
3060 && !runparams.use_polyglossia;
3064 par.getParLanguage(buffer().params())->lang();
3065 if (lang == "farsi")
3067 else if (lang == "arabic_arabi")
3069 // currently, remaining RTL languages are
3070 // arabic_arabtex and hebrew
3074 // pass to the OutputParams that we are in a cell and
3075 // which alignment we have set.
3076 // InsetNewline needs this context information.
3077 OutputParams newrp(runparams);
3078 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3079 ? OutputParams::PLAIN
3080 : OutputParams::ALIGNED;
3082 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3083 // copy cell and split in 2
3084 InsetTableCell head = InsetTableCell(*cellInset(cell));
3085 head.setBuffer(const_cast<Buffer &>(buffer()));
3086 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3088 dit.push_back(CursorSlice(head));
3089 head.setMacrocontextPositionRecursive(dit);
3090 bool hassep = false;
3091 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3093 tail.setBuffer(head.buffer());
3095 dit.push_back(CursorSlice(tail));
3096 tail.setMacrocontextPositionRecursive(dit);
3100 tail.latex(os, newrp);
3103 head.latex(os, newrp);
3105 head.latex(os, newrp);
3108 tail.latex(os, newrp);
3111 } else if (ltCaption(row)) {
3112 // Inside longtable caption rows, we must only output the caption inset
3113 // with its content and omit anything outside of that (see #10791)
3114 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
3115 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
3116 for (; it != i_end; ++it) {
3117 if (it->lyxCode() != CAPTION_CODE)
3119 it->latex(os, runparams);
3122 } else if (!isPartOfMultiRow(row, c)) {
3123 if (!runparams.nice)
3124 os.texrow().start(par.id(), 0);
3125 inset->latex(os, newrp);
3128 runparams.encoding = newrp.encoding;
3132 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3133 if (cell != lastcell) { // not last cell in row
3140 os << "\\tabularnewline";
3141 if (row_info[row].bottom_space_default) {
3143 os << "\\addlinespace";
3145 os << "[\\doublerulesep]";
3146 } else if (!row_info[row].bottom_space.zero()) {
3148 os << "\\addlinespace";
3150 << from_ascii(row_info[row].bottom_space.asLatexString())
3155 //output the bottom line
3156 TeXBottomHLine(os, row, columns);
3158 if (row_info[row].interline_space_default) {
3160 os << "\\addlinespace\n";
3162 os << "\\noalign{\\vskip\\doublerulesep}\n";
3163 } else if (!row_info[row].interline_space.zero()) {
3165 os << "\\addlinespace["
3166 << from_ascii(row_info[row].interline_space.asLatexString())
3169 os << "\\noalign{\\vskip"
3170 << from_ascii(row_info[row].interline_space.asLatexString())
3176 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3178 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3179 && !hasVarwidthColumn();
3180 bool const is_xltabular = is_long_tabular
3181 && (hasVarwidthColumn() || !tabular_width.zero());
3182 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3184 //+---------------------------------------------------------------------
3185 //+ first the opening preamble +
3186 //+---------------------------------------------------------------------
3189 if (!TexRow::isNone(pos))
3190 os.texrow().start(pos);
3193 if (is_long_tabular)
3194 os << "\\begin{landscape}\n";
3196 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3199 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3200 // order for RTL (#9686). Thus we use this list.
3201 bool const bidi_rtl =
3202 runparams.local_font->isRightToLeft()
3203 && runparams.useBidiPackage();
3204 list<col_type> columns;
3205 for (col_type cl = 0; cl < ncols(); ++cl) {
3206 if (!buffer().params().output_changes && column_info[cl].change.deleted())
3209 columns.push_front(cl);
3211 columns.push_back(cl);
3214 // If we use \cline or \cmidrule, we need to locally de-activate
3215 // the - character when using languages that activate it (e.g., Czech, Slovak).
3216 bool deactivate_chars = false;
3217 if ((runparams.use_babel || runparams.use_polyglossia)
3218 && contains(runparams.active_chars, '-')) {
3219 bool have_clines = false;
3220 // Check if we use \cline or \cmidrule
3221 for (row_type row = 0; row < nrows(); ++row) {
3222 col_type bset = 0, tset = 0;
3223 for (auto const & c : columns) {
3224 idx_type const idx = cellIndex(row, c);
3225 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3226 || topLineTrim(idx).first || topLineTrim(idx).second) {
3230 if (bottomLine(cellIndex(row, c)))
3232 if (topLine(cellIndex(row, c)))
3235 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3241 deactivate_chars = true;
3242 os << "\\begingroup\n"
3243 << "\\catcode`\\-=12\n";
3247 if (is_long_tabular) {
3249 os << "\\begin{xltabular}";
3251 os << "\\begin{longtable}";
3252 switch (longtabular_alignment) {
3253 case LYX_LONGTABULAR_ALIGN_LEFT:
3256 case LYX_LONGTABULAR_ALIGN_CENTER:
3259 case LYX_LONGTABULAR_ALIGN_RIGHT:
3264 if (tabular_width.zero())
3265 os << "{" << from_ascii("\\columnwidth") << "}";
3267 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3270 if (is_tabular_star)
3271 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3272 else if (hasVarwidthColumn()) {
3273 os << "\\begin{tabularx}{";
3274 if (tabular_width.zero())
3275 os << from_ascii("\\columnwidth") << "}";
3277 os << from_ascii(tabular_width.asLatexString()) << "}";
3279 os << "\\begin{tabular}";
3280 switch (tabular_valignment) {
3281 case LYX_VALIGN_TOP:
3284 case LYX_VALIGN_MIDDLE:
3286 case LYX_VALIGN_BOTTOM:
3294 if (is_tabular_star)
3295 os << "@{\\extracolsep{\\fill}}";
3297 for (auto const & c : columns) {
3298 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3300 if (!column_info[c].align_special.empty()) {
3301 os << column_info[c].align_special;
3303 if (!column_info[c].p_width.zero()) {
3304 bool decimal = false;
3305 switch (column_info[c].alignment) {
3306 case LYX_ALIGN_LEFT:
3307 os << ">{\\raggedright}";
3309 case LYX_ALIGN_RIGHT:
3310 os << ">{\\raggedleft}";
3312 case LYX_ALIGN_CENTER:
3313 os << ">{\\centering}";
3315 case LYX_ALIGN_NONE:
3316 case LYX_ALIGN_BLOCK:
3317 case LYX_ALIGN_LAYOUT:
3318 case LYX_ALIGN_SPECIAL:
3320 case LYX_ALIGN_DECIMAL: {
3322 os << ">{\\raggedright}";
3324 os << ">{\\raggedleft}";
3331 switch (column_info[c].valignment) {
3332 case LYX_VALIGN_TOP:
3333 // this is the default
3335 case LYX_VALIGN_MIDDLE:
3338 case LYX_VALIGN_BOTTOM:
3344 // Fixed-width cells with alignment at decimal separator
3345 // are output as two cells of half the width with the decimal
3346 // separator as column sep. This effectively puts the content
3347 // centered, which differs from the normal decimal sep alignment
3348 // and is not ideal, but we cannot do better ATM (see #9568).
3349 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3351 docstring const halffixedwith =
3352 from_ascii(Length(column_info[c].p_width.value() / 2,
3353 column_info[c].p_width.unit()).asLatexString());
3357 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3364 << from_ascii(column_info[c].p_width.asLatexString())
3366 } else if (column_info[c].varwidth) {
3367 switch (column_info[c].alignment) {
3368 case LYX_ALIGN_LEFT:
3369 os << ">{\\raggedright\\arraybackslash}";
3371 case LYX_ALIGN_RIGHT:
3372 os << ">{\\raggedleft\\arraybackslash}";
3374 case LYX_ALIGN_CENTER:
3375 os << ">{\\centering\\arraybackslash}";
3377 case LYX_ALIGN_NONE:
3378 case LYX_ALIGN_BLOCK:
3379 case LYX_ALIGN_LAYOUT:
3380 case LYX_ALIGN_SPECIAL:
3381 case LYX_ALIGN_DECIMAL:
3385 } else if (isVTypeColumn(c)) {
3386 switch (column_info[c].alignment) {
3387 case LYX_ALIGN_LEFT:
3388 os << ">{\\raggedright}";
3390 case LYX_ALIGN_RIGHT:
3391 os << ">{\\raggedleft}";
3393 case LYX_ALIGN_CENTER:
3394 os << ">{\\centering}";
3396 case LYX_ALIGN_NONE:
3397 case LYX_ALIGN_BLOCK:
3398 case LYX_ALIGN_LAYOUT:
3399 case LYX_ALIGN_SPECIAL:
3400 case LYX_ALIGN_DECIMAL:
3403 os << "V{\\linewidth}";
3405 switch (column_info[c].alignment) {
3406 case LYX_ALIGN_LEFT:
3409 case LYX_ALIGN_RIGHT:
3412 case LYX_ALIGN_DECIMAL:
3413 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3419 } // end if else !column_info[i].p_width
3420 } // end if else !column_info[i].align_special
3421 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3426 TeXLongtableHeaderFooter(os, runparams, columns);
3428 //+---------------------------------------------------------------------
3429 //+ the single row and columns (cells) +
3430 //+---------------------------------------------------------------------
3432 for (row_type r = 0; r < nrows(); ++r) {
3433 if (!buffer().params().output_changes && row_info[r].change.deleted())
3435 if (isValidRow(r)) {
3436 TeXRow(os, r, runparams, columns);
3437 if (is_long_tabular && row_info[r].newpage)
3438 os << "\\newpage\n";
3442 //+---------------------------------------------------------------------
3443 //+ the closing of the tabular +
3444 //+---------------------------------------------------------------------
3446 if (is_long_tabular) {
3448 os << "\\end{xltabular}";
3450 os << "\\end{longtable}";
3452 if (is_tabular_star)
3453 os << "\\end{tabular*}";
3454 else if (hasVarwidthColumn())
3455 os << "\\end{tabularx}";
3457 os << "\\end{tabular}";
3460 if (deactivate_chars)
3462 os << "\n\\endgroup\n";
3465 if (is_long_tabular)
3466 os << breakln << "\\end{landscape}";
3468 os << breakln << "\\end{turn}";
3471 if (!TexRow::isNone(pos))
3472 os.texrow().start(pos);
3476 int Tabular::docbookRow(odocstream & os, row_type row,
3477 OutputParams const & runparams) const
3480 idx_type cell = getFirstCellInRow(row);
3483 for (col_type c = 0; c < ncols(); ++c) {
3484 if (isPartOfMultiColumn(row, c))
3487 os << "<entry align=\"";
3488 switch (getAlignment(cell)) {
3489 case LYX_ALIGN_LEFT:
3492 case LYX_ALIGN_RIGHT:
3500 os << "\" valign=\"";
3501 switch (getVAlignment(cell)) {
3502 case LYX_VALIGN_TOP:
3505 case LYX_VALIGN_BOTTOM:
3508 case LYX_VALIGN_MIDDLE:
3513 if (isMultiColumn(cell)) {
3514 os << " namest=\"col" << c << "\" ";
3515 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3519 ret += cellInset(cell)->docbook(os, runparams);
3528 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3532 //+---------------------------------------------------------------------
3533 //+ first the opening preamble +
3534 //+---------------------------------------------------------------------
3536 os << "<tgroup cols=\"" << ncols()
3537 << "\" colsep=\"1\" rowsep=\"1\">\n";
3539 for (col_type c = 0; c < ncols(); ++c) {
3540 os << "<colspec colname=\"col" << c << "\" align=\"";
3541 switch (column_info[c].alignment) {
3542 case LYX_ALIGN_LEFT:
3545 case LYX_ALIGN_RIGHT:
3553 if (runparams.flavor == OutputParams::XML)
3559 //+---------------------------------------------------------------------
3560 //+ Long Tabular case +
3561 //+---------------------------------------------------------------------
3563 // output caption info
3564 // The caption flag wins over head/foot
3565 if (haveLTCaption()) {
3566 os << "<caption>\n";
3568 for (row_type r = 0; r < nrows(); ++r) {
3569 if (row_info[r].caption) {
3570 ret += docbookRow(os, r, runparams);
3573 os << "</caption>\n";
3576 // output header info
3577 if (haveLTHead(false) || haveLTFirstHead(false)) {
3580 for (row_type r = 0; r < nrows(); ++r) {
3581 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3582 !row_info[r].caption) {
3583 ret += docbookRow(os, r, runparams);
3589 // output footer info
3590 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3593 for (row_type r = 0; r < nrows(); ++r) {
3594 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3595 !row_info[r].caption) {
3596 ret += docbookRow(os, r, runparams);
3603 //+---------------------------------------------------------------------
3604 //+ the single row and columns (cells) +
3605 //+---------------------------------------------------------------------
3609 for (row_type r = 0; r < nrows(); ++r) {
3610 if (isValidRow(r)) {
3611 ret += docbookRow(os, r, runparams);
3616 //+---------------------------------------------------------------------
3617 //+ the closing of the tabular +
3618 //+---------------------------------------------------------------------
3627 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3628 OutputParams const & runparams, bool header) const
3631 string const celltag = header ? "th" : "td";
3632 idx_type cell = getFirstCellInRow(row);
3634 xs << html::StartTag("tr");
3635 for (col_type c = 0; c < ncols(); ++c) {
3636 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3641 Length const cwidth = column_info[c].p_width;
3642 if (!cwidth.zero()) {
3643 string const hwidth = cwidth.asHTMLString();
3644 attr << "style =\"width: " << hwidth << ";\" ";
3648 switch (getAlignment(cell)) {
3649 case LYX_ALIGN_LEFT:
3652 case LYX_ALIGN_RIGHT:
3660 attr << " valign='";
3661 switch (getVAlignment(cell)) {
3662 case LYX_VALIGN_TOP:
3665 case LYX_VALIGN_BOTTOM:
3668 case LYX_VALIGN_MIDDLE:
3673 if (isMultiColumn(cell))
3674 attr << " colspan='" << columnSpan(cell) << "'";
3675 else if (isMultiRow(cell))
3676 attr << " rowspan='" << rowSpan(cell) << "'";
3678 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3679 ret += cellInset(cell)->xhtml(xs, runparams);
3680 xs << html::EndTag(celltag) << html::CR();
3683 xs << html::EndTag("tr");
3688 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3692 if (is_long_tabular) {
3693 // we'll wrap it in a div, so as to deal with alignment
3695 switch (longtabular_alignment) {
3696 case LYX_LONGTABULAR_ALIGN_LEFT:
3699 case LYX_LONGTABULAR_ALIGN_CENTER:
3702 case LYX_LONGTABULAR_ALIGN_RIGHT:
3706 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3708 // The caption flag wins over head/foot
3709 if (haveLTCaption()) {
3710 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3712 for (row_type r = 0; r < nrows(); ++r)
3713 if (row_info[r].caption)
3714 ret += xhtmlRow(xs, r, runparams);
3715 xs << html::EndTag("div") << html::CR();
3719 xs << html::StartTag("table") << html::CR();
3721 // output header info
3722 bool const havefirsthead = haveLTFirstHead(false);
3723 // if we have a first head, then we are going to ignore the
3724 // headers for the additional pages, since there aren't any
3725 // in XHTML. this test accomplishes that.
3726 bool const havehead = !havefirsthead && haveLTHead(false);
3727 if (havehead || havefirsthead) {
3728 xs << html::StartTag("thead") << html::CR();
3729 for (row_type r = 0; r < nrows(); ++r) {
3730 if (((havefirsthead && row_info[r].endfirsthead) ||
3731 (havehead && row_info[r].endhead)) &&
3732 !row_info[r].caption) {
3733 ret += xhtmlRow(xs, r, runparams, true);
3736 xs << html::EndTag("thead") << html::CR();
3738 // output footer info
3739 bool const havelastfoot = haveLTLastFoot(false);
3741 bool const havefoot = !havelastfoot && haveLTFoot(false);
3742 if (havefoot || havelastfoot) {
3743 xs << html::StartTag("tfoot") << html::CR();
3744 for (row_type r = 0; r < nrows(); ++r) {
3745 if (((havelastfoot && row_info[r].endlastfoot) ||
3746 (havefoot && row_info[r].endfoot)) &&
3747 !row_info[r].caption) {
3748 ret += xhtmlRow(xs, r, runparams);
3751 xs << html::EndTag("tfoot") << html::CR();
3754 xs << html::StartTag("tbody") << html::CR();
3755 for (row_type r = 0; r < nrows(); ++r) {
3756 if (isValidRow(r)) {
3757 ret += xhtmlRow(xs, r, runparams);
3760 xs << html::EndTag("tbody")
3762 << html::EndTag("table")
3764 if (is_long_tabular)
3765 xs << html::EndTag("div") << html::CR();
3770 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3771 vector<unsigned int> const & clen) const
3773 idx_type const fcell = getFirstCellInRow(row);
3774 idx_type const n = numberOfCellsInRow(row) + fcell;
3777 for (idx_type i = fcell; i < n; ++i) {
3787 for (idx_type i = fcell; i < n; ++i) {
3798 col_type column = cellColumn(i);
3799 int len = clen[column];
3800 while (column < ncols() - 1
3801 && isPartOfMultiColumn(row, ++column))
3802 len += clen[column] + 4;
3803 os << docstring(len, ch);
3818 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3819 vector<unsigned int> const & clen) const
3821 idx_type const fcell = getFirstCellInRow(row);
3822 idx_type const n = numberOfCellsInRow(row) + fcell;
3825 for (idx_type i = fcell; i < n; ++i) {
3826 if (bottomLine(i)) {
3835 for (idx_type i = fcell; i < n; ++i) {
3836 if (bottomLine(i)) {
3846 col_type column = cellColumn(i);
3847 int len = clen[column];
3848 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3849 len += clen[column] + 4;
3850 os << docstring(len, ch);
3851 if (bottomLine(i)) {
3865 void Tabular::plaintextPrintCell(odocstringstream & os,
3866 OutputParams const & runparams,
3867 idx_type cell, row_type row, col_type column,
3868 vector<unsigned int> const & clen,
3869 bool onlydata, size_t max_length) const
3871 odocstringstream sstr;
3872 cellInset(cell)->plaintext(sstr, runparams, max_length);
3884 unsigned int len1 = sstr.str().length();
3885 unsigned int len2 = clen[column];
3886 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3887 len2 += clen[column] + 4;
3890 switch (getAlignment(cell)) {
3892 case LYX_ALIGN_LEFT:
3895 case LYX_ALIGN_RIGHT:
3899 case LYX_ALIGN_CENTER:
3905 os << docstring(len1, ' ') << sstr.str()
3906 << docstring(len2, ' ');
3908 if (rightLine(cell))
3915 void Tabular::plaintext(odocstringstream & os,
3916 OutputParams const & runparams, int const depth,
3917 bool onlydata, char_type delim, size_t max_length) const
3919 // first calculate the width of the single columns
3920 vector<unsigned int> clen(ncols());
3923 // first all non multicolumn cells!
3924 for (col_type c = 0; c < ncols(); ++c) {
3926 for (row_type r = 0; r < nrows(); ++r) {
3927 idx_type cell = cellIndex(r, c);
3928 if (isMultiColumn(cell))
3930 odocstringstream sstr;
3931 cellInset(cell)->plaintext(sstr, runparams, max_length);
3932 if (clen[c] < sstr.str().length())
3933 clen[c] = sstr.str().length();
3936 // then all multicolumn cells!
3937 for (col_type c = 0; c < ncols(); ++c) {
3938 for (row_type r = 0; r < nrows(); ++r) {
3939 idx_type cell = cellIndex(r, c);
3940 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3942 odocstringstream sstr;
3943 cellInset(cell)->plaintext(sstr, runparams, max_length);
3944 int len = int(sstr.str().length());
3945 idx_type const n = columnSpan(cell);
3946 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3948 if (len > int(clen[c + n - 1]))
3949 clen[c + n - 1] = len;
3954 for (row_type r = 0; r < nrows(); ++r) {
3955 if (!onlydata && plaintextTopHLine(os, r, clen))
3956 os << docstring(depth * 2, ' ');
3957 for (col_type c = 0; c < ncols(); ++c) {
3958 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3960 if (onlydata && c > 0)
3961 // we don't use operator<< for single UCS4 character.
3962 // see explanation in docstream.h
3964 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3966 if (os.str().size() > max_length)
3971 os << docstring(depth * 2, ' ');
3972 if (plaintextBottomHLine(os, r, clen))
3973 os << docstring(depth * 2, ' ');
3975 if (os.str().size() > max_length)
3981 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3983 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3987 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3989 return cell_info[row][column].inset;
3993 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3995 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3999 void Tabular::setCellInset(row_type row, col_type column,
4000 shared_ptr<InsetTableCell> ins)
4002 CellData & cd = cell_info[row][column];
4007 void Tabular::validate(LaTeXFeatures & features) const
4009 features.require("NeedTabularnewline");
4011 features.require("booktabs");
4012 if (is_long_tabular && !hasVarwidthColumn()) {
4013 if (tabular_width.zero())
4014 features.require("longtable");
4016 features.require("xltabular");
4018 if (rotate && is_long_tabular)
4019 features.require("lscape");
4021 features.require("rotating");
4022 if (hasVarwidthColumn()) {
4023 if (is_long_tabular)
4024 features.require("xltabular");
4026 features.require("tabularx");
4028 for (idx_type cell = 0; cell < numberofcells; ++cell) {
4029 if (isMultiRow(cell))
4030 features.require("multirow");
4031 if (getUsebox(cell) == BOX_VARWIDTH)
4032 features.require("varwidth");
4033 if (getVAlignment(cell) != LYX_VALIGN_TOP
4034 || !getPWidth(cell).zero()
4035 || isVTypeColumn(cellColumn(cell)))
4036 features.require("array");
4037 // Tell footnote that we need a savenote
4038 // environment in non-long tables or
4039 // longtable headers/footers
4040 else if (!is_long_tabular && !features.inFloat())
4041 features.saveNoteEnv("tabular");
4042 else if (!isValidRow(cellRow(cell)))
4043 features.saveNoteEnv("longtable");
4045 cellInset(cell)->validate(features);
4046 features.saveNoteEnv(string());
4051 Tabular::BoxType Tabular::useBox(idx_type cell) const
4053 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4054 if (parlist.size() > 1)
4055 return BOX_VARWIDTH;
4057 ParagraphList::const_iterator cit = parlist.begin();
4058 ParagraphList::const_iterator end = parlist.end();
4060 for (; cit != end; ++cit)
4061 for (int i = 0; i < cit->size(); ++i)
4062 if (cit->isNewline(i) || cit->layout().isEnvironment())
4063 return BOX_VARWIDTH;
4069 /////////////////////////////////////////////////////////////////////
4073 /////////////////////////////////////////////////////////////////////
4075 InsetTableCell::InsetTableCell(Buffer * buf)
4076 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
4077 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4081 bool InsetTableCell::forcePlainLayout(idx_type) const
4083 return isMultiRow || (isMultiColumn && !isFixedWidth);
4087 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4089 return isFixedWidth;
4093 bool InsetTableCell::forceLocalFontSwitch() const
4099 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4100 FuncStatus & status) const
4102 bool enabled = true;
4103 switch (cmd.action()) {
4104 case LFUN_INSET_DISSOLVE:
4107 case LFUN_MATH_DISPLAY:
4108 if (!hasFixedWidth()) {
4113 return InsetText::getStatus(cur, cmd, status);
4115 status.setEnabled(enabled);
4119 docstring InsetTableCell::asString(bool intoInsets)
4122 if (paragraphs().empty())
4124 ParagraphList::const_iterator it = paragraphs().begin();
4125 ParagraphList::const_iterator en = paragraphs().end();
4127 for (; it != en; ++it) {
4132 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4138 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4139 UpdateType utype, TocBackend & backend) const
4141 InsetText::iterateForToc(di, output_active, utype, backend);
4145 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4148 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4149 return InsetText::xhtml(xs, rp);
4153 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4155 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4157 // Hand font through to contained lyxtext:
4158 tm.font_.fontInfo() = mi.base.font;
4159 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
4161 // This can happen when a layout has a left and right margin,
4162 // and the view is made very narrow. We can't do better than
4163 // to draw it partly out of view (bug 5890).
4164 if (mi.base.textwidth < 1)
4165 mi.base.textwidth = 1;
4167 // We tell metrics here not to expand on multiple pars
4168 // This is the difference to InsetText::Metrics
4169 if (hasFixedWidth())
4170 tm.metrics(mi, dim, mi.base.textwidth, false);
4172 tm.metrics(mi, dim, 0, false);
4173 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
4174 dim.asc += TEXT_TO_INSET_OFFSET;
4175 dim.des += TEXT_TO_INSET_OFFSET;
4176 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
4181 /////////////////////////////////////////////////////////////////////
4185 /////////////////////////////////////////////////////////////////////
4187 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4189 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4190 rowselect_(false), colselect_(false)
4195 InsetTabular::InsetTabular(InsetTabular const & tab)
4196 : Inset(tab), tabular(tab.tabular),
4197 rowselect_(false), colselect_(false)
4202 InsetTabular::~InsetTabular()
4204 hideDialogs("tabular", this);
4208 void InsetTabular::setBuffer(Buffer & buf)
4210 tabular.setBuffer(buf);
4211 Inset::setBuffer(buf);
4215 bool InsetTabular::insetAllowed(InsetCode code) const
4220 case MATHMACRO_CODE:
4225 return tabular.is_long_tabular;
4233 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4235 return tabular.is_long_tabular &&
4236 (newtype == "Standard" || newtype == "Unnumbered");
4240 void InsetTabular::write(ostream & os) const
4242 os << "Tabular" << endl;
4247 string InsetTabular::contextMenu(BufferView const &, int, int) const
4249 // FIXME: depending on the selection state,
4250 // we could offer a different menu.
4251 return cell(0)->contextMenuName() + ";" + contextMenuName();
4255 string InsetTabular::contextMenuName() const
4257 return "context-tabular";
4261 void InsetTabular::read(Lexer & lex)
4263 //bool const old_format = (lex.getString() == "\\LyXTable");
4271 string token = lex.getString();
4272 while (lex && token != "\\end_inset") {
4274 token = lex.getString();
4277 lex.printError("Missing \\end_inset at this point. ");
4281 int InsetTabular::rowFromY(Cursor & cur, int y) const
4283 // top y coordinate of tabular
4284 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4286 for (; r < tabular.nrows() && y > h; ++r)
4287 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4288 + tabular.interRowSpace(r);
4294 int InsetTabular::columnFromX(Cursor & cur, int x) const
4296 // left x coordinate of tabular
4297 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4299 for (; c < tabular.ncols() && x > w; ++c)
4300 w += tabular.cellWidth(c);
4305 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4307 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4308 // mi.base.textwidth << "\n";
4309 LBUFERR(mi.base.bv);
4311 for (row_type r = 0; r < tabular.nrows(); ++r) {
4314 for (col_type c = 0; c < tabular.ncols(); ++c) {
4315 if (tabular.isPartOfMultiColumn(r, c)
4316 || tabular.isPartOfMultiRow(r, c))
4317 // multicolumn or multirow cell, but not first one
4319 idx_type const cell = tabular.cellIndex(r, c);
4322 Length const p_width = tabular.getPWidth(cell);
4323 if (!p_width.zero())
4324 m.base.textwidth = mi.base.inPixels(p_width);
4325 else if (tabular.column_info[c].varwidth)
4326 m.base.textwidth = tabular.column_info[c].width;
4327 tabular.cellInset(cell)->metrics(m, dim0);
4328 if (!p_width.zero() || tabular.column_info[c].varwidth)
4329 dim0.wid = m.base.textwidth;
4330 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4331 + tabular.interColumnSpace(cell);
4333 // FIXME(?): do we need a second metrics call?
4334 TextMetrics const & tm =
4335 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4337 // determine horizontal offset because of decimal align (if necessary)
4338 int decimal_width = 0;
4339 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4340 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4341 tail.setBuffer(tabular.buffer());
4342 // we need to set macrocontext position everywhere
4343 // otherwise we crash with nested insets (e.g. footnotes)
4344 // after decimal point
4345 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4347 dit.push_back(CursorSlice(tail));
4348 tail.setMacrocontextPositionRecursive(dit);
4350 // remove text leading decimal point
4351 docstring const align_d = tabular.column_info[c].decimal_point;
4352 dit = separatorPos(&tail, align_d);
4354 pit_type const psize = tail.paragraphs().front().size();
4356 tail.paragraphs().front().eraseChars(0,
4357 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4359 tail.metrics(m, dim1);
4360 decimal_width = dim1.width();
4363 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4364 tabular.cell_info[r][c].decimal_width = decimal_width;
4366 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4367 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
4368 int const lastpardes = tm.last().second->descent()
4369 + TEXT_TO_INSET_OFFSET;
4371 switch (tabular.getVAlignment(cell)) {
4372 case Tabular::LYX_VALIGN_TOP:
4374 case Tabular::LYX_VALIGN_MIDDLE:
4375 offset = -(dim0.des - lastpardes)/2;
4377 case Tabular::LYX_VALIGN_BOTTOM:
4378 offset = -(dim0.des - lastpardes);
4381 tabular.cell_info[r][c].voffset = offset;
4382 maxasc = max(maxasc, dim0.asc - offset);
4383 maxdes = max(maxdes, dim0.des + offset);
4385 int const top_space = tabular.row_info[r].top_space_default ?
4386 default_line_space :
4387 mi.base.inPixels(tabular.row_info[r].top_space);
4388 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4389 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4390 default_line_space :
4391 mi.base.inPixels(tabular.row_info[r].bottom_space);
4392 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4395 // We need to recalculate the metrics after column width calculation
4396 // with xtabular (possibly multiple times, so the call is recursive).
4397 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4399 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4400 dim.des = tabular.height() - dim.asc;
4401 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4405 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4407 if (&cur.inset() == this && cur.selection()) {
4408 if (cur.selIsMultiCell()) {
4411 getSelection(cur, rs, re, cs, ce);
4413 idx_type const cell = tabular.cellIndex(row, col);
4414 col_type const cspan = tabular.columnSpan(cell);
4415 row_type const rspan = tabular.rowSpan(cell);
4416 if (col + cspan - 1 >= cs && col <= ce
4417 && row + rspan - 1 >= rs && row <= re)
4420 if (col == tabular.cellColumn(cur.idx())
4421 && row == tabular.cellRow(cur.idx())) {
4422 CursorSlice const & beg = cur.selBegin();
4423 CursorSlice const & end = cur.selEnd();
4425 if ((end.lastpos() > 0 || end.lastpit() > 0)
4426 && end.pos() == end.lastpos() && beg.pos() == 0
4427 && end.pit() == end.lastpit() && beg.pit() == 0)
4435 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4437 x += ADD_TO_TABULAR_WIDTH;
4439 BufferView * bv = pi.base.bv;
4440 Cursor & cur = pi.base.bv->cursor();
4442 // FIXME: As the full background is painted in drawBackground(),
4443 // we have no choice but to do a full repaint for the Text cells.
4444 pi.full_repaint = true;
4446 bool const original_selection_state = pi.selected;
4450 int yy = y + tabular.offsetVAlignment();
4451 for (row_type r = 0; r < tabular.nrows(); ++r) {
4453 for (col_type c = 0; c < tabular.ncols(); ++c) {
4454 if (tabular.isPartOfMultiColumn(r, c))
4457 idx = tabular.cellIndex(r, c);
4459 if (tabular.isPartOfMultiRow(r, c)) {
4460 nx += tabular.cellWidth(idx);
4464 pi.selected |= isCellSelected(cur, r, c);
4465 int const cx = nx + tabular.textHOffset(idx);
4466 int const cy = yy + tabular.textVOffset(idx);
4467 // Cache the Inset position.
4468 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4469 cell(idx)->draw(pi, cx, cy);
4470 drawCellLines(pi, nx, yy, r, idx);
4471 nx += tabular.cellWidth(idx);
4472 pi.selected = original_selection_state;
4475 if (r + 1 < tabular.nrows())
4476 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4477 + tabular.interRowSpace(r + 1);
4482 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4484 x += ADD_TO_TABULAR_WIDTH;
4485 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4486 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4487 pi.backgroundColor(this));
4491 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4493 Cursor & cur = pi.base.bv->cursor();
4495 x += ADD_TO_TABULAR_WIDTH;
4497 if (!cur.selection())
4499 if (&cur.inset() != this)
4504 bool const full_cell_selected = isCellSelected(cur,
4505 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4507 if (cur.selIsMultiCell() || full_cell_selected) {
4508 for (row_type r = 0; r < tabular.nrows(); ++r) {
4510 for (col_type c = 0; c < tabular.ncols(); ++c) {
4511 if (tabular.isPartOfMultiColumn(r, c))
4514 idx_type const cell = tabular.cellIndex(r, c);
4516 if (tabular.isPartOfMultiRow(r, c)) {
4517 xx += tabular.cellWidth(cell);
4520 int const w = tabular.cellWidth(cell);
4521 int const h = tabular.cellHeight(cell);
4522 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4523 if (isCellSelected(cur, r, c))
4524 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4527 if (r + 1 < tabular.nrows())
4528 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4529 + tabular.interRowSpace(r + 1);
4533 // FIXME: This code has no effect because InsetTableCell does not handle
4534 // drawSelection other than the trivial implementation in Inset.
4536 // x += cellXPos(cur.idx());
4537 // x += tabular.textHOffset(cur.idx());
4538 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4545 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4546 Color const incol, bool drawline, bool heavy = false)
4548 Color const col = drawline ? incol : Color_tabularonoffline;
4549 if (drawline && lt > 0)
4550 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4551 Painter::line_onoffdash,
4552 Painter::thin_line);
4553 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4554 drawline ? Painter::line_solid : Painter::line_onoffdash,
4555 (heavy ? 2 : 1) * Painter::thin_line);
4556 if (drawline && rt > 0)
4557 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4558 Painter::line_onoffdash,
4559 Painter::thin_line);
4565 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4566 row_type row, idx_type cell) const
4568 y -= tabular.rowAscent(row);
4569 int const w = tabular.cellWidth(cell);
4570 int const h = tabular.cellHeight(cell);
4574 col_type const col = tabular.cellColumn(cell);
4576 // Colour the frame if rows/columns are added or deleted
4577 Color colour = Color_tabularline;
4578 if (tabular.column_info[col].change.changed()
4579 || tabular.row_info[row].change.changed())
4580 colour = InsetTableCell(*tabular.cellInset(cell)).paragraphs().front().lookupChange(0).color();
4583 bool drawline = tabular.topLine(cell)
4584 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4585 bool heavy = tabular.use_booktabs
4586 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4587 && tabular.rowTopLine(row);
4588 if (tabular.topLineTrim(cell).first
4589 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4591 if (tabular.topLineTrim(cell).second
4592 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4594 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4598 drawline = tabular.bottomLine(cell);
4599 row_type const lastrow = tabular.nrows() - 1;
4600 // Consider multi-rows
4602 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4603 && tabular.isPartOfMultiRow(r + 1, col))
4605 heavy = tabular.use_booktabs
4606 && ((row == lastrow && tabular.rowBottomLine(row))
4607 || (r == lastrow && tabular.rowBottomLine(r)));
4608 if (tabular.bottomLineTrim(cell).first)
4610 if (tabular.bottomLineTrim(cell).second)
4612 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4615 drawline = tabular.leftLine(cell)
4616 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4617 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4620 x -= tabular.interColumnSpace(cell);
4621 col_type next_cell_col = col + 1;
4622 while (next_cell_col < tabular.ncols()
4623 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4625 drawline = tabular.rightLine(cell)
4626 || (next_cell_col < tabular.ncols()
4627 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4628 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4632 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4634 //lyxerr << "InsetTabular::edit: " << this << endl;
4638 if (isRightToLeft(cur))
4639 cur.idx() = tabular.getLastCellInRow(0);
4645 if (isRightToLeft(cur))
4646 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4648 cur.idx() = tabular.numberofcells - 1;
4650 cur.pos() = cur.lastpos(); // FIXME crude guess
4652 cur.setCurrentFont();
4653 // FIXME: this accesses the position cache before it is initialized
4654 //cur.bv().fitCursor();
4658 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
4660 // In a longtable, tell captions what the current float is
4661 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4662 string const saveflt = cnts.current_float();
4663 if (tabular.is_long_tabular) {
4664 cnts.current_float("table");
4665 // in longtables, we only step the counter once
4666 cnts.step(from_ascii("table"), utype);
4667 cnts.isLongtable(true);
4670 ParIterator it2 = it;
4672 size_t const end = it2.nargs();
4673 for ( ; it2.idx() < end; it2.top().forwardIdx())
4674 buffer().updateBuffer(it2, utype);
4677 if (tabular.is_long_tabular) {
4678 cnts.current_float(saveflt);
4679 cnts.isLongtable(false);
4684 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4685 UpdateType utype, TocBackend & backend) const
4687 DocIterator dit = cpit;
4689 size_t const end = dit.nargs();
4690 for ( ; dit.idx() < end; dit.top().forwardIdx())
4691 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4695 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4697 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4698 return x < x0 || x > x0 + tabular.width();
4702 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4704 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4705 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4706 // there is no margin added vertically to tabular insets.
4707 // However, it works for now.
4708 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4712 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4714 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4718 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4720 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4721 << "\n cur:" << cur);
4722 CursorSlice sl = cur.top();
4723 Cursor & bvcur = cur.bv().cursor();
4725 FuncCode const act = cmd.action();
4729 case LFUN_MOUSE_PRESS: {
4730 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4732 if (hitSelectRow(cur.bv(), cmd.x())) {
4733 row_type r = rowFromY(cur, cmd.y());
4734 cur.idx() = tabular.getFirstCellInRow(r);
4738 cur.idx() = tabular.getLastCellInRow(r);
4739 cur.pit() = cur.lastpit();
4740 cur.pos() = cur.lastpos();
4741 cur.selection(true);
4747 if (hitSelectColumn(cur.bv(), cmd.y())) {
4748 col_type c = columnFromX(cur, cmd.x());
4749 cur.idx() = tabular.cellIndex(0, c);
4753 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4754 cur.pit() = cur.lastpit();
4755 cur.pos() = cur.lastpos();
4756 cur.selection(true);
4761 // do not reset cursor/selection if we have selected
4762 // some cells (bug 2715).
4763 if (cmd.button() == mouse_button::button3
4764 && &bvcur.selBegin().inset() == this
4765 && bvcur.selIsMultiCell())
4768 // Let InsetTableCell do it
4769 cell(cur.idx())->dispatch(cur, cmd);
4772 case LFUN_MOUSE_MOTION:
4773 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4774 if (cmd.button() == mouse_button::button1) {
4775 // only accept motions to places not deeper nested than the real anchor
4776 if (!bvcur.realAnchor().hasPart(cur)) {
4780 // select (additional) row
4782 row_type r = rowFromY(cur, cmd.y());
4783 cur.idx() = tabular.getLastCellInRow(r);
4784 // we need to reset the cursor's pit and pos now, as the old ones
4785 // may no longer be valid.
4788 bvcur.setCursor(cur);
4789 bvcur.selection(true);
4792 // select (additional) column
4794 col_type c = columnFromX(cur, cmd.x());
4795 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4796 // we need to reset the cursor's pit and pos now, as the old ones
4797 // may no longer be valid.
4800 bvcur.setCursor(cur);
4801 bvcur.selection(true);
4804 // only update if selection changes
4805 if (bvcur.idx() == cur.idx() &&
4806 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4807 cur.noScreenUpdate();
4808 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4809 bvcur.setCursor(cur);
4810 bvcur.selection(true);
4811 // if this is a multicell selection, we just set the cursor to
4812 // the beginning of the cell's text.
4813 if (bvcur.selIsMultiCell()) {
4814 bvcur.pit() = bvcur.lastpit();
4815 bvcur.pos() = bvcur.lastpos();
4820 case LFUN_MOUSE_RELEASE:
4825 case LFUN_CELL_BACKWARD:
4827 cur.selection(false);
4830 case LFUN_CELL_FORWARD:
4832 cur.selection(false);
4835 case LFUN_CHAR_FORWARD_SELECT:
4836 case LFUN_CHAR_FORWARD:
4837 case LFUN_CHAR_BACKWARD_SELECT:
4838 case LFUN_CHAR_BACKWARD:
4839 case LFUN_CHAR_RIGHT_SELECT:
4840 case LFUN_CHAR_RIGHT:
4841 case LFUN_CHAR_LEFT_SELECT:
4842 case LFUN_CHAR_LEFT:
4843 case LFUN_WORD_FORWARD:
4844 case LFUN_WORD_FORWARD_SELECT:
4845 case LFUN_WORD_BACKWARD:
4846 case LFUN_WORD_BACKWARD_SELECT:
4847 case LFUN_WORD_RIGHT:
4848 case LFUN_WORD_RIGHT_SELECT:
4849 case LFUN_WORD_LEFT:
4850 case LFUN_WORD_LEFT_SELECT: {
4851 // determine whether we move to next or previous cell, where to enter
4852 // the new cell from, and which command to "finish" (i.e., exit the
4855 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4856 FuncCode finish_lfun;
4858 if (act == LFUN_CHAR_FORWARD
4859 || act == LFUN_CHAR_FORWARD_SELECT
4860 || act == LFUN_WORD_FORWARD
4861 || act == LFUN_WORD_FORWARD_SELECT) {
4863 finish_lfun = LFUN_FINISHED_FORWARD;
4865 else if (act == LFUN_CHAR_BACKWARD
4866 || act == LFUN_CHAR_BACKWARD_SELECT
4867 || act == LFUN_WORD_BACKWARD
4868 || act == LFUN_WORD_BACKWARD_SELECT) {
4870 finish_lfun = LFUN_FINISHED_BACKWARD;
4872 // LEFT or RIGHT commands --- the interpretation will depend on the
4873 // table's direction.
4875 bool const right = act == LFUN_CHAR_RIGHT
4876 || act == LFUN_CHAR_RIGHT_SELECT
4877 || act == LFUN_WORD_RIGHT
4878 || act == LFUN_WORD_RIGHT_SELECT;
4879 next_cell = isRightToLeft(cur) != right;
4881 if (lyxrc.visual_cursor)
4882 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4884 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4887 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4888 || act == LFUN_CHAR_BACKWARD_SELECT
4889 || act == LFUN_CHAR_RIGHT_SELECT
4890 || act == LFUN_CHAR_LEFT_SELECT
4891 || act == LFUN_WORD_FORWARD_SELECT
4892 || act == LFUN_WORD_RIGHT_SELECT
4893 || act == LFUN_WORD_BACKWARD_SELECT
4894 || act == LFUN_WORD_LEFT_SELECT;
4896 // If we have a multicell selection or we're
4897 // not doing some LFUN_*_SELECT thing anyway...
4898 if (!cur.selIsMultiCell() || !select) {
4899 col_type const c = tabular.cellColumn(cur.idx());
4900 row_type const r = tabular.cellRow(cur.idx());
4901 // Are we trying to select the whole cell and is the whole cell
4902 // not yet selected?
4903 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4904 ((next_cell && cur.pit() == cur.lastpit()
4905 && cur.pos() == cur.lastpos())
4906 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4908 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4910 // ...try to dispatch to the cell's inset.
4911 cell(cur.idx())->dispatch(cur, cmd);
4913 // When we already have a selection we want to select the whole cell
4914 // before going to the next cell.
4915 if (select_whole && !empty_cell){
4916 getText(cur.idx())->selectAll(cur);
4918 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4922 // FIXME: When we support the selection of an empty cell, remove
4923 // the !empty_cell from this condition. For now we jump to the next
4924 // cell if the current cell is empty.
4925 if (cur.result().dispatched() && !empty_cell)
4929 // move to next/prev cell, as appropriate
4930 // note that we will always do this if we're selecting and we have
4931 // a multicell selection
4932 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4933 << " cell from: " << int(entry_from));
4935 moveNextCell(cur, entry_from);
4937 movePrevCell(cur, entry_from);
4938 // if we're exiting the table, call the appropriate FINISHED lfun
4939 if (sl == cur.top()) {
4940 cmd = FuncRequest(finish_lfun);
4945 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4950 case LFUN_DOWN_SELECT:
4952 if (!(cur.selection() && cur.selIsMultiCell()))
4953 cell(cur.idx())->dispatch(cur, cmd);
4955 cur.dispatched(); // override the cell's decision
4956 if (sl == cur.top()) {
4957 // if our Text didn't do anything to the cursor
4958 // then we try to put the cursor into the cell below
4959 // setting also the right targetX.
4960 cur.selHandle(act == LFUN_DOWN_SELECT);
4961 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4962 int const xtarget = cur.targetX();
4963 // WARNING: Once cur.idx() has been reset, the cursor is in
4964 // an inconsistent state until pos() has been set. Be careful
4965 // what you do with it!
4966 cur.idx() = tabular.cellBelow(cur.idx());
4968 TextMetrics const & tm =
4969 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4970 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4971 cur.setCurrentFont();
4974 if (sl == cur.top()) {
4975 // we trick it to go to forward after leaving the
4977 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4980 if (cur.selIsMultiCell()) {
4981 cur.pit() = cur.lastpit();
4982 cur.pos() = cur.lastpos();
4983 cur.setCurrentFont();
4984 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4987 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4990 case LFUN_UP_SELECT:
4992 if (!(cur.selection() && cur.selIsMultiCell()))
4993 cell(cur.idx())->dispatch(cur, cmd);
4994 cur.dispatched(); // override the cell's decision
4995 if (sl == cur.top()) {
4996 // if our Text didn't do anything to the cursor
4997 // then we try to put the cursor into the cell above
4998 // setting also the right targetX.
4999 cur.selHandle(act == LFUN_UP_SELECT);
5000 if (tabular.cellRow(cur.idx()) != 0) {
5001 int const xtarget = cur.targetX();
5002 // WARNING: Once cur.idx() has been reset, the cursor is in
5003 // an inconsistent state until pos() has been set. Be careful
5004 // what you do with it!
5005 cur.idx() = tabular.cellAbove(cur.idx());
5006 cur.pit() = cur.lastpit();
5007 Text const * text = cell(cur.idx())->getText(0);
5008 TextMetrics const & tm = cur.bv().textMetrics(text);
5009 ParagraphMetrics const & pm =
5010 tm.parMetrics(cur.lastpit());
5011 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
5012 cur.setCurrentFont();
5015 if (sl == cur.top()) {
5016 cmd = FuncRequest(LFUN_UP);
5019 if (cur.selIsMultiCell()) {
5021 cur.pos() = cur.lastpos();
5022 cur.setCurrentFont();
5023 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5026 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5029 case LFUN_LAYOUT_TABULAR:
5030 cur.bv().showDialog("tabular");
5033 case LFUN_INSET_MODIFY:
5034 // we come from the dialog
5035 if (cmd.getArg(0) == "tabular")
5036 tabularFeatures(cur, cmd.getLongArg(1));
5041 case LFUN_TABULAR_FEATURE:
5042 tabularFeatures(cur, to_utf8(cmd.argument()));
5045 // insert file functions
5046 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5047 case LFUN_FILE_INSERT_PLAINTEXT:
5049 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5050 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5051 FileName(to_utf8(cmd.argument())));
5054 cur.recordUndoInset();
5055 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5056 // content has been replaced,
5057 // so cursor might be invalid
5058 cur.pos() = cur.lastpos();
5059 cur.pit() = cur.lastpit();
5060 bvcur.setCursor(cur);
5067 if (cur.selIsMultiCell()) {
5068 if (copySelection(cur)) {
5069 cur.recordUndoInset();
5073 cell(cur.idx())->dispatch(cur, cmd);
5076 case LFUN_SELF_INSERT:
5077 if (cur.selIsMultiCell()) {
5078 cur.recordUndoInset();
5080 BufferView * bv = &cur.bv();
5081 docstring::const_iterator cit = cmd.argument().begin();
5082 docstring::const_iterator const end = cmd.argument().end();
5083 for (; cit != end; ++cit)
5084 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5087 bv->bookmarkEditPosition();
5089 cell(cur.idx())->dispatch(cur, cmd);
5092 case LFUN_CHAR_DELETE_BACKWARD:
5093 case LFUN_CHAR_DELETE_FORWARD:
5094 if (cur.selIsMultiCell()) {
5095 cur.recordUndoInset();
5098 cell(cur.idx())->dispatch(cur, cmd);
5102 if (!cur.selection())
5104 if (cur.selIsMultiCell()) {
5108 cell(cur.idx())->dispatch(cur, cmd);
5111 case LFUN_CLIPBOARD_PASTE:
5112 case LFUN_PRIMARY_SELECTION_PASTE: {
5113 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5114 theClipboard().getAsText(Clipboard::PlainTextType) :
5115 theSelection().get();
5118 // pass to InsertPlaintextString, but
5119 // only if we have multi-cell content
5120 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5121 cur.recordUndoInset();
5122 if (insertPlaintextString(cur.bv(), clip, false)) {
5123 // content has been replaced,
5124 // so cursor might be invalid
5125 cur.pos() = cur.lastpos();
5126 cur.pit() = cur.lastpit();
5127 bvcur.setCursor(cur);
5131 // Let the cell handle normal text
5132 cell(cur.idx())->dispatch(cur, cmd);
5137 if (!tabularStackDirty()) {
5138 // Check if we have plain text or HTML with rows/columns.
5139 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5140 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5141 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5142 docstring const clip =
5143 theClipboard().getAsText(Clipboard::AnyTextType);
5144 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5145 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5146 doDispatch(cur, ncmd);
5150 if (!cur.selIsMultiCell())
5151 cell(cur.idx())->dispatch(cur, cmd);
5154 if (theClipboard().isInternal()) {
5155 cur.recordUndoInset();
5156 pasteClipboard(cur);
5160 case LFUN_CHANGE_ACCEPT:
5161 case LFUN_CHANGE_REJECT:
5162 case LFUN_FONT_EMPH:
5163 case LFUN_FONT_BOLD:
5164 case LFUN_FONT_BOLDSYMBOL:
5165 case LFUN_FONT_ROMAN:
5166 case LFUN_FONT_NOUN:
5167 case LFUN_FONT_ITAL:
5168 case LFUN_FONT_FRAK:
5169 case LFUN_FONT_TYPEWRITER:
5170 case LFUN_FONT_SANS:
5171 case LFUN_TEXTSTYLE_APPLY:
5172 case LFUN_TEXTSTYLE_UPDATE:
5173 case LFUN_FONT_SIZE:
5174 case LFUN_FONT_UNDERLINE:
5175 case LFUN_FONT_STRIKEOUT:
5176 case LFUN_FONT_CROSSOUT:
5177 case LFUN_FONT_UNDERUNDERLINE:
5178 case LFUN_FONT_UNDERWAVE:
5180 case LFUN_PARAGRAPH_PARAMS_APPLY:
5181 case LFUN_PARAGRAPH_PARAMS:
5182 case LFUN_WORD_CAPITALIZE:
5183 case LFUN_WORD_UPCASE:
5184 case LFUN_WORD_LOWCASE:
5185 case LFUN_CHARS_TRANSPOSE: {
5186 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5187 if (cur.selIsMultiCell()) {
5190 getSelection(cur, rs, re, cs, ce);
5191 Cursor tmpcur = cur;
5192 for (row_type r = rs; r <= re; ++r) {
5193 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5194 // whole row selected
5195 if (act == LFUN_CHANGE_ACCEPT) {
5196 if (tabular.row_info[r].change.inserted())
5197 tabular.row_info[r].change.setUnchanged();
5198 else if (tabular.row_info[r].change.deleted()) {
5199 tabular.deleteRow(r, true);
5204 if (tabular.row_info[r].change.deleted())
5205 tabular.row_info[r].change.setUnchanged();
5206 else if (tabular.row_info[r].change.inserted()) {
5207 tabular.deleteRow(r, true);
5213 for (col_type c = cs; c <= ce; ++c) {
5214 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5215 // whole col selected
5216 if (act == LFUN_CHANGE_ACCEPT) {
5217 if (tabular.column_info[c].change.inserted())
5218 tabular.column_info[c].change.setUnchanged();
5219 else if (tabular.column_info[c].change.deleted()) {
5220 tabular.deleteColumn(c, true);
5225 if (tabular.column_info[c].change.deleted())
5226 tabular.column_info[c].change.setUnchanged();
5227 else if (tabular.column_info[c].change.inserted()) {
5228 tabular.deleteColumn(c, true);
5234 // cursor follows cell:
5235 tmpcur.idx() = tabular.cellIndex(r, c);
5236 // select this cell only:
5239 tmpcur.resetAnchor();
5240 tmpcur.pit() = tmpcur.lastpit();
5241 tmpcur.pos() = tmpcur.top().lastpos();
5242 tmpcur.setCursor(tmpcur);
5243 tmpcur.setSelection();
5244 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5248 tabular.updateIndexes();
5249 // cursor might be invalid
5251 // change bar might need to be redrawn
5252 cur.screenUpdateFlags(Update::Force);
5253 cur.forceBufferUpdate();
5257 cell(cur.idx())->dispatch(cur, cmd);
5262 case LFUN_CHANGE_NEXT:
5263 case LFUN_CHANGE_PREVIOUS: {
5264 // BufferView::dispatch has already moved the cursor, we just
5265 // need to select here if we have a changed row or column
5266 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5268 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5272 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5273 cur.pit() = cur.lastpit();
5274 cur.pos() = cur.lastpos();
5275 cur.selection(true);
5279 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5281 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5285 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5286 cur.pit() = cur.lastpit();
5287 cur.pos() = cur.lastpos();
5288 cur.selection(true);
5295 case LFUN_INSET_SETTINGS:
5296 // relay this lfun to Inset, not to the cell.
5297 Inset::doDispatch(cur, cmd);
5301 // we try to handle this event in the insets dispatch function.
5302 cell(cur.idx())->dispatch(cur, cmd);
5308 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5309 string const & argument, FuncStatus & status) const
5312 int action = Tabular::LAST_ACTION;
5314 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5315 if (tabularFeature[i].feature == s) {
5316 action = tabularFeature[i].action;
5320 if (action == Tabular::LAST_ACTION) {
5322 status.setUnknown(true);
5326 row_type sel_row_start = 0;
5327 row_type sel_row_end = 0;
5328 col_type sel_col_start = 0;
5329 col_type sel_col_end = 0;
5330 Tabular::ltType dummyltt;
5333 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5336 case Tabular::SET_PWIDTH:
5337 case Tabular::SET_MPWIDTH:
5338 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5339 case Tabular::SET_SPECIAL_COLUMN:
5340 case Tabular::SET_SPECIAL_MULTICOLUMN:
5341 case Tabular::APPEND_ROW:
5342 case Tabular::APPEND_COLUMN:
5343 case Tabular::DELETE_ROW:
5344 case Tabular::DELETE_COLUMN:
5345 case Tabular::COPY_ROW:
5346 case Tabular::COPY_COLUMN:
5347 case Tabular::SET_TOP_SPACE:
5348 case Tabular::SET_BOTTOM_SPACE:
5349 case Tabular::SET_INTERLINE_SPACE:
5353 case Tabular::SET_TABULAR_WIDTH:
5354 status.setEnabled(!tabular.rotate
5355 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5358 case Tabular::MOVE_COLUMN_RIGHT:
5359 case Tabular::MOVE_COLUMN_LEFT:
5360 case Tabular::MOVE_ROW_DOWN:
5361 case Tabular::MOVE_ROW_UP: {
5362 if (cur.selection()) {
5363 status.message(_("Selections not supported."));
5364 status.setEnabled(false);
5368 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
5369 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
5370 (action == Tabular::MOVE_COLUMN_LEFT &&
5371 tabular.cellColumn(cur.idx()) == 0) ||
5372 (action == Tabular::MOVE_ROW_DOWN &&
5373 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
5374 (action == Tabular::MOVE_ROW_UP &&
5375 tabular.cellRow(cur.idx()) == 0)) {
5376 status.setEnabled(false);
5380 if (action == Tabular::MOVE_COLUMN_RIGHT ||
5381 action == Tabular::MOVE_COLUMN_LEFT) {
5382 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
5383 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
5384 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
5385 status.message(_("Multi-column in current or"
5386 " destination column."));
5387 status.setEnabled(false);
5392 if (action == Tabular::MOVE_ROW_DOWN ||
5393 action == Tabular::MOVE_ROW_UP) {
5394 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
5395 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
5396 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
5397 status.message(_("Multi-row in current or"
5398 " destination row."));
5399 status.setEnabled(false);
5404 status.setEnabled(true);
5408 case Tabular::SET_DECIMAL_POINT:
5410 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5413 case Tabular::SET_MULTICOLUMN:
5414 case Tabular::UNSET_MULTICOLUMN:
5415 case Tabular::MULTICOLUMN:
5416 // If a row is set as longtable caption, it must not be allowed
5417 // to unset that this row is a multicolumn.
5418 status.setEnabled(sel_row_start == sel_row_end
5419 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5420 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5423 case Tabular::SET_MULTIROW:
5424 case Tabular::UNSET_MULTIROW:
5425 case Tabular::MULTIROW:
5426 // If a row is set as longtable caption, it must not be allowed
5427 // to unset that this row is a multirow.
5428 status.setEnabled(sel_col_start == sel_col_end
5429 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5430 status.setOnOff(tabular.isMultiRow(cur.idx()));
5433 case Tabular::SET_ALL_LINES:
5434 case Tabular::UNSET_ALL_LINES:
5435 case Tabular::SET_INNER_LINES:
5436 case Tabular::SET_BORDER_LINES:
5437 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5440 case Tabular::RESET_FORMAL_DEFAULT:
5441 status.setEnabled(tabular.use_booktabs);
5444 case Tabular::SET_LINE_TOP:
5445 case Tabular::SET_LINE_BOTTOM:
5446 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5449 case Tabular::SET_LINE_LEFT:
5450 case Tabular::SET_LINE_RIGHT:
5451 status.setEnabled(!tabular.use_booktabs
5452 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5455 case Tabular::SET_LTRIM_TOP:
5456 case Tabular::SET_RTRIM_TOP:
5457 status.setEnabled(tabular.use_booktabs
5458 && tabular.cellRow(cur.idx()) != 0
5459 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5462 case Tabular::SET_LTRIM_BOTTOM:
5463 case Tabular::SET_RTRIM_BOTTOM:
5464 status.setEnabled(tabular.use_booktabs
5465 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5466 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5469 case Tabular::TOGGLE_LINE_TOP:
5470 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5471 status.setOnOff(tabular.topLine(cur.idx()));
5474 case Tabular::TOGGLE_LINE_BOTTOM:
5475 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5476 status.setOnOff(tabular.bottomLine(cur.idx()));
5479 case Tabular::TOGGLE_LINE_LEFT:
5480 status.setEnabled(!tabular.use_booktabs
5481 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5482 status.setOnOff(tabular.leftLine(cur.idx()));
5485 case Tabular::TOGGLE_LINE_RIGHT:
5486 status.setEnabled(!tabular.use_booktabs
5487 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5488 status.setOnOff(tabular.rightLine(cur.idx()));
5491 case Tabular::TOGGLE_LTRIM_TOP:
5492 status.setEnabled(tabular.use_booktabs
5493 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5494 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5497 case Tabular::TOGGLE_RTRIM_TOP:
5498 status.setEnabled(tabular.use_booktabs
5499 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5500 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5503 case Tabular::TOGGLE_LTRIM_BOTTOM:
5504 status.setEnabled(tabular.use_booktabs
5505 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5506 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5509 case Tabular::TOGGLE_RTRIM_BOTTOM:
5510 status.setEnabled(tabular.use_booktabs
5511 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5512 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5515 // multirow cells only inherit the alignment of the column if the column has
5516 // no width, otherwise they are left-aligned
5517 // therefore allow always left but right and center only if there is no width
5518 case Tabular::M_ALIGN_LEFT:
5521 case Tabular::ALIGN_LEFT:
5522 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5525 case Tabular::M_ALIGN_RIGHT:
5528 case Tabular::ALIGN_RIGHT:
5529 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5530 && !tabular.getPWidth(cur.idx()).zero()));
5531 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5534 case Tabular::M_ALIGN_CENTER:
5537 case Tabular::ALIGN_CENTER:
5538 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5539 && !tabular.getPWidth(cur.idx()).zero()));
5540 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5543 case Tabular::ALIGN_BLOCK:
5544 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5545 && !tabular.isMultiRow(cur.idx()));
5546 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5549 case Tabular::ALIGN_DECIMAL:
5550 status.setEnabled(!tabular.isMultiRow(cur.idx())
5551 && !tabular.isMultiColumn(cur.idx()));
5552 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5555 case Tabular::M_VALIGN_TOP:
5558 case Tabular::VALIGN_TOP:
5559 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5560 && !tabular.isMultiRow(cur.idx()));
5562 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5565 case Tabular::M_VALIGN_BOTTOM:
5568 case Tabular::VALIGN_BOTTOM:
5569 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5570 && !tabular.isMultiRow(cur.idx()));
5572 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5575 case Tabular::M_VALIGN_MIDDLE:
5578 case Tabular::VALIGN_MIDDLE:
5579 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5580 && !tabular.isMultiRow(cur.idx()));
5582 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5585 case Tabular::SET_LONGTABULAR:
5586 case Tabular::TOGGLE_LONGTABULAR:
5587 // setting as longtable is not allowed when table is inside a float
5588 if (cur.innerInsetOfType(FLOAT_CODE) != 0
5589 || cur.innerInsetOfType(WRAP_CODE) != 0)
5590 status.setEnabled(false);
5592 status.setEnabled(true);
5593 status.setOnOff(tabular.is_long_tabular);
5596 case Tabular::UNSET_LONGTABULAR:
5597 status.setOnOff(!tabular.is_long_tabular);
5600 case Tabular::TOGGLE_ROTATE_TABULAR:
5601 case Tabular::SET_ROTATE_TABULAR:
5602 status.setOnOff(tabular.rotate != 0);
5605 case Tabular::TABULAR_VALIGN_TOP:
5606 status.setEnabled(tabular.tabular_width.zero());
5607 status.setOnOff(tabular.tabular_valignment
5608 == Tabular::LYX_VALIGN_TOP);
5610 case Tabular::TABULAR_VALIGN_MIDDLE:
5611 status.setEnabled(tabular.tabular_width.zero());
5612 status.setOnOff(tabular.tabular_valignment
5613 == Tabular::LYX_VALIGN_MIDDLE);
5615 case Tabular::TABULAR_VALIGN_BOTTOM:
5616 status.setEnabled(tabular.tabular_width.zero());
5617 status.setOnOff(tabular.tabular_valignment
5618 == Tabular::LYX_VALIGN_BOTTOM);
5621 case Tabular::LONGTABULAR_ALIGN_LEFT:
5622 status.setOnOff(tabular.longtabular_alignment
5623 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5625 case Tabular::LONGTABULAR_ALIGN_CENTER:
5626 status.setOnOff(tabular.longtabular_alignment
5627 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5629 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5630 status.setOnOff(tabular.longtabular_alignment
5631 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5634 case Tabular::UNSET_ROTATE_TABULAR:
5635 status.setOnOff(tabular.rotate == 0);
5638 case Tabular::TOGGLE_ROTATE_CELL:
5639 case Tabular::SET_ROTATE_CELL:
5640 status.setOnOff(!oneCellHasRotationState(false,
5641 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5644 case Tabular::UNSET_ROTATE_CELL:
5645 status.setOnOff(!oneCellHasRotationState(true,
5646 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5649 case Tabular::SET_USEBOX:
5650 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5653 // every row can only be one thing:
5654 // either a footer or header
5655 case Tabular::SET_LTFIRSTHEAD:
5656 status.setEnabled(sel_row_start == sel_row_end);
5657 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5660 case Tabular::UNSET_LTFIRSTHEAD:
5661 status.setEnabled(sel_row_start == sel_row_end);
5662 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5665 case Tabular::SET_LTHEAD:
5666 status.setEnabled(sel_row_start == sel_row_end);
5667 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5670 case Tabular::UNSET_LTHEAD:
5671 status.setEnabled(sel_row_start == sel_row_end);
5672 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5675 case Tabular::SET_LTFOOT:
5676 status.setEnabled(sel_row_start == sel_row_end);
5677 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5680 case Tabular::UNSET_LTFOOT:
5681 status.setEnabled(sel_row_start == sel_row_end);
5682 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5685 case Tabular::SET_LTLASTFOOT:
5686 status.setEnabled(sel_row_start == sel_row_end);
5687 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5690 case Tabular::UNSET_LTLASTFOOT:
5691 status.setEnabled(sel_row_start == sel_row_end);
5692 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5695 case Tabular::SET_LTNEWPAGE:
5696 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5698 case Tabular::UNSET_LTNEWPAGE:
5699 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5702 // only one row in head/firsthead/foot/lasthead can be the caption
5703 // and a multirow cannot be set as caption
5704 case Tabular::SET_LTCAPTION:
5705 status.setEnabled(sel_row_start == sel_row_end
5706 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5707 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5708 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5709 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5710 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5711 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5712 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5713 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5714 && !tabular.isMultiRow(sel_row_start));
5715 status.setOnOff(tabular.ltCaption(sel_row_start));
5718 case Tabular::UNSET_LTCAPTION:
5719 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5722 case Tabular::TOGGLE_LTCAPTION:
5723 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5724 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5725 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5726 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5727 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5728 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5729 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5730 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5731 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5732 status.setOnOff(tabular.ltCaption(sel_row_start));
5735 case Tabular::TOGGLE_BOOKTABS:
5736 case Tabular::SET_BOOKTABS:
5737 status.setOnOff(tabular.use_booktabs);
5740 case Tabular::UNSET_BOOKTABS:
5741 status.setOnOff(!tabular.use_booktabs);
5746 status.setEnabled(false);
5753 // function sets an object as defined in FuncStatus.h:
5754 // states OK, Unknown, Disabled, On, Off.
5755 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5756 FuncStatus & status) const
5758 switch (cmd.action()) {
5759 case LFUN_INSET_MODIFY:
5760 if (cmd.getArg(0) != "tabular")
5762 if (cmd.getArg(1) == "for-dialog") {
5763 // The dialog is asking the status of a command
5764 if (&cur.inset() != this)
5766 string action = cmd.getArg(2);
5767 string arg = cmd.getLongArg(3);
5768 return getFeatureStatus(cur, action, arg, status);
5770 // We always enable the lfun if it is coming from the dialog
5771 // because the dialog makes sure all the settings are valid,
5772 // even though the first argument might not be valid now.
5773 status.setEnabled(true);
5777 case LFUN_TABULAR_FEATURE: {
5778 if (&cur.inset() != this)
5780 string action = cmd.getArg(0);
5781 string arg = cmd.getLongArg(1);
5782 return getFeatureStatus(cur, action, arg, status);
5785 case LFUN_CAPTION_INSERT: {
5786 // caption is only allowed in caption cell of longtable
5787 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5788 status.setEnabled(false);
5791 // only standard caption is allowed
5792 string arg = cmd.getArg(0);
5793 if (!arg.empty() && arg != "Standard") {
5794 status.setEnabled(false);
5797 // check if there is already a caption
5798 bool have_caption = false;
5799 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5800 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5801 ParagraphList::const_iterator pend = itc.paragraphs().end();
5802 for (; pit != pend; ++pit) {
5803 InsetList::const_iterator it = pit->insetList().begin();
5804 InsetList::const_iterator end = pit->insetList().end();
5805 for (; it != end; ++it) {
5806 if (it->inset->lyxCode() == CAPTION_CODE) {
5807 have_caption = true;
5812 status.setEnabled(!have_caption);
5816 // These are only enabled inside tabular
5817 case LFUN_CELL_BACKWARD:
5818 case LFUN_CELL_FORWARD:
5819 status.setEnabled(true);
5822 // disable these with multiple cells selected
5823 case LFUN_INSET_INSERT:
5824 case LFUN_TABULAR_INSERT:
5825 case LFUN_TABULAR_STYLE_INSERT:
5826 case LFUN_FLEX_INSERT:
5827 case LFUN_FLOAT_INSERT:
5828 case LFUN_FLOAT_WIDE_INSERT:
5829 case LFUN_FOOTNOTE_INSERT:
5830 case LFUN_MARGINALNOTE_INSERT:
5831 case LFUN_MATH_INSERT:
5832 case LFUN_MATH_MODE:
5833 case LFUN_MATH_MUTATE:
5834 case LFUN_MATH_DISPLAY:
5835 case LFUN_NOTE_INSERT:
5836 case LFUN_ARGUMENT_INSERT:
5837 case LFUN_BOX_INSERT:
5838 case LFUN_BRANCH_INSERT:
5839 case LFUN_PHANTOM_INSERT:
5840 case LFUN_WRAP_INSERT:
5841 case LFUN_PREVIEW_INSERT:
5842 case LFUN_ERT_INSERT: {
5843 if (cur.selIsMultiCell()) {
5844 status.setEnabled(false);
5847 return cell(cur.idx())->getStatus(cur, cmd, status);
5850 case LFUN_CHANGE_ACCEPT:
5851 case LFUN_CHANGE_REJECT: {
5852 if (cur.selIsMultiCell()) {
5855 getSelection(cur, rs, re, cs, ce);
5856 for (row_type r = rs; r <= re; ++r) {
5857 if (tabular.row_info[r].change.changed()) {
5858 status.setEnabled(true);
5861 for (col_type c = cs; c <= ce; ++c) {
5862 if (tabular.column_info[c].change.changed()) {
5863 status.setEnabled(true);
5869 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5870 status.setEnabled(true);
5873 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5874 status.setEnabled(true);
5878 return cell(cur.idx())->getStatus(cur, cmd, status);
5881 // disable in non-fixed-width cells
5882 case LFUN_PARAGRAPH_BREAK:
5883 // multirow does not allow paragraph breaks
5884 if (tabular.isMultiRow(cur.idx())) {
5885 status.setEnabled(false);
5889 case LFUN_NEWLINE_INSERT:
5890 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
5891 && tabular.getPWidth(cur.idx()).zero()) {
5892 status.setEnabled(false);
5895 return cell(cur.idx())->getStatus(cur, cmd, status);
5897 case LFUN_NEWPAGE_INSERT:
5898 status.setEnabled(false);
5902 if (tabularStackDirty() && theClipboard().isInternal()) {
5903 if (cur.selIsMultiCell()) {
5906 getSelection(cur, rs, re, cs, ce);
5907 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5908 && paste_tabular->nrows() == re - rs + 1)
5909 status.setEnabled(true);
5911 status.setEnabled(false);
5912 status.message(_("Selection size should match clipboard content."));
5915 status.setEnabled(true);
5918 return cell(cur.idx())->getStatus(cur, cmd, status);
5920 case LFUN_INSET_SETTINGS:
5921 // relay this lfun to Inset, not to the cell.
5922 return Inset::getStatus(cur, cmd, status);
5925 // we try to handle this event in the insets dispatch function.
5926 return cell(cur.idx())->getStatus(cur, cmd, status);
5932 Inset::DisplayType InsetTabular::display() const
5934 if (tabular.is_long_tabular) {
5935 switch (tabular.longtabular_alignment) {
5936 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5938 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5940 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5950 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5952 tabular.latex(os, runparams);
5956 int InsetTabular::plaintext(odocstringstream & os,
5957 OutputParams const & runparams, size_t max_length) const
5959 os << '\n'; // output table on a new line
5960 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5961 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5962 return PLAINTEXT_NEWLINE;
5966 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5971 // FIXME: Why not pass a proper DocIterator here?
5973 // if the table is inside a float it doesn't need the informaltable
5974 // wrapper. Search for it.
5975 for (master = owner(); master; master = master->owner())
5976 if (master->lyxCode() == FLOAT_CODE)
5981 os << "<informaltable>";
5984 ret += tabular.docbook(os, runparams);
5986 os << "</informaltable>";
5993 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5995 return tabular.xhtml(xs, rp);
5999 void InsetTabular::validate(LaTeXFeatures & features) const
6001 tabular.validate(features);
6002 features.useInsetLayout(getLayout());
6006 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
6008 return tabular.cellInset(idx);
6012 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
6014 return tabular.cellInset(idx);
6018 void InsetTabular::cursorPos(BufferView const & bv,
6019 CursorSlice const & sl, bool boundary, int & x, int & y) const
6021 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
6023 // y offset correction
6024 y += cellYPos(sl.idx());
6025 y += tabular.textVOffset(sl.idx());
6026 y += tabular.offsetVAlignment();
6028 // x offset correction
6029 x += cellXPos(sl.idx());
6030 x += tabular.textHOffset(sl.idx());
6031 x += ADD_TO_TABULAR_WIDTH;
6035 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6039 Inset const & inset = *tabular.cellInset(cell);
6040 Point o = bv.coordCache().getInsets().xy(&inset);
6041 int const xbeg = o.x_ - tabular.textHOffset(cell);
6042 int const xend = xbeg + tabular.cellWidth(cell);
6043 row_type const row = tabular.cellRow(cell);
6044 int const ybeg = o.y_ - tabular.rowAscent(row)
6045 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6046 int const yend = ybeg + tabular.cellHeight(cell);
6058 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6059 // << " ybeg=" << ybeg << " yend=" << yend
6060 // << " xx=" << xx << " yy=" << yy
6061 // << " dist=" << xx + yy << endl;
6066 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6068 //lyxerr << "InsetTabular::editXY: " << this << endl;
6070 cur.idx() = getNearestCell(cur.bv(), x, y);
6071 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6075 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6077 cur.idx() = getNearestCell(cur.bv(), x, y);
6078 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6082 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6084 idx_type idx_min = 0;
6085 int dist_min = numeric_limits<int>::max();
6086 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6087 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6088 int const d = dist(bv, i, x, y);
6099 int InsetTabular::cellYPos(idx_type const cell) const
6101 row_type row = tabular.cellRow(cell);
6103 for (row_type r = 0; r < row; ++r)
6104 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6105 + tabular.interRowSpace(r + 1);
6110 int InsetTabular::cellXPos(idx_type const cell) const
6112 col_type col = tabular.cellColumn(cell);
6114 for (col_type c = 0; c < col; ++c)
6115 lx += tabular.column_info[c].width;
6120 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6122 row_type const row = tabular.cellRow(cur.idx());
6123 col_type const col = tabular.cellColumn(cur.idx());
6125 if (isRightToLeft(cur)) {
6126 if (tabular.cellColumn(cur.idx()) == 0) {
6127 if (row == tabular.nrows() - 1)
6129 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6134 cur.idx() = tabular.getLastCellInRow(row - 1);
6136 cur.idx() = tabular.cellIndex(row, col - 1);
6139 if (tabular.isLastCell(cur.idx()))
6141 if (cur.idx() == tabular.getLastCellInRow(row))
6142 cur.idx() = tabular.cellIndex(row + 1, 0);
6144 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6145 cur.idx() = tabular.cellIndex(row, colnextcell);
6149 cur.boundary(false);
6151 if (cur.selIsMultiCell()) {
6152 cur.pit() = cur.lastpit();
6153 cur.pos() = cur.lastpos();
6160 // in visual mode, place cursor at extreme left or right
6162 switch(entry_from) {
6164 case ENTRY_DIRECTION_RIGHT:
6165 cur.posVisToRowExtremity(false /* !left */);
6167 case ENTRY_DIRECTION_LEFT:
6168 cur.posVisToRowExtremity(true /* left */);
6170 case ENTRY_DIRECTION_IGNORE:
6171 // nothing to do in this case
6175 cur.setCurrentFont();
6179 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6181 row_type const row = tabular.cellRow(cur.idx());
6182 col_type const col = tabular.cellColumn(cur.idx());
6184 if (isRightToLeft(cur)) {
6185 if (cur.idx() == tabular.getLastCellInRow(row)) {
6188 cur.idx() = tabular.getFirstCellInRow(row);
6189 cur.idx() = tabular.cellAbove(cur.idx());
6191 if (tabular.isLastCell(cur.idx()))
6193 if (cur.idx() == tabular.getLastCellInRow(row))
6194 cur.idx() = tabular.cellIndex(row + 1, 0);
6196 cur.idx() = tabular.cellIndex(row, col + 1);
6199 if (cur.idx() == 0) // first cell
6202 cur.idx() = tabular.getLastCellInRow(row - 1);
6204 cur.idx() = tabular.cellIndex(row, col - 1);
6207 if (cur.selIsMultiCell()) {
6208 cur.pit() = cur.lastpit();
6209 cur.pos() = cur.lastpos();
6213 cur.pit() = cur.lastpit();
6214 cur.pos() = cur.lastpos();
6216 // in visual mode, place cursor at extreme left or right
6218 switch(entry_from) {
6220 case ENTRY_DIRECTION_RIGHT:
6221 cur.posVisToRowExtremity(false /* !left */);
6223 case ENTRY_DIRECTION_LEFT:
6224 cur.posVisToRowExtremity(true /* left */);
6226 case ENTRY_DIRECTION_IGNORE:
6227 // nothing to do in this case
6231 cur.setCurrentFont();
6235 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6237 cur.recordUndoInset(this);
6239 istringstream is(argument);
6240 // limit the size of strings we read to avoid memory problems
6244 size_t safe_guard = 0;
6249 if (safe_guard > 1000) {
6250 LYXERR0("parameter max count reached!");
6254 Tabular::Feature action = Tabular::LAST_ACTION;
6257 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6258 if (s != tabularFeature[i].feature)
6260 action = tabularFeature[i].action;
6263 if (action == Tabular::LAST_ACTION) {
6264 LYXERR0("Feature not found " << s);
6268 if (tabularFeature[i].need_value)
6270 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6271 tabularFeatures(cur, action, val);
6276 static void checkLongtableSpecial(Tabular::ltType & ltt,
6277 string const & special, bool & flag)
6279 if (special == "dl_above") {
6282 } else if (special == "dl_below") {
6283 ltt.bottomDL = flag;
6285 } else if (special == "empty") {
6295 bool InsetTabular::oneCellHasRotationState(bool rotated,
6296 row_type row_start, row_type row_end,
6297 col_type col_start, col_type col_end) const
6299 for (row_type r = row_start; r <= row_end; ++r)
6300 for (col_type c = col_start; c <= col_end; ++c)
6302 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6305 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6312 void InsetTabular::tabularFeatures(Cursor & cur,
6313 Tabular::Feature feature, string const & value)
6315 col_type sel_col_start;
6316 col_type sel_col_end;
6317 row_type sel_row_start;
6318 row_type sel_row_end;
6319 bool setLines = false;
6320 bool setLinesInnerOnly = false;
6321 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6322 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6326 case Tabular::M_ALIGN_LEFT:
6327 case Tabular::ALIGN_LEFT:
6328 setAlign = LYX_ALIGN_LEFT;
6331 case Tabular::M_ALIGN_RIGHT:
6332 case Tabular::ALIGN_RIGHT:
6333 setAlign = LYX_ALIGN_RIGHT;
6336 case Tabular::M_ALIGN_CENTER:
6337 case Tabular::ALIGN_CENTER:
6338 setAlign = LYX_ALIGN_CENTER;
6341 case Tabular::ALIGN_BLOCK:
6342 setAlign = LYX_ALIGN_BLOCK;
6345 case Tabular::ALIGN_DECIMAL:
6346 setAlign = LYX_ALIGN_DECIMAL;
6349 case Tabular::M_VALIGN_TOP:
6350 case Tabular::VALIGN_TOP:
6351 setVAlign = Tabular::LYX_VALIGN_TOP;
6354 case Tabular::M_VALIGN_BOTTOM:
6355 case Tabular::VALIGN_BOTTOM:
6356 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6359 case Tabular::M_VALIGN_MIDDLE:
6360 case Tabular::VALIGN_MIDDLE:
6361 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6368 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6369 row_type const row = tabular.cellRow(cur.idx());
6370 col_type const column = tabular.cellColumn(cur.idx());
6372 Tabular::ltType ltt;
6376 case Tabular::SET_TABULAR_WIDTH:
6377 tabular.setTabularWidth(Length(value));
6380 case Tabular::SET_PWIDTH: {
6381 Length const len(value);
6382 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6383 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6385 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6386 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6391 case Tabular::SET_MPWIDTH:
6392 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6395 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6396 bool const varwidth = value == "on";
6397 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6398 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6402 case Tabular::SET_MROFFSET:
6403 tabular.setMROffset(cur, cur.idx(), Length(value));
6406 case Tabular::SET_SPECIAL_COLUMN:
6407 case Tabular::SET_SPECIAL_MULTICOLUMN:
6408 if (value == "none")
6409 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6411 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6414 case Tabular::APPEND_ROW:
6415 // append the row into the tabular
6416 tabular.appendRow(row);
6419 case Tabular::APPEND_COLUMN:
6420 // append the column into the tabular
6421 tabular.appendColumn(column);
6422 cur.idx() = tabular.cellIndex(row, column);
6425 case Tabular::DELETE_ROW:
6426 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6427 for (col_type c = 0; c < tabular.ncols(); c++) {
6428 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6429 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6430 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6431 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6435 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6436 tabular.deleteRow(sel_row_start);
6437 if (sel_row_start >= tabular.nrows())
6439 cur.idx() = tabular.cellIndex(sel_row_start, column);
6442 cur.selection(false);
6445 case Tabular::DELETE_COLUMN:
6446 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6447 for (row_type r = 0; r < tabular.nrows(); r++)
6448 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6449 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6452 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6453 for (row_type r = 0; r < tabular.nrows(); r++)
6454 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6455 tabular.leftLine(tabular.cellIndex(r, 0)));
6458 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6459 tabular.deleteColumn(sel_col_start);
6460 if (sel_col_start >= tabular.ncols())
6462 cur.idx() = tabular.cellIndex(row, sel_col_start);
6465 cur.selection(false);
6468 case Tabular::COPY_ROW:
6469 tabular.copyRow(row);
6472 case Tabular::COPY_COLUMN:
6473 tabular.copyColumn(column);
6474 cur.idx() = tabular.cellIndex(row, column);
6477 case Tabular::MOVE_COLUMN_RIGHT:
6478 tabular.moveColumn(column, Tabular::RIGHT);
6479 cur.idx() = tabular.cellIndex(row, column + 1);
6482 case Tabular::MOVE_COLUMN_LEFT:
6483 tabular.moveColumn(column, Tabular::LEFT);
6484 cur.idx() = tabular.cellIndex(row, column - 1);
6487 case Tabular::MOVE_ROW_DOWN:
6488 tabular.moveRow(row, Tabular::DOWN);
6489 cur.idx() = tabular.cellIndex(row + 1, column);
6492 case Tabular::MOVE_ROW_UP:
6493 tabular.moveRow(row, Tabular::UP);
6494 cur.idx() = tabular.cellIndex(row - 1, column);
6497 case Tabular::SET_LINE_TOP:
6498 case Tabular::TOGGLE_LINE_TOP: {
6499 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6500 ? (value == "true") : !tabular.topLine(cur.idx());
6501 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6502 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6503 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6507 case Tabular::SET_LINE_BOTTOM:
6508 case Tabular::TOGGLE_LINE_BOTTOM: {
6509 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6510 ? (value == "true") : !tabular.bottomLine(cur.idx());
6511 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6512 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6513 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6517 case Tabular::SET_LTRIM_TOP:
6518 case Tabular::TOGGLE_LTRIM_TOP: {
6519 bool l = (feature == Tabular::SET_LTRIM_TOP)
6520 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6521 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6522 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6523 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6527 case Tabular::SET_RTRIM_TOP:
6528 case Tabular::TOGGLE_RTRIM_TOP: {
6529 bool l = (feature == Tabular::SET_RTRIM_TOP)
6530 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6531 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6532 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6533 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6537 case Tabular::SET_LTRIM_BOTTOM:
6538 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6539 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6540 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6541 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6542 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6543 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6547 case Tabular::SET_RTRIM_BOTTOM:
6548 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6549 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6550 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6551 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6552 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6553 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6557 case Tabular::SET_LINE_LEFT:
6558 case Tabular::TOGGLE_LINE_LEFT: {
6559 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6560 ? (value == "true") : !tabular.leftLine(cur.idx());
6561 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6562 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6563 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6567 case Tabular::SET_LINE_RIGHT:
6568 case Tabular::TOGGLE_LINE_RIGHT: {
6569 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6570 ? (value == "true") : !tabular.rightLine(cur.idx());
6571 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6572 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6573 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6577 case Tabular::M_ALIGN_LEFT:
6578 case Tabular::M_ALIGN_RIGHT:
6579 case Tabular::M_ALIGN_CENTER:
6580 case Tabular::ALIGN_LEFT:
6581 case Tabular::ALIGN_RIGHT:
6582 case Tabular::ALIGN_CENTER:
6583 case Tabular::ALIGN_BLOCK:
6584 case Tabular::ALIGN_DECIMAL:
6585 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6586 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6587 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6588 !tabular.getPWidth(c).zero());
6591 case Tabular::M_VALIGN_TOP:
6592 case Tabular::M_VALIGN_BOTTOM:
6593 case Tabular::M_VALIGN_MIDDLE:
6596 case Tabular::VALIGN_TOP:
6597 case Tabular::VALIGN_BOTTOM:
6598 case Tabular::VALIGN_MIDDLE:
6599 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6600 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6601 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6604 case Tabular::SET_MULTICOLUMN: {
6605 if (!cur.selection()) {
6606 // just multicol for one single cell
6607 // check whether we are completely in a multicol
6608 if (!tabular.isMultiColumn(cur.idx()))
6609 tabular.setMultiColumn(cur, cur.idx(), 1,
6610 tabular.rightLine(cur.idx()));
6613 // we have a selection so this means we just add all these
6614 // cells to form a multicolumn cell
6615 idx_type const s_start = cur.selBegin().idx();
6616 row_type const col_start = tabular.cellColumn(s_start);
6617 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6618 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6619 tabular.rightLine(cur.selEnd().idx()));
6622 cur.selection(false);
6626 case Tabular::UNSET_MULTICOLUMN: {
6627 if (!cur.selection()) {
6628 if (tabular.isMultiColumn(cur.idx()))
6629 tabular.unsetMultiColumn(cur.idx());
6634 case Tabular::MULTICOLUMN: {
6635 if (!cur.selection()) {
6636 if (tabular.isMultiColumn(cur.idx()))
6637 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6639 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6643 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6644 row_type const r = sel_row_start;
6645 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6646 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6649 // If the selection contains at least one singlecol cell
6650 // or multiple multicol cells,
6651 // we assume the user will merge is to a single multicol
6653 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6655 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6659 case Tabular::SET_MULTIROW: {
6660 if (!cur.selection()) {
6661 // just multirow for one single cell
6662 // check whether we are completely in a multirow
6663 if (!tabular.isMultiRow(cur.idx()))
6664 tabular.setMultiRow(cur, cur.idx(), 1,
6665 tabular.bottomLine(cur.idx()),
6666 tabular.getAlignment(cur.idx()));
6669 // we have a selection so this means we just add all this
6670 // cells to form a multirow cell
6671 idx_type const s_start = cur.selBegin().idx();
6672 row_type const row_start = tabular.cellRow(s_start);
6673 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6674 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6675 tabular.bottomLine(cur.selEnd().idx()),
6676 tabular.getAlignment(cur.selEnd().idx()));
6679 cur.selection(false);
6683 case Tabular::UNSET_MULTIROW: {
6684 if (!cur.selection()) {
6685 if (tabular.isMultiRow(cur.idx()))
6686 tabular.unsetMultiRow(cur.idx());
6691 case Tabular::MULTIROW: {
6692 if (!cur.selection()) {
6693 if (tabular.isMultiRow(cur.idx()))
6694 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6696 tabularFeatures(cur, Tabular::SET_MULTIROW);
6700 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6701 col_type const c = sel_col_start;
6702 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6703 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6706 // If the selection contains at least one singlerow cell
6707 // or multiple multirow cells,
6708 // we assume the user will merge is to a single multirow
6710 tabularFeatures(cur, Tabular::SET_MULTIROW);
6712 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6716 case Tabular::SET_INNER_LINES:
6717 setLinesInnerOnly = true;
6719 case Tabular::SET_ALL_LINES:
6722 case Tabular::UNSET_ALL_LINES:
6723 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6724 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6725 idx_type const cell = tabular.cellIndex(r, c);
6726 if (!setLinesInnerOnly || r != sel_row_start)
6727 tabular.setTopLine(cell, setLines);
6728 if ((!setLinesInnerOnly || r != sel_row_end)
6729 && (!setLines || r == sel_row_end))
6730 tabular.setBottomLine(cell, setLines);
6731 if ((!setLinesInnerOnly || c != sel_col_end)
6732 && (!setLines || c == sel_col_end))
6733 tabular.setRightLine(cell, setLines);
6734 if ((!setLinesInnerOnly || c != sel_col_start))
6735 tabular.setLeftLine(cell, setLines);
6739 case Tabular::RESET_FORMAL_DEFAULT:
6740 for (row_type r = 0; r < tabular.nrows(); ++r) {
6741 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6742 for (col_type c = 0; c < tabular.ncols(); ++c) {
6743 idx_type const cell = tabular.cellIndex(r, c);
6744 tabular.setTopLine(cell, head_or_foot);
6745 tabular.setBottomLine(cell, head_or_foot);
6750 case Tabular::SET_BORDER_LINES:
6751 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6752 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6753 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6755 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6756 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6757 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6761 case Tabular::TOGGLE_LONGTABULAR:
6762 if (tabular.is_long_tabular)
6763 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6765 tabular.is_long_tabular = true;
6768 case Tabular::SET_LONGTABULAR:
6769 tabular.is_long_tabular = true;
6772 case Tabular::UNSET_LONGTABULAR:
6773 for (row_type r = 0; r < tabular.nrows(); ++r) {
6774 if (tabular.ltCaption(r)) {
6775 cur.idx() = tabular.cellIndex(r, 0);
6778 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6781 tabular.is_long_tabular = false;
6784 case Tabular::SET_ROTATE_TABULAR:
6785 tabular.rotate = convert<int>(value);
6788 case Tabular::UNSET_ROTATE_TABULAR:
6792 case Tabular::TOGGLE_ROTATE_TABULAR:
6793 // when pressing the rotate button we default to 90° rotation
6794 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6797 case Tabular::TABULAR_VALIGN_TOP:
6798 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6801 case Tabular::TABULAR_VALIGN_MIDDLE:
6802 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6805 case Tabular::TABULAR_VALIGN_BOTTOM:
6806 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6809 case Tabular::LONGTABULAR_ALIGN_LEFT:
6810 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6813 case Tabular::LONGTABULAR_ALIGN_CENTER:
6814 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6817 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6818 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6821 case Tabular::SET_ROTATE_CELL:
6822 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6823 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6824 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6827 case Tabular::UNSET_ROTATE_CELL:
6828 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6829 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6830 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6833 case Tabular::TOGGLE_ROTATE_CELL:
6835 bool oneNotRotated = oneCellHasRotationState(false,
6836 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6838 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6839 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6840 // when pressing the rotate cell button we default to 90° rotation
6842 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6844 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6849 case Tabular::SET_USEBOX: {
6850 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6851 if (val == tabular.getUsebox(cur.idx()))
6852 val = Tabular::BOX_NONE;
6853 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6854 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6855 tabular.setUsebox(tabular.cellIndex(r, c), val);
6859 case Tabular::UNSET_LTFIRSTHEAD:
6862 case Tabular::SET_LTFIRSTHEAD:
6863 tabular.getRowOfLTFirstHead(row, ltt);
6864 checkLongtableSpecial(ltt, value, flag);
6865 tabular.setLTHead(row, flag, ltt, true);
6868 case Tabular::UNSET_LTHEAD:
6871 case Tabular::SET_LTHEAD:
6872 tabular.getRowOfLTHead(row, ltt);
6873 checkLongtableSpecial(ltt, value, flag);
6874 tabular.setLTHead(row, flag, ltt, false);
6877 case Tabular::UNSET_LTFOOT:
6880 case Tabular::SET_LTFOOT:
6881 tabular.getRowOfLTFoot(row, ltt);
6882 checkLongtableSpecial(ltt, value, flag);
6883 tabular.setLTFoot(row, flag, ltt, false);
6886 case Tabular::UNSET_LTLASTFOOT:
6889 case Tabular::SET_LTLASTFOOT:
6890 tabular.getRowOfLTLastFoot(row, ltt);
6891 checkLongtableSpecial(ltt, value, flag);
6892 tabular.setLTFoot(row, flag, ltt, true);
6895 case Tabular::UNSET_LTNEWPAGE:
6898 case Tabular::SET_LTNEWPAGE:
6899 tabular.setLTNewPage(row, flag);
6902 case Tabular::SET_LTCAPTION: {
6903 if (tabular.ltCaption(row))
6905 cur.idx() = tabular.setLTCaption(cur, row, true);
6908 cur.selection(false);
6909 // If a row is set as caption, then also insert
6910 // a caption. Otherwise the LaTeX output is broken.
6911 // Select cell if it is non-empty
6912 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6913 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6914 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6918 case Tabular::UNSET_LTCAPTION: {
6919 if (!tabular.ltCaption(row))
6921 cur.idx() = tabular.setLTCaption(cur, row, false);
6924 cur.selection(false);
6925 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6926 if (lyx::getStatus(fr).enabled())
6931 case Tabular::TOGGLE_LTCAPTION: {
6932 if (tabular.ltCaption(row))
6933 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6935 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6939 case Tabular::TOGGLE_BOOKTABS:
6940 tabular.use_booktabs = !tabular.use_booktabs;
6943 case Tabular::SET_BOOKTABS:
6944 tabular.use_booktabs = true;
6947 case Tabular::UNSET_BOOKTABS:
6948 tabular.use_booktabs = false;
6951 case Tabular::SET_TOP_SPACE: {
6953 if (value == "default")
6954 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6955 tabular.row_info[r].top_space_default = true;
6956 else if (value == "none")
6957 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6958 tabular.row_info[r].top_space_default = false;
6959 tabular.row_info[r].top_space = len;
6961 else if (isValidLength(value, &len))
6962 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6963 tabular.row_info[r].top_space_default = false;
6964 tabular.row_info[r].top_space = len;
6969 case Tabular::SET_BOTTOM_SPACE: {
6971 if (value == "default")
6972 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6973 tabular.row_info[r].bottom_space_default = true;
6974 else if (value == "none")
6975 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6976 tabular.row_info[r].bottom_space_default = false;
6977 tabular.row_info[r].bottom_space = len;
6979 else if (isValidLength(value, &len))
6980 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6981 tabular.row_info[r].bottom_space_default = false;
6982 tabular.row_info[r].bottom_space = len;
6987 case Tabular::SET_INTERLINE_SPACE: {
6989 if (value == "default")
6990 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6991 tabular.row_info[r].interline_space_default = true;
6992 else if (value == "none")
6993 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6994 tabular.row_info[r].interline_space_default = false;
6995 tabular.row_info[r].interline_space = len;
6997 else if (isValidLength(value, &len))
6998 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6999 tabular.row_info[r].interline_space_default = false;
7000 tabular.row_info[r].interline_space = len;
7005 case Tabular::SET_DECIMAL_POINT:
7006 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
7007 tabular.column_info[c].decimal_point = from_utf8(value);
7010 // dummy stuff just to avoid warnings
7011 case Tabular::LAST_ACTION:
7017 bool InsetTabular::copySelection(Cursor & cur)
7019 if (!cur.selection())
7024 getSelection(cur, rs, re, cs, ce);
7026 paste_tabular.reset(new Tabular(tabular));
7028 for (row_type r = 0; r < rs; ++r)
7029 paste_tabular->deleteRow(0);
7031 row_type const rows = re - rs + 1;
7032 while (paste_tabular->nrows() > rows)
7033 paste_tabular->deleteRow(rows);
7035 for (col_type c = 0; c < cs; ++c)
7036 paste_tabular->deleteColumn(0);
7038 col_type const columns = ce - cs + 1;
7039 while (paste_tabular->ncols() > columns)
7040 paste_tabular->deleteColumn(columns);
7042 paste_tabular->setBuffer(tabular.buffer());
7044 odocstringstream os;
7045 OutputParams const runparams(0);
7046 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7047 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7048 cap::copySelection(cur, os.str());
7050 // mark tabular stack dirty
7051 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7052 // when we (hopefully) have a one-for-all paste mechanism.
7053 // This must be called after cap::copySelection.
7054 dirtyTabularStack(true);
7060 bool InsetTabular::pasteClipboard(Cursor & cur)
7064 col_type actcol = tabular.cellColumn(cur.idx());
7065 row_type actrow = tabular.cellRow(cur.idx());
7067 if (cur.selIsMultiCell()) {
7070 getSelection(cur, actrow, re, actcol, ce);
7073 for (row_type r1 = 0, r2 = actrow;
7074 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
7076 for (col_type c1 = 0, c2 = actcol;
7077 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
7079 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7080 tabular.isPartOfMultiColumn(r2, c2))
7082 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7086 if (tabular.isPartOfMultiColumn(r2, c2)) {
7090 shared_ptr<InsetTableCell> inset(
7091 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7092 tabular.setCellInset(r2, c2, inset);
7093 // FIXME?: why do we need to do this explicitly? (EL)
7094 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7096 // FIXME: change tracking (MG)
7097 inset->setChange(Change(buffer().params().track_changes ?
7098 Change::INSERTED : Change::UNCHANGED));
7107 void InsetTabular::cutSelection(Cursor & cur)
7109 if (!cur.selection())
7114 getSelection(cur, rs, re, cs, ce);
7115 for (row_type r = rs; r <= re; ++r) {
7116 for (col_type c = cs; c <= ce; ++c) {
7117 shared_ptr<InsetTableCell> t
7118 = cell(tabular.cellIndex(r, c));
7119 if (buffer().params().track_changes)
7120 // FIXME: Change tracking (MG)
7121 t->setChange(Change(Change::DELETED));
7127 // cursor position might be invalid now
7128 if (cur.pit() > cur.lastpit())
7129 cur.pit() = cur.lastpit();
7130 if (cur.pos() > cur.lastpos())
7131 cur.pos() = cur.lastpos();
7132 cur.clearSelection();
7136 bool InsetTabular::isRightToLeft(Cursor & cur) const
7138 // LASSERT: It might be better to abandon this Buffer.
7139 LASSERT(cur.depth() > 1, return false);
7140 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7141 pos_type const parentpos = cur[cur.depth() - 2].pos();
7142 return parentpar.getFontSettings(buffer().params(),
7143 parentpos).language()->rightToLeft();
7147 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7150 LASSERT(stidx <= enidx, return docstring());
7152 col_type const col1 = tabular.cellColumn(stidx);
7153 col_type const col2 = tabular.cellColumn(enidx);
7154 row_type const row1 = tabular.cellRow(stidx);
7155 row_type const row2 = tabular.cellRow(enidx);
7157 for (col_type col = col1; col <= col2; col++)
7158 for (row_type row = row1; row <= row2; row++) {
7163 retval += tabular.cellInset(row, col)->asString(intoInsets);
7169 void InsetTabular::getSelection(Cursor & cur,
7170 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7172 CursorSlice const & beg = cur.selBegin();
7173 CursorSlice const & end = cur.selEnd();
7174 cs = tabular.cellColumn(beg.idx());
7175 ce = tabular.cellColumn(end.idx());
7179 rs = tabular.cellRow(beg.idx());
7180 re = tabular.cellRow(end.idx());
7186 Text * InsetTabular::getText(int idx) const
7188 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
7192 bool InsetTabular::isChanged() const
7194 for (idx_type idx = 0; idx < nargs(); ++idx) {
7195 if (cell(idx)->isChanged())
7197 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7199 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7206 void InsetTabular::setChange(Change const & change)
7208 for (idx_type idx = 0; idx < nargs(); ++idx)
7209 cell(idx)->setChange(change);
7213 void InsetTabular::acceptChanges()
7215 for (idx_type idx = 0; idx < nargs(); ++idx)
7216 cell(idx)->acceptChanges();
7217 for (row_type row = 0; row < tabular.nrows(); ++row) {
7218 if (tabular.row_info[row].change.inserted())
7219 tabular.row_info[row].change.setUnchanged();
7220 else if (tabular.row_info[row].change.deleted())
7221 tabular.deleteRow(row, true);
7223 for (col_type col = 0; col < tabular.ncols(); ++col) {
7224 if (tabular.column_info[col].change.inserted())
7225 tabular.column_info[col].change.setUnchanged();
7226 else if (tabular.column_info[col].change.deleted())
7227 tabular.deleteColumn(col, true);
7229 tabular.updateIndexes();
7233 void InsetTabular::rejectChanges()
7235 for (idx_type idx = 0; idx < nargs(); ++idx)
7236 cell(idx)->rejectChanges();
7237 for (row_type row = 0; row < tabular.nrows(); ++row) {
7238 if (tabular.row_info[row].change.deleted())
7239 tabular.row_info[row].change.setUnchanged();
7240 else if (tabular.row_info[row].change.inserted())
7241 tabular.deleteRow(row, true);
7243 for (col_type col = 0; col < tabular.ncols(); ++col) {
7244 if (tabular.column_info[col].change.deleted())
7245 tabular.column_info[col].change.setUnchanged();
7246 else if (tabular.column_info[col].change.inserted())
7247 tabular.deleteColumn(col, true);
7249 tabular.updateIndexes();
7253 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7255 return tabular.getPWidth(cell).zero();
7259 bool InsetTabular::forcePlainLayout(idx_type cell) const
7261 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
7265 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7268 if (buf.length() <= 0)
7273 col_type maxCols = 1;
7274 size_t const len = buf.length();
7278 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7286 maxCols = max(cols, maxCols);
7292 maxCols = max(cols, maxCols);
7298 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7299 loctab = paste_tabular.get();
7300 dirtyTabularStack(true);
7303 cell = bv.cursor().idx();
7304 ocol = tabular.cellColumn(cell);
7305 row = tabular.cellRow(cell);
7309 idx_type cells = loctab->numberofcells;
7312 rows = loctab->nrows();
7313 col_type columns = loctab->ncols();
7316 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7322 // append column if necessary
7323 if (cols == columns) {
7324 loctab->appendColumn(cols - 1);
7325 columns = loctab->ncols();
7326 cells = loctab->numberofcells;
7329 if (cols < columns) {
7330 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7331 Font const font = bv.textMetrics(&inset->text()).
7333 inset->setText(buf.substr(op, p - op), font,
7334 buffer().params().track_changes);
7340 // we can only set this if we are not too far right
7341 if (cols < columns) {
7342 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7343 Font const font = bv.textMetrics(&inset->text()).
7345 inset->setText(buf.substr(op, p - op), font,
7346 buffer().params().track_changes);
7350 // append row if necessary
7351 if (row == rows && p < len - 1) {
7352 loctab->appendRow(row - 1);
7353 rows = loctab->nrows();
7354 cells = loctab->numberofcells;
7357 cell = loctab->cellIndex(row, cols);
7363 // check for the last cell if there is no trailing '\n'
7364 if (cell < cells && op < len) {
7365 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7366 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7367 inset->setText(buf.substr(op, len - op), font,
7368 buffer().params().track_changes);
7374 void InsetTabular::addPreview(DocIterator const & inset_pos,
7375 PreviewLoader & loader) const
7377 DocIterator cell_pos = inset_pos;
7379 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7380 for (row_type r = 0; r < tabular.nrows(); ++r) {
7381 for (col_type c = 0; c < tabular.ncols(); ++c) {
7382 cell_pos.top().idx() = tabular.cellIndex(r, c);
7383 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7389 bool InsetTabular::completionSupported(Cursor const & cur) const
7391 Cursor const & bvCur = cur.bv().cursor();
7392 if (&bvCur.inset() != this)
7394 return cur.text()->completionSupported(cur);
7398 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7400 return completionSupported(cur);
7404 bool InsetTabular::automaticInlineCompletion() const
7406 return lyxrc.completion_inline_text;
7410 bool InsetTabular::automaticPopupCompletion() const
7412 return lyxrc.completion_popup_text;
7416 bool InsetTabular::showCompletionCursor() const
7418 return lyxrc.completion_cursor_text;
7422 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7424 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
7428 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7430 if (!completionSupported(cur))
7432 return cur.text()->completionPrefix(cur);
7436 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7438 if (!completionSupported(cur))
7441 return cur.text()->insertCompletion(cur, s, finished);
7445 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7446 Dimension & dim) const
7448 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7449 tm.completionPosAndDim(cur, x, y, dim);
7453 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7455 istringstream data(in);
7457 lex.setStream(data);
7464 if (!lex || token != "tabular") {
7465 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7469 // This is part of the inset proper that is usually swallowed
7470 // by Buffer::readInset
7472 if (!lex || token != "Tabular") {
7473 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7481 string InsetTabular::params2string(InsetTabular const & inset)
7484 data << "tabular" << ' ';
7486 data << "\\end_inset\n";
7491 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7493 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
7494 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
7495 if (!tabular.isPartOfMultiColumn(r,c) &&
7496 !tabular.isPartOfMultiRow(r,c))
7499 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7500 ParagraphList::iterator it = parlist.begin();
7501 ParagraphList::iterator const en = parlist.end();
7502 for (; it != en; ++it)
7503 it->setLayout(dc.plainLayout());