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) 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)
1661 return cell_info[row][c].cellno;
1665 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1667 col_type c = ncols() - 1;
1668 // of course we check against 0 so we don't crash. but we have the same
1669 // problem as in the previous routine: if all the cells are part of a
1670 // multirow or part of a multi column, then our return value is invalid.
1672 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1673 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1675 return cell_info[row][c].cellno;
1679 Tabular::row_type Tabular::cellRow(idx_type cell) const
1681 if (cell >= numberofcells)
1685 return rowofcell[cell];
1689 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1691 if (cell >= numberofcells)
1695 return columnofcell[cell];
1699 void Tabular::write(ostream & os) const
1703 << write_attribute("version", 3)
1704 << write_attribute("rows", nrows())
1705 << write_attribute("columns", ncols())
1707 // global longtable options
1709 << write_attribute("rotate", rotate)
1710 << write_attribute("booktabs", use_booktabs)
1711 << write_attribute("islongtable", is_long_tabular)
1712 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1713 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1714 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1715 << write_attribute("headTopDL", endhead.topDL)
1716 << write_attribute("headBottomDL", endhead.bottomDL)
1717 << write_attribute("footTopDL", endfoot.topDL)
1718 << write_attribute("footBottomDL", endfoot.bottomDL)
1719 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1720 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1721 << write_attribute("lastFootEmpty", endlastfoot.empty);
1722 // longtables cannot be aligned vertically
1723 if (!is_long_tabular)
1724 os << write_attribute("tabularvalignment", tabular_valignment);
1725 os << write_attribute("tabularwidth", tabular_width);
1726 if (is_long_tabular)
1727 os << write_attribute("longtabularalignment", longtabular_alignment);
1729 for (col_type c = 0; c < ncols(); ++c) {
1731 << write_attribute("alignment", column_info[c].alignment);
1732 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1733 os << write_attribute("decimal_point", column_info[c].decimal_point);
1734 os << write_attribute("change", column_info[c].change, buffer().params())
1735 << write_attribute("valignment", column_info[c].valignment)
1736 << write_attribute("width", column_info[c].p_width.asString())
1737 << write_attribute("varwidth", column_info[c].varwidth)
1738 << write_attribute("special", column_info[c].align_special)
1741 for (row_type r = 0; r < nrows(); ++r) {
1742 static const string def("default");
1744 if (row_info[r].top_space_default)
1745 os << write_attribute("topspace", def);
1747 os << write_attribute("topspace", row_info[r].top_space);
1748 if (row_info[r].bottom_space_default)
1749 os << write_attribute("bottomspace", def);
1751 os << write_attribute("bottomspace", row_info[r].bottom_space);
1752 if (row_info[r].interline_space_default)
1753 os << write_attribute("interlinespace", def);
1755 os << write_attribute("interlinespace", row_info[r].interline_space);
1756 os << write_attribute("change", row_info[r].change, buffer().params())
1757 << write_attribute("endhead", row_info[r].endhead)
1758 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1759 << write_attribute("endfoot", row_info[r].endfoot)
1760 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1761 << write_attribute("newpage", row_info[r].newpage)
1762 << write_attribute("caption", row_info[r].caption)
1764 for (col_type c = 0; c < ncols(); ++c) {
1766 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1767 << write_attribute("multirow", cell_info[r][c].multirow)
1768 << write_attribute("mroffset", cell_info[r][c].mroffset)
1769 << write_attribute("alignment", cell_info[r][c].alignment)
1770 << write_attribute("valignment", cell_info[r][c].valignment)
1771 << write_attribute("topline", cell_info[r][c].top_line)
1772 << write_attribute("toplineltrim", cell_info[r][c].top_line_ltrimmed)
1773 << write_attribute("toplinertrim", cell_info[r][c].top_line_rtrimmed)
1774 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1775 << write_attribute("bottomlineltrim", cell_info[r][c].bottom_line_ltrimmed)
1776 << write_attribute("bottomlinertrim", cell_info[r][c].bottom_line_rtrimmed)
1777 << write_attribute("leftline", cell_info[r][c].left_line)
1778 << write_attribute("rightline", cell_info[r][c].right_line)
1779 << write_attribute("rotate", cell_info[r][c].rotate)
1780 << write_attribute("usebox", cell_info[r][c].usebox)
1781 << write_attribute("width", cell_info[r][c].p_width)
1782 << write_attribute("special", cell_info[r][c].align_special)
1784 os << "\\begin_inset ";
1785 cell_info[r][c].inset->write(os);
1786 os << "\n\\end_inset\n"
1788 // FIXME This can be removed again once the mystery
1789 // crash has been resolved.
1794 os << "</lyxtabular>\n";
1798 void Tabular::read(Lexer & lex)
1801 istream & is = lex.getStream();
1803 l_getline(is, line);
1804 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1805 LASSERT(false, return);
1809 if (!getTokenValue(line, "version", version))
1811 LATTEST(version >= 2);
1814 if (!getTokenValue(line, "rows", rows_arg))
1817 if (!getTokenValue(line, "columns", columns_arg))
1819 init(buffer_, rows_arg, columns_arg);
1820 l_getline(is, line);
1821 if (!prefixIs(line, "<features")) {
1822 lyxerr << "Wrong tabular format (expected <features ...> got"
1823 << line << ')' << endl;
1826 getTokenValue(line, "rotate", rotate);
1827 getTokenValue(line, "booktabs", use_booktabs);
1828 getTokenValue(line, "islongtable", is_long_tabular);
1829 getTokenValue(line, "tabularvalignment", tabular_valignment);
1830 getTokenValue(line, "tabularwidth", tabular_width);
1831 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1832 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1833 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1834 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1835 getTokenValue(line, "headTopDL", endhead.topDL);
1836 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1837 getTokenValue(line, "footTopDL", endfoot.topDL);
1838 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1839 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1840 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1841 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1843 for (col_type c = 0; c < ncols(); ++c) {
1845 if (!prefixIs(line,"<column")) {
1846 lyxerr << "Wrong tabular format (expected <column ...> got"
1847 << line << ')' << endl;
1850 getTokenValue(line, "alignment", column_info[c].alignment);
1851 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1852 getTokenValue(line, "valignment", column_info[c].valignment);
1853 getTokenValue(line, "width", column_info[c].p_width);
1854 getTokenValue(line, "special", column_info[c].align_special);
1855 getTokenValue(line, "varwidth", column_info[c].varwidth);
1856 getTokenValue(line, "change", column_info[c].change, buffer().params());
1859 for (row_type i = 0; i < nrows(); ++i) {
1860 l_getline(is, line);
1861 if (!prefixIs(line, "<row")) {
1862 lyxerr << "Wrong tabular format (expected <row ...> got"
1863 << line << ')' << endl;
1866 getTokenValue(line, "topspace", row_info[i].top_space,
1867 row_info[i].top_space_default);
1868 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1869 row_info[i].bottom_space_default);
1870 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1871 row_info[i].interline_space_default);
1872 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1873 getTokenValue(line, "endhead", row_info[i].endhead);
1874 getTokenValue(line, "endfoot", row_info[i].endfoot);
1875 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1876 getTokenValue(line, "newpage", row_info[i].newpage);
1877 getTokenValue(line, "caption", row_info[i].caption);
1878 getTokenValue(line, "change", row_info[i].change, buffer().params());
1879 for (col_type j = 0; j < ncols(); ++j) {
1880 l_getline(is, line);
1881 if (!prefixIs(line, "<cell")) {
1882 lyxerr << "Wrong tabular format (expected <cell ...> got"
1883 << line << ')' << endl;
1886 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1887 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1888 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1889 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1890 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1891 getTokenValue(line, "topline", cell_info[i][j].top_line);
1892 getTokenValue(line, "toplineltrim", cell_info[i][j].top_line_ltrimmed);
1893 getTokenValue(line, "toplinertrim", cell_info[i][j].top_line_rtrimmed);
1894 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1895 getTokenValue(line, "bottomlineltrim", cell_info[i][j].bottom_line_ltrimmed);
1896 getTokenValue(line, "bottomlinertrim", cell_info[i][j].bottom_line_rtrimmed);
1897 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1898 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1899 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1900 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1901 getTokenValue(line, "width", cell_info[i][j].p_width);
1903 getTokenValue(line, "special", cell_info[i][j].align_special);
1904 l_getline(is, line);
1905 if (prefixIs(line, "\\begin_inset")) {
1906 cell_info[i][j].inset->setBuffer(*buffer_);
1907 cell_info[i][j].inset->read(lex);
1908 l_getline(is, line);
1910 if (!prefixIs(line, "</cell>")) {
1911 lyxerr << "Wrong tabular format (expected </cell> got"
1912 << line << ')' << endl;
1916 l_getline(is, line);
1917 if (!prefixIs(line, "</row>")) {
1918 lyxerr << "Wrong tabular format (expected </row> got"
1919 << line << ')' << endl;
1923 while (!prefixIs(line, "</lyxtabular>")) {
1924 l_getline(is, line);
1930 bool Tabular::isMultiColumn(idx_type cell) const
1932 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1933 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1937 bool Tabular::hasMultiColumn(col_type c) const
1939 for (row_type r = 0; r < nrows(); ++r) {
1940 if (isMultiColumn(cellIndex(r, c)))
1947 bool Tabular::hasVarwidthColumn() const
1949 for (col_type c = 0; c < ncols(); ++c) {
1950 if (column_info[c].varwidth)
1957 bool Tabular::isVTypeColumn(col_type c) const
1959 for (row_type r = 0; r < nrows(); ++r) {
1960 idx_type idx = cellIndex(r, c);
1961 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1968 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1970 return cell_info[cellRow(cell)][cellColumn(cell)];
1974 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1976 return cell_info[cellRow(cell)][cellColumn(cell)];
1980 Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
1981 bool const right_border)
1983 idx_type const col = cellColumn(cell);
1984 idx_type const row = cellRow(cell);
1985 for (idx_type i = 0; i < number; ++i)
1986 unsetMultiRow(cellIndex(row, col + i));
1988 // unsetting of multirow may have invalidated cell index
1989 cell = cellIndex(row, col);
1990 CellData & cs = cellInfo(cell);
1991 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1992 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1993 cs.alignment = column_info[col].alignment;
1994 setRightLine(cell, right_border);
1995 // non-fixed width multicolumns cannot have multiple paragraphs
1996 if (getPWidth(cell).zero()) {
1997 toggleFixedWidth(cur, cellInset(cell).get(),
1998 !getPWidth(cell).zero(), isMultiColumn(cell),
2000 // cur can become invalid after paragraphs were merged
2004 idx_type lastcell = cellIndex(row, col + number - 1);
2005 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
2006 CellData & cs1 = cellInfo(cell + i);
2007 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
2008 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2016 bool Tabular::isMultiRow(idx_type cell) const
2018 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
2019 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
2022 bool Tabular::hasMultiRow(row_type r) const
2024 for (col_type c = 0; c < ncols(); ++c) {
2025 if (isMultiRow(cellIndex(r, c)))
2031 Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
2032 bool const bottom_border,
2033 LyXAlignment const halign)
2035 idx_type const col = cellColumn(cell);
2036 idx_type const row = cellRow(cell);
2037 for (idx_type i = 0; i < number; ++i)
2038 unsetMultiColumn(cellIndex(row + i, col));
2040 // unsetting of multirow may have invalidated cell index
2041 cell = cellIndex(row, col);
2042 CellData & cs = cellInfo(cell);
2043 cs.multirow = CELL_BEGIN_OF_MULTIROW;
2044 cs.valignment = LYX_VALIGN_MIDDLE;
2045 // the horizontal alignment of multirow cells can only
2046 // be changed for the whole table row,
2047 // support changing this only for the multirow cell can be done via
2049 if (getPWidth(cell).zero())
2050 cs.alignment = halign;
2052 cs.alignment = LYX_ALIGN_LEFT;
2054 // Multirows cannot have multiple paragraphs
2055 if (getPWidth(cell).zero()) {
2056 toggleFixedWidth(cur, cellInset(cell).get(),
2057 !getPWidth(cell).zero(),
2058 isMultiColumn(cell), isMultiRow(cell));
2059 // cur can become invalid after paragraphs were merged
2063 // set the bottom line of the last selected cell
2064 setBottomLine(cell, bottom_border);
2066 for (idx_type i = 1; i < number; ++i) {
2067 CellData & cs1 = cell_info[row + i][col];
2068 cs1.multirow = CELL_PART_OF_MULTIROW;
2069 cs.inset->appendParagraphs(cs1.inset->paragraphs());
2077 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
2079 row_type const row = cellRow(cell);
2080 col_type const col = cellColumn(cell);
2082 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
2089 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
2091 col_type const column = cellColumn(cell);
2092 col_type row = cellRow(cell) + 1;
2093 while (row < nrows() && isPartOfMultiRow(row, column))
2096 return row - cellRow(cell);
2100 void Tabular::unsetMultiColumn(idx_type cell)
2102 if (!isMultiColumn(cell))
2105 row_type const row = cellRow(cell);
2106 col_type const col = cellColumn(cell);
2107 row_type const span = columnSpan(cell);
2108 for (col_type c = 0; c < span; ++c) {
2109 // in the table dialog the lines are set in every case
2110 // when unsetting a multicolumn this leads to an additional right
2111 // line for every cell that was part of the former multicolumn cell,
2112 // except if the cell is in the last column
2113 // therefore remove this line
2114 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
2115 && (col + c) < (col + span - 1))
2116 cell_info[row][col + c].right_line = false;
2117 cell_info[row][col + c].multicolumn = CELL_NORMAL;
2123 void Tabular::unsetMultiRow(idx_type cell)
2125 if (!isMultiRow(cell))
2128 cellInfo(cell).valignment = LYX_VALIGN_TOP;
2129 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
2130 row_type const row = cellRow(cell);
2131 col_type const col = cellColumn(cell);
2132 row_type const span = rowSpan(cell);
2133 for (row_type r = 0; r < span; ++r)
2134 cell_info[row + r][col].multirow = CELL_NORMAL;
2139 void Tabular::setRotateCell(idx_type cell, int value)
2141 cellInfo(cell).rotate = value;
2145 int Tabular::getRotateCell(idx_type cell) const
2147 return cellInfo(cell).rotate;
2151 bool Tabular::needRotating() const
2153 if (rotate && !is_long_tabular)
2155 for (row_type r = 0; r < nrows(); ++r)
2156 for (col_type c = 0; c < ncols(); ++c)
2157 if (cell_info[r][c].rotate != 0)
2163 bool Tabular::isLastCell(idx_type cell) const
2165 if (cell + 1 < numberofcells)
2171 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2173 if (cellRow(cell) == 0)
2176 col_type const col = cellColumn(cell);
2177 row_type r = cellRow(cell) - 1;
2178 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2181 return cell_info[r][col].cellno;
2185 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2187 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2188 if (nextrow < nrows())
2189 return cell_info[nextrow][cellColumn(cell)].cellno;
2194 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2196 LASSERT(column != npos && column < ncols(), column = 0);
2197 LASSERT(row != npos && row < nrows(), row = 0);
2198 return cell_info[row][column].cellno;
2202 void Tabular::setUsebox(idx_type cell, BoxType type)
2204 cellInfo(cell).usebox = type;
2208 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2210 if (getRotateCell(cell) == 0
2211 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2212 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2214 if (cellInfo(cell).usebox > 1)
2215 return cellInfo(cell).usebox;
2216 return useBox(cell);
2221 // This are functions used for the longtable support
2223 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2229 row_info[row].endfirsthead = flag;
2233 row_info[row].endhead = flag;
2238 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2241 hd.set = haveLTHead();
2242 return row_info[row].endhead;
2246 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2249 hd.set = haveLTFirstHead();
2250 return row_info[row].endfirsthead;
2254 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2260 row_info[row].endlastfoot = flag;
2264 row_info[row].endfoot = flag;
2269 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2272 fd.set = haveLTFoot();
2273 return row_info[row].endfoot;
2277 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2280 fd.set = haveLTLastFoot();
2281 return row_info[row].endlastfoot;
2285 void Tabular::setLTNewPage(row_type row, bool what)
2287 row_info[row].newpage = what;
2291 bool Tabular::getLTNewPage(row_type row) const
2293 return row_info[row].newpage;
2297 bool Tabular::haveLTHead(bool withcaptions) const
2299 if (!is_long_tabular)
2301 for (row_type i = 0; i < nrows(); ++i)
2302 if (row_info[i].endhead &&
2303 (withcaptions || !row_info[i].caption))
2309 bool Tabular::haveLTFirstHead(bool withcaptions) const
2311 if (!is_long_tabular || endfirsthead.empty)
2313 for (row_type r = 0; r < nrows(); ++r)
2314 if (row_info[r].endfirsthead &&
2315 (withcaptions || !row_info[r].caption))
2321 bool Tabular::haveLTFoot(bool withcaptions) const
2323 if (!is_long_tabular)
2325 for (row_type r = 0; r < nrows(); ++r)
2326 if (row_info[r].endfoot &&
2327 (withcaptions || !row_info[r].caption))
2333 bool Tabular::haveLTLastFoot(bool withcaptions) const
2335 if (!is_long_tabular || endlastfoot.empty)
2337 for (row_type r = 0; r < nrows(); ++r)
2338 if (row_info[r].endlastfoot &&
2339 (withcaptions || !row_info[r].caption))
2345 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2347 idx_type i = getFirstCellInRow(row);
2349 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2350 setTopLine(i, false);
2351 setBottomLine(i, false);
2352 setLeftLine(i, false);
2353 setRightLine(i, false);
2354 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2355 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2356 setLTHead(row, true, endfirsthead, true);
2357 row_info[row].endfirsthead = true;
2360 unsetMultiColumn(i);
2361 // When unsetting a caption row, also all existing
2362 // captions in this row must be dissolved.
2364 row_info[row].caption = what;
2369 bool Tabular::ltCaption(row_type row) const
2371 return row_info[row].caption;
2375 bool Tabular::haveLTCaption(CaptionType captiontype) const
2377 if (!is_long_tabular)
2379 for (row_type r = 0; r < nrows(); ++r) {
2380 if (row_info[r].caption) {
2381 switch (captiontype) {
2382 case CAPTION_FIRSTHEAD:
2383 if (row_info[r].endfirsthead)
2387 if (row_info[r].endhead)
2391 if (row_info[r].endfoot)
2394 case CAPTION_LASTFOOT:
2395 if (row_info[r].endlastfoot)
2407 // end longtable support functions
2409 void Tabular::setRowAscent(row_type row, int height)
2411 if (row >= nrows() || row_info[row].ascent == height)
2413 row_info[row].ascent = height;
2417 void Tabular::setRowDescent(row_type row, int height)
2419 if (row >= nrows() || row_info[row].descent == height)
2421 row_info[row].descent = height;
2425 int Tabular::rowAscent(row_type row) const
2427 LASSERT(row < nrows(), row = 0);
2428 return row_info[row].ascent;
2432 int Tabular::rowDescent(row_type row) const
2434 LASSERT(row < nrows(), row = 0);
2435 return row_info[row].descent;
2439 int Tabular::height() const
2442 for (row_type row = 0; row < nrows(); ++row)
2443 height += rowAscent(row) + rowDescent(row) +
2449 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2451 LASSERT(row < nrows(), return false);
2452 LASSERT(column < ncols(), return false);
2453 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2457 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2459 LASSERT(row < nrows(), return false);
2460 LASSERT(column < ncols(), return false);
2461 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2465 void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> columns) const
2467 // we only output complete row lines and the 1st row here, the rest
2468 // is done in Tabular::TeXBottomHLine(...)
2470 // get for each column the topline (if any)
2471 map<col_type, bool> topline, topltrims, toprtrims;
2473 bool have_trims = false;
2474 for (auto const & c : columns) {
2475 topline[c] = topLine(cellIndex(row, c));
2476 topltrims[c] = topLineTrim(cellIndex(row, c)).first;
2477 toprtrims[c] = topLineTrim(cellIndex(row, c)).second;
2478 // If cell is part of a multirow and not the first cell of the
2479 // multirow, no line must be drawn.
2481 if (isMultiRow(cellIndex(row, c))
2482 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2484 topltrims[c] = false;
2485 toprtrims[c] = false;
2487 // copy trimming to multicolumn parts
2488 if (isPartOfMultiColumn(row, c)) {
2489 topltrims[c] = topltrims[c-1];
2490 toprtrims[c] = toprtrims[c-1];
2492 if (topline.find(c) != topline.end() && topline.find(c)->second)
2494 if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
2495 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
2499 // do nothing if empty first row, or incomplete row line after
2500 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2503 // Is this the actual first row (excluding longtable caption row)?
2504 bool const realfirstrow = (row == 0
2505 || (is_long_tabular && row == 1 && ltCaption(0)));
2507 // only output complete row lines and the 1st row's clines
2508 if (nset == ncols() && !have_trims) {
2510 os << (realfirstrow ? "\\toprule " : "\\midrule ");
2514 } else if (realfirstrow || have_trims) {
2515 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2517 for (auto & cl : columns) {
2521 if (topline.find(c)->second) {
2522 col_type offset = 0;
2523 for (col_type j = 0 ; j < c; ++j)
2524 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2526 string const firstcol = convert<string>(c + 1 + offset);
2527 while (isPartOfMultiColumn(row, c))
2530 if (topltrims.find(c) != topltrims.end()
2531 && topltrims.find(c)->second)
2533 col_type cstart = c;
2534 for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
2535 if (isMultiColumn(cellIndex(row, c))
2536 && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
2538 if (c > cstart && topltrims.find(c) != topltrims.end()
2539 && topltrims.find(c)->second) {
2540 if (!isPartOfMultiColumn(row, c))
2543 } else if (toprtrims.find(c) != toprtrims.end()
2544 && toprtrims.find(c)->second)
2548 for (col_type j = cstart ; j < c ; ++j)
2549 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2551 col_type const lastcol = c + 1 + offset;
2552 if (toprtrims.find(c) != toprtrims.end()
2553 && toprtrims.find(c)->second)
2558 os << "(" << trim << ")";
2559 os << "{" << firstcol << '-' << lastcol << "}";
2560 if (c == ncols() - 1)
2570 void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> columns) const
2572 // we output bottomlines of row r and the toplines of row r+1
2573 // if the latter do not span the whole tabular
2575 // get the bottomlines of row r, and toplines in next row
2576 bool lastrow = row == nrows() - 1;
2577 map<col_type, bool> bottomline, topline, topltrims, toprtrims, bottomltrims, bottomrtrims;
2578 bool nextrowset = true;
2579 for (auto const & c : columns) {
2580 idx_type const idx = cellIndex(row, c);
2581 bottomline[c] = bottomLine(cellIndex(row, c));
2582 bottomltrims[c] = bottomLineTrim(idx).first;
2583 bottomrtrims[c] = bottomLineTrim(idx).second;
2584 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2585 topltrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).first;
2586 toprtrims[c] = !lastrow && topLineTrim(cellIndex(row + 1, c)).second;
2587 // If cell is part of a multirow and not the last cell of the
2588 // multirow, no line must be drawn.
2590 if (isMultiRow(cellIndex(row, c))
2591 && isMultiRow(cellIndex(row + 1, c))
2592 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2593 bottomline[c] = false;
2595 bottomltrims[c] = false;
2596 bottomrtrims[c] = false;
2597 topltrims[c] = false;
2598 toprtrims[c] = false;
2600 // copy trimming in multicolumn parts
2601 if (isPartOfMultiColumn(row, c)) {
2602 topltrims[c] = topltrims[c-1];
2603 toprtrims[c] = toprtrims[c-1];
2604 bottomltrims[c] = bottomltrims[c-1];
2605 bottomrtrims[c] = bottomrtrims[c-1];
2608 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2611 // combine this row's bottom lines and next row's toplines if necessary
2613 bool have_trims = false;
2614 for (auto const & c : columns) {
2616 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2617 bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2618 || (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
2619 bottomrtrims[c] =(bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
2620 || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
2621 if (bottomline.find(c)->second)
2623 if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
2624 || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
2628 // do nothing if empty, OR incomplete row line with a topline in next row
2629 if (nset == 0 || (nextrowset && nset != ncols()))
2632 if (nset == ncols() && !have_trims) {
2634 os << (lastrow ? "\\bottomrule" : "\\midrule");
2638 string const cline = use_booktabs ? "\\cmidrule" : "\\cline";
2640 for (auto & cl : columns) {
2644 if (bottomline.find(c)->second) {
2645 col_type offset = 0;
2646 for (col_type j = 0 ; j < c; ++j)
2647 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2649 string const firstcol = convert<string>(c + 1 + offset);
2650 while (isPartOfMultiColumn(row, c))
2653 if (bottomltrims.find(c) != bottomltrims.end()
2654 && bottomltrims.find(c)->second)
2656 col_type cstart = c;
2657 for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
2658 if (isMultiColumn(cellIndex(row, c))
2660 && isPartOfMultiColumn(row, c + 1))
2663 && bottomltrims.find(c) != bottomltrims.end()
2664 && bottomltrims.find(c)->second) {
2665 if (!isPartOfMultiColumn(row, c))
2668 } else if (bottomrtrims.find(c) != bottomrtrims.end()
2669 && bottomrtrims.find(c)->second)
2673 for (col_type j = cstart ; j < c ; ++j)
2674 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2676 col_type const lastcol = c + 1 + offset;
2677 if (bottomrtrims.find(c) != bottomrtrims.end()
2678 && bottomrtrims.find(c)->second)
2683 os << "(" << trim << ")";
2684 os << "{" << firstcol << '-' << lastcol << "}";
2685 if (c == ncols() - 1)
2695 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2696 bool & ismulticol, bool & ismultirow,
2697 bool const bidi) const
2699 row_type const r = cellRow(cell);
2700 if (is_long_tabular && row_info[r].caption)
2703 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2704 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2705 // figure out how to set the lines
2706 // we always set double lines to the right of the cell
2707 // or left in bidi RTL, respectively.
2708 col_type const c = cellColumn(cell);
2709 col_type const nextcol = c + columnSpan(cell);
2710 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2711 bool colright = columnRightLine(c);
2712 bool colleft = columnLeftLine(c);
2713 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2714 bool nextcellleft = nextcol < ncols()
2715 && leftLine(cellIndex(r, nextcol));
2716 bool coldouble = colright && nextcolleft;
2717 bool celldouble = rightLine(cell) && nextcellleft;
2719 ismulticol = (isMultiColumn(cell)
2720 || (c == 0 && colleft != leftLine(cell))
2721 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2722 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2723 || (coldouble != celldouble))
2726 // we center in multicol when no decimal point
2728 docstring const align_d = column_info[c].decimal_point;
2729 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2730 bool const nosep = !dit;
2731 ismulticol |= nosep;
2732 celldouble &= nosep;
2735 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2736 int latexcolspan = columnSpan(cell);
2737 for (col_type col = c; col < c + columnSpan(cell); ++col)
2738 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2742 os << "\\multicolumn{" << latexcolspan << "}{";
2743 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2744 || (!bidi && c == 0 && leftLine(cell))))
2746 if (bidi && celldouble)
2747 // add extra vertical line if we want a double one
2749 if (!cellInfo(cell).align_special.empty()) {
2750 os << cellInfo(cell).align_special;
2752 if (!getPWidth(cell).zero()) {
2754 case LYX_ALIGN_LEFT:
2755 os << ">{\\raggedright}";
2757 case LYX_ALIGN_RIGHT:
2758 os << ">{\\raggedleft}";
2760 case LYX_ALIGN_CENTER:
2761 os << ">{\\centering}";
2767 case LYX_VALIGN_TOP:
2770 case LYX_VALIGN_MIDDLE:
2773 case LYX_VALIGN_BOTTOM:
2778 << from_ascii(getPWidth(cell).asLatexString())
2782 case LYX_ALIGN_LEFT:
2785 case LYX_ALIGN_RIGHT:
2792 } // end if else !getPWidth
2793 } // end if else !cellinfo_of_cell
2794 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2796 if (!bidi && celldouble)
2797 // add extra vertical line if we want a double one
2800 } // end if ismulticol
2802 // we only need code for the first multirow cell
2803 ismultirow = isMultiRow(cell);
2805 os << "\\multirow{" << rowSpan(cell) << "}{";
2806 if (!getPWidth(cell).zero())
2807 os << from_ascii(getPWidth(cell).asLatexString());
2809 // we need to set a default value
2812 if (!getMROffset(cell).zero())
2813 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2815 } // end if ismultirow
2817 if (getRotateCell(cell) != 0)
2818 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2820 if (getUsebox(cell) == BOX_PARBOX) {
2823 case LYX_VALIGN_TOP:
2826 case LYX_VALIGN_MIDDLE:
2829 case LYX_VALIGN_BOTTOM:
2833 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2835 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2836 os << "\\begin{minipage}[";
2838 case LYX_VALIGN_TOP:
2841 case LYX_VALIGN_MIDDLE:
2844 case LYX_VALIGN_BOTTOM:
2848 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2850 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2851 os << "\\begin{varwidth}[";
2853 case LYX_VALIGN_TOP:
2856 case LYX_VALIGN_MIDDLE:
2859 case LYX_VALIGN_BOTTOM:
2863 os << "]{\\linewidth}\n";
2868 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2869 bool ismulticol, bool ismultirow) const
2871 row_type const r = cellRow(cell);
2872 if (is_long_tabular && row_info[r].caption)
2876 if (getUsebox(cell) == BOX_PARBOX)
2878 else if (getUsebox(cell) == BOX_MINIPAGE)
2879 os << breakln << "\\end{minipage}";
2880 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2881 os << breakln << "\\end{varwidth}";
2882 if (getRotateCell(cell) != 0)
2883 os << breakln << "\\end{turn}";
2891 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2892 OutputParams const & runparams,
2893 list<col_type> columns) const
2895 if (!is_long_tabular)
2899 // output caption which is in no header or footer
2900 if (haveLTCaption()) {
2901 for (row_type r = 0; r < nrows(); ++r) {
2902 if (row_info[r].caption &&
2903 !row_info[r].endfirsthead && !row_info[r].endhead &&
2904 !row_info[r].endfoot && !row_info[r].endlastfoot)
2905 TeXRow(os, r, runparams, columns);
2908 // output first header info
2909 if (haveLTFirstHead()) {
2910 if (endfirsthead.topDL)
2912 for (row_type r = 0; r < nrows(); ++r) {
2913 if (row_info[r].endfirsthead)
2914 TeXRow(os, r, runparams, columns);
2916 if (endfirsthead.bottomDL)
2918 os << "\\endfirsthead\n";
2920 // output header info
2922 if (endfirsthead.empty && !haveLTFirstHead())
2923 os << "\\endfirsthead\n";
2926 for (row_type r = 0; r < nrows(); ++r) {
2927 if (row_info[r].endhead)
2928 TeXRow(os, r, runparams, columns);
2930 if (endhead.bottomDL)
2932 os << "\\endhead\n";
2934 // output footer info
2938 for (row_type r = 0; r < nrows(); ++r) {
2939 if (row_info[r].endfoot)
2940 TeXRow(os, r, runparams, columns);
2942 if (endfoot.bottomDL)
2944 os << "\\endfoot\n";
2945 if (endlastfoot.empty && !haveLTLastFoot())
2946 os << "\\endlastfoot\n";
2948 // output lastfooter info
2949 if (haveLTLastFoot()) {
2950 if (endlastfoot.topDL)
2952 for (row_type r = 0; r < nrows(); ++r) {
2953 if (row_info[r].endlastfoot)
2954 TeXRow(os, r, runparams, columns);
2956 if (endlastfoot.bottomDL)
2958 os << "\\endlastfoot\n";
2963 bool Tabular::isValidRow(row_type row) const
2965 if (!is_long_tabular)
2967 return !row_info[row].endhead && !row_info[row].endfirsthead
2968 && !row_info[row].endfoot && !row_info[row].endlastfoot
2969 && !row_info[row].caption;
2973 void Tabular::TeXRow(otexstream & os, row_type row,
2974 OutputParams const & runparams,
2975 list<col_type> columns) const
2977 idx_type cell = cellIndex(row, 0);
2979 //output the top line
2980 TeXTopHLine(os, row, columns);
2982 if (row_info[row].top_space_default) {
2984 os << "\\addlinespace\n";
2986 os << "\\noalign{\\vskip\\doublerulesep}\n";
2987 } else if(!row_info[row].top_space.zero()) {
2989 os << "\\addlinespace["
2990 << from_ascii(row_info[row].top_space.asLatexString())
2993 os << "\\noalign{\\vskip"
2994 << from_ascii(row_info[row].top_space.asLatexString())
2998 bool ismulticol = false;
2999 bool ismultirow = false;
3001 // The bidi package (loaded by polyglossia with XeTeX) reverses RTL table columns
3002 // Luabibdi (used by LuaTeX) behaves like classic
3003 bool const bidi_rtl =
3004 runparams.local_font->isRightToLeft()
3005 && runparams.useBidiPackage();
3007 bidi_rtl ? getFirstCellInRow(row) : getLastCellInRow(row);
3009 for (auto const & c : columns) {
3010 if (isPartOfMultiColumn(row, c))
3013 cell = cellIndex(row, c);
3015 if (isPartOfMultiRow(row, c)
3016 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
3017 if (cell != lastcell)
3022 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
3023 InsetTableCell const * inset = cellInset(cell);
3025 Paragraph const & par = inset->paragraphs().front();
3027 os.texrow().forceStart(par.id(), 0);
3029 bool rtl = par.isRTL(buffer().params())
3031 && getPWidth(cell).zero()
3032 && !runparams.use_polyglossia;
3036 par.getParLanguage(buffer().params())->lang();
3037 if (lang == "farsi")
3039 else if (lang == "arabic_arabi")
3041 // currently, remaining RTL languages are
3042 // arabic_arabtex and hebrew
3046 // pass to the OutputParams that we are in a cell and
3047 // which alignment we have set.
3048 // InsetNewline needs this context information.
3049 OutputParams newrp(runparams);
3050 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
3051 ? OutputParams::PLAIN
3052 : OutputParams::ALIGNED;
3054 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3055 // copy cell and split in 2
3056 InsetTableCell head = InsetTableCell(*cellInset(cell));
3057 head.setBuffer(const_cast<Buffer &>(buffer()));
3058 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
3060 dit.push_back(CursorSlice(head));
3061 head.setMacrocontextPositionRecursive(dit);
3062 bool hassep = false;
3063 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
3065 tail.setBuffer(head.buffer());
3067 dit.push_back(CursorSlice(tail));
3068 tail.setMacrocontextPositionRecursive(dit);
3072 tail.latex(os, newrp);
3075 head.latex(os, newrp);
3077 head.latex(os, newrp);
3080 tail.latex(os, newrp);
3083 } else if (ltCaption(row)) {
3084 // Inside longtable caption rows, we must only output the caption inset
3085 // with its content and omit anything outside of that (see #10791)
3086 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
3087 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
3088 for (; it != i_end; ++it) {
3089 if (it->lyxCode() != CAPTION_CODE)
3091 it->latex(os, runparams);
3094 } else if (!isPartOfMultiRow(row, c)) {
3095 if (!runparams.nice)
3096 os.texrow().start(par.id(), 0);
3097 inset->latex(os, newrp);
3100 runparams.encoding = newrp.encoding;
3104 TeXCellPostamble(os, cell, ismulticol, ismultirow);
3105 if (cell != lastcell) { // not last cell in row
3112 os << "\\tabularnewline";
3113 if (row_info[row].bottom_space_default) {
3115 os << "\\addlinespace";
3117 os << "[\\doublerulesep]";
3118 } else if (!row_info[row].bottom_space.zero()) {
3120 os << "\\addlinespace";
3122 << from_ascii(row_info[row].bottom_space.asLatexString())
3127 //output the bottom line
3128 TeXBottomHLine(os, row, columns);
3130 if (row_info[row].interline_space_default) {
3132 os << "\\addlinespace\n";
3134 os << "\\noalign{\\vskip\\doublerulesep}\n";
3135 } else if (!row_info[row].interline_space.zero()) {
3137 os << "\\addlinespace["
3138 << from_ascii(row_info[row].interline_space.asLatexString())
3141 os << "\\noalign{\\vskip"
3142 << from_ascii(row_info[row].interline_space.asLatexString())
3148 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
3150 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
3151 && !hasVarwidthColumn();
3152 bool const is_xltabular = is_long_tabular
3153 && (hasVarwidthColumn() || !tabular_width.zero());
3154 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
3156 //+---------------------------------------------------------------------
3157 //+ first the opening preamble +
3158 //+---------------------------------------------------------------------
3161 if (!TexRow::isNone(pos))
3162 os.texrow().start(pos);
3165 if (is_long_tabular)
3166 os << "\\begin{landscape}\n";
3168 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
3171 // The bidi package (loaded by polyglossia with XeTeX) swaps the column
3172 // order for RTL (#9686). Thus we use this list.
3173 bool const bidi_rtl =
3174 runparams.local_font->isRightToLeft()
3175 && runparams.useBidiPackage();
3176 list<col_type> columns;
3177 for (col_type cl = 0; cl < ncols(); ++cl) {
3179 columns.push_front(cl);
3181 columns.push_back(cl);
3184 // If we use \cline or \cmidrule, we need to locally de-activate
3185 // the - character when using languages that activate it (e.g., Czech, Slovak).
3186 bool deactivate_chars = false;
3187 if ((runparams.use_babel || runparams.use_polyglossia)
3188 && contains(runparams.active_chars, '-')) {
3189 bool have_clines = false;
3190 // Check if we use \cline or \cmidrule
3191 for (row_type row = 0; row < nrows(); ++row) {
3192 col_type bset = 0, tset = 0;
3193 for (auto const & c : columns) {
3194 idx_type const idx = cellIndex(row, c);
3195 if (bottomLineTrim(idx).first || bottomLineTrim(idx).second
3196 || topLineTrim(idx).first || topLineTrim(idx).second) {
3200 if (bottomLine(cellIndex(row, c)))
3202 if (topLine(cellIndex(row, c)))
3205 if ((bset > 0 && bset < ncols()) || (tset > 0 && tset < ncols())) {
3211 deactivate_chars = true;
3212 os << "\\begingroup\n"
3213 << "\\catcode`\\-=12\n";
3217 if (is_long_tabular) {
3219 os << "\\begin{xltabular}";
3221 os << "\\begin{longtable}";
3222 switch (longtabular_alignment) {
3223 case LYX_LONGTABULAR_ALIGN_LEFT:
3226 case LYX_LONGTABULAR_ALIGN_CENTER:
3229 case LYX_LONGTABULAR_ALIGN_RIGHT:
3234 if (tabular_width.zero())
3235 os << "{" << from_ascii("\\columnwidth") << "}";
3237 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
3240 if (is_tabular_star)
3241 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
3242 else if (hasVarwidthColumn()) {
3243 os << "\\begin{tabularx}{";
3244 if (tabular_width.zero())
3245 os << from_ascii("\\columnwidth") << "}";
3247 os << from_ascii(tabular_width.asLatexString()) << "}";
3249 os << "\\begin{tabular}";
3250 switch (tabular_valignment) {
3251 case LYX_VALIGN_TOP:
3254 case LYX_VALIGN_MIDDLE:
3256 case LYX_VALIGN_BOTTOM:
3264 if (is_tabular_star)
3265 os << "@{\\extracolsep{\\fill}}";
3267 for (auto const & c : columns) {
3268 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
3270 if (!column_info[c].align_special.empty()) {
3271 os << column_info[c].align_special;
3273 if (!column_info[c].p_width.zero()) {
3274 bool decimal = false;
3275 switch (column_info[c].alignment) {
3276 case LYX_ALIGN_LEFT:
3277 os << ">{\\raggedright}";
3279 case LYX_ALIGN_RIGHT:
3280 os << ">{\\raggedleft}";
3282 case LYX_ALIGN_CENTER:
3283 os << ">{\\centering}";
3285 case LYX_ALIGN_NONE:
3286 case LYX_ALIGN_BLOCK:
3287 case LYX_ALIGN_LAYOUT:
3288 case LYX_ALIGN_SPECIAL:
3290 case LYX_ALIGN_DECIMAL: {
3292 os << ">{\\raggedright}";
3294 os << ">{\\raggedleft}";
3301 switch (column_info[c].valignment) {
3302 case LYX_VALIGN_TOP:
3303 // this is the default
3305 case LYX_VALIGN_MIDDLE:
3308 case LYX_VALIGN_BOTTOM:
3314 // Fixed-width cells with alignment at decimal separator
3315 // are output as two cells of half the width with the decimal
3316 // separator as column sep. This effectively puts the content
3317 // centered, which differs from the normal decimal sep alignment
3318 // and is not ideal, but we cannot do better ATM (see #9568).
3319 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3321 docstring const halffixedwith =
3322 from_ascii(Length(column_info[c].p_width.value() / 2,
3323 column_info[c].p_width.unit()).asLatexString());
3327 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3334 << from_ascii(column_info[c].p_width.asLatexString())
3336 } else if (column_info[c].varwidth) {
3337 switch (column_info[c].alignment) {
3338 case LYX_ALIGN_LEFT:
3339 os << ">{\\raggedright\\arraybackslash}";
3341 case LYX_ALIGN_RIGHT:
3342 os << ">{\\raggedleft\\arraybackslash}";
3344 case LYX_ALIGN_CENTER:
3345 os << ">{\\centering\\arraybackslash}";
3347 case LYX_ALIGN_NONE:
3348 case LYX_ALIGN_BLOCK:
3349 case LYX_ALIGN_LAYOUT:
3350 case LYX_ALIGN_SPECIAL:
3351 case LYX_ALIGN_DECIMAL:
3355 } else if (isVTypeColumn(c)) {
3356 switch (column_info[c].alignment) {
3357 case LYX_ALIGN_LEFT:
3358 os << ">{\\raggedright}";
3360 case LYX_ALIGN_RIGHT:
3361 os << ">{\\raggedleft}";
3363 case LYX_ALIGN_CENTER:
3364 os << ">{\\centering}";
3366 case LYX_ALIGN_NONE:
3367 case LYX_ALIGN_BLOCK:
3368 case LYX_ALIGN_LAYOUT:
3369 case LYX_ALIGN_SPECIAL:
3370 case LYX_ALIGN_DECIMAL:
3373 os << "V{\\linewidth}";
3375 switch (column_info[c].alignment) {
3376 case LYX_ALIGN_LEFT:
3379 case LYX_ALIGN_RIGHT:
3382 case LYX_ALIGN_DECIMAL:
3383 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3389 } // end if else !column_info[i].p_width
3390 } // end if else !column_info[i].align_special
3391 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3396 TeXLongtableHeaderFooter(os, runparams, columns);
3398 //+---------------------------------------------------------------------
3399 //+ the single row and columns (cells) +
3400 //+---------------------------------------------------------------------
3402 for (row_type r = 0; r < nrows(); ++r) {
3403 if (isValidRow(r)) {
3404 TeXRow(os, r, runparams, columns);
3405 if (is_long_tabular && row_info[r].newpage)
3406 os << "\\newpage\n";
3410 //+---------------------------------------------------------------------
3411 //+ the closing of the tabular +
3412 //+---------------------------------------------------------------------
3414 if (is_long_tabular) {
3416 os << "\\end{xltabular}";
3418 os << "\\end{longtable}";
3420 if (is_tabular_star)
3421 os << "\\end{tabular*}";
3422 else if (hasVarwidthColumn())
3423 os << "\\end{tabularx}";
3425 os << "\\end{tabular}";
3428 if (deactivate_chars)
3430 os << "\n\\endgroup\n";
3433 if (is_long_tabular)
3434 os << breakln << "\\end{landscape}";
3436 os << breakln << "\\end{turn}";
3439 if (!TexRow::isNone(pos))
3440 os.texrow().start(pos);
3444 int Tabular::docbookRow(odocstream & os, row_type row,
3445 OutputParams const & runparams) const
3448 idx_type cell = getFirstCellInRow(row);
3451 for (col_type c = 0; c < ncols(); ++c) {
3452 if (isPartOfMultiColumn(row, c))
3455 os << "<entry align=\"";
3456 switch (getAlignment(cell)) {
3457 case LYX_ALIGN_LEFT:
3460 case LYX_ALIGN_RIGHT:
3468 os << "\" valign=\"";
3469 switch (getVAlignment(cell)) {
3470 case LYX_VALIGN_TOP:
3473 case LYX_VALIGN_BOTTOM:
3476 case LYX_VALIGN_MIDDLE:
3481 if (isMultiColumn(cell)) {
3482 os << " namest=\"col" << c << "\" ";
3483 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3487 ret += cellInset(cell)->docbook(os, runparams);
3496 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3500 //+---------------------------------------------------------------------
3501 //+ first the opening preamble +
3502 //+---------------------------------------------------------------------
3504 os << "<tgroup cols=\"" << ncols()
3505 << "\" colsep=\"1\" rowsep=\"1\">\n";
3507 for (col_type c = 0; c < ncols(); ++c) {
3508 os << "<colspec colname=\"col" << c << "\" align=\"";
3509 switch (column_info[c].alignment) {
3510 case LYX_ALIGN_LEFT:
3513 case LYX_ALIGN_RIGHT:
3521 if (runparams.flavor == OutputParams::XML)
3527 //+---------------------------------------------------------------------
3528 //+ Long Tabular case +
3529 //+---------------------------------------------------------------------
3531 // output caption info
3532 // The caption flag wins over head/foot
3533 if (haveLTCaption()) {
3534 os << "<caption>\n";
3536 for (row_type r = 0; r < nrows(); ++r) {
3537 if (row_info[r].caption) {
3538 ret += docbookRow(os, r, runparams);
3541 os << "</caption>\n";
3544 // output header info
3545 if (haveLTHead(false) || haveLTFirstHead(false)) {
3548 for (row_type r = 0; r < nrows(); ++r) {
3549 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3550 !row_info[r].caption) {
3551 ret += docbookRow(os, r, runparams);
3557 // output footer info
3558 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3561 for (row_type r = 0; r < nrows(); ++r) {
3562 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3563 !row_info[r].caption) {
3564 ret += docbookRow(os, r, runparams);
3571 //+---------------------------------------------------------------------
3572 //+ the single row and columns (cells) +
3573 //+---------------------------------------------------------------------
3577 for (row_type r = 0; r < nrows(); ++r) {
3578 if (isValidRow(r)) {
3579 ret += docbookRow(os, r, runparams);
3584 //+---------------------------------------------------------------------
3585 //+ the closing of the tabular +
3586 //+---------------------------------------------------------------------
3595 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3596 OutputParams const & runparams, bool header) const
3599 string const celltag = header ? "th" : "td";
3600 idx_type cell = getFirstCellInRow(row);
3602 xs << html::StartTag("tr");
3603 for (col_type c = 0; c < ncols(); ++c) {
3604 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3609 Length const cwidth = column_info[c].p_width;
3610 if (!cwidth.zero()) {
3611 string const hwidth = cwidth.asHTMLString();
3612 attr << "style =\"width: " << hwidth << ";\" ";
3616 switch (getAlignment(cell)) {
3617 case LYX_ALIGN_LEFT:
3620 case LYX_ALIGN_RIGHT:
3628 attr << " valign='";
3629 switch (getVAlignment(cell)) {
3630 case LYX_VALIGN_TOP:
3633 case LYX_VALIGN_BOTTOM:
3636 case LYX_VALIGN_MIDDLE:
3641 if (isMultiColumn(cell))
3642 attr << " colspan='" << columnSpan(cell) << "'";
3643 else if (isMultiRow(cell))
3644 attr << " rowspan='" << rowSpan(cell) << "'";
3646 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3647 ret += cellInset(cell)->xhtml(xs, runparams);
3648 xs << html::EndTag(celltag) << html::CR();
3651 xs << html::EndTag("tr");
3656 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3660 if (is_long_tabular) {
3661 // we'll wrap it in a div, so as to deal with alignment
3663 switch (longtabular_alignment) {
3664 case LYX_LONGTABULAR_ALIGN_LEFT:
3667 case LYX_LONGTABULAR_ALIGN_CENTER:
3670 case LYX_LONGTABULAR_ALIGN_RIGHT:
3674 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3676 // The caption flag wins over head/foot
3677 if (haveLTCaption()) {
3678 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3680 for (row_type r = 0; r < nrows(); ++r)
3681 if (row_info[r].caption)
3682 ret += xhtmlRow(xs, r, runparams);
3683 xs << html::EndTag("div") << html::CR();
3687 xs << html::StartTag("table") << html::CR();
3689 // output header info
3690 bool const havefirsthead = haveLTFirstHead(false);
3691 // if we have a first head, then we are going to ignore the
3692 // headers for the additional pages, since there aren't any
3693 // in XHTML. this test accomplishes that.
3694 bool const havehead = !havefirsthead && haveLTHead(false);
3695 if (havehead || havefirsthead) {
3696 xs << html::StartTag("thead") << html::CR();
3697 for (row_type r = 0; r < nrows(); ++r) {
3698 if (((havefirsthead && row_info[r].endfirsthead) ||
3699 (havehead && row_info[r].endhead)) &&
3700 !row_info[r].caption) {
3701 ret += xhtmlRow(xs, r, runparams, true);
3704 xs << html::EndTag("thead") << html::CR();
3706 // output footer info
3707 bool const havelastfoot = haveLTLastFoot(false);
3709 bool const havefoot = !havelastfoot && haveLTFoot(false);
3710 if (havefoot || havelastfoot) {
3711 xs << html::StartTag("tfoot") << html::CR();
3712 for (row_type r = 0; r < nrows(); ++r) {
3713 if (((havelastfoot && row_info[r].endlastfoot) ||
3714 (havefoot && row_info[r].endfoot)) &&
3715 !row_info[r].caption) {
3716 ret += xhtmlRow(xs, r, runparams);
3719 xs << html::EndTag("tfoot") << html::CR();
3722 xs << html::StartTag("tbody") << html::CR();
3723 for (row_type r = 0; r < nrows(); ++r) {
3724 if (isValidRow(r)) {
3725 ret += xhtmlRow(xs, r, runparams);
3728 xs << html::EndTag("tbody")
3730 << html::EndTag("table")
3732 if (is_long_tabular)
3733 xs << html::EndTag("div") << html::CR();
3738 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3739 vector<unsigned int> const & clen) const
3741 idx_type const fcell = getFirstCellInRow(row);
3742 idx_type const n = numberOfCellsInRow(row) + fcell;
3745 for (idx_type i = fcell; i < n; ++i) {
3755 for (idx_type i = fcell; i < n; ++i) {
3766 col_type column = cellColumn(i);
3767 int len = clen[column];
3768 while (column < ncols() - 1
3769 && isPartOfMultiColumn(row, ++column))
3770 len += clen[column] + 4;
3771 os << docstring(len, ch);
3786 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3787 vector<unsigned int> const & clen) const
3789 idx_type const fcell = getFirstCellInRow(row);
3790 idx_type const n = numberOfCellsInRow(row) + fcell;
3793 for (idx_type i = fcell; i < n; ++i) {
3794 if (bottomLine(i)) {
3803 for (idx_type i = fcell; i < n; ++i) {
3804 if (bottomLine(i)) {
3814 col_type column = cellColumn(i);
3815 int len = clen[column];
3816 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3817 len += clen[column] + 4;
3818 os << docstring(len, ch);
3819 if (bottomLine(i)) {
3833 void Tabular::plaintextPrintCell(odocstringstream & os,
3834 OutputParams const & runparams,
3835 idx_type cell, row_type row, col_type column,
3836 vector<unsigned int> const & clen,
3837 bool onlydata, size_t max_length) const
3839 odocstringstream sstr;
3840 cellInset(cell)->plaintext(sstr, runparams, max_length);
3852 unsigned int len1 = sstr.str().length();
3853 unsigned int len2 = clen[column];
3854 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3855 len2 += clen[column] + 4;
3858 switch (getAlignment(cell)) {
3860 case LYX_ALIGN_LEFT:
3863 case LYX_ALIGN_RIGHT:
3867 case LYX_ALIGN_CENTER:
3873 os << docstring(len1, ' ') << sstr.str()
3874 << docstring(len2, ' ');
3876 if (rightLine(cell))
3883 void Tabular::plaintext(odocstringstream & os,
3884 OutputParams const & runparams, int const depth,
3885 bool onlydata, char_type delim, size_t max_length) const
3887 // first calculate the width of the single columns
3888 vector<unsigned int> clen(ncols());
3891 // first all non multicolumn cells!
3892 for (col_type c = 0; c < ncols(); ++c) {
3894 for (row_type r = 0; r < nrows(); ++r) {
3895 idx_type cell = cellIndex(r, c);
3896 if (isMultiColumn(cell))
3898 odocstringstream sstr;
3899 cellInset(cell)->plaintext(sstr, runparams, max_length);
3900 if (clen[c] < sstr.str().length())
3901 clen[c] = sstr.str().length();
3904 // then all multicolumn cells!
3905 for (col_type c = 0; c < ncols(); ++c) {
3906 for (row_type r = 0; r < nrows(); ++r) {
3907 idx_type cell = cellIndex(r, c);
3908 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3910 odocstringstream sstr;
3911 cellInset(cell)->plaintext(sstr, runparams, max_length);
3912 int len = int(sstr.str().length());
3913 idx_type const n = columnSpan(cell);
3914 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3916 if (len > int(clen[c + n - 1]))
3917 clen[c + n - 1] = len;
3922 for (row_type r = 0; r < nrows(); ++r) {
3923 if (!onlydata && plaintextTopHLine(os, r, clen))
3924 os << docstring(depth * 2, ' ');
3925 for (col_type c = 0; c < ncols(); ++c) {
3926 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3928 if (onlydata && c > 0)
3929 // we don't use operator<< for single UCS4 character.
3930 // see explanation in docstream.h
3932 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3934 if (os.str().size() > max_length)
3939 os << docstring(depth * 2, ' ');
3940 if (plaintextBottomHLine(os, r, clen))
3941 os << docstring(depth * 2, ' ');
3943 if (os.str().size() > max_length)
3949 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3951 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3955 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3957 return cell_info[row][column].inset;
3961 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3963 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3967 void Tabular::setCellInset(row_type row, col_type column,
3968 shared_ptr<InsetTableCell> ins)
3970 CellData & cd = cell_info[row][column];
3975 void Tabular::validate(LaTeXFeatures & features) const
3977 features.require("NeedTabularnewline");
3979 features.require("booktabs");
3980 if (is_long_tabular && !hasVarwidthColumn()) {
3981 if (tabular_width.zero())
3982 features.require("longtable");
3984 features.require("xltabular");
3986 if (rotate && is_long_tabular)
3987 features.require("lscape");
3989 features.require("rotating");
3990 if (hasVarwidthColumn()) {
3991 if (is_long_tabular)
3992 features.require("xltabular");
3994 features.require("tabularx");
3996 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3997 if (isMultiRow(cell))
3998 features.require("multirow");
3999 if (getUsebox(cell) == BOX_VARWIDTH)
4000 features.require("varwidth");
4001 if (getVAlignment(cell) != LYX_VALIGN_TOP
4002 || !getPWidth(cell).zero()
4003 || isVTypeColumn(cellColumn(cell)))
4004 features.require("array");
4005 // Tell footnote that we need a savenote
4006 // environment in non-long tables or
4007 // longtable headers/footers
4008 else if (!is_long_tabular && !features.inFloat())
4009 features.saveNoteEnv("tabular");
4010 else if (!isValidRow(cellRow(cell)))
4011 features.saveNoteEnv("longtable");
4013 cellInset(cell)->validate(features);
4014 features.saveNoteEnv(string());
4019 Tabular::BoxType Tabular::useBox(idx_type cell) const
4021 ParagraphList const & parlist = cellInset(cell)->paragraphs();
4022 if (parlist.size() > 1)
4023 return BOX_VARWIDTH;
4025 ParagraphList::const_iterator cit = parlist.begin();
4026 ParagraphList::const_iterator end = parlist.end();
4028 for (; cit != end; ++cit)
4029 for (int i = 0; i < cit->size(); ++i)
4030 if (cit->isNewline(i) || cit->layout().isEnvironment())
4031 return BOX_VARWIDTH;
4037 /////////////////////////////////////////////////////////////////////
4041 /////////////////////////////////////////////////////////////////////
4043 InsetTableCell::InsetTableCell(Buffer * buf)
4044 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
4045 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
4049 bool InsetTableCell::forcePlainLayout(idx_type) const
4051 return isMultiRow || (isMultiColumn && !isFixedWidth);
4055 bool InsetTableCell::allowParagraphCustomization(idx_type) const
4057 return isFixedWidth;
4061 bool InsetTableCell::forceLocalFontSwitch() const
4067 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
4068 FuncStatus & status) const
4070 bool enabled = true;
4071 switch (cmd.action()) {
4072 case LFUN_INSET_DISSOLVE:
4075 case LFUN_MATH_DISPLAY:
4076 if (!hasFixedWidth()) {
4081 return InsetText::getStatus(cur, cmd, status);
4083 status.setEnabled(enabled);
4087 docstring InsetTableCell::asString(bool intoInsets)
4090 if (paragraphs().empty())
4092 ParagraphList::const_iterator it = paragraphs().begin();
4093 ParagraphList::const_iterator en = paragraphs().end();
4095 for (; it != en; ++it) {
4100 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
4106 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
4107 UpdateType utype, TocBackend & backend) const
4109 InsetText::iterateForToc(di, output_active, utype, backend);
4113 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4116 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
4117 return InsetText::xhtml(xs, rp);
4121 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
4123 TextMetrics & tm = mi.base.bv->textMetrics(&text());
4125 // Hand font through to contained lyxtext:
4126 tm.font_.fontInfo() = mi.base.font;
4127 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
4129 // This can happen when a layout has a left and right margin,
4130 // and the view is made very narrow. We can't do better than
4131 // to draw it partly out of view (bug 5890).
4132 if (mi.base.textwidth < 1)
4133 mi.base.textwidth = 1;
4135 // We tell metrics here not to expand on multiple pars
4136 // This is the difference to InsetText::Metrics
4137 if (hasFixedWidth())
4138 tm.metrics(mi, dim, mi.base.textwidth, false);
4140 tm.metrics(mi, dim, 0, false);
4141 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
4142 dim.asc += TEXT_TO_INSET_OFFSET;
4143 dim.des += TEXT_TO_INSET_OFFSET;
4144 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
4149 /////////////////////////////////////////////////////////////////////
4153 /////////////////////////////////////////////////////////////////////
4155 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
4157 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
4158 rowselect_(false), colselect_(false)
4163 InsetTabular::InsetTabular(InsetTabular const & tab)
4164 : Inset(tab), tabular(tab.tabular),
4165 rowselect_(false), colselect_(false)
4170 InsetTabular::~InsetTabular()
4172 hideDialogs("tabular", this);
4176 void InsetTabular::setBuffer(Buffer & buf)
4178 tabular.setBuffer(buf);
4179 Inset::setBuffer(buf);
4183 bool InsetTabular::insetAllowed(InsetCode code) const
4188 case MATHMACRO_CODE:
4193 return tabular.is_long_tabular;
4201 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
4203 return tabular.is_long_tabular &&
4204 (newtype == "Standard" || newtype == "Unnumbered");
4208 void InsetTabular::write(ostream & os) const
4210 os << "Tabular" << endl;
4215 string InsetTabular::contextMenu(BufferView const &, int, int) const
4217 // FIXME: depending on the selection state,
4218 // we could offer a different menu.
4219 return cell(0)->contextMenuName() + ";" + contextMenuName();
4223 string InsetTabular::contextMenuName() const
4225 return "context-tabular";
4229 void InsetTabular::read(Lexer & lex)
4231 //bool const old_format = (lex.getString() == "\\LyXTable");
4239 string token = lex.getString();
4240 while (lex && token != "\\end_inset") {
4242 token = lex.getString();
4245 lex.printError("Missing \\end_inset at this point. ");
4249 int InsetTabular::rowFromY(Cursor & cur, int y) const
4251 // top y coordinate of tabular
4252 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4254 for (; r < tabular.nrows() && y > h; ++r)
4255 h += tabular.rowAscent(r) + tabular.rowDescent(r)
4256 + tabular.interRowSpace(r);
4262 int InsetTabular::columnFromX(Cursor & cur, int x) const
4264 // left x coordinate of tabular
4265 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
4267 for (; c < tabular.ncols() && x > w; ++c)
4268 w += tabular.cellWidth(c);
4273 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
4275 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
4276 // mi.base.textwidth << "\n";
4277 LBUFERR(mi.base.bv);
4279 for (row_type r = 0; r < tabular.nrows(); ++r) {
4282 for (col_type c = 0; c < tabular.ncols(); ++c) {
4283 if (tabular.isPartOfMultiColumn(r, c)
4284 || tabular.isPartOfMultiRow(r, c))
4285 // multicolumn or multirow cell, but not first one
4287 idx_type const cell = tabular.cellIndex(r, c);
4290 Length const p_width = tabular.getPWidth(cell);
4291 if (!p_width.zero())
4292 m.base.textwidth = mi.base.inPixels(p_width);
4293 else if (tabular.column_info[c].varwidth)
4294 m.base.textwidth = tabular.column_info[c].width;
4295 tabular.cellInset(cell)->metrics(m, dim0);
4296 if (!p_width.zero() || tabular.column_info[c].varwidth)
4297 dim0.wid = m.base.textwidth;
4298 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4299 + tabular.interColumnSpace(cell);
4301 // FIXME(?): do we need a second metrics call?
4302 TextMetrics const & tm =
4303 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4305 // determine horizontal offset because of decimal align (if necessary)
4306 int decimal_width = 0;
4307 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4308 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4309 tail.setBuffer(tabular.buffer());
4310 // we need to set macrocontext position everywhere
4311 // otherwise we crash with nested insets (e.g. footnotes)
4312 // after decimal point
4313 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4315 dit.push_back(CursorSlice(tail));
4316 tail.setMacrocontextPositionRecursive(dit);
4318 // remove text leading decimal point
4319 docstring const align_d = tabular.column_info[c].decimal_point;
4320 dit = separatorPos(&tail, align_d);
4322 pit_type const psize = tail.paragraphs().front().size();
4324 tail.paragraphs().front().eraseChars(0,
4325 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4327 tail.metrics(m, dim1);
4328 decimal_width = dim1.width();
4331 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4332 tabular.cell_info[r][c].decimal_width = decimal_width;
4334 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4335 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
4336 int const lastpardes = tm.last().second->descent()
4337 + TEXT_TO_INSET_OFFSET;
4339 switch (tabular.getVAlignment(cell)) {
4340 case Tabular::LYX_VALIGN_TOP:
4342 case Tabular::LYX_VALIGN_MIDDLE:
4343 offset = -(dim0.des - lastpardes)/2;
4345 case Tabular::LYX_VALIGN_BOTTOM:
4346 offset = -(dim0.des - lastpardes);
4349 tabular.cell_info[r][c].voffset = offset;
4350 maxasc = max(maxasc, dim0.asc - offset);
4351 maxdes = max(maxdes, dim0.des + offset);
4353 int const top_space = tabular.row_info[r].top_space_default ?
4354 default_line_space :
4355 mi.base.inPixels(tabular.row_info[r].top_space);
4356 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4357 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4358 default_line_space :
4359 mi.base.inPixels(tabular.row_info[r].bottom_space);
4360 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4363 // We need to recalculate the metrics after column width calculation
4364 // with xtabular (possibly multiple times, so the call is recursive).
4365 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4367 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4368 dim.des = tabular.height() - dim.asc;
4369 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4373 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4375 if (&cur.inset() == this && cur.selection()) {
4376 if (cur.selIsMultiCell()) {
4379 getSelection(cur, rs, re, cs, ce);
4381 idx_type const cell = tabular.cellIndex(row, col);
4382 col_type const cspan = tabular.columnSpan(cell);
4383 row_type const rspan = tabular.rowSpan(cell);
4384 if (col + cspan - 1 >= cs && col <= ce
4385 && row + rspan - 1 >= rs && row <= re)
4388 if (col == tabular.cellColumn(cur.idx())
4389 && row == tabular.cellRow(cur.idx())) {
4390 CursorSlice const & beg = cur.selBegin();
4391 CursorSlice const & end = cur.selEnd();
4393 if ((end.lastpos() > 0 || end.lastpit() > 0)
4394 && end.pos() == end.lastpos() && beg.pos() == 0
4395 && end.pit() == end.lastpit() && beg.pit() == 0)
4403 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4405 x += ADD_TO_TABULAR_WIDTH;
4407 BufferView * bv = pi.base.bv;
4408 Cursor & cur = pi.base.bv->cursor();
4410 // FIXME: As the full background is painted in drawBackground(),
4411 // we have no choice but to do a full repaint for the Text cells.
4412 pi.full_repaint = true;
4414 bool const original_selection_state = pi.selected;
4418 int yy = y + tabular.offsetVAlignment();
4419 for (row_type r = 0; r < tabular.nrows(); ++r) {
4421 for (col_type c = 0; c < tabular.ncols(); ++c) {
4422 if (tabular.isPartOfMultiColumn(r, c))
4425 idx = tabular.cellIndex(r, c);
4427 if (tabular.isPartOfMultiRow(r, c)) {
4428 nx += tabular.cellWidth(idx);
4432 pi.selected |= isCellSelected(cur, r, c);
4433 int const cx = nx + tabular.textHOffset(idx);
4434 int const cy = yy + tabular.textVOffset(idx);
4435 // Cache the Inset position.
4436 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4437 cell(idx)->draw(pi, cx, cy);
4438 drawCellLines(pi, nx, yy, r, idx);
4439 nx += tabular.cellWidth(idx);
4440 pi.selected = original_selection_state;
4443 if (r + 1 < tabular.nrows())
4444 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4445 + tabular.interRowSpace(r + 1);
4450 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4452 x += ADD_TO_TABULAR_WIDTH;
4453 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4454 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4455 pi.backgroundColor(this));
4459 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4461 Cursor & cur = pi.base.bv->cursor();
4463 x += ADD_TO_TABULAR_WIDTH;
4465 if (!cur.selection())
4467 if (&cur.inset() != this)
4472 bool const full_cell_selected = isCellSelected(cur,
4473 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4475 if (cur.selIsMultiCell() || full_cell_selected) {
4476 for (row_type r = 0; r < tabular.nrows(); ++r) {
4478 for (col_type c = 0; c < tabular.ncols(); ++c) {
4479 if (tabular.isPartOfMultiColumn(r, c))
4482 idx_type const cell = tabular.cellIndex(r, c);
4484 if (tabular.isPartOfMultiRow(r, c)) {
4485 xx += tabular.cellWidth(cell);
4488 int const w = tabular.cellWidth(cell);
4489 int const h = tabular.cellHeight(cell);
4490 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4491 if (isCellSelected(cur, r, c))
4492 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4495 if (r + 1 < tabular.nrows())
4496 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4497 + tabular.interRowSpace(r + 1);
4501 // FIXME: This code has no effect because InsetTableCell does not handle
4502 // drawSelection other than the trivial implementation in Inset.
4504 // x += cellXPos(cur.idx());
4505 // x += tabular.textHOffset(cur.idx());
4506 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4513 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2, int lt, int rt,
4514 Color const incol, bool drawline, bool heavy = false)
4516 Color const col = drawline ? incol : Color_tabularonoffline;
4517 if (drawline && lt > 0)
4518 pi.pain.line(x1, y1, x1 + lt, y2, pi.textColor(Color_tabularonoffline),
4519 Painter::line_onoffdash,
4520 Painter::thin_line);
4521 pi.pain.line(x1 + lt, y1, x2 - rt, y2, pi.textColor(col),
4522 drawline ? Painter::line_solid : Painter::line_onoffdash,
4523 (heavy ? 2 : 1) * Painter::thin_line);
4524 if (drawline && rt > 0)
4525 pi.pain.line(x2 - rt, y1, x2, y2, pi.textColor(Color_tabularonoffline),
4526 Painter::line_onoffdash,
4527 Painter::thin_line);
4533 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4534 row_type row, idx_type cell) const
4536 y -= tabular.rowAscent(row);
4537 int const w = tabular.cellWidth(cell);
4538 int const h = tabular.cellHeight(cell);
4542 col_type const col = tabular.cellColumn(cell);
4544 // Colour the frame if rows/columns are added or deleted
4545 Color colour = Color_tabularline;
4546 if (tabular.column_info[col].change.changed()
4547 || tabular.row_info[row].change.changed())
4548 colour = InsetTableCell(*tabular.cellInset(cell)).paragraphs().front().lookupChange(0).color();
4551 bool drawline = tabular.topLine(cell)
4552 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4553 bool heavy = tabular.use_booktabs
4554 && (row == 0 || (tabular.is_long_tabular && row == 1 && tabular.ltCaption(0)))
4555 && tabular.rowTopLine(row);
4556 if (tabular.topLineTrim(cell).first
4557 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).first))
4559 if (tabular.topLineTrim(cell).second
4560 || (row > 0 && tabular.bottomLineTrim(tabular.cellIndex(row - 1, col)).second))
4562 tabline(pi, x, y, x + w, y, lt, rt, colour, drawline, heavy);
4566 drawline = tabular.bottomLine(cell);
4567 row_type const lastrow = tabular.nrows() - 1;
4568 // Consider multi-rows
4570 while (r < lastrow && tabular.isMultiRow(tabular.cellIndex(r, col))
4571 && tabular.isPartOfMultiRow(r + 1, col))
4573 heavy = tabular.use_booktabs
4574 && ((row == lastrow && tabular.rowBottomLine(row))
4575 || (r == lastrow && tabular.rowBottomLine(r)));
4576 if (tabular.bottomLineTrim(cell).first)
4578 if (tabular.bottomLineTrim(cell).second)
4580 tabline(pi, x, y + h, x + w, y + h, lt, rt, colour, drawline, heavy);
4583 drawline = tabular.leftLine(cell)
4584 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4585 tabline(pi, x, y, x, y + h, 0, 0, colour, drawline);
4588 x -= tabular.interColumnSpace(cell);
4589 col_type next_cell_col = col + 1;
4590 while (next_cell_col < tabular.ncols()
4591 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4593 drawline = tabular.rightLine(cell)
4594 || (next_cell_col < tabular.ncols()
4595 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4596 tabline(pi, x + w, y, x + w, y + h, 0, 0, colour, drawline);
4600 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4602 //lyxerr << "InsetTabular::edit: " << this << endl;
4606 if (isRightToLeft(cur))
4607 cur.idx() = tabular.getLastCellInRow(0);
4613 if (isRightToLeft(cur))
4614 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4616 cur.idx() = tabular.numberofcells - 1;
4618 cur.pos() = cur.lastpos(); // FIXME crude guess
4620 cur.setCurrentFont();
4621 // FIXME: this accesses the position cache before it is initialized
4622 //cur.bv().fitCursor();
4626 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
4628 // In a longtable, tell captions what the current float is
4629 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4630 string const saveflt = cnts.current_float();
4631 if (tabular.is_long_tabular) {
4632 cnts.current_float("table");
4633 // in longtables, we only step the counter once
4634 cnts.step(from_ascii("table"), utype);
4635 cnts.isLongtable(true);
4638 ParIterator it2 = it;
4640 size_t const end = it2.nargs();
4641 for ( ; it2.idx() < end; it2.top().forwardIdx())
4642 buffer().updateBuffer(it2, utype);
4645 if (tabular.is_long_tabular) {
4646 cnts.current_float(saveflt);
4647 cnts.isLongtable(false);
4652 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4653 UpdateType utype, TocBackend & backend) const
4655 DocIterator dit = cpit;
4657 size_t const end = dit.nargs();
4658 for ( ; dit.idx() < end; dit.top().forwardIdx())
4659 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4663 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4665 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4666 return x < x0 || x > x0 + tabular.width();
4670 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4672 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4673 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4674 // there is no margin added vertically to tabular insets.
4675 // However, it works for now.
4676 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4680 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4682 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4686 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4688 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4689 << "\n cur:" << cur);
4690 CursorSlice sl = cur.top();
4691 Cursor & bvcur = cur.bv().cursor();
4693 FuncCode const act = cmd.action();
4697 case LFUN_MOUSE_PRESS: {
4698 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4700 if (hitSelectRow(cur.bv(), cmd.x())) {
4701 row_type r = rowFromY(cur, cmd.y());
4702 cur.idx() = tabular.getFirstCellInRow(r);
4706 cur.idx() = tabular.getLastCellInRow(r);
4707 cur.pit() = cur.lastpit();
4708 cur.pos() = cur.lastpos();
4709 cur.selection(true);
4715 if (hitSelectColumn(cur.bv(), cmd.y())) {
4716 col_type c = columnFromX(cur, cmd.x());
4717 cur.idx() = tabular.cellIndex(0, c);
4721 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4722 cur.pit() = cur.lastpit();
4723 cur.pos() = cur.lastpos();
4724 cur.selection(true);
4729 // do not reset cursor/selection if we have selected
4730 // some cells (bug 2715).
4731 if (cmd.button() == mouse_button::button3
4732 && &bvcur.selBegin().inset() == this
4733 && bvcur.selIsMultiCell())
4736 // Let InsetTableCell do it
4737 cell(cur.idx())->dispatch(cur, cmd);
4740 case LFUN_MOUSE_MOTION:
4741 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4742 if (cmd.button() == mouse_button::button1) {
4743 // only accept motions to places not deeper nested than the real anchor
4744 if (!bvcur.realAnchor().hasPart(cur)) {
4748 // select (additional) row
4750 row_type r = rowFromY(cur, cmd.y());
4751 cur.idx() = tabular.getLastCellInRow(r);
4752 // we need to reset the cursor's pit and pos now, as the old ones
4753 // may no longer be valid.
4756 bvcur.setCursor(cur);
4757 bvcur.selection(true);
4760 // select (additional) column
4762 col_type c = columnFromX(cur, cmd.x());
4763 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4764 // we need to reset the cursor's pit and pos now, as the old ones
4765 // may no longer be valid.
4768 bvcur.setCursor(cur);
4769 bvcur.selection(true);
4772 // only update if selection changes
4773 if (bvcur.idx() == cur.idx() &&
4774 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4775 cur.noScreenUpdate();
4776 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4777 bvcur.setCursor(cur);
4778 bvcur.selection(true);
4779 // if this is a multicell selection, we just set the cursor to
4780 // the beginning of the cell's text.
4781 if (bvcur.selIsMultiCell()) {
4782 bvcur.pit() = bvcur.lastpit();
4783 bvcur.pos() = bvcur.lastpos();
4788 case LFUN_MOUSE_RELEASE:
4793 case LFUN_CELL_BACKWARD:
4795 cur.selection(false);
4798 case LFUN_CELL_FORWARD:
4800 cur.selection(false);
4803 case LFUN_CHAR_FORWARD_SELECT:
4804 case LFUN_CHAR_FORWARD:
4805 case LFUN_CHAR_BACKWARD_SELECT:
4806 case LFUN_CHAR_BACKWARD:
4807 case LFUN_CHAR_RIGHT_SELECT:
4808 case LFUN_CHAR_RIGHT:
4809 case LFUN_CHAR_LEFT_SELECT:
4810 case LFUN_CHAR_LEFT:
4811 case LFUN_WORD_FORWARD:
4812 case LFUN_WORD_FORWARD_SELECT:
4813 case LFUN_WORD_BACKWARD:
4814 case LFUN_WORD_BACKWARD_SELECT:
4815 case LFUN_WORD_RIGHT:
4816 case LFUN_WORD_RIGHT_SELECT:
4817 case LFUN_WORD_LEFT:
4818 case LFUN_WORD_LEFT_SELECT: {
4819 // determine whether we move to next or previous cell, where to enter
4820 // the new cell from, and which command to "finish" (i.e., exit the
4823 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4824 FuncCode finish_lfun;
4826 if (act == LFUN_CHAR_FORWARD
4827 || act == LFUN_CHAR_FORWARD_SELECT
4828 || act == LFUN_WORD_FORWARD
4829 || act == LFUN_WORD_FORWARD_SELECT) {
4831 finish_lfun = LFUN_FINISHED_FORWARD;
4833 else if (act == LFUN_CHAR_BACKWARD
4834 || act == LFUN_CHAR_BACKWARD_SELECT
4835 || act == LFUN_WORD_BACKWARD
4836 || act == LFUN_WORD_BACKWARD_SELECT) {
4838 finish_lfun = LFUN_FINISHED_BACKWARD;
4840 // LEFT or RIGHT commands --- the interpretation will depend on the
4841 // table's direction.
4843 bool const right = act == LFUN_CHAR_RIGHT
4844 || act == LFUN_CHAR_RIGHT_SELECT
4845 || act == LFUN_WORD_RIGHT
4846 || act == LFUN_WORD_RIGHT_SELECT;
4847 next_cell = isRightToLeft(cur) != right;
4849 if (lyxrc.visual_cursor)
4850 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4852 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4855 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4856 || act == LFUN_CHAR_BACKWARD_SELECT
4857 || act == LFUN_CHAR_RIGHT_SELECT
4858 || act == LFUN_CHAR_LEFT_SELECT
4859 || act == LFUN_WORD_FORWARD_SELECT
4860 || act == LFUN_WORD_RIGHT_SELECT
4861 || act == LFUN_WORD_BACKWARD_SELECT
4862 || act == LFUN_WORD_LEFT_SELECT;
4864 // If we have a multicell selection or we're
4865 // not doing some LFUN_*_SELECT thing anyway...
4866 if (!cur.selIsMultiCell() || !select) {
4867 col_type const c = tabular.cellColumn(cur.idx());
4868 row_type const r = tabular.cellRow(cur.idx());
4869 // Are we trying to select the whole cell and is the whole cell
4870 // not yet selected?
4871 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4872 ((next_cell && cur.pit() == cur.lastpit()
4873 && cur.pos() == cur.lastpos())
4874 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4876 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4878 // ...try to dispatch to the cell's inset.
4879 cell(cur.idx())->dispatch(cur, cmd);
4881 // When we already have a selection we want to select the whole cell
4882 // before going to the next cell.
4883 if (select_whole && !empty_cell){
4884 getText(cur.idx())->selectAll(cur);
4886 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4890 // FIXME: When we support the selection of an empty cell, remove
4891 // the !empty_cell from this condition. For now we jump to the next
4892 // cell if the current cell is empty.
4893 if (cur.result().dispatched() && !empty_cell)
4897 // move to next/prev cell, as appropriate
4898 // note that we will always do this if we're selecting and we have
4899 // a multicell selection
4900 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4901 << " cell from: " << int(entry_from));
4903 moveNextCell(cur, entry_from);
4905 movePrevCell(cur, entry_from);
4906 // if we're exiting the table, call the appropriate FINISHED lfun
4907 if (sl == cur.top()) {
4908 cmd = FuncRequest(finish_lfun);
4913 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4918 case LFUN_DOWN_SELECT:
4920 if (!(cur.selection() && cur.selIsMultiCell()))
4921 cell(cur.idx())->dispatch(cur, cmd);
4923 cur.dispatched(); // override the cell's decision
4924 if (sl == cur.top()) {
4925 // if our Text didn't do anything to the cursor
4926 // then we try to put the cursor into the cell below
4927 // setting also the right targetX.
4928 cur.selHandle(act == LFUN_DOWN_SELECT);
4929 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4930 int const xtarget = cur.targetX();
4931 // WARNING: Once cur.idx() has been reset, the cursor is in
4932 // an inconsistent state until pos() has been set. Be careful
4933 // what you do with it!
4934 cur.idx() = tabular.cellBelow(cur.idx());
4936 TextMetrics const & tm =
4937 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4938 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4939 cur.setCurrentFont();
4942 if (sl == cur.top()) {
4943 // we trick it to go to forward after leaving the
4945 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4948 if (cur.selIsMultiCell()) {
4949 cur.pit() = cur.lastpit();
4950 cur.pos() = cur.lastpos();
4951 cur.setCurrentFont();
4952 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4955 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4958 case LFUN_UP_SELECT:
4960 if (!(cur.selection() && cur.selIsMultiCell()))
4961 cell(cur.idx())->dispatch(cur, cmd);
4962 cur.dispatched(); // override the cell's decision
4963 if (sl == cur.top()) {
4964 // if our Text didn't do anything to the cursor
4965 // then we try to put the cursor into the cell above
4966 // setting also the right targetX.
4967 cur.selHandle(act == LFUN_UP_SELECT);
4968 if (tabular.cellRow(cur.idx()) != 0) {
4969 int const xtarget = cur.targetX();
4970 // WARNING: Once cur.idx() has been reset, the cursor is in
4971 // an inconsistent state until pos() has been set. Be careful
4972 // what you do with it!
4973 cur.idx() = tabular.cellAbove(cur.idx());
4974 cur.pit() = cur.lastpit();
4975 Text const * text = cell(cur.idx())->getText(0);
4976 TextMetrics const & tm = cur.bv().textMetrics(text);
4977 ParagraphMetrics const & pm =
4978 tm.parMetrics(cur.lastpit());
4979 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4980 cur.setCurrentFont();
4983 if (sl == cur.top()) {
4984 cmd = FuncRequest(LFUN_UP);
4987 if (cur.selIsMultiCell()) {
4989 cur.pos() = cur.lastpos();
4990 cur.setCurrentFont();
4991 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4994 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4997 case LFUN_LAYOUT_TABULAR:
4998 cur.bv().showDialog("tabular");
5001 case LFUN_INSET_MODIFY:
5002 // we come from the dialog
5003 if (cmd.getArg(0) == "tabular")
5004 tabularFeatures(cur, cmd.getLongArg(1));
5009 case LFUN_TABULAR_FEATURE:
5010 tabularFeatures(cur, to_utf8(cmd.argument()));
5013 // insert file functions
5014 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
5015 case LFUN_FILE_INSERT_PLAINTEXT:
5017 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
5018 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
5019 FileName(to_utf8(cmd.argument())));
5022 cur.recordUndoInset();
5023 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
5024 // content has been replaced,
5025 // so cursor might be invalid
5026 cur.pos() = cur.lastpos();
5027 cur.pit() = cur.lastpit();
5028 bvcur.setCursor(cur);
5035 if (cur.selIsMultiCell()) {
5036 if (copySelection(cur)) {
5037 cur.recordUndoInset();
5041 cell(cur.idx())->dispatch(cur, cmd);
5044 case LFUN_SELF_INSERT:
5045 if (cur.selIsMultiCell()) {
5046 cur.recordUndoInset();
5048 BufferView * bv = &cur.bv();
5049 docstring::const_iterator cit = cmd.argument().begin();
5050 docstring::const_iterator const end = cmd.argument().end();
5051 for (; cit != end; ++cit)
5052 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
5055 bv->bookmarkEditPosition();
5057 cell(cur.idx())->dispatch(cur, cmd);
5060 case LFUN_CHAR_DELETE_BACKWARD:
5061 case LFUN_CHAR_DELETE_FORWARD:
5062 if (cur.selIsMultiCell()) {
5063 cur.recordUndoInset();
5066 cell(cur.idx())->dispatch(cur, cmd);
5070 if (!cur.selection())
5072 if (cur.selIsMultiCell()) {
5076 cell(cur.idx())->dispatch(cur, cmd);
5079 case LFUN_CLIPBOARD_PASTE:
5080 case LFUN_PRIMARY_SELECTION_PASTE: {
5081 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
5082 theClipboard().getAsText(Clipboard::PlainTextType) :
5083 theSelection().get();
5086 // pass to InsertPlaintextString, but
5087 // only if we have multi-cell content
5088 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5089 cur.recordUndoInset();
5090 if (insertPlaintextString(cur.bv(), clip, false)) {
5091 // content has been replaced,
5092 // so cursor might be invalid
5093 cur.pos() = cur.lastpos();
5094 cur.pit() = cur.lastpit();
5095 bvcur.setCursor(cur);
5099 // Let the cell handle normal text
5100 cell(cur.idx())->dispatch(cur, cmd);
5105 if (!tabularStackDirty()) {
5106 // Check if we have plain text or HTML with rows/columns.
5107 // and if so, pass over to LFUN_CLIPBOARD_PASTE
5108 if (theClipboard().hasTextContents(Clipboard::AnyTextType)
5109 && !theClipboard().hasTextContents(Clipboard::LyXTextType)) {
5110 docstring const clip =
5111 theClipboard().getAsText(Clipboard::AnyTextType);
5112 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
5113 FuncRequest ncmd = FuncRequest(LFUN_CLIPBOARD_PASTE, cmd.argument());
5114 doDispatch(cur, ncmd);
5118 if (!cur.selIsMultiCell())
5119 cell(cur.idx())->dispatch(cur, cmd);
5122 if (theClipboard().isInternal()) {
5123 cur.recordUndoInset();
5124 pasteClipboard(cur);
5128 case LFUN_CHANGE_ACCEPT:
5129 case LFUN_CHANGE_REJECT:
5130 case LFUN_FONT_EMPH:
5131 case LFUN_FONT_BOLD:
5132 case LFUN_FONT_BOLDSYMBOL:
5133 case LFUN_FONT_ROMAN:
5134 case LFUN_FONT_NOUN:
5135 case LFUN_FONT_ITAL:
5136 case LFUN_FONT_FRAK:
5137 case LFUN_FONT_TYPEWRITER:
5138 case LFUN_FONT_SANS:
5139 case LFUN_TEXTSTYLE_APPLY:
5140 case LFUN_TEXTSTYLE_UPDATE:
5141 case LFUN_FONT_SIZE:
5142 case LFUN_FONT_UNDERLINE:
5143 case LFUN_FONT_STRIKEOUT:
5144 case LFUN_FONT_CROSSOUT:
5145 case LFUN_FONT_UNDERUNDERLINE:
5146 case LFUN_FONT_UNDERWAVE:
5148 case LFUN_PARAGRAPH_PARAMS_APPLY:
5149 case LFUN_PARAGRAPH_PARAMS:
5150 case LFUN_WORD_CAPITALIZE:
5151 case LFUN_WORD_UPCASE:
5152 case LFUN_WORD_LOWCASE:
5153 case LFUN_CHARS_TRANSPOSE: {
5154 bool const ct = (act == LFUN_CHANGE_ACCEPT || act == LFUN_CHANGE_REJECT);
5155 if (cur.selIsMultiCell()) {
5158 getSelection(cur, rs, re, cs, ce);
5159 Cursor tmpcur = cur;
5160 for (row_type r = rs; r <= re; ++r) {
5161 if (ct && cs == 0 && ce == tabular.ncols() - 1) {
5162 // whole row selected
5163 if (act == LFUN_CHANGE_ACCEPT) {
5164 if (tabular.row_info[r].change.inserted())
5165 tabular.row_info[r].change.setUnchanged();
5166 else if (tabular.row_info[r].change.deleted()) {
5167 tabular.deleteRow(r, true);
5172 if (tabular.row_info[r].change.deleted())
5173 tabular.row_info[r].change.setUnchanged();
5174 else if (tabular.row_info[r].change.inserted()) {
5175 tabular.deleteRow(r, true);
5181 for (col_type c = cs; c <= ce; ++c) {
5182 if (ct && rs == 0 && re == tabular.nrows() - 1) {
5183 // whole col selected
5184 if (act == LFUN_CHANGE_ACCEPT) {
5185 if (tabular.column_info[c].change.inserted())
5186 tabular.column_info[c].change.setUnchanged();
5187 else if (tabular.column_info[c].change.deleted()) {
5188 tabular.deleteColumn(c, true);
5193 if (tabular.column_info[c].change.deleted())
5194 tabular.column_info[c].change.setUnchanged();
5195 else if (tabular.column_info[c].change.inserted()) {
5196 tabular.deleteColumn(c, true);
5202 // cursor follows cell:
5203 tmpcur.idx() = tabular.cellIndex(r, c);
5204 // select this cell only:
5207 tmpcur.resetAnchor();
5208 tmpcur.pit() = tmpcur.lastpit();
5209 tmpcur.pos() = tmpcur.top().lastpos();
5210 tmpcur.setCursor(tmpcur);
5211 tmpcur.setSelection();
5212 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
5216 tabular.updateIndexes();
5217 // cursor might be invalid
5219 // change bar might need to be redrawn
5220 cur.screenUpdateFlags(Update::Force);
5221 cur.forceBufferUpdate();
5225 cell(cur.idx())->dispatch(cur, cmd);
5230 case LFUN_CHANGE_NEXT:
5231 case LFUN_CHANGE_PREVIOUS: {
5232 // BufferView::dispatch has already moved the cursor, we just
5233 // need to select here if we have a changed row or column
5234 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5236 cur.idx() = tabular.getFirstCellInRow(tabular.cellRow(cur.idx()));
5240 cur.idx() = tabular.getLastCellInRow(tabular.cellRow(cur.idx()));
5241 cur.pit() = cur.lastpit();
5242 cur.pos() = cur.lastpos();
5243 cur.selection(true);
5247 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5249 cur.idx() = tabular.cellIndex(0, tabular.cellColumn(cur.idx()));
5253 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, tabular.cellColumn(cur.idx()));
5254 cur.pit() = cur.lastpit();
5255 cur.pos() = cur.lastpos();
5256 cur.selection(true);
5263 case LFUN_INSET_SETTINGS:
5264 // relay this lfun to Inset, not to the cell.
5265 Inset::doDispatch(cur, cmd);
5269 // we try to handle this event in the insets dispatch function.
5270 cell(cur.idx())->dispatch(cur, cmd);
5276 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
5277 string const & argument, FuncStatus & status) const
5280 int action = Tabular::LAST_ACTION;
5282 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5283 if (tabularFeature[i].feature == s) {
5284 action = tabularFeature[i].action;
5288 if (action == Tabular::LAST_ACTION) {
5290 status.setUnknown(true);
5294 row_type sel_row_start = 0;
5295 row_type sel_row_end = 0;
5296 col_type sel_col_start = 0;
5297 col_type sel_col_end = 0;
5298 Tabular::ltType dummyltt;
5301 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5304 case Tabular::SET_PWIDTH:
5305 case Tabular::SET_MPWIDTH:
5306 case Tabular::TOGGLE_VARWIDTH_COLUMN:
5307 case Tabular::SET_SPECIAL_COLUMN:
5308 case Tabular::SET_SPECIAL_MULTICOLUMN:
5309 case Tabular::APPEND_ROW:
5310 case Tabular::APPEND_COLUMN:
5311 case Tabular::DELETE_ROW:
5312 case Tabular::DELETE_COLUMN:
5313 case Tabular::COPY_ROW:
5314 case Tabular::COPY_COLUMN:
5315 case Tabular::SET_TOP_SPACE:
5316 case Tabular::SET_BOTTOM_SPACE:
5317 case Tabular::SET_INTERLINE_SPACE:
5321 case Tabular::SET_TABULAR_WIDTH:
5322 status.setEnabled(!tabular.rotate
5323 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
5326 case Tabular::MOVE_COLUMN_RIGHT:
5327 case Tabular::MOVE_COLUMN_LEFT:
5328 case Tabular::MOVE_ROW_DOWN:
5329 case Tabular::MOVE_ROW_UP: {
5330 if (cur.selection()) {
5331 status.message(_("Selections not supported."));
5332 status.setEnabled(false);
5336 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
5337 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
5338 (action == Tabular::MOVE_COLUMN_LEFT &&
5339 tabular.cellColumn(cur.idx()) == 0) ||
5340 (action == Tabular::MOVE_ROW_DOWN &&
5341 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
5342 (action == Tabular::MOVE_ROW_UP &&
5343 tabular.cellRow(cur.idx()) == 0)) {
5344 status.setEnabled(false);
5348 if (action == Tabular::MOVE_COLUMN_RIGHT ||
5349 action == Tabular::MOVE_COLUMN_LEFT) {
5350 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
5351 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
5352 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
5353 status.message(_("Multi-column in current or"
5354 " destination column."));
5355 status.setEnabled(false);
5360 if (action == Tabular::MOVE_ROW_DOWN ||
5361 action == Tabular::MOVE_ROW_UP) {
5362 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
5363 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
5364 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
5365 status.message(_("Multi-row in current or"
5366 " destination row."));
5367 status.setEnabled(false);
5372 status.setEnabled(true);
5376 case Tabular::SET_DECIMAL_POINT:
5378 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
5381 case Tabular::SET_MULTICOLUMN:
5382 case Tabular::UNSET_MULTICOLUMN:
5383 case Tabular::MULTICOLUMN:
5384 // If a row is set as longtable caption, it must not be allowed
5385 // to unset that this row is a multicolumn.
5386 status.setEnabled(sel_row_start == sel_row_end
5387 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5388 status.setOnOff(tabular.isMultiColumn(cur.idx()));
5391 case Tabular::SET_MULTIROW:
5392 case Tabular::UNSET_MULTIROW:
5393 case Tabular::MULTIROW:
5394 // If a row is set as longtable caption, it must not be allowed
5395 // to unset that this row is a multirow.
5396 status.setEnabled(sel_col_start == sel_col_end
5397 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5398 status.setOnOff(tabular.isMultiRow(cur.idx()));
5401 case Tabular::SET_ALL_LINES:
5402 case Tabular::UNSET_ALL_LINES:
5403 case Tabular::SET_INNER_LINES:
5404 case Tabular::SET_BORDER_LINES:
5405 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5408 case Tabular::RESET_FORMAL_DEFAULT:
5409 status.setEnabled(tabular.use_booktabs);
5412 case Tabular::SET_LINE_TOP:
5413 case Tabular::SET_LINE_BOTTOM:
5414 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5417 case Tabular::SET_LINE_LEFT:
5418 case Tabular::SET_LINE_RIGHT:
5419 status.setEnabled(!tabular.use_booktabs
5420 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5423 case Tabular::SET_LTRIM_TOP:
5424 case Tabular::SET_RTRIM_TOP:
5425 status.setEnabled(tabular.use_booktabs
5426 && tabular.cellRow(cur.idx()) != 0
5427 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5430 case Tabular::SET_LTRIM_BOTTOM:
5431 case Tabular::SET_RTRIM_BOTTOM:
5432 status.setEnabled(tabular.use_booktabs
5433 && tabular.cellRow(cur.idx()) != tabular.nrows() - 1
5434 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5437 case Tabular::TOGGLE_LINE_TOP:
5438 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5439 status.setOnOff(tabular.topLine(cur.idx()));
5442 case Tabular::TOGGLE_LINE_BOTTOM:
5443 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
5444 status.setOnOff(tabular.bottomLine(cur.idx()));
5447 case Tabular::TOGGLE_LINE_LEFT:
5448 status.setEnabled(!tabular.use_booktabs
5449 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5450 status.setOnOff(tabular.leftLine(cur.idx()));
5453 case Tabular::TOGGLE_LINE_RIGHT:
5454 status.setEnabled(!tabular.use_booktabs
5455 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5456 status.setOnOff(tabular.rightLine(cur.idx()));
5459 case Tabular::TOGGLE_LTRIM_TOP:
5460 status.setEnabled(tabular.use_booktabs
5461 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5462 status.setOnOff(tabular.topLineTrim(cur.idx()).first);
5465 case Tabular::TOGGLE_RTRIM_TOP:
5466 status.setEnabled(tabular.use_booktabs
5467 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5468 status.setOnOff(tabular.topLineTrim(cur.idx()).second);
5471 case Tabular::TOGGLE_LTRIM_BOTTOM:
5472 status.setEnabled(tabular.use_booktabs
5473 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5474 status.setOnOff(tabular.bottomLineTrim(cur.idx()).first);
5477 case Tabular::TOGGLE_RTRIM_BOTTOM:
5478 status.setEnabled(tabular.use_booktabs
5479 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5480 status.setOnOff(tabular.bottomLineTrim(cur.idx()).second);
5483 // multirow cells only inherit the alignment of the column if the column has
5484 // no width, otherwise they are left-aligned
5485 // therefore allow always left but right and center only if there is no width
5486 case Tabular::M_ALIGN_LEFT:
5489 case Tabular::ALIGN_LEFT:
5490 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5493 case Tabular::M_ALIGN_RIGHT:
5496 case Tabular::ALIGN_RIGHT:
5497 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5498 && !tabular.getPWidth(cur.idx()).zero()));
5499 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5502 case Tabular::M_ALIGN_CENTER:
5505 case Tabular::ALIGN_CENTER:
5506 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5507 && !tabular.getPWidth(cur.idx()).zero()));
5508 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5511 case Tabular::ALIGN_BLOCK:
5512 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5513 && !tabular.isMultiRow(cur.idx()));
5514 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5517 case Tabular::ALIGN_DECIMAL:
5518 status.setEnabled(!tabular.isMultiRow(cur.idx())
5519 && !tabular.isMultiColumn(cur.idx()));
5520 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5523 case Tabular::M_VALIGN_TOP:
5526 case Tabular::VALIGN_TOP:
5527 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5528 && !tabular.isMultiRow(cur.idx()));
5530 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5533 case Tabular::M_VALIGN_BOTTOM:
5536 case Tabular::VALIGN_BOTTOM:
5537 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5538 && !tabular.isMultiRow(cur.idx()));
5540 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5543 case Tabular::M_VALIGN_MIDDLE:
5546 case Tabular::VALIGN_MIDDLE:
5547 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5548 && !tabular.isMultiRow(cur.idx()));
5550 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5553 case Tabular::SET_LONGTABULAR:
5554 case Tabular::TOGGLE_LONGTABULAR:
5555 // setting as longtable is not allowed when table is inside a float
5556 if (cur.innerInsetOfType(FLOAT_CODE) != 0
5557 || cur.innerInsetOfType(WRAP_CODE) != 0)
5558 status.setEnabled(false);
5560 status.setEnabled(true);
5561 status.setOnOff(tabular.is_long_tabular);
5564 case Tabular::UNSET_LONGTABULAR:
5565 status.setOnOff(!tabular.is_long_tabular);
5568 case Tabular::TOGGLE_ROTATE_TABULAR:
5569 case Tabular::SET_ROTATE_TABULAR:
5570 status.setOnOff(tabular.rotate != 0);
5573 case Tabular::TABULAR_VALIGN_TOP:
5574 status.setEnabled(tabular.tabular_width.zero());
5575 status.setOnOff(tabular.tabular_valignment
5576 == Tabular::LYX_VALIGN_TOP);
5578 case Tabular::TABULAR_VALIGN_MIDDLE:
5579 status.setEnabled(tabular.tabular_width.zero());
5580 status.setOnOff(tabular.tabular_valignment
5581 == Tabular::LYX_VALIGN_MIDDLE);
5583 case Tabular::TABULAR_VALIGN_BOTTOM:
5584 status.setEnabled(tabular.tabular_width.zero());
5585 status.setOnOff(tabular.tabular_valignment
5586 == Tabular::LYX_VALIGN_BOTTOM);
5589 case Tabular::LONGTABULAR_ALIGN_LEFT:
5590 status.setOnOff(tabular.longtabular_alignment
5591 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5593 case Tabular::LONGTABULAR_ALIGN_CENTER:
5594 status.setOnOff(tabular.longtabular_alignment
5595 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5597 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5598 status.setOnOff(tabular.longtabular_alignment
5599 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5602 case Tabular::UNSET_ROTATE_TABULAR:
5603 status.setOnOff(tabular.rotate == 0);
5606 case Tabular::TOGGLE_ROTATE_CELL:
5607 case Tabular::SET_ROTATE_CELL:
5608 status.setOnOff(!oneCellHasRotationState(false,
5609 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5612 case Tabular::UNSET_ROTATE_CELL:
5613 status.setOnOff(!oneCellHasRotationState(true,
5614 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5617 case Tabular::SET_USEBOX:
5618 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5621 // every row can only be one thing:
5622 // either a footer or header
5623 case Tabular::SET_LTFIRSTHEAD:
5624 status.setEnabled(sel_row_start == sel_row_end);
5625 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5628 case Tabular::UNSET_LTFIRSTHEAD:
5629 status.setEnabled(sel_row_start == sel_row_end);
5630 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5633 case Tabular::SET_LTHEAD:
5634 status.setEnabled(sel_row_start == sel_row_end);
5635 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5638 case Tabular::UNSET_LTHEAD:
5639 status.setEnabled(sel_row_start == sel_row_end);
5640 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5643 case Tabular::SET_LTFOOT:
5644 status.setEnabled(sel_row_start == sel_row_end);
5645 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5648 case Tabular::UNSET_LTFOOT:
5649 status.setEnabled(sel_row_start == sel_row_end);
5650 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5653 case Tabular::SET_LTLASTFOOT:
5654 status.setEnabled(sel_row_start == sel_row_end);
5655 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5658 case Tabular::UNSET_LTLASTFOOT:
5659 status.setEnabled(sel_row_start == sel_row_end);
5660 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5663 case Tabular::SET_LTNEWPAGE:
5664 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5666 case Tabular::UNSET_LTNEWPAGE:
5667 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5670 // only one row in head/firsthead/foot/lasthead can be the caption
5671 // and a multirow cannot be set as caption
5672 case Tabular::SET_LTCAPTION:
5673 status.setEnabled(sel_row_start == sel_row_end
5674 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5675 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5676 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5677 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5678 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5679 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5680 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5681 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5682 && !tabular.isMultiRow(sel_row_start));
5683 status.setOnOff(tabular.ltCaption(sel_row_start));
5686 case Tabular::UNSET_LTCAPTION:
5687 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5690 case Tabular::TOGGLE_LTCAPTION:
5691 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5692 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5693 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5694 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5695 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5696 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5697 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5698 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5699 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5700 status.setOnOff(tabular.ltCaption(sel_row_start));
5703 case Tabular::TOGGLE_BOOKTABS:
5704 case Tabular::SET_BOOKTABS:
5705 status.setOnOff(tabular.use_booktabs);
5708 case Tabular::UNSET_BOOKTABS:
5709 status.setOnOff(!tabular.use_booktabs);
5714 status.setEnabled(false);
5721 // function sets an object as defined in FuncStatus.h:
5722 // states OK, Unknown, Disabled, On, Off.
5723 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5724 FuncStatus & status) const
5726 switch (cmd.action()) {
5727 case LFUN_INSET_MODIFY:
5728 if (cmd.getArg(0) != "tabular")
5730 if (cmd.getArg(1) == "for-dialog") {
5731 // The dialog is asking the status of a command
5732 if (&cur.inset() != this)
5734 string action = cmd.getArg(2);
5735 string arg = cmd.getLongArg(3);
5736 return getFeatureStatus(cur, action, arg, status);
5738 // We always enable the lfun if it is coming from the dialog
5739 // because the dialog makes sure all the settings are valid,
5740 // even though the first argument might not be valid now.
5741 status.setEnabled(true);
5745 case LFUN_TABULAR_FEATURE: {
5746 if (&cur.inset() != this)
5748 string action = cmd.getArg(0);
5749 string arg = cmd.getLongArg(1);
5750 return getFeatureStatus(cur, action, arg, status);
5753 case LFUN_CAPTION_INSERT: {
5754 // caption is only allowed in caption cell of longtable
5755 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5756 status.setEnabled(false);
5759 // only standard caption is allowed
5760 string arg = cmd.getArg(0);
5761 if (!arg.empty() && arg != "Standard") {
5762 status.setEnabled(false);
5765 // check if there is already a caption
5766 bool have_caption = false;
5767 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5768 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5769 ParagraphList::const_iterator pend = itc.paragraphs().end();
5770 for (; pit != pend; ++pit) {
5771 InsetList::const_iterator it = pit->insetList().begin();
5772 InsetList::const_iterator end = pit->insetList().end();
5773 for (; it != end; ++it) {
5774 if (it->inset->lyxCode() == CAPTION_CODE) {
5775 have_caption = true;
5780 status.setEnabled(!have_caption);
5784 // These are only enabled inside tabular
5785 case LFUN_CELL_BACKWARD:
5786 case LFUN_CELL_FORWARD:
5787 status.setEnabled(true);
5790 // disable these with multiple cells selected
5791 case LFUN_INSET_INSERT:
5792 case LFUN_TABULAR_INSERT:
5793 case LFUN_TABULAR_STYLE_INSERT:
5794 case LFUN_FLEX_INSERT:
5795 case LFUN_FLOAT_INSERT:
5796 case LFUN_FLOAT_WIDE_INSERT:
5797 case LFUN_FOOTNOTE_INSERT:
5798 case LFUN_MARGINALNOTE_INSERT:
5799 case LFUN_MATH_INSERT:
5800 case LFUN_MATH_MODE:
5801 case LFUN_MATH_MUTATE:
5802 case LFUN_MATH_DISPLAY:
5803 case LFUN_NOTE_INSERT:
5804 case LFUN_ARGUMENT_INSERT:
5805 case LFUN_BOX_INSERT:
5806 case LFUN_BRANCH_INSERT:
5807 case LFUN_PHANTOM_INSERT:
5808 case LFUN_WRAP_INSERT:
5809 case LFUN_PREVIEW_INSERT:
5810 case LFUN_ERT_INSERT: {
5811 if (cur.selIsMultiCell()) {
5812 status.setEnabled(false);
5815 return cell(cur.idx())->getStatus(cur, cmd, status);
5818 case LFUN_CHANGE_ACCEPT:
5819 case LFUN_CHANGE_REJECT: {
5820 if (cur.selIsMultiCell()) {
5823 getSelection(cur, rs, re, cs, ce);
5824 for (row_type r = rs; r <= re; ++r) {
5825 if (tabular.row_info[r].change.changed()) {
5826 status.setEnabled(true);
5829 for (col_type c = cs; c <= ce; ++c) {
5830 if (tabular.column_info[c].change.changed()) {
5831 status.setEnabled(true);
5837 if (tabular.row_info[tabular.cellRow(cur.idx())].change.changed()) {
5838 status.setEnabled(true);
5841 else if (tabular.column_info[tabular.cellColumn(cur.idx())].change.changed()) {
5842 status.setEnabled(true);
5846 return cell(cur.idx())->getStatus(cur, cmd, status);
5849 // disable in non-fixed-width cells
5850 case LFUN_PARAGRAPH_BREAK:
5851 // multirow does not allow paragraph breaks
5852 if (tabular.isMultiRow(cur.idx())) {
5853 status.setEnabled(false);
5857 case LFUN_NEWLINE_INSERT:
5858 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
5859 && tabular.getPWidth(cur.idx()).zero()) {
5860 status.setEnabled(false);
5863 return cell(cur.idx())->getStatus(cur, cmd, status);
5865 case LFUN_NEWPAGE_INSERT:
5866 status.setEnabled(false);
5870 if (tabularStackDirty() && theClipboard().isInternal()) {
5871 if (cur.selIsMultiCell()) {
5874 getSelection(cur, rs, re, cs, ce);
5875 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5876 && paste_tabular->nrows() == re - rs + 1)
5877 status.setEnabled(true);
5879 status.setEnabled(false);
5880 status.message(_("Selection size should match clipboard content."));
5883 status.setEnabled(true);
5886 return cell(cur.idx())->getStatus(cur, cmd, status);
5888 case LFUN_INSET_SETTINGS:
5889 // relay this lfun to Inset, not to the cell.
5890 return Inset::getStatus(cur, cmd, status);
5893 // we try to handle this event in the insets dispatch function.
5894 return cell(cur.idx())->getStatus(cur, cmd, status);
5900 Inset::DisplayType InsetTabular::display() const
5902 if (tabular.is_long_tabular) {
5903 switch (tabular.longtabular_alignment) {
5904 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5906 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5908 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5918 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5920 tabular.latex(os, runparams);
5924 int InsetTabular::plaintext(odocstringstream & os,
5925 OutputParams const & runparams, size_t max_length) const
5927 os << '\n'; // output table on a new line
5928 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5929 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5930 return PLAINTEXT_NEWLINE;
5934 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5939 // FIXME: Why not pass a proper DocIterator here?
5941 // if the table is inside a float it doesn't need the informaltable
5942 // wrapper. Search for it.
5943 for (master = owner(); master; master = master->owner())
5944 if (master->lyxCode() == FLOAT_CODE)
5949 os << "<informaltable>";
5952 ret += tabular.docbook(os, runparams);
5954 os << "</informaltable>";
5961 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5963 return tabular.xhtml(xs, rp);
5967 void InsetTabular::validate(LaTeXFeatures & features) const
5969 tabular.validate(features);
5970 features.useInsetLayout(getLayout());
5974 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5976 return tabular.cellInset(idx);
5980 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5982 return tabular.cellInset(idx);
5986 void InsetTabular::cursorPos(BufferView const & bv,
5987 CursorSlice const & sl, bool boundary, int & x, int & y) const
5989 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5991 // y offset correction
5992 y += cellYPos(sl.idx());
5993 y += tabular.textVOffset(sl.idx());
5994 y += tabular.offsetVAlignment();
5996 // x offset correction
5997 x += cellXPos(sl.idx());
5998 x += tabular.textHOffset(sl.idx());
5999 x += ADD_TO_TABULAR_WIDTH;
6003 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
6007 Inset const & inset = *tabular.cellInset(cell);
6008 Point o = bv.coordCache().getInsets().xy(&inset);
6009 int const xbeg = o.x_ - tabular.textHOffset(cell);
6010 int const xend = xbeg + tabular.cellWidth(cell);
6011 row_type const row = tabular.cellRow(cell);
6012 int const ybeg = o.y_ - tabular.rowAscent(row)
6013 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
6014 int const yend = ybeg + tabular.cellHeight(cell);
6026 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
6027 // << " ybeg=" << ybeg << " yend=" << yend
6028 // << " xx=" << xx << " yy=" << yy
6029 // << " dist=" << xx + yy << endl;
6034 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
6036 //lyxerr << "InsetTabular::editXY: " << this << endl;
6038 cur.idx() = getNearestCell(cur.bv(), x, y);
6039 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
6043 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
6045 cur.idx() = getNearestCell(cur.bv(), x, y);
6046 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
6050 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
6052 idx_type idx_min = 0;
6053 int dist_min = numeric_limits<int>::max();
6054 for (idx_type i = 0, n = nargs(); i != n; ++i) {
6055 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
6056 int const d = dist(bv, i, x, y);
6067 int InsetTabular::cellYPos(idx_type const cell) const
6069 row_type row = tabular.cellRow(cell);
6071 for (row_type r = 0; r < row; ++r)
6072 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
6073 + tabular.interRowSpace(r + 1);
6078 int InsetTabular::cellXPos(idx_type const cell) const
6080 col_type col = tabular.cellColumn(cell);
6082 for (col_type c = 0; c < col; ++c)
6083 lx += tabular.column_info[c].width;
6088 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
6090 row_type const row = tabular.cellRow(cur.idx());
6091 col_type const col = tabular.cellColumn(cur.idx());
6093 if (isRightToLeft(cur)) {
6094 if (tabular.cellColumn(cur.idx()) == 0) {
6095 if (row == tabular.nrows() - 1)
6097 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
6102 cur.idx() = tabular.getLastCellInRow(row - 1);
6104 cur.idx() = tabular.cellIndex(row, col - 1);
6107 if (tabular.isLastCell(cur.idx()))
6109 if (cur.idx() == tabular.getLastCellInRow(row))
6110 cur.idx() = tabular.cellIndex(row + 1, 0);
6112 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
6113 cur.idx() = tabular.cellIndex(row, colnextcell);
6117 cur.boundary(false);
6119 if (cur.selIsMultiCell()) {
6120 cur.pit() = cur.lastpit();
6121 cur.pos() = cur.lastpos();
6128 // in visual mode, place cursor at extreme left or right
6130 switch(entry_from) {
6132 case ENTRY_DIRECTION_RIGHT:
6133 cur.posVisToRowExtremity(false /* !left */);
6135 case ENTRY_DIRECTION_LEFT:
6136 cur.posVisToRowExtremity(true /* left */);
6138 case ENTRY_DIRECTION_IGNORE:
6139 // nothing to do in this case
6143 cur.setCurrentFont();
6147 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
6149 row_type const row = tabular.cellRow(cur.idx());
6150 col_type const col = tabular.cellColumn(cur.idx());
6152 if (isRightToLeft(cur)) {
6153 if (cur.idx() == tabular.getLastCellInRow(row)) {
6156 cur.idx() = tabular.getFirstCellInRow(row);
6157 cur.idx() = tabular.cellAbove(cur.idx());
6159 if (tabular.isLastCell(cur.idx()))
6161 if (cur.idx() == tabular.getLastCellInRow(row))
6162 cur.idx() = tabular.cellIndex(row + 1, 0);
6164 cur.idx() = tabular.cellIndex(row, col + 1);
6167 if (cur.idx() == 0) // first cell
6170 cur.idx() = tabular.getLastCellInRow(row - 1);
6172 cur.idx() = tabular.cellIndex(row, col - 1);
6175 if (cur.selIsMultiCell()) {
6176 cur.pit() = cur.lastpit();
6177 cur.pos() = cur.lastpos();
6181 cur.pit() = cur.lastpit();
6182 cur.pos() = cur.lastpos();
6184 // in visual mode, place cursor at extreme left or right
6186 switch(entry_from) {
6188 case ENTRY_DIRECTION_RIGHT:
6189 cur.posVisToRowExtremity(false /* !left */);
6191 case ENTRY_DIRECTION_LEFT:
6192 cur.posVisToRowExtremity(true /* left */);
6194 case ENTRY_DIRECTION_IGNORE:
6195 // nothing to do in this case
6199 cur.setCurrentFont();
6203 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
6205 cur.recordUndoInset(this);
6207 istringstream is(argument);
6208 // limit the size of strings we read to avoid memory problems
6212 size_t safe_guard = 0;
6217 if (safe_guard > 1000) {
6218 LYXERR0("parameter max count reached!");
6222 Tabular::Feature action = Tabular::LAST_ACTION;
6225 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
6226 if (s != tabularFeature[i].feature)
6228 action = tabularFeature[i].action;
6231 if (action == Tabular::LAST_ACTION) {
6232 LYXERR0("Feature not found " << s);
6236 if (tabularFeature[i].need_value)
6238 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
6239 tabularFeatures(cur, action, val);
6244 static void checkLongtableSpecial(Tabular::ltType & ltt,
6245 string const & special, bool & flag)
6247 if (special == "dl_above") {
6250 } else if (special == "dl_below") {
6251 ltt.bottomDL = flag;
6253 } else if (special == "empty") {
6263 bool InsetTabular::oneCellHasRotationState(bool rotated,
6264 row_type row_start, row_type row_end,
6265 col_type col_start, col_type col_end) const
6267 for (row_type r = row_start; r <= row_end; ++r)
6268 for (col_type c = col_start; c <= col_end; ++c)
6270 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
6273 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
6280 void InsetTabular::tabularFeatures(Cursor & cur,
6281 Tabular::Feature feature, string const & value)
6283 col_type sel_col_start;
6284 col_type sel_col_end;
6285 row_type sel_row_start;
6286 row_type sel_row_end;
6287 bool setLines = false;
6288 bool setLinesInnerOnly = false;
6289 LyXAlignment setAlign = LYX_ALIGN_LEFT;
6290 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
6294 case Tabular::M_ALIGN_LEFT:
6295 case Tabular::ALIGN_LEFT:
6296 setAlign = LYX_ALIGN_LEFT;
6299 case Tabular::M_ALIGN_RIGHT:
6300 case Tabular::ALIGN_RIGHT:
6301 setAlign = LYX_ALIGN_RIGHT;
6304 case Tabular::M_ALIGN_CENTER:
6305 case Tabular::ALIGN_CENTER:
6306 setAlign = LYX_ALIGN_CENTER;
6309 case Tabular::ALIGN_BLOCK:
6310 setAlign = LYX_ALIGN_BLOCK;
6313 case Tabular::ALIGN_DECIMAL:
6314 setAlign = LYX_ALIGN_DECIMAL;
6317 case Tabular::M_VALIGN_TOP:
6318 case Tabular::VALIGN_TOP:
6319 setVAlign = Tabular::LYX_VALIGN_TOP;
6322 case Tabular::M_VALIGN_BOTTOM:
6323 case Tabular::VALIGN_BOTTOM:
6324 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
6327 case Tabular::M_VALIGN_MIDDLE:
6328 case Tabular::VALIGN_MIDDLE:
6329 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
6336 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6337 row_type const row = tabular.cellRow(cur.idx());
6338 col_type const column = tabular.cellColumn(cur.idx());
6340 Tabular::ltType ltt;
6344 case Tabular::SET_TABULAR_WIDTH:
6345 tabular.setTabularWidth(Length(value));
6348 case Tabular::SET_PWIDTH: {
6349 Length const len(value);
6350 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6351 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
6353 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
6354 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
6359 case Tabular::SET_MPWIDTH:
6360 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
6363 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
6364 bool const varwidth = value == "on";
6365 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6366 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
6370 case Tabular::SET_MROFFSET:
6371 tabular.setMROffset(cur, cur.idx(), Length(value));
6374 case Tabular::SET_SPECIAL_COLUMN:
6375 case Tabular::SET_SPECIAL_MULTICOLUMN:
6376 if (value == "none")
6377 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
6379 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
6382 case Tabular::APPEND_ROW:
6383 // append the row into the tabular
6384 tabular.appendRow(row);
6387 case Tabular::APPEND_COLUMN:
6388 // append the column into the tabular
6389 tabular.appendColumn(column);
6390 cur.idx() = tabular.cellIndex(row, column);
6393 case Tabular::DELETE_ROW:
6394 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
6395 for (col_type c = 0; c < tabular.ncols(); c++) {
6396 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
6397 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
6398 tabular.setBottomLineTrim(tabular.cellIndex(sel_row_start - 1, c),
6399 tabular.bottomLineTrim(tabular.cellIndex(sel_row_end, c)));
6403 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6404 tabular.deleteRow(sel_row_start);
6405 if (sel_row_start >= tabular.nrows())
6407 cur.idx() = tabular.cellIndex(sel_row_start, column);
6410 cur.selection(false);
6413 case Tabular::DELETE_COLUMN:
6414 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
6415 for (row_type r = 0; r < tabular.nrows(); r++)
6416 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
6417 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
6420 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
6421 for (row_type r = 0; r < tabular.nrows(); r++)
6422 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
6423 tabular.leftLine(tabular.cellIndex(r, 0)));
6426 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6427 tabular.deleteColumn(sel_col_start);
6428 if (sel_col_start >= tabular.ncols())
6430 cur.idx() = tabular.cellIndex(row, sel_col_start);
6433 cur.selection(false);
6436 case Tabular::COPY_ROW:
6437 tabular.copyRow(row);
6440 case Tabular::COPY_COLUMN:
6441 tabular.copyColumn(column);
6442 cur.idx() = tabular.cellIndex(row, column);
6445 case Tabular::MOVE_COLUMN_RIGHT:
6446 tabular.moveColumn(column, Tabular::RIGHT);
6447 cur.idx() = tabular.cellIndex(row, column + 1);
6450 case Tabular::MOVE_COLUMN_LEFT:
6451 tabular.moveColumn(column, Tabular::LEFT);
6452 cur.idx() = tabular.cellIndex(row, column - 1);
6455 case Tabular::MOVE_ROW_DOWN:
6456 tabular.moveRow(row, Tabular::DOWN);
6457 cur.idx() = tabular.cellIndex(row + 1, column);
6460 case Tabular::MOVE_ROW_UP:
6461 tabular.moveRow(row, Tabular::UP);
6462 cur.idx() = tabular.cellIndex(row - 1, column);
6465 case Tabular::SET_LINE_TOP:
6466 case Tabular::TOGGLE_LINE_TOP: {
6467 bool lineSet = (feature == Tabular::SET_LINE_TOP)
6468 ? (value == "true") : !tabular.topLine(cur.idx());
6469 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6470 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6471 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
6475 case Tabular::SET_LINE_BOTTOM:
6476 case Tabular::TOGGLE_LINE_BOTTOM: {
6477 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
6478 ? (value == "true") : !tabular.bottomLine(cur.idx());
6479 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6480 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6481 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
6485 case Tabular::SET_LTRIM_TOP:
6486 case Tabular::TOGGLE_LTRIM_TOP: {
6487 bool l = (feature == Tabular::SET_LTRIM_TOP)
6488 ? (value == "true") : !tabular.topLineTrim(cur.idx()).first;
6489 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6490 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6491 tabular.setTopLineLTrim(tabular.cellIndex(r, c), l);
6495 case Tabular::SET_RTRIM_TOP:
6496 case Tabular::TOGGLE_RTRIM_TOP: {
6497 bool l = (feature == Tabular::SET_RTRIM_TOP)
6498 ? (value == "true") : !tabular.topLineTrim(cur.idx()).second;
6499 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6500 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6501 tabular.setTopLineRTrim(tabular.cellIndex(r, c), l);
6505 case Tabular::SET_LTRIM_BOTTOM:
6506 case Tabular::TOGGLE_LTRIM_BOTTOM: {
6507 bool l = (feature == Tabular::SET_LTRIM_BOTTOM)
6508 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).first;
6509 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6510 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6511 tabular.setBottomLineLTrim(tabular.cellIndex(r, c), l);
6515 case Tabular::SET_RTRIM_BOTTOM:
6516 case Tabular::TOGGLE_RTRIM_BOTTOM: {
6517 bool l = (feature == Tabular::SET_RTRIM_BOTTOM)
6518 ? (value == "true") : !tabular.bottomLineTrim(cur.idx()).second;
6519 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6520 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6521 tabular.setBottomLineRTrim(tabular.cellIndex(r, c), l);
6525 case Tabular::SET_LINE_LEFT:
6526 case Tabular::TOGGLE_LINE_LEFT: {
6527 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
6528 ? (value == "true") : !tabular.leftLine(cur.idx());
6529 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6530 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6531 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
6535 case Tabular::SET_LINE_RIGHT:
6536 case Tabular::TOGGLE_LINE_RIGHT: {
6537 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
6538 ? (value == "true") : !tabular.rightLine(cur.idx());
6539 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6540 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6541 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
6545 case Tabular::M_ALIGN_LEFT:
6546 case Tabular::M_ALIGN_RIGHT:
6547 case Tabular::M_ALIGN_CENTER:
6548 case Tabular::ALIGN_LEFT:
6549 case Tabular::ALIGN_RIGHT:
6550 case Tabular::ALIGN_CENTER:
6551 case Tabular::ALIGN_BLOCK:
6552 case Tabular::ALIGN_DECIMAL:
6553 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6554 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6555 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6556 !tabular.getPWidth(c).zero());
6559 case Tabular::M_VALIGN_TOP:
6560 case Tabular::M_VALIGN_BOTTOM:
6561 case Tabular::M_VALIGN_MIDDLE:
6564 case Tabular::VALIGN_TOP:
6565 case Tabular::VALIGN_BOTTOM:
6566 case Tabular::VALIGN_MIDDLE:
6567 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6568 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6569 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6572 case Tabular::SET_MULTICOLUMN: {
6573 if (!cur.selection()) {
6574 // just multicol for one single cell
6575 // check whether we are completely in a multicol
6576 if (!tabular.isMultiColumn(cur.idx()))
6577 tabular.setMultiColumn(cur, cur.idx(), 1,
6578 tabular.rightLine(cur.idx()));
6581 // we have a selection so this means we just add all these
6582 // cells to form a multicolumn cell
6583 idx_type const s_start = cur.selBegin().idx();
6584 row_type const col_start = tabular.cellColumn(s_start);
6585 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6586 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6587 tabular.rightLine(cur.selEnd().idx()));
6590 cur.selection(false);
6594 case Tabular::UNSET_MULTICOLUMN: {
6595 if (!cur.selection()) {
6596 if (tabular.isMultiColumn(cur.idx()))
6597 tabular.unsetMultiColumn(cur.idx());
6602 case Tabular::MULTICOLUMN: {
6603 if (!cur.selection()) {
6604 if (tabular.isMultiColumn(cur.idx()))
6605 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6607 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6611 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6612 row_type const r = sel_row_start;
6613 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6614 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6617 // If the selection contains at least one singlecol cell
6618 // or multiple multicol cells,
6619 // we assume the user will merge is to a single multicol
6621 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6623 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6627 case Tabular::SET_MULTIROW: {
6628 if (!cur.selection()) {
6629 // just multirow for one single cell
6630 // check whether we are completely in a multirow
6631 if (!tabular.isMultiRow(cur.idx()))
6632 tabular.setMultiRow(cur, cur.idx(), 1,
6633 tabular.bottomLine(cur.idx()),
6634 tabular.getAlignment(cur.idx()));
6637 // we have a selection so this means we just add all this
6638 // cells to form a multirow cell
6639 idx_type const s_start = cur.selBegin().idx();
6640 row_type const row_start = tabular.cellRow(s_start);
6641 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6642 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6643 tabular.bottomLine(cur.selEnd().idx()),
6644 tabular.getAlignment(cur.selEnd().idx()));
6647 cur.selection(false);
6651 case Tabular::UNSET_MULTIROW: {
6652 if (!cur.selection()) {
6653 if (tabular.isMultiRow(cur.idx()))
6654 tabular.unsetMultiRow(cur.idx());
6659 case Tabular::MULTIROW: {
6660 if (!cur.selection()) {
6661 if (tabular.isMultiRow(cur.idx()))
6662 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6664 tabularFeatures(cur, Tabular::SET_MULTIROW);
6668 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6669 col_type const c = sel_col_start;
6670 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6671 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6674 // If the selection contains at least one singlerow cell
6675 // or multiple multirow cells,
6676 // we assume the user will merge is to a single multirow
6678 tabularFeatures(cur, Tabular::SET_MULTIROW);
6680 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6684 case Tabular::SET_INNER_LINES:
6685 setLinesInnerOnly = true;
6687 case Tabular::SET_ALL_LINES:
6690 case Tabular::UNSET_ALL_LINES:
6691 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6692 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6693 idx_type const cell = tabular.cellIndex(r, c);
6694 if (!setLinesInnerOnly || r != sel_row_start)
6695 tabular.setTopLine(cell, setLines);
6696 if ((!setLinesInnerOnly || r != sel_row_end)
6697 && (!setLines || r == sel_row_end))
6698 tabular.setBottomLine(cell, setLines);
6699 if ((!setLinesInnerOnly || c != sel_col_end)
6700 && (!setLines || c == sel_col_end))
6701 tabular.setRightLine(cell, setLines);
6702 if ((!setLinesInnerOnly || c != sel_col_start))
6703 tabular.setLeftLine(cell, setLines);
6707 case Tabular::RESET_FORMAL_DEFAULT:
6708 for (row_type r = 0; r < tabular.nrows(); ++r) {
6709 bool const head_or_foot = r == 0 || r == tabular.nrows() - 1;
6710 for (col_type c = 0; c < tabular.ncols(); ++c) {
6711 idx_type const cell = tabular.cellIndex(r, c);
6712 tabular.setTopLine(cell, head_or_foot);
6713 tabular.setBottomLine(cell, head_or_foot);
6718 case Tabular::SET_BORDER_LINES:
6719 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6720 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6721 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6723 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6724 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6725 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6729 case Tabular::TOGGLE_LONGTABULAR:
6730 if (tabular.is_long_tabular)
6731 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6733 tabular.is_long_tabular = true;
6736 case Tabular::SET_LONGTABULAR:
6737 tabular.is_long_tabular = true;
6740 case Tabular::UNSET_LONGTABULAR:
6741 for (row_type r = 0; r < tabular.nrows(); ++r) {
6742 if (tabular.ltCaption(r)) {
6743 cur.idx() = tabular.cellIndex(r, 0);
6746 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6749 tabular.is_long_tabular = false;
6752 case Tabular::SET_ROTATE_TABULAR:
6753 tabular.rotate = convert<int>(value);
6756 case Tabular::UNSET_ROTATE_TABULAR:
6760 case Tabular::TOGGLE_ROTATE_TABULAR:
6761 // when pressing the rotate button we default to 90° rotation
6762 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6765 case Tabular::TABULAR_VALIGN_TOP:
6766 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6769 case Tabular::TABULAR_VALIGN_MIDDLE:
6770 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6773 case Tabular::TABULAR_VALIGN_BOTTOM:
6774 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6777 case Tabular::LONGTABULAR_ALIGN_LEFT:
6778 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6781 case Tabular::LONGTABULAR_ALIGN_CENTER:
6782 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6785 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6786 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6789 case Tabular::SET_ROTATE_CELL:
6790 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6791 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6792 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6795 case Tabular::UNSET_ROTATE_CELL:
6796 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6797 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6798 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6801 case Tabular::TOGGLE_ROTATE_CELL:
6803 bool oneNotRotated = oneCellHasRotationState(false,
6804 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6806 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6807 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6808 // when pressing the rotate cell button we default to 90° rotation
6810 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6812 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6817 case Tabular::SET_USEBOX: {
6818 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6819 if (val == tabular.getUsebox(cur.idx()))
6820 val = Tabular::BOX_NONE;
6821 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6822 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6823 tabular.setUsebox(tabular.cellIndex(r, c), val);
6827 case Tabular::UNSET_LTFIRSTHEAD:
6830 case Tabular::SET_LTFIRSTHEAD:
6831 tabular.getRowOfLTFirstHead(row, ltt);
6832 checkLongtableSpecial(ltt, value, flag);
6833 tabular.setLTHead(row, flag, ltt, true);
6836 case Tabular::UNSET_LTHEAD:
6839 case Tabular::SET_LTHEAD:
6840 tabular.getRowOfLTHead(row, ltt);
6841 checkLongtableSpecial(ltt, value, flag);
6842 tabular.setLTHead(row, flag, ltt, false);
6845 case Tabular::UNSET_LTFOOT:
6848 case Tabular::SET_LTFOOT:
6849 tabular.getRowOfLTFoot(row, ltt);
6850 checkLongtableSpecial(ltt, value, flag);
6851 tabular.setLTFoot(row, flag, ltt, false);
6854 case Tabular::UNSET_LTLASTFOOT:
6857 case Tabular::SET_LTLASTFOOT:
6858 tabular.getRowOfLTLastFoot(row, ltt);
6859 checkLongtableSpecial(ltt, value, flag);
6860 tabular.setLTFoot(row, flag, ltt, true);
6863 case Tabular::UNSET_LTNEWPAGE:
6866 case Tabular::SET_LTNEWPAGE:
6867 tabular.setLTNewPage(row, flag);
6870 case Tabular::SET_LTCAPTION: {
6871 if (tabular.ltCaption(row))
6873 cur.idx() = tabular.setLTCaption(cur, row, true);
6876 cur.selection(false);
6877 // If a row is set as caption, then also insert
6878 // a caption. Otherwise the LaTeX output is broken.
6879 // Select cell if it is non-empty
6880 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6881 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6882 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6886 case Tabular::UNSET_LTCAPTION: {
6887 if (!tabular.ltCaption(row))
6889 cur.idx() = tabular.setLTCaption(cur, row, false);
6892 cur.selection(false);
6893 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6894 if (lyx::getStatus(fr).enabled())
6899 case Tabular::TOGGLE_LTCAPTION: {
6900 if (tabular.ltCaption(row))
6901 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6903 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6907 case Tabular::TOGGLE_BOOKTABS:
6908 tabular.use_booktabs = !tabular.use_booktabs;
6911 case Tabular::SET_BOOKTABS:
6912 tabular.use_booktabs = true;
6915 case Tabular::UNSET_BOOKTABS:
6916 tabular.use_booktabs = false;
6919 case Tabular::SET_TOP_SPACE: {
6921 if (value == "default")
6922 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6923 tabular.row_info[r].top_space_default = true;
6924 else if (value == "none")
6925 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6926 tabular.row_info[r].top_space_default = false;
6927 tabular.row_info[r].top_space = len;
6929 else if (isValidLength(value, &len))
6930 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6931 tabular.row_info[r].top_space_default = false;
6932 tabular.row_info[r].top_space = len;
6937 case Tabular::SET_BOTTOM_SPACE: {
6939 if (value == "default")
6940 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6941 tabular.row_info[r].bottom_space_default = true;
6942 else if (value == "none")
6943 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6944 tabular.row_info[r].bottom_space_default = false;
6945 tabular.row_info[r].bottom_space = len;
6947 else if (isValidLength(value, &len))
6948 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6949 tabular.row_info[r].bottom_space_default = false;
6950 tabular.row_info[r].bottom_space = len;
6955 case Tabular::SET_INTERLINE_SPACE: {
6957 if (value == "default")
6958 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6959 tabular.row_info[r].interline_space_default = true;
6960 else if (value == "none")
6961 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6962 tabular.row_info[r].interline_space_default = false;
6963 tabular.row_info[r].interline_space = len;
6965 else if (isValidLength(value, &len))
6966 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6967 tabular.row_info[r].interline_space_default = false;
6968 tabular.row_info[r].interline_space = len;
6973 case Tabular::SET_DECIMAL_POINT:
6974 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6975 tabular.column_info[c].decimal_point = from_utf8(value);
6978 // dummy stuff just to avoid warnings
6979 case Tabular::LAST_ACTION:
6985 bool InsetTabular::copySelection(Cursor & cur)
6987 if (!cur.selection())
6992 getSelection(cur, rs, re, cs, ce);
6994 paste_tabular.reset(new Tabular(tabular));
6996 for (row_type r = 0; r < rs; ++r)
6997 paste_tabular->deleteRow(0);
6999 row_type const rows = re - rs + 1;
7000 while (paste_tabular->nrows() > rows)
7001 paste_tabular->deleteRow(rows);
7003 for (col_type c = 0; c < cs; ++c)
7004 paste_tabular->deleteColumn(0);
7006 col_type const columns = ce - cs + 1;
7007 while (paste_tabular->ncols() > columns)
7008 paste_tabular->deleteColumn(columns);
7010 paste_tabular->setBuffer(tabular.buffer());
7012 odocstringstream os;
7013 OutputParams const runparams(0);
7014 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
7015 // Needed for the "Edit->Paste recent" menu and the system clipboard.
7016 cap::copySelection(cur, os.str());
7018 // mark tabular stack dirty
7019 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
7020 // when we (hopefully) have a one-for-all paste mechanism.
7021 // This must be called after cap::copySelection.
7022 dirtyTabularStack(true);
7028 bool InsetTabular::pasteClipboard(Cursor & cur)
7032 col_type actcol = tabular.cellColumn(cur.idx());
7033 row_type actrow = tabular.cellRow(cur.idx());
7035 if (cur.selIsMultiCell()) {
7038 getSelection(cur, actrow, re, actcol, ce);
7041 for (row_type r1 = 0, r2 = actrow;
7042 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
7044 for (col_type c1 = 0, c2 = actcol;
7045 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
7047 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
7048 tabular.isPartOfMultiColumn(r2, c2))
7050 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
7054 if (tabular.isPartOfMultiColumn(r2, c2)) {
7058 shared_ptr<InsetTableCell> inset(
7059 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
7060 tabular.setCellInset(r2, c2, inset);
7061 // FIXME?: why do we need to do this explicitly? (EL)
7062 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
7064 // FIXME: change tracking (MG)
7065 inset->setChange(Change(buffer().params().track_changes ?
7066 Change::INSERTED : Change::UNCHANGED));
7075 void InsetTabular::cutSelection(Cursor & cur)
7077 if (!cur.selection())
7082 getSelection(cur, rs, re, cs, ce);
7083 for (row_type r = rs; r <= re; ++r) {
7084 for (col_type c = cs; c <= ce; ++c) {
7085 shared_ptr<InsetTableCell> t
7086 = cell(tabular.cellIndex(r, c));
7087 if (buffer().params().track_changes)
7088 // FIXME: Change tracking (MG)
7089 t->setChange(Change(Change::DELETED));
7095 // cursor position might be invalid now
7096 if (cur.pit() > cur.lastpit())
7097 cur.pit() = cur.lastpit();
7098 if (cur.pos() > cur.lastpos())
7099 cur.pos() = cur.lastpos();
7100 cur.clearSelection();
7104 bool InsetTabular::isRightToLeft(Cursor & cur) const
7106 // LASSERT: It might be better to abandon this Buffer.
7107 LASSERT(cur.depth() > 1, return false);
7108 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
7109 pos_type const parentpos = cur[cur.depth() - 2].pos();
7110 return parentpar.getFontSettings(buffer().params(),
7111 parentpos).language()->rightToLeft();
7115 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
7118 LASSERT(stidx <= enidx, return docstring());
7120 col_type const col1 = tabular.cellColumn(stidx);
7121 col_type const col2 = tabular.cellColumn(enidx);
7122 row_type const row1 = tabular.cellRow(stidx);
7123 row_type const row2 = tabular.cellRow(enidx);
7125 for (col_type col = col1; col <= col2; col++)
7126 for (row_type row = row1; row <= row2; row++) {
7131 retval += tabular.cellInset(row, col)->asString(intoInsets);
7137 void InsetTabular::getSelection(Cursor & cur,
7138 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
7140 CursorSlice const & beg = cur.selBegin();
7141 CursorSlice const & end = cur.selEnd();
7142 cs = tabular.cellColumn(beg.idx());
7143 ce = tabular.cellColumn(end.idx());
7147 rs = tabular.cellRow(beg.idx());
7148 re = tabular.cellRow(end.idx());
7154 Text * InsetTabular::getText(int idx) const
7156 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
7160 bool InsetTabular::isChanged() const
7162 for (idx_type idx = 0; idx < nargs(); ++idx) {
7163 if (cell(idx)->isChanged())
7165 if (tabular.row_info[tabular.cellRow(idx)].change.changed())
7167 if (tabular.column_info[tabular.cellColumn(idx)].change.changed())
7174 void InsetTabular::setChange(Change const & change)
7176 for (idx_type idx = 0; idx < nargs(); ++idx)
7177 cell(idx)->setChange(change);
7181 void InsetTabular::acceptChanges()
7183 for (idx_type idx = 0; idx < nargs(); ++idx)
7184 cell(idx)->acceptChanges();
7185 for (row_type row = 0; row < tabular.nrows(); ++row) {
7186 if (tabular.row_info[row].change.inserted())
7187 tabular.row_info[row].change.setUnchanged();
7188 else if (tabular.row_info[row].change.deleted())
7189 tabular.deleteRow(row, true);
7191 for (col_type col = 0; col < tabular.ncols(); ++col) {
7192 if (tabular.column_info[col].change.inserted())
7193 tabular.column_info[col].change.setUnchanged();
7194 else if (tabular.column_info[col].change.deleted())
7195 tabular.deleteColumn(col, true);
7197 tabular.updateIndexes();
7201 void InsetTabular::rejectChanges()
7203 for (idx_type idx = 0; idx < nargs(); ++idx)
7204 cell(idx)->rejectChanges();
7205 for (row_type row = 0; row < tabular.nrows(); ++row) {
7206 if (tabular.row_info[row].change.deleted())
7207 tabular.row_info[row].change.setUnchanged();
7208 else if (tabular.row_info[row].change.inserted())
7209 tabular.deleteRow(row, true);
7211 for (col_type col = 0; col < tabular.ncols(); ++col) {
7212 if (tabular.column_info[col].change.deleted())
7213 tabular.column_info[col].change.setUnchanged();
7214 else if (tabular.column_info[col].change.inserted())
7215 tabular.deleteColumn(col, true);
7217 tabular.updateIndexes();
7221 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
7223 return tabular.getPWidth(cell).zero();
7227 bool InsetTabular::forcePlainLayout(idx_type cell) const
7229 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
7233 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
7236 if (buf.length() <= 0)
7241 col_type maxCols = 1;
7242 size_t const len = buf.length();
7246 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
7254 maxCols = max(cols, maxCols);
7260 maxCols = max(cols, maxCols);
7266 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
7267 loctab = paste_tabular.get();
7268 dirtyTabularStack(true);
7271 cell = bv.cursor().idx();
7272 ocol = tabular.cellColumn(cell);
7273 row = tabular.cellRow(cell);
7277 idx_type cells = loctab->numberofcells;
7280 rows = loctab->nrows();
7281 col_type columns = loctab->ncols();
7284 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
7290 // append column if necessary
7291 if (cols == columns) {
7292 loctab->appendColumn(cols - 1);
7293 columns = loctab->ncols();
7294 cells = loctab->numberofcells;
7297 if (cols < columns) {
7298 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7299 Font const font = bv.textMetrics(&inset->text()).
7301 inset->setText(buf.substr(op, p - op), font,
7302 buffer().params().track_changes);
7308 // we can only set this if we are not too far right
7309 if (cols < columns) {
7310 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
7311 Font const font = bv.textMetrics(&inset->text()).
7313 inset->setText(buf.substr(op, p - op), font,
7314 buffer().params().track_changes);
7318 // append row if necessary
7319 if (row == rows && p < len - 1) {
7320 loctab->appendRow(row - 1);
7321 rows = loctab->nrows();
7322 cells = loctab->numberofcells;
7325 cell = loctab->cellIndex(row, cols);
7331 // check for the last cell if there is no trailing '\n'
7332 if (cell < cells && op < len) {
7333 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
7334 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
7335 inset->setText(buf.substr(op, len - op), font,
7336 buffer().params().track_changes);
7342 void InsetTabular::addPreview(DocIterator const & inset_pos,
7343 PreviewLoader & loader) const
7345 DocIterator cell_pos = inset_pos;
7347 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
7348 for (row_type r = 0; r < tabular.nrows(); ++r) {
7349 for (col_type c = 0; c < tabular.ncols(); ++c) {
7350 cell_pos.top().idx() = tabular.cellIndex(r, c);
7351 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
7357 bool InsetTabular::completionSupported(Cursor const & cur) const
7359 Cursor const & bvCur = cur.bv().cursor();
7360 if (&bvCur.inset() != this)
7362 return cur.text()->completionSupported(cur);
7366 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
7368 return completionSupported(cur);
7372 bool InsetTabular::automaticInlineCompletion() const
7374 return lyxrc.completion_inline_text;
7378 bool InsetTabular::automaticPopupCompletion() const
7380 return lyxrc.completion_popup_text;
7384 bool InsetTabular::showCompletionCursor() const
7386 return lyxrc.completion_cursor_text;
7390 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
7392 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
7396 docstring InsetTabular::completionPrefix(Cursor const & cur) const
7398 if (!completionSupported(cur))
7400 return cur.text()->completionPrefix(cur);
7404 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
7406 if (!completionSupported(cur))
7409 return cur.text()->insertCompletion(cur, s, finished);
7413 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
7414 Dimension & dim) const
7416 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
7417 tm.completionPosAndDim(cur, x, y, dim);
7421 void InsetTabular::string2params(string const & in, InsetTabular & inset)
7423 istringstream data(in);
7425 lex.setStream(data);
7432 if (!lex || token != "tabular") {
7433 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
7437 // This is part of the inset proper that is usually swallowed
7438 // by Buffer::readInset
7440 if (!lex || token != "Tabular") {
7441 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
7449 string InsetTabular::params2string(InsetTabular const & inset)
7452 data << "tabular" << ' ';
7454 data << "\\end_inset\n";
7459 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
7461 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
7462 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
7463 if (!tabular.isPartOfMultiColumn(r,c) &&
7464 !tabular.isPartOfMultiRow(r,c))
7467 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
7468 ParagraphList::iterator it = parlist.begin();
7469 ParagraphList::iterator const en = parlist.end();
7470 for (; it != en; ++it)
7471 it->setLayout(dc.plainLayout());