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:
267 case Tabular::BOX_VARWIDTH:
274 // I would have liked a fromstr template a lot better. (Lgb)
275 bool string2type(string const & str, LyXAlignment & num)
278 num = LYX_ALIGN_NONE;
279 else if (str == "block")
280 num = LYX_ALIGN_BLOCK;
281 else if (str == "left")
282 num = LYX_ALIGN_LEFT;
283 else if (str == "center")
284 num = LYX_ALIGN_CENTER;
285 else if (str == "right")
286 num = LYX_ALIGN_RIGHT;
287 else if (str == "decimal")
288 num = LYX_ALIGN_DECIMAL;
295 bool string2type(string const & str, Tabular::HAlignment & num)
298 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
299 else if (str == "center" )
300 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
301 else if (str == "right")
302 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
309 bool string2type(string const & str, Tabular::VAlignment & num)
312 num = Tabular::LYX_VALIGN_TOP;
313 else if (str == "middle" )
314 num = Tabular::LYX_VALIGN_MIDDLE;
315 else if (str == "bottom")
316 num = Tabular::LYX_VALIGN_BOTTOM;
323 bool string2type(string const & str, Tabular::BoxType & num)
326 num = Tabular::BOX_NONE;
327 else if (str == "parbox")
328 num = Tabular::BOX_PARBOX;
329 else if (str == "minipage")
330 num = Tabular::BOX_MINIPAGE;
331 else if (str == "varwidth")
332 num = Tabular::BOX_VARWIDTH;
339 bool string2type(string const & str, bool & num)
343 else if (str == "false")
351 bool getTokenValue(string const & str, char const * token, string & ret)
354 size_t token_length = strlen(token);
355 size_t pos = str.find(token);
357 if (pos == string::npos || pos + token_length + 1 >= str.length()
358 || str[pos + token_length] != '=')
360 pos += token_length + 1;
362 if (ch != '"' && ch != '\'') { // only read till next space
366 while (pos < str.length() - 1 && str[++pos] != ch)
373 bool getTokenValue(string const & str, char const * token, docstring & ret)
376 bool const success = getTokenValue(str, token, tmp);
377 ret = from_utf8(tmp);
382 bool getTokenValue(string const & str, char const * token, int & num)
386 if (!getTokenValue(str, token, tmp))
388 num = convert<int>(tmp);
393 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
396 return getTokenValue(str, token, tmp) && string2type(tmp, num);
400 bool getTokenValue(string const & str, char const * token,
401 Tabular::HAlignment & num)
404 return getTokenValue(str, token, tmp) && string2type(tmp, num);
408 bool getTokenValue(string const & str, char const * token,
409 Tabular::VAlignment & num)
412 return getTokenValue(str, token, tmp) && string2type(tmp, num);
416 bool getTokenValue(string const & str, char const * token,
417 Tabular::BoxType & num)
420 return getTokenValue(str, token, tmp) && string2type(tmp, num);
424 bool getTokenValue(string const & str, char const * token, bool & flag)
426 // set the flag always to false as this should be the default for bools
427 // not in the file-format.
430 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
434 bool getTokenValue(string const & str, char const * token, Length & len)
436 // set the length to be zero() as default as this it should be if not
437 // in the file format.
440 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
444 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
449 if (!getTokenValue(str, token, tmp))
451 if (tmp == "default") {
455 return isValidLength(tmp, &len);
459 void l_getline(istream & is, string & str)
462 while (str.empty()) {
464 if (!str.empty() && str[str.length() - 1] == '\r')
465 str.erase(str.length() - 1);
470 string const write_attribute(string const & name, T const & t)
472 string const s = tostr(t);
473 return s.empty() ? s : " " + name + "=\"" + s + "\"";
477 string const write_attribute(string const & name, string const & t)
479 return t.empty() ? t : " " + name + "=\"" + t + "\"";
484 string const write_attribute(string const & name, docstring const & t)
486 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
491 string const write_attribute(string const & name, bool const & b)
493 // we write only true attribute values so we remove a bit of the
494 // file format bloat for tabulars.
495 return b ? write_attribute(name, convert<string>(b)) : string();
500 string const write_attribute(string const & name, int const & i)
502 // we write only true attribute values so we remove a bit of the
503 // file format bloat for tabulars.
504 return i ? write_attribute(name, convert<string>(i)) : string();
509 string const write_attribute(string const & name, Tabular::idx_type const & i)
511 // we write only true attribute values so we remove a bit of the
512 // file format bloat for tabulars.
513 return i ? write_attribute(name, convert<string>(i)) : string();
518 string const write_attribute(string const & name, Length const & value)
520 // we write only the value if we really have one same reson as above.
521 return value.zero() ? string() : write_attribute(name, value.asString());
527 string const featureAsString(Tabular::Feature action)
529 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
530 if (tabularFeature[i].action == action)
531 return tabularFeature[i].feature;
537 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
539 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
540 for (; dit; dit.forwardChar())
541 if (dit.inTexted() && dit.depth() == 1
542 && dit.paragraph().find(align_d, false, false, dit.pos()))
549 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
551 InsetTableCell tail = InsetTableCell(head);
552 DocIterator const dit = separatorPos(&head, align_d);
555 pit_type const psize = head.paragraphs().front().size();
556 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
557 tail.paragraphs().front().eraseChars(0,
558 dit.pos() < psize ? dit.pos() + 1 : psize, false);
565 /////////////////////////////////////////////////////////////////////
569 /////////////////////////////////////////////////////////////////////
572 Tabular::CellData::CellData(Buffer * buf)
575 multicolumn(Tabular::CELL_NORMAL),
576 multirow(Tabular::CELL_NORMAL),
577 alignment(LYX_ALIGN_CENTER),
578 valignment(LYX_VALIGN_TOP),
588 inset(new InsetTableCell(buf))
590 inset->setBuffer(*buf);
594 Tabular::CellData::CellData(CellData const & cs)
597 multicolumn(cs.multicolumn),
598 multirow(cs.multirow),
599 mroffset(cs.mroffset),
600 alignment(cs.alignment),
601 valignment(cs.valignment),
602 decimal_hoffset(cs.decimal_hoffset),
603 decimal_width(cs.decimal_width),
605 top_line(cs.top_line),
606 bottom_line(cs.bottom_line),
607 left_line(cs.left_line),
608 right_line(cs.right_line),
611 align_special(cs.align_special),
613 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
617 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
623 multicolumn = cs.multicolumn;
624 multirow = cs.multirow;
625 mroffset = cs.mroffset;
626 alignment = cs.alignment;
627 valignment = cs.valignment;
628 decimal_hoffset = cs.decimal_hoffset;
629 decimal_width = cs.decimal_width;
630 voffset = cs.voffset;
631 top_line = cs.top_line;
632 bottom_line = cs.bottom_line;
633 left_line = cs.left_line;
634 right_line = cs.right_line;
637 align_special = cs.align_special;
638 p_width = cs.p_width;
639 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
643 Tabular::RowData::RowData()
646 top_space_default(false),
647 bottom_space_default(false),
648 interline_space_default(false),
658 Tabular::ColumnData::ColumnData()
659 : alignment(LYX_ALIGN_CENTER),
660 valignment(LYX_VALIGN_TOP),
667 Tabular::ltType::ltType()
668 : set(false), topDL(false),
674 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
676 init(buffer, rows_arg, columns_arg);
680 void Tabular::setBuffer(Buffer & buffer)
683 for (row_type i = 0; i < nrows(); ++i)
684 for (col_type j = 0; j < ncols(); ++j)
685 cell_info[i][j].inset->setBuffer(*buffer_);
689 // activates all lines and sets all widths to 0
690 void Tabular::init(Buffer * buf, row_type rows_arg,
691 col_type columns_arg)
694 row_info = row_vector(rows_arg);
695 column_info = column_vector(columns_arg);
696 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
697 row_info.reserve(10);
698 column_info.reserve(10);
699 cell_info.reserve(100);
701 is_long_tabular = false;
702 tabular_valignment = LYX_VALIGN_MIDDLE;
703 tabular_width = Length();
704 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
706 use_booktabs = false;
707 // set silly default lines
708 for (row_type r = 0; r < nrows(); ++r)
709 for (col_type c = 0; c < ncols(); ++c) {
710 cell_info[r][c].inset->setBuffer(*buffer_);
711 cell_info[r][c].top_line = true;
712 cell_info[r][c].left_line = true;
713 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
714 cell_info[r][c].right_line = c == ncols() - 1;
719 void Tabular::deleteRow(row_type const row)
721 // Not allowed to delete last row
725 for (col_type c = 0; c < ncols(); ++c) {
726 // Care about multirow cells
727 if (row + 1 < nrows() &&
728 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
729 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
730 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
733 row_info.erase(row_info.begin() + row);
734 cell_info.erase(cell_info.begin() + row);
739 void Tabular::copyRow(row_type const row)
741 insertRow(row, true);
745 void Tabular::appendRow(row_type row)
747 insertRow(row, false);
751 void Tabular::insertRow(row_type const row, bool copy)
753 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
754 cell_info.insert(cell_info.begin() + row + 1,
755 cell_vector(0, CellData(buffer_)));
757 for (col_type c = 0; c < ncols(); ++c) {
758 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
759 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
760 if (buffer().params().track_changes)
761 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
762 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
763 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
767 for (col_type c = 0; c < ncols(); ++c) {
768 if (isPartOfMultiRow(row, c))
770 // inherit line settings
771 idx_type const i = cellIndex(row + 1, c);
772 idx_type const j = cellIndex(row, c);
773 setLeftLine(i, leftLine(j));
774 setRightLine(i, rightLine(j));
775 setTopLine(i, topLine(j));
776 if (topLine(j) && bottomLine(j)) {
777 setBottomLine(i, true);
778 setBottomLine(j, false);
780 // mark track changes
781 if (buffer().params().track_changes)
782 cellInfo(i).inset->setChange(Change(Change::INSERTED));
787 void Tabular::moveColumn(col_type col, ColDirection direction)
789 if (direction == Tabular::LEFT)
792 std::swap(column_info[col], column_info[col + 1]);
794 for (row_type r = 0; r < nrows(); ++r) {
795 std::swap(cell_info[r][col], cell_info[r][col + 1]);
796 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
797 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
799 // FIXME track changes is broken for tabular features (#8469)
800 idx_type const i = cellIndex(r, col);
801 idx_type const j = cellIndex(r, col + 1);
802 if (buffer().params().track_changes) {
803 cellInfo(i).inset->setChange(Change(Change::INSERTED));
804 cellInfo(j).inset->setChange(Change(Change::INSERTED));
811 void Tabular::moveRow(row_type row, RowDirection direction)
813 if (direction == Tabular::UP)
816 std::swap(row_info[row], row_info[row + 1]);
818 for (col_type c = 0; c < ncols(); ++c) {
819 std::swap(cell_info[row][c], cell_info[row + 1][c]);
820 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
821 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
823 // FIXME track changes is broken for tabular features (#8469)
824 idx_type const i = cellIndex(row, c);
825 idx_type const j = cellIndex(row + 1, c);
826 if (buffer().params().track_changes) {
827 cellInfo(i).inset->setChange(Change(Change::INSERTED));
828 cellInfo(j).inset->setChange(Change(Change::INSERTED));
835 void Tabular::deleteColumn(col_type const col)
837 // Not allowed to delete last column
841 for (row_type r = 0; r < nrows(); ++r) {
842 // Care about multicolumn cells
843 if (col + 1 < ncols() &&
844 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
845 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
846 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
848 cell_info[r].erase(cell_info[r].begin() + col);
850 column_info.erase(column_info.begin() + col);
855 void Tabular::copyColumn(col_type const col)
857 insertColumn(col, true);
861 void Tabular::appendColumn(col_type col)
863 insertColumn(col, false);
867 void Tabular::insertColumn(col_type const col, bool copy)
869 BufferParams const & bp = buffer().params();
870 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
872 for (row_type r = 0; r < nrows(); ++r) {
873 cell_info[r].insert(cell_info[r].begin() + col + 1,
874 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
875 if (bp.track_changes)
876 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
877 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
878 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
881 for (row_type r = 0; r < nrows(); ++r) {
882 // inherit line settings
883 idx_type const i = cellIndex(r, col + 1);
884 idx_type const j = cellIndex(r, col);
885 setBottomLine(i, bottomLine(j));
886 setTopLine(i, topLine(j));
887 setLeftLine(i, leftLine(j));
888 setRightLine(i, rightLine(j));
889 if (rightLine(i) && rightLine(j)) {
890 setRightLine(j, false);
892 if (buffer().params().track_changes)
893 cellInfo(i).inset->setChange(Change(Change::INSERTED));
898 void Tabular::updateIndexes()
903 for (row_type row = 0; row < nrows(); ++row)
904 for (col_type column = 0; column < ncols(); ++column) {
905 if (!isPartOfMultiColumn(row, column)
906 && !isPartOfMultiRow(row, column))
908 if (isPartOfMultiRow(row, column))
909 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
911 cell_info[row][column].cellno = numberofcells - 1;
914 rowofcell.resize(numberofcells);
915 columnofcell.resize(numberofcells);
917 // reset column and row of cells and update their width and alignment
918 for (row_type row = 0; row < nrows(); ++row)
919 for (col_type column = 0; column < ncols(); ++column) {
920 if (isPartOfMultiColumn(row, column)) {
921 cell_info[row][column].inset->toggleMultiCol(true);
924 cell_info[row][column].inset->toggleMultiCol(false);
925 // columnofcell needs to be called before setting width and aligment
926 // multirow cells inherit the width from the column width
927 if (!isPartOfMultiRow(row, column)) {
928 columnofcell[i] = column;
931 setFixedWidth(row, column);
932 if (isPartOfMultiRow(row, column)) {
933 cell_info[row][column].inset->toggleMultiRow(true);
936 cell_info[row][column].inset->toggleMultiRow(false);
937 cell_info[row][column].inset->setContentAlignment(
938 getAlignment(cellIndex(row, column)));
944 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
947 for (col_type c = 0; c < ncols(); ++c)
948 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
954 bool Tabular::topLine(idx_type const cell) const
956 return cellInfo(cell).top_line;
960 bool Tabular::bottomLine(idx_type const cell) const
962 return cellInfo(cell).bottom_line;
966 bool Tabular::leftLine(idx_type cell) const
970 return cellInfo(cell).left_line;
974 bool Tabular::rightLine(idx_type cell) const
978 return cellInfo(cell).right_line;
982 int Tabular::interRowSpace(row_type row) const
984 if (!row || row >= nrows())
987 int const interline_space = row_info[row - 1].interline_space_default ?
989 row_info[row - 1].interline_space.inPixels(width());
990 if (rowTopLine(row) && rowBottomLine(row - 1))
991 return interline_space + WIDTH_OF_LINE;
992 return interline_space;
996 int Tabular::interColumnSpace(idx_type cell) const
998 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
999 if (rightLine(cell) && nextcol < ncols()
1000 && leftLine(cellIndex(cellRow(cell), nextcol)))
1001 return WIDTH_OF_LINE;
1006 int Tabular::cellWidth(idx_type cell) const
1009 col_type const span = columnSpan(cell);
1010 col_type const col = cellColumn(cell);
1011 for(col_type c = col; c < col + span ; ++c)
1012 w += column_info[c].width;
1017 int Tabular::cellHeight(idx_type cell) const
1019 row_type const span = rowSpan(cell);
1020 row_type const row = cellRow(cell);
1022 for(row_type r = row; r < row + span ; ++r) {
1023 h += rowAscent(r) + rowDescent(r);
1024 if (r != row + span - 1)
1025 h += interRowSpace(r + 1);
1032 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1034 vector<int> max_dwidth(ncols(), 0);
1035 // collect max. fixed width of column
1036 map<col_type, int> max_pwidth;
1037 // collect max. variable width of column
1038 map<col_type, int> max_width;
1039 // collect max. variable width of xcolumn
1040 map<col_type, int> max_xwidth;
1042 for(col_type c = 0; c < ncols(); ++c)
1043 for(row_type r = 0; r < nrows(); ++r) {
1044 idx_type const i = cellIndex(r, c);
1045 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1046 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1047 if (!getPWidth(i).zero())
1048 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1049 else if (column_info[c].varwidth)
1050 max_xwidth[c] = max(max_xwidth[c], cell_info[r][c].width);
1052 max_width[c] = max(max_width[c], cell_info[r][c].width);
1055 // If we have a fixed tabular width, we take this into account
1056 Length tab_width = tabular_width;
1057 bool const tabularx = hasVarwidthColumn();
1058 if (tabularx && tab_width.zero())
1059 // If no tabular width is specified with X columns,
1060 // we use 100% colwidth
1061 tab_width = Length(100, Length::PCW);
1063 if (!tab_width.zero()) {
1064 restwidth = mi.base.inPixels(tab_width);
1065 // Substract the fixed widths from the table width
1066 for (auto const w : max_pwidth)
1067 restwidth -= w.second;
1070 // If we have a fixed width, distribute the available table width
1071 // (minus the fixed widths) to the variable-width columns
1073 int restcols = ncols() - max_pwidth.size();
1075 vcolwidth = restwidth / restcols;
1077 // Now consider that some variable width columns exceed the vcolwidth
1078 // FIXME As opposed to tabular*, tabularx with X column do not exceed
1079 // the overall table width. This is not yet represented, as it
1080 // needs to include row breaking.
1081 if (vcolwidth > 0) {
1082 bool changed = false;
1083 for (auto const w : max_width) {
1084 if (tabularx || w.second > vcolwidth) {
1086 restwidth -= w.second;
1090 for (auto const w : max_xwidth) {
1091 if (w.second > vcolwidth) {
1093 restwidth -= w.second;
1097 if (changed && restwidth > 0)
1098 vcolwidth = restwidth / restcols;
1101 bool update = false;
1102 // for each col get max of single col cells
1103 for(col_type c = 0; c < ncols(); ++c) {
1105 for(row_type r = 0; r < nrows(); ++r) {
1106 idx_type const i = cellIndex(r, c);
1107 if (columnSpan(i) == 1) {
1108 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1109 && cell_info[r][c].decimal_width != 0)
1110 new_width = max(new_width, cellInfo(i).width
1111 + max_dwidth[c] - cellInfo(i).decimal_width);
1112 else if (getPWidth(i).zero() && vcolwidth > 0) {
1113 if (tabularx && !column_info[c].varwidth)
1114 new_width = max(new_width, cellInfo(i).width);
1116 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1118 new_width = max(new_width, cellInfo(i).width);
1122 if (column_info[c].width != new_width) {
1123 column_info[c].width = new_width;
1127 // update col widths to fit merged cells
1128 for(col_type c = 0; c < ncols(); ++c)
1129 for(row_type r = 0; r < nrows(); ++r) {
1130 idx_type const i = cellIndex(r, c);
1131 int const span = columnSpan(i);
1132 if (span == 1 || c > cellColumn(i))
1136 for(col_type j = c; j < c + span ; ++j)
1137 old_width += column_info[j].width;
1139 if (cellInfo(i).width > old_width) {
1140 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1149 int Tabular::width() const
1152 for (col_type c = 0; c < ncols(); ++c)
1153 width += column_info[c].width;
1158 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1161 col_type const col = cellColumn(cell);
1162 // set alignment for the whole row if we are not in a multicolumn cell,
1163 // exclude possible multicolumn cells in the row
1164 if (!isMultiColumn(cell)) {
1165 for (row_type r = 0; r < nrows(); ++r) {
1166 // only if the column has no width the multirow inherits the
1167 // alignment of the column, otherwise it is left aligned
1168 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1169 && !isMultiColumn(cellIndex(r, col))) {
1170 cell_info[r][col].alignment = align;
1171 cell_info[r][col].inset->setContentAlignment(align);
1173 if ((isMultiRow(cellIndex(r, col)) && has_width)
1174 && !isMultiColumn(cellIndex(r, col))) {
1175 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1176 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1179 column_info[col].alignment = align;
1180 docstring & dpoint = column_info[col].decimal_point;
1181 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1182 dpoint = from_utf8(lyxrc.default_decimal_point);
1184 cellInfo(cell).alignment = align;
1185 cellInset(cell)->setContentAlignment(align);
1190 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1193 if (!isMultiColumn(cell) || onlycolumn)
1194 column_info[cellColumn(cell)].valignment = align;
1196 cellInfo(cell).valignment = align;
1203 * Allow line and paragraph breaks for fixed width multicol/multirow cells
1204 * or disallow them, merge cell paragraphs and reset layout to standard
1205 * for variable width multicol cells.
1207 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1208 bool const fixedWidth, bool const multicol,
1209 bool const multirow)
1211 inset->toggleFixedWidth(fixedWidth);
1212 if (!multirow && (fixedWidth || !multicol))
1215 // merge all paragraphs to one
1216 BufferParams const & bp = cur.bv().buffer().params();
1217 while (inset->paragraphs().size() > 1)
1218 mergeParagraph(bp, inset->paragraphs(), 0);
1220 // This is relevant for multirows
1225 ParagraphList::iterator pit = inset->paragraphs().begin();
1226 for (; pit != inset->paragraphs().end(); ++pit) {
1227 for (pos_type j = 0; j != pit->size(); ++j) {
1228 if (pit->isNewline(j))
1229 pit->eraseChar(j, bp.track_changes);
1235 // undo information has already been recorded
1236 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1237 bp.documentClass().plainLayoutName());
1244 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1245 Length const & width)
1247 col_type const c = cellColumn(cell);
1249 column_info[c].p_width = width;
1250 // reset the vertical alignment to top if the fixed width
1251 // is removed or zero because only fixed width columns can
1252 // have a vertical alignment
1253 if (column_info[c].p_width.zero())
1254 column_info[c].valignment = LYX_VALIGN_TOP;
1255 for (row_type r = 0; r < nrows(); ++r) {
1256 idx_type const cidx = cellIndex(r, c);
1257 // because of multicolumns
1258 toggleFixedWidth(cur, cellInset(cidx).get(),
1259 !getPWidth(cidx).zero(), isMultiColumn(cidx),
1261 if (isMultiRow(cidx))
1262 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1264 // cur can become invalid after paragraphs were merged
1269 bool Tabular::setFixedWidth(row_type r, col_type c)
1271 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1272 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1273 || (multicol && !cell_info[r][c].p_width.zero());
1274 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1279 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1280 Length const & width)
1282 if (!isMultiColumn(cell))
1285 cellInfo(cell).p_width = width;
1286 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1287 isMultiColumn(cell), isMultiRow(cell));
1288 // cur can become invalid after paragraphs were merged
1294 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1296 column_info[cellColumn(cell)].varwidth = varwidth;
1301 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1303 cellInfo(cell).mroffset = mroffset;
1308 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1309 Tabular::Feature what)
1311 if (what == SET_SPECIAL_MULTICOLUMN)
1312 cellInfo(cell).align_special = special;
1314 column_info[cellColumn(cell)].align_special = special;
1318 void Tabular::setTopLine(idx_type i, bool line)
1320 cellInfo(i).top_line = line;
1324 void Tabular::setBottomLine(idx_type i, bool line)
1326 cellInfo(i).bottom_line = line;
1330 void Tabular::setLeftLine(idx_type cell, bool line)
1332 cellInfo(cell).left_line = line;
1336 void Tabular::setRightLine(idx_type cell, bool line)
1338 cellInfo(cell).right_line = line;
1341 bool Tabular::rowTopLine(row_type r) const
1343 bool all_rows_set = true;
1344 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1345 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1346 return all_rows_set;
1350 bool Tabular::rowBottomLine(row_type r) const
1352 bool all_rows_set = true;
1353 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1354 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1355 return all_rows_set;
1359 bool Tabular::columnLeftLine(col_type c) const
1366 for (row_type r = 0; r < nrows(); ++r) {
1367 idx_type const i = cellIndex(r, c);
1368 if (c == cellColumn(i)) {
1370 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1371 if (cellInfo(i).left_line || right)
1375 return 2 * nrows_left >= total;
1379 bool Tabular::columnRightLine(col_type c) const
1384 int nrows_right = 0;
1386 for (row_type r = 0; r < nrows(); ++r) {
1387 idx_type i = cellIndex(r, c);
1388 if (c == cellColumn(i) + columnSpan(i) - 1) {
1390 bool left = (c + 1 < ncols()
1391 && cellInfo(cellIndex(r, c + 1)).left_line)
1392 || c + 1 == ncols();
1393 if (cellInfo(i).right_line && left)
1397 return 2 * nrows_right >= total;
1401 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1403 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1404 return cellInfo(cell).alignment;
1406 return column_info[cellColumn(cell)].alignment;
1411 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1413 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1414 return cellInfo(cell).valignment;
1415 return column_info[cellColumn(cell)].valignment;
1419 int Tabular::offsetVAlignment() const
1421 // for top-alignment the first horizontal table line must be exactly at
1422 // the position of the base line of the surrounding text line
1423 // for bottom alignment, the same is for the last table line
1424 int offset_valign = 0;
1425 switch (tabular_valignment) {
1426 case Tabular::LYX_VALIGN_BOTTOM:
1427 offset_valign = rowAscent(0) - height();
1429 case Tabular::LYX_VALIGN_MIDDLE:
1430 offset_valign = (- height()) / 2 + rowAscent(0);
1432 case Tabular::LYX_VALIGN_TOP:
1433 offset_valign = rowAscent(0);
1436 return offset_valign;
1440 Length const Tabular::getPWidth(idx_type cell) const
1442 if (isMultiColumn(cell))
1443 return cellInfo(cell).p_width;
1444 return column_info[cellColumn(cell)].p_width;
1448 Length const Tabular::getMROffset(idx_type cell) const
1450 return cellInfo(cell).mroffset;
1454 int Tabular::textHOffset(idx_type cell) const
1456 // the LaTeX Way :-(
1457 int x = WIDTH_OF_LINE;
1459 int const w = cellWidth(cell) - cellInfo(cell).width;
1461 switch (getAlignment(cell)) {
1462 case LYX_ALIGN_CENTER:
1465 case LYX_ALIGN_RIGHT:
1468 case LYX_ALIGN_DECIMAL: {
1469 // we center when no decimal point
1470 if (cellInfo(cell).decimal_width == 0) {
1474 col_type const c = cellColumn(cell);
1475 int max_dhoffset = 0;
1476 for(row_type r = 0; r < row_info.size() ; ++r) {
1477 idx_type const i = cellIndex(r, c);
1478 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1479 && cellInfo(i).decimal_width != 0)
1480 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1482 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1485 // LYX_ALIGN_LEFT: nothing :-)
1493 int Tabular::textVOffset(idx_type cell) const
1495 int voffset = cellInfo(cell).voffset;
1496 if (isMultiRow(cell)) {
1497 row_type const row = cellRow(cell);
1498 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1504 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1507 idx_type const numcells = numberOfCellsInRow(row);
1508 // we check against numcells to make sure we do not crash if all the
1509 // cells are multirow (bug #7535), but in that case our return value
1510 // is really invalid, i.e., it is NOT the first cell in the row. but
1511 // i do not know what to do here. (rgh)
1512 while (c < numcells - 1
1513 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1515 return cell_info[row][c].cellno;
1519 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1521 col_type c = ncols() - 1;
1522 // of course we check against 0 so we don't crash. but we have the same
1523 // problem as in the previous routine: if all the cells are part of a
1524 // multirow or part of a multi column, then our return value is invalid.
1526 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1527 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1529 return cell_info[row][c].cellno;
1533 Tabular::row_type Tabular::cellRow(idx_type cell) const
1535 if (cell >= numberofcells)
1539 return rowofcell[cell];
1543 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1545 if (cell >= numberofcells)
1549 return columnofcell[cell];
1553 void Tabular::write(ostream & os) const
1557 << write_attribute("version", 3)
1558 << write_attribute("rows", nrows())
1559 << write_attribute("columns", ncols())
1561 // global longtable options
1563 << write_attribute("rotate", rotate)
1564 << write_attribute("booktabs", use_booktabs)
1565 << write_attribute("islongtable", is_long_tabular)
1566 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1567 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1568 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1569 << write_attribute("headTopDL", endhead.topDL)
1570 << write_attribute("headBottomDL", endhead.bottomDL)
1571 << write_attribute("footTopDL", endfoot.topDL)
1572 << write_attribute("footBottomDL", endfoot.bottomDL)
1573 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1574 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1575 << write_attribute("lastFootEmpty", endlastfoot.empty);
1576 // longtables cannot be aligned vertically
1577 if (!is_long_tabular)
1578 os << write_attribute("tabularvalignment", tabular_valignment);
1579 os << write_attribute("tabularwidth", tabular_width);
1580 if (is_long_tabular)
1581 os << write_attribute("longtabularalignment", longtabular_alignment);
1583 for (col_type c = 0; c < ncols(); ++c) {
1585 << write_attribute("alignment", column_info[c].alignment);
1586 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1587 os << write_attribute("decimal_point", column_info[c].decimal_point);
1588 os << write_attribute("valignment", column_info[c].valignment)
1589 << write_attribute("width", column_info[c].p_width.asString())
1590 << write_attribute("varwidth", column_info[c].varwidth)
1591 << write_attribute("special", column_info[c].align_special)
1594 for (row_type r = 0; r < nrows(); ++r) {
1595 static const string def("default");
1597 if (row_info[r].top_space_default)
1598 os << write_attribute("topspace", def);
1600 os << write_attribute("topspace", row_info[r].top_space);
1601 if (row_info[r].bottom_space_default)
1602 os << write_attribute("bottomspace", def);
1604 os << write_attribute("bottomspace", row_info[r].bottom_space);
1605 if (row_info[r].interline_space_default)
1606 os << write_attribute("interlinespace", def);
1608 os << write_attribute("interlinespace", row_info[r].interline_space);
1609 os << write_attribute("endhead", row_info[r].endhead)
1610 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1611 << write_attribute("endfoot", row_info[r].endfoot)
1612 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1613 << write_attribute("newpage", row_info[r].newpage)
1614 << write_attribute("caption", row_info[r].caption)
1616 for (col_type c = 0; c < ncols(); ++c) {
1618 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1619 << write_attribute("multirow", cell_info[r][c].multirow)
1620 << write_attribute("mroffset", cell_info[r][c].mroffset)
1621 << write_attribute("alignment", cell_info[r][c].alignment)
1622 << write_attribute("valignment", cell_info[r][c].valignment)
1623 << write_attribute("topline", cell_info[r][c].top_line)
1624 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1625 << write_attribute("leftline", cell_info[r][c].left_line)
1626 << write_attribute("rightline", cell_info[r][c].right_line)
1627 << write_attribute("rotate", cell_info[r][c].rotate)
1628 << write_attribute("usebox", cell_info[r][c].usebox)
1629 << write_attribute("width", cell_info[r][c].p_width)
1630 << write_attribute("special", cell_info[r][c].align_special)
1632 os << "\\begin_inset ";
1633 cell_info[r][c].inset->write(os);
1634 os << "\n\\end_inset\n"
1636 // FIXME This can be removed again once the mystery
1637 // crash has been resolved.
1642 os << "</lyxtabular>\n";
1646 void Tabular::read(Lexer & lex)
1649 istream & is = lex.getStream();
1651 l_getline(is, line);
1652 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1653 LASSERT(false, return);
1657 if (!getTokenValue(line, "version", version))
1659 LATTEST(version >= 2);
1662 if (!getTokenValue(line, "rows", rows_arg))
1665 if (!getTokenValue(line, "columns", columns_arg))
1667 init(buffer_, rows_arg, columns_arg);
1668 l_getline(is, line);
1669 if (!prefixIs(line, "<features")) {
1670 lyxerr << "Wrong tabular format (expected <features ...> got"
1671 << line << ')' << endl;
1674 getTokenValue(line, "rotate", rotate);
1675 getTokenValue(line, "booktabs", use_booktabs);
1676 getTokenValue(line, "islongtable", is_long_tabular);
1677 getTokenValue(line, "tabularvalignment", tabular_valignment);
1678 getTokenValue(line, "tabularwidth", tabular_width);
1679 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1680 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1681 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1682 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1683 getTokenValue(line, "headTopDL", endhead.topDL);
1684 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1685 getTokenValue(line, "footTopDL", endfoot.topDL);
1686 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1687 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1688 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1689 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1691 for (col_type c = 0; c < ncols(); ++c) {
1693 if (!prefixIs(line,"<column")) {
1694 lyxerr << "Wrong tabular format (expected <column ...> got"
1695 << line << ')' << endl;
1698 getTokenValue(line, "alignment", column_info[c].alignment);
1699 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1700 getTokenValue(line, "valignment", column_info[c].valignment);
1701 getTokenValue(line, "width", column_info[c].p_width);
1702 getTokenValue(line, "special", column_info[c].align_special);
1703 getTokenValue(line, "varwidth", column_info[c].varwidth);
1706 for (row_type i = 0; i < nrows(); ++i) {
1707 l_getline(is, line);
1708 if (!prefixIs(line, "<row")) {
1709 lyxerr << "Wrong tabular format (expected <row ...> got"
1710 << line << ')' << endl;
1713 getTokenValue(line, "topspace", row_info[i].top_space,
1714 row_info[i].top_space_default);
1715 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1716 row_info[i].bottom_space_default);
1717 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1718 row_info[i].interline_space_default);
1719 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1720 getTokenValue(line, "endhead", row_info[i].endhead);
1721 getTokenValue(line, "endfoot", row_info[i].endfoot);
1722 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1723 getTokenValue(line, "newpage", row_info[i].newpage);
1724 getTokenValue(line, "caption", row_info[i].caption);
1725 for (col_type j = 0; j < ncols(); ++j) {
1726 l_getline(is, line);
1727 if (!prefixIs(line, "<cell")) {
1728 lyxerr << "Wrong tabular format (expected <cell ...> got"
1729 << line << ')' << endl;
1732 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1733 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1734 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1735 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1736 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1737 getTokenValue(line, "topline", cell_info[i][j].top_line);
1738 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1739 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1740 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1741 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1742 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1743 getTokenValue(line, "width", cell_info[i][j].p_width);
1745 getTokenValue(line, "special", cell_info[i][j].align_special);
1746 l_getline(is, line);
1747 if (prefixIs(line, "\\begin_inset")) {
1748 cell_info[i][j].inset->setBuffer(*buffer_);
1749 cell_info[i][j].inset->read(lex);
1750 l_getline(is, line);
1752 if (!prefixIs(line, "</cell>")) {
1753 lyxerr << "Wrong tabular format (expected </cell> got"
1754 << line << ')' << endl;
1758 l_getline(is, line);
1759 if (!prefixIs(line, "</row>")) {
1760 lyxerr << "Wrong tabular format (expected </row> got"
1761 << line << ')' << endl;
1765 while (!prefixIs(line, "</lyxtabular>")) {
1766 l_getline(is, line);
1772 bool Tabular::isMultiColumn(idx_type cell) const
1774 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1775 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1779 bool Tabular::hasMultiColumn(col_type c) const
1781 for (row_type r = 0; r < nrows(); ++r) {
1782 if (isMultiColumn(cellIndex(r, c)))
1789 bool Tabular::hasVarwidthColumn() const
1791 for (col_type c = 0; c < ncols(); ++c) {
1792 if (column_info[c].varwidth)
1799 bool Tabular::isVTypeColumn(col_type c) const
1801 for (row_type r = 0; r < nrows(); ++r) {
1802 idx_type idx = cellIndex(r, c);
1803 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1810 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1812 return cell_info[cellRow(cell)][cellColumn(cell)];
1816 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1818 return cell_info[cellRow(cell)][cellColumn(cell)];
1822 Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
1823 bool const right_border)
1825 idx_type const col = cellColumn(cell);
1826 idx_type const row = cellRow(cell);
1827 for (idx_type i = 0; i < number; ++i)
1828 unsetMultiRow(cellIndex(row, col + i));
1830 // unsetting of multirow may have invalidated cell index
1831 cell = cellIndex(row, col);
1832 CellData & cs = cellInfo(cell);
1833 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1834 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1835 cs.alignment = column_info[col].alignment;
1836 setRightLine(cell, right_border);
1837 // non-fixed width multicolumns cannot have multiple paragraphs
1838 if (getPWidth(cell).zero()) {
1839 toggleFixedWidth(cur, cellInset(cell).get(),
1840 !getPWidth(cell).zero(), isMultiColumn(cell),
1842 // cur can become invalid after paragraphs were merged
1846 idx_type lastcell = cellIndex(row, col + number - 1);
1847 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1848 CellData & cs1 = cellInfo(cell + i);
1849 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1850 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1858 bool Tabular::isMultiRow(idx_type cell) const
1860 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1861 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1864 bool Tabular::hasMultiRow(row_type r) const
1866 for (col_type c = 0; c < ncols(); ++c) {
1867 if (isMultiRow(cellIndex(r, c)))
1873 Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
1874 bool const bottom_border,
1875 LyXAlignment const halign)
1877 idx_type const col = cellColumn(cell);
1878 idx_type const row = cellRow(cell);
1879 for (idx_type i = 0; i < number; ++i)
1880 unsetMultiColumn(cellIndex(row + i, col));
1882 // unsetting of multirow may have invalidated cell index
1883 cell = cellIndex(row, col);
1884 CellData & cs = cellInfo(cell);
1885 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1886 cs.valignment = LYX_VALIGN_MIDDLE;
1887 // the horizontal alignment of multirow cells can only
1888 // be changed for the whole table row,
1889 // support changing this only for the multirow cell can be done via
1891 if (getPWidth(cell).zero())
1892 cs.alignment = halign;
1894 cs.alignment = LYX_ALIGN_LEFT;
1896 // Multirows cannot have multiple paragraphs
1897 if (getPWidth(cell).zero()) {
1898 toggleFixedWidth(cur, cellInset(cell).get(),
1899 !getPWidth(cell).zero(),
1900 isMultiColumn(cell), isMultiRow(cell));
1901 // cur can become invalid after paragraphs were merged
1905 // set the bottom line of the last selected cell
1906 setBottomLine(cell, bottom_border);
1908 for (idx_type i = 1; i < number; ++i) {
1909 CellData & cs1 = cell_info[row + i][col];
1910 cs1.multirow = CELL_PART_OF_MULTIROW;
1911 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1919 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1921 row_type const row = cellRow(cell);
1922 col_type const col = cellColumn(cell);
1924 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1931 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1933 col_type const column = cellColumn(cell);
1934 col_type row = cellRow(cell) + 1;
1935 while (row < nrows() && isPartOfMultiRow(row, column))
1938 return row - cellRow(cell);
1942 void Tabular::unsetMultiColumn(idx_type cell)
1944 if (!isMultiColumn(cell))
1947 row_type const row = cellRow(cell);
1948 col_type const col = cellColumn(cell);
1949 row_type const span = columnSpan(cell);
1950 for (col_type c = 0; c < span; ++c) {
1951 // in the table dialog the lines are set in every case
1952 // when unsetting a multicolumn this leads to an additional right
1953 // line for every cell that was part of the former multicolumn cell,
1954 // except if the cell is in the last column
1955 // therefore remove this line
1956 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1957 && (col + c) < (col + span - 1))
1958 cell_info[row][col + c].right_line = false;
1959 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1965 void Tabular::unsetMultiRow(idx_type cell)
1967 if (!isMultiRow(cell))
1970 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1971 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1972 row_type const row = cellRow(cell);
1973 col_type const col = cellColumn(cell);
1974 row_type const span = rowSpan(cell);
1975 for (row_type r = 0; r < span; ++r)
1976 cell_info[row + r][col].multirow = CELL_NORMAL;
1981 void Tabular::setRotateCell(idx_type cell, int value)
1983 cellInfo(cell).rotate = value;
1987 int Tabular::getRotateCell(idx_type cell) const
1989 return cellInfo(cell).rotate;
1993 bool Tabular::needRotating() const
1995 if (rotate && !is_long_tabular)
1997 for (row_type r = 0; r < nrows(); ++r)
1998 for (col_type c = 0; c < ncols(); ++c)
1999 if (cell_info[r][c].rotate != 0)
2005 bool Tabular::isLastCell(idx_type cell) const
2007 if (cell + 1 < numberofcells)
2013 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2015 if (cellRow(cell) == 0)
2018 col_type const col = cellColumn(cell);
2019 row_type r = cellRow(cell) - 1;
2020 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2023 return cell_info[r][col].cellno;
2027 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2029 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2030 if (nextrow < nrows())
2031 return cell_info[nextrow][cellColumn(cell)].cellno;
2036 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2038 LASSERT(column != npos && column < ncols(), column = 0);
2039 LASSERT(row != npos && row < nrows(), row = 0);
2040 return cell_info[row][column].cellno;
2044 void Tabular::setUsebox(idx_type cell, BoxType type)
2046 cellInfo(cell).usebox = type;
2050 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2052 if (getRotateCell(cell) == 0
2053 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2054 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2056 if (cellInfo(cell).usebox > 1)
2057 return cellInfo(cell).usebox;
2058 return useBox(cell);
2063 // This are functions used for the longtable support
2065 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2071 row_info[row].endfirsthead = flag;
2075 row_info[row].endhead = flag;
2080 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2083 hd.set = haveLTHead();
2084 return row_info[row].endhead;
2088 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2091 hd.set = haveLTFirstHead();
2092 return row_info[row].endfirsthead;
2096 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2102 row_info[row].endlastfoot = flag;
2106 row_info[row].endfoot = flag;
2111 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2114 fd.set = haveLTFoot();
2115 return row_info[row].endfoot;
2119 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2122 fd.set = haveLTLastFoot();
2123 return row_info[row].endlastfoot;
2127 void Tabular::setLTNewPage(row_type row, bool what)
2129 row_info[row].newpage = what;
2133 bool Tabular::getLTNewPage(row_type row) const
2135 return row_info[row].newpage;
2139 bool Tabular::haveLTHead(bool withcaptions) const
2141 if (!is_long_tabular)
2143 for (row_type i = 0; i < nrows(); ++i)
2144 if (row_info[i].endhead &&
2145 (withcaptions || !row_info[i].caption))
2151 bool Tabular::haveLTFirstHead(bool withcaptions) const
2153 if (!is_long_tabular || endfirsthead.empty)
2155 for (row_type r = 0; r < nrows(); ++r)
2156 if (row_info[r].endfirsthead &&
2157 (withcaptions || !row_info[r].caption))
2163 bool Tabular::haveLTFoot(bool withcaptions) const
2165 if (!is_long_tabular)
2167 for (row_type r = 0; r < nrows(); ++r)
2168 if (row_info[r].endfoot &&
2169 (withcaptions || !row_info[r].caption))
2175 bool Tabular::haveLTLastFoot(bool withcaptions) const
2177 if (!is_long_tabular || endlastfoot.empty)
2179 for (row_type r = 0; r < nrows(); ++r)
2180 if (row_info[r].endlastfoot &&
2181 (withcaptions || !row_info[r].caption))
2187 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2189 idx_type i = getFirstCellInRow(row);
2191 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2192 setTopLine(i, false);
2193 setBottomLine(i, false);
2194 setLeftLine(i, false);
2195 setRightLine(i, false);
2196 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2197 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2198 setLTHead(row, true, endfirsthead, true);
2199 row_info[row].endfirsthead = true;
2202 unsetMultiColumn(i);
2203 // When unsetting a caption row, also all existing
2204 // captions in this row must be dissolved.
2206 row_info[row].caption = what;
2211 bool Tabular::ltCaption(row_type row) const
2213 return row_info[row].caption;
2217 bool Tabular::haveLTCaption(CaptionType captiontype) const
2219 if (!is_long_tabular)
2221 for (row_type r = 0; r < nrows(); ++r) {
2222 if (row_info[r].caption) {
2223 switch (captiontype) {
2224 case CAPTION_FIRSTHEAD:
2225 if (row_info[r].endfirsthead)
2229 if (row_info[r].endhead)
2233 if (row_info[r].endfoot)
2236 case CAPTION_LASTFOOT:
2237 if (row_info[r].endlastfoot)
2249 // end longtable support functions
2251 void Tabular::setRowAscent(row_type row, int height)
2253 if (row >= nrows() || row_info[row].ascent == height)
2255 row_info[row].ascent = height;
2259 void Tabular::setRowDescent(row_type row, int height)
2261 if (row >= nrows() || row_info[row].descent == height)
2263 row_info[row].descent = height;
2267 int Tabular::rowAscent(row_type row) const
2269 LASSERT(row < nrows(), row = 0);
2270 return row_info[row].ascent;
2274 int Tabular::rowDescent(row_type row) const
2276 LASSERT(row < nrows(), row = 0);
2277 return row_info[row].descent;
2281 int Tabular::height() const
2284 for (row_type row = 0; row < nrows(); ++row)
2285 height += rowAscent(row) + rowDescent(row) +
2291 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2293 LASSERT(row < nrows(), return false);
2294 LASSERT(column < ncols(), return false);
2295 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2299 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2301 LASSERT(row < nrows(), return false);
2302 LASSERT(column < ncols(), return false);
2303 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2307 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang,
2308 list<col_type> columns) const
2310 // we only output complete row lines and the 1st row here, the rest
2311 // is done in Tabular::TeXBottomHLine(...)
2313 // get for each column the topline (if any)
2314 map<col_type, bool> topline;
2316 for (auto const & c : columns) {
2317 topline[c] = topLine(cellIndex(row, c));
2318 // If cell is part of a multirow and not the first cell of the
2319 // multirow, no line must be drawn.
2321 if (isMultiRow(cellIndex(row, c))
2322 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2324 if (topline.find(c) != topline.end() && topline.find(c)->second)
2328 // do nothing if empty first row, or incomplete row line after
2329 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2332 // only output complete row lines and the 1st row's clines
2333 if (nset == ncols()) {
2335 os << (row == 0 ? "\\toprule " : "\\midrule ");
2339 } else if (row == 0) {
2340 for (auto & c : columns) {
2341 if (topline.find(c)->second) {
2342 col_type offset = 0;
2343 for (col_type j = 0 ; j < c; ++j)
2344 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2347 //babel makes the "-" character an active one, so we have to suppress this here
2348 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2349 if (lang == "slovak" || lang == "czech")
2350 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2351 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2353 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2355 col_type cstart = c;
2356 for ( ; c < ncols() && topline.find(c)->second; ++c) {}
2358 for (col_type j = cstart ; j < c ; ++j)
2359 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2362 os << c + offset << "} ";
2370 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang,
2371 list<col_type> columns) const
2373 // we output bottomlines of row r and the toplines of row r+1
2374 // if the latter do not span the whole tabular
2376 // get the bottomlines of row r, and toplines in next row
2377 bool lastrow = row == nrows() - 1;
2378 map<col_type, bool> bottomline, topline;
2379 bool nextrowset = true;
2380 for (auto const & c : columns) {
2381 bottomline[c] = bottomLine(cellIndex(row, c));
2382 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2383 // If cell is part of a multirow and not the last cell of the
2384 // multirow, no line must be drawn.
2386 if (isMultiRow(cellIndex(row, c))
2387 && isMultiRow(cellIndex(row + 1, c))
2388 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2389 bottomline[c] = false;
2392 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2395 // combine this row's bottom lines and next row's toplines if necessary
2397 for (auto const & c : columns) {
2399 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2400 if (bottomline.find(c)->second)
2404 // do nothing if empty, OR incomplete row line with a topline in next row
2405 if (nset == 0 || (nextrowset && nset != ncols()))
2408 if (nset == ncols()) {
2410 os << (lastrow ? "\\bottomrule" : "\\midrule");
2414 for (auto & c : columns) {
2415 if (bottomline.find(c)->second) {
2416 col_type offset = 0;
2417 for (col_type j = 0 ; j < c; ++j)
2418 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2421 //babel makes the "-" character an active one, so we have to suppress this here
2422 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2423 if (lang == "slovak" || lang == "czech")
2424 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2425 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2427 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2429 col_type cstart = c;
2430 for ( ; c < ncols() && bottomline.find(c)->second; ++c) {}
2432 for (col_type j = cstart ; j < c ; ++j)
2433 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2436 os << c + offset << "} ";
2444 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2445 bool & ismulticol, bool & ismultirow,
2446 bool const bidi) const
2448 row_type const r = cellRow(cell);
2449 if (is_long_tabular && row_info[r].caption)
2452 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2453 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2454 // figure out how to set the lines
2455 // we always set double lines to the right of the cell
2456 // or left in bidi RTL, respectively.
2457 col_type const c = cellColumn(cell);
2458 col_type const nextcol = c + columnSpan(cell);
2459 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2460 bool colright = columnRightLine(c);
2461 bool colleft = columnLeftLine(c);
2462 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2463 bool nextcellleft = nextcol < ncols()
2464 && leftLine(cellIndex(r, nextcol));
2465 bool coldouble = colright && nextcolleft;
2466 bool celldouble = rightLine(cell) && nextcellleft;
2468 ismulticol = (isMultiColumn(cell)
2469 || (c == 0 && colleft != leftLine(cell))
2470 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2471 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2472 || (coldouble != celldouble))
2475 // we center in multicol when no decimal point
2477 docstring const align_d = column_info[c].decimal_point;
2478 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2479 bool const nosep = !dit;
2480 ismulticol |= nosep;
2481 celldouble &= nosep;
2484 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2485 int latexcolspan = columnSpan(cell);
2486 for (col_type col = c; col < c + columnSpan(cell); ++col)
2487 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2491 os << "\\multicolumn{" << latexcolspan << "}{";
2492 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2493 || (!bidi && c == 0 && leftLine(cell))))
2495 if (bidi && celldouble)
2496 // add extra vertical line if we want a double one
2498 if (!cellInfo(cell).align_special.empty()) {
2499 os << cellInfo(cell).align_special;
2501 if (!getPWidth(cell).zero()) {
2503 case LYX_ALIGN_LEFT:
2504 os << ">{\\raggedright}";
2506 case LYX_ALIGN_RIGHT:
2507 os << ">{\\raggedleft}";
2509 case LYX_ALIGN_CENTER:
2510 os << ">{\\centering}";
2516 case LYX_VALIGN_TOP:
2519 case LYX_VALIGN_MIDDLE:
2522 case LYX_VALIGN_BOTTOM:
2527 << from_ascii(getPWidth(cell).asLatexString())
2531 case LYX_ALIGN_LEFT:
2534 case LYX_ALIGN_RIGHT:
2541 } // end if else !getPWidth
2542 } // end if else !cellinfo_of_cell
2543 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2545 if (!bidi && celldouble)
2546 // add extra vertical line if we want a double one
2549 } // end if ismulticol
2551 // we only need code for the first multirow cell
2552 ismultirow = isMultiRow(cell);
2554 os << "\\multirow{" << rowSpan(cell) << "}{";
2555 if (!getPWidth(cell).zero())
2556 os << from_ascii(getPWidth(cell).asLatexString());
2558 // we need to set a default value
2561 if (!getMROffset(cell).zero())
2562 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2564 } // end if ismultirow
2566 if (getRotateCell(cell) != 0)
2567 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2569 if (getUsebox(cell) == BOX_PARBOX) {
2572 case LYX_VALIGN_TOP:
2575 case LYX_VALIGN_MIDDLE:
2578 case LYX_VALIGN_BOTTOM:
2582 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2584 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2585 os << "\\begin{minipage}[";
2587 case LYX_VALIGN_TOP:
2590 case LYX_VALIGN_MIDDLE:
2593 case LYX_VALIGN_BOTTOM:
2597 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2599 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2600 os << "\\begin{varwidth}[";
2602 case LYX_VALIGN_TOP:
2605 case LYX_VALIGN_MIDDLE:
2608 case LYX_VALIGN_BOTTOM:
2612 os << "]{\\linewidth}\n";
2617 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2618 bool ismulticol, bool ismultirow) const
2620 row_type const r = cellRow(cell);
2621 if (is_long_tabular && row_info[r].caption)
2625 if (getUsebox(cell) == BOX_PARBOX)
2627 else if (getUsebox(cell) == BOX_MINIPAGE)
2628 os << breakln << "\\end{minipage}";
2629 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2630 os << breakln << "\\end{varwidth}";
2631 if (getRotateCell(cell) != 0)
2632 os << breakln << "\\end{turn}";
2640 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2641 OutputParams const & runparams,
2642 list<col_type> columns) const
2644 if (!is_long_tabular)
2648 // output caption which is in no header or footer
2649 if (haveLTCaption()) {
2650 for (row_type r = 0; r < nrows(); ++r) {
2651 if (row_info[r].caption &&
2652 !row_info[r].endfirsthead && !row_info[r].endhead &&
2653 !row_info[r].endfoot && !row_info[r].endlastfoot)
2654 TeXRow(os, r, runparams, columns);
2657 // output first header info
2658 if (haveLTFirstHead()) {
2659 if (endfirsthead.topDL)
2661 for (row_type r = 0; r < nrows(); ++r) {
2662 if (row_info[r].endfirsthead)
2663 TeXRow(os, r, runparams, columns);
2665 if (endfirsthead.bottomDL)
2667 os << "\\endfirsthead\n";
2669 // output header info
2671 if (endfirsthead.empty && !haveLTFirstHead())
2672 os << "\\endfirsthead\n";
2675 for (row_type r = 0; r < nrows(); ++r) {
2676 if (row_info[r].endhead)
2677 TeXRow(os, r, runparams, columns);
2679 if (endhead.bottomDL)
2681 os << "\\endhead\n";
2683 // output footer info
2687 for (row_type r = 0; r < nrows(); ++r) {
2688 if (row_info[r].endfoot)
2689 TeXRow(os, r, runparams, columns);
2691 if (endfoot.bottomDL)
2693 os << "\\endfoot\n";
2694 if (endlastfoot.empty && !haveLTLastFoot())
2695 os << "\\endlastfoot\n";
2697 // output lastfooter info
2698 if (haveLTLastFoot()) {
2699 if (endlastfoot.topDL)
2701 for (row_type r = 0; r < nrows(); ++r) {
2702 if (row_info[r].endlastfoot)
2703 TeXRow(os, r, runparams, columns);
2705 if (endlastfoot.bottomDL)
2707 os << "\\endlastfoot\n";
2712 bool Tabular::isValidRow(row_type row) const
2714 if (!is_long_tabular)
2716 return !row_info[row].endhead && !row_info[row].endfirsthead
2717 && !row_info[row].endfoot && !row_info[row].endlastfoot
2718 && !row_info[row].caption;
2722 void Tabular::TeXRow(otexstream & os, row_type row,
2723 OutputParams const & runparams,
2724 list<col_type> columns) const
2726 idx_type cell = cellIndex(row, 0);
2727 InsetTableCell const * cinset = cellInset(cell);
2728 Paragraph const & cpar = cinset->paragraphs().front();
2729 string const clang = cpar.getParLanguage(buffer().params())->lang();
2731 //output the top line
2732 TeXTopHLine(os, row, clang, columns);
2734 if (row_info[row].top_space_default) {
2736 os << "\\addlinespace\n";
2738 os << "\\noalign{\\vskip\\doublerulesep}\n";
2739 } else if(!row_info[row].top_space.zero()) {
2741 os << "\\addlinespace["
2742 << from_ascii(row_info[row].top_space.asLatexString())
2745 os << "\\noalign{\\vskip"
2746 << from_ascii(row_info[row].top_space.asLatexString())
2750 bool ismulticol = false;
2751 bool ismultirow = false;
2753 // The bidi package (loaded by polyglossia) reverses RTL table columns
2754 bool const bidi_rtl =
2755 runparams.local_font->isRightToLeft()
2756 && runparams.use_polyglossia;
2758 bidi_rtl ? getFirstCellInRow(row) : getLastCellInRow(row);
2760 for (auto const & c : columns) {
2761 if (isPartOfMultiColumn(row, c))
2764 cell = cellIndex(row, c);
2766 if (isPartOfMultiRow(row, c)
2767 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2768 if (cell != lastcell)
2773 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
2774 InsetTableCell const * inset = cellInset(cell);
2776 Paragraph const & par = inset->paragraphs().front();
2778 os.texrow().forceStart(par.id(), 0);
2780 bool rtl = par.isRTL(buffer().params())
2782 && getPWidth(cell).zero()
2783 && !runparams.use_polyglossia;
2787 par.getParLanguage(buffer().params())->lang();
2788 if (lang == "farsi")
2790 else if (lang == "arabic_arabi")
2792 // currently, remaining RTL languages are
2793 // arabic_arabtex and hebrew
2797 // pass to the OutputParams that we are in a cell and
2798 // which alignment we have set.
2799 // InsetNewline needs this context information.
2800 OutputParams newrp(runparams);
2801 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2802 ? OutputParams::PLAIN
2803 : OutputParams::ALIGNED;
2805 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2806 // copy cell and split in 2
2807 InsetTableCell head = InsetTableCell(*cellInset(cell));
2808 head.setBuffer(const_cast<Buffer &>(buffer()));
2809 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2811 dit.push_back(CursorSlice(head));
2812 head.setMacrocontextPositionRecursive(dit);
2813 bool hassep = false;
2814 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2816 tail.setBuffer(head.buffer());
2818 dit.push_back(CursorSlice(tail));
2819 tail.setMacrocontextPositionRecursive(dit);
2823 tail.latex(os, newrp);
2826 head.latex(os, newrp);
2828 head.latex(os, newrp);
2831 tail.latex(os, newrp);
2834 } else if (ltCaption(row)) {
2835 // Inside longtable caption rows, we must only output the caption inset
2836 // with its content and omit anything outside of that (see #10791)
2837 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
2838 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
2839 for (; it != i_end; ++it) {
2840 if (it->lyxCode() != CAPTION_CODE)
2842 it->latex(os, runparams);
2845 } else if (!isPartOfMultiRow(row, c)) {
2846 if (!runparams.nice)
2847 os.texrow().start(par.id(), 0);
2848 inset->latex(os, newrp);
2851 runparams.encoding = newrp.encoding;
2855 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2856 if (cell != lastcell) { // not last cell in row
2863 os << "\\tabularnewline";
2864 if (row_info[row].bottom_space_default) {
2866 os << "\\addlinespace";
2868 os << "[\\doublerulesep]";
2869 } else if (!row_info[row].bottom_space.zero()) {
2871 os << "\\addlinespace";
2873 << from_ascii(row_info[row].bottom_space.asLatexString())
2878 //output the bottom line
2879 TeXBottomHLine(os, row, clang, columns);
2881 if (row_info[row].interline_space_default) {
2883 os << "\\addlinespace\n";
2885 os << "\\noalign{\\vskip\\doublerulesep}\n";
2886 } else if (!row_info[row].interline_space.zero()) {
2888 os << "\\addlinespace["
2889 << from_ascii(row_info[row].interline_space.asLatexString())
2892 os << "\\noalign{\\vskip"
2893 << from_ascii(row_info[row].interline_space.asLatexString())
2899 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2901 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
2902 && !hasVarwidthColumn();
2903 bool const is_xltabular = is_long_tabular
2904 && (hasVarwidthColumn() || !tabular_width.zero());
2905 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
2907 //+---------------------------------------------------------------------
2908 //+ first the opening preamble +
2909 //+---------------------------------------------------------------------
2912 if (!TexRow::isNone(pos))
2913 os.texrow().start(pos);
2916 if (is_long_tabular)
2917 os << "\\begin{landscape}\n";
2919 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2922 if (is_long_tabular) {
2924 os << "\\begin{xltabular}";
2926 os << "\\begin{longtable}";
2927 switch (longtabular_alignment) {
2928 case LYX_LONGTABULAR_ALIGN_LEFT:
2931 case LYX_LONGTABULAR_ALIGN_CENTER:
2934 case LYX_LONGTABULAR_ALIGN_RIGHT:
2939 if (tabular_width.zero())
2940 os << "{" << from_ascii("\\columnwidth") << "}";
2942 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
2945 if (is_tabular_star)
2946 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2947 else if (hasVarwidthColumn()) {
2948 os << "\\begin{tabularx}{";
2949 if (tabular_width.zero())
2950 os << from_ascii("\\columnwidth") << "}";
2952 os << from_ascii(tabular_width.asLatexString()) << "}";
2954 os << "\\begin{tabular}";
2955 switch (tabular_valignment) {
2956 case LYX_VALIGN_TOP:
2959 case LYX_VALIGN_MIDDLE:
2961 case LYX_VALIGN_BOTTOM:
2969 if (is_tabular_star)
2970 os << "@{\\extracolsep{\\fill}}";
2972 // The bidi package (loaded by polyglossia) swaps the column
2973 // order for RTL (#9686). Thus we use this list.
2974 bool const bidi_rtl =
2975 runparams.local_font->isRightToLeft()
2976 && runparams.use_polyglossia;
2977 list<col_type> columns;
2978 for (col_type cl = 0; cl < ncols(); ++cl) {
2980 columns.push_front(cl);
2982 columns.push_back(cl);
2985 for (auto const & c : columns) {
2986 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
2988 if (!column_info[c].align_special.empty()) {
2989 os << column_info[c].align_special;
2991 if (!column_info[c].p_width.zero()) {
2992 bool decimal = false;
2993 switch (column_info[c].alignment) {
2994 case LYX_ALIGN_LEFT:
2995 os << ">{\\raggedright}";
2997 case LYX_ALIGN_RIGHT:
2998 os << ">{\\raggedleft}";
3000 case LYX_ALIGN_CENTER:
3001 os << ">{\\centering}";
3003 case LYX_ALIGN_NONE:
3004 case LYX_ALIGN_BLOCK:
3005 case LYX_ALIGN_LAYOUT:
3006 case LYX_ALIGN_SPECIAL:
3008 case LYX_ALIGN_DECIMAL: {
3010 os << ">{\\raggedright}";
3012 os << ">{\\raggedleft}";
3019 switch (column_info[c].valignment) {
3020 case LYX_VALIGN_TOP:
3021 // this is the default
3023 case LYX_VALIGN_MIDDLE:
3026 case LYX_VALIGN_BOTTOM:
3032 // Fixed-width cells with alignment at decimal separator
3033 // are output as two cells of half the width with the decimal
3034 // separator as column sep. This effectively puts the content
3035 // centered, which differs from the normal decimal sep alignment
3036 // and is not ideal, but we cannot do better ATM (see #9568).
3037 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3039 docstring const halffixedwith =
3040 from_ascii(Length(column_info[c].p_width.value() / 2,
3041 column_info[c].p_width.unit()).asLatexString());
3045 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3052 << from_ascii(column_info[c].p_width.asLatexString())
3054 } else if (column_info[c].varwidth) {
3055 switch (column_info[c].alignment) {
3056 case LYX_ALIGN_LEFT:
3057 os << ">{\\raggedright\\arraybackslash}";
3059 case LYX_ALIGN_RIGHT:
3060 os << ">{\\raggedleft\\arraybackslash}";
3062 case LYX_ALIGN_CENTER:
3063 os << ">{\\centering\\arraybackslash}";
3065 case LYX_ALIGN_NONE:
3066 case LYX_ALIGN_BLOCK:
3067 case LYX_ALIGN_LAYOUT:
3068 case LYX_ALIGN_SPECIAL:
3069 case LYX_ALIGN_DECIMAL:
3073 } else if (isVTypeColumn(c)) {
3074 switch (column_info[c].alignment) {
3075 case LYX_ALIGN_LEFT:
3076 os << ">{\\raggedright}";
3078 case LYX_ALIGN_RIGHT:
3079 os << ">{\\raggedleft}";
3081 case LYX_ALIGN_CENTER:
3082 os << ">{\\centering}";
3084 case LYX_ALIGN_NONE:
3085 case LYX_ALIGN_BLOCK:
3086 case LYX_ALIGN_LAYOUT:
3087 case LYX_ALIGN_SPECIAL:
3088 case LYX_ALIGN_DECIMAL:
3091 os << "V{\\linewidth}";
3093 switch (column_info[c].alignment) {
3094 case LYX_ALIGN_LEFT:
3097 case LYX_ALIGN_RIGHT:
3100 case LYX_ALIGN_DECIMAL:
3101 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3107 } // end if else !column_info[i].p_width
3108 } // end if else !column_info[i].align_special
3109 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3114 TeXLongtableHeaderFooter(os, runparams, columns);
3116 //+---------------------------------------------------------------------
3117 //+ the single row and columns (cells) +
3118 //+---------------------------------------------------------------------
3120 for (row_type r = 0; r < nrows(); ++r) {
3121 if (isValidRow(r)) {
3122 TeXRow(os, r, runparams, columns);
3123 if (is_long_tabular && row_info[r].newpage)
3124 os << "\\newpage\n";
3128 //+---------------------------------------------------------------------
3129 //+ the closing of the tabular +
3130 //+---------------------------------------------------------------------
3132 if (is_long_tabular) {
3134 os << "\\end{xltabular}";
3136 os << "\\end{longtable}";
3138 if (is_tabular_star)
3139 os << "\\end{tabular*}";
3140 else if (hasVarwidthColumn())
3141 os << "\\end{tabularx}";
3143 os << "\\end{tabular}";
3147 if (is_long_tabular)
3148 os << breakln << "\\end{landscape}";
3150 os << breakln << "\\end{turn}";
3153 if (!TexRow::isNone(pos))
3154 os.texrow().start(pos);
3158 int Tabular::docbookRow(odocstream & os, row_type row,
3159 OutputParams const & runparams) const
3162 idx_type cell = getFirstCellInRow(row);
3165 for (col_type c = 0; c < ncols(); ++c) {
3166 if (isPartOfMultiColumn(row, c))
3169 os << "<entry align=\"";
3170 switch (getAlignment(cell)) {
3171 case LYX_ALIGN_LEFT:
3174 case LYX_ALIGN_RIGHT:
3182 os << "\" valign=\"";
3183 switch (getVAlignment(cell)) {
3184 case LYX_VALIGN_TOP:
3187 case LYX_VALIGN_BOTTOM:
3190 case LYX_VALIGN_MIDDLE:
3195 if (isMultiColumn(cell)) {
3196 os << " namest=\"col" << c << "\" ";
3197 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3201 ret += cellInset(cell)->docbook(os, runparams);
3210 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3214 //+---------------------------------------------------------------------
3215 //+ first the opening preamble +
3216 //+---------------------------------------------------------------------
3218 os << "<tgroup cols=\"" << ncols()
3219 << "\" colsep=\"1\" rowsep=\"1\">\n";
3221 for (col_type c = 0; c < ncols(); ++c) {
3222 os << "<colspec colname=\"col" << c << "\" align=\"";
3223 switch (column_info[c].alignment) {
3224 case LYX_ALIGN_LEFT:
3227 case LYX_ALIGN_RIGHT:
3235 if (runparams.flavor == OutputParams::XML)
3241 //+---------------------------------------------------------------------
3242 //+ Long Tabular case +
3243 //+---------------------------------------------------------------------
3245 // output caption info
3246 // The caption flag wins over head/foot
3247 if (haveLTCaption()) {
3248 os << "<caption>\n";
3250 for (row_type r = 0; r < nrows(); ++r) {
3251 if (row_info[r].caption) {
3252 ret += docbookRow(os, r, runparams);
3255 os << "</caption>\n";
3258 // output header info
3259 if (haveLTHead(false) || haveLTFirstHead(false)) {
3262 for (row_type r = 0; r < nrows(); ++r) {
3263 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3264 !row_info[r].caption) {
3265 ret += docbookRow(os, r, runparams);
3271 // output footer info
3272 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3275 for (row_type r = 0; r < nrows(); ++r) {
3276 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3277 !row_info[r].caption) {
3278 ret += docbookRow(os, r, runparams);
3285 //+---------------------------------------------------------------------
3286 //+ the single row and columns (cells) +
3287 //+---------------------------------------------------------------------
3291 for (row_type r = 0; r < nrows(); ++r) {
3292 if (isValidRow(r)) {
3293 ret += docbookRow(os, r, runparams);
3298 //+---------------------------------------------------------------------
3299 //+ the closing of the tabular +
3300 //+---------------------------------------------------------------------
3309 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3310 OutputParams const & runparams, bool header) const
3313 string const celltag = header ? "th" : "td";
3314 idx_type cell = getFirstCellInRow(row);
3316 xs << html::StartTag("tr");
3317 for (col_type c = 0; c < ncols(); ++c) {
3318 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3323 Length const cwidth = column_info[c].p_width;
3324 if (!cwidth.zero()) {
3325 string const hwidth = cwidth.asHTMLString();
3326 attr << "style =\"width: " << hwidth << ";\" ";
3330 switch (getAlignment(cell)) {
3331 case LYX_ALIGN_LEFT:
3334 case LYX_ALIGN_RIGHT:
3342 attr << " valign='";
3343 switch (getVAlignment(cell)) {
3344 case LYX_VALIGN_TOP:
3347 case LYX_VALIGN_BOTTOM:
3350 case LYX_VALIGN_MIDDLE:
3355 if (isMultiColumn(cell))
3356 attr << " colspan='" << columnSpan(cell) << "'";
3357 else if (isMultiRow(cell))
3358 attr << " rowspan='" << rowSpan(cell) << "'";
3360 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3361 ret += cellInset(cell)->xhtml(xs, runparams);
3362 xs << html::EndTag(celltag) << html::CR();
3365 xs << html::EndTag("tr");
3370 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3374 if (is_long_tabular) {
3375 // we'll wrap it in a div, so as to deal with alignment
3377 switch (longtabular_alignment) {
3378 case LYX_LONGTABULAR_ALIGN_LEFT:
3381 case LYX_LONGTABULAR_ALIGN_CENTER:
3384 case LYX_LONGTABULAR_ALIGN_RIGHT:
3388 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3390 // The caption flag wins over head/foot
3391 if (haveLTCaption()) {
3392 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3394 for (row_type r = 0; r < nrows(); ++r)
3395 if (row_info[r].caption)
3396 ret += xhtmlRow(xs, r, runparams);
3397 xs << html::EndTag("div") << html::CR();
3401 xs << html::StartTag("table") << html::CR();
3403 // output header info
3404 bool const havefirsthead = haveLTFirstHead(false);
3405 // if we have a first head, then we are going to ignore the
3406 // headers for the additional pages, since there aren't any
3407 // in XHTML. this test accomplishes that.
3408 bool const havehead = !havefirsthead && haveLTHead(false);
3409 if (havehead || havefirsthead) {
3410 xs << html::StartTag("thead") << html::CR();
3411 for (row_type r = 0; r < nrows(); ++r) {
3412 if (((havefirsthead && row_info[r].endfirsthead) ||
3413 (havehead && row_info[r].endhead)) &&
3414 !row_info[r].caption) {
3415 ret += xhtmlRow(xs, r, runparams, true);
3418 xs << html::EndTag("thead") << html::CR();
3420 // output footer info
3421 bool const havelastfoot = haveLTLastFoot(false);
3423 bool const havefoot = !havelastfoot && haveLTFoot(false);
3424 if (havefoot || havelastfoot) {
3425 xs << html::StartTag("tfoot") << html::CR();
3426 for (row_type r = 0; r < nrows(); ++r) {
3427 if (((havelastfoot && row_info[r].endlastfoot) ||
3428 (havefoot && row_info[r].endfoot)) &&
3429 !row_info[r].caption) {
3430 ret += xhtmlRow(xs, r, runparams);
3433 xs << html::EndTag("tfoot") << html::CR();
3436 xs << html::StartTag("tbody") << html::CR();
3437 for (row_type r = 0; r < nrows(); ++r) {
3438 if (isValidRow(r)) {
3439 ret += xhtmlRow(xs, r, runparams);
3442 xs << html::EndTag("tbody")
3444 << html::EndTag("table")
3446 if (is_long_tabular)
3447 xs << html::EndTag("div") << html::CR();
3452 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3453 vector<unsigned int> const & clen) const
3455 idx_type const fcell = getFirstCellInRow(row);
3456 idx_type const n = numberOfCellsInRow(row) + fcell;
3459 for (idx_type i = fcell; i < n; ++i) {
3469 for (idx_type i = fcell; i < n; ++i) {
3480 col_type column = cellColumn(i);
3481 int len = clen[column];
3482 while (column < ncols() - 1
3483 && isPartOfMultiColumn(row, ++column))
3484 len += clen[column] + 4;
3485 os << docstring(len, ch);
3500 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3501 vector<unsigned int> const & clen) const
3503 idx_type const fcell = getFirstCellInRow(row);
3504 idx_type const n = numberOfCellsInRow(row) + fcell;
3507 for (idx_type i = fcell; i < n; ++i) {
3508 if (bottomLine(i)) {
3517 for (idx_type i = fcell; i < n; ++i) {
3518 if (bottomLine(i)) {
3528 col_type column = cellColumn(i);
3529 int len = clen[column];
3530 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3531 len += clen[column] + 4;
3532 os << docstring(len, ch);
3533 if (bottomLine(i)) {
3547 void Tabular::plaintextPrintCell(odocstringstream & os,
3548 OutputParams const & runparams,
3549 idx_type cell, row_type row, col_type column,
3550 vector<unsigned int> const & clen,
3551 bool onlydata, size_t max_length) const
3553 odocstringstream sstr;
3554 cellInset(cell)->plaintext(sstr, runparams, max_length);
3566 unsigned int len1 = sstr.str().length();
3567 unsigned int len2 = clen[column];
3568 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3569 len2 += clen[column] + 4;
3572 switch (getAlignment(cell)) {
3574 case LYX_ALIGN_LEFT:
3577 case LYX_ALIGN_RIGHT:
3581 case LYX_ALIGN_CENTER:
3587 os << docstring(len1, ' ') << sstr.str()
3588 << docstring(len2, ' ');
3590 if (rightLine(cell))
3597 void Tabular::plaintext(odocstringstream & os,
3598 OutputParams const & runparams, int const depth,
3599 bool onlydata, char_type delim, size_t max_length) const
3601 // first calculate the width of the single columns
3602 vector<unsigned int> clen(ncols());
3605 // first all non multicolumn cells!
3606 for (col_type c = 0; c < ncols(); ++c) {
3608 for (row_type r = 0; r < nrows(); ++r) {
3609 idx_type cell = cellIndex(r, c);
3610 if (isMultiColumn(cell))
3612 odocstringstream sstr;
3613 cellInset(cell)->plaintext(sstr, runparams, max_length);
3614 if (clen[c] < sstr.str().length())
3615 clen[c] = sstr.str().length();
3618 // then all multicolumn cells!
3619 for (col_type c = 0; c < ncols(); ++c) {
3620 for (row_type r = 0; r < nrows(); ++r) {
3621 idx_type cell = cellIndex(r, c);
3622 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3624 odocstringstream sstr;
3625 cellInset(cell)->plaintext(sstr, runparams, max_length);
3626 int len = int(sstr.str().length());
3627 idx_type const n = columnSpan(cell);
3628 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3630 if (len > int(clen[c + n - 1]))
3631 clen[c + n - 1] = len;
3636 for (row_type r = 0; r < nrows(); ++r) {
3637 if (!onlydata && plaintextTopHLine(os, r, clen))
3638 os << docstring(depth * 2, ' ');
3639 for (col_type c = 0; c < ncols(); ++c) {
3640 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3642 if (onlydata && c > 0)
3643 // we don't use operator<< for single UCS4 character.
3644 // see explanation in docstream.h
3646 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3648 if (os.str().size() > max_length)
3653 os << docstring(depth * 2, ' ');
3654 if (plaintextBottomHLine(os, r, clen))
3655 os << docstring(depth * 2, ' ');
3657 if (os.str().size() > max_length)
3663 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3665 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3669 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3671 return cell_info[row][column].inset;
3675 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3677 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3681 void Tabular::setCellInset(row_type row, col_type column,
3682 shared_ptr<InsetTableCell> ins)
3684 CellData & cd = cell_info[row][column];
3689 void Tabular::validate(LaTeXFeatures & features) const
3691 features.require("NeedTabularnewline");
3693 features.require("booktabs");
3694 if (is_long_tabular && !hasVarwidthColumn()) {
3695 if (tabular_width.zero())
3696 features.require("longtable");
3698 features.require("xltabular");
3700 if (rotate && is_long_tabular)
3701 features.require("lscape");
3703 features.require("rotating");
3704 if (hasVarwidthColumn()) {
3705 if (is_long_tabular)
3706 features.require("xltabular");
3708 features.require("tabularx");
3710 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3711 if (isMultiRow(cell))
3712 features.require("multirow");
3713 if (getUsebox(cell) == BOX_VARWIDTH)
3714 features.require("varwidth");
3715 if (getVAlignment(cell) != LYX_VALIGN_TOP
3716 || !getPWidth(cell).zero()
3717 || isVTypeColumn(cellColumn(cell)))
3718 features.require("array");
3719 // Tell footnote that we need a savenote
3720 // environment in non-long tables or
3721 // longtable headers/footers
3722 else if (!is_long_tabular && !features.inFloat())
3723 features.saveNoteEnv("tabular");
3724 else if (!isValidRow(cellRow(cell)))
3725 features.saveNoteEnv("longtable");
3727 cellInset(cell)->validate(features);
3728 features.saveNoteEnv(string());
3733 Tabular::BoxType Tabular::useBox(idx_type cell) const
3735 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3736 if (parlist.size() > 1)
3737 return BOX_VARWIDTH;
3739 ParagraphList::const_iterator cit = parlist.begin();
3740 ParagraphList::const_iterator end = parlist.end();
3742 for (; cit != end; ++cit)
3743 for (int i = 0; i < cit->size(); ++i)
3744 if (cit->isNewline(i) || cit->layout().isEnvironment())
3745 return BOX_VARWIDTH;
3751 /////////////////////////////////////////////////////////////////////
3755 /////////////////////////////////////////////////////////////////////
3757 InsetTableCell::InsetTableCell(Buffer * buf)
3758 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3759 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
3763 bool InsetTableCell::forcePlainLayout(idx_type) const
3765 return isMultiRow || (isMultiColumn && !isFixedWidth);
3769 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3771 return isFixedWidth;
3775 bool InsetTableCell::forceLocalFontSwitch() const
3777 return isFixedWidth;
3781 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3782 FuncStatus & status) const
3784 bool enabled = true;
3785 switch (cmd.action()) {
3786 case LFUN_MATH_DISPLAY:
3787 if (!hasFixedWidth()) {
3792 return InsetText::getStatus(cur, cmd, status);
3794 status.setEnabled(enabled);
3798 docstring InsetTableCell::asString(bool intoInsets)
3801 if (paragraphs().empty())
3803 ParagraphList::const_iterator it = paragraphs().begin();
3804 ParagraphList::const_iterator en = paragraphs().end();
3806 for (; it != en; ++it) {
3811 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3817 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3818 UpdateType utype, TocBackend & backend) const
3820 InsetText::iterateForToc(di, output_active, utype, backend);
3824 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3827 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3828 return InsetText::xhtml(xs, rp);
3832 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
3834 TextMetrics & tm = mi.base.bv->textMetrics(&text());
3836 // Hand font through to contained lyxtext:
3837 tm.font_.fontInfo() = mi.base.font;
3838 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
3840 // This can happen when a layout has a left and right margin,
3841 // and the view is made very narrow. We can't do better than
3842 // to draw it partly out of view (bug 5890).
3843 if (mi.base.textwidth < 1)
3844 mi.base.textwidth = 1;
3846 // We tell metrics here not to expand on multiple pars
3847 // This is the difference to InsetText::Metrics
3848 // FIXME: pars with newlines are still too wide!
3849 if (hasFixedWidth())
3850 tm.metrics(mi, dim, mi.base.textwidth, false);
3852 tm.metrics(mi, dim, 0, false);
3853 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
3854 dim.asc += TEXT_TO_INSET_OFFSET;
3855 dim.des += TEXT_TO_INSET_OFFSET;
3856 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
3861 /////////////////////////////////////////////////////////////////////
3865 /////////////////////////////////////////////////////////////////////
3867 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3869 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3870 rowselect_(false), colselect_(false)
3875 InsetTabular::InsetTabular(InsetTabular const & tab)
3876 : Inset(tab), tabular(tab.tabular),
3877 rowselect_(false), colselect_(false)
3882 InsetTabular::~InsetTabular()
3884 hideDialogs("tabular", this);
3888 void InsetTabular::setBuffer(Buffer & buf)
3890 tabular.setBuffer(buf);
3891 Inset::setBuffer(buf);
3895 bool InsetTabular::insetAllowed(InsetCode code) const
3900 case MATHMACRO_CODE:
3905 return tabular.is_long_tabular;
3913 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3915 return tabular.is_long_tabular &&
3916 (newtype == "Standard" || newtype == "Unnumbered");
3920 void InsetTabular::write(ostream & os) const
3922 os << "Tabular" << endl;
3927 string InsetTabular::contextMenu(BufferView const &, int, int) const
3929 // FIXME: depending on the selection state,
3930 // we could offer a different menu.
3931 return cell(0)->contextMenuName() + ";" + contextMenuName();
3935 string InsetTabular::contextMenuName() const
3937 return "context-tabular";
3941 void InsetTabular::read(Lexer & lex)
3943 //bool const old_format = (lex.getString() == "\\LyXTable");
3951 string token = lex.getString();
3952 while (lex && token != "\\end_inset") {
3954 token = lex.getString();
3957 lex.printError("Missing \\end_inset at this point. ");
3961 int InsetTabular::rowFromY(Cursor & cur, int y) const
3963 // top y coordinate of tabular
3964 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3966 for (; r < tabular.nrows() && y > h; ++r)
3967 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3968 + tabular.interRowSpace(r);
3974 int InsetTabular::columnFromX(Cursor & cur, int x) const
3976 // left x coordinate of tabular
3977 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3979 for (; c < tabular.ncols() && x > w; ++c)
3980 w += tabular.cellWidth(c);
3985 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3987 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3988 // mi.base.textwidth << "\n";
3989 LBUFERR(mi.base.bv);
3991 for (row_type r = 0; r < tabular.nrows(); ++r) {
3994 for (col_type c = 0; c < tabular.ncols(); ++c) {
3995 if (tabular.isPartOfMultiColumn(r, c)
3996 || tabular.isPartOfMultiRow(r, c))
3997 // multicolumn or multirow cell, but not first one
3999 idx_type const cell = tabular.cellIndex(r, c);
4002 Length const p_width = tabular.getPWidth(cell);
4003 if (!p_width.zero())
4004 m.base.textwidth = mi.base.inPixels(p_width);
4005 tabular.cellInset(cell)->metrics(m, dim0);
4006 if (!p_width.zero())
4007 dim0.wid = m.base.textwidth;
4008 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4009 + tabular.interColumnSpace(cell);
4011 // FIXME(?): do we need a second metrics call?
4012 TextMetrics const & tm =
4013 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4015 // determine horizontal offset because of decimal align (if necessary)
4016 int decimal_width = 0;
4017 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4018 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4019 tail.setBuffer(tabular.buffer());
4020 // we need to set macrocontext position everywhere
4021 // otherwise we crash with nested insets (e.g. footnotes)
4022 // after decimal point
4023 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4025 dit.push_back(CursorSlice(tail));
4026 tail.setMacrocontextPositionRecursive(dit);
4028 // remove text leading decimal point
4029 docstring const align_d = tabular.column_info[c].decimal_point;
4030 dit = separatorPos(&tail, align_d);
4032 pit_type const psize = tail.paragraphs().front().size();
4034 tail.paragraphs().front().eraseChars(0,
4035 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4037 tail.metrics(m, dim1);
4038 decimal_width = dim1.width();
4041 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4042 tabular.cell_info[r][c].decimal_width = decimal_width;
4044 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4045 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
4046 int const lastpardes = tm.last().second->descent()
4047 + TEXT_TO_INSET_OFFSET;
4049 switch (tabular.getVAlignment(cell)) {
4050 case Tabular::LYX_VALIGN_TOP:
4052 case Tabular::LYX_VALIGN_MIDDLE:
4053 offset = -(dim0.des - lastpardes)/2;
4055 case Tabular::LYX_VALIGN_BOTTOM:
4056 offset = -(dim0.des - lastpardes);
4059 tabular.cell_info[r][c].voffset = offset;
4060 maxasc = max(maxasc, dim0.asc - offset);
4061 maxdes = max(maxdes, dim0.des + offset);
4063 int const top_space = tabular.row_info[r].top_space_default ?
4064 default_line_space :
4065 mi.base.inPixels(tabular.row_info[r].top_space);
4066 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4067 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4068 default_line_space :
4069 mi.base.inPixels(tabular.row_info[r].bottom_space);
4070 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4073 tabular.updateColumnWidths(mi);
4074 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4075 dim.des = tabular.height() - dim.asc;
4076 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4080 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4082 if (&cur.inset() == this && cur.selection()) {
4083 if (cur.selIsMultiCell()) {
4086 getSelection(cur, rs, re, cs, ce);
4088 idx_type const cell = tabular.cellIndex(row, col);
4089 col_type const cspan = tabular.columnSpan(cell);
4090 row_type const rspan = tabular.rowSpan(cell);
4091 if (col + cspan - 1 >= cs && col <= ce
4092 && row + rspan - 1 >= rs && row <= re)
4095 if (col == tabular.cellColumn(cur.idx())
4096 && row == tabular.cellRow(cur.idx())) {
4097 CursorSlice const & beg = cur.selBegin();
4098 CursorSlice const & end = cur.selEnd();
4100 if ((end.lastpos() > 0 || end.lastpit() > 0)
4101 && end.pos() == end.lastpos() && beg.pos() == 0
4102 && end.pit() == end.lastpit() && beg.pit() == 0)
4110 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4112 x += ADD_TO_TABULAR_WIDTH;
4114 BufferView * bv = pi.base.bv;
4115 Cursor & cur = pi.base.bv->cursor();
4117 // FIXME: As the full background is painted in drawBackground(),
4118 // we have no choice but to do a full repaint for the Text cells.
4119 pi.full_repaint = true;
4121 bool const original_selection_state = pi.selected;
4125 int yy = y + tabular.offsetVAlignment();
4126 for (row_type r = 0; r < tabular.nrows(); ++r) {
4128 for (col_type c = 0; c < tabular.ncols(); ++c) {
4129 if (tabular.isPartOfMultiColumn(r, c))
4132 idx = tabular.cellIndex(r, c);
4134 if (tabular.isPartOfMultiRow(r, c)) {
4135 nx += tabular.cellWidth(idx);
4139 pi.selected |= isCellSelected(cur, r, c);
4140 int const cx = nx + tabular.textHOffset(idx);
4141 int const cy = yy + tabular.textVOffset(idx);
4142 // Cache the Inset position.
4143 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4144 cell(idx)->draw(pi, cx, cy);
4145 drawCellLines(pi, nx, yy, r, idx);
4146 nx += tabular.cellWidth(idx);
4147 pi.selected = original_selection_state;
4150 if (r + 1 < tabular.nrows())
4151 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4152 + tabular.interRowSpace(r + 1);
4157 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4159 x += ADD_TO_TABULAR_WIDTH;
4160 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4161 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4162 pi.backgroundColor(this));
4166 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4168 Cursor & cur = pi.base.bv->cursor();
4170 x += ADD_TO_TABULAR_WIDTH;
4172 if (!cur.selection())
4174 if (&cur.inset() != this)
4179 bool const full_cell_selected = isCellSelected(cur,
4180 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4182 if (cur.selIsMultiCell() || full_cell_selected) {
4183 for (row_type r = 0; r < tabular.nrows(); ++r) {
4185 for (col_type c = 0; c < tabular.ncols(); ++c) {
4186 if (tabular.isPartOfMultiColumn(r, c))
4189 idx_type const cell = tabular.cellIndex(r, c);
4191 if (tabular.isPartOfMultiRow(r, c)) {
4192 xx += tabular.cellWidth(cell);
4195 int const w = tabular.cellWidth(cell);
4196 int const h = tabular.cellHeight(cell);
4197 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4198 if (isCellSelected(cur, r, c))
4199 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4202 if (r + 1 < tabular.nrows())
4203 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4204 + tabular.interRowSpace(r + 1);
4208 // FIXME: This code has no effect because InsetTableCell does not handle
4209 // drawSelection other than the trivial implementation in Inset.
4211 // x += cellXPos(cur.idx());
4212 // x += tabular.textHOffset(cur.idx());
4213 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4218 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4219 row_type row, idx_type cell) const
4221 y -= tabular.rowAscent(row);
4222 int const w = tabular.cellWidth(cell);
4223 int const h = tabular.cellHeight(cell);
4224 Color const linecolor = pi.textColor(Color_tabularline);
4225 Color const gridcolor = pi.textColor(Color_tabularonoffline);
4228 bool drawline = tabular.topLine(cell)
4229 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4230 pi.pain.line(x, y, x + w, y,
4231 drawline ? linecolor : gridcolor,
4232 drawline ? Painter::line_solid : Painter::line_onoffdash);
4235 drawline = tabular.bottomLine(cell);
4236 pi.pain.line(x, y + h, x + w, y + h,
4237 drawline ? linecolor : gridcolor,
4238 drawline ? Painter::line_solid : Painter::line_onoffdash);
4241 col_type const col = tabular.cellColumn(cell);
4242 drawline = tabular.leftLine(cell)
4243 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4244 pi.pain.line(x, y, x, y + h,
4245 drawline ? linecolor : gridcolor,
4246 drawline ? Painter::line_solid : Painter::line_onoffdash);
4249 x -= tabular.interColumnSpace(cell);
4250 col_type next_cell_col = col + 1;
4251 while (next_cell_col < tabular.ncols()
4252 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4254 drawline = tabular.rightLine(cell)
4255 || (next_cell_col < tabular.ncols()
4256 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4257 pi.pain.line(x + w, y, x + w, y + h,
4258 drawline ? linecolor : gridcolor,
4259 drawline ? Painter::line_solid : Painter::line_onoffdash);
4263 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4265 //lyxerr << "InsetTabular::edit: " << this << endl;
4269 if (isRightToLeft(cur))
4270 cur.idx() = tabular.getLastCellInRow(0);
4276 if (isRightToLeft(cur))
4277 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4279 cur.idx() = tabular.numberofcells - 1;
4281 cur.pos() = cur.lastpos(); // FIXME crude guess
4283 cur.setCurrentFont();
4284 // FIXME: this accesses the position cache before it is initialized
4285 //cur.bv().fitCursor();
4289 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
4291 // In a longtable, tell captions what the current float is
4292 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4293 string const saveflt = cnts.current_float();
4294 if (tabular.is_long_tabular) {
4295 cnts.current_float("table");
4296 // in longtables, we only step the counter once
4297 cnts.step(from_ascii("table"), utype);
4298 cnts.isLongtable(true);
4301 ParIterator it2 = it;
4303 size_t const end = it2.nargs();
4304 for ( ; it2.idx() < end; it2.top().forwardIdx())
4305 buffer().updateBuffer(it2, utype);
4308 if (tabular.is_long_tabular) {
4309 cnts.current_float(saveflt);
4310 cnts.isLongtable(false);
4315 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4316 UpdateType utype, TocBackend & backend) const
4318 DocIterator dit = cpit;
4320 size_t const end = dit.nargs();
4321 for ( ; dit.idx() < end; dit.top().forwardIdx())
4322 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4326 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4328 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4329 return x < x0 || x > x0 + tabular.width();
4333 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4335 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4336 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4337 // there is no margin added vertically to tabular insets.
4338 // However, it works for now.
4339 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4343 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4345 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4349 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4351 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4352 << "\n cur:" << cur);
4353 CursorSlice sl = cur.top();
4354 Cursor & bvcur = cur.bv().cursor();
4356 FuncCode const act = cmd.action();
4360 case LFUN_MOUSE_PRESS: {
4361 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4363 if (hitSelectRow(cur.bv(), cmd.x())) {
4364 row_type r = rowFromY(cur, cmd.y());
4365 cur.idx() = tabular.getFirstCellInRow(r);
4369 cur.idx() = tabular.getLastCellInRow(r);
4370 cur.pit() = cur.lastpit();
4371 cur.pos() = cur.lastpos();
4372 cur.selection(true);
4378 if (hitSelectColumn(cur.bv(), cmd.y())) {
4379 col_type c = columnFromX(cur, cmd.x());
4380 cur.idx() = tabular.cellIndex(0, c);
4384 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4385 cur.pit() = cur.lastpit();
4386 cur.pos() = cur.lastpos();
4387 cur.selection(true);
4392 // do not reset cursor/selection if we have selected
4393 // some cells (bug 2715).
4394 if (cmd.button() == mouse_button::button3
4395 && &bvcur.selBegin().inset() == this
4396 && bvcur.selIsMultiCell())
4399 // Let InsetTableCell do it
4400 cell(cur.idx())->dispatch(cur, cmd);
4403 case LFUN_MOUSE_MOTION:
4404 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4405 if (cmd.button() == mouse_button::button1) {
4406 // only accept motions to places not deeper nested than the real anchor
4407 if (!bvcur.realAnchor().hasPart(cur)) {
4411 // select (additional) row
4413 row_type r = rowFromY(cur, cmd.y());
4414 cur.idx() = tabular.getLastCellInRow(r);
4415 // we need to reset the cursor's pit and pos now, as the old ones
4416 // may no longer be valid.
4419 bvcur.setCursor(cur);
4420 bvcur.selection(true);
4423 // select (additional) column
4425 col_type c = columnFromX(cur, cmd.x());
4426 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4427 // we need to reset the cursor's pit and pos now, as the old ones
4428 // may no longer be valid.
4431 bvcur.setCursor(cur);
4432 bvcur.selection(true);
4435 // only update if selection changes
4436 if (bvcur.idx() == cur.idx() &&
4437 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4438 cur.noScreenUpdate();
4439 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4440 bvcur.setCursor(cur);
4441 bvcur.selection(true);
4442 // if this is a multicell selection, we just set the cursor to
4443 // the beginning of the cell's text.
4444 if (bvcur.selIsMultiCell()) {
4445 bvcur.pit() = bvcur.lastpit();
4446 bvcur.pos() = bvcur.lastpos();
4451 case LFUN_MOUSE_RELEASE:
4456 case LFUN_CELL_BACKWARD:
4458 cur.selection(false);
4461 case LFUN_CELL_FORWARD:
4463 cur.selection(false);
4466 case LFUN_CHAR_FORWARD_SELECT:
4467 case LFUN_CHAR_FORWARD:
4468 case LFUN_CHAR_BACKWARD_SELECT:
4469 case LFUN_CHAR_BACKWARD:
4470 case LFUN_CHAR_RIGHT_SELECT:
4471 case LFUN_CHAR_RIGHT:
4472 case LFUN_CHAR_LEFT_SELECT:
4473 case LFUN_CHAR_LEFT:
4474 case LFUN_WORD_FORWARD:
4475 case LFUN_WORD_FORWARD_SELECT:
4476 case LFUN_WORD_BACKWARD:
4477 case LFUN_WORD_BACKWARD_SELECT:
4478 case LFUN_WORD_RIGHT:
4479 case LFUN_WORD_RIGHT_SELECT:
4480 case LFUN_WORD_LEFT:
4481 case LFUN_WORD_LEFT_SELECT: {
4482 // determine whether we move to next or previous cell, where to enter
4483 // the new cell from, and which command to "finish" (i.e., exit the
4486 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4487 FuncCode finish_lfun;
4489 if (act == LFUN_CHAR_FORWARD
4490 || act == LFUN_CHAR_FORWARD_SELECT
4491 || act == LFUN_WORD_FORWARD
4492 || act == LFUN_WORD_FORWARD_SELECT) {
4494 finish_lfun = LFUN_FINISHED_FORWARD;
4496 else if (act == LFUN_CHAR_BACKWARD
4497 || act == LFUN_CHAR_BACKWARD_SELECT
4498 || act == LFUN_WORD_BACKWARD
4499 || act == LFUN_WORD_BACKWARD_SELECT) {
4501 finish_lfun = LFUN_FINISHED_BACKWARD;
4503 // LEFT or RIGHT commands --- the interpretation will depend on the
4504 // table's direction.
4506 bool const right = act == LFUN_CHAR_RIGHT
4507 || act == LFUN_CHAR_RIGHT_SELECT
4508 || act == LFUN_WORD_RIGHT
4509 || act == LFUN_WORD_RIGHT_SELECT;
4510 next_cell = isRightToLeft(cur) != right;
4512 if (lyxrc.visual_cursor)
4513 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4515 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4518 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4519 || act == LFUN_CHAR_BACKWARD_SELECT
4520 || act == LFUN_CHAR_RIGHT_SELECT
4521 || act == LFUN_CHAR_LEFT_SELECT
4522 || act == LFUN_WORD_FORWARD_SELECT
4523 || act == LFUN_WORD_RIGHT_SELECT
4524 || act == LFUN_WORD_BACKWARD_SELECT
4525 || act == LFUN_WORD_LEFT_SELECT;
4527 // If we have a multicell selection or we're
4528 // not doing some LFUN_*_SELECT thing anyway...
4529 if (!cur.selIsMultiCell() || !select) {
4530 col_type const c = tabular.cellColumn(cur.idx());
4531 row_type const r = tabular.cellRow(cur.idx());
4532 // Are we trying to select the whole cell and is the whole cell
4533 // not yet selected?
4534 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4535 ((next_cell && cur.pit() == cur.lastpit()
4536 && cur.pos() == cur.lastpos())
4537 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4539 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4541 // ...try to dispatch to the cell's inset.
4542 cell(cur.idx())->dispatch(cur, cmd);
4544 // When we already have a selection we want to select the whole cell
4545 // before going to the next cell.
4546 if (select_whole && !empty_cell){
4547 getText(cur.idx())->selectAll(cur);
4549 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4553 // FIXME: When we support the selection of an empty cell, remove
4554 // the !empty_cell from this condition. For now we jump to the next
4555 // cell if the current cell is empty.
4556 if (cur.result().dispatched() && !empty_cell)
4560 // move to next/prev cell, as appropriate
4561 // note that we will always do this if we're selecting and we have
4562 // a multicell selection
4563 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4564 << " cell from: " << int(entry_from));
4566 moveNextCell(cur, entry_from);
4568 movePrevCell(cur, entry_from);
4569 // if we're exiting the table, call the appropriate FINISHED lfun
4570 if (sl == cur.top()) {
4571 cmd = FuncRequest(finish_lfun);
4576 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4581 case LFUN_DOWN_SELECT:
4583 if (!(cur.selection() && cur.selIsMultiCell()))
4584 cell(cur.idx())->dispatch(cur, cmd);
4586 cur.dispatched(); // override the cell's decision
4587 if (sl == cur.top()) {
4588 // if our Text didn't do anything to the cursor
4589 // then we try to put the cursor into the cell below
4590 // setting also the right targetX.
4591 cur.selHandle(act == LFUN_DOWN_SELECT);
4592 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4593 int const xtarget = cur.targetX();
4594 // WARNING: Once cur.idx() has been reset, the cursor is in
4595 // an inconsistent state until pos() has been set. Be careful
4596 // what you do with it!
4597 cur.idx() = tabular.cellBelow(cur.idx());
4599 TextMetrics const & tm =
4600 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4601 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4602 cur.setCurrentFont();
4605 if (sl == cur.top()) {
4606 // we trick it to go to forward after leaving the
4608 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4611 if (cur.selIsMultiCell()) {
4612 cur.pit() = cur.lastpit();
4613 cur.pos() = cur.lastpos();
4614 cur.setCurrentFont();
4615 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4618 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4621 case LFUN_UP_SELECT:
4623 if (!(cur.selection() && cur.selIsMultiCell()))
4624 cell(cur.idx())->dispatch(cur, cmd);
4625 cur.dispatched(); // override the cell's decision
4626 if (sl == cur.top()) {
4627 // if our Text didn't do anything to the cursor
4628 // then we try to put the cursor into the cell above
4629 // setting also the right targetX.
4630 cur.selHandle(act == LFUN_UP_SELECT);
4631 if (tabular.cellRow(cur.idx()) != 0) {
4632 int const xtarget = cur.targetX();
4633 // WARNING: Once cur.idx() has been reset, the cursor is in
4634 // an inconsistent state until pos() has been set. Be careful
4635 // what you do with it!
4636 cur.idx() = tabular.cellAbove(cur.idx());
4637 cur.pit() = cur.lastpit();
4638 Text const * text = cell(cur.idx())->getText(0);
4639 TextMetrics const & tm = cur.bv().textMetrics(text);
4640 ParagraphMetrics const & pm =
4641 tm.parMetrics(cur.lastpit());
4642 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4643 cur.setCurrentFont();
4646 if (sl == cur.top()) {
4647 cmd = FuncRequest(LFUN_UP);
4650 if (cur.selIsMultiCell()) {
4652 cur.pos() = cur.lastpos();
4653 cur.setCurrentFont();
4654 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4657 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4660 case LFUN_LAYOUT_TABULAR:
4661 cur.bv().showDialog("tabular");
4664 case LFUN_INSET_MODIFY:
4665 // we come from the dialog
4666 if (cmd.getArg(0) == "tabular")
4667 tabularFeatures(cur, cmd.getLongArg(1));
4672 case LFUN_TABULAR_FEATURE:
4673 tabularFeatures(cur, to_utf8(cmd.argument()));
4676 // insert file functions
4677 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4678 case LFUN_FILE_INSERT_PLAINTEXT:
4680 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4681 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4682 FileName(to_utf8(cmd.argument())));
4685 cur.recordUndoInset();
4686 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4687 // content has been replaced,
4688 // so cursor might be invalid
4689 cur.pos() = cur.lastpos();
4690 cur.pit() = cur.lastpit();
4691 bvcur.setCursor(cur);
4698 if (cur.selIsMultiCell()) {
4699 if (copySelection(cur)) {
4700 cur.recordUndoInset();
4704 cell(cur.idx())->dispatch(cur, cmd);
4707 case LFUN_SELF_INSERT:
4708 if (cur.selIsMultiCell()) {
4709 cur.recordUndoInset();
4711 BufferView * bv = &cur.bv();
4712 docstring::const_iterator cit = cmd.argument().begin();
4713 docstring::const_iterator const end = cmd.argument().end();
4714 for (; cit != end; ++cit)
4715 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4718 bv->bookmarkEditPosition();
4720 cell(cur.idx())->dispatch(cur, cmd);
4723 case LFUN_CHAR_DELETE_BACKWARD:
4724 case LFUN_CHAR_DELETE_FORWARD:
4725 if (cur.selIsMultiCell()) {
4726 cur.recordUndoInset();
4729 cell(cur.idx())->dispatch(cur, cmd);
4733 if (!cur.selection())
4735 if (cur.selIsMultiCell()) {
4739 cell(cur.idx())->dispatch(cur, cmd);
4742 case LFUN_CLIPBOARD_PASTE:
4743 case LFUN_PRIMARY_SELECTION_PASTE: {
4744 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4745 theClipboard().getAsText(Clipboard::PlainTextType) :
4746 theSelection().get();
4749 // pass to InsertPlaintextString, but
4750 // only if we have multi-cell content
4751 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4752 cur.recordUndoInset();
4753 if (insertPlaintextString(cur.bv(), clip, false)) {
4754 // content has been replaced,
4755 // so cursor might be invalid
4756 cur.pos() = cur.lastpos();
4757 cur.pit() = cur.lastpit();
4758 bvcur.setCursor(cur);
4762 // Let the cell handle normal text
4763 cell(cur.idx())->dispatch(cur, cmd);
4768 if (!tabularStackDirty()) {
4769 if (!cur.selIsMultiCell())
4770 cell(cur.idx())->dispatch(cur, cmd);
4773 if (theClipboard().isInternal()) {
4774 cur.recordUndoInset();
4775 pasteClipboard(cur);
4779 case LFUN_FONT_EMPH:
4780 case LFUN_FONT_BOLD:
4781 case LFUN_FONT_BOLDSYMBOL:
4782 case LFUN_FONT_ROMAN:
4783 case LFUN_FONT_NOUN:
4784 case LFUN_FONT_ITAL:
4785 case LFUN_FONT_FRAK:
4786 case LFUN_FONT_TYPEWRITER:
4787 case LFUN_FONT_SANS:
4788 case LFUN_TEXTSTYLE_APPLY:
4789 case LFUN_TEXTSTYLE_UPDATE:
4790 case LFUN_FONT_SIZE:
4791 case LFUN_FONT_UNDERLINE:
4792 case LFUN_FONT_STRIKEOUT:
4793 case LFUN_FONT_CROSSOUT:
4794 case LFUN_FONT_UNDERUNDERLINE:
4795 case LFUN_FONT_UNDERWAVE:
4797 case LFUN_PARAGRAPH_PARAMS_APPLY:
4798 case LFUN_PARAGRAPH_PARAMS:
4799 case LFUN_WORD_CAPITALIZE:
4800 case LFUN_WORD_UPCASE:
4801 case LFUN_WORD_LOWCASE:
4802 case LFUN_CHARS_TRANSPOSE:
4803 if (cur.selIsMultiCell()) {
4806 getSelection(cur, rs, re, cs, ce);
4807 Cursor tmpcur = cur;
4808 for (row_type r = rs; r <= re; ++r) {
4809 for (col_type c = cs; c <= ce; ++c) {
4810 // cursor follows cell:
4811 tmpcur.idx() = tabular.cellIndex(r, c);
4812 // select this cell only:
4815 tmpcur.resetAnchor();
4816 tmpcur.pit() = tmpcur.lastpit();
4817 tmpcur.pos() = tmpcur.top().lastpos();
4818 tmpcur.setCursor(tmpcur);
4819 tmpcur.setSelection();
4820 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4825 cell(cur.idx())->dispatch(cur, cmd);
4829 case LFUN_INSET_SETTINGS:
4830 // relay this lfun to Inset, not to the cell.
4831 Inset::doDispatch(cur, cmd);
4835 // we try to handle this event in the insets dispatch function.
4836 cell(cur.idx())->dispatch(cur, cmd);
4842 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4843 string const & argument, FuncStatus & status) const
4846 int action = Tabular::LAST_ACTION;
4848 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4849 if (tabularFeature[i].feature == s) {
4850 action = tabularFeature[i].action;
4854 if (action == Tabular::LAST_ACTION) {
4856 status.setUnknown(true);
4860 row_type sel_row_start = 0;
4861 row_type sel_row_end = 0;
4862 col_type sel_col_start = 0;
4863 col_type sel_col_end = 0;
4864 Tabular::ltType dummyltt;
4867 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4870 case Tabular::SET_PWIDTH:
4871 case Tabular::SET_MPWIDTH:
4872 case Tabular::TOGGLE_VARWIDTH_COLUMN:
4873 case Tabular::SET_SPECIAL_COLUMN:
4874 case Tabular::SET_SPECIAL_MULTICOLUMN:
4875 case Tabular::APPEND_ROW:
4876 case Tabular::APPEND_COLUMN:
4877 case Tabular::DELETE_ROW:
4878 case Tabular::DELETE_COLUMN:
4879 case Tabular::COPY_ROW:
4880 case Tabular::COPY_COLUMN:
4881 case Tabular::SET_TOP_SPACE:
4882 case Tabular::SET_BOTTOM_SPACE:
4883 case Tabular::SET_INTERLINE_SPACE:
4887 case Tabular::SET_TABULAR_WIDTH:
4888 status.setEnabled(!tabular.rotate
4889 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4892 case Tabular::MOVE_COLUMN_RIGHT:
4893 case Tabular::MOVE_COLUMN_LEFT:
4894 case Tabular::MOVE_ROW_DOWN:
4895 case Tabular::MOVE_ROW_UP: {
4896 if (cur.selection()) {
4897 status.message(_("Selections not supported."));
4898 status.setEnabled(false);
4902 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4903 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4904 (action == Tabular::MOVE_COLUMN_LEFT &&
4905 tabular.cellColumn(cur.idx()) == 0) ||
4906 (action == Tabular::MOVE_ROW_DOWN &&
4907 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4908 (action == Tabular::MOVE_ROW_UP &&
4909 tabular.cellRow(cur.idx()) == 0)) {
4910 status.setEnabled(false);
4914 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4915 action == Tabular::MOVE_COLUMN_LEFT) {
4916 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4917 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4918 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4919 status.message(_("Multi-column in current or"
4920 " destination column."));
4921 status.setEnabled(false);
4926 if (action == Tabular::MOVE_ROW_DOWN ||
4927 action == Tabular::MOVE_ROW_UP) {
4928 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4929 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4930 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4931 status.message(_("Multi-row in current or"
4932 " destination row."));
4933 status.setEnabled(false);
4938 status.setEnabled(true);
4942 case Tabular::SET_DECIMAL_POINT:
4944 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4947 case Tabular::SET_MULTICOLUMN:
4948 case Tabular::UNSET_MULTICOLUMN:
4949 case Tabular::MULTICOLUMN:
4950 // If a row is set as longtable caption, it must not be allowed
4951 // to unset that this row is a multicolumn.
4952 status.setEnabled(sel_row_start == sel_row_end
4953 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4954 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4957 case Tabular::SET_MULTIROW:
4958 case Tabular::UNSET_MULTIROW:
4959 case Tabular::MULTIROW:
4960 // If a row is set as longtable caption, it must not be allowed
4961 // to unset that this row is a multirow.
4962 status.setEnabled(sel_col_start == sel_col_end
4963 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4964 status.setOnOff(tabular.isMultiRow(cur.idx()));
4967 case Tabular::SET_ALL_LINES:
4968 case Tabular::UNSET_ALL_LINES:
4969 case Tabular::SET_BORDER_LINES:
4970 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4973 case Tabular::SET_LINE_TOP:
4974 case Tabular::SET_LINE_BOTTOM:
4975 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4978 case Tabular::SET_LINE_LEFT:
4979 case Tabular::SET_LINE_RIGHT:
4980 status.setEnabled(!tabular.use_booktabs
4981 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4984 case Tabular::TOGGLE_LINE_TOP:
4985 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4986 status.setOnOff(tabular.topLine(cur.idx()));
4989 case Tabular::TOGGLE_LINE_BOTTOM:
4990 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4991 status.setOnOff(tabular.bottomLine(cur.idx()));
4994 case Tabular::TOGGLE_LINE_LEFT:
4995 status.setEnabled(!tabular.use_booktabs
4996 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4997 status.setOnOff(tabular.leftLine(cur.idx()));
5000 case Tabular::TOGGLE_LINE_RIGHT:
5001 status.setEnabled(!tabular.use_booktabs
5002 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5003 status.setOnOff(tabular.rightLine(cur.idx()));
5006 // multirow cells only inherit the alignment of the column if the column has
5007 // no width, otherwise they are left-aligned
5008 // therefore allow always left but right and center only if there is no width
5009 case Tabular::M_ALIGN_LEFT:
5012 case Tabular::ALIGN_LEFT:
5013 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5016 case Tabular::M_ALIGN_RIGHT:
5019 case Tabular::ALIGN_RIGHT:
5020 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5021 && !tabular.getPWidth(cur.idx()).zero()));
5022 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5025 case Tabular::M_ALIGN_CENTER:
5028 case Tabular::ALIGN_CENTER:
5029 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5030 && !tabular.getPWidth(cur.idx()).zero()));
5031 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5034 case Tabular::ALIGN_BLOCK:
5035 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5036 && !tabular.isMultiRow(cur.idx()));
5037 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5040 case Tabular::ALIGN_DECIMAL:
5041 status.setEnabled(!tabular.isMultiRow(cur.idx())
5042 && !tabular.isMultiColumn(cur.idx()));
5043 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5046 case Tabular::M_VALIGN_TOP:
5049 case Tabular::VALIGN_TOP:
5050 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5051 && !tabular.isMultiRow(cur.idx()));
5053 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5056 case Tabular::M_VALIGN_BOTTOM:
5059 case Tabular::VALIGN_BOTTOM:
5060 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5061 && !tabular.isMultiRow(cur.idx()));
5063 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5066 case Tabular::M_VALIGN_MIDDLE:
5069 case Tabular::VALIGN_MIDDLE:
5070 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5071 && !tabular.isMultiRow(cur.idx()));
5073 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5076 case Tabular::SET_LONGTABULAR:
5077 case Tabular::TOGGLE_LONGTABULAR:
5078 // setting as longtable is not allowed when table is inside a float
5079 if (cur.innerInsetOfType(FLOAT_CODE) != 0
5080 || cur.innerInsetOfType(WRAP_CODE) != 0)
5081 status.setEnabled(false);
5083 status.setEnabled(true);
5084 status.setOnOff(tabular.is_long_tabular);
5087 case Tabular::UNSET_LONGTABULAR:
5088 status.setOnOff(!tabular.is_long_tabular);
5091 case Tabular::TOGGLE_ROTATE_TABULAR:
5092 case Tabular::SET_ROTATE_TABULAR:
5093 status.setOnOff(tabular.rotate != 0);
5096 case Tabular::TABULAR_VALIGN_TOP:
5097 status.setEnabled(tabular.tabular_width.zero());
5098 status.setOnOff(tabular.tabular_valignment
5099 == Tabular::LYX_VALIGN_TOP);
5101 case Tabular::TABULAR_VALIGN_MIDDLE:
5102 status.setEnabled(tabular.tabular_width.zero());
5103 status.setOnOff(tabular.tabular_valignment
5104 == Tabular::LYX_VALIGN_MIDDLE);
5106 case Tabular::TABULAR_VALIGN_BOTTOM:
5107 status.setEnabled(tabular.tabular_width.zero());
5108 status.setOnOff(tabular.tabular_valignment
5109 == Tabular::LYX_VALIGN_BOTTOM);
5112 case Tabular::LONGTABULAR_ALIGN_LEFT:
5113 status.setOnOff(tabular.longtabular_alignment
5114 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5116 case Tabular::LONGTABULAR_ALIGN_CENTER:
5117 status.setOnOff(tabular.longtabular_alignment
5118 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5120 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5121 status.setOnOff(tabular.longtabular_alignment
5122 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5125 case Tabular::UNSET_ROTATE_TABULAR:
5126 status.setOnOff(tabular.rotate == 0);
5129 case Tabular::TOGGLE_ROTATE_CELL:
5130 case Tabular::SET_ROTATE_CELL:
5131 status.setOnOff(!oneCellHasRotationState(false,
5132 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5135 case Tabular::UNSET_ROTATE_CELL:
5136 status.setOnOff(!oneCellHasRotationState(true,
5137 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5140 case Tabular::SET_USEBOX:
5141 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5144 // every row can only be one thing:
5145 // either a footer or header
5146 case Tabular::SET_LTFIRSTHEAD:
5147 status.setEnabled(sel_row_start == sel_row_end);
5148 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5151 case Tabular::UNSET_LTFIRSTHEAD:
5152 status.setEnabled(sel_row_start == sel_row_end);
5153 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5156 case Tabular::SET_LTHEAD:
5157 status.setEnabled(sel_row_start == sel_row_end);
5158 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5161 case Tabular::UNSET_LTHEAD:
5162 status.setEnabled(sel_row_start == sel_row_end);
5163 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5166 case Tabular::SET_LTFOOT:
5167 status.setEnabled(sel_row_start == sel_row_end);
5168 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5171 case Tabular::UNSET_LTFOOT:
5172 status.setEnabled(sel_row_start == sel_row_end);
5173 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5176 case Tabular::SET_LTLASTFOOT:
5177 status.setEnabled(sel_row_start == sel_row_end);
5178 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5181 case Tabular::UNSET_LTLASTFOOT:
5182 status.setEnabled(sel_row_start == sel_row_end);
5183 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5186 case Tabular::SET_LTNEWPAGE:
5187 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5189 case Tabular::UNSET_LTNEWPAGE:
5190 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5193 // only one row in head/firsthead/foot/lasthead can be the caption
5194 // and a multirow cannot be set as caption
5195 case Tabular::SET_LTCAPTION:
5196 status.setEnabled(sel_row_start == sel_row_end
5197 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5198 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5199 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5200 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5201 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5202 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5203 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5204 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5205 && !tabular.isMultiRow(sel_row_start));
5206 status.setOnOff(tabular.ltCaption(sel_row_start));
5209 case Tabular::UNSET_LTCAPTION:
5210 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5213 case Tabular::TOGGLE_LTCAPTION:
5214 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5215 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5216 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5217 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5218 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5219 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5220 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5221 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5222 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5223 status.setOnOff(tabular.ltCaption(sel_row_start));
5226 case Tabular::TOGGLE_BOOKTABS:
5227 case Tabular::SET_BOOKTABS:
5228 status.setOnOff(tabular.use_booktabs);
5231 case Tabular::UNSET_BOOKTABS:
5232 status.setOnOff(!tabular.use_booktabs);
5237 status.setEnabled(false);
5244 // function sets an object as defined in FuncStatus.h:
5245 // states OK, Unknown, Disabled, On, Off.
5246 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5247 FuncStatus & status) const
5249 switch (cmd.action()) {
5250 case LFUN_INSET_MODIFY:
5251 if (cmd.getArg(0) != "tabular")
5253 if (cmd.getArg(1) == "for-dialog") {
5254 // The dialog is asking the status of a command
5255 if (&cur.inset() != this)
5257 string action = cmd.getArg(2);
5258 string arg = cmd.getLongArg(3);
5259 return getFeatureStatus(cur, action, arg, status);
5261 // We always enable the lfun if it is coming from the dialog
5262 // because the dialog makes sure all the settings are valid,
5263 // even though the first argument might not be valid now.
5264 status.setEnabled(true);
5268 case LFUN_TABULAR_FEATURE: {
5269 if (&cur.inset() != this)
5271 string action = cmd.getArg(0);
5272 string arg = cmd.getLongArg(1);
5273 return getFeatureStatus(cur, action, arg, status);
5276 case LFUN_CAPTION_INSERT: {
5277 // caption is only allowed in caption cell of longtable
5278 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5279 status.setEnabled(false);
5282 // only standard caption is allowed
5283 string arg = cmd.getArg(0);
5284 if (!arg.empty() && arg != "Standard") {
5285 status.setEnabled(false);
5288 // check if there is already a caption
5289 bool have_caption = false;
5290 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5291 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5292 ParagraphList::const_iterator pend = itc.paragraphs().end();
5293 for (; pit != pend; ++pit) {
5294 InsetList::const_iterator it = pit->insetList().begin();
5295 InsetList::const_iterator end = pit->insetList().end();
5296 for (; it != end; ++it) {
5297 if (it->inset->lyxCode() == CAPTION_CODE) {
5298 have_caption = true;
5303 status.setEnabled(!have_caption);
5307 // These are only enabled inside tabular
5308 case LFUN_CELL_BACKWARD:
5309 case LFUN_CELL_FORWARD:
5310 status.setEnabled(true);
5313 // disable these with multiple cells selected
5314 case LFUN_INSET_INSERT:
5315 case LFUN_TABULAR_INSERT:
5316 case LFUN_FLEX_INSERT:
5317 case LFUN_FLOAT_INSERT:
5318 case LFUN_FLOAT_WIDE_INSERT:
5319 case LFUN_FOOTNOTE_INSERT:
5320 case LFUN_MARGINALNOTE_INSERT:
5321 case LFUN_MATH_INSERT:
5322 case LFUN_MATH_MODE:
5323 case LFUN_MATH_MUTATE:
5324 case LFUN_MATH_DISPLAY:
5325 case LFUN_NOTE_INSERT:
5326 case LFUN_ARGUMENT_INSERT:
5327 case LFUN_BOX_INSERT:
5328 case LFUN_BRANCH_INSERT:
5329 case LFUN_PHANTOM_INSERT:
5330 case LFUN_WRAP_INSERT:
5331 case LFUN_PREVIEW_INSERT:
5332 case LFUN_ERT_INSERT: {
5333 if (cur.selIsMultiCell()) {
5334 status.setEnabled(false);
5337 return cell(cur.idx())->getStatus(cur, cmd, status);
5340 // disable in non-fixed-width cells
5341 case LFUN_PARAGRAPH_BREAK:
5342 // multirow does not allow paragraph breaks
5343 if (tabular.isMultiRow(cur.idx())) {
5344 status.setEnabled(false);
5348 case LFUN_NEWLINE_INSERT:
5349 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
5350 && tabular.getPWidth(cur.idx()).zero()) {
5351 status.setEnabled(false);
5354 return cell(cur.idx())->getStatus(cur, cmd, status);
5356 case LFUN_NEWPAGE_INSERT:
5357 status.setEnabled(false);
5361 if (tabularStackDirty() && theClipboard().isInternal()) {
5362 if (cur.selIsMultiCell()) {
5365 getSelection(cur, rs, re, cs, ce);
5366 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5367 && paste_tabular->nrows() == re - rs + 1)
5368 status.setEnabled(true);
5370 status.setEnabled(false);
5371 status.message(_("Selection size should match clipboard content."));
5374 status.setEnabled(true);
5377 return cell(cur.idx())->getStatus(cur, cmd, status);
5379 case LFUN_INSET_SETTINGS:
5380 // relay this lfun to Inset, not to the cell.
5381 return Inset::getStatus(cur, cmd, status);
5384 // we try to handle this event in the insets dispatch function.
5385 return cell(cur.idx())->getStatus(cur, cmd, status);
5391 Inset::DisplayType InsetTabular::display() const
5393 if (tabular.is_long_tabular) {
5394 switch (tabular.longtabular_alignment) {
5395 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5397 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5399 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5409 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5411 tabular.latex(os, runparams);
5415 int InsetTabular::plaintext(odocstringstream & os,
5416 OutputParams const & runparams, size_t max_length) const
5418 os << '\n'; // output table on a new line
5419 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5420 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5421 return PLAINTEXT_NEWLINE;
5425 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5430 // FIXME: Why not pass a proper DocIterator here?
5432 // if the table is inside a float it doesn't need the informaltable
5433 // wrapper. Search for it.
5434 for (master = owner(); master; master = master->owner())
5435 if (master->lyxCode() == FLOAT_CODE)
5440 os << "<informaltable>";
5443 ret += tabular.docbook(os, runparams);
5445 os << "</informaltable>";
5452 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5454 return tabular.xhtml(xs, rp);
5458 void InsetTabular::validate(LaTeXFeatures & features) const
5460 tabular.validate(features);
5461 features.useInsetLayout(getLayout());
5465 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5467 return tabular.cellInset(idx);
5471 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5473 return tabular.cellInset(idx);
5477 void InsetTabular::cursorPos(BufferView const & bv,
5478 CursorSlice const & sl, bool boundary, int & x, int & y) const
5480 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5482 // y offset correction
5483 y += cellYPos(sl.idx());
5484 y += tabular.textVOffset(sl.idx());
5485 y += tabular.offsetVAlignment();
5487 // x offset correction
5488 x += cellXPos(sl.idx());
5489 x += tabular.textHOffset(sl.idx());
5490 x += ADD_TO_TABULAR_WIDTH;
5494 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5498 Inset const & inset = *tabular.cellInset(cell);
5499 Point o = bv.coordCache().getInsets().xy(&inset);
5500 int const xbeg = o.x_ - tabular.textHOffset(cell);
5501 int const xend = xbeg + tabular.cellWidth(cell);
5502 row_type const row = tabular.cellRow(cell);
5503 int const ybeg = o.y_ - tabular.rowAscent(row)
5504 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5505 int const yend = ybeg + tabular.cellHeight(cell);
5517 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5518 // << " ybeg=" << ybeg << " yend=" << yend
5519 // << " xx=" << xx << " yy=" << yy
5520 // << " dist=" << xx + yy << endl;
5525 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5527 //lyxerr << "InsetTabular::editXY: " << this << endl;
5529 cur.idx() = getNearestCell(cur.bv(), x, y);
5530 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5534 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5536 cur.idx() = getNearestCell(cur.bv(), x, y);
5537 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5541 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5543 idx_type idx_min = 0;
5544 int dist_min = numeric_limits<int>::max();
5545 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5546 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5547 int const d = dist(bv, i, x, y);
5558 int InsetTabular::cellYPos(idx_type const cell) const
5560 row_type row = tabular.cellRow(cell);
5562 for (row_type r = 0; r < row; ++r)
5563 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5564 + tabular.interRowSpace(r + 1);
5569 int InsetTabular::cellXPos(idx_type const cell) const
5571 col_type col = tabular.cellColumn(cell);
5573 for (col_type c = 0; c < col; ++c)
5574 lx += tabular.column_info[c].width;
5579 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5581 row_type const row = tabular.cellRow(cur.idx());
5582 col_type const col = tabular.cellColumn(cur.idx());
5584 if (isRightToLeft(cur)) {
5585 if (tabular.cellColumn(cur.idx()) == 0) {
5586 if (row == tabular.nrows() - 1)
5588 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5593 cur.idx() = tabular.getLastCellInRow(row - 1);
5595 cur.idx() = tabular.cellIndex(row, col - 1);
5598 if (tabular.isLastCell(cur.idx()))
5600 if (cur.idx() == tabular.getLastCellInRow(row))
5601 cur.idx() = tabular.cellIndex(row + 1, 0);
5603 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5604 cur.idx() = tabular.cellIndex(row, colnextcell);
5608 cur.boundary(false);
5610 if (cur.selIsMultiCell()) {
5611 cur.pit() = cur.lastpit();
5612 cur.pos() = cur.lastpos();
5619 // in visual mode, place cursor at extreme left or right
5621 switch(entry_from) {
5623 case ENTRY_DIRECTION_RIGHT:
5624 cur.posVisToRowExtremity(false /* !left */);
5626 case ENTRY_DIRECTION_LEFT:
5627 cur.posVisToRowExtremity(true /* left */);
5629 case ENTRY_DIRECTION_IGNORE:
5630 // nothing to do in this case
5634 cur.setCurrentFont();
5638 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5640 row_type const row = tabular.cellRow(cur.idx());
5641 col_type const col = tabular.cellColumn(cur.idx());
5643 if (isRightToLeft(cur)) {
5644 if (cur.idx() == tabular.getLastCellInRow(row)) {
5647 cur.idx() = tabular.getFirstCellInRow(row);
5648 cur.idx() = tabular.cellAbove(cur.idx());
5650 if (tabular.isLastCell(cur.idx()))
5652 if (cur.idx() == tabular.getLastCellInRow(row))
5653 cur.idx() = tabular.cellIndex(row + 1, 0);
5655 cur.idx() = tabular.cellIndex(row, col + 1);
5658 if (cur.idx() == 0) // first cell
5661 cur.idx() = tabular.getLastCellInRow(row - 1);
5663 cur.idx() = tabular.cellIndex(row, col - 1);
5666 if (cur.selIsMultiCell()) {
5667 cur.pit() = cur.lastpit();
5668 cur.pos() = cur.lastpos();
5672 cur.pit() = cur.lastpit();
5673 cur.pos() = cur.lastpos();
5675 // in visual mode, place cursor at extreme left or right
5677 switch(entry_from) {
5679 case ENTRY_DIRECTION_RIGHT:
5680 cur.posVisToRowExtremity(false /* !left */);
5682 case ENTRY_DIRECTION_LEFT:
5683 cur.posVisToRowExtremity(true /* left */);
5685 case ENTRY_DIRECTION_IGNORE:
5686 // nothing to do in this case
5690 cur.setCurrentFont();
5694 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5696 cur.recordUndoInset(this);
5698 istringstream is(argument);
5699 // limit the size of strings we read to avoid memory problems
5703 size_t safe_guard = 0;
5708 if (safe_guard > 1000) {
5709 LYXERR0("parameter max count reached!");
5713 Tabular::Feature action = Tabular::LAST_ACTION;
5716 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5717 if (s != tabularFeature[i].feature)
5719 action = tabularFeature[i].action;
5722 if (action == Tabular::LAST_ACTION) {
5723 LYXERR0("Feature not found " << s);
5727 if (tabularFeature[i].need_value)
5729 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5730 tabularFeatures(cur, action, val);
5735 static void checkLongtableSpecial(Tabular::ltType & ltt,
5736 string const & special, bool & flag)
5738 if (special == "dl_above") {
5741 } else if (special == "dl_below") {
5742 ltt.bottomDL = flag;
5744 } else if (special == "empty") {
5754 bool InsetTabular::oneCellHasRotationState(bool rotated,
5755 row_type row_start, row_type row_end,
5756 col_type col_start, col_type col_end) const
5758 for (row_type r = row_start; r <= row_end; ++r)
5759 for (col_type c = col_start; c <= col_end; ++c)
5761 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5764 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5771 void InsetTabular::tabularFeatures(Cursor & cur,
5772 Tabular::Feature feature, string const & value)
5774 col_type sel_col_start;
5775 col_type sel_col_end;
5776 row_type sel_row_start;
5777 row_type sel_row_end;
5778 bool setLines = false;
5779 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5780 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5784 case Tabular::M_ALIGN_LEFT:
5785 case Tabular::ALIGN_LEFT:
5786 setAlign = LYX_ALIGN_LEFT;
5789 case Tabular::M_ALIGN_RIGHT:
5790 case Tabular::ALIGN_RIGHT:
5791 setAlign = LYX_ALIGN_RIGHT;
5794 case Tabular::M_ALIGN_CENTER:
5795 case Tabular::ALIGN_CENTER:
5796 setAlign = LYX_ALIGN_CENTER;
5799 case Tabular::ALIGN_BLOCK:
5800 setAlign = LYX_ALIGN_BLOCK;
5803 case Tabular::ALIGN_DECIMAL:
5804 setAlign = LYX_ALIGN_DECIMAL;
5807 case Tabular::M_VALIGN_TOP:
5808 case Tabular::VALIGN_TOP:
5809 setVAlign = Tabular::LYX_VALIGN_TOP;
5812 case Tabular::M_VALIGN_BOTTOM:
5813 case Tabular::VALIGN_BOTTOM:
5814 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5817 case Tabular::M_VALIGN_MIDDLE:
5818 case Tabular::VALIGN_MIDDLE:
5819 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5826 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5827 row_type const row = tabular.cellRow(cur.idx());
5828 col_type const column = tabular.cellColumn(cur.idx());
5830 Tabular::ltType ltt;
5834 case Tabular::SET_TABULAR_WIDTH:
5835 tabular.setTabularWidth(Length(value));
5838 case Tabular::SET_PWIDTH: {
5839 Length const len(value);
5840 tabular.setColumnPWidth(cur, cur.idx(), len);
5842 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5843 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5847 case Tabular::SET_MPWIDTH:
5848 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5851 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
5852 bool const varwidth = value == "on";
5853 tabular.toggleVarwidth(cur.idx(), varwidth);
5857 case Tabular::SET_MROFFSET:
5858 tabular.setMROffset(cur, cur.idx(), Length(value));
5861 case Tabular::SET_SPECIAL_COLUMN:
5862 case Tabular::SET_SPECIAL_MULTICOLUMN:
5863 if (value == "none")
5864 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5866 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5869 case Tabular::APPEND_ROW:
5870 // append the row into the tabular
5871 tabular.appendRow(row);
5874 case Tabular::APPEND_COLUMN:
5875 // append the column into the tabular
5876 tabular.appendColumn(column);
5877 cur.idx() = tabular.cellIndex(row, column);
5880 case Tabular::DELETE_ROW:
5881 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5882 for (col_type c = 0; c < tabular.ncols(); c++)
5883 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5884 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5887 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5888 tabular.deleteRow(sel_row_start);
5889 if (sel_row_start >= tabular.nrows())
5891 cur.idx() = tabular.cellIndex(sel_row_start, column);
5894 cur.selection(false);
5897 case Tabular::DELETE_COLUMN:
5898 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5899 for (row_type r = 0; r < tabular.nrows(); r++)
5900 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5901 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5904 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5905 for (row_type r = 0; r < tabular.nrows(); r++)
5906 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5907 tabular.leftLine(tabular.cellIndex(r, 0)));
5910 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5911 tabular.deleteColumn(sel_col_start);
5912 if (sel_col_start >= tabular.ncols())
5914 cur.idx() = tabular.cellIndex(row, sel_col_start);
5917 cur.selection(false);
5920 case Tabular::COPY_ROW:
5921 tabular.copyRow(row);
5924 case Tabular::COPY_COLUMN:
5925 tabular.copyColumn(column);
5926 cur.idx() = tabular.cellIndex(row, column);
5929 case Tabular::MOVE_COLUMN_RIGHT:
5930 tabular.moveColumn(column, Tabular::RIGHT);
5931 cur.idx() = tabular.cellIndex(row, column + 1);
5934 case Tabular::MOVE_COLUMN_LEFT:
5935 tabular.moveColumn(column, Tabular::LEFT);
5936 cur.idx() = tabular.cellIndex(row, column - 1);
5939 case Tabular::MOVE_ROW_DOWN:
5940 tabular.moveRow(row, Tabular::DOWN);
5941 cur.idx() = tabular.cellIndex(row + 1, column);
5944 case Tabular::MOVE_ROW_UP:
5945 tabular.moveRow(row, Tabular::UP);
5946 cur.idx() = tabular.cellIndex(row - 1, column);
5949 case Tabular::SET_LINE_TOP:
5950 case Tabular::TOGGLE_LINE_TOP: {
5951 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5952 ? (value == "true") : !tabular.topLine(cur.idx());
5953 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5954 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5955 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5959 case Tabular::SET_LINE_BOTTOM:
5960 case Tabular::TOGGLE_LINE_BOTTOM: {
5961 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5962 ? (value == "true") : !tabular.bottomLine(cur.idx());
5963 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5964 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5965 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5969 case Tabular::SET_LINE_LEFT:
5970 case Tabular::TOGGLE_LINE_LEFT: {
5971 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5972 ? (value == "true") : !tabular.leftLine(cur.idx());
5973 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5974 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5975 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5979 case Tabular::SET_LINE_RIGHT:
5980 case Tabular::TOGGLE_LINE_RIGHT: {
5981 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5982 ? (value == "true") : !tabular.rightLine(cur.idx());
5983 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5984 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5985 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5989 case Tabular::M_ALIGN_LEFT:
5990 case Tabular::M_ALIGN_RIGHT:
5991 case Tabular::M_ALIGN_CENTER:
5992 case Tabular::ALIGN_LEFT:
5993 case Tabular::ALIGN_RIGHT:
5994 case Tabular::ALIGN_CENTER:
5995 case Tabular::ALIGN_BLOCK:
5996 case Tabular::ALIGN_DECIMAL:
5997 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5998 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5999 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6000 !tabular.getPWidth(c).zero());
6003 case Tabular::M_VALIGN_TOP:
6004 case Tabular::M_VALIGN_BOTTOM:
6005 case Tabular::M_VALIGN_MIDDLE:
6008 case Tabular::VALIGN_TOP:
6009 case Tabular::VALIGN_BOTTOM:
6010 case Tabular::VALIGN_MIDDLE:
6011 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6012 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6013 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6016 case Tabular::SET_MULTICOLUMN: {
6017 if (!cur.selection()) {
6018 // just multicol for one single cell
6019 // check whether we are completely in a multicol
6020 if (!tabular.isMultiColumn(cur.idx()))
6021 tabular.setMultiColumn(cur, cur.idx(), 1,
6022 tabular.rightLine(cur.idx()));
6025 // we have a selection so this means we just add all these
6026 // cells to form a multicolumn cell
6027 idx_type const s_start = cur.selBegin().idx();
6028 row_type const col_start = tabular.cellColumn(s_start);
6029 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6030 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6031 tabular.rightLine(cur.selEnd().idx()));
6034 cur.selection(false);
6038 case Tabular::UNSET_MULTICOLUMN: {
6039 if (!cur.selection()) {
6040 if (tabular.isMultiColumn(cur.idx()))
6041 tabular.unsetMultiColumn(cur.idx());
6046 case Tabular::MULTICOLUMN: {
6047 if (!cur.selection()) {
6048 if (tabular.isMultiColumn(cur.idx()))
6049 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6051 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6055 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6056 row_type const r = sel_row_start;
6057 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6058 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6061 // If the selection contains at least one singlecol cell
6062 // or multiple multicol cells,
6063 // we assume the user will merge is to a single multicol
6065 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6067 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6071 case Tabular::SET_MULTIROW: {
6072 if (!cur.selection()) {
6073 // just multirow for one single cell
6074 // check whether we are completely in a multirow
6075 if (!tabular.isMultiRow(cur.idx()))
6076 tabular.setMultiRow(cur, cur.idx(), 1,
6077 tabular.bottomLine(cur.idx()),
6078 tabular.getAlignment(cur.idx()));
6081 // we have a selection so this means we just add all this
6082 // cells to form a multirow cell
6083 idx_type const s_start = cur.selBegin().idx();
6084 row_type const row_start = tabular.cellRow(s_start);
6085 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6086 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6087 tabular.bottomLine(cur.selEnd().idx()),
6088 tabular.getAlignment(cur.selEnd().idx()));
6091 cur.selection(false);
6095 case Tabular::UNSET_MULTIROW: {
6096 if (!cur.selection()) {
6097 if (tabular.isMultiRow(cur.idx()))
6098 tabular.unsetMultiRow(cur.idx());
6103 case Tabular::MULTIROW: {
6104 if (!cur.selection()) {
6105 if (tabular.isMultiRow(cur.idx()))
6106 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6108 tabularFeatures(cur, Tabular::SET_MULTIROW);
6112 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6113 col_type const c = sel_col_start;
6114 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6115 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6118 // If the selection contains at least one singlerow cell
6119 // or multiple multirow cells,
6120 // we assume the user will merge is to a single multirow
6122 tabularFeatures(cur, Tabular::SET_MULTIROW);
6124 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6128 case Tabular::SET_ALL_LINES:
6131 case Tabular::UNSET_ALL_LINES:
6132 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6133 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6134 idx_type const cell = tabular.cellIndex(r, c);
6135 tabular.setTopLine(cell, setLines);
6136 tabular.setBottomLine(cell, setLines);
6137 tabular.setRightLine(cell, setLines);
6138 tabular.setLeftLine(cell, setLines);
6142 case Tabular::SET_BORDER_LINES:
6143 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6144 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6145 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6147 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6148 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6149 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6153 case Tabular::TOGGLE_LONGTABULAR:
6154 if (tabular.is_long_tabular)
6155 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6157 tabular.is_long_tabular = true;
6160 case Tabular::SET_LONGTABULAR:
6161 tabular.is_long_tabular = true;
6164 case Tabular::UNSET_LONGTABULAR:
6165 for (row_type r = 0; r < tabular.nrows(); ++r) {
6166 if (tabular.ltCaption(r)) {
6167 cur.idx() = tabular.cellIndex(r, 0);
6170 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6173 tabular.is_long_tabular = false;
6176 case Tabular::SET_ROTATE_TABULAR:
6177 tabular.rotate = convert<int>(value);
6180 case Tabular::UNSET_ROTATE_TABULAR:
6184 case Tabular::TOGGLE_ROTATE_TABULAR:
6185 // when pressing the rotate button we default to 90° rotation
6186 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6189 case Tabular::TABULAR_VALIGN_TOP:
6190 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6193 case Tabular::TABULAR_VALIGN_MIDDLE:
6194 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6197 case Tabular::TABULAR_VALIGN_BOTTOM:
6198 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6201 case Tabular::LONGTABULAR_ALIGN_LEFT:
6202 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6205 case Tabular::LONGTABULAR_ALIGN_CENTER:
6206 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6209 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6210 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6215 case Tabular::SET_ROTATE_CELL:
6216 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6217 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6218 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6221 case Tabular::UNSET_ROTATE_CELL:
6222 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6223 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6224 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6227 case Tabular::TOGGLE_ROTATE_CELL:
6229 bool oneNotRotated = oneCellHasRotationState(false,
6230 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6232 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6233 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6234 // when pressing the rotate cell button we default to 90° rotation
6236 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6238 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6243 case Tabular::SET_USEBOX: {
6244 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6245 if (val == tabular.getUsebox(cur.idx()))
6246 val = Tabular::BOX_NONE;
6247 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6248 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6249 tabular.setUsebox(tabular.cellIndex(r, c), val);
6253 case Tabular::UNSET_LTFIRSTHEAD:
6256 case Tabular::SET_LTFIRSTHEAD:
6257 tabular.getRowOfLTFirstHead(row, ltt);
6258 checkLongtableSpecial(ltt, value, flag);
6259 tabular.setLTHead(row, flag, ltt, true);
6262 case Tabular::UNSET_LTHEAD:
6265 case Tabular::SET_LTHEAD:
6266 tabular.getRowOfLTHead(row, ltt);
6267 checkLongtableSpecial(ltt, value, flag);
6268 tabular.setLTHead(row, flag, ltt, false);
6271 case Tabular::UNSET_LTFOOT:
6274 case Tabular::SET_LTFOOT:
6275 tabular.getRowOfLTFoot(row, ltt);
6276 checkLongtableSpecial(ltt, value, flag);
6277 tabular.setLTFoot(row, flag, ltt, false);
6280 case Tabular::UNSET_LTLASTFOOT:
6283 case Tabular::SET_LTLASTFOOT:
6284 tabular.getRowOfLTLastFoot(row, ltt);
6285 checkLongtableSpecial(ltt, value, flag);
6286 tabular.setLTFoot(row, flag, ltt, true);
6289 case Tabular::UNSET_LTNEWPAGE:
6292 case Tabular::SET_LTNEWPAGE:
6293 tabular.setLTNewPage(row, flag);
6296 case Tabular::SET_LTCAPTION: {
6297 if (tabular.ltCaption(row))
6299 cur.idx() = tabular.setLTCaption(cur, row, true);
6302 cur.selection(false);
6303 // If a row is set as caption, then also insert
6304 // a caption. Otherwise the LaTeX output is broken.
6305 // Select cell if it is non-empty
6306 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6307 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6308 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6312 case Tabular::UNSET_LTCAPTION: {
6313 if (!tabular.ltCaption(row))
6315 cur.idx() = tabular.setLTCaption(cur, row, false);
6318 cur.selection(false);
6319 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6320 if (lyx::getStatus(fr).enabled())
6325 case Tabular::TOGGLE_LTCAPTION: {
6326 if (tabular.ltCaption(row))
6327 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6329 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6333 case Tabular::TOGGLE_BOOKTABS:
6334 tabular.use_booktabs = !tabular.use_booktabs;
6337 case Tabular::SET_BOOKTABS:
6338 tabular.use_booktabs = true;
6341 case Tabular::UNSET_BOOKTABS:
6342 tabular.use_booktabs = false;
6345 case Tabular::SET_TOP_SPACE: {
6347 if (value == "default")
6348 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6349 tabular.row_info[r].top_space_default = true;
6350 else if (value == "none")
6351 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6352 tabular.row_info[r].top_space_default = false;
6353 tabular.row_info[r].top_space = len;
6355 else if (isValidLength(value, &len))
6356 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6357 tabular.row_info[r].top_space_default = false;
6358 tabular.row_info[r].top_space = len;
6363 case Tabular::SET_BOTTOM_SPACE: {
6365 if (value == "default")
6366 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6367 tabular.row_info[r].bottom_space_default = true;
6368 else if (value == "none")
6369 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6370 tabular.row_info[r].bottom_space_default = false;
6371 tabular.row_info[r].bottom_space = len;
6373 else if (isValidLength(value, &len))
6374 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6375 tabular.row_info[r].bottom_space_default = false;
6376 tabular.row_info[r].bottom_space = len;
6381 case Tabular::SET_INTERLINE_SPACE: {
6383 if (value == "default")
6384 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6385 tabular.row_info[r].interline_space_default = true;
6386 else if (value == "none")
6387 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6388 tabular.row_info[r].interline_space_default = false;
6389 tabular.row_info[r].interline_space = len;
6391 else if (isValidLength(value, &len))
6392 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6393 tabular.row_info[r].interline_space_default = false;
6394 tabular.row_info[r].interline_space = len;
6399 case Tabular::SET_DECIMAL_POINT:
6400 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6401 tabular.column_info[c].decimal_point = from_utf8(value);
6404 // dummy stuff just to avoid warnings
6405 case Tabular::LAST_ACTION:
6411 bool InsetTabular::copySelection(Cursor & cur)
6413 if (!cur.selection())
6418 getSelection(cur, rs, re, cs, ce);
6420 paste_tabular.reset(new Tabular(tabular));
6422 for (row_type r = 0; r < rs; ++r)
6423 paste_tabular->deleteRow(0);
6425 row_type const rows = re - rs + 1;
6426 while (paste_tabular->nrows() > rows)
6427 paste_tabular->deleteRow(rows);
6429 for (col_type c = 0; c < cs; ++c)
6430 paste_tabular->deleteColumn(0);
6432 col_type const columns = ce - cs + 1;
6433 while (paste_tabular->ncols() > columns)
6434 paste_tabular->deleteColumn(columns);
6436 paste_tabular->setBuffer(tabular.buffer());
6438 odocstringstream os;
6439 OutputParams const runparams(0);
6440 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6441 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6442 cap::copySelection(cur, os.str());
6444 // mark tabular stack dirty
6445 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6446 // when we (hopefully) have a one-for-all paste mechanism.
6447 // This must be called after cap::copySelection.
6448 dirtyTabularStack(true);
6454 bool InsetTabular::pasteClipboard(Cursor & cur)
6458 col_type actcol = tabular.cellColumn(cur.idx());
6459 row_type actrow = tabular.cellRow(cur.idx());
6461 if (cur.selIsMultiCell()) {
6464 getSelection(cur, actrow, re, actcol, ce);
6467 for (row_type r1 = 0, r2 = actrow;
6468 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6470 for (col_type c1 = 0, c2 = actcol;
6471 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6473 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6474 tabular.isPartOfMultiColumn(r2, c2))
6476 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6480 if (tabular.isPartOfMultiColumn(r2, c2)) {
6484 shared_ptr<InsetTableCell> inset(
6485 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6486 tabular.setCellInset(r2, c2, inset);
6487 // FIXME?: why do we need to do this explicitly? (EL)
6488 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6490 // FIXME: change tracking (MG)
6491 inset->setChange(Change(buffer().params().track_changes ?
6492 Change::INSERTED : Change::UNCHANGED));
6501 void InsetTabular::cutSelection(Cursor & cur)
6503 if (!cur.selection())
6508 getSelection(cur, rs, re, cs, ce);
6509 for (row_type r = rs; r <= re; ++r) {
6510 for (col_type c = cs; c <= ce; ++c) {
6511 shared_ptr<InsetTableCell> t
6512 = cell(tabular.cellIndex(r, c));
6513 if (buffer().params().track_changes)
6514 // FIXME: Change tracking (MG)
6515 t->setChange(Change(Change::DELETED));
6521 // cursor position might be invalid now
6522 if (cur.pit() > cur.lastpit())
6523 cur.pit() = cur.lastpit();
6524 if (cur.pos() > cur.lastpos())
6525 cur.pos() = cur.lastpos();
6526 cur.clearSelection();
6530 bool InsetTabular::isRightToLeft(Cursor & cur) const
6532 // LASSERT: It might be better to abandon this Buffer.
6533 LASSERT(cur.depth() > 1, return false);
6534 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6535 pos_type const parentpos = cur[cur.depth() - 2].pos();
6536 return parentpar.getFontSettings(buffer().params(),
6537 parentpos).language()->rightToLeft();
6541 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6544 LASSERT(stidx <= enidx, return docstring());
6546 col_type const col1 = tabular.cellColumn(stidx);
6547 col_type const col2 = tabular.cellColumn(enidx);
6548 row_type const row1 = tabular.cellRow(stidx);
6549 row_type const row2 = tabular.cellRow(enidx);
6551 for (col_type col = col1; col <= col2; col++)
6552 for (row_type row = row1; row <= row2; row++) {
6557 retval += tabular.cellInset(row, col)->asString(intoInsets);
6563 void InsetTabular::getSelection(Cursor & cur,
6564 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6566 CursorSlice const & beg = cur.selBegin();
6567 CursorSlice const & end = cur.selEnd();
6568 cs = tabular.cellColumn(beg.idx());
6569 ce = tabular.cellColumn(end.idx());
6573 rs = tabular.cellRow(beg.idx());
6574 re = tabular.cellRow(end.idx());
6580 Text * InsetTabular::getText(int idx) const
6582 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6586 void InsetTabular::setChange(Change const & change)
6588 for (idx_type idx = 0; idx < nargs(); ++idx)
6589 cell(idx)->setChange(change);
6593 void InsetTabular::acceptChanges()
6595 for (idx_type idx = 0; idx < nargs(); ++idx)
6596 cell(idx)->acceptChanges();
6600 void InsetTabular::rejectChanges()
6602 for (idx_type idx = 0; idx < nargs(); ++idx)
6603 cell(idx)->rejectChanges();
6607 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6609 return tabular.getPWidth(cell).zero();
6613 bool InsetTabular::forcePlainLayout(idx_type cell) const
6615 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
6619 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6622 if (buf.length() <= 0)
6627 col_type maxCols = 1;
6628 size_t const len = buf.length();
6632 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6640 maxCols = max(cols, maxCols);
6646 maxCols = max(cols, maxCols);
6652 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6653 loctab = paste_tabular.get();
6654 dirtyTabularStack(true);
6657 cell = bv.cursor().idx();
6658 ocol = tabular.cellColumn(cell);
6659 row = tabular.cellRow(cell);
6663 idx_type const cells = loctab->numberofcells;
6666 rows = loctab->nrows();
6667 col_type const columns = loctab->ncols();
6669 while (cell < cells && p < len && row < rows &&
6670 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6676 // we can only set this if we are not too far right
6677 if (cols < columns) {
6678 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6679 Font const font = bv.textMetrics(&inset->text()).
6681 inset->setText(buf.substr(op, p - op), font,
6682 buffer().params().track_changes);
6688 // we can only set this if we are not too far right
6689 if (cols < columns) {
6690 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6691 Font const font = bv.textMetrics(&inset->text()).
6693 inset->setText(buf.substr(op, p - op), font,
6694 buffer().params().track_changes);
6699 cell = loctab->cellIndex(row, cols);
6705 // check for the last cell if there is no trailing '\n'
6706 if (cell < cells && op < len) {
6707 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6708 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6709 inset->setText(buf.substr(op, len - op), font,
6710 buffer().params().track_changes);
6716 void InsetTabular::addPreview(DocIterator const & inset_pos,
6717 PreviewLoader & loader) const
6719 DocIterator cell_pos = inset_pos;
6721 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6722 for (row_type r = 0; r < tabular.nrows(); ++r) {
6723 for (col_type c = 0; c < tabular.ncols(); ++c) {
6724 cell_pos.top().idx() = tabular.cellIndex(r, c);
6725 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6731 bool InsetTabular::completionSupported(Cursor const & cur) const
6733 Cursor const & bvCur = cur.bv().cursor();
6734 if (&bvCur.inset() != this)
6736 return cur.text()->completionSupported(cur);
6740 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6742 return completionSupported(cur);
6746 bool InsetTabular::automaticInlineCompletion() const
6748 return lyxrc.completion_inline_text;
6752 bool InsetTabular::automaticPopupCompletion() const
6754 return lyxrc.completion_popup_text;
6758 bool InsetTabular::showCompletionCursor() const
6760 return lyxrc.completion_cursor_text;
6764 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6766 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6770 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6772 if (!completionSupported(cur))
6774 return cur.text()->completionPrefix(cur);
6778 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6780 if (!completionSupported(cur))
6783 return cur.text()->insertCompletion(cur, s, finished);
6787 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6788 Dimension & dim) const
6790 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6791 tm.completionPosAndDim(cur, x, y, dim);
6795 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6797 istringstream data(in);
6799 lex.setStream(data);
6806 if (!lex || token != "tabular") {
6807 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6811 // This is part of the inset proper that is usually swallowed
6812 // by Buffer::readInset
6814 if (!lex || token != "Tabular") {
6815 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6823 string InsetTabular::params2string(InsetTabular const & inset)
6826 data << "tabular" << ' ';
6828 data << "\\end_inset\n";
6833 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6835 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6836 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6837 if (!tabular.isPartOfMultiColumn(r,c) &&
6838 !tabular.isPartOfMultiRow(r,c))
6841 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6842 ParagraphList::iterator it = parlist.begin();
6843 ParagraphList::iterator const en = parlist.end();
6844 for (; it != en; ++it)
6845 it->setLayout(dc.plainLayout());