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_LINE_LEFT, "set-line-left", true },
131 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
132 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
133 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
134 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
135 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
136 { Tabular::ALIGN_LEFT, "align-left", false },
137 { Tabular::ALIGN_RIGHT, "align-right", false },
138 { Tabular::ALIGN_CENTER, "align-center", false },
139 { Tabular::ALIGN_BLOCK, "align-block", false },
140 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
141 { Tabular::VALIGN_TOP, "valign-top", false },
142 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
143 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
144 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
145 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
146 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
147 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
148 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
149 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
150 { Tabular::MULTICOLUMN, "multicolumn", false },
151 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
152 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
153 { Tabular::MULTIROW, "multirow", false },
154 { Tabular::SET_MULTIROW, "set-multirow", false },
155 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
156 { Tabular::SET_MROFFSET, "set-mroffset", true },
157 { Tabular::SET_ALL_LINES, "set-all-lines", false },
158 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
159 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
160 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
161 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
162 { Tabular::SET_PWIDTH, "set-pwidth", true },
163 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
164 { Tabular::TOGGLE_VARWIDTH_COLUMN, "toggle-varwidth-column", true },
165 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
166 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
167 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
168 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
169 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
170 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
171 { Tabular::SET_USEBOX, "set-usebox", true },
172 { Tabular::SET_LTHEAD, "set-lthead", true },
173 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
174 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
175 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
176 { Tabular::SET_LTFOOT, "set-ltfoot", true },
177 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
178 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
179 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
180 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
181 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
182 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
183 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
184 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
185 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
186 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
187 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
188 { Tabular::SET_BOOKTABS, "set-booktabs", false },
189 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
190 { Tabular::SET_TOP_SPACE, "set-top-space", true },
191 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
192 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
193 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
194 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
195 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
196 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
197 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
198 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
199 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
200 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
201 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
202 { Tabular::LAST_ACTION, "", false }
206 string const tostr(LyXAlignment const & num)
211 case LYX_ALIGN_BLOCK:
215 case LYX_ALIGN_CENTER:
217 case LYX_ALIGN_RIGHT:
219 case LYX_ALIGN_LAYOUT:
221 case LYX_ALIGN_SPECIAL:
223 case LYX_ALIGN_DECIMAL:
230 string const tostr(Tabular::HAlignment const & num)
233 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
235 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
237 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
244 string const tostr(Tabular::VAlignment const & num)
247 case Tabular::LYX_VALIGN_TOP:
249 case Tabular::LYX_VALIGN_MIDDLE:
251 case Tabular::LYX_VALIGN_BOTTOM:
258 string const tostr(Tabular::BoxType const & num)
261 case Tabular::BOX_NONE:
263 case Tabular::BOX_PARBOX:
265 case Tabular::BOX_MINIPAGE:
272 // I would have liked a fromstr template a lot better. (Lgb)
273 bool string2type(string const & str, LyXAlignment & num)
276 num = LYX_ALIGN_NONE;
277 else if (str == "block")
278 num = LYX_ALIGN_BLOCK;
279 else if (str == "left")
280 num = LYX_ALIGN_LEFT;
281 else if (str == "center")
282 num = LYX_ALIGN_CENTER;
283 else if (str == "right")
284 num = LYX_ALIGN_RIGHT;
285 else if (str == "decimal")
286 num = LYX_ALIGN_DECIMAL;
293 bool string2type(string const & str, Tabular::HAlignment & num)
296 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
297 else if (str == "center" )
298 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
299 else if (str == "right")
300 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
307 bool string2type(string const & str, Tabular::VAlignment & num)
310 num = Tabular::LYX_VALIGN_TOP;
311 else if (str == "middle" )
312 num = Tabular::LYX_VALIGN_MIDDLE;
313 else if (str == "bottom")
314 num = Tabular::LYX_VALIGN_BOTTOM;
321 bool string2type(string const & str, Tabular::BoxType & num)
324 num = Tabular::BOX_NONE;
325 else if (str == "parbox")
326 num = Tabular::BOX_PARBOX;
327 else if (str == "minipage")
328 num = Tabular::BOX_MINIPAGE;
335 bool string2type(string const & str, bool & num)
339 else if (str == "false")
347 bool getTokenValue(string const & str, char const * token, string & ret)
350 size_t token_length = strlen(token);
351 size_t pos = str.find(token);
353 if (pos == string::npos || pos + token_length + 1 >= str.length()
354 || str[pos + token_length] != '=')
356 pos += token_length + 1;
358 if (ch != '"' && ch != '\'') { // only read till next space
362 while (pos < str.length() - 1 && str[++pos] != ch)
369 bool getTokenValue(string const & str, char const * token, docstring & ret)
372 bool const success = getTokenValue(str, token, tmp);
373 ret = from_utf8(tmp);
378 bool getTokenValue(string const & str, char const * token, int & num)
382 if (!getTokenValue(str, token, tmp))
384 num = convert<int>(tmp);
389 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
392 return getTokenValue(str, token, tmp) && string2type(tmp, num);
396 bool getTokenValue(string const & str, char const * token,
397 Tabular::HAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::VAlignment & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token,
413 Tabular::BoxType & num)
416 return getTokenValue(str, token, tmp) && string2type(tmp, num);
420 bool getTokenValue(string const & str, char const * token, bool & flag)
422 // set the flag always to false as this should be the default for bools
423 // not in the file-format.
426 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
430 bool getTokenValue(string const & str, char const * token, Length & len)
432 // set the length to be zero() as default as this it should be if not
433 // in the file format.
436 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
440 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
445 if (!getTokenValue(str, token, tmp))
447 if (tmp == "default") {
451 return isValidLength(tmp, &len);
455 void l_getline(istream & is, string & str)
458 while (str.empty()) {
460 if (!str.empty() && str[str.length() - 1] == '\r')
461 str.erase(str.length() - 1);
466 string const write_attribute(string const & name, T const & t)
468 string const s = tostr(t);
469 return s.empty() ? s : " " + name + "=\"" + s + "\"";
473 string const write_attribute(string const & name, string const & t)
475 return t.empty() ? t : " " + name + "=\"" + t + "\"";
480 string const write_attribute(string const & name, docstring const & t)
482 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
487 string const write_attribute(string const & name, bool const & b)
489 // we write only true attribute values so we remove a bit of the
490 // file format bloat for tabulars.
491 return b ? write_attribute(name, convert<string>(b)) : string();
496 string const write_attribute(string const & name, int const & i)
498 // we write only true attribute values so we remove a bit of the
499 // file format bloat for tabulars.
500 return i ? write_attribute(name, convert<string>(i)) : string();
505 string const write_attribute(string const & name, Tabular::idx_type const & i)
507 // we write only true attribute values so we remove a bit of the
508 // file format bloat for tabulars.
509 return i ? write_attribute(name, convert<string>(i)) : string();
514 string const write_attribute(string const & name, Length const & value)
516 // we write only the value if we really have one same reson as above.
517 return value.zero() ? string() : write_attribute(name, value.asString());
523 string const featureAsString(Tabular::Feature action)
525 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
526 if (tabularFeature[i].action == action)
527 return tabularFeature[i].feature;
533 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
535 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
536 for (; dit; dit.forwardChar())
537 if (dit.inTexted() && dit.depth() == 1
538 && dit.paragraph().find(align_d, false, false, dit.pos()))
545 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
547 InsetTableCell tail = InsetTableCell(head);
548 DocIterator const dit = separatorPos(&head, align_d);
551 pit_type const psize = head.paragraphs().front().size();
552 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
553 tail.paragraphs().front().eraseChars(0,
554 dit.pos() < psize ? dit.pos() + 1 : psize, false);
561 /////////////////////////////////////////////////////////////////////
565 /////////////////////////////////////////////////////////////////////
568 Tabular::CellData::CellData(Buffer * buf)
571 multicolumn(Tabular::CELL_NORMAL),
572 multirow(Tabular::CELL_NORMAL),
573 alignment(LYX_ALIGN_CENTER),
574 valignment(LYX_VALIGN_TOP),
584 inset(new InsetTableCell(buf))
586 inset->setBuffer(*buf);
590 Tabular::CellData::CellData(CellData const & cs)
593 multicolumn(cs.multicolumn),
594 multirow(cs.multirow),
595 mroffset(cs.mroffset),
596 alignment(cs.alignment),
597 valignment(cs.valignment),
598 decimal_hoffset(cs.decimal_hoffset),
599 decimal_width(cs.decimal_width),
601 top_line(cs.top_line),
602 bottom_line(cs.bottom_line),
603 left_line(cs.left_line),
604 right_line(cs.right_line),
607 align_special(cs.align_special),
609 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
613 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
619 multicolumn = cs.multicolumn;
620 multirow = cs.multirow;
621 mroffset = cs.mroffset;
622 alignment = cs.alignment;
623 valignment = cs.valignment;
624 decimal_hoffset = cs.decimal_hoffset;
625 decimal_width = cs.decimal_width;
626 voffset = cs.voffset;
627 top_line = cs.top_line;
628 bottom_line = cs.bottom_line;
629 left_line = cs.left_line;
630 right_line = cs.right_line;
633 align_special = cs.align_special;
634 p_width = cs.p_width;
635 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
639 Tabular::RowData::RowData()
642 top_space_default(false),
643 bottom_space_default(false),
644 interline_space_default(false),
654 Tabular::ColumnData::ColumnData()
655 : alignment(LYX_ALIGN_CENTER),
656 valignment(LYX_VALIGN_TOP),
663 Tabular::ltType::ltType()
664 : set(false), topDL(false),
670 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
672 init(buffer, rows_arg, columns_arg);
676 void Tabular::setBuffer(Buffer & buffer)
679 for (row_type i = 0; i < nrows(); ++i)
680 for (col_type j = 0; j < ncols(); ++j)
681 cell_info[i][j].inset->setBuffer(*buffer_);
685 // activates all lines and sets all widths to 0
686 void Tabular::init(Buffer * buf, row_type rows_arg,
687 col_type columns_arg)
690 row_info = row_vector(rows_arg);
691 column_info = column_vector(columns_arg);
692 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
693 row_info.reserve(10);
694 column_info.reserve(10);
695 cell_info.reserve(100);
697 is_long_tabular = false;
698 tabular_valignment = LYX_VALIGN_MIDDLE;
699 tabular_width = Length();
700 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
702 use_booktabs = false;
703 // set silly default lines
704 for (row_type r = 0; r < nrows(); ++r)
705 for (col_type c = 0; c < ncols(); ++c) {
706 cell_info[r][c].inset->setBuffer(*buffer_);
707 cell_info[r][c].top_line = true;
708 cell_info[r][c].left_line = true;
709 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
710 cell_info[r][c].right_line = c == ncols() - 1;
715 void Tabular::deleteRow(row_type const row)
717 // Not allowed to delete last row
721 for (col_type c = 0; c < ncols(); ++c) {
722 // Care about multirow cells
723 if (row + 1 < nrows() &&
724 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
725 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
726 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
729 row_info.erase(row_info.begin() + row);
730 cell_info.erase(cell_info.begin() + row);
735 void Tabular::copyRow(row_type const row)
737 insertRow(row, true);
741 void Tabular::appendRow(row_type row)
743 insertRow(row, false);
747 void Tabular::insertRow(row_type const row, bool copy)
749 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
750 cell_info.insert(cell_info.begin() + row + 1,
751 cell_vector(0, CellData(buffer_)));
753 for (col_type c = 0; c < ncols(); ++c) {
754 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
755 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
756 if (buffer().params().track_changes)
757 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
758 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
759 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
763 for (col_type c = 0; c < ncols(); ++c) {
764 if (isPartOfMultiRow(row, c))
766 // inherit line settings
767 idx_type const i = cellIndex(row + 1, c);
768 idx_type const j = cellIndex(row, c);
769 setLeftLine(i, leftLine(j));
770 setRightLine(i, rightLine(j));
771 setTopLine(i, topLine(j));
772 if (topLine(j) && bottomLine(j)) {
773 setBottomLine(i, true);
774 setBottomLine(j, false);
776 // mark track changes
777 if (buffer().params().track_changes)
778 cellInfo(i).inset->setChange(Change(Change::INSERTED));
783 void Tabular::moveColumn(col_type col, ColDirection direction)
785 if (direction == Tabular::LEFT)
788 std::swap(column_info[col], column_info[col + 1]);
790 for (row_type r = 0; r < nrows(); ++r) {
791 std::swap(cell_info[r][col], cell_info[r][col + 1]);
792 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
793 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
795 // FIXME track changes is broken for tabular features (#8469)
796 idx_type const i = cellIndex(r, col);
797 idx_type const j = cellIndex(r, col + 1);
798 if (buffer().params().track_changes) {
799 cellInfo(i).inset->setChange(Change(Change::INSERTED));
800 cellInfo(j).inset->setChange(Change(Change::INSERTED));
807 void Tabular::moveRow(row_type row, RowDirection direction)
809 if (direction == Tabular::UP)
812 std::swap(row_info[row], row_info[row + 1]);
814 for (col_type c = 0; c < ncols(); ++c) {
815 std::swap(cell_info[row][c], cell_info[row + 1][c]);
816 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
817 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
819 // FIXME track changes is broken for tabular features (#8469)
820 idx_type const i = cellIndex(row, c);
821 idx_type const j = cellIndex(row + 1, c);
822 if (buffer().params().track_changes) {
823 cellInfo(i).inset->setChange(Change(Change::INSERTED));
824 cellInfo(j).inset->setChange(Change(Change::INSERTED));
831 void Tabular::deleteColumn(col_type const col)
833 // Not allowed to delete last column
837 for (row_type r = 0; r < nrows(); ++r) {
838 // Care about multicolumn cells
839 if (col + 1 < ncols() &&
840 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
841 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
842 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
844 cell_info[r].erase(cell_info[r].begin() + col);
846 column_info.erase(column_info.begin() + col);
851 void Tabular::copyColumn(col_type const col)
853 insertColumn(col, true);
857 void Tabular::appendColumn(col_type col)
859 insertColumn(col, false);
863 void Tabular::insertColumn(col_type const col, bool copy)
865 BufferParams const & bp = buffer().params();
866 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
868 for (row_type r = 0; r < nrows(); ++r) {
869 cell_info[r].insert(cell_info[r].begin() + col + 1,
870 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
871 if (bp.track_changes)
872 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
873 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
874 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
877 for (row_type r = 0; r < nrows(); ++r) {
878 // inherit line settings
879 idx_type const i = cellIndex(r, col + 1);
880 idx_type const j = cellIndex(r, col);
881 setBottomLine(i, bottomLine(j));
882 setTopLine(i, topLine(j));
883 setLeftLine(i, leftLine(j));
884 setRightLine(i, rightLine(j));
885 if (rightLine(i) && rightLine(j)) {
886 setRightLine(j, false);
888 if (buffer().params().track_changes)
889 cellInfo(i).inset->setChange(Change(Change::INSERTED));
894 void Tabular::updateIndexes()
899 for (row_type row = 0; row < nrows(); ++row)
900 for (col_type column = 0; column < ncols(); ++column) {
901 if (!isPartOfMultiColumn(row, column)
902 && !isPartOfMultiRow(row, column))
904 if (isPartOfMultiRow(row, column))
905 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
907 cell_info[row][column].cellno = numberofcells - 1;
910 rowofcell.resize(numberofcells);
911 columnofcell.resize(numberofcells);
913 // reset column and row of cells and update their width and alignment
914 for (row_type row = 0; row < nrows(); ++row)
915 for (col_type column = 0; column < ncols(); ++column) {
916 if (isPartOfMultiColumn(row, column))
918 // columnofcell needs to be called before setting width and aligment
919 // multirow cells inherit the width from the column width
920 if (!isPartOfMultiRow(row, column)) {
921 columnofcell[i] = column;
924 setFixedWidth(row, column);
925 if (isPartOfMultiRow(row, column))
927 cell_info[row][column].inset->setContentAlignment(
928 getAlignment(cellIndex(row, column)));
934 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
937 for (col_type c = 0; c < ncols(); ++c)
938 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
944 bool Tabular::topLine(idx_type const cell) const
946 return cellInfo(cell).top_line;
950 bool Tabular::bottomLine(idx_type const cell) const
952 return cellInfo(cell).bottom_line;
956 bool Tabular::leftLine(idx_type cell) const
960 return cellInfo(cell).left_line;
964 bool Tabular::rightLine(idx_type cell) const
968 return cellInfo(cell).right_line;
972 int Tabular::interRowSpace(row_type row) const
974 if (!row || row >= nrows())
977 int const interline_space = row_info[row - 1].interline_space_default ?
979 row_info[row - 1].interline_space.inPixels(width());
980 if (rowTopLine(row) && rowBottomLine(row - 1))
981 return interline_space + WIDTH_OF_LINE;
982 return interline_space;
986 int Tabular::interColumnSpace(idx_type cell) const
988 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
989 if (rightLine(cell) && nextcol < ncols()
990 && leftLine(cellIndex(cellRow(cell), nextcol)))
991 return WIDTH_OF_LINE;
996 int Tabular::cellWidth(idx_type cell) const
999 col_type const span = columnSpan(cell);
1000 col_type const col = cellColumn(cell);
1001 for(col_type c = col; c < col + span ; ++c)
1002 w += column_info[c].width;
1007 int Tabular::cellHeight(idx_type cell) const
1009 row_type const span = rowSpan(cell);
1010 row_type const row = cellRow(cell);
1012 for(row_type r = row; r < row + span ; ++r) {
1013 h += rowAscent(r) + rowDescent(r);
1014 if (r != row + span - 1)
1015 h += interRowSpace(r + 1);
1022 bool Tabular::updateColumnWidths()
1024 vector<int> max_dwidth(ncols(), 0);
1025 for(col_type c = 0; c < ncols(); ++c)
1026 for(row_type r = 0; r < nrows(); ++r) {
1027 idx_type const i = cellIndex(r, c);
1028 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1029 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1032 bool update = false;
1033 // for each col get max of single col cells
1034 for(col_type c = 0; c < ncols(); ++c) {
1036 for(row_type r = 0; r < nrows(); ++r) {
1037 idx_type const i = cellIndex(r, c);
1038 if (columnSpan(i) == 1) {
1039 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1040 && cell_info[r][c].decimal_width != 0)
1041 new_width = max(new_width, cellInfo(i).width
1042 + max_dwidth[c] - cellInfo(i).decimal_width);
1044 new_width = max(new_width, cellInfo(i).width);
1048 if (column_info[c].width != new_width) {
1049 column_info[c].width = new_width;
1053 // update col widths to fit merged cells
1054 for(col_type c = 0; c < ncols(); ++c)
1055 for(row_type r = 0; r < nrows(); ++r) {
1056 idx_type const i = cellIndex(r, c);
1057 int const span = columnSpan(i);
1058 if (span == 1 || c > cellColumn(i))
1062 for(col_type j = c; j < c + span ; ++j)
1063 old_width += column_info[j].width;
1065 if (cellInfo(i).width > old_width) {
1066 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1075 int Tabular::width() const
1078 for (col_type c = 0; c < ncols(); ++c)
1079 width += column_info[c].width;
1084 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1087 col_type const col = cellColumn(cell);
1088 // set alignment for the whole row if we are not in a multicolumn cell,
1089 // exclude possible multicolumn cells in the row
1090 if (!isMultiColumn(cell)) {
1091 for (row_type r = 0; r < nrows(); ++r) {
1092 // only if the column has no width the multirow inherits the
1093 // alignment of the column, otherwise it is left aligned
1094 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1095 && !isMultiColumn(cellIndex(r, col))) {
1096 cell_info[r][col].alignment = align;
1097 cell_info[r][col].inset->setContentAlignment(align);
1099 if ((isMultiRow(cellIndex(r, col)) && has_width)
1100 && !isMultiColumn(cellIndex(r, col))) {
1101 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1102 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1105 column_info[col].alignment = align;
1106 docstring & dpoint = column_info[col].decimal_point;
1107 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1108 dpoint = from_utf8(lyxrc.default_decimal_point);
1110 cellInfo(cell).alignment = align;
1111 cellInset(cell)->setContentAlignment(align);
1116 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1119 if (!isMultiColumn(cell) || onlycolumn)
1120 column_info[cellColumn(cell)].valignment = align;
1122 cellInfo(cell).valignment = align;
1129 * Allow line and paragraph breaks for fixed width cells or disallow them,
1130 * merge cell paragraphs and reset layout to standard for variable width
1133 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1135 inset->toggleFixedWidth(fixedWidth);
1139 // merge all paragraphs to one
1140 BufferParams const & bp = cur.bv().buffer().params();
1141 while (inset->paragraphs().size() > 1)
1142 mergeParagraph(bp, inset->paragraphs(), 0);
1146 // undo information has already been recorded
1147 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1148 bp.documentClass().plainLayoutName());
1155 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1156 Length const & width)
1158 col_type const c = cellColumn(cell);
1160 column_info[c].p_width = width;
1161 // reset the vertical alignment to top if the fixed width
1162 // is removed or zero because only fixed width columns can
1163 // have a vertical alignment
1164 if (column_info[c].p_width.zero())
1165 column_info[c].valignment = LYX_VALIGN_TOP;
1166 for (row_type r = 0; r < nrows(); ++r) {
1167 idx_type const cidx = cellIndex(r, c);
1168 // because of multicolumns
1169 toggleFixedWidth(cur, cellInset(cidx).get(),
1170 !getPWidth(cidx).zero());
1171 if (isMultiRow(cidx))
1172 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1174 // cur paragraph can become invalid after paragraphs were merged
1175 if (cur.pit() > cur.lastpit())
1176 cur.pit() = cur.lastpit();
1177 // cur position can become invalid after newlines were removed
1178 if (cur.pos() > cur.lastpos())
1179 cur.pos() = cur.lastpos();
1183 bool Tabular::setFixedWidth(row_type r, col_type c)
1185 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1186 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1187 || (multicol && !cell_info[r][c].p_width.zero());
1188 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1193 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1194 Length const & width)
1196 if (!isMultiColumn(cell))
1199 cellInfo(cell).p_width = width;
1200 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1201 // cur paragraph can become invalid after paragraphs were merged
1202 if (cur.pit() > cur.lastpit())
1203 cur.pit() = cur.lastpit();
1204 // cur position can become invalid after newlines were removed
1205 if (cur.pos() > cur.lastpos())
1206 cur.pos() = cur.lastpos();
1211 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1213 column_info[cellColumn(cell)].varwidth = varwidth;
1218 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1220 cellInfo(cell).mroffset = mroffset;
1225 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1226 Tabular::Feature what)
1228 if (what == SET_SPECIAL_MULTICOLUMN)
1229 cellInfo(cell).align_special = special;
1231 column_info[cellColumn(cell)].align_special = special;
1235 void Tabular::setTopLine(idx_type i, bool line)
1237 cellInfo(i).top_line = line;
1241 void Tabular::setBottomLine(idx_type i, bool line)
1243 cellInfo(i).bottom_line = line;
1247 void Tabular::setLeftLine(idx_type cell, bool line)
1249 cellInfo(cell).left_line = line;
1253 void Tabular::setRightLine(idx_type cell, bool line)
1255 cellInfo(cell).right_line = line;
1258 bool Tabular::rowTopLine(row_type r) const
1260 bool all_rows_set = true;
1261 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1262 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1263 return all_rows_set;
1267 bool Tabular::rowBottomLine(row_type r) const
1269 bool all_rows_set = true;
1270 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1271 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1272 return all_rows_set;
1276 bool Tabular::columnLeftLine(col_type c) const
1283 for (row_type r = 0; r < nrows(); ++r) {
1284 idx_type const i = cellIndex(r, c);
1285 if (c == cellColumn(i)) {
1287 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1288 if (cellInfo(i).left_line || right)
1292 return 2 * nrows_left >= total;
1296 bool Tabular::columnRightLine(col_type c) const
1301 int nrows_right = 0;
1303 for (row_type r = 0; r < nrows(); ++r) {
1304 idx_type i = cellIndex(r, c);
1305 if (c == cellColumn(i) + columnSpan(i) - 1) {
1307 bool left = (c + 1 < ncols()
1308 && cellInfo(cellIndex(r, c + 1)).left_line)
1309 || c + 1 == ncols();
1310 if (cellInfo(i).right_line && left)
1314 return 2 * nrows_right >= total;
1318 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1320 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1321 return cellInfo(cell).alignment;
1323 return column_info[cellColumn(cell)].alignment;
1328 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1330 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1331 return cellInfo(cell).valignment;
1332 return column_info[cellColumn(cell)].valignment;
1336 int Tabular::offsetVAlignment() const
1338 // for top-alignment the first horizontal table line must be exactly at
1339 // the position of the base line of the surrounding text line
1340 // for bottom alignment, the same is for the last table line
1341 int offset_valign = 0;
1342 switch (tabular_valignment) {
1343 case Tabular::LYX_VALIGN_BOTTOM:
1344 offset_valign = rowAscent(0) - height();
1346 case Tabular::LYX_VALIGN_MIDDLE:
1347 offset_valign = (- height()) / 2 + rowAscent(0);
1349 case Tabular::LYX_VALIGN_TOP:
1350 offset_valign = rowAscent(0);
1353 return offset_valign;
1357 Length const Tabular::getPWidth(idx_type cell) const
1359 if (isMultiColumn(cell))
1360 return cellInfo(cell).p_width;
1361 return column_info[cellColumn(cell)].p_width;
1365 Length const Tabular::getMROffset(idx_type cell) const
1367 return cellInfo(cell).mroffset;
1371 int Tabular::textHOffset(idx_type cell) const
1373 // the LaTeX Way :-(
1374 int x = WIDTH_OF_LINE;
1376 int const w = cellWidth(cell) - cellInfo(cell).width;
1378 switch (getAlignment(cell)) {
1379 case LYX_ALIGN_CENTER:
1382 case LYX_ALIGN_RIGHT:
1385 case LYX_ALIGN_DECIMAL: {
1386 // we center when no decimal point
1387 if (cellInfo(cell).decimal_width == 0) {
1391 col_type const c = cellColumn(cell);
1392 int max_dhoffset = 0;
1393 for(row_type r = 0; r < row_info.size() ; ++r) {
1394 idx_type const i = cellIndex(r, c);
1395 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1396 && cellInfo(i).decimal_width != 0)
1397 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1399 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1402 // LYX_ALIGN_LEFT: nothing :-)
1410 int Tabular::textVOffset(idx_type cell) const
1412 int voffset = cellInfo(cell).voffset;
1413 if (isMultiRow(cell)) {
1414 row_type const row = cellRow(cell);
1415 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1421 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1424 idx_type const numcells = numberOfCellsInRow(row);
1425 // we check against numcells to make sure we do not crash if all the
1426 // cells are multirow (bug #7535), but in that case our return value
1427 // is really invalid, i.e., it is NOT the first cell in the row. but
1428 // i do not know what to do here. (rgh)
1429 while (c < numcells - 1
1430 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1432 return cell_info[row][c].cellno;
1436 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1438 col_type c = ncols() - 1;
1439 // of course we check against 0 so we don't crash. but we have the same
1440 // problem as in the previous routine: if all the cells are part of a
1441 // multirow or part of a multi column, then our return value is invalid.
1443 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1444 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1446 return cell_info[row][c].cellno;
1450 Tabular::row_type Tabular::cellRow(idx_type cell) const
1452 if (cell >= numberofcells)
1456 return rowofcell[cell];
1460 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1462 if (cell >= numberofcells)
1466 return columnofcell[cell];
1470 void Tabular::write(ostream & os) const
1474 << write_attribute("version", 3)
1475 << write_attribute("rows", nrows())
1476 << write_attribute("columns", ncols())
1478 // global longtable options
1480 << write_attribute("rotate", rotate)
1481 << write_attribute("booktabs", use_booktabs)
1482 << write_attribute("islongtable", is_long_tabular)
1483 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1484 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1485 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1486 << write_attribute("headTopDL", endhead.topDL)
1487 << write_attribute("headBottomDL", endhead.bottomDL)
1488 << write_attribute("footTopDL", endfoot.topDL)
1489 << write_attribute("footBottomDL", endfoot.bottomDL)
1490 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1491 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1492 << write_attribute("lastFootEmpty", endlastfoot.empty);
1493 // longtables cannot be aligned vertically
1494 if (!is_long_tabular)
1495 os << write_attribute("tabularvalignment", tabular_valignment);
1496 os << write_attribute("tabularwidth", tabular_width);
1497 if (is_long_tabular)
1498 os << write_attribute("longtabularalignment", longtabular_alignment);
1500 for (col_type c = 0; c < ncols(); ++c) {
1502 << write_attribute("alignment", column_info[c].alignment);
1503 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1504 os << write_attribute("decimal_point", column_info[c].decimal_point);
1505 os << write_attribute("valignment", column_info[c].valignment)
1506 << write_attribute("width", column_info[c].p_width.asString())
1507 << write_attribute("varwidth", column_info[c].varwidth)
1508 << write_attribute("special", column_info[c].align_special)
1511 for (row_type r = 0; r < nrows(); ++r) {
1512 static const string def("default");
1514 if (row_info[r].top_space_default)
1515 os << write_attribute("topspace", def);
1517 os << write_attribute("topspace", row_info[r].top_space);
1518 if (row_info[r].bottom_space_default)
1519 os << write_attribute("bottomspace", def);
1521 os << write_attribute("bottomspace", row_info[r].bottom_space);
1522 if (row_info[r].interline_space_default)
1523 os << write_attribute("interlinespace", def);
1525 os << write_attribute("interlinespace", row_info[r].interline_space);
1526 os << write_attribute("endhead", row_info[r].endhead)
1527 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1528 << write_attribute("endfoot", row_info[r].endfoot)
1529 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1530 << write_attribute("newpage", row_info[r].newpage)
1531 << write_attribute("caption", row_info[r].caption)
1533 for (col_type c = 0; c < ncols(); ++c) {
1535 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1536 << write_attribute("multirow", cell_info[r][c].multirow)
1537 << write_attribute("mroffset", cell_info[r][c].mroffset)
1538 << write_attribute("alignment", cell_info[r][c].alignment)
1539 << write_attribute("valignment", cell_info[r][c].valignment)
1540 << write_attribute("topline", cell_info[r][c].top_line)
1541 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1542 << write_attribute("leftline", cell_info[r][c].left_line)
1543 << write_attribute("rightline", cell_info[r][c].right_line)
1544 << write_attribute("rotate", cell_info[r][c].rotate)
1545 << write_attribute("usebox", cell_info[r][c].usebox)
1546 << write_attribute("width", cell_info[r][c].p_width)
1547 << write_attribute("special", cell_info[r][c].align_special)
1549 os << "\\begin_inset ";
1550 cell_info[r][c].inset->write(os);
1551 os << "\n\\end_inset\n"
1553 // FIXME This can be removed again once the mystery
1554 // crash has been resolved.
1559 os << "</lyxtabular>\n";
1563 void Tabular::read(Lexer & lex)
1566 istream & is = lex.getStream();
1568 l_getline(is, line);
1569 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1570 LASSERT(false, return);
1574 if (!getTokenValue(line, "version", version))
1576 LATTEST(version >= 2);
1579 if (!getTokenValue(line, "rows", rows_arg))
1582 if (!getTokenValue(line, "columns", columns_arg))
1584 init(buffer_, rows_arg, columns_arg);
1585 l_getline(is, line);
1586 if (!prefixIs(line, "<features")) {
1587 lyxerr << "Wrong tabular format (expected <features ...> got"
1588 << line << ')' << endl;
1591 getTokenValue(line, "rotate", rotate);
1592 getTokenValue(line, "booktabs", use_booktabs);
1593 getTokenValue(line, "islongtable", is_long_tabular);
1594 getTokenValue(line, "tabularvalignment", tabular_valignment);
1595 getTokenValue(line, "tabularwidth", tabular_width);
1596 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1597 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1598 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1599 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1600 getTokenValue(line, "headTopDL", endhead.topDL);
1601 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1602 getTokenValue(line, "footTopDL", endfoot.topDL);
1603 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1604 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1605 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1606 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1608 for (col_type c = 0; c < ncols(); ++c) {
1610 if (!prefixIs(line,"<column")) {
1611 lyxerr << "Wrong tabular format (expected <column ...> got"
1612 << line << ')' << endl;
1615 getTokenValue(line, "alignment", column_info[c].alignment);
1616 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1617 getTokenValue(line, "valignment", column_info[c].valignment);
1618 getTokenValue(line, "width", column_info[c].p_width);
1619 getTokenValue(line, "special", column_info[c].align_special);
1620 getTokenValue(line, "varwidth", column_info[c].varwidth);
1623 for (row_type i = 0; i < nrows(); ++i) {
1624 l_getline(is, line);
1625 if (!prefixIs(line, "<row")) {
1626 lyxerr << "Wrong tabular format (expected <row ...> got"
1627 << line << ')' << endl;
1630 getTokenValue(line, "topspace", row_info[i].top_space,
1631 row_info[i].top_space_default);
1632 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1633 row_info[i].bottom_space_default);
1634 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1635 row_info[i].interline_space_default);
1636 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1637 getTokenValue(line, "endhead", row_info[i].endhead);
1638 getTokenValue(line, "endfoot", row_info[i].endfoot);
1639 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1640 getTokenValue(line, "newpage", row_info[i].newpage);
1641 getTokenValue(line, "caption", row_info[i].caption);
1642 for (col_type j = 0; j < ncols(); ++j) {
1643 l_getline(is, line);
1644 if (!prefixIs(line, "<cell")) {
1645 lyxerr << "Wrong tabular format (expected <cell ...> got"
1646 << line << ')' << endl;
1649 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1650 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1651 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1652 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1653 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1654 getTokenValue(line, "topline", cell_info[i][j].top_line);
1655 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1656 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1657 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1658 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1659 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1660 getTokenValue(line, "width", cell_info[i][j].p_width);
1662 getTokenValue(line, "special", cell_info[i][j].align_special);
1663 l_getline(is, line);
1664 if (prefixIs(line, "\\begin_inset")) {
1665 cell_info[i][j].inset->setBuffer(*buffer_);
1666 cell_info[i][j].inset->read(lex);
1667 l_getline(is, line);
1669 if (!prefixIs(line, "</cell>")) {
1670 lyxerr << "Wrong tabular format (expected </cell> got"
1671 << line << ')' << endl;
1675 l_getline(is, line);
1676 if (!prefixIs(line, "</row>")) {
1677 lyxerr << "Wrong tabular format (expected </row> got"
1678 << line << ')' << endl;
1682 while (!prefixIs(line, "</lyxtabular>")) {
1683 l_getline(is, line);
1689 bool Tabular::isMultiColumn(idx_type cell) const
1691 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1692 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1696 bool Tabular::hasMultiColumn(col_type c) const
1698 for (row_type r = 0; r < nrows(); ++r) {
1699 if (isMultiColumn(cellIndex(r, c)))
1706 bool Tabular::hasVarwidthColumn() const
1708 for (col_type c = 0; c < ncols(); ++c) {
1709 if (column_info[c].varwidth)
1717 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1719 return cell_info[cellRow(cell)][cellColumn(cell)];
1723 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1725 return cell_info[cellRow(cell)][cellColumn(cell)];
1729 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1730 bool const right_border)
1732 idx_type const col = cellColumn(cell);
1733 idx_type const row = cellRow(cell);
1734 for (idx_type i = 0; i < number; ++i)
1735 unsetMultiRow(cellIndex(row, col + i));
1737 // unsetting of multirow may have invalidated cell index
1738 cell = cellIndex(row, col);
1739 CellData & cs = cellInfo(cell);
1740 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1741 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1742 cs.alignment = column_info[col].alignment;
1743 setRightLine(cell, right_border);
1745 idx_type lastcell = cellIndex(row, col + number - 1);
1746 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1747 CellData & cs1 = cellInfo(cell + i);
1748 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1749 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1757 bool Tabular::isMultiRow(idx_type cell) const
1759 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1760 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1763 bool Tabular::hasMultiRow(row_type r) const
1765 for (col_type c = 0; c < ncols(); ++c) {
1766 if (isMultiRow(cellIndex(r, c)))
1772 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1773 bool const bottom_border,
1774 LyXAlignment const halign)
1776 idx_type const col = cellColumn(cell);
1777 idx_type const row = cellRow(cell);
1778 for (idx_type i = 0; i < number; ++i)
1779 unsetMultiColumn(cellIndex(row + i, col));
1781 // unsetting of multirow may have invalidated cell index
1782 cell = cellIndex(row, col);
1783 CellData & cs = cellInfo(cell);
1784 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1785 cs.valignment = LYX_VALIGN_MIDDLE;
1786 // the horizontal alignment of multirow cells can only
1787 // be changed for the whole table row,
1788 // support changing this only for the multirow cell can be done via
1790 if (getPWidth(cell).zero())
1791 cs.alignment = halign;
1793 cs.alignment = LYX_ALIGN_LEFT;
1795 // set the bottom line of the last selected cell
1796 setBottomLine(cell, bottom_border);
1798 for (idx_type i = 1; i < number; ++i) {
1799 CellData & cs1 = cell_info[row + i][col];
1800 cs1.multirow = CELL_PART_OF_MULTIROW;
1801 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1809 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1811 row_type const row = cellRow(cell);
1812 col_type const col = cellColumn(cell);
1814 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1821 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1823 col_type const column = cellColumn(cell);
1824 col_type row = cellRow(cell) + 1;
1825 while (row < nrows() && isPartOfMultiRow(row, column))
1828 return row - cellRow(cell);
1832 void Tabular::unsetMultiColumn(idx_type cell)
1834 if (!isMultiColumn(cell))
1837 row_type const row = cellRow(cell);
1838 col_type const col = cellColumn(cell);
1839 row_type const span = columnSpan(cell);
1840 for (col_type c = 0; c < span; ++c) {
1841 // in the table dialog the lines are set in every case
1842 // when unsetting a multicolumn this leads to an additional right
1843 // line for every cell that was part of the former multicolumn cell,
1844 // except if the cell is in the last column
1845 // therefore remove this line
1846 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1847 && (col + c) < (col + span - 1))
1848 cell_info[row][col + c].right_line = false;
1849 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1855 void Tabular::unsetMultiRow(idx_type cell)
1857 if (!isMultiRow(cell))
1860 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1861 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1862 row_type const row = cellRow(cell);
1863 col_type const col = cellColumn(cell);
1864 row_type const span = rowSpan(cell);
1865 for (row_type r = 0; r < span; ++r)
1866 cell_info[row + r][col].multirow = CELL_NORMAL;
1871 void Tabular::setRotateCell(idx_type cell, int value)
1873 cellInfo(cell).rotate = value;
1877 int Tabular::getRotateCell(idx_type cell) const
1879 return cellInfo(cell).rotate;
1883 bool Tabular::needRotating() const
1885 if (rotate && !is_long_tabular)
1887 for (row_type r = 0; r < nrows(); ++r)
1888 for (col_type c = 0; c < ncols(); ++c)
1889 if (cell_info[r][c].rotate != 0)
1895 bool Tabular::isLastCell(idx_type cell) const
1897 if (cell + 1 < numberofcells)
1903 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1905 if (cellRow(cell) == 0)
1908 col_type const col = cellColumn(cell);
1909 row_type r = cellRow(cell) - 1;
1910 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1913 return cell_info[r][col].cellno;
1917 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1919 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1920 if (nextrow < nrows())
1921 return cell_info[nextrow][cellColumn(cell)].cellno;
1926 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1928 LASSERT(column != npos && column < ncols(), column = 0);
1929 LASSERT(row != npos && row < nrows(), row = 0);
1930 return cell_info[row][column].cellno;
1934 void Tabular::setUsebox(idx_type cell, BoxType type)
1936 cellInfo(cell).usebox = type;
1940 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1941 // adds line breaks, see bug 4886.
1942 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1944 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1945 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1947 if (cellInfo(cell).usebox > 1)
1948 return cellInfo(cell).usebox;
1949 return useParbox(cell);
1954 // This are functions used for the longtable support
1956 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1962 row_info[row].endfirsthead = flag;
1966 row_info[row].endhead = flag;
1971 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1974 hd.set = haveLTHead();
1975 return row_info[row].endhead;
1979 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1982 hd.set = haveLTFirstHead();
1983 return row_info[row].endfirsthead;
1987 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1993 row_info[row].endlastfoot = flag;
1997 row_info[row].endfoot = flag;
2002 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2005 fd.set = haveLTFoot();
2006 return row_info[row].endfoot;
2010 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2013 fd.set = haveLTLastFoot();
2014 return row_info[row].endlastfoot;
2018 void Tabular::setLTNewPage(row_type row, bool what)
2020 row_info[row].newpage = what;
2024 bool Tabular::getLTNewPage(row_type row) const
2026 return row_info[row].newpage;
2030 bool Tabular::haveLTHead(bool withcaptions) const
2032 if (!is_long_tabular)
2034 for (row_type i = 0; i < nrows(); ++i)
2035 if (row_info[i].endhead &&
2036 (withcaptions || !row_info[i].caption))
2042 bool Tabular::haveLTFirstHead(bool withcaptions) const
2044 if (!is_long_tabular || endfirsthead.empty)
2046 for (row_type r = 0; r < nrows(); ++r)
2047 if (row_info[r].endfirsthead &&
2048 (withcaptions || !row_info[r].caption))
2054 bool Tabular::haveLTFoot(bool withcaptions) const
2056 if (!is_long_tabular)
2058 for (row_type r = 0; r < nrows(); ++r)
2059 if (row_info[r].endfoot &&
2060 (withcaptions || !row_info[r].caption))
2066 bool Tabular::haveLTLastFoot(bool withcaptions) const
2068 if (!is_long_tabular || endlastfoot.empty)
2070 for (row_type r = 0; r < nrows(); ++r)
2071 if (row_info[r].endlastfoot &&
2072 (withcaptions || !row_info[r].caption))
2078 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2080 idx_type i = getFirstCellInRow(row);
2082 setMultiColumn(i, numberOfCellsInRow(row), false);
2083 setTopLine(i, false);
2084 setBottomLine(i, false);
2085 setLeftLine(i, false);
2086 setRightLine(i, false);
2087 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2088 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2089 setLTHead(row, true, endfirsthead, true);
2090 row_info[row].endfirsthead = true;
2093 unsetMultiColumn(i);
2094 // When unsetting a caption row, also all existing
2095 // captions in this row must be dissolved.
2097 row_info[row].caption = what;
2102 bool Tabular::ltCaption(row_type row) const
2104 return row_info[row].caption;
2108 bool Tabular::haveLTCaption(CaptionType captiontype) const
2110 if (!is_long_tabular)
2112 for (row_type r = 0; r < nrows(); ++r) {
2113 if (row_info[r].caption) {
2114 switch (captiontype) {
2115 case CAPTION_FIRSTHEAD:
2116 if (row_info[r].endfirsthead)
2120 if (row_info[r].endhead)
2124 if (row_info[r].endfoot)
2127 case CAPTION_LASTFOOT:
2128 if (row_info[r].endlastfoot)
2140 // end longtable support functions
2142 void Tabular::setRowAscent(row_type row, int height)
2144 if (row >= nrows() || row_info[row].ascent == height)
2146 row_info[row].ascent = height;
2150 void Tabular::setRowDescent(row_type row, int height)
2152 if (row >= nrows() || row_info[row].descent == height)
2154 row_info[row].descent = height;
2158 int Tabular::rowAscent(row_type row) const
2160 LASSERT(row < nrows(), row = 0);
2161 return row_info[row].ascent;
2165 int Tabular::rowDescent(row_type row) const
2167 LASSERT(row < nrows(), row = 0);
2168 return row_info[row].descent;
2172 int Tabular::height() const
2175 for (row_type row = 0; row < nrows(); ++row)
2176 height += rowAscent(row) + rowDescent(row) +
2182 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2184 LASSERT(row < nrows(), return false);
2185 LASSERT(column < ncols(), return false);
2186 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2190 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2192 LASSERT(row < nrows(), return false);
2193 LASSERT(column < ncols(), return false);
2194 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2198 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang,
2199 list<col_type> columns) const
2201 // we only output complete row lines and the 1st row here, the rest
2202 // is done in Tabular::TeXBottomHLine(...)
2204 // get for each column the topline (if any)
2205 map<col_type, bool> topline;
2207 for (auto const & c : columns) {
2208 topline[c] = topLine(cellIndex(row, c));
2209 // If cell is part of a multirow and not the first cell of the
2210 // multirow, no line must be drawn.
2212 if (isMultiRow(cellIndex(row, c))
2213 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2215 if (topline.find(c) != topline.end() && topline.find(c)->second)
2219 // do nothing if empty first row, or incomplete row line after
2220 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2223 // only output complete row lines and the 1st row's clines
2224 if (nset == ncols()) {
2226 os << (row == 0 ? "\\toprule " : "\\midrule ");
2230 } else if (row == 0) {
2231 for (auto & c : columns) {
2232 if (topline.find(c)->second) {
2233 col_type offset = 0;
2234 for (col_type j = 0 ; j < c; ++j)
2235 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2238 //babel makes the "-" character an active one, so we have to suppress this here
2239 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2240 if (lang == "slovak" || lang == "czech")
2241 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2242 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2244 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2246 col_type cstart = c;
2247 for ( ; c < ncols() && topline.find(c)->second; ++c) {}
2249 for (col_type j = cstart ; j < c ; ++j)
2250 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2253 os << c + offset << "} ";
2261 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang,
2262 list<col_type> columns) const
2264 // we output bottomlines of row r and the toplines of row r+1
2265 // if the latter do not span the whole tabular
2267 // get the bottomlines of row r, and toplines in next row
2268 bool lastrow = row == nrows() - 1;
2269 map<col_type, bool> bottomline, topline;
2270 bool nextrowset = true;
2271 for (auto const & c : columns) {
2272 bottomline[c] = bottomLine(cellIndex(row, c));
2273 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2274 // If cell is part of a multirow and not the last cell of the
2275 // multirow, no line must be drawn.
2277 if (isMultiRow(cellIndex(row, c))
2278 && isMultiRow(cellIndex(row + 1, c))
2279 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2280 bottomline[c] = false;
2283 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2286 // combine this row's bottom lines and next row's toplines if necessary
2288 for (auto const & c : columns) {
2290 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2291 if (bottomline.find(c)->second)
2295 // do nothing if empty, OR incomplete row line with a topline in next row
2296 if (nset == 0 || (nextrowset && nset != ncols()))
2299 if (nset == ncols()) {
2301 os << (lastrow ? "\\bottomrule" : "\\midrule");
2305 for (auto & c : columns) {
2306 if (bottomline.find(c)->second) {
2307 col_type offset = 0;
2308 for (col_type j = 0 ; j < c; ++j)
2309 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2312 //babel makes the "-" character an active one, so we have to suppress this here
2313 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2314 if (lang == "slovak" || lang == "czech")
2315 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2316 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2318 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2320 col_type cstart = c;
2321 for ( ; c < ncols() && bottomline.find(c)->second; ++c) {}
2323 for (col_type j = cstart ; j < c ; ++j)
2324 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2327 os << c + offset << "} ";
2335 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2336 bool & ismulticol, bool & ismultirow,
2337 bool const bidi) const
2339 row_type const r = cellRow(cell);
2340 if (is_long_tabular && row_info[r].caption)
2343 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2344 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2345 // figure out how to set the lines
2346 // we always set double lines to the right of the cell
2347 // or left in bidi RTL, respectively.
2348 col_type const c = cellColumn(cell);
2349 col_type const nextcol = c + columnSpan(cell);
2350 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2351 bool colright = columnRightLine(c);
2352 bool colleft = columnLeftLine(c);
2353 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2354 bool nextcellleft = nextcol < ncols()
2355 && leftLine(cellIndex(r, nextcol));
2356 bool coldouble = colright && nextcolleft;
2357 bool celldouble = rightLine(cell) && nextcellleft;
2359 ismulticol = (isMultiColumn(cell)
2360 || (c == 0 && colleft != leftLine(cell))
2361 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2362 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2363 || (coldouble != celldouble))
2366 // we center in multicol when no decimal point
2368 docstring const align_d = column_info[c].decimal_point;
2369 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2370 bool const nosep = !dit;
2371 ismulticol |= nosep;
2372 celldouble &= nosep;
2375 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2376 int latexcolspan = columnSpan(cell);
2377 for (col_type col = c; col < c + columnSpan(cell); ++col)
2378 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2382 os << "\\multicolumn{" << latexcolspan << "}{";
2383 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2384 || (!bidi && c == 0 && leftLine(cell))))
2386 if (bidi && celldouble)
2387 // add extra vertical line if we want a double one
2389 if (!cellInfo(cell).align_special.empty()) {
2390 os << cellInfo(cell).align_special;
2392 if (!getPWidth(cell).zero()) {
2394 case LYX_ALIGN_LEFT:
2395 os << ">{\\raggedright}";
2397 case LYX_ALIGN_RIGHT:
2398 os << ">{\\raggedleft}";
2400 case LYX_ALIGN_CENTER:
2401 os << ">{\\centering}";
2407 case LYX_VALIGN_TOP:
2410 case LYX_VALIGN_MIDDLE:
2413 case LYX_VALIGN_BOTTOM:
2418 << from_ascii(getPWidth(cell).asLatexString())
2422 case LYX_ALIGN_LEFT:
2425 case LYX_ALIGN_RIGHT:
2432 } // end if else !getPWidth
2433 } // end if else !cellinfo_of_cell
2434 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2436 if (!bidi && celldouble)
2437 // add extra vertical line if we want a double one
2440 } // end if ismulticol
2442 // we only need code for the first multirow cell
2443 ismultirow = isMultiRow(cell);
2445 os << "\\multirow{" << rowSpan(cell) << "}{";
2446 if (!getPWidth(cell).zero())
2447 os << from_ascii(getPWidth(cell).asLatexString());
2449 // we need to set a default value
2452 if (!getMROffset(cell).zero())
2453 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2455 } // end if ismultirow
2457 if (getRotateCell(cell) != 0)
2458 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2460 if (getUsebox(cell) == BOX_PARBOX) {
2463 case LYX_VALIGN_TOP:
2466 case LYX_VALIGN_MIDDLE:
2469 case LYX_VALIGN_BOTTOM:
2473 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2475 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2476 os << "\\begin{minipage}[";
2478 case LYX_VALIGN_TOP:
2481 case LYX_VALIGN_MIDDLE:
2484 case LYX_VALIGN_BOTTOM:
2488 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2494 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2495 bool ismulticol, bool ismultirow) const
2497 row_type const r = cellRow(cell);
2498 if (is_long_tabular && row_info[r].caption)
2502 if (getUsebox(cell) == BOX_PARBOX)
2504 else if (getUsebox(cell) == BOX_MINIPAGE)
2505 os << breakln << "\\end{minipage}";
2506 if (getRotateCell(cell) != 0)
2507 os << breakln << "\\end{turn}";
2515 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2516 OutputParams const & runparams,
2517 list<col_type> columns) const
2519 if (!is_long_tabular)
2523 // output caption which is in no header or footer
2524 if (haveLTCaption()) {
2525 for (row_type r = 0; r < nrows(); ++r) {
2526 if (row_info[r].caption &&
2527 !row_info[r].endfirsthead && !row_info[r].endhead &&
2528 !row_info[r].endfoot && !row_info[r].endlastfoot)
2529 TeXRow(os, r, runparams, columns);
2532 // output first header info
2533 if (haveLTFirstHead()) {
2534 if (endfirsthead.topDL)
2536 for (row_type r = 0; r < nrows(); ++r) {
2537 if (row_info[r].endfirsthead)
2538 TeXRow(os, r, runparams, columns);
2540 if (endfirsthead.bottomDL)
2542 os << "\\endfirsthead\n";
2544 // output header info
2546 if (endfirsthead.empty && !haveLTFirstHead())
2547 os << "\\endfirsthead\n";
2550 for (row_type r = 0; r < nrows(); ++r) {
2551 if (row_info[r].endhead)
2552 TeXRow(os, r, runparams, columns);
2554 if (endhead.bottomDL)
2556 os << "\\endhead\n";
2558 // output footer info
2562 for (row_type r = 0; r < nrows(); ++r) {
2563 if (row_info[r].endfoot)
2564 TeXRow(os, r, runparams, columns);
2566 if (endfoot.bottomDL)
2568 os << "\\endfoot\n";
2569 if (endlastfoot.empty && !haveLTLastFoot())
2570 os << "\\endlastfoot\n";
2572 // output lastfooter info
2573 if (haveLTLastFoot()) {
2574 if (endlastfoot.topDL)
2576 for (row_type r = 0; r < nrows(); ++r) {
2577 if (row_info[r].endlastfoot)
2578 TeXRow(os, r, runparams, columns);
2580 if (endlastfoot.bottomDL)
2582 os << "\\endlastfoot\n";
2587 bool Tabular::isValidRow(row_type row) const
2589 if (!is_long_tabular)
2591 return !row_info[row].endhead && !row_info[row].endfirsthead
2592 && !row_info[row].endfoot && !row_info[row].endlastfoot
2593 && !row_info[row].caption;
2597 void Tabular::TeXRow(otexstream & os, row_type row,
2598 OutputParams const & runparams,
2599 list<col_type> columns) const
2601 idx_type cell = cellIndex(row, 0);
2602 InsetTableCell const * cinset = cellInset(cell);
2603 Paragraph const & cpar = cinset->paragraphs().front();
2604 string const clang = cpar.getParLanguage(buffer().params())->lang();
2606 //output the top line
2607 TeXTopHLine(os, row, clang, columns);
2609 if (row_info[row].top_space_default) {
2611 os << "\\addlinespace\n";
2613 os << "\\noalign{\\vskip\\doublerulesep}\n";
2614 } else if(!row_info[row].top_space.zero()) {
2616 os << "\\addlinespace["
2617 << from_ascii(row_info[row].top_space.asLatexString())
2620 os << "\\noalign{\\vskip"
2621 << from_ascii(row_info[row].top_space.asLatexString())
2625 bool ismulticol = false;
2626 bool ismultirow = false;
2628 // The bidi package (loaded by polyglossia) reverses RTL table columns
2629 bool const bidi_rtl =
2630 runparams.local_font->isRightToLeft()
2631 && runparams.use_polyglossia;
2633 bidi_rtl ? getFirstCellInRow(row) : getLastCellInRow(row);
2635 for (auto const & c : columns) {
2636 if (isPartOfMultiColumn(row, c))
2639 cell = cellIndex(row, c);
2641 if (isPartOfMultiRow(row, c)
2642 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2643 if (cell != lastcell)
2648 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
2649 InsetTableCell const * inset = cellInset(cell);
2651 Paragraph const & par = inset->paragraphs().front();
2653 os.texrow().forceStart(par.id(), 0);
2655 bool rtl = par.isRTL(buffer().params())
2657 && getPWidth(cell).zero()
2658 && !runparams.use_polyglossia;
2662 par.getParLanguage(buffer().params())->lang();
2663 if (lang == "farsi")
2665 else if (lang == "arabic_arabi")
2667 // currently, remaining RTL languages are
2668 // arabic_arabtex and hebrew
2672 // pass to the OutputParams that we are in a cell and
2673 // which alignment we have set.
2674 // InsetNewline needs this context information.
2675 OutputParams newrp(runparams);
2676 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2677 ? OutputParams::PLAIN
2678 : OutputParams::ALIGNED;
2680 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2681 // copy cell and split in 2
2682 InsetTableCell head = InsetTableCell(*cellInset(cell));
2683 head.setBuffer(const_cast<Buffer &>(buffer()));
2684 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2686 dit.push_back(CursorSlice(head));
2687 head.setMacrocontextPositionRecursive(dit);
2688 bool hassep = false;
2689 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2691 tail.setBuffer(head.buffer());
2693 dit.push_back(CursorSlice(tail));
2694 tail.setMacrocontextPositionRecursive(dit);
2698 tail.latex(os, newrp);
2701 head.latex(os, newrp);
2703 head.latex(os, newrp);
2706 tail.latex(os, newrp);
2709 } else if (ltCaption(row)) {
2710 // Inside longtable caption rows, we must only output the caption inset
2711 // with its content and omit anything outside of that (see #10791)
2712 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
2713 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
2714 for (; it != i_end; ++it) {
2715 if (it->lyxCode() != CAPTION_CODE)
2717 it->latex(os, runparams);
2720 } else if (!isPartOfMultiRow(row, c)) {
2721 if (!runparams.nice)
2722 os.texrow().start(par.id(), 0);
2723 inset->latex(os, newrp);
2726 runparams.encoding = newrp.encoding;
2730 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2731 if (cell != lastcell) { // not last cell in row
2738 os << "\\tabularnewline";
2739 if (row_info[row].bottom_space_default) {
2741 os << "\\addlinespace";
2743 os << "[\\doublerulesep]";
2744 } else if (!row_info[row].bottom_space.zero()) {
2746 os << "\\addlinespace";
2748 << from_ascii(row_info[row].bottom_space.asLatexString())
2753 //output the bottom line
2754 TeXBottomHLine(os, row, clang, columns);
2756 if (row_info[row].interline_space_default) {
2758 os << "\\addlinespace\n";
2760 os << "\\noalign{\\vskip\\doublerulesep}\n";
2761 } else if (!row_info[row].interline_space.zero()) {
2763 os << "\\addlinespace["
2764 << from_ascii(row_info[row].interline_space.asLatexString())
2767 os << "\\noalign{\\vskip"
2768 << from_ascii(row_info[row].interline_space.asLatexString())
2774 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2776 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
2777 && !hasVarwidthColumn();
2778 bool const is_xltabular = is_long_tabular
2779 && (hasVarwidthColumn() || !tabular_width.zero());
2780 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
2782 //+---------------------------------------------------------------------
2783 //+ first the opening preamble +
2784 //+---------------------------------------------------------------------
2787 if (!TexRow::isNone(pos))
2788 os.texrow().start(pos);
2791 if (is_long_tabular)
2792 os << "\\begin{landscape}\n";
2794 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2797 if (is_long_tabular) {
2799 os << "\\begin{xltabular}";
2801 os << "\\begin{longtable}";
2802 switch (longtabular_alignment) {
2803 case LYX_LONGTABULAR_ALIGN_LEFT:
2806 case LYX_LONGTABULAR_ALIGN_CENTER:
2809 case LYX_LONGTABULAR_ALIGN_RIGHT:
2814 if (tabular_width.zero())
2815 os << "{" << from_ascii("\\columnwidth") << "}";
2817 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
2820 if (is_tabular_star)
2821 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2822 else if (hasVarwidthColumn()) {
2823 os << "\\begin{tabularx}{";
2824 if (tabular_width.zero())
2825 os << from_ascii("\\columnwidth") << "}";
2827 os << from_ascii(tabular_width.asLatexString()) << "}";
2829 os << "\\begin{tabular}";
2830 switch (tabular_valignment) {
2831 case LYX_VALIGN_TOP:
2834 case LYX_VALIGN_MIDDLE:
2836 case LYX_VALIGN_BOTTOM:
2844 if (is_tabular_star)
2845 os << "@{\\extracolsep{\\fill}}";
2847 // The bidi package (loaded by polyglossia) swaps the column
2848 // order for RTL (#9686). Thus we use this list.
2849 bool const bidi_rtl =
2850 runparams.local_font->isRightToLeft()
2851 && runparams.use_polyglossia;
2852 list<col_type> columns;
2853 for (col_type cl = 0; cl < ncols(); ++cl) {
2855 columns.push_front(cl);
2857 columns.push_back(cl);
2860 for (auto const & c : columns) {
2861 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
2863 if (!column_info[c].align_special.empty()) {
2864 os << column_info[c].align_special;
2866 if (!column_info[c].p_width.zero()) {
2867 bool decimal = false;
2868 switch (column_info[c].alignment) {
2869 case LYX_ALIGN_LEFT:
2870 os << ">{\\raggedright}";
2872 case LYX_ALIGN_RIGHT:
2873 os << ">{\\raggedleft}";
2875 case LYX_ALIGN_CENTER:
2876 os << ">{\\centering}";
2878 case LYX_ALIGN_NONE:
2879 case LYX_ALIGN_BLOCK:
2880 case LYX_ALIGN_LAYOUT:
2881 case LYX_ALIGN_SPECIAL:
2883 case LYX_ALIGN_DECIMAL: {
2885 os << ">{\\raggedright}";
2887 os << ">{\\raggedleft}";
2894 switch (column_info[c].valignment) {
2895 case LYX_VALIGN_TOP:
2896 // this is the default
2898 case LYX_VALIGN_MIDDLE:
2901 case LYX_VALIGN_BOTTOM:
2907 // Fixed-width cells with alignment at decimal separator
2908 // are output as two cells of half the width with the decimal
2909 // separator as column sep. This effectively puts the content
2910 // centered, which differs from the normal decimal sep alignment
2911 // and is not ideal, but we cannot do better ATM (see #9568).
2912 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
2914 docstring const halffixedwith =
2915 from_ascii(Length(column_info[c].p_width.value() / 2,
2916 column_info[c].p_width.unit()).asLatexString());
2920 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
2927 << from_ascii(column_info[c].p_width.asLatexString())
2929 } else if (column_info[c].varwidth) {
2930 switch (column_info[c].alignment) {
2931 case LYX_ALIGN_LEFT:
2932 os << ">{\\raggedright\\arraybackslash}";
2934 case LYX_ALIGN_RIGHT:
2935 os << ">{\\raggedleft\\arraybackslash}";
2937 case LYX_ALIGN_CENTER:
2938 os << ">{\\centering\\arraybackslash}";
2940 case LYX_ALIGN_NONE:
2941 case LYX_ALIGN_BLOCK:
2942 case LYX_ALIGN_LAYOUT:
2943 case LYX_ALIGN_SPECIAL:
2944 case LYX_ALIGN_DECIMAL:
2949 switch (column_info[c].alignment) {
2950 case LYX_ALIGN_LEFT:
2953 case LYX_ALIGN_RIGHT:
2956 case LYX_ALIGN_DECIMAL:
2957 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2963 } // end if else !column_info[i].p_width
2964 } // end if else !column_info[i].align_special
2965 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
2970 TeXLongtableHeaderFooter(os, runparams, columns);
2972 //+---------------------------------------------------------------------
2973 //+ the single row and columns (cells) +
2974 //+---------------------------------------------------------------------
2976 for (row_type r = 0; r < nrows(); ++r) {
2977 if (isValidRow(r)) {
2978 TeXRow(os, r, runparams, columns);
2979 if (is_long_tabular && row_info[r].newpage)
2980 os << "\\newpage\n";
2984 //+---------------------------------------------------------------------
2985 //+ the closing of the tabular +
2986 //+---------------------------------------------------------------------
2988 if (is_long_tabular) {
2990 os << "\\end{xltabular}";
2992 os << "\\end{longtable}";
2994 if (is_tabular_star)
2995 os << "\\end{tabular*}";
2996 else if (hasVarwidthColumn())
2997 os << "\\end{tabularx}";
2999 os << "\\end{tabular}";
3003 if (is_long_tabular)
3004 os << breakln << "\\end{landscape}";
3006 os << breakln << "\\end{turn}";
3009 if (!TexRow::isNone(pos))
3010 os.texrow().start(pos);
3014 int Tabular::docbookRow(odocstream & os, row_type row,
3015 OutputParams const & runparams) const
3018 idx_type cell = getFirstCellInRow(row);
3021 for (col_type c = 0; c < ncols(); ++c) {
3022 if (isPartOfMultiColumn(row, c))
3025 os << "<entry align=\"";
3026 switch (getAlignment(cell)) {
3027 case LYX_ALIGN_LEFT:
3030 case LYX_ALIGN_RIGHT:
3038 os << "\" valign=\"";
3039 switch (getVAlignment(cell)) {
3040 case LYX_VALIGN_TOP:
3043 case LYX_VALIGN_BOTTOM:
3046 case LYX_VALIGN_MIDDLE:
3051 if (isMultiColumn(cell)) {
3052 os << " namest=\"col" << c << "\" ";
3053 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3057 ret += cellInset(cell)->docbook(os, runparams);
3066 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3070 //+---------------------------------------------------------------------
3071 //+ first the opening preamble +
3072 //+---------------------------------------------------------------------
3074 os << "<tgroup cols=\"" << ncols()
3075 << "\" colsep=\"1\" rowsep=\"1\">\n";
3077 for (col_type c = 0; c < ncols(); ++c) {
3078 os << "<colspec colname=\"col" << c << "\" align=\"";
3079 switch (column_info[c].alignment) {
3080 case LYX_ALIGN_LEFT:
3083 case LYX_ALIGN_RIGHT:
3091 if (runparams.flavor == OutputParams::XML)
3097 //+---------------------------------------------------------------------
3098 //+ Long Tabular case +
3099 //+---------------------------------------------------------------------
3101 // output caption info
3102 // The caption flag wins over head/foot
3103 if (haveLTCaption()) {
3104 os << "<caption>\n";
3106 for (row_type r = 0; r < nrows(); ++r) {
3107 if (row_info[r].caption) {
3108 ret += docbookRow(os, r, runparams);
3111 os << "</caption>\n";
3114 // output header info
3115 if (haveLTHead(false) || haveLTFirstHead(false)) {
3118 for (row_type r = 0; r < nrows(); ++r) {
3119 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3120 !row_info[r].caption) {
3121 ret += docbookRow(os, r, runparams);
3127 // output footer info
3128 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3131 for (row_type r = 0; r < nrows(); ++r) {
3132 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3133 !row_info[r].caption) {
3134 ret += docbookRow(os, r, runparams);
3141 //+---------------------------------------------------------------------
3142 //+ the single row and columns (cells) +
3143 //+---------------------------------------------------------------------
3147 for (row_type r = 0; r < nrows(); ++r) {
3148 if (isValidRow(r)) {
3149 ret += docbookRow(os, r, runparams);
3154 //+---------------------------------------------------------------------
3155 //+ the closing of the tabular +
3156 //+---------------------------------------------------------------------
3165 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3166 OutputParams const & runparams, bool header) const
3169 string const celltag = header ? "th" : "td";
3170 idx_type cell = getFirstCellInRow(row);
3172 xs << html::StartTag("tr");
3173 for (col_type c = 0; c < ncols(); ++c) {
3174 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3179 Length const cwidth = column_info[c].p_width;
3180 if (!cwidth.zero()) {
3181 string const hwidth = cwidth.asHTMLString();
3182 attr << "style =\"width: " << hwidth << ";\" ";
3186 switch (getAlignment(cell)) {
3187 case LYX_ALIGN_LEFT:
3190 case LYX_ALIGN_RIGHT:
3198 attr << " valign='";
3199 switch (getVAlignment(cell)) {
3200 case LYX_VALIGN_TOP:
3203 case LYX_VALIGN_BOTTOM:
3206 case LYX_VALIGN_MIDDLE:
3211 if (isMultiColumn(cell))
3212 attr << " colspan='" << columnSpan(cell) << "'";
3213 else if (isMultiRow(cell))
3214 attr << " rowspan='" << rowSpan(cell) << "'";
3216 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3217 ret += cellInset(cell)->xhtml(xs, runparams);
3218 xs << html::EndTag(celltag) << html::CR();
3221 xs << html::EndTag("tr");
3226 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3230 if (is_long_tabular) {
3231 // we'll wrap it in a div, so as to deal with alignment
3233 switch (longtabular_alignment) {
3234 case LYX_LONGTABULAR_ALIGN_LEFT:
3237 case LYX_LONGTABULAR_ALIGN_CENTER:
3240 case LYX_LONGTABULAR_ALIGN_RIGHT:
3244 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3246 // The caption flag wins over head/foot
3247 if (haveLTCaption()) {
3248 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3250 for (row_type r = 0; r < nrows(); ++r)
3251 if (row_info[r].caption)
3252 ret += xhtmlRow(xs, r, runparams);
3253 xs << html::EndTag("div") << html::CR();
3257 xs << html::StartTag("table") << html::CR();
3259 // output header info
3260 bool const havefirsthead = haveLTFirstHead(false);
3261 // if we have a first head, then we are going to ignore the
3262 // headers for the additional pages, since there aren't any
3263 // in XHTML. this test accomplishes that.
3264 bool const havehead = !havefirsthead && haveLTHead(false);
3265 if (havehead || havefirsthead) {
3266 xs << html::StartTag("thead") << html::CR();
3267 for (row_type r = 0; r < nrows(); ++r) {
3268 if (((havefirsthead && row_info[r].endfirsthead) ||
3269 (havehead && row_info[r].endhead)) &&
3270 !row_info[r].caption) {
3271 ret += xhtmlRow(xs, r, runparams, true);
3274 xs << html::EndTag("thead") << html::CR();
3276 // output footer info
3277 bool const havelastfoot = haveLTLastFoot(false);
3279 bool const havefoot = !havelastfoot && haveLTFoot(false);
3280 if (havefoot || havelastfoot) {
3281 xs << html::StartTag("tfoot") << html::CR();
3282 for (row_type r = 0; r < nrows(); ++r) {
3283 if (((havelastfoot && row_info[r].endlastfoot) ||
3284 (havefoot && row_info[r].endfoot)) &&
3285 !row_info[r].caption) {
3286 ret += xhtmlRow(xs, r, runparams);
3289 xs << html::EndTag("tfoot") << html::CR();
3292 xs << html::StartTag("tbody") << html::CR();
3293 for (row_type r = 0; r < nrows(); ++r) {
3294 if (isValidRow(r)) {
3295 ret += xhtmlRow(xs, r, runparams);
3298 xs << html::EndTag("tbody")
3300 << html::EndTag("table")
3302 if (is_long_tabular)
3303 xs << html::EndTag("div") << html::CR();
3308 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3309 vector<unsigned int> const & clen) const
3311 idx_type const fcell = getFirstCellInRow(row);
3312 idx_type const n = numberOfCellsInRow(row) + fcell;
3315 for (idx_type i = fcell; i < n; ++i) {
3325 for (idx_type i = fcell; i < n; ++i) {
3336 col_type column = cellColumn(i);
3337 int len = clen[column];
3338 while (column < ncols() - 1
3339 && isPartOfMultiColumn(row, ++column))
3340 len += clen[column] + 4;
3341 os << docstring(len, ch);
3356 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3357 vector<unsigned int> const & clen) const
3359 idx_type const fcell = getFirstCellInRow(row);
3360 idx_type const n = numberOfCellsInRow(row) + fcell;
3363 for (idx_type i = fcell; i < n; ++i) {
3364 if (bottomLine(i)) {
3373 for (idx_type i = fcell; i < n; ++i) {
3374 if (bottomLine(i)) {
3384 col_type column = cellColumn(i);
3385 int len = clen[column];
3386 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3387 len += clen[column] + 4;
3388 os << docstring(len, ch);
3389 if (bottomLine(i)) {
3403 void Tabular::plaintextPrintCell(odocstringstream & os,
3404 OutputParams const & runparams,
3405 idx_type cell, row_type row, col_type column,
3406 vector<unsigned int> const & clen,
3407 bool onlydata, size_t max_length) const
3409 odocstringstream sstr;
3410 cellInset(cell)->plaintext(sstr, runparams, max_length);
3422 unsigned int len1 = sstr.str().length();
3423 unsigned int len2 = clen[column];
3424 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3425 len2 += clen[column] + 4;
3428 switch (getAlignment(cell)) {
3430 case LYX_ALIGN_LEFT:
3433 case LYX_ALIGN_RIGHT:
3437 case LYX_ALIGN_CENTER:
3443 os << docstring(len1, ' ') << sstr.str()
3444 << docstring(len2, ' ');
3446 if (rightLine(cell))
3453 void Tabular::plaintext(odocstringstream & os,
3454 OutputParams const & runparams, int const depth,
3455 bool onlydata, char_type delim, size_t max_length) const
3457 // first calculate the width of the single columns
3458 vector<unsigned int> clen(ncols());
3461 // first all non multicolumn cells!
3462 for (col_type c = 0; c < ncols(); ++c) {
3464 for (row_type r = 0; r < nrows(); ++r) {
3465 idx_type cell = cellIndex(r, c);
3466 if (isMultiColumn(cell))
3468 odocstringstream sstr;
3469 cellInset(cell)->plaintext(sstr, runparams, max_length);
3470 if (clen[c] < sstr.str().length())
3471 clen[c] = sstr.str().length();
3474 // then all multicolumn cells!
3475 for (col_type c = 0; c < ncols(); ++c) {
3476 for (row_type r = 0; r < nrows(); ++r) {
3477 idx_type cell = cellIndex(r, c);
3478 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3480 odocstringstream sstr;
3481 cellInset(cell)->plaintext(sstr, runparams, max_length);
3482 int len = int(sstr.str().length());
3483 idx_type const n = columnSpan(cell);
3484 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3486 if (len > int(clen[c + n - 1]))
3487 clen[c + n - 1] = len;
3492 for (row_type r = 0; r < nrows(); ++r) {
3493 if (!onlydata && plaintextTopHLine(os, r, clen))
3494 os << docstring(depth * 2, ' ');
3495 for (col_type c = 0; c < ncols(); ++c) {
3496 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3498 if (onlydata && c > 0)
3499 // we don't use operator<< for single UCS4 character.
3500 // see explanation in docstream.h
3502 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3504 if (os.str().size() > max_length)
3509 os << docstring(depth * 2, ' ');
3510 if (plaintextBottomHLine(os, r, clen))
3511 os << docstring(depth * 2, ' ');
3513 if (os.str().size() > max_length)
3519 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3521 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3525 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3527 return cell_info[row][column].inset;
3531 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3533 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3537 void Tabular::setCellInset(row_type row, col_type column,
3538 shared_ptr<InsetTableCell> ins)
3540 CellData & cd = cell_info[row][column];
3545 void Tabular::validate(LaTeXFeatures & features) const
3547 features.require("NeedTabularnewline");
3549 features.require("booktabs");
3550 if (is_long_tabular && !hasVarwidthColumn()) {
3551 if (tabular_width.zero())
3552 features.require("longtable");
3554 features.require("xltabular");
3556 if (rotate && is_long_tabular)
3557 features.require("lscape");
3559 features.require("rotating");
3560 if (hasVarwidthColumn()) {
3561 if (is_long_tabular)
3562 features.require("xltabular");
3564 features.require("tabularx");
3566 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3567 if (isMultiRow(cell))
3568 features.require("multirow");
3569 if (getVAlignment(cell) != LYX_VALIGN_TOP
3570 || !getPWidth(cell).zero())
3571 features.require("array");
3572 // Tell footnote that we need a savenote
3573 // environment in non-long tables or
3574 // longtable headers/footers
3575 else if (!is_long_tabular && !features.inFloat())
3576 features.saveNoteEnv("tabular");
3577 else if (!isValidRow(cellRow(cell)))
3578 features.saveNoteEnv("longtable");
3580 cellInset(cell)->validate(features);
3581 features.saveNoteEnv(string());
3586 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3588 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3589 ParagraphList::const_iterator cit = parlist.begin();
3590 ParagraphList::const_iterator end = parlist.end();
3592 for (; cit != end; ++cit)
3593 for (int i = 0; i < cit->size(); ++i)
3594 if (cit->isNewline(i))
3601 /////////////////////////////////////////////////////////////////////
3605 /////////////////////////////////////////////////////////////////////
3607 InsetTableCell::InsetTableCell(Buffer * buf)
3608 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3609 contentAlign(LYX_ALIGN_CENTER)
3613 bool InsetTableCell::forcePlainLayout(idx_type) const
3615 return !isFixedWidth;
3619 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3621 return isFixedWidth;
3625 bool InsetTableCell::forceLocalFontSwitch() const
3627 return isFixedWidth;
3631 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3632 FuncStatus & status) const
3634 bool enabled = true;
3635 switch (cmd.action()) {
3636 case LFUN_MATH_DISPLAY:
3637 if (!hasFixedWidth()) {
3642 return InsetText::getStatus(cur, cmd, status);
3644 status.setEnabled(enabled);
3648 docstring InsetTableCell::asString(bool intoInsets)
3651 if (paragraphs().empty())
3653 ParagraphList::const_iterator it = paragraphs().begin();
3654 ParagraphList::const_iterator en = paragraphs().end();
3656 for (; it != en; ++it) {
3661 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3667 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3668 UpdateType utype, TocBackend & backend) const
3670 InsetText::iterateForToc(di, output_active, utype, backend);
3674 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3677 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3678 return InsetText::xhtml(xs, rp);
3683 /////////////////////////////////////////////////////////////////////
3687 /////////////////////////////////////////////////////////////////////
3689 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3691 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3692 rowselect_(false), colselect_(false)
3697 InsetTabular::InsetTabular(InsetTabular const & tab)
3698 : Inset(tab), tabular(tab.tabular),
3699 rowselect_(false), colselect_(false)
3704 InsetTabular::~InsetTabular()
3706 hideDialogs("tabular", this);
3710 void InsetTabular::setBuffer(Buffer & buf)
3712 tabular.setBuffer(buf);
3713 Inset::setBuffer(buf);
3717 bool InsetTabular::insetAllowed(InsetCode code) const
3722 case MATHMACRO_CODE:
3727 return tabular.is_long_tabular;
3735 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3737 return tabular.is_long_tabular &&
3738 (newtype == "Standard" || newtype == "Unnumbered");
3742 void InsetTabular::write(ostream & os) const
3744 os << "Tabular" << endl;
3749 string InsetTabular::contextMenu(BufferView const &, int, int) const
3751 // FIXME: depending on the selection state,
3752 // we could offer a different menu.
3753 return cell(0)->contextMenuName() + ";" + contextMenuName();
3757 string InsetTabular::contextMenuName() const
3759 return "context-tabular";
3763 void InsetTabular::read(Lexer & lex)
3765 //bool const old_format = (lex.getString() == "\\LyXTable");
3773 string token = lex.getString();
3774 while (lex && token != "\\end_inset") {
3776 token = lex.getString();
3779 lex.printError("Missing \\end_inset at this point. ");
3783 int InsetTabular::rowFromY(Cursor & cur, int y) const
3785 // top y coordinate of tabular
3786 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3788 for (; r < tabular.nrows() && y > h; ++r)
3789 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3790 + tabular.interRowSpace(r);
3796 int InsetTabular::columnFromX(Cursor & cur, int x) const
3798 // left x coordinate of tabular
3799 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3801 for (; c < tabular.ncols() && x > w; ++c)
3802 w += tabular.cellWidth(c);
3807 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3809 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3810 // mi.base.textwidth << "\n";
3811 LBUFERR(mi.base.bv);
3813 for (row_type r = 0; r < tabular.nrows(); ++r) {
3816 for (col_type c = 0; c < tabular.ncols(); ++c) {
3817 if (tabular.isPartOfMultiColumn(r, c)
3818 || tabular.isPartOfMultiRow(r, c))
3819 // multicolumn or multirow cell, but not first one
3821 idx_type const cell = tabular.cellIndex(r, c);
3824 Length const p_width = tabular.getPWidth(cell);
3825 if (!p_width.zero())
3826 m.base.textwidth = mi.base.inPixels(p_width);
3827 tabular.cellInset(cell)->metrics(m, dim0);
3828 if (!p_width.zero())
3829 dim0.wid = m.base.textwidth;
3830 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
3831 + tabular.interColumnSpace(cell);
3833 // FIXME(?): do we need a second metrics call?
3834 TextMetrics const & tm =
3835 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3837 // determine horizontal offset because of decimal align (if necessary)
3838 int decimal_width = 0;
3839 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3840 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
3841 tail.setBuffer(tabular.buffer());
3842 // we need to set macrocontext position everywhere
3843 // otherwise we crash with nested insets (e.g. footnotes)
3844 // after decimal point
3845 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3847 dit.push_back(CursorSlice(tail));
3848 tail.setMacrocontextPositionRecursive(dit);
3850 // remove text leading decimal point
3851 docstring const align_d = tabular.column_info[c].decimal_point;
3852 dit = separatorPos(&tail, align_d);
3854 pit_type const psize = tail.paragraphs().front().size();
3856 tail.paragraphs().front().eraseChars(0,
3857 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3859 tail.metrics(m, dim1);
3860 decimal_width = dim1.width();
3863 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3864 tabular.cell_info[r][c].decimal_width = decimal_width;
3866 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3867 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3868 int const lastpardes = tm.last().second->descent()
3869 + TEXT_TO_INSET_OFFSET;
3871 switch (tabular.getVAlignment(cell)) {
3872 case Tabular::LYX_VALIGN_TOP:
3874 case Tabular::LYX_VALIGN_MIDDLE:
3875 offset = -(dim0.des - lastpardes)/2;
3877 case Tabular::LYX_VALIGN_BOTTOM:
3878 offset = -(dim0.des - lastpardes);
3881 tabular.cell_info[r][c].voffset = offset;
3882 maxasc = max(maxasc, dim0.asc - offset);
3883 maxdes = max(maxdes, dim0.des + offset);
3885 int const top_space = tabular.row_info[r].top_space_default ?
3886 default_line_space :
3887 mi.base.inPixels(tabular.row_info[r].top_space);
3888 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3889 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3890 default_line_space :
3891 mi.base.inPixels(tabular.row_info[r].bottom_space);
3892 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3895 tabular.updateColumnWidths();
3896 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
3897 dim.des = tabular.height() - dim.asc;
3898 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3902 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3905 if (&cur.inset() == this && cur.selection()) {
3906 if (cur.selIsMultiCell()) {
3909 getSelection(cur, rs, re, cs, ce);
3911 idx_type const cell = tabular.cellIndex(row, col);
3912 col_type const cspan = tabular.columnSpan(cell);
3913 row_type const rspan = tabular.rowSpan(cell);
3914 if (col + cspan - 1 >= cs && col <= ce
3915 && row + rspan - 1 >= rs && row <= re)
3918 if (col == tabular.cellColumn(cur.idx())
3919 && row == tabular.cellRow(cur.idx())) {
3920 CursorSlice const & beg = cur.selBegin();
3921 CursorSlice const & end = cur.selEnd();
3923 if ((end.lastpos() > 0 || end.lastpit() > 0)
3924 && end.pos() == end.lastpos() && beg.pos() == 0
3925 && end.pit() == end.lastpit() && beg.pit() == 0)
3933 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3935 x += ADD_TO_TABULAR_WIDTH;
3937 BufferView * bv = pi.base.bv;
3938 Cursor & cur = pi.base.bv->cursor();
3940 // FIXME: As the full background is painted in drawBackground(),
3941 // we have no choice but to do a full repaint for the Text cells.
3942 pi.full_repaint = true;
3944 bool const original_selection_state = pi.selected;
3948 int yy = y + tabular.offsetVAlignment();
3949 for (row_type r = 0; r < tabular.nrows(); ++r) {
3951 for (col_type c = 0; c < tabular.ncols(); ++c) {
3952 if (tabular.isPartOfMultiColumn(r, c))
3955 idx = tabular.cellIndex(r, c);
3957 if (tabular.isPartOfMultiRow(r, c)) {
3958 nx += tabular.cellWidth(idx);
3962 pi.selected |= isCellSelected(cur, r, c);
3963 int const cx = nx + tabular.textHOffset(idx);
3964 int const cy = yy + tabular.textVOffset(idx);
3965 // Cache the Inset position.
3966 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3967 cell(idx)->draw(pi, cx, cy);
3968 drawCellLines(pi, nx, yy, r, idx);
3969 nx += tabular.cellWidth(idx);
3970 pi.selected = original_selection_state;
3973 if (r + 1 < tabular.nrows())
3974 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3975 + tabular.interRowSpace(r + 1);
3980 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3982 x += ADD_TO_TABULAR_WIDTH;
3983 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
3984 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3985 pi.backgroundColor(this));
3989 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3991 Cursor & cur = pi.base.bv->cursor();
3993 x += ADD_TO_TABULAR_WIDTH;
3995 if (!cur.selection())
3997 if (&cur.inset() != this)
4002 bool const full_cell_selected = isCellSelected(cur,
4003 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4005 if (cur.selIsMultiCell() || full_cell_selected) {
4006 for (row_type r = 0; r < tabular.nrows(); ++r) {
4008 for (col_type c = 0; c < tabular.ncols(); ++c) {
4009 if (tabular.isPartOfMultiColumn(r, c))
4012 idx_type const cell = tabular.cellIndex(r, c);
4014 if (tabular.isPartOfMultiRow(r, c)) {
4015 xx += tabular.cellWidth(cell);
4018 int const w = tabular.cellWidth(cell);
4019 int const h = tabular.cellHeight(cell);
4020 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4021 if (isCellSelected(cur, r, c))
4022 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4025 if (r + 1 < tabular.nrows())
4026 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4027 + tabular.interRowSpace(r + 1);
4031 // FIXME: This code has no effect because InsetTableCell does not handle
4032 // drawSelection other than the trivial implementation in Inset.
4034 // x += cellXPos(cur.idx());
4035 // x += tabular.textHOffset(cur.idx());
4036 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4041 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4042 row_type row, idx_type cell) const
4044 y -= tabular.rowAscent(row);
4045 int const w = tabular.cellWidth(cell);
4046 int const h = tabular.cellHeight(cell);
4047 Color const linecolor = pi.textColor(Color_tabularline);
4048 Color const gridcolor = pi.textColor(Color_tabularonoffline);
4051 bool drawline = tabular.topLine(cell)
4052 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4053 pi.pain.line(x, y, x + w, y,
4054 drawline ? linecolor : gridcolor,
4055 drawline ? Painter::line_solid : Painter::line_onoffdash);
4058 drawline = tabular.bottomLine(cell);
4059 pi.pain.line(x, y + h, x + w, y + h,
4060 drawline ? linecolor : gridcolor,
4061 drawline ? Painter::line_solid : Painter::line_onoffdash);
4064 col_type const col = tabular.cellColumn(cell);
4065 drawline = tabular.leftLine(cell)
4066 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4067 pi.pain.line(x, y, x, y + h,
4068 drawline ? linecolor : gridcolor,
4069 drawline ? Painter::line_solid : Painter::line_onoffdash);
4072 x -= tabular.interColumnSpace(cell);
4073 col_type next_cell_col = col + 1;
4074 while (next_cell_col < tabular.ncols()
4075 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4077 drawline = tabular.rightLine(cell)
4078 || (next_cell_col < tabular.ncols()
4079 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4080 pi.pain.line(x + w, y, x + w, y + h,
4081 drawline ? linecolor : gridcolor,
4082 drawline ? Painter::line_solid : Painter::line_onoffdash);
4086 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4088 //lyxerr << "InsetTabular::edit: " << this << endl;
4092 if (isRightToLeft(cur))
4093 cur.idx() = tabular.getLastCellInRow(0);
4099 if (isRightToLeft(cur))
4100 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4102 cur.idx() = tabular.numberofcells - 1;
4104 cur.pos() = cur.lastpos(); // FIXME crude guess
4106 cur.setCurrentFont();
4107 // FIXME: this accesses the position cache before it is initialized
4108 //cur.bv().fitCursor();
4112 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
4114 // In a longtable, tell captions what the current float is
4115 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4116 string const saveflt = cnts.current_float();
4117 if (tabular.is_long_tabular) {
4118 cnts.current_float("table");
4119 // in longtables, we only step the counter once
4120 cnts.step(from_ascii("table"), utype);
4121 cnts.isLongtable(true);
4124 ParIterator it2 = it;
4126 size_t const end = it2.nargs();
4127 for ( ; it2.idx() < end; it2.top().forwardIdx())
4128 buffer().updateBuffer(it2, utype);
4131 if (tabular.is_long_tabular) {
4132 cnts.current_float(saveflt);
4133 cnts.isLongtable(false);
4138 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4139 UpdateType utype, TocBackend & backend) const
4141 DocIterator dit = cpit;
4143 size_t const end = dit.nargs();
4144 for ( ; dit.idx() < end; dit.top().forwardIdx())
4145 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4149 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4151 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4152 return x < x0 || x > x0 + tabular.width();
4156 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4158 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4159 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4160 // there is no margin added vertically to tabular insets.
4161 // However, it works for now.
4162 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4166 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4168 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4172 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4174 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4175 << "\n cur:" << cur);
4176 CursorSlice sl = cur.top();
4177 Cursor & bvcur = cur.bv().cursor();
4179 FuncCode const act = cmd.action();
4183 case LFUN_MOUSE_PRESS: {
4184 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4186 if (hitSelectRow(cur.bv(), cmd.x())) {
4187 row_type r = rowFromY(cur, cmd.y());
4188 cur.idx() = tabular.getFirstCellInRow(r);
4192 cur.idx() = tabular.getLastCellInRow(r);
4193 cur.pit() = cur.lastpit();
4194 cur.pos() = cur.lastpos();
4195 cur.selection(true);
4201 if (hitSelectColumn(cur.bv(), cmd.y())) {
4202 col_type c = columnFromX(cur, cmd.x());
4203 cur.idx() = tabular.cellIndex(0, c);
4207 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4208 cur.pit() = cur.lastpit();
4209 cur.pos() = cur.lastpos();
4210 cur.selection(true);
4215 // do not reset cursor/selection if we have selected
4216 // some cells (bug 2715).
4217 if (cmd.button() == mouse_button::button3
4218 && &bvcur.selBegin().inset() == this
4219 && bvcur.selIsMultiCell())
4222 // Let InsetTableCell do it
4223 cell(cur.idx())->dispatch(cur, cmd);
4226 case LFUN_MOUSE_MOTION:
4227 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4228 if (cmd.button() == mouse_button::button1) {
4229 // only accept motions to places not deeper nested than the real anchor
4230 if (!bvcur.realAnchor().hasPart(cur)) {
4234 // select (additional) row
4236 row_type r = rowFromY(cur, cmd.y());
4237 cur.idx() = tabular.getLastCellInRow(r);
4238 // we need to reset the cursor's pit and pos now, as the old ones
4239 // may no longer be valid.
4242 bvcur.setCursor(cur);
4243 bvcur.selection(true);
4246 // select (additional) column
4248 col_type c = columnFromX(cur, cmd.x());
4249 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4250 // we need to reset the cursor's pit and pos now, as the old ones
4251 // may no longer be valid.
4254 bvcur.setCursor(cur);
4255 bvcur.selection(true);
4258 // only update if selection changes
4259 if (bvcur.idx() == cur.idx() &&
4260 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4261 cur.noScreenUpdate();
4262 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4263 bvcur.setCursor(cur);
4264 bvcur.selection(true);
4265 // if this is a multicell selection, we just set the cursor to
4266 // the beginning of the cell's text.
4267 if (bvcur.selIsMultiCell()) {
4268 bvcur.pit() = bvcur.lastpit();
4269 bvcur.pos() = bvcur.lastpos();
4274 case LFUN_MOUSE_RELEASE:
4279 case LFUN_CELL_BACKWARD:
4281 cur.selection(false);
4284 case LFUN_CELL_FORWARD:
4286 cur.selection(false);
4289 case LFUN_CHAR_FORWARD_SELECT:
4290 case LFUN_CHAR_FORWARD:
4291 case LFUN_CHAR_BACKWARD_SELECT:
4292 case LFUN_CHAR_BACKWARD:
4293 case LFUN_CHAR_RIGHT_SELECT:
4294 case LFUN_CHAR_RIGHT:
4295 case LFUN_CHAR_LEFT_SELECT:
4296 case LFUN_CHAR_LEFT:
4297 case LFUN_WORD_FORWARD:
4298 case LFUN_WORD_FORWARD_SELECT:
4299 case LFUN_WORD_BACKWARD:
4300 case LFUN_WORD_BACKWARD_SELECT:
4301 case LFUN_WORD_RIGHT:
4302 case LFUN_WORD_RIGHT_SELECT:
4303 case LFUN_WORD_LEFT:
4304 case LFUN_WORD_LEFT_SELECT: {
4305 // determine whether we move to next or previous cell, where to enter
4306 // the new cell from, and which command to "finish" (i.e., exit the
4309 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4310 FuncCode finish_lfun;
4312 if (act == LFUN_CHAR_FORWARD
4313 || act == LFUN_CHAR_FORWARD_SELECT
4314 || act == LFUN_WORD_FORWARD
4315 || act == LFUN_WORD_FORWARD_SELECT) {
4317 finish_lfun = LFUN_FINISHED_FORWARD;
4319 else if (act == LFUN_CHAR_BACKWARD
4320 || act == LFUN_CHAR_BACKWARD_SELECT
4321 || act == LFUN_WORD_BACKWARD
4322 || act == LFUN_WORD_BACKWARD_SELECT) {
4324 finish_lfun = LFUN_FINISHED_BACKWARD;
4326 // LEFT or RIGHT commands --- the interpretation will depend on the
4327 // table's direction.
4329 bool const right = act == LFUN_CHAR_RIGHT
4330 || act == LFUN_CHAR_RIGHT_SELECT
4331 || act == LFUN_WORD_RIGHT
4332 || act == LFUN_WORD_RIGHT_SELECT;
4333 next_cell = isRightToLeft(cur) != right;
4335 if (lyxrc.visual_cursor)
4336 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4338 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4341 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4342 || act == LFUN_CHAR_BACKWARD_SELECT
4343 || act == LFUN_CHAR_RIGHT_SELECT
4344 || act == LFUN_CHAR_LEFT_SELECT
4345 || act == LFUN_WORD_FORWARD_SELECT
4346 || act == LFUN_WORD_RIGHT_SELECT
4347 || act == LFUN_WORD_BACKWARD_SELECT
4348 || act == LFUN_WORD_LEFT_SELECT;
4350 // If we have a multicell selection or we're
4351 // not doing some LFUN_*_SELECT thing anyway...
4352 if (!cur.selIsMultiCell() || !select) {
4353 col_type const c = tabular.cellColumn(cur.idx());
4354 row_type const r = tabular.cellRow(cur.idx());
4355 // Are we trying to select the whole cell and is the whole cell
4356 // not yet selected?
4357 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4358 ((next_cell && cur.pit() == cur.lastpit()
4359 && cur.pos() == cur.lastpos())
4360 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4362 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4364 // ...try to dispatch to the cell's inset.
4365 cell(cur.idx())->dispatch(cur, cmd);
4367 // When we already have a selection we want to select the whole cell
4368 // before going to the next cell.
4369 if (select_whole && !empty_cell){
4370 getText(cur.idx())->selectAll(cur);
4372 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4376 // FIXME: When we support the selection of an empty cell, remove
4377 // the !empty_cell from this condition. For now we jump to the next
4378 // cell if the current cell is empty.
4379 if (cur.result().dispatched() && !empty_cell)
4383 // move to next/prev cell, as appropriate
4384 // note that we will always do this if we're selecting and we have
4385 // a multicell selection
4386 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4387 << " cell from: " << int(entry_from));
4389 moveNextCell(cur, entry_from);
4391 movePrevCell(cur, entry_from);
4392 // if we're exiting the table, call the appropriate FINISHED lfun
4393 if (sl == cur.top()) {
4394 cmd = FuncRequest(finish_lfun);
4399 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4404 case LFUN_DOWN_SELECT:
4406 if (!(cur.selection() && cur.selIsMultiCell()))
4407 cell(cur.idx())->dispatch(cur, cmd);
4409 cur.dispatched(); // override the cell's decision
4410 if (sl == cur.top()) {
4411 // if our Text didn't do anything to the cursor
4412 // then we try to put the cursor into the cell below
4413 // setting also the right targetX.
4414 cur.selHandle(act == LFUN_DOWN_SELECT);
4415 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4416 int const xtarget = cur.targetX();
4417 // WARNING: Once cur.idx() has been reset, the cursor is in
4418 // an inconsistent state until pos() has been set. Be careful
4419 // what you do with it!
4420 cur.idx() = tabular.cellBelow(cur.idx());
4422 TextMetrics const & tm =
4423 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4424 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4425 cur.setCurrentFont();
4428 if (sl == cur.top()) {
4429 // we trick it to go to forward after leaving the
4431 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4434 if (cur.selIsMultiCell()) {
4435 cur.pit() = cur.lastpit();
4436 cur.pos() = cur.lastpos();
4437 cur.setCurrentFont();
4438 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4441 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4444 case LFUN_UP_SELECT:
4446 if (!(cur.selection() && cur.selIsMultiCell()))
4447 cell(cur.idx())->dispatch(cur, cmd);
4448 cur.dispatched(); // override the cell's decision
4449 if (sl == cur.top()) {
4450 // if our Text didn't do anything to the cursor
4451 // then we try to put the cursor into the cell above
4452 // setting also the right targetX.
4453 cur.selHandle(act == LFUN_UP_SELECT);
4454 if (tabular.cellRow(cur.idx()) != 0) {
4455 int const xtarget = cur.targetX();
4456 // WARNING: Once cur.idx() has been reset, the cursor is in
4457 // an inconsistent state until pos() has been set. Be careful
4458 // what you do with it!
4459 cur.idx() = tabular.cellAbove(cur.idx());
4460 cur.pit() = cur.lastpit();
4461 Text const * text = cell(cur.idx())->getText(0);
4462 TextMetrics const & tm = cur.bv().textMetrics(text);
4463 ParagraphMetrics const & pm =
4464 tm.parMetrics(cur.lastpit());
4465 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4466 cur.setCurrentFont();
4469 if (sl == cur.top()) {
4470 cmd = FuncRequest(LFUN_UP);
4473 if (cur.selIsMultiCell()) {
4475 cur.pos() = cur.lastpos();
4476 cur.setCurrentFont();
4477 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4480 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4483 case LFUN_LAYOUT_TABULAR:
4484 cur.bv().showDialog("tabular");
4487 case LFUN_INSET_MODIFY:
4488 // we come from the dialog
4489 if (cmd.getArg(0) == "tabular")
4490 tabularFeatures(cur, cmd.getLongArg(1));
4495 case LFUN_TABULAR_FEATURE:
4496 tabularFeatures(cur, to_utf8(cmd.argument()));
4499 // insert file functions
4500 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4501 case LFUN_FILE_INSERT_PLAINTEXT:
4503 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4504 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4505 FileName(to_utf8(cmd.argument())));
4508 cur.recordUndoInset();
4509 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4510 // content has been replaced,
4511 // so cursor might be invalid
4512 cur.pos() = cur.lastpos();
4513 cur.pit() = cur.lastpit();
4514 bvcur.setCursor(cur);
4521 if (cur.selIsMultiCell()) {
4522 if (copySelection(cur)) {
4523 cur.recordUndoInset();
4527 cell(cur.idx())->dispatch(cur, cmd);
4530 case LFUN_SELF_INSERT:
4531 if (cur.selIsMultiCell()) {
4532 cur.recordUndoInset();
4534 BufferView * bv = &cur.bv();
4535 docstring::const_iterator cit = cmd.argument().begin();
4536 docstring::const_iterator const end = cmd.argument().end();
4537 for (; cit != end; ++cit)
4538 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4541 bv->bookmarkEditPosition();
4543 cell(cur.idx())->dispatch(cur, cmd);
4546 case LFUN_CHAR_DELETE_BACKWARD:
4547 case LFUN_CHAR_DELETE_FORWARD:
4548 if (cur.selIsMultiCell()) {
4549 cur.recordUndoInset();
4552 cell(cur.idx())->dispatch(cur, cmd);
4556 if (!cur.selection())
4558 if (cur.selIsMultiCell()) {
4562 cell(cur.idx())->dispatch(cur, cmd);
4565 case LFUN_CLIPBOARD_PASTE:
4566 case LFUN_PRIMARY_SELECTION_PASTE: {
4567 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4568 theClipboard().getAsText(Clipboard::PlainTextType) :
4569 theSelection().get();
4572 // pass to InsertPlaintextString, but
4573 // only if we have multi-cell content
4574 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4575 cur.recordUndoInset();
4576 if (insertPlaintextString(cur.bv(), clip, false)) {
4577 // content has been replaced,
4578 // so cursor might be invalid
4579 cur.pos() = cur.lastpos();
4580 cur.pit() = cur.lastpit();
4581 bvcur.setCursor(cur);
4585 // Let the cell handle normal text
4586 cell(cur.idx())->dispatch(cur, cmd);
4591 if (!tabularStackDirty()) {
4592 if (!cur.selIsMultiCell())
4593 cell(cur.idx())->dispatch(cur, cmd);
4596 if (theClipboard().isInternal()) {
4597 cur.recordUndoInset();
4598 pasteClipboard(cur);
4602 case LFUN_FONT_EMPH:
4603 case LFUN_FONT_BOLD:
4604 case LFUN_FONT_BOLDSYMBOL:
4605 case LFUN_FONT_ROMAN:
4606 case LFUN_FONT_NOUN:
4607 case LFUN_FONT_ITAL:
4608 case LFUN_FONT_FRAK:
4609 case LFUN_FONT_TYPEWRITER:
4610 case LFUN_FONT_SANS:
4611 case LFUN_TEXTSTYLE_APPLY:
4612 case LFUN_TEXTSTYLE_UPDATE:
4613 case LFUN_FONT_SIZE:
4614 case LFUN_FONT_UNDERLINE:
4615 case LFUN_FONT_STRIKEOUT:
4616 case LFUN_FONT_CROSSOUT:
4617 case LFUN_FONT_UNDERUNDERLINE:
4618 case LFUN_FONT_UNDERWAVE:
4620 case LFUN_PARAGRAPH_PARAMS_APPLY:
4621 case LFUN_PARAGRAPH_PARAMS:
4622 case LFUN_WORD_CAPITALIZE:
4623 case LFUN_WORD_UPCASE:
4624 case LFUN_WORD_LOWCASE:
4625 case LFUN_CHARS_TRANSPOSE:
4626 if (cur.selIsMultiCell()) {
4629 getSelection(cur, rs, re, cs, ce);
4630 Cursor tmpcur = cur;
4631 for (row_type r = rs; r <= re; ++r) {
4632 for (col_type c = cs; c <= ce; ++c) {
4633 // cursor follows cell:
4634 tmpcur.idx() = tabular.cellIndex(r, c);
4635 // select this cell only:
4638 tmpcur.resetAnchor();
4639 tmpcur.pit() = tmpcur.lastpit();
4640 tmpcur.pos() = tmpcur.top().lastpos();
4641 tmpcur.setCursor(tmpcur);
4642 tmpcur.setSelection();
4643 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4648 cell(cur.idx())->dispatch(cur, cmd);
4652 case LFUN_INSET_SETTINGS:
4653 // relay this lfun to Inset, not to the cell.
4654 Inset::doDispatch(cur, cmd);
4658 // we try to handle this event in the insets dispatch function.
4659 cell(cur.idx())->dispatch(cur, cmd);
4665 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4666 string const & argument, FuncStatus & status) const
4669 int action = Tabular::LAST_ACTION;
4671 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4672 if (tabularFeature[i].feature == s) {
4673 action = tabularFeature[i].action;
4677 if (action == Tabular::LAST_ACTION) {
4679 status.setUnknown(true);
4683 row_type sel_row_start = 0;
4684 row_type sel_row_end = 0;
4685 col_type sel_col_start = 0;
4686 col_type sel_col_end = 0;
4687 Tabular::ltType dummyltt;
4690 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4693 case Tabular::SET_PWIDTH:
4694 case Tabular::SET_MPWIDTH:
4695 case Tabular::TOGGLE_VARWIDTH_COLUMN:
4696 case Tabular::SET_SPECIAL_COLUMN:
4697 case Tabular::SET_SPECIAL_MULTICOLUMN:
4698 case Tabular::APPEND_ROW:
4699 case Tabular::APPEND_COLUMN:
4700 case Tabular::DELETE_ROW:
4701 case Tabular::DELETE_COLUMN:
4702 case Tabular::COPY_ROW:
4703 case Tabular::COPY_COLUMN:
4704 case Tabular::SET_TOP_SPACE:
4705 case Tabular::SET_BOTTOM_SPACE:
4706 case Tabular::SET_INTERLINE_SPACE:
4710 case Tabular::SET_TABULAR_WIDTH:
4711 status.setEnabled(!tabular.rotate
4712 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4715 case Tabular::MOVE_COLUMN_RIGHT:
4716 case Tabular::MOVE_COLUMN_LEFT:
4717 case Tabular::MOVE_ROW_DOWN:
4718 case Tabular::MOVE_ROW_UP: {
4719 if (cur.selection()) {
4720 status.message(_("Selections not supported."));
4721 status.setEnabled(false);
4725 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4726 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4727 (action == Tabular::MOVE_COLUMN_LEFT &&
4728 tabular.cellColumn(cur.idx()) == 0) ||
4729 (action == Tabular::MOVE_ROW_DOWN &&
4730 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4731 (action == Tabular::MOVE_ROW_UP &&
4732 tabular.cellRow(cur.idx()) == 0)) {
4733 status.setEnabled(false);
4737 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4738 action == Tabular::MOVE_COLUMN_LEFT) {
4739 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4740 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4741 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4742 status.message(_("Multi-column in current or"
4743 " destination column."));
4744 status.setEnabled(false);
4749 if (action == Tabular::MOVE_ROW_DOWN ||
4750 action == Tabular::MOVE_ROW_UP) {
4751 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4752 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4753 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4754 status.message(_("Multi-row in current or"
4755 " destination row."));
4756 status.setEnabled(false);
4761 status.setEnabled(true);
4765 case Tabular::SET_DECIMAL_POINT:
4767 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4770 case Tabular::SET_MULTICOLUMN:
4771 case Tabular::UNSET_MULTICOLUMN:
4772 case Tabular::MULTICOLUMN:
4773 // If a row is set as longtable caption, it must not be allowed
4774 // to unset that this row is a multicolumn.
4775 status.setEnabled(sel_row_start == sel_row_end
4776 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4777 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4780 case Tabular::SET_MULTIROW:
4781 case Tabular::UNSET_MULTIROW:
4782 case Tabular::MULTIROW:
4783 // If a row is set as longtable caption, it must not be allowed
4784 // to unset that this row is a multirow.
4785 status.setEnabled(sel_col_start == sel_col_end
4786 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4787 status.setOnOff(tabular.isMultiRow(cur.idx()));
4790 case Tabular::SET_ALL_LINES:
4791 case Tabular::UNSET_ALL_LINES:
4792 case Tabular::SET_BORDER_LINES:
4793 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4796 case Tabular::SET_LINE_TOP:
4797 case Tabular::SET_LINE_BOTTOM:
4798 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4801 case Tabular::SET_LINE_LEFT:
4802 case Tabular::SET_LINE_RIGHT:
4803 status.setEnabled(!tabular.use_booktabs
4804 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4807 case Tabular::TOGGLE_LINE_TOP:
4808 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4809 status.setOnOff(tabular.topLine(cur.idx()));
4812 case Tabular::TOGGLE_LINE_BOTTOM:
4813 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4814 status.setOnOff(tabular.bottomLine(cur.idx()));
4817 case Tabular::TOGGLE_LINE_LEFT:
4818 status.setEnabled(!tabular.use_booktabs
4819 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4820 status.setOnOff(tabular.leftLine(cur.idx()));
4823 case Tabular::TOGGLE_LINE_RIGHT:
4824 status.setEnabled(!tabular.use_booktabs
4825 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4826 status.setOnOff(tabular.rightLine(cur.idx()));
4829 // multirow cells only inherit the alignment of the column if the column has
4830 // no width, otherwise they are left-aligned
4831 // therefore allow always left but right and center only if there is no width
4832 case Tabular::M_ALIGN_LEFT:
4835 case Tabular::ALIGN_LEFT:
4836 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4839 case Tabular::M_ALIGN_RIGHT:
4842 case Tabular::ALIGN_RIGHT:
4843 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4844 && !tabular.getPWidth(cur.idx()).zero()));
4845 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4848 case Tabular::M_ALIGN_CENTER:
4851 case Tabular::ALIGN_CENTER:
4852 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4853 && !tabular.getPWidth(cur.idx()).zero()));
4854 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4857 case Tabular::ALIGN_BLOCK:
4858 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4859 && !tabular.isMultiRow(cur.idx()));
4860 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4863 case Tabular::ALIGN_DECIMAL:
4864 status.setEnabled(!tabular.isMultiRow(cur.idx())
4865 && !tabular.isMultiColumn(cur.idx()));
4866 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4869 case Tabular::M_VALIGN_TOP:
4872 case Tabular::VALIGN_TOP:
4873 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4874 && !tabular.isMultiRow(cur.idx()));
4876 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4879 case Tabular::M_VALIGN_BOTTOM:
4882 case Tabular::VALIGN_BOTTOM:
4883 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4884 && !tabular.isMultiRow(cur.idx()));
4886 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4889 case Tabular::M_VALIGN_MIDDLE:
4892 case Tabular::VALIGN_MIDDLE:
4893 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4894 && !tabular.isMultiRow(cur.idx()));
4896 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4899 case Tabular::SET_LONGTABULAR:
4900 case Tabular::TOGGLE_LONGTABULAR:
4901 // setting as longtable is not allowed when table is inside a float
4902 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4903 || cur.innerInsetOfType(WRAP_CODE) != 0)
4904 status.setEnabled(false);
4906 status.setEnabled(true);
4907 status.setOnOff(tabular.is_long_tabular);
4910 case Tabular::UNSET_LONGTABULAR:
4911 status.setOnOff(!tabular.is_long_tabular);
4914 case Tabular::TOGGLE_ROTATE_TABULAR:
4915 case Tabular::SET_ROTATE_TABULAR:
4916 status.setOnOff(tabular.rotate != 0);
4919 case Tabular::TABULAR_VALIGN_TOP:
4920 status.setEnabled(tabular.tabular_width.zero());
4921 status.setOnOff(tabular.tabular_valignment
4922 == Tabular::LYX_VALIGN_TOP);
4924 case Tabular::TABULAR_VALIGN_MIDDLE:
4925 status.setEnabled(tabular.tabular_width.zero());
4926 status.setOnOff(tabular.tabular_valignment
4927 == Tabular::LYX_VALIGN_MIDDLE);
4929 case Tabular::TABULAR_VALIGN_BOTTOM:
4930 status.setEnabled(tabular.tabular_width.zero());
4931 status.setOnOff(tabular.tabular_valignment
4932 == Tabular::LYX_VALIGN_BOTTOM);
4935 case Tabular::LONGTABULAR_ALIGN_LEFT:
4936 status.setOnOff(tabular.longtabular_alignment
4937 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4939 case Tabular::LONGTABULAR_ALIGN_CENTER:
4940 status.setOnOff(tabular.longtabular_alignment
4941 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4943 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4944 status.setOnOff(tabular.longtabular_alignment
4945 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4948 case Tabular::UNSET_ROTATE_TABULAR:
4949 status.setOnOff(tabular.rotate == 0);
4952 case Tabular::TOGGLE_ROTATE_CELL:
4953 case Tabular::SET_ROTATE_CELL:
4954 status.setOnOff(!oneCellHasRotationState(false,
4955 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4958 case Tabular::UNSET_ROTATE_CELL:
4959 status.setOnOff(!oneCellHasRotationState(true,
4960 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4963 case Tabular::SET_USEBOX:
4964 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4967 // every row can only be one thing:
4968 // either a footer or header
4969 case Tabular::SET_LTFIRSTHEAD:
4970 status.setEnabled(sel_row_start == sel_row_end);
4971 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4974 case Tabular::UNSET_LTFIRSTHEAD:
4975 status.setEnabled(sel_row_start == sel_row_end);
4976 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4979 case Tabular::SET_LTHEAD:
4980 status.setEnabled(sel_row_start == sel_row_end);
4981 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4984 case Tabular::UNSET_LTHEAD:
4985 status.setEnabled(sel_row_start == sel_row_end);
4986 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4989 case Tabular::SET_LTFOOT:
4990 status.setEnabled(sel_row_start == sel_row_end);
4991 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4994 case Tabular::UNSET_LTFOOT:
4995 status.setEnabled(sel_row_start == sel_row_end);
4996 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4999 case Tabular::SET_LTLASTFOOT:
5000 status.setEnabled(sel_row_start == sel_row_end);
5001 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5004 case Tabular::UNSET_LTLASTFOOT:
5005 status.setEnabled(sel_row_start == sel_row_end);
5006 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5009 case Tabular::SET_LTNEWPAGE:
5010 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5012 case Tabular::UNSET_LTNEWPAGE:
5013 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5016 // only one row in head/firsthead/foot/lasthead can be the caption
5017 // and a multirow cannot be set as caption
5018 case Tabular::SET_LTCAPTION:
5019 status.setEnabled(sel_row_start == sel_row_end
5020 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5021 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5022 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5023 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5024 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5025 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5026 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5027 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5028 && !tabular.isMultiRow(sel_row_start));
5029 status.setOnOff(tabular.ltCaption(sel_row_start));
5032 case Tabular::UNSET_LTCAPTION:
5033 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5036 case Tabular::TOGGLE_LTCAPTION:
5037 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5038 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5039 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5040 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5041 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5042 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5043 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5044 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5045 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5046 status.setOnOff(tabular.ltCaption(sel_row_start));
5049 case Tabular::TOGGLE_BOOKTABS:
5050 case Tabular::SET_BOOKTABS:
5051 status.setOnOff(tabular.use_booktabs);
5054 case Tabular::UNSET_BOOKTABS:
5055 status.setOnOff(!tabular.use_booktabs);
5060 status.setEnabled(false);
5067 // function sets an object as defined in FuncStatus.h:
5068 // states OK, Unknown, Disabled, On, Off.
5069 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5070 FuncStatus & status) const
5072 switch (cmd.action()) {
5073 case LFUN_INSET_MODIFY:
5074 if (cmd.getArg(0) != "tabular")
5076 if (cmd.getArg(1) == "for-dialog") {
5077 // The dialog is asking the status of a command
5078 if (&cur.inset() != this)
5080 string action = cmd.getArg(2);
5081 string arg = cmd.getLongArg(3);
5082 return getFeatureStatus(cur, action, arg, status);
5084 // We always enable the lfun if it is coming from the dialog
5085 // because the dialog makes sure all the settings are valid,
5086 // even though the first argument might not be valid now.
5087 status.setEnabled(true);
5091 case LFUN_TABULAR_FEATURE: {
5092 if (&cur.inset() != this)
5094 string action = cmd.getArg(0);
5095 string arg = cmd.getLongArg(1);
5096 return getFeatureStatus(cur, action, arg, status);
5099 case LFUN_CAPTION_INSERT: {
5100 // caption is only allowed in caption cell of longtable
5101 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5102 status.setEnabled(false);
5105 // only standard caption is allowed
5106 string arg = cmd.getArg(0);
5107 if (!arg.empty() && arg != "Standard") {
5108 status.setEnabled(false);
5111 // check if there is already a caption
5112 bool have_caption = false;
5113 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5114 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5115 ParagraphList::const_iterator pend = itc.paragraphs().end();
5116 for (; pit != pend; ++pit) {
5117 InsetList::const_iterator it = pit->insetList().begin();
5118 InsetList::const_iterator end = pit->insetList().end();
5119 for (; it != end; ++it) {
5120 if (it->inset->lyxCode() == CAPTION_CODE) {
5121 have_caption = true;
5126 status.setEnabled(!have_caption);
5130 // These are only enabled inside tabular
5131 case LFUN_CELL_BACKWARD:
5132 case LFUN_CELL_FORWARD:
5133 status.setEnabled(true);
5136 // disable these with multiple cells selected
5137 case LFUN_INSET_INSERT:
5138 case LFUN_TABULAR_INSERT:
5139 case LFUN_FLEX_INSERT:
5140 case LFUN_FLOAT_INSERT:
5141 case LFUN_FLOAT_WIDE_INSERT:
5142 case LFUN_FOOTNOTE_INSERT:
5143 case LFUN_MARGINALNOTE_INSERT:
5144 case LFUN_MATH_INSERT:
5145 case LFUN_MATH_MODE:
5146 case LFUN_MATH_MUTATE:
5147 case LFUN_MATH_DISPLAY:
5148 case LFUN_NOTE_INSERT:
5149 case LFUN_ARGUMENT_INSERT:
5150 case LFUN_BOX_INSERT:
5151 case LFUN_BRANCH_INSERT:
5152 case LFUN_PHANTOM_INSERT:
5153 case LFUN_WRAP_INSERT:
5154 case LFUN_PREVIEW_INSERT:
5155 case LFUN_ERT_INSERT: {
5156 if (cur.selIsMultiCell()) {
5157 status.setEnabled(false);
5160 return cell(cur.idx())->getStatus(cur, cmd, status);
5163 // disable in non-fixed-width cells
5164 case LFUN_PARAGRAPH_BREAK:
5165 // multirow does not allow paragraph breaks
5166 if (tabular.isMultiRow(cur.idx())) {
5167 status.setEnabled(false);
5171 case LFUN_NEWLINE_INSERT: {
5172 if (tabular.getPWidth(cur.idx()).zero()) {
5173 status.setEnabled(false);
5176 return cell(cur.idx())->getStatus(cur, cmd, status);
5179 case LFUN_NEWPAGE_INSERT:
5180 status.setEnabled(false);
5184 if (tabularStackDirty() && theClipboard().isInternal()) {
5185 if (cur.selIsMultiCell()) {
5188 getSelection(cur, rs, re, cs, ce);
5189 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5190 && paste_tabular->nrows() == re - rs + 1)
5191 status.setEnabled(true);
5193 status.setEnabled(false);
5194 status.message(_("Selection size should match clipboard content."));
5197 status.setEnabled(true);
5200 return cell(cur.idx())->getStatus(cur, cmd, status);
5202 case LFUN_INSET_SETTINGS:
5203 // relay this lfun to Inset, not to the cell.
5204 return Inset::getStatus(cur, cmd, status);
5207 // we try to handle this event in the insets dispatch function.
5208 return cell(cur.idx())->getStatus(cur, cmd, status);
5214 Inset::DisplayType InsetTabular::display() const
5216 if (tabular.is_long_tabular) {
5217 switch (tabular.longtabular_alignment) {
5218 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5220 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5222 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5232 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5234 tabular.latex(os, runparams);
5238 int InsetTabular::plaintext(odocstringstream & os,
5239 OutputParams const & runparams, size_t max_length) const
5241 os << '\n'; // output table on a new line
5242 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5243 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5244 return PLAINTEXT_NEWLINE;
5248 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5253 // FIXME: Why not pass a proper DocIterator here?
5255 // if the table is inside a float it doesn't need the informaltable
5256 // wrapper. Search for it.
5257 for (master = owner(); master; master = master->owner())
5258 if (master->lyxCode() == FLOAT_CODE)
5263 os << "<informaltable>";
5266 ret += tabular.docbook(os, runparams);
5268 os << "</informaltable>";
5275 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5277 return tabular.xhtml(xs, rp);
5281 void InsetTabular::validate(LaTeXFeatures & features) const
5283 tabular.validate(features);
5284 features.useInsetLayout(getLayout());
5288 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5290 return tabular.cellInset(idx);
5294 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5296 return tabular.cellInset(idx);
5300 void InsetTabular::cursorPos(BufferView const & bv,
5301 CursorSlice const & sl, bool boundary, int & x, int & y) const
5303 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5305 // y offset correction
5306 y += cellYPos(sl.idx());
5307 y += tabular.textVOffset(sl.idx());
5308 y += tabular.offsetVAlignment();
5310 // x offset correction
5311 x += cellXPos(sl.idx());
5312 x += tabular.textHOffset(sl.idx());
5313 x += ADD_TO_TABULAR_WIDTH;
5317 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5321 Inset const & inset = *tabular.cellInset(cell);
5322 Point o = bv.coordCache().getInsets().xy(&inset);
5323 int const xbeg = o.x_ - tabular.textHOffset(cell);
5324 int const xend = xbeg + tabular.cellWidth(cell);
5325 row_type const row = tabular.cellRow(cell);
5326 int const ybeg = o.y_ - tabular.rowAscent(row)
5327 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5328 int const yend = ybeg + tabular.cellHeight(cell);
5340 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5341 // << " ybeg=" << ybeg << " yend=" << yend
5342 // << " xx=" << xx << " yy=" << yy
5343 // << " dist=" << xx + yy << endl;
5348 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5350 //lyxerr << "InsetTabular::editXY: " << this << endl;
5352 cur.idx() = getNearestCell(cur.bv(), x, y);
5353 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5357 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5359 cur.idx() = getNearestCell(cur.bv(), x, y);
5360 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5364 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5366 idx_type idx_min = 0;
5367 int dist_min = numeric_limits<int>::max();
5368 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5369 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5370 int const d = dist(bv, i, x, y);
5381 int InsetTabular::cellYPos(idx_type const cell) const
5383 row_type row = tabular.cellRow(cell);
5385 for (row_type r = 0; r < row; ++r)
5386 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5387 + tabular.interRowSpace(r + 1);
5392 int InsetTabular::cellXPos(idx_type const cell) const
5394 col_type col = tabular.cellColumn(cell);
5396 for (col_type c = 0; c < col; ++c)
5397 lx += tabular.column_info[c].width;
5402 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5404 row_type const row = tabular.cellRow(cur.idx());
5405 col_type const col = tabular.cellColumn(cur.idx());
5407 if (isRightToLeft(cur)) {
5408 if (tabular.cellColumn(cur.idx()) == 0) {
5409 if (row == tabular.nrows() - 1)
5411 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5416 cur.idx() = tabular.getLastCellInRow(row - 1);
5418 cur.idx() = tabular.cellIndex(row, col - 1);
5421 if (tabular.isLastCell(cur.idx()))
5423 if (cur.idx() == tabular.getLastCellInRow(row))
5424 cur.idx() = tabular.cellIndex(row + 1, 0);
5426 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5427 cur.idx() = tabular.cellIndex(row, colnextcell);
5431 cur.boundary(false);
5433 if (cur.selIsMultiCell()) {
5434 cur.pit() = cur.lastpit();
5435 cur.pos() = cur.lastpos();
5442 // in visual mode, place cursor at extreme left or right
5444 switch(entry_from) {
5446 case ENTRY_DIRECTION_RIGHT:
5447 cur.posVisToRowExtremity(false /* !left */);
5449 case ENTRY_DIRECTION_LEFT:
5450 cur.posVisToRowExtremity(true /* left */);
5452 case ENTRY_DIRECTION_IGNORE:
5453 // nothing to do in this case
5457 cur.setCurrentFont();
5461 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5463 row_type const row = tabular.cellRow(cur.idx());
5464 col_type const col = tabular.cellColumn(cur.idx());
5466 if (isRightToLeft(cur)) {
5467 if (cur.idx() == tabular.getLastCellInRow(row)) {
5470 cur.idx() = tabular.getFirstCellInRow(row);
5471 cur.idx() = tabular.cellAbove(cur.idx());
5473 if (tabular.isLastCell(cur.idx()))
5475 if (cur.idx() == tabular.getLastCellInRow(row))
5476 cur.idx() = tabular.cellIndex(row + 1, 0);
5478 cur.idx() = tabular.cellIndex(row, col + 1);
5481 if (cur.idx() == 0) // first cell
5484 cur.idx() = tabular.getLastCellInRow(row - 1);
5486 cur.idx() = tabular.cellIndex(row, col - 1);
5489 if (cur.selIsMultiCell()) {
5490 cur.pit() = cur.lastpit();
5491 cur.pos() = cur.lastpos();
5495 cur.pit() = cur.lastpit();
5496 cur.pos() = cur.lastpos();
5498 // in visual mode, place cursor at extreme left or right
5500 switch(entry_from) {
5502 case ENTRY_DIRECTION_RIGHT:
5503 cur.posVisToRowExtremity(false /* !left */);
5505 case ENTRY_DIRECTION_LEFT:
5506 cur.posVisToRowExtremity(true /* left */);
5508 case ENTRY_DIRECTION_IGNORE:
5509 // nothing to do in this case
5513 cur.setCurrentFont();
5517 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5519 cur.recordUndoInset(this);
5521 istringstream is(argument);
5522 // limit the size of strings we read to avoid memory problems
5526 size_t safe_guard = 0;
5531 if (safe_guard > 1000) {
5532 LYXERR0("parameter max count reached!");
5536 Tabular::Feature action = Tabular::LAST_ACTION;
5539 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5540 if (s != tabularFeature[i].feature)
5542 action = tabularFeature[i].action;
5545 if (action == Tabular::LAST_ACTION) {
5546 LYXERR0("Feature not found " << s);
5550 if (tabularFeature[i].need_value)
5552 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5553 tabularFeatures(cur, action, val);
5558 static void checkLongtableSpecial(Tabular::ltType & ltt,
5559 string const & special, bool & flag)
5561 if (special == "dl_above") {
5564 } else if (special == "dl_below") {
5565 ltt.bottomDL = flag;
5567 } else if (special == "empty") {
5577 bool InsetTabular::oneCellHasRotationState(bool rotated,
5578 row_type row_start, row_type row_end,
5579 col_type col_start, col_type col_end) const
5581 for (row_type r = row_start; r <= row_end; ++r)
5582 for (col_type c = col_start; c <= col_end; ++c)
5584 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5587 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5594 void InsetTabular::tabularFeatures(Cursor & cur,
5595 Tabular::Feature feature, string const & value)
5597 col_type sel_col_start;
5598 col_type sel_col_end;
5599 row_type sel_row_start;
5600 row_type sel_row_end;
5601 bool setLines = false;
5602 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5603 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5607 case Tabular::M_ALIGN_LEFT:
5608 case Tabular::ALIGN_LEFT:
5609 setAlign = LYX_ALIGN_LEFT;
5612 case Tabular::M_ALIGN_RIGHT:
5613 case Tabular::ALIGN_RIGHT:
5614 setAlign = LYX_ALIGN_RIGHT;
5617 case Tabular::M_ALIGN_CENTER:
5618 case Tabular::ALIGN_CENTER:
5619 setAlign = LYX_ALIGN_CENTER;
5622 case Tabular::ALIGN_BLOCK:
5623 setAlign = LYX_ALIGN_BLOCK;
5626 case Tabular::ALIGN_DECIMAL:
5627 setAlign = LYX_ALIGN_DECIMAL;
5630 case Tabular::M_VALIGN_TOP:
5631 case Tabular::VALIGN_TOP:
5632 setVAlign = Tabular::LYX_VALIGN_TOP;
5635 case Tabular::M_VALIGN_BOTTOM:
5636 case Tabular::VALIGN_BOTTOM:
5637 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5640 case Tabular::M_VALIGN_MIDDLE:
5641 case Tabular::VALIGN_MIDDLE:
5642 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5649 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5650 row_type const row = tabular.cellRow(cur.idx());
5651 col_type const column = tabular.cellColumn(cur.idx());
5653 Tabular::ltType ltt;
5657 case Tabular::SET_TABULAR_WIDTH:
5658 tabular.setTabularWidth(Length(value));
5661 case Tabular::SET_PWIDTH: {
5662 Length const len(value);
5663 tabular.setColumnPWidth(cur, cur.idx(), len);
5665 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5666 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5670 case Tabular::SET_MPWIDTH:
5671 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5674 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
5675 bool const varwidth = value == "on";
5676 tabular.toggleVarwidth(cur.idx(), varwidth);
5680 case Tabular::SET_MROFFSET:
5681 tabular.setMROffset(cur, cur.idx(), Length(value));
5684 case Tabular::SET_SPECIAL_COLUMN:
5685 case Tabular::SET_SPECIAL_MULTICOLUMN:
5686 if (value == "none")
5687 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5689 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5692 case Tabular::APPEND_ROW:
5693 // append the row into the tabular
5694 tabular.appendRow(row);
5697 case Tabular::APPEND_COLUMN:
5698 // append the column into the tabular
5699 tabular.appendColumn(column);
5700 cur.idx() = tabular.cellIndex(row, column);
5703 case Tabular::DELETE_ROW:
5704 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5705 for (col_type c = 0; c < tabular.ncols(); c++)
5706 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5707 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5710 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5711 tabular.deleteRow(sel_row_start);
5712 if (sel_row_start >= tabular.nrows())
5714 cur.idx() = tabular.cellIndex(sel_row_start, column);
5717 cur.selection(false);
5720 case Tabular::DELETE_COLUMN:
5721 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5722 for (row_type r = 0; r < tabular.nrows(); r++)
5723 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5724 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5727 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5728 for (row_type r = 0; r < tabular.nrows(); r++)
5729 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5730 tabular.leftLine(tabular.cellIndex(r, 0)));
5733 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5734 tabular.deleteColumn(sel_col_start);
5735 if (sel_col_start >= tabular.ncols())
5737 cur.idx() = tabular.cellIndex(row, sel_col_start);
5740 cur.selection(false);
5743 case Tabular::COPY_ROW:
5744 tabular.copyRow(row);
5747 case Tabular::COPY_COLUMN:
5748 tabular.copyColumn(column);
5749 cur.idx() = tabular.cellIndex(row, column);
5752 case Tabular::MOVE_COLUMN_RIGHT:
5753 tabular.moveColumn(column, Tabular::RIGHT);
5754 cur.idx() = tabular.cellIndex(row, column + 1);
5757 case Tabular::MOVE_COLUMN_LEFT:
5758 tabular.moveColumn(column, Tabular::LEFT);
5759 cur.idx() = tabular.cellIndex(row, column - 1);
5762 case Tabular::MOVE_ROW_DOWN:
5763 tabular.moveRow(row, Tabular::DOWN);
5764 cur.idx() = tabular.cellIndex(row + 1, column);
5767 case Tabular::MOVE_ROW_UP:
5768 tabular.moveRow(row, Tabular::UP);
5769 cur.idx() = tabular.cellIndex(row - 1, column);
5772 case Tabular::SET_LINE_TOP:
5773 case Tabular::TOGGLE_LINE_TOP: {
5774 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5775 ? (value == "true") : !tabular.topLine(cur.idx());
5776 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5777 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5778 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5782 case Tabular::SET_LINE_BOTTOM:
5783 case Tabular::TOGGLE_LINE_BOTTOM: {
5784 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5785 ? (value == "true") : !tabular.bottomLine(cur.idx());
5786 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5787 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5788 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5792 case Tabular::SET_LINE_LEFT:
5793 case Tabular::TOGGLE_LINE_LEFT: {
5794 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5795 ? (value == "true") : !tabular.leftLine(cur.idx());
5796 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5797 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5798 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5802 case Tabular::SET_LINE_RIGHT:
5803 case Tabular::TOGGLE_LINE_RIGHT: {
5804 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5805 ? (value == "true") : !tabular.rightLine(cur.idx());
5806 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5807 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5808 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5812 case Tabular::M_ALIGN_LEFT:
5813 case Tabular::M_ALIGN_RIGHT:
5814 case Tabular::M_ALIGN_CENTER:
5815 case Tabular::ALIGN_LEFT:
5816 case Tabular::ALIGN_RIGHT:
5817 case Tabular::ALIGN_CENTER:
5818 case Tabular::ALIGN_BLOCK:
5819 case Tabular::ALIGN_DECIMAL:
5820 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5821 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5822 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5823 !tabular.getPWidth(c).zero());
5826 case Tabular::M_VALIGN_TOP:
5827 case Tabular::M_VALIGN_BOTTOM:
5828 case Tabular::M_VALIGN_MIDDLE:
5831 case Tabular::VALIGN_TOP:
5832 case Tabular::VALIGN_BOTTOM:
5833 case Tabular::VALIGN_MIDDLE:
5834 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5835 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5836 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5839 case Tabular::SET_MULTICOLUMN: {
5840 if (!cur.selection()) {
5841 // just multicol for one single cell
5842 // check whether we are completely in a multicol
5843 if (!tabular.isMultiColumn(cur.idx()))
5844 tabular.setMultiColumn(cur.idx(), 1,
5845 tabular.rightLine(cur.idx()));
5848 // we have a selection so this means we just add all these
5849 // cells to form a multicolumn cell
5850 idx_type const s_start = cur.selBegin().idx();
5851 row_type const col_start = tabular.cellColumn(s_start);
5852 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5853 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5854 tabular.rightLine(cur.selEnd().idx()));
5857 cur.selection(false);
5861 case Tabular::UNSET_MULTICOLUMN: {
5862 if (!cur.selection()) {
5863 if (tabular.isMultiColumn(cur.idx()))
5864 tabular.unsetMultiColumn(cur.idx());
5869 case Tabular::MULTICOLUMN: {
5870 if (!cur.selection()) {
5871 if (tabular.isMultiColumn(cur.idx()))
5872 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5874 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5878 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5879 row_type const r = sel_row_start;
5880 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5881 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5884 // If the selection contains at least one singlecol cell
5885 // or multiple multicol cells,
5886 // we assume the user will merge is to a single multicol
5888 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5890 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5894 case Tabular::SET_MULTIROW: {
5895 if (!cur.selection()) {
5896 // just multirow for one single cell
5897 // check whether we are completely in a multirow
5898 if (!tabular.isMultiRow(cur.idx()))
5899 tabular.setMultiRow(cur.idx(), 1,
5900 tabular.bottomLine(cur.idx()),
5901 tabular.getAlignment(cur.idx()));
5904 // we have a selection so this means we just add all this
5905 // cells to form a multirow cell
5906 idx_type const s_start = cur.selBegin().idx();
5907 row_type const row_start = tabular.cellRow(s_start);
5908 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5909 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5910 tabular.bottomLine(cur.selEnd().idx()),
5911 tabular.getAlignment(cur.selEnd().idx()));
5914 cur.selection(false);
5918 case Tabular::UNSET_MULTIROW: {
5919 if (!cur.selection()) {
5920 if (tabular.isMultiRow(cur.idx()))
5921 tabular.unsetMultiRow(cur.idx());
5926 case Tabular::MULTIROW: {
5927 if (!cur.selection()) {
5928 if (tabular.isMultiRow(cur.idx()))
5929 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5931 tabularFeatures(cur, Tabular::SET_MULTIROW);
5935 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5936 col_type const c = sel_col_start;
5937 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5938 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5941 // If the selection contains at least one singlerow cell
5942 // or multiple multirow cells,
5943 // we assume the user will merge is to a single multirow
5945 tabularFeatures(cur, Tabular::SET_MULTIROW);
5947 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5951 case Tabular::SET_ALL_LINES:
5954 case Tabular::UNSET_ALL_LINES:
5955 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5956 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5957 idx_type const cell = tabular.cellIndex(r, c);
5958 tabular.setTopLine(cell, setLines);
5959 tabular.setBottomLine(cell, setLines);
5960 tabular.setRightLine(cell, setLines);
5961 tabular.setLeftLine(cell, setLines);
5965 case Tabular::SET_BORDER_LINES:
5966 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5967 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5968 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5970 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5971 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5972 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5976 case Tabular::TOGGLE_LONGTABULAR:
5977 if (tabular.is_long_tabular)
5978 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
5980 tabular.is_long_tabular = true;
5983 case Tabular::SET_LONGTABULAR:
5984 tabular.is_long_tabular = true;
5987 case Tabular::UNSET_LONGTABULAR:
5988 for (row_type r = 0; r < tabular.nrows(); ++r) {
5989 if (tabular.ltCaption(r)) {
5990 cur.idx() = tabular.cellIndex(r, 0);
5993 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5996 tabular.is_long_tabular = false;
5999 case Tabular::SET_ROTATE_TABULAR:
6000 tabular.rotate = convert<int>(value);
6003 case Tabular::UNSET_ROTATE_TABULAR:
6007 case Tabular::TOGGLE_ROTATE_TABULAR:
6008 // when pressing the rotate button we default to 90° rotation
6009 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6012 case Tabular::TABULAR_VALIGN_TOP:
6013 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6016 case Tabular::TABULAR_VALIGN_MIDDLE:
6017 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6020 case Tabular::TABULAR_VALIGN_BOTTOM:
6021 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6024 case Tabular::LONGTABULAR_ALIGN_LEFT:
6025 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6028 case Tabular::LONGTABULAR_ALIGN_CENTER:
6029 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6032 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6033 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6038 case Tabular::SET_ROTATE_CELL:
6039 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6040 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6041 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6044 case Tabular::UNSET_ROTATE_CELL:
6045 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6046 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6047 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6050 case Tabular::TOGGLE_ROTATE_CELL:
6052 bool oneNotRotated = oneCellHasRotationState(false,
6053 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6055 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6056 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6057 // when pressing the rotate cell button we default to 90° rotation
6059 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6061 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6066 case Tabular::SET_USEBOX: {
6067 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6068 if (val == tabular.getUsebox(cur.idx()))
6069 val = Tabular::BOX_NONE;
6070 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6071 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6072 tabular.setUsebox(tabular.cellIndex(r, c), val);
6076 case Tabular::UNSET_LTFIRSTHEAD:
6079 case Tabular::SET_LTFIRSTHEAD:
6080 tabular.getRowOfLTFirstHead(row, ltt);
6081 checkLongtableSpecial(ltt, value, flag);
6082 tabular.setLTHead(row, flag, ltt, true);
6085 case Tabular::UNSET_LTHEAD:
6088 case Tabular::SET_LTHEAD:
6089 tabular.getRowOfLTHead(row, ltt);
6090 checkLongtableSpecial(ltt, value, flag);
6091 tabular.setLTHead(row, flag, ltt, false);
6094 case Tabular::UNSET_LTFOOT:
6097 case Tabular::SET_LTFOOT:
6098 tabular.getRowOfLTFoot(row, ltt);
6099 checkLongtableSpecial(ltt, value, flag);
6100 tabular.setLTFoot(row, flag, ltt, false);
6103 case Tabular::UNSET_LTLASTFOOT:
6106 case Tabular::SET_LTLASTFOOT:
6107 tabular.getRowOfLTLastFoot(row, ltt);
6108 checkLongtableSpecial(ltt, value, flag);
6109 tabular.setLTFoot(row, flag, ltt, true);
6112 case Tabular::UNSET_LTNEWPAGE:
6115 case Tabular::SET_LTNEWPAGE:
6116 tabular.setLTNewPage(row, flag);
6119 case Tabular::SET_LTCAPTION: {
6120 if (tabular.ltCaption(row))
6122 cur.idx() = tabular.setLTCaption(row, true);
6125 cur.selection(false);
6126 // If a row is set as caption, then also insert
6127 // a caption. Otherwise the LaTeX output is broken.
6128 // Select cell if it is non-empty
6129 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6130 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6131 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6135 case Tabular::UNSET_LTCAPTION: {
6136 if (!tabular.ltCaption(row))
6138 cur.idx() = tabular.setLTCaption(row, false);
6141 cur.selection(false);
6142 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6143 if (lyx::getStatus(fr).enabled())
6148 case Tabular::TOGGLE_LTCAPTION: {
6149 if (tabular.ltCaption(row))
6150 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6152 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6156 case Tabular::TOGGLE_BOOKTABS:
6157 tabular.use_booktabs = !tabular.use_booktabs;
6160 case Tabular::SET_BOOKTABS:
6161 tabular.use_booktabs = true;
6164 case Tabular::UNSET_BOOKTABS:
6165 tabular.use_booktabs = false;
6168 case Tabular::SET_TOP_SPACE: {
6170 if (value == "default")
6171 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6172 tabular.row_info[r].top_space_default = true;
6173 else if (value == "none")
6174 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6175 tabular.row_info[r].top_space_default = false;
6176 tabular.row_info[r].top_space = len;
6178 else if (isValidLength(value, &len))
6179 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6180 tabular.row_info[r].top_space_default = false;
6181 tabular.row_info[r].top_space = len;
6186 case Tabular::SET_BOTTOM_SPACE: {
6188 if (value == "default")
6189 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6190 tabular.row_info[r].bottom_space_default = true;
6191 else if (value == "none")
6192 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6193 tabular.row_info[r].bottom_space_default = false;
6194 tabular.row_info[r].bottom_space = len;
6196 else if (isValidLength(value, &len))
6197 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6198 tabular.row_info[r].bottom_space_default = false;
6199 tabular.row_info[r].bottom_space = len;
6204 case Tabular::SET_INTERLINE_SPACE: {
6206 if (value == "default")
6207 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6208 tabular.row_info[r].interline_space_default = true;
6209 else if (value == "none")
6210 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6211 tabular.row_info[r].interline_space_default = false;
6212 tabular.row_info[r].interline_space = len;
6214 else if (isValidLength(value, &len))
6215 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6216 tabular.row_info[r].interline_space_default = false;
6217 tabular.row_info[r].interline_space = len;
6222 case Tabular::SET_DECIMAL_POINT:
6223 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6224 tabular.column_info[c].decimal_point = from_utf8(value);
6227 // dummy stuff just to avoid warnings
6228 case Tabular::LAST_ACTION:
6234 bool InsetTabular::copySelection(Cursor & cur)
6236 if (!cur.selection())
6241 getSelection(cur, rs, re, cs, ce);
6243 paste_tabular.reset(new Tabular(tabular));
6245 for (row_type r = 0; r < rs; ++r)
6246 paste_tabular->deleteRow(0);
6248 row_type const rows = re - rs + 1;
6249 while (paste_tabular->nrows() > rows)
6250 paste_tabular->deleteRow(rows);
6252 for (col_type c = 0; c < cs; ++c)
6253 paste_tabular->deleteColumn(0);
6255 col_type const columns = ce - cs + 1;
6256 while (paste_tabular->ncols() > columns)
6257 paste_tabular->deleteColumn(columns);
6259 paste_tabular->setBuffer(tabular.buffer());
6261 odocstringstream os;
6262 OutputParams const runparams(0);
6263 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6264 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6265 cap::copySelection(cur, os.str());
6267 // mark tabular stack dirty
6268 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6269 // when we (hopefully) have a one-for-all paste mechanism.
6270 // This must be called after cap::copySelection.
6271 dirtyTabularStack(true);
6277 bool InsetTabular::pasteClipboard(Cursor & cur)
6281 col_type actcol = tabular.cellColumn(cur.idx());
6282 row_type actrow = tabular.cellRow(cur.idx());
6284 if (cur.selIsMultiCell()) {
6287 getSelection(cur, actrow, re, actcol, ce);
6290 for (row_type r1 = 0, r2 = actrow;
6291 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6293 for (col_type c1 = 0, c2 = actcol;
6294 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6296 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6297 tabular.isPartOfMultiColumn(r2, c2))
6299 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6303 if (tabular.isPartOfMultiColumn(r2, c2)) {
6307 shared_ptr<InsetTableCell> inset(
6308 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6309 tabular.setCellInset(r2, c2, inset);
6310 // FIXME?: why do we need to do this explicitly? (EL)
6311 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6313 // FIXME: change tracking (MG)
6314 inset->setChange(Change(buffer().params().track_changes ?
6315 Change::INSERTED : Change::UNCHANGED));
6324 void InsetTabular::cutSelection(Cursor & cur)
6326 if (!cur.selection())
6331 getSelection(cur, rs, re, cs, ce);
6332 for (row_type r = rs; r <= re; ++r) {
6333 for (col_type c = cs; c <= ce; ++c) {
6334 shared_ptr<InsetTableCell> t
6335 = cell(tabular.cellIndex(r, c));
6336 if (buffer().params().track_changes)
6337 // FIXME: Change tracking (MG)
6338 t->setChange(Change(Change::DELETED));
6344 // cursor position might be invalid now
6345 if (cur.pit() > cur.lastpit())
6346 cur.pit() = cur.lastpit();
6347 if (cur.pos() > cur.lastpos())
6348 cur.pos() = cur.lastpos();
6349 cur.clearSelection();
6353 bool InsetTabular::isRightToLeft(Cursor & cur) const
6355 // LASSERT: It might be better to abandon this Buffer.
6356 LASSERT(cur.depth() > 1, return false);
6357 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6358 pos_type const parentpos = cur[cur.depth() - 2].pos();
6359 return parentpar.getFontSettings(buffer().params(),
6360 parentpos).language()->rightToLeft();
6364 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6367 LASSERT(stidx <= enidx, return docstring());
6369 col_type const col1 = tabular.cellColumn(stidx);
6370 col_type const col2 = tabular.cellColumn(enidx);
6371 row_type const row1 = tabular.cellRow(stidx);
6372 row_type const row2 = tabular.cellRow(enidx);
6374 for (col_type col = col1; col <= col2; col++)
6375 for (row_type row = row1; row <= row2; row++) {
6380 retval += tabular.cellInset(row, col)->asString(intoInsets);
6386 void InsetTabular::getSelection(Cursor & cur,
6387 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6389 CursorSlice const & beg = cur.selBegin();
6390 CursorSlice const & end = cur.selEnd();
6391 cs = tabular.cellColumn(beg.idx());
6392 ce = tabular.cellColumn(end.idx());
6396 rs = tabular.cellRow(beg.idx());
6397 re = tabular.cellRow(end.idx());
6403 Text * InsetTabular::getText(int idx) const
6405 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6409 void InsetTabular::setChange(Change const & change)
6411 for (idx_type idx = 0; idx < nargs(); ++idx)
6412 cell(idx)->setChange(change);
6416 void InsetTabular::acceptChanges()
6418 for (idx_type idx = 0; idx < nargs(); ++idx)
6419 cell(idx)->acceptChanges();
6423 void InsetTabular::rejectChanges()
6425 for (idx_type idx = 0; idx < nargs(); ++idx)
6426 cell(idx)->rejectChanges();
6430 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6432 return tabular.getPWidth(cell).zero();
6436 bool InsetTabular::forcePlainLayout(idx_type cell) const
6438 return !tabular.getPWidth(cell).zero();
6442 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6445 if (buf.length() <= 0)
6450 col_type maxCols = 1;
6451 size_t const len = buf.length();
6455 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6463 maxCols = max(cols, maxCols);
6469 maxCols = max(cols, maxCols);
6475 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6476 loctab = paste_tabular.get();
6477 dirtyTabularStack(true);
6480 cell = bv.cursor().idx();
6481 ocol = tabular.cellColumn(cell);
6482 row = tabular.cellRow(cell);
6486 idx_type const cells = loctab->numberofcells;
6489 rows = loctab->nrows();
6490 col_type const columns = loctab->ncols();
6492 while (cell < cells && p < len && row < rows &&
6493 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6499 // we can only set this if we are not too far right
6500 if (cols < columns) {
6501 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6502 Font const font = bv.textMetrics(&inset->text()).
6504 inset->setText(buf.substr(op, p - op), font,
6505 buffer().params().track_changes);
6511 // we can only set this if we are not too far right
6512 if (cols < columns) {
6513 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6514 Font const font = bv.textMetrics(&inset->text()).
6516 inset->setText(buf.substr(op, p - op), font,
6517 buffer().params().track_changes);
6522 cell = loctab->cellIndex(row, cols);
6528 // check for the last cell if there is no trailing '\n'
6529 if (cell < cells && op < len) {
6530 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6531 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6532 inset->setText(buf.substr(op, len - op), font,
6533 buffer().params().track_changes);
6539 void InsetTabular::addPreview(DocIterator const & inset_pos,
6540 PreviewLoader & loader) const
6542 DocIterator cell_pos = inset_pos;
6544 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6545 for (row_type r = 0; r < tabular.nrows(); ++r) {
6546 for (col_type c = 0; c < tabular.ncols(); ++c) {
6547 cell_pos.top().idx() = tabular.cellIndex(r, c);
6548 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6554 bool InsetTabular::completionSupported(Cursor const & cur) const
6556 Cursor const & bvCur = cur.bv().cursor();
6557 if (&bvCur.inset() != this)
6559 return cur.text()->completionSupported(cur);
6563 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6565 return completionSupported(cur);
6569 bool InsetTabular::automaticInlineCompletion() const
6571 return lyxrc.completion_inline_text;
6575 bool InsetTabular::automaticPopupCompletion() const
6577 return lyxrc.completion_popup_text;
6581 bool InsetTabular::showCompletionCursor() const
6583 return lyxrc.completion_cursor_text;
6587 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6589 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6593 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6595 if (!completionSupported(cur))
6597 return cur.text()->completionPrefix(cur);
6601 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6603 if (!completionSupported(cur))
6606 return cur.text()->insertCompletion(cur, s, finished);
6610 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6611 Dimension & dim) const
6613 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6614 tm.completionPosAndDim(cur, x, y, dim);
6618 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6620 istringstream data(in);
6622 lex.setStream(data);
6629 if (!lex || token != "tabular") {
6630 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6634 // This is part of the inset proper that is usually swallowed
6635 // by Buffer::readInset
6637 if (!lex || token != "Tabular") {
6638 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6646 string InsetTabular::params2string(InsetTabular const & inset)
6649 data << "tabular" << ' ';
6651 data << "\\end_inset\n";
6656 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6658 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6659 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6660 if (!tabular.isPartOfMultiColumn(r,c) &&
6661 !tabular.isPartOfMultiRow(r,c))
6664 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6665 ParagraphList::iterator it = parlist.begin();
6666 ParagraphList::iterator const en = parlist.end();
6667 for (; it != en; ++it)
6668 it->setLayout(dc.plainLayout());