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;
1040 for(col_type c = 0; c < ncols(); ++c)
1041 for(row_type r = 0; r < nrows(); ++r) {
1042 idx_type const i = cellIndex(r, c);
1043 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1044 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1045 if (!getPWidth(i).zero())
1046 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1047 else if (!column_info[c].varwidth)
1048 max_width[c] = max(max_width[c], cell_info[r][c].width);
1051 // If we have a fixed tabular width, we take this into account
1052 Length tab_width = tabular_width;
1053 bool const tabularx = hasVarwidthColumn();
1054 if (tabularx && tab_width.zero())
1055 // If no tabular width is specified with X columns,
1056 // we use 100% colwidth
1057 tab_width = Length(100, Length::PCW);
1059 if (!tab_width.zero()) {
1060 restwidth = mi.base.inPixels(tab_width);
1061 // Substract the fixed widths from the table width
1062 for (auto const w : max_pwidth)
1063 restwidth -= w.second;
1066 // If we have a fixed width, distribute the available table width
1067 // (minus the fixed widths) to the variable-width columns
1069 int restcols = ncols() - max_pwidth.size();
1071 vcolwidth = restwidth / restcols;
1073 // Now consider that some variable width columns exceed the vcolwidth
1074 if (vcolwidth > 0) {
1075 bool changed = false;
1076 for (auto const w : max_width) {
1077 if (tabularx || w.second > vcolwidth) {
1079 restwidth -= w.second;
1083 if (changed && restwidth > 0)
1084 vcolwidth = restwidth / restcols;
1087 bool update = false;
1088 // for each col get max of single col cells
1089 for(col_type c = 0; c < ncols(); ++c) {
1091 for(row_type r = 0; r < nrows(); ++r) {
1092 idx_type const i = cellIndex(r, c);
1093 if (columnSpan(i) == 1) {
1094 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1095 && cell_info[r][c].decimal_width != 0)
1096 new_width = max(new_width, cellInfo(i).width
1097 + max_dwidth[c] - cellInfo(i).decimal_width);
1098 else if (getPWidth(i).zero() && vcolwidth > 0) {
1099 if (tabularx && !column_info[c].varwidth)
1100 new_width = max(new_width, cellInfo(i).width);
1102 new_width = vcolwidth;
1104 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1106 new_width = max(new_width, cellInfo(i).width);
1110 if (column_info[c].width != new_width) {
1111 column_info[c].width = new_width;
1112 // Do not trigger update when no space is left for variable
1113 // columns, as this will loop
1114 update = tab_width.zero() || restwidth > 0;
1117 // update col widths to fit merged cells
1118 for(col_type c = 0; c < ncols(); ++c)
1119 for(row_type r = 0; r < nrows(); ++r) {
1120 idx_type const i = cellIndex(r, c);
1121 int const span = columnSpan(i);
1122 if (span == 1 || c > cellColumn(i))
1126 for(col_type j = c; j < c + span ; ++j)
1127 old_width += column_info[j].width;
1129 if (cellInfo(i).width > old_width) {
1130 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1131 // Do not trigger update when no space is left for variable
1132 // columns, as this will loop
1133 update = tab_width.zero() || restwidth > 0;
1141 int Tabular::width() const
1144 for (col_type c = 0; c < ncols(); ++c)
1145 width += column_info[c].width;
1150 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1153 col_type const col = cellColumn(cell);
1154 // set alignment for the whole row if we are not in a multicolumn cell,
1155 // exclude possible multicolumn cells in the row
1156 if (!isMultiColumn(cell)) {
1157 for (row_type r = 0; r < nrows(); ++r) {
1158 // only if the column has no width the multirow inherits the
1159 // alignment of the column, otherwise it is left aligned
1160 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1161 && !isMultiColumn(cellIndex(r, col))) {
1162 cell_info[r][col].alignment = align;
1163 cell_info[r][col].inset->setContentAlignment(align);
1165 if ((isMultiRow(cellIndex(r, col)) && has_width)
1166 && !isMultiColumn(cellIndex(r, col))) {
1167 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1168 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1171 column_info[col].alignment = align;
1172 docstring & dpoint = column_info[col].decimal_point;
1173 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1174 dpoint = from_utf8(lyxrc.default_decimal_point);
1176 cellInfo(cell).alignment = align;
1177 cellInset(cell)->setContentAlignment(align);
1182 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1185 if (!isMultiColumn(cell) || onlycolumn)
1186 column_info[cellColumn(cell)].valignment = align;
1188 cellInfo(cell).valignment = align;
1195 * Allow line and paragraph breaks for fixed width multicol/multirow cells
1196 * or disallow them, merge cell paragraphs and reset layout to standard
1197 * for variable width multicol cells.
1199 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1200 bool const fixedWidth, bool const multicol,
1201 bool const multirow)
1203 inset->toggleFixedWidth(fixedWidth);
1204 if (!multirow && (fixedWidth || !multicol))
1207 // merge all paragraphs to one
1208 BufferParams const & bp = cur.bv().buffer().params();
1209 while (inset->paragraphs().size() > 1)
1210 mergeParagraph(bp, inset->paragraphs(), 0);
1212 // This is relevant for multirows
1217 ParagraphList::iterator pit = inset->paragraphs().begin();
1218 for (; pit != inset->paragraphs().end(); ++pit) {
1219 for (pos_type j = 0; j != pit->size(); ++j) {
1220 if (pit->isNewline(j))
1221 pit->eraseChar(j, bp.track_changes);
1227 // undo information has already been recorded
1228 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1229 bp.documentClass().plainLayoutName());
1236 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1237 Length const & width)
1239 col_type const c = cellColumn(cell);
1241 column_info[c].p_width = width;
1242 // reset the vertical alignment to top if the fixed width
1243 // is removed or zero because only fixed width columns can
1244 // have a vertical alignment
1245 if (column_info[c].p_width.zero())
1246 column_info[c].valignment = LYX_VALIGN_TOP;
1247 for (row_type r = 0; r < nrows(); ++r) {
1248 idx_type const cidx = cellIndex(r, c);
1249 // because of multicolumns
1250 toggleFixedWidth(cur, cellInset(cidx).get(),
1251 !getPWidth(cidx).zero(), isMultiColumn(cidx),
1253 if (isMultiRow(cidx))
1254 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1256 // cur can become invalid after paragraphs were merged
1261 bool Tabular::setFixedWidth(row_type r, col_type c)
1263 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1264 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1265 || (multicol && !cell_info[r][c].p_width.zero());
1266 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1271 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1272 Length const & width)
1274 if (!isMultiColumn(cell))
1277 cellInfo(cell).p_width = width;
1278 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1279 isMultiColumn(cell), isMultiRow(cell));
1280 // cur can become invalid after paragraphs were merged
1286 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1288 column_info[cellColumn(cell)].varwidth = varwidth;
1293 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1295 cellInfo(cell).mroffset = mroffset;
1300 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1301 Tabular::Feature what)
1303 if (what == SET_SPECIAL_MULTICOLUMN)
1304 cellInfo(cell).align_special = special;
1306 column_info[cellColumn(cell)].align_special = special;
1310 void Tabular::setTopLine(idx_type i, bool line)
1312 cellInfo(i).top_line = line;
1316 void Tabular::setBottomLine(idx_type i, bool line)
1318 cellInfo(i).bottom_line = line;
1322 void Tabular::setLeftLine(idx_type cell, bool line)
1324 cellInfo(cell).left_line = line;
1328 void Tabular::setRightLine(idx_type cell, bool line)
1330 cellInfo(cell).right_line = line;
1333 bool Tabular::rowTopLine(row_type r) const
1335 bool all_rows_set = true;
1336 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1337 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1338 return all_rows_set;
1342 bool Tabular::rowBottomLine(row_type r) const
1344 bool all_rows_set = true;
1345 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1346 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1347 return all_rows_set;
1351 bool Tabular::columnLeftLine(col_type c) const
1358 for (row_type r = 0; r < nrows(); ++r) {
1359 idx_type const i = cellIndex(r, c);
1360 if (c == cellColumn(i)) {
1362 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1363 if (cellInfo(i).left_line || right)
1367 return 2 * nrows_left >= total;
1371 bool Tabular::columnRightLine(col_type c) const
1376 int nrows_right = 0;
1378 for (row_type r = 0; r < nrows(); ++r) {
1379 idx_type i = cellIndex(r, c);
1380 if (c == cellColumn(i) + columnSpan(i) - 1) {
1382 bool left = (c + 1 < ncols()
1383 && cellInfo(cellIndex(r, c + 1)).left_line)
1384 || c + 1 == ncols();
1385 if (cellInfo(i).right_line && left)
1389 return 2 * nrows_right >= total;
1393 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1395 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1396 return cellInfo(cell).alignment;
1398 return column_info[cellColumn(cell)].alignment;
1403 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1405 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1406 return cellInfo(cell).valignment;
1407 return column_info[cellColumn(cell)].valignment;
1411 int Tabular::offsetVAlignment() const
1413 // for top-alignment the first horizontal table line must be exactly at
1414 // the position of the base line of the surrounding text line
1415 // for bottom alignment, the same is for the last table line
1416 int offset_valign = 0;
1417 switch (tabular_valignment) {
1418 case Tabular::LYX_VALIGN_BOTTOM:
1419 offset_valign = rowAscent(0) - height();
1421 case Tabular::LYX_VALIGN_MIDDLE:
1422 offset_valign = (- height()) / 2 + rowAscent(0);
1424 case Tabular::LYX_VALIGN_TOP:
1425 offset_valign = rowAscent(0);
1428 return offset_valign;
1432 Length const Tabular::getPWidth(idx_type cell) const
1434 if (isMultiColumn(cell))
1435 return cellInfo(cell).p_width;
1436 return column_info[cellColumn(cell)].p_width;
1440 Length const Tabular::getMROffset(idx_type cell) const
1442 return cellInfo(cell).mroffset;
1446 int Tabular::textHOffset(idx_type cell) const
1448 // the LaTeX Way :-(
1449 int x = WIDTH_OF_LINE;
1451 int const w = cellWidth(cell) - cellInfo(cell).width;
1453 switch (getAlignment(cell)) {
1454 case LYX_ALIGN_CENTER:
1457 case LYX_ALIGN_RIGHT:
1460 case LYX_ALIGN_DECIMAL: {
1461 // we center when no decimal point
1462 if (cellInfo(cell).decimal_width == 0) {
1466 col_type const c = cellColumn(cell);
1467 int max_dhoffset = 0;
1468 for(row_type r = 0; r < row_info.size() ; ++r) {
1469 idx_type const i = cellIndex(r, c);
1470 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1471 && cellInfo(i).decimal_width != 0)
1472 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1474 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1477 // LYX_ALIGN_LEFT: nothing :-)
1485 int Tabular::textVOffset(idx_type cell) const
1487 int voffset = cellInfo(cell).voffset;
1488 if (isMultiRow(cell)) {
1489 row_type const row = cellRow(cell);
1490 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1496 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1499 idx_type const numcells = numberOfCellsInRow(row);
1500 // we check against numcells to make sure we do not crash if all the
1501 // cells are multirow (bug #7535), but in that case our return value
1502 // is really invalid, i.e., it is NOT the first cell in the row. but
1503 // i do not know what to do here. (rgh)
1504 while (c < numcells - 1
1505 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1507 return cell_info[row][c].cellno;
1511 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1513 col_type c = ncols() - 1;
1514 // of course we check against 0 so we don't crash. but we have the same
1515 // problem as in the previous routine: if all the cells are part of a
1516 // multirow or part of a multi column, then our return value is invalid.
1518 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1519 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1521 return cell_info[row][c].cellno;
1525 Tabular::row_type Tabular::cellRow(idx_type cell) const
1527 if (cell >= numberofcells)
1531 return rowofcell[cell];
1535 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1537 if (cell >= numberofcells)
1541 return columnofcell[cell];
1545 void Tabular::write(ostream & os) const
1549 << write_attribute("version", 3)
1550 << write_attribute("rows", nrows())
1551 << write_attribute("columns", ncols())
1553 // global longtable options
1555 << write_attribute("rotate", rotate)
1556 << write_attribute("booktabs", use_booktabs)
1557 << write_attribute("islongtable", is_long_tabular)
1558 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1559 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1560 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1561 << write_attribute("headTopDL", endhead.topDL)
1562 << write_attribute("headBottomDL", endhead.bottomDL)
1563 << write_attribute("footTopDL", endfoot.topDL)
1564 << write_attribute("footBottomDL", endfoot.bottomDL)
1565 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1566 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1567 << write_attribute("lastFootEmpty", endlastfoot.empty);
1568 // longtables cannot be aligned vertically
1569 if (!is_long_tabular)
1570 os << write_attribute("tabularvalignment", tabular_valignment);
1571 os << write_attribute("tabularwidth", tabular_width);
1572 if (is_long_tabular)
1573 os << write_attribute("longtabularalignment", longtabular_alignment);
1575 for (col_type c = 0; c < ncols(); ++c) {
1577 << write_attribute("alignment", column_info[c].alignment);
1578 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1579 os << write_attribute("decimal_point", column_info[c].decimal_point);
1580 os << write_attribute("valignment", column_info[c].valignment)
1581 << write_attribute("width", column_info[c].p_width.asString())
1582 << write_attribute("varwidth", column_info[c].varwidth)
1583 << write_attribute("special", column_info[c].align_special)
1586 for (row_type r = 0; r < nrows(); ++r) {
1587 static const string def("default");
1589 if (row_info[r].top_space_default)
1590 os << write_attribute("topspace", def);
1592 os << write_attribute("topspace", row_info[r].top_space);
1593 if (row_info[r].bottom_space_default)
1594 os << write_attribute("bottomspace", def);
1596 os << write_attribute("bottomspace", row_info[r].bottom_space);
1597 if (row_info[r].interline_space_default)
1598 os << write_attribute("interlinespace", def);
1600 os << write_attribute("interlinespace", row_info[r].interline_space);
1601 os << write_attribute("endhead", row_info[r].endhead)
1602 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1603 << write_attribute("endfoot", row_info[r].endfoot)
1604 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1605 << write_attribute("newpage", row_info[r].newpage)
1606 << write_attribute("caption", row_info[r].caption)
1608 for (col_type c = 0; c < ncols(); ++c) {
1610 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1611 << write_attribute("multirow", cell_info[r][c].multirow)
1612 << write_attribute("mroffset", cell_info[r][c].mroffset)
1613 << write_attribute("alignment", cell_info[r][c].alignment)
1614 << write_attribute("valignment", cell_info[r][c].valignment)
1615 << write_attribute("topline", cell_info[r][c].top_line)
1616 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1617 << write_attribute("leftline", cell_info[r][c].left_line)
1618 << write_attribute("rightline", cell_info[r][c].right_line)
1619 << write_attribute("rotate", cell_info[r][c].rotate)
1620 << write_attribute("usebox", cell_info[r][c].usebox)
1621 << write_attribute("width", cell_info[r][c].p_width)
1622 << write_attribute("special", cell_info[r][c].align_special)
1624 os << "\\begin_inset ";
1625 cell_info[r][c].inset->write(os);
1626 os << "\n\\end_inset\n"
1628 // FIXME This can be removed again once the mystery
1629 // crash has been resolved.
1634 os << "</lyxtabular>\n";
1638 void Tabular::read(Lexer & lex)
1641 istream & is = lex.getStream();
1643 l_getline(is, line);
1644 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1645 LASSERT(false, return);
1649 if (!getTokenValue(line, "version", version))
1651 LATTEST(version >= 2);
1654 if (!getTokenValue(line, "rows", rows_arg))
1657 if (!getTokenValue(line, "columns", columns_arg))
1659 init(buffer_, rows_arg, columns_arg);
1660 l_getline(is, line);
1661 if (!prefixIs(line, "<features")) {
1662 lyxerr << "Wrong tabular format (expected <features ...> got"
1663 << line << ')' << endl;
1666 getTokenValue(line, "rotate", rotate);
1667 getTokenValue(line, "booktabs", use_booktabs);
1668 getTokenValue(line, "islongtable", is_long_tabular);
1669 getTokenValue(line, "tabularvalignment", tabular_valignment);
1670 getTokenValue(line, "tabularwidth", tabular_width);
1671 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1672 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1673 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1674 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1675 getTokenValue(line, "headTopDL", endhead.topDL);
1676 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1677 getTokenValue(line, "footTopDL", endfoot.topDL);
1678 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1679 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1680 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1681 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1683 for (col_type c = 0; c < ncols(); ++c) {
1685 if (!prefixIs(line,"<column")) {
1686 lyxerr << "Wrong tabular format (expected <column ...> got"
1687 << line << ')' << endl;
1690 getTokenValue(line, "alignment", column_info[c].alignment);
1691 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1692 getTokenValue(line, "valignment", column_info[c].valignment);
1693 getTokenValue(line, "width", column_info[c].p_width);
1694 getTokenValue(line, "special", column_info[c].align_special);
1695 getTokenValue(line, "varwidth", column_info[c].varwidth);
1698 for (row_type i = 0; i < nrows(); ++i) {
1699 l_getline(is, line);
1700 if (!prefixIs(line, "<row")) {
1701 lyxerr << "Wrong tabular format (expected <row ...> got"
1702 << line << ')' << endl;
1705 getTokenValue(line, "topspace", row_info[i].top_space,
1706 row_info[i].top_space_default);
1707 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1708 row_info[i].bottom_space_default);
1709 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1710 row_info[i].interline_space_default);
1711 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1712 getTokenValue(line, "endhead", row_info[i].endhead);
1713 getTokenValue(line, "endfoot", row_info[i].endfoot);
1714 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1715 getTokenValue(line, "newpage", row_info[i].newpage);
1716 getTokenValue(line, "caption", row_info[i].caption);
1717 for (col_type j = 0; j < ncols(); ++j) {
1718 l_getline(is, line);
1719 if (!prefixIs(line, "<cell")) {
1720 lyxerr << "Wrong tabular format (expected <cell ...> got"
1721 << line << ')' << endl;
1724 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1725 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1726 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1727 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1728 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1729 getTokenValue(line, "topline", cell_info[i][j].top_line);
1730 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1731 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1732 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1733 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1734 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1735 getTokenValue(line, "width", cell_info[i][j].p_width);
1737 getTokenValue(line, "special", cell_info[i][j].align_special);
1738 l_getline(is, line);
1739 if (prefixIs(line, "\\begin_inset")) {
1740 cell_info[i][j].inset->setBuffer(*buffer_);
1741 cell_info[i][j].inset->read(lex);
1742 l_getline(is, line);
1744 if (!prefixIs(line, "</cell>")) {
1745 lyxerr << "Wrong tabular format (expected </cell> got"
1746 << line << ')' << endl;
1750 l_getline(is, line);
1751 if (!prefixIs(line, "</row>")) {
1752 lyxerr << "Wrong tabular format (expected </row> got"
1753 << line << ')' << endl;
1757 while (!prefixIs(line, "</lyxtabular>")) {
1758 l_getline(is, line);
1764 bool Tabular::isMultiColumn(idx_type cell) const
1766 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1767 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1771 bool Tabular::hasMultiColumn(col_type c) const
1773 for (row_type r = 0; r < nrows(); ++r) {
1774 if (isMultiColumn(cellIndex(r, c)))
1781 bool Tabular::hasVarwidthColumn() const
1783 for (col_type c = 0; c < ncols(); ++c) {
1784 if (column_info[c].varwidth)
1791 bool Tabular::isVTypeColumn(col_type c) const
1793 for (row_type r = 0; r < nrows(); ++r) {
1794 idx_type idx = cellIndex(r, c);
1795 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1802 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1804 return cell_info[cellRow(cell)][cellColumn(cell)];
1808 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1810 return cell_info[cellRow(cell)][cellColumn(cell)];
1814 Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
1815 bool const right_border)
1817 idx_type const col = cellColumn(cell);
1818 idx_type const row = cellRow(cell);
1819 for (idx_type i = 0; i < number; ++i)
1820 unsetMultiRow(cellIndex(row, col + i));
1822 // unsetting of multirow may have invalidated cell index
1823 cell = cellIndex(row, col);
1824 CellData & cs = cellInfo(cell);
1825 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1826 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1827 cs.alignment = column_info[col].alignment;
1828 setRightLine(cell, right_border);
1829 // non-fixed width multicolumns cannot have multiple paragraphs
1830 if (getPWidth(cell).zero()) {
1831 toggleFixedWidth(cur, cellInset(cell).get(),
1832 !getPWidth(cell).zero(), isMultiColumn(cell),
1834 // cur can become invalid after paragraphs were merged
1838 idx_type lastcell = cellIndex(row, col + number - 1);
1839 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1840 CellData & cs1 = cellInfo(cell + i);
1841 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1842 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1850 bool Tabular::isMultiRow(idx_type cell) const
1852 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1853 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1856 bool Tabular::hasMultiRow(row_type r) const
1858 for (col_type c = 0; c < ncols(); ++c) {
1859 if (isMultiRow(cellIndex(r, c)))
1865 Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
1866 bool const bottom_border,
1867 LyXAlignment const halign)
1869 idx_type const col = cellColumn(cell);
1870 idx_type const row = cellRow(cell);
1871 for (idx_type i = 0; i < number; ++i)
1872 unsetMultiColumn(cellIndex(row + i, col));
1874 // unsetting of multirow may have invalidated cell index
1875 cell = cellIndex(row, col);
1876 CellData & cs = cellInfo(cell);
1877 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1878 cs.valignment = LYX_VALIGN_MIDDLE;
1879 // the horizontal alignment of multirow cells can only
1880 // be changed for the whole table row,
1881 // support changing this only for the multirow cell can be done via
1883 if (getPWidth(cell).zero())
1884 cs.alignment = halign;
1886 cs.alignment = LYX_ALIGN_LEFT;
1888 // Multirows cannot have multiple paragraphs
1889 if (getPWidth(cell).zero()) {
1890 toggleFixedWidth(cur, cellInset(cell).get(),
1891 !getPWidth(cell).zero(),
1892 isMultiColumn(cell), isMultiRow(cell));
1893 // cur can become invalid after paragraphs were merged
1897 // set the bottom line of the last selected cell
1898 setBottomLine(cell, bottom_border);
1900 for (idx_type i = 1; i < number; ++i) {
1901 CellData & cs1 = cell_info[row + i][col];
1902 cs1.multirow = CELL_PART_OF_MULTIROW;
1903 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1911 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1913 row_type const row = cellRow(cell);
1914 col_type const col = cellColumn(cell);
1916 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1923 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1925 col_type const column = cellColumn(cell);
1926 col_type row = cellRow(cell) + 1;
1927 while (row < nrows() && isPartOfMultiRow(row, column))
1930 return row - cellRow(cell);
1934 void Tabular::unsetMultiColumn(idx_type cell)
1936 if (!isMultiColumn(cell))
1939 row_type const row = cellRow(cell);
1940 col_type const col = cellColumn(cell);
1941 row_type const span = columnSpan(cell);
1942 for (col_type c = 0; c < span; ++c) {
1943 // in the table dialog the lines are set in every case
1944 // when unsetting a multicolumn this leads to an additional right
1945 // line for every cell that was part of the former multicolumn cell,
1946 // except if the cell is in the last column
1947 // therefore remove this line
1948 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1949 && (col + c) < (col + span - 1))
1950 cell_info[row][col + c].right_line = false;
1951 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1957 void Tabular::unsetMultiRow(idx_type cell)
1959 if (!isMultiRow(cell))
1962 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1963 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1964 row_type const row = cellRow(cell);
1965 col_type const col = cellColumn(cell);
1966 row_type const span = rowSpan(cell);
1967 for (row_type r = 0; r < span; ++r)
1968 cell_info[row + r][col].multirow = CELL_NORMAL;
1973 void Tabular::setRotateCell(idx_type cell, int value)
1975 cellInfo(cell).rotate = value;
1979 int Tabular::getRotateCell(idx_type cell) const
1981 return cellInfo(cell).rotate;
1985 bool Tabular::needRotating() const
1987 if (rotate && !is_long_tabular)
1989 for (row_type r = 0; r < nrows(); ++r)
1990 for (col_type c = 0; c < ncols(); ++c)
1991 if (cell_info[r][c].rotate != 0)
1997 bool Tabular::isLastCell(idx_type cell) const
1999 if (cell + 1 < numberofcells)
2005 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2007 if (cellRow(cell) == 0)
2010 col_type const col = cellColumn(cell);
2011 row_type r = cellRow(cell) - 1;
2012 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2015 return cell_info[r][col].cellno;
2019 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2021 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2022 if (nextrow < nrows())
2023 return cell_info[nextrow][cellColumn(cell)].cellno;
2028 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2030 LASSERT(column != npos && column < ncols(), column = 0);
2031 LASSERT(row != npos && row < nrows(), row = 0);
2032 return cell_info[row][column].cellno;
2036 void Tabular::setUsebox(idx_type cell, BoxType type)
2038 cellInfo(cell).usebox = type;
2042 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2044 if (getRotateCell(cell) == 0
2045 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2046 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2048 if (cellInfo(cell).usebox > 1)
2049 return cellInfo(cell).usebox;
2050 return useBox(cell);
2055 // This are functions used for the longtable support
2057 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2063 row_info[row].endfirsthead = flag;
2067 row_info[row].endhead = flag;
2072 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2075 hd.set = haveLTHead();
2076 return row_info[row].endhead;
2080 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2083 hd.set = haveLTFirstHead();
2084 return row_info[row].endfirsthead;
2088 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2094 row_info[row].endlastfoot = flag;
2098 row_info[row].endfoot = flag;
2103 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2106 fd.set = haveLTFoot();
2107 return row_info[row].endfoot;
2111 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2114 fd.set = haveLTLastFoot();
2115 return row_info[row].endlastfoot;
2119 void Tabular::setLTNewPage(row_type row, bool what)
2121 row_info[row].newpage = what;
2125 bool Tabular::getLTNewPage(row_type row) const
2127 return row_info[row].newpage;
2131 bool Tabular::haveLTHead(bool withcaptions) const
2133 if (!is_long_tabular)
2135 for (row_type i = 0; i < nrows(); ++i)
2136 if (row_info[i].endhead &&
2137 (withcaptions || !row_info[i].caption))
2143 bool Tabular::haveLTFirstHead(bool withcaptions) const
2145 if (!is_long_tabular || endfirsthead.empty)
2147 for (row_type r = 0; r < nrows(); ++r)
2148 if (row_info[r].endfirsthead &&
2149 (withcaptions || !row_info[r].caption))
2155 bool Tabular::haveLTFoot(bool withcaptions) const
2157 if (!is_long_tabular)
2159 for (row_type r = 0; r < nrows(); ++r)
2160 if (row_info[r].endfoot &&
2161 (withcaptions || !row_info[r].caption))
2167 bool Tabular::haveLTLastFoot(bool withcaptions) const
2169 if (!is_long_tabular || endlastfoot.empty)
2171 for (row_type r = 0; r < nrows(); ++r)
2172 if (row_info[r].endlastfoot &&
2173 (withcaptions || !row_info[r].caption))
2179 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2181 idx_type i = getFirstCellInRow(row);
2183 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2184 setTopLine(i, false);
2185 setBottomLine(i, false);
2186 setLeftLine(i, false);
2187 setRightLine(i, false);
2188 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2189 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2190 setLTHead(row, true, endfirsthead, true);
2191 row_info[row].endfirsthead = true;
2194 unsetMultiColumn(i);
2195 // When unsetting a caption row, also all existing
2196 // captions in this row must be dissolved.
2198 row_info[row].caption = what;
2203 bool Tabular::ltCaption(row_type row) const
2205 return row_info[row].caption;
2209 bool Tabular::haveLTCaption(CaptionType captiontype) const
2211 if (!is_long_tabular)
2213 for (row_type r = 0; r < nrows(); ++r) {
2214 if (row_info[r].caption) {
2215 switch (captiontype) {
2216 case CAPTION_FIRSTHEAD:
2217 if (row_info[r].endfirsthead)
2221 if (row_info[r].endhead)
2225 if (row_info[r].endfoot)
2228 case CAPTION_LASTFOOT:
2229 if (row_info[r].endlastfoot)
2241 // end longtable support functions
2243 void Tabular::setRowAscent(row_type row, int height)
2245 if (row >= nrows() || row_info[row].ascent == height)
2247 row_info[row].ascent = height;
2251 void Tabular::setRowDescent(row_type row, int height)
2253 if (row >= nrows() || row_info[row].descent == height)
2255 row_info[row].descent = height;
2259 int Tabular::rowAscent(row_type row) const
2261 LASSERT(row < nrows(), row = 0);
2262 return row_info[row].ascent;
2266 int Tabular::rowDescent(row_type row) const
2268 LASSERT(row < nrows(), row = 0);
2269 return row_info[row].descent;
2273 int Tabular::height() const
2276 for (row_type row = 0; row < nrows(); ++row)
2277 height += rowAscent(row) + rowDescent(row) +
2283 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2285 LASSERT(row < nrows(), return false);
2286 LASSERT(column < ncols(), return false);
2287 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2291 bool Tabular::isPartOfMultiRow(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].multirow == CELL_PART_OF_MULTIROW;
2299 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang,
2300 list<col_type> columns) const
2302 // we only output complete row lines and the 1st row here, the rest
2303 // is done in Tabular::TeXBottomHLine(...)
2305 // get for each column the topline (if any)
2306 map<col_type, bool> topline;
2308 for (auto const & c : columns) {
2309 topline[c] = topLine(cellIndex(row, c));
2310 // If cell is part of a multirow and not the first cell of the
2311 // multirow, no line must be drawn.
2313 if (isMultiRow(cellIndex(row, c))
2314 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2316 if (topline.find(c) != topline.end() && topline.find(c)->second)
2320 // do nothing if empty first row, or incomplete row line after
2321 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2324 // only output complete row lines and the 1st row's clines
2325 if (nset == ncols()) {
2327 os << (row == 0 ? "\\toprule " : "\\midrule ");
2331 } else if (row == 0) {
2332 for (auto & c : columns) {
2333 if (topline.find(c)->second) {
2334 col_type offset = 0;
2335 for (col_type j = 0 ; j < c; ++j)
2336 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2339 //babel makes the "-" character an active one, so we have to suppress this here
2340 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2341 if (lang == "slovak" || lang == "czech")
2342 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2343 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2345 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2347 col_type cstart = c;
2348 for ( ; c < ncols() && topline.find(c)->second; ++c) {}
2350 for (col_type j = cstart ; j < c ; ++j)
2351 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2354 os << c + offset << "} ";
2362 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang,
2363 list<col_type> columns) const
2365 // we output bottomlines of row r and the toplines of row r+1
2366 // if the latter do not span the whole tabular
2368 // get the bottomlines of row r, and toplines in next row
2369 bool lastrow = row == nrows() - 1;
2370 map<col_type, bool> bottomline, topline;
2371 bool nextrowset = true;
2372 for (auto const & c : columns) {
2373 bottomline[c] = bottomLine(cellIndex(row, c));
2374 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2375 // If cell is part of a multirow and not the last cell of the
2376 // multirow, no line must be drawn.
2378 if (isMultiRow(cellIndex(row, c))
2379 && isMultiRow(cellIndex(row + 1, c))
2380 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2381 bottomline[c] = false;
2384 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2387 // combine this row's bottom lines and next row's toplines if necessary
2389 for (auto const & c : columns) {
2391 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2392 if (bottomline.find(c)->second)
2396 // do nothing if empty, OR incomplete row line with a topline in next row
2397 if (nset == 0 || (nextrowset && nset != ncols()))
2400 if (nset == ncols()) {
2402 os << (lastrow ? "\\bottomrule" : "\\midrule");
2406 for (auto & c : columns) {
2407 if (bottomline.find(c)->second) {
2408 col_type offset = 0;
2409 for (col_type j = 0 ; j < c; ++j)
2410 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2413 //babel makes the "-" character an active one, so we have to suppress this here
2414 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2415 if (lang == "slovak" || lang == "czech")
2416 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2417 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2419 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2421 col_type cstart = c;
2422 for ( ; c < ncols() && bottomline.find(c)->second; ++c) {}
2424 for (col_type j = cstart ; j < c ; ++j)
2425 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2428 os << c + offset << "} ";
2436 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2437 bool & ismulticol, bool & ismultirow,
2438 bool const bidi) const
2440 row_type const r = cellRow(cell);
2441 if (is_long_tabular && row_info[r].caption)
2444 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2445 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2446 // figure out how to set the lines
2447 // we always set double lines to the right of the cell
2448 // or left in bidi RTL, respectively.
2449 col_type const c = cellColumn(cell);
2450 col_type const nextcol = c + columnSpan(cell);
2451 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2452 bool colright = columnRightLine(c);
2453 bool colleft = columnLeftLine(c);
2454 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2455 bool nextcellleft = nextcol < ncols()
2456 && leftLine(cellIndex(r, nextcol));
2457 bool coldouble = colright && nextcolleft;
2458 bool celldouble = rightLine(cell) && nextcellleft;
2460 ismulticol = (isMultiColumn(cell)
2461 || (c == 0 && colleft != leftLine(cell))
2462 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2463 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2464 || (coldouble != celldouble))
2467 // we center in multicol when no decimal point
2469 docstring const align_d = column_info[c].decimal_point;
2470 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2471 bool const nosep = !dit;
2472 ismulticol |= nosep;
2473 celldouble &= nosep;
2476 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2477 int latexcolspan = columnSpan(cell);
2478 for (col_type col = c; col < c + columnSpan(cell); ++col)
2479 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2483 os << "\\multicolumn{" << latexcolspan << "}{";
2484 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2485 || (!bidi && c == 0 && leftLine(cell))))
2487 if (bidi && celldouble)
2488 // add extra vertical line if we want a double one
2490 if (!cellInfo(cell).align_special.empty()) {
2491 os << cellInfo(cell).align_special;
2493 if (!getPWidth(cell).zero()) {
2495 case LYX_ALIGN_LEFT:
2496 os << ">{\\raggedright}";
2498 case LYX_ALIGN_RIGHT:
2499 os << ">{\\raggedleft}";
2501 case LYX_ALIGN_CENTER:
2502 os << ">{\\centering}";
2508 case LYX_VALIGN_TOP:
2511 case LYX_VALIGN_MIDDLE:
2514 case LYX_VALIGN_BOTTOM:
2519 << from_ascii(getPWidth(cell).asLatexString())
2523 case LYX_ALIGN_LEFT:
2526 case LYX_ALIGN_RIGHT:
2533 } // end if else !getPWidth
2534 } // end if else !cellinfo_of_cell
2535 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2537 if (!bidi && celldouble)
2538 // add extra vertical line if we want a double one
2541 } // end if ismulticol
2543 // we only need code for the first multirow cell
2544 ismultirow = isMultiRow(cell);
2546 os << "\\multirow{" << rowSpan(cell) << "}{";
2547 if (!getPWidth(cell).zero())
2548 os << from_ascii(getPWidth(cell).asLatexString());
2550 // we need to set a default value
2553 if (!getMROffset(cell).zero())
2554 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2556 } // end if ismultirow
2558 if (getRotateCell(cell) != 0)
2559 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2561 if (getUsebox(cell) == BOX_PARBOX) {
2564 case LYX_VALIGN_TOP:
2567 case LYX_VALIGN_MIDDLE:
2570 case LYX_VALIGN_BOTTOM:
2574 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2576 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2577 os << "\\begin{minipage}[";
2579 case LYX_VALIGN_TOP:
2582 case LYX_VALIGN_MIDDLE:
2585 case LYX_VALIGN_BOTTOM:
2589 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2591 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2592 os << "\\begin{varwidth}[";
2594 case LYX_VALIGN_TOP:
2597 case LYX_VALIGN_MIDDLE:
2600 case LYX_VALIGN_BOTTOM:
2604 os << "]{\\linewidth}\n";
2609 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2610 bool ismulticol, bool ismultirow) const
2612 row_type const r = cellRow(cell);
2613 if (is_long_tabular && row_info[r].caption)
2617 if (getUsebox(cell) == BOX_PARBOX)
2619 else if (getUsebox(cell) == BOX_MINIPAGE)
2620 os << breakln << "\\end{minipage}";
2621 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2622 os << breakln << "\\end{varwidth}";
2623 if (getRotateCell(cell) != 0)
2624 os << breakln << "\\end{turn}";
2632 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2633 OutputParams const & runparams,
2634 list<col_type> columns) const
2636 if (!is_long_tabular)
2640 // output caption which is in no header or footer
2641 if (haveLTCaption()) {
2642 for (row_type r = 0; r < nrows(); ++r) {
2643 if (row_info[r].caption &&
2644 !row_info[r].endfirsthead && !row_info[r].endhead &&
2645 !row_info[r].endfoot && !row_info[r].endlastfoot)
2646 TeXRow(os, r, runparams, columns);
2649 // output first header info
2650 if (haveLTFirstHead()) {
2651 if (endfirsthead.topDL)
2653 for (row_type r = 0; r < nrows(); ++r) {
2654 if (row_info[r].endfirsthead)
2655 TeXRow(os, r, runparams, columns);
2657 if (endfirsthead.bottomDL)
2659 os << "\\endfirsthead\n";
2661 // output header info
2663 if (endfirsthead.empty && !haveLTFirstHead())
2664 os << "\\endfirsthead\n";
2667 for (row_type r = 0; r < nrows(); ++r) {
2668 if (row_info[r].endhead)
2669 TeXRow(os, r, runparams, columns);
2671 if (endhead.bottomDL)
2673 os << "\\endhead\n";
2675 // output footer info
2679 for (row_type r = 0; r < nrows(); ++r) {
2680 if (row_info[r].endfoot)
2681 TeXRow(os, r, runparams, columns);
2683 if (endfoot.bottomDL)
2685 os << "\\endfoot\n";
2686 if (endlastfoot.empty && !haveLTLastFoot())
2687 os << "\\endlastfoot\n";
2689 // output lastfooter info
2690 if (haveLTLastFoot()) {
2691 if (endlastfoot.topDL)
2693 for (row_type r = 0; r < nrows(); ++r) {
2694 if (row_info[r].endlastfoot)
2695 TeXRow(os, r, runparams, columns);
2697 if (endlastfoot.bottomDL)
2699 os << "\\endlastfoot\n";
2704 bool Tabular::isValidRow(row_type row) const
2706 if (!is_long_tabular)
2708 return !row_info[row].endhead && !row_info[row].endfirsthead
2709 && !row_info[row].endfoot && !row_info[row].endlastfoot
2710 && !row_info[row].caption;
2714 void Tabular::TeXRow(otexstream & os, row_type row,
2715 OutputParams const & runparams,
2716 list<col_type> columns) const
2718 idx_type cell = cellIndex(row, 0);
2719 InsetTableCell const * cinset = cellInset(cell);
2720 Paragraph const & cpar = cinset->paragraphs().front();
2721 string const clang = cpar.getParLanguage(buffer().params())->lang();
2723 //output the top line
2724 TeXTopHLine(os, row, clang, columns);
2726 if (row_info[row].top_space_default) {
2728 os << "\\addlinespace\n";
2730 os << "\\noalign{\\vskip\\doublerulesep}\n";
2731 } else if(!row_info[row].top_space.zero()) {
2733 os << "\\addlinespace["
2734 << from_ascii(row_info[row].top_space.asLatexString())
2737 os << "\\noalign{\\vskip"
2738 << from_ascii(row_info[row].top_space.asLatexString())
2742 bool ismulticol = false;
2743 bool ismultirow = false;
2745 // The bidi package (loaded by polyglossia) reverses RTL table columns
2746 bool const bidi_rtl =
2747 runparams.local_font->isRightToLeft()
2748 && runparams.use_polyglossia;
2750 bidi_rtl ? getFirstCellInRow(row) : getLastCellInRow(row);
2752 for (auto const & c : columns) {
2753 if (isPartOfMultiColumn(row, c))
2756 cell = cellIndex(row, c);
2758 if (isPartOfMultiRow(row, c)
2759 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2760 if (cell != lastcell)
2765 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
2766 InsetTableCell const * inset = cellInset(cell);
2768 Paragraph const & par = inset->paragraphs().front();
2770 os.texrow().forceStart(par.id(), 0);
2772 bool rtl = par.isRTL(buffer().params())
2774 && getPWidth(cell).zero()
2775 && !runparams.use_polyglossia;
2779 par.getParLanguage(buffer().params())->lang();
2780 if (lang == "farsi")
2782 else if (lang == "arabic_arabi")
2784 // currently, remaining RTL languages are
2785 // arabic_arabtex and hebrew
2789 // pass to the OutputParams that we are in a cell and
2790 // which alignment we have set.
2791 // InsetNewline needs this context information.
2792 OutputParams newrp(runparams);
2793 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2794 ? OutputParams::PLAIN
2795 : OutputParams::ALIGNED;
2797 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2798 // copy cell and split in 2
2799 InsetTableCell head = InsetTableCell(*cellInset(cell));
2800 head.setBuffer(const_cast<Buffer &>(buffer()));
2801 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2803 dit.push_back(CursorSlice(head));
2804 head.setMacrocontextPositionRecursive(dit);
2805 bool hassep = false;
2806 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2808 tail.setBuffer(head.buffer());
2810 dit.push_back(CursorSlice(tail));
2811 tail.setMacrocontextPositionRecursive(dit);
2815 tail.latex(os, newrp);
2818 head.latex(os, newrp);
2820 head.latex(os, newrp);
2823 tail.latex(os, newrp);
2826 } else if (ltCaption(row)) {
2827 // Inside longtable caption rows, we must only output the caption inset
2828 // with its content and omit anything outside of that (see #10791)
2829 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
2830 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
2831 for (; it != i_end; ++it) {
2832 if (it->lyxCode() != CAPTION_CODE)
2834 it->latex(os, runparams);
2837 } else if (!isPartOfMultiRow(row, c)) {
2838 if (!runparams.nice)
2839 os.texrow().start(par.id(), 0);
2840 inset->latex(os, newrp);
2843 runparams.encoding = newrp.encoding;
2847 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2848 if (cell != lastcell) { // not last cell in row
2855 os << "\\tabularnewline";
2856 if (row_info[row].bottom_space_default) {
2858 os << "\\addlinespace";
2860 os << "[\\doublerulesep]";
2861 } else if (!row_info[row].bottom_space.zero()) {
2863 os << "\\addlinespace";
2865 << from_ascii(row_info[row].bottom_space.asLatexString())
2870 //output the bottom line
2871 TeXBottomHLine(os, row, clang, columns);
2873 if (row_info[row].interline_space_default) {
2875 os << "\\addlinespace\n";
2877 os << "\\noalign{\\vskip\\doublerulesep}\n";
2878 } else if (!row_info[row].interline_space.zero()) {
2880 os << "\\addlinespace["
2881 << from_ascii(row_info[row].interline_space.asLatexString())
2884 os << "\\noalign{\\vskip"
2885 << from_ascii(row_info[row].interline_space.asLatexString())
2891 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2893 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
2894 && !hasVarwidthColumn();
2895 bool const is_xltabular = is_long_tabular
2896 && (hasVarwidthColumn() || !tabular_width.zero());
2897 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
2899 //+---------------------------------------------------------------------
2900 //+ first the opening preamble +
2901 //+---------------------------------------------------------------------
2904 if (!TexRow::isNone(pos))
2905 os.texrow().start(pos);
2908 if (is_long_tabular)
2909 os << "\\begin{landscape}\n";
2911 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2914 if (is_long_tabular) {
2916 os << "\\begin{xltabular}";
2918 os << "\\begin{longtable}";
2919 switch (longtabular_alignment) {
2920 case LYX_LONGTABULAR_ALIGN_LEFT:
2923 case LYX_LONGTABULAR_ALIGN_CENTER:
2926 case LYX_LONGTABULAR_ALIGN_RIGHT:
2931 if (tabular_width.zero())
2932 os << "{" << from_ascii("\\columnwidth") << "}";
2934 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
2937 if (is_tabular_star)
2938 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2939 else if (hasVarwidthColumn()) {
2940 os << "\\begin{tabularx}{";
2941 if (tabular_width.zero())
2942 os << from_ascii("\\columnwidth") << "}";
2944 os << from_ascii(tabular_width.asLatexString()) << "}";
2946 os << "\\begin{tabular}";
2947 switch (tabular_valignment) {
2948 case LYX_VALIGN_TOP:
2951 case LYX_VALIGN_MIDDLE:
2953 case LYX_VALIGN_BOTTOM:
2961 if (is_tabular_star)
2962 os << "@{\\extracolsep{\\fill}}";
2964 // The bidi package (loaded by polyglossia) swaps the column
2965 // order for RTL (#9686). Thus we use this list.
2966 bool const bidi_rtl =
2967 runparams.local_font->isRightToLeft()
2968 && runparams.use_polyglossia;
2969 list<col_type> columns;
2970 for (col_type cl = 0; cl < ncols(); ++cl) {
2972 columns.push_front(cl);
2974 columns.push_back(cl);
2977 for (auto const & c : columns) {
2978 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
2980 if (!column_info[c].align_special.empty()) {
2981 os << column_info[c].align_special;
2983 if (!column_info[c].p_width.zero()) {
2984 bool decimal = false;
2985 switch (column_info[c].alignment) {
2986 case LYX_ALIGN_LEFT:
2987 os << ">{\\raggedright}";
2989 case LYX_ALIGN_RIGHT:
2990 os << ">{\\raggedleft}";
2992 case LYX_ALIGN_CENTER:
2993 os << ">{\\centering}";
2995 case LYX_ALIGN_NONE:
2996 case LYX_ALIGN_BLOCK:
2997 case LYX_ALIGN_LAYOUT:
2998 case LYX_ALIGN_SPECIAL:
3000 case LYX_ALIGN_DECIMAL: {
3002 os << ">{\\raggedright}";
3004 os << ">{\\raggedleft}";
3011 switch (column_info[c].valignment) {
3012 case LYX_VALIGN_TOP:
3013 // this is the default
3015 case LYX_VALIGN_MIDDLE:
3018 case LYX_VALIGN_BOTTOM:
3024 // Fixed-width cells with alignment at decimal separator
3025 // are output as two cells of half the width with the decimal
3026 // separator as column sep. This effectively puts the content
3027 // centered, which differs from the normal decimal sep alignment
3028 // and is not ideal, but we cannot do better ATM (see #9568).
3029 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3031 docstring const halffixedwith =
3032 from_ascii(Length(column_info[c].p_width.value() / 2,
3033 column_info[c].p_width.unit()).asLatexString());
3037 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3044 << from_ascii(column_info[c].p_width.asLatexString())
3046 } else if (column_info[c].varwidth) {
3047 switch (column_info[c].alignment) {
3048 case LYX_ALIGN_LEFT:
3049 os << ">{\\raggedright\\arraybackslash}";
3051 case LYX_ALIGN_RIGHT:
3052 os << ">{\\raggedleft\\arraybackslash}";
3054 case LYX_ALIGN_CENTER:
3055 os << ">{\\centering\\arraybackslash}";
3057 case LYX_ALIGN_NONE:
3058 case LYX_ALIGN_BLOCK:
3059 case LYX_ALIGN_LAYOUT:
3060 case LYX_ALIGN_SPECIAL:
3061 case LYX_ALIGN_DECIMAL:
3065 } else if (isVTypeColumn(c)) {
3066 switch (column_info[c].alignment) {
3067 case LYX_ALIGN_LEFT:
3068 os << ">{\\raggedright}";
3070 case LYX_ALIGN_RIGHT:
3071 os << ">{\\raggedleft}";
3073 case LYX_ALIGN_CENTER:
3074 os << ">{\\centering}";
3076 case LYX_ALIGN_NONE:
3077 case LYX_ALIGN_BLOCK:
3078 case LYX_ALIGN_LAYOUT:
3079 case LYX_ALIGN_SPECIAL:
3080 case LYX_ALIGN_DECIMAL:
3083 os << "V{\\linewidth}";
3085 switch (column_info[c].alignment) {
3086 case LYX_ALIGN_LEFT:
3089 case LYX_ALIGN_RIGHT:
3092 case LYX_ALIGN_DECIMAL:
3093 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3099 } // end if else !column_info[i].p_width
3100 } // end if else !column_info[i].align_special
3101 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3106 TeXLongtableHeaderFooter(os, runparams, columns);
3108 //+---------------------------------------------------------------------
3109 //+ the single row and columns (cells) +
3110 //+---------------------------------------------------------------------
3112 for (row_type r = 0; r < nrows(); ++r) {
3113 if (isValidRow(r)) {
3114 TeXRow(os, r, runparams, columns);
3115 if (is_long_tabular && row_info[r].newpage)
3116 os << "\\newpage\n";
3120 //+---------------------------------------------------------------------
3121 //+ the closing of the tabular +
3122 //+---------------------------------------------------------------------
3124 if (is_long_tabular) {
3126 os << "\\end{xltabular}";
3128 os << "\\end{longtable}";
3130 if (is_tabular_star)
3131 os << "\\end{tabular*}";
3132 else if (hasVarwidthColumn())
3133 os << "\\end{tabularx}";
3135 os << "\\end{tabular}";
3139 if (is_long_tabular)
3140 os << breakln << "\\end{landscape}";
3142 os << breakln << "\\end{turn}";
3145 if (!TexRow::isNone(pos))
3146 os.texrow().start(pos);
3150 int Tabular::docbookRow(odocstream & os, row_type row,
3151 OutputParams const & runparams) const
3154 idx_type cell = getFirstCellInRow(row);
3157 for (col_type c = 0; c < ncols(); ++c) {
3158 if (isPartOfMultiColumn(row, c))
3161 os << "<entry align=\"";
3162 switch (getAlignment(cell)) {
3163 case LYX_ALIGN_LEFT:
3166 case LYX_ALIGN_RIGHT:
3174 os << "\" valign=\"";
3175 switch (getVAlignment(cell)) {
3176 case LYX_VALIGN_TOP:
3179 case LYX_VALIGN_BOTTOM:
3182 case LYX_VALIGN_MIDDLE:
3187 if (isMultiColumn(cell)) {
3188 os << " namest=\"col" << c << "\" ";
3189 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3193 ret += cellInset(cell)->docbook(os, runparams);
3202 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3206 //+---------------------------------------------------------------------
3207 //+ first the opening preamble +
3208 //+---------------------------------------------------------------------
3210 os << "<tgroup cols=\"" << ncols()
3211 << "\" colsep=\"1\" rowsep=\"1\">\n";
3213 for (col_type c = 0; c < ncols(); ++c) {
3214 os << "<colspec colname=\"col" << c << "\" align=\"";
3215 switch (column_info[c].alignment) {
3216 case LYX_ALIGN_LEFT:
3219 case LYX_ALIGN_RIGHT:
3227 if (runparams.flavor == OutputParams::XML)
3233 //+---------------------------------------------------------------------
3234 //+ Long Tabular case +
3235 //+---------------------------------------------------------------------
3237 // output caption info
3238 // The caption flag wins over head/foot
3239 if (haveLTCaption()) {
3240 os << "<caption>\n";
3242 for (row_type r = 0; r < nrows(); ++r) {
3243 if (row_info[r].caption) {
3244 ret += docbookRow(os, r, runparams);
3247 os << "</caption>\n";
3250 // output header info
3251 if (haveLTHead(false) || haveLTFirstHead(false)) {
3254 for (row_type r = 0; r < nrows(); ++r) {
3255 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3256 !row_info[r].caption) {
3257 ret += docbookRow(os, r, runparams);
3263 // output footer info
3264 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3267 for (row_type r = 0; r < nrows(); ++r) {
3268 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3269 !row_info[r].caption) {
3270 ret += docbookRow(os, r, runparams);
3277 //+---------------------------------------------------------------------
3278 //+ the single row and columns (cells) +
3279 //+---------------------------------------------------------------------
3283 for (row_type r = 0; r < nrows(); ++r) {
3284 if (isValidRow(r)) {
3285 ret += docbookRow(os, r, runparams);
3290 //+---------------------------------------------------------------------
3291 //+ the closing of the tabular +
3292 //+---------------------------------------------------------------------
3301 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3302 OutputParams const & runparams, bool header) const
3305 string const celltag = header ? "th" : "td";
3306 idx_type cell = getFirstCellInRow(row);
3308 xs << html::StartTag("tr");
3309 for (col_type c = 0; c < ncols(); ++c) {
3310 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3315 Length const cwidth = column_info[c].p_width;
3316 if (!cwidth.zero()) {
3317 string const hwidth = cwidth.asHTMLString();
3318 attr << "style =\"width: " << hwidth << ";\" ";
3322 switch (getAlignment(cell)) {
3323 case LYX_ALIGN_LEFT:
3326 case LYX_ALIGN_RIGHT:
3334 attr << " valign='";
3335 switch (getVAlignment(cell)) {
3336 case LYX_VALIGN_TOP:
3339 case LYX_VALIGN_BOTTOM:
3342 case LYX_VALIGN_MIDDLE:
3347 if (isMultiColumn(cell))
3348 attr << " colspan='" << columnSpan(cell) << "'";
3349 else if (isMultiRow(cell))
3350 attr << " rowspan='" << rowSpan(cell) << "'";
3352 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3353 ret += cellInset(cell)->xhtml(xs, runparams);
3354 xs << html::EndTag(celltag) << html::CR();
3357 xs << html::EndTag("tr");
3362 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3366 if (is_long_tabular) {
3367 // we'll wrap it in a div, so as to deal with alignment
3369 switch (longtabular_alignment) {
3370 case LYX_LONGTABULAR_ALIGN_LEFT:
3373 case LYX_LONGTABULAR_ALIGN_CENTER:
3376 case LYX_LONGTABULAR_ALIGN_RIGHT:
3380 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3382 // The caption flag wins over head/foot
3383 if (haveLTCaption()) {
3384 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3386 for (row_type r = 0; r < nrows(); ++r)
3387 if (row_info[r].caption)
3388 ret += xhtmlRow(xs, r, runparams);
3389 xs << html::EndTag("div") << html::CR();
3393 xs << html::StartTag("table") << html::CR();
3395 // output header info
3396 bool const havefirsthead = haveLTFirstHead(false);
3397 // if we have a first head, then we are going to ignore the
3398 // headers for the additional pages, since there aren't any
3399 // in XHTML. this test accomplishes that.
3400 bool const havehead = !havefirsthead && haveLTHead(false);
3401 if (havehead || havefirsthead) {
3402 xs << html::StartTag("thead") << html::CR();
3403 for (row_type r = 0; r < nrows(); ++r) {
3404 if (((havefirsthead && row_info[r].endfirsthead) ||
3405 (havehead && row_info[r].endhead)) &&
3406 !row_info[r].caption) {
3407 ret += xhtmlRow(xs, r, runparams, true);
3410 xs << html::EndTag("thead") << html::CR();
3412 // output footer info
3413 bool const havelastfoot = haveLTLastFoot(false);
3415 bool const havefoot = !havelastfoot && haveLTFoot(false);
3416 if (havefoot || havelastfoot) {
3417 xs << html::StartTag("tfoot") << html::CR();
3418 for (row_type r = 0; r < nrows(); ++r) {
3419 if (((havelastfoot && row_info[r].endlastfoot) ||
3420 (havefoot && row_info[r].endfoot)) &&
3421 !row_info[r].caption) {
3422 ret += xhtmlRow(xs, r, runparams);
3425 xs << html::EndTag("tfoot") << html::CR();
3428 xs << html::StartTag("tbody") << html::CR();
3429 for (row_type r = 0; r < nrows(); ++r) {
3430 if (isValidRow(r)) {
3431 ret += xhtmlRow(xs, r, runparams);
3434 xs << html::EndTag("tbody")
3436 << html::EndTag("table")
3438 if (is_long_tabular)
3439 xs << html::EndTag("div") << html::CR();
3444 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3445 vector<unsigned int> const & clen) const
3447 idx_type const fcell = getFirstCellInRow(row);
3448 idx_type const n = numberOfCellsInRow(row) + fcell;
3451 for (idx_type i = fcell; i < n; ++i) {
3461 for (idx_type i = fcell; i < n; ++i) {
3472 col_type column = cellColumn(i);
3473 int len = clen[column];
3474 while (column < ncols() - 1
3475 && isPartOfMultiColumn(row, ++column))
3476 len += clen[column] + 4;
3477 os << docstring(len, ch);
3492 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3493 vector<unsigned int> const & clen) const
3495 idx_type const fcell = getFirstCellInRow(row);
3496 idx_type const n = numberOfCellsInRow(row) + fcell;
3499 for (idx_type i = fcell; i < n; ++i) {
3500 if (bottomLine(i)) {
3509 for (idx_type i = fcell; i < n; ++i) {
3510 if (bottomLine(i)) {
3520 col_type column = cellColumn(i);
3521 int len = clen[column];
3522 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3523 len += clen[column] + 4;
3524 os << docstring(len, ch);
3525 if (bottomLine(i)) {
3539 void Tabular::plaintextPrintCell(odocstringstream & os,
3540 OutputParams const & runparams,
3541 idx_type cell, row_type row, col_type column,
3542 vector<unsigned int> const & clen,
3543 bool onlydata, size_t max_length) const
3545 odocstringstream sstr;
3546 cellInset(cell)->plaintext(sstr, runparams, max_length);
3558 unsigned int len1 = sstr.str().length();
3559 unsigned int len2 = clen[column];
3560 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3561 len2 += clen[column] + 4;
3564 switch (getAlignment(cell)) {
3566 case LYX_ALIGN_LEFT:
3569 case LYX_ALIGN_RIGHT:
3573 case LYX_ALIGN_CENTER:
3579 os << docstring(len1, ' ') << sstr.str()
3580 << docstring(len2, ' ');
3582 if (rightLine(cell))
3589 void Tabular::plaintext(odocstringstream & os,
3590 OutputParams const & runparams, int const depth,
3591 bool onlydata, char_type delim, size_t max_length) const
3593 // first calculate the width of the single columns
3594 vector<unsigned int> clen(ncols());
3597 // first all non multicolumn cells!
3598 for (col_type c = 0; c < ncols(); ++c) {
3600 for (row_type r = 0; r < nrows(); ++r) {
3601 idx_type cell = cellIndex(r, c);
3602 if (isMultiColumn(cell))
3604 odocstringstream sstr;
3605 cellInset(cell)->plaintext(sstr, runparams, max_length);
3606 if (clen[c] < sstr.str().length())
3607 clen[c] = sstr.str().length();
3610 // then all multicolumn cells!
3611 for (col_type c = 0; c < ncols(); ++c) {
3612 for (row_type r = 0; r < nrows(); ++r) {
3613 idx_type cell = cellIndex(r, c);
3614 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3616 odocstringstream sstr;
3617 cellInset(cell)->plaintext(sstr, runparams, max_length);
3618 int len = int(sstr.str().length());
3619 idx_type const n = columnSpan(cell);
3620 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3622 if (len > int(clen[c + n - 1]))
3623 clen[c + n - 1] = len;
3628 for (row_type r = 0; r < nrows(); ++r) {
3629 if (!onlydata && plaintextTopHLine(os, r, clen))
3630 os << docstring(depth * 2, ' ');
3631 for (col_type c = 0; c < ncols(); ++c) {
3632 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3634 if (onlydata && c > 0)
3635 // we don't use operator<< for single UCS4 character.
3636 // see explanation in docstream.h
3638 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3640 if (os.str().size() > max_length)
3645 os << docstring(depth * 2, ' ');
3646 if (plaintextBottomHLine(os, r, clen))
3647 os << docstring(depth * 2, ' ');
3649 if (os.str().size() > max_length)
3655 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3657 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3661 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3663 return cell_info[row][column].inset;
3667 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3669 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3673 void Tabular::setCellInset(row_type row, col_type column,
3674 shared_ptr<InsetTableCell> ins)
3676 CellData & cd = cell_info[row][column];
3681 void Tabular::validate(LaTeXFeatures & features) const
3683 features.require("NeedTabularnewline");
3685 features.require("booktabs");
3686 if (is_long_tabular && !hasVarwidthColumn()) {
3687 if (tabular_width.zero())
3688 features.require("longtable");
3690 features.require("xltabular");
3692 if (rotate && is_long_tabular)
3693 features.require("lscape");
3695 features.require("rotating");
3696 if (hasVarwidthColumn()) {
3697 if (is_long_tabular)
3698 features.require("xltabular");
3700 features.require("tabularx");
3702 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3703 if (isMultiRow(cell))
3704 features.require("multirow");
3705 if (getUsebox(cell) == BOX_VARWIDTH)
3706 features.require("varwidth");
3707 if (getVAlignment(cell) != LYX_VALIGN_TOP
3708 || !getPWidth(cell).zero()
3709 || isVTypeColumn(cellColumn(cell)))
3710 features.require("array");
3711 // Tell footnote that we need a savenote
3712 // environment in non-long tables or
3713 // longtable headers/footers
3714 else if (!is_long_tabular && !features.inFloat())
3715 features.saveNoteEnv("tabular");
3716 else if (!isValidRow(cellRow(cell)))
3717 features.saveNoteEnv("longtable");
3719 cellInset(cell)->validate(features);
3720 features.saveNoteEnv(string());
3725 Tabular::BoxType Tabular::useBox(idx_type cell) const
3727 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3728 if (parlist.size() > 1)
3729 return BOX_VARWIDTH;
3731 ParagraphList::const_iterator cit = parlist.begin();
3732 ParagraphList::const_iterator end = parlist.end();
3734 for (; cit != end; ++cit)
3735 for (int i = 0; i < cit->size(); ++i)
3736 if (cit->isNewline(i) || cit->layout().isEnvironment())
3737 return BOX_VARWIDTH;
3743 /////////////////////////////////////////////////////////////////////
3747 /////////////////////////////////////////////////////////////////////
3749 InsetTableCell::InsetTableCell(Buffer * buf)
3750 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3751 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
3755 bool InsetTableCell::forcePlainLayout(idx_type) const
3757 return isMultiRow || (isMultiColumn && !isFixedWidth);
3761 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3763 return isFixedWidth;
3767 bool InsetTableCell::forceLocalFontSwitch() const
3769 return isFixedWidth;
3773 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3774 FuncStatus & status) const
3776 bool enabled = true;
3777 switch (cmd.action()) {
3778 case LFUN_MATH_DISPLAY:
3779 if (!hasFixedWidth()) {
3784 return InsetText::getStatus(cur, cmd, status);
3786 status.setEnabled(enabled);
3790 docstring InsetTableCell::asString(bool intoInsets)
3793 if (paragraphs().empty())
3795 ParagraphList::const_iterator it = paragraphs().begin();
3796 ParagraphList::const_iterator en = paragraphs().end();
3798 for (; it != en; ++it) {
3803 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3809 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3810 UpdateType utype, TocBackend & backend) const
3812 InsetText::iterateForToc(di, output_active, utype, backend);
3816 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3819 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3820 return InsetText::xhtml(xs, rp);
3824 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
3826 TextMetrics & tm = mi.base.bv->textMetrics(&text());
3828 // Hand font through to contained lyxtext:
3829 tm.font_.fontInfo() = mi.base.font;
3830 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
3832 // This can happen when a layout has a left and right margin,
3833 // and the view is made very narrow. We can't do better than
3834 // to draw it partly out of view (bug 5890).
3835 if (mi.base.textwidth < 1)
3836 mi.base.textwidth = 1;
3838 // We tell metrics here not to expand on multiple pars
3839 // This is the difference to InsetText::Metrics
3840 if (hasFixedWidth())
3841 tm.metrics(mi, dim, mi.base.textwidth, false);
3843 tm.metrics(mi, dim, 0, false);
3844 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
3845 dim.asc += TEXT_TO_INSET_OFFSET;
3846 dim.des += TEXT_TO_INSET_OFFSET;
3847 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
3852 /////////////////////////////////////////////////////////////////////
3856 /////////////////////////////////////////////////////////////////////
3858 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3860 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3861 rowselect_(false), colselect_(false)
3866 InsetTabular::InsetTabular(InsetTabular const & tab)
3867 : Inset(tab), tabular(tab.tabular),
3868 rowselect_(false), colselect_(false)
3873 InsetTabular::~InsetTabular()
3875 hideDialogs("tabular", this);
3879 void InsetTabular::setBuffer(Buffer & buf)
3881 tabular.setBuffer(buf);
3882 Inset::setBuffer(buf);
3886 bool InsetTabular::insetAllowed(InsetCode code) const
3891 case MATHMACRO_CODE:
3896 return tabular.is_long_tabular;
3904 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3906 return tabular.is_long_tabular &&
3907 (newtype == "Standard" || newtype == "Unnumbered");
3911 void InsetTabular::write(ostream & os) const
3913 os << "Tabular" << endl;
3918 string InsetTabular::contextMenu(BufferView const &, int, int) const
3920 // FIXME: depending on the selection state,
3921 // we could offer a different menu.
3922 return cell(0)->contextMenuName() + ";" + contextMenuName();
3926 string InsetTabular::contextMenuName() const
3928 return "context-tabular";
3932 void InsetTabular::read(Lexer & lex)
3934 //bool const old_format = (lex.getString() == "\\LyXTable");
3942 string token = lex.getString();
3943 while (lex && token != "\\end_inset") {
3945 token = lex.getString();
3948 lex.printError("Missing \\end_inset at this point. ");
3952 int InsetTabular::rowFromY(Cursor & cur, int y) const
3954 // top y coordinate of tabular
3955 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3957 for (; r < tabular.nrows() && y > h; ++r)
3958 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3959 + tabular.interRowSpace(r);
3965 int InsetTabular::columnFromX(Cursor & cur, int x) const
3967 // left x coordinate of tabular
3968 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3970 for (; c < tabular.ncols() && x > w; ++c)
3971 w += tabular.cellWidth(c);
3976 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3978 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3979 // mi.base.textwidth << "\n";
3980 LBUFERR(mi.base.bv);
3982 for (row_type r = 0; r < tabular.nrows(); ++r) {
3985 for (col_type c = 0; c < tabular.ncols(); ++c) {
3986 if (tabular.isPartOfMultiColumn(r, c)
3987 || tabular.isPartOfMultiRow(r, c))
3988 // multicolumn or multirow cell, but not first one
3990 idx_type const cell = tabular.cellIndex(r, c);
3993 Length const p_width = tabular.getPWidth(cell);
3994 if (!p_width.zero())
3995 m.base.textwidth = mi.base.inPixels(p_width);
3996 else if (tabular.column_info[c].varwidth)
3997 m.base.textwidth = tabular.column_info[c].width;
3998 tabular.cellInset(cell)->metrics(m, dim0);
3999 if (!p_width.zero() || tabular.column_info[c].varwidth)
4000 dim0.wid = m.base.textwidth;
4001 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4002 + tabular.interColumnSpace(cell);
4004 // FIXME(?): do we need a second metrics call?
4005 TextMetrics const & tm =
4006 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4008 // determine horizontal offset because of decimal align (if necessary)
4009 int decimal_width = 0;
4010 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4011 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4012 tail.setBuffer(tabular.buffer());
4013 // we need to set macrocontext position everywhere
4014 // otherwise we crash with nested insets (e.g. footnotes)
4015 // after decimal point
4016 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4018 dit.push_back(CursorSlice(tail));
4019 tail.setMacrocontextPositionRecursive(dit);
4021 // remove text leading decimal point
4022 docstring const align_d = tabular.column_info[c].decimal_point;
4023 dit = separatorPos(&tail, align_d);
4025 pit_type const psize = tail.paragraphs().front().size();
4027 tail.paragraphs().front().eraseChars(0,
4028 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4030 tail.metrics(m, dim1);
4031 decimal_width = dim1.width();
4034 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4035 tabular.cell_info[r][c].decimal_width = decimal_width;
4037 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4038 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
4039 int const lastpardes = tm.last().second->descent()
4040 + TEXT_TO_INSET_OFFSET;
4042 switch (tabular.getVAlignment(cell)) {
4043 case Tabular::LYX_VALIGN_TOP:
4045 case Tabular::LYX_VALIGN_MIDDLE:
4046 offset = -(dim0.des - lastpardes)/2;
4048 case Tabular::LYX_VALIGN_BOTTOM:
4049 offset = -(dim0.des - lastpardes);
4052 tabular.cell_info[r][c].voffset = offset;
4053 maxasc = max(maxasc, dim0.asc - offset);
4054 maxdes = max(maxdes, dim0.des + offset);
4056 int const top_space = tabular.row_info[r].top_space_default ?
4057 default_line_space :
4058 mi.base.inPixels(tabular.row_info[r].top_space);
4059 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4060 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4061 default_line_space :
4062 mi.base.inPixels(tabular.row_info[r].bottom_space);
4063 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4066 // We need to recalculate the metrics after column width calculation
4067 // with xtabular (possibly multiple times, so the call is recursive).
4068 if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
4070 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4071 dim.des = tabular.height() - dim.asc;
4072 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4076 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4078 if (&cur.inset() == this && cur.selection()) {
4079 if (cur.selIsMultiCell()) {
4082 getSelection(cur, rs, re, cs, ce);
4084 idx_type const cell = tabular.cellIndex(row, col);
4085 col_type const cspan = tabular.columnSpan(cell);
4086 row_type const rspan = tabular.rowSpan(cell);
4087 if (col + cspan - 1 >= cs && col <= ce
4088 && row + rspan - 1 >= rs && row <= re)
4091 if (col == tabular.cellColumn(cur.idx())
4092 && row == tabular.cellRow(cur.idx())) {
4093 CursorSlice const & beg = cur.selBegin();
4094 CursorSlice const & end = cur.selEnd();
4096 if ((end.lastpos() > 0 || end.lastpit() > 0)
4097 && end.pos() == end.lastpos() && beg.pos() == 0
4098 && end.pit() == end.lastpit() && beg.pit() == 0)
4106 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4108 x += ADD_TO_TABULAR_WIDTH;
4110 BufferView * bv = pi.base.bv;
4111 Cursor & cur = pi.base.bv->cursor();
4113 // FIXME: As the full background is painted in drawBackground(),
4114 // we have no choice but to do a full repaint for the Text cells.
4115 pi.full_repaint = true;
4117 bool const original_selection_state = pi.selected;
4121 int yy = y + tabular.offsetVAlignment();
4122 for (row_type r = 0; r < tabular.nrows(); ++r) {
4124 for (col_type c = 0; c < tabular.ncols(); ++c) {
4125 if (tabular.isPartOfMultiColumn(r, c))
4128 idx = tabular.cellIndex(r, c);
4130 if (tabular.isPartOfMultiRow(r, c)) {
4131 nx += tabular.cellWidth(idx);
4135 pi.selected |= isCellSelected(cur, r, c);
4136 int const cx = nx + tabular.textHOffset(idx);
4137 int const cy = yy + tabular.textVOffset(idx);
4138 // Cache the Inset position.
4139 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4140 cell(idx)->draw(pi, cx, cy);
4141 drawCellLines(pi, nx, yy, r, idx);
4142 nx += tabular.cellWidth(idx);
4143 pi.selected = original_selection_state;
4146 if (r + 1 < tabular.nrows())
4147 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4148 + tabular.interRowSpace(r + 1);
4153 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4155 x += ADD_TO_TABULAR_WIDTH;
4156 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4157 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4158 pi.backgroundColor(this));
4162 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4164 Cursor & cur = pi.base.bv->cursor();
4166 x += ADD_TO_TABULAR_WIDTH;
4168 if (!cur.selection())
4170 if (&cur.inset() != this)
4175 bool const full_cell_selected = isCellSelected(cur,
4176 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4178 if (cur.selIsMultiCell() || full_cell_selected) {
4179 for (row_type r = 0; r < tabular.nrows(); ++r) {
4181 for (col_type c = 0; c < tabular.ncols(); ++c) {
4182 if (tabular.isPartOfMultiColumn(r, c))
4185 idx_type const cell = tabular.cellIndex(r, c);
4187 if (tabular.isPartOfMultiRow(r, c)) {
4188 xx += tabular.cellWidth(cell);
4191 int const w = tabular.cellWidth(cell);
4192 int const h = tabular.cellHeight(cell);
4193 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4194 if (isCellSelected(cur, r, c))
4195 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4198 if (r + 1 < tabular.nrows())
4199 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4200 + tabular.interRowSpace(r + 1);
4204 // FIXME: This code has no effect because InsetTableCell does not handle
4205 // drawSelection other than the trivial implementation in Inset.
4207 // x += cellXPos(cur.idx());
4208 // x += tabular.textHOffset(cur.idx());
4209 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4216 void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2,
4217 bool drawline, bool heavy = false)
4219 ColorCode const col = drawline ? Color_tabularline : Color_tabularonoffline;
4220 pi.pain.line(x1, y1, x2, y2, pi.textColor(col),
4221 drawline ? Painter::line_solid : Painter::line_onoffdash,
4222 (heavy ? 2 : 1) * Painter::thin_line);
4228 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4229 row_type row, idx_type cell) const
4231 y -= tabular.rowAscent(row);
4232 int const w = tabular.cellWidth(cell);
4233 int const h = tabular.cellHeight(cell);
4236 bool drawline = tabular.topLine(cell)
4237 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4238 bool heavy = tabular.use_booktabs && row == 0 && tabular.rowTopLine(row);
4239 tabline(pi, x, y, x + w, y, drawline, heavy);
4242 drawline = tabular.bottomLine(cell);
4243 heavy = tabular.use_booktabs && row == tabular.nrows() - 1
4244 && tabular.rowBottomLine(row);
4245 tabline(pi, x, y + h, x + w, y + h, drawline, heavy);
4248 col_type const col = tabular.cellColumn(cell);
4249 drawline = tabular.leftLine(cell)
4250 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4251 tabline(pi, x, y, x, y + h, drawline);
4254 x -= tabular.interColumnSpace(cell);
4255 col_type next_cell_col = col + 1;
4256 while (next_cell_col < tabular.ncols()
4257 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4259 drawline = tabular.rightLine(cell)
4260 || (next_cell_col < tabular.ncols()
4261 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4262 tabline(pi, x + w, y, x + w, y + h, drawline);
4266 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4268 //lyxerr << "InsetTabular::edit: " << this << endl;
4272 if (isRightToLeft(cur))
4273 cur.idx() = tabular.getLastCellInRow(0);
4279 if (isRightToLeft(cur))
4280 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4282 cur.idx() = tabular.numberofcells - 1;
4284 cur.pos() = cur.lastpos(); // FIXME crude guess
4286 cur.setCurrentFont();
4287 // FIXME: this accesses the position cache before it is initialized
4288 //cur.bv().fitCursor();
4292 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
4294 // In a longtable, tell captions what the current float is
4295 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4296 string const saveflt = cnts.current_float();
4297 if (tabular.is_long_tabular) {
4298 cnts.current_float("table");
4299 // in longtables, we only step the counter once
4300 cnts.step(from_ascii("table"), utype);
4301 cnts.isLongtable(true);
4304 ParIterator it2 = it;
4306 size_t const end = it2.nargs();
4307 for ( ; it2.idx() < end; it2.top().forwardIdx())
4308 buffer().updateBuffer(it2, utype);
4311 if (tabular.is_long_tabular) {
4312 cnts.current_float(saveflt);
4313 cnts.isLongtable(false);
4318 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4319 UpdateType utype, TocBackend & backend) const
4321 DocIterator dit = cpit;
4323 size_t const end = dit.nargs();
4324 for ( ; dit.idx() < end; dit.top().forwardIdx())
4325 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4329 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4331 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4332 return x < x0 || x > x0 + tabular.width();
4336 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4338 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4339 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4340 // there is no margin added vertically to tabular insets.
4341 // However, it works for now.
4342 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4346 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4348 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4352 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4354 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4355 << "\n cur:" << cur);
4356 CursorSlice sl = cur.top();
4357 Cursor & bvcur = cur.bv().cursor();
4359 FuncCode const act = cmd.action();
4363 case LFUN_MOUSE_PRESS: {
4364 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4366 if (hitSelectRow(cur.bv(), cmd.x())) {
4367 row_type r = rowFromY(cur, cmd.y());
4368 cur.idx() = tabular.getFirstCellInRow(r);
4372 cur.idx() = tabular.getLastCellInRow(r);
4373 cur.pit() = cur.lastpit();
4374 cur.pos() = cur.lastpos();
4375 cur.selection(true);
4381 if (hitSelectColumn(cur.bv(), cmd.y())) {
4382 col_type c = columnFromX(cur, cmd.x());
4383 cur.idx() = tabular.cellIndex(0, c);
4387 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4388 cur.pit() = cur.lastpit();
4389 cur.pos() = cur.lastpos();
4390 cur.selection(true);
4395 // do not reset cursor/selection if we have selected
4396 // some cells (bug 2715).
4397 if (cmd.button() == mouse_button::button3
4398 && &bvcur.selBegin().inset() == this
4399 && bvcur.selIsMultiCell())
4402 // Let InsetTableCell do it
4403 cell(cur.idx())->dispatch(cur, cmd);
4406 case LFUN_MOUSE_MOTION:
4407 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4408 if (cmd.button() == mouse_button::button1) {
4409 // only accept motions to places not deeper nested than the real anchor
4410 if (!bvcur.realAnchor().hasPart(cur)) {
4414 // select (additional) row
4416 row_type r = rowFromY(cur, cmd.y());
4417 cur.idx() = tabular.getLastCellInRow(r);
4418 // we need to reset the cursor's pit and pos now, as the old ones
4419 // may no longer be valid.
4422 bvcur.setCursor(cur);
4423 bvcur.selection(true);
4426 // select (additional) column
4428 col_type c = columnFromX(cur, cmd.x());
4429 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4430 // we need to reset the cursor's pit and pos now, as the old ones
4431 // may no longer be valid.
4434 bvcur.setCursor(cur);
4435 bvcur.selection(true);
4438 // only update if selection changes
4439 if (bvcur.idx() == cur.idx() &&
4440 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4441 cur.noScreenUpdate();
4442 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4443 bvcur.setCursor(cur);
4444 bvcur.selection(true);
4445 // if this is a multicell selection, we just set the cursor to
4446 // the beginning of the cell's text.
4447 if (bvcur.selIsMultiCell()) {
4448 bvcur.pit() = bvcur.lastpit();
4449 bvcur.pos() = bvcur.lastpos();
4454 case LFUN_MOUSE_RELEASE:
4459 case LFUN_CELL_BACKWARD:
4461 cur.selection(false);
4464 case LFUN_CELL_FORWARD:
4466 cur.selection(false);
4469 case LFUN_CHAR_FORWARD_SELECT:
4470 case LFUN_CHAR_FORWARD:
4471 case LFUN_CHAR_BACKWARD_SELECT:
4472 case LFUN_CHAR_BACKWARD:
4473 case LFUN_CHAR_RIGHT_SELECT:
4474 case LFUN_CHAR_RIGHT:
4475 case LFUN_CHAR_LEFT_SELECT:
4476 case LFUN_CHAR_LEFT:
4477 case LFUN_WORD_FORWARD:
4478 case LFUN_WORD_FORWARD_SELECT:
4479 case LFUN_WORD_BACKWARD:
4480 case LFUN_WORD_BACKWARD_SELECT:
4481 case LFUN_WORD_RIGHT:
4482 case LFUN_WORD_RIGHT_SELECT:
4483 case LFUN_WORD_LEFT:
4484 case LFUN_WORD_LEFT_SELECT: {
4485 // determine whether we move to next or previous cell, where to enter
4486 // the new cell from, and which command to "finish" (i.e., exit the
4489 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4490 FuncCode finish_lfun;
4492 if (act == LFUN_CHAR_FORWARD
4493 || act == LFUN_CHAR_FORWARD_SELECT
4494 || act == LFUN_WORD_FORWARD
4495 || act == LFUN_WORD_FORWARD_SELECT) {
4497 finish_lfun = LFUN_FINISHED_FORWARD;
4499 else if (act == LFUN_CHAR_BACKWARD
4500 || act == LFUN_CHAR_BACKWARD_SELECT
4501 || act == LFUN_WORD_BACKWARD
4502 || act == LFUN_WORD_BACKWARD_SELECT) {
4504 finish_lfun = LFUN_FINISHED_BACKWARD;
4506 // LEFT or RIGHT commands --- the interpretation will depend on the
4507 // table's direction.
4509 bool const right = act == LFUN_CHAR_RIGHT
4510 || act == LFUN_CHAR_RIGHT_SELECT
4511 || act == LFUN_WORD_RIGHT
4512 || act == LFUN_WORD_RIGHT_SELECT;
4513 next_cell = isRightToLeft(cur) != right;
4515 if (lyxrc.visual_cursor)
4516 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4518 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4521 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4522 || act == LFUN_CHAR_BACKWARD_SELECT
4523 || act == LFUN_CHAR_RIGHT_SELECT
4524 || act == LFUN_CHAR_LEFT_SELECT
4525 || act == LFUN_WORD_FORWARD_SELECT
4526 || act == LFUN_WORD_RIGHT_SELECT
4527 || act == LFUN_WORD_BACKWARD_SELECT
4528 || act == LFUN_WORD_LEFT_SELECT;
4530 // If we have a multicell selection or we're
4531 // not doing some LFUN_*_SELECT thing anyway...
4532 if (!cur.selIsMultiCell() || !select) {
4533 col_type const c = tabular.cellColumn(cur.idx());
4534 row_type const r = tabular.cellRow(cur.idx());
4535 // Are we trying to select the whole cell and is the whole cell
4536 // not yet selected?
4537 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4538 ((next_cell && cur.pit() == cur.lastpit()
4539 && cur.pos() == cur.lastpos())
4540 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4542 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4544 // ...try to dispatch to the cell's inset.
4545 cell(cur.idx())->dispatch(cur, cmd);
4547 // When we already have a selection we want to select the whole cell
4548 // before going to the next cell.
4549 if (select_whole && !empty_cell){
4550 getText(cur.idx())->selectAll(cur);
4552 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4556 // FIXME: When we support the selection of an empty cell, remove
4557 // the !empty_cell from this condition. For now we jump to the next
4558 // cell if the current cell is empty.
4559 if (cur.result().dispatched() && !empty_cell)
4563 // move to next/prev cell, as appropriate
4564 // note that we will always do this if we're selecting and we have
4565 // a multicell selection
4566 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4567 << " cell from: " << int(entry_from));
4569 moveNextCell(cur, entry_from);
4571 movePrevCell(cur, entry_from);
4572 // if we're exiting the table, call the appropriate FINISHED lfun
4573 if (sl == cur.top()) {
4574 cmd = FuncRequest(finish_lfun);
4579 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4584 case LFUN_DOWN_SELECT:
4586 if (!(cur.selection() && cur.selIsMultiCell()))
4587 cell(cur.idx())->dispatch(cur, cmd);
4589 cur.dispatched(); // override the cell's decision
4590 if (sl == cur.top()) {
4591 // if our Text didn't do anything to the cursor
4592 // then we try to put the cursor into the cell below
4593 // setting also the right targetX.
4594 cur.selHandle(act == LFUN_DOWN_SELECT);
4595 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4596 int const xtarget = cur.targetX();
4597 // WARNING: Once cur.idx() has been reset, the cursor is in
4598 // an inconsistent state until pos() has been set. Be careful
4599 // what you do with it!
4600 cur.idx() = tabular.cellBelow(cur.idx());
4602 TextMetrics const & tm =
4603 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4604 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4605 cur.setCurrentFont();
4608 if (sl == cur.top()) {
4609 // we trick it to go to forward after leaving the
4611 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4614 if (cur.selIsMultiCell()) {
4615 cur.pit() = cur.lastpit();
4616 cur.pos() = cur.lastpos();
4617 cur.setCurrentFont();
4618 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4621 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4624 case LFUN_UP_SELECT:
4626 if (!(cur.selection() && cur.selIsMultiCell()))
4627 cell(cur.idx())->dispatch(cur, cmd);
4628 cur.dispatched(); // override the cell's decision
4629 if (sl == cur.top()) {
4630 // if our Text didn't do anything to the cursor
4631 // then we try to put the cursor into the cell above
4632 // setting also the right targetX.
4633 cur.selHandle(act == LFUN_UP_SELECT);
4634 if (tabular.cellRow(cur.idx()) != 0) {
4635 int const xtarget = cur.targetX();
4636 // WARNING: Once cur.idx() has been reset, the cursor is in
4637 // an inconsistent state until pos() has been set. Be careful
4638 // what you do with it!
4639 cur.idx() = tabular.cellAbove(cur.idx());
4640 cur.pit() = cur.lastpit();
4641 Text const * text = cell(cur.idx())->getText(0);
4642 TextMetrics const & tm = cur.bv().textMetrics(text);
4643 ParagraphMetrics const & pm =
4644 tm.parMetrics(cur.lastpit());
4645 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4646 cur.setCurrentFont();
4649 if (sl == cur.top()) {
4650 cmd = FuncRequest(LFUN_UP);
4653 if (cur.selIsMultiCell()) {
4655 cur.pos() = cur.lastpos();
4656 cur.setCurrentFont();
4657 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4660 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4663 case LFUN_LAYOUT_TABULAR:
4664 cur.bv().showDialog("tabular");
4667 case LFUN_INSET_MODIFY:
4668 // we come from the dialog
4669 if (cmd.getArg(0) == "tabular")
4670 tabularFeatures(cur, cmd.getLongArg(1));
4675 case LFUN_TABULAR_FEATURE:
4676 tabularFeatures(cur, to_utf8(cmd.argument()));
4679 // insert file functions
4680 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4681 case LFUN_FILE_INSERT_PLAINTEXT:
4683 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4684 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4685 FileName(to_utf8(cmd.argument())));
4688 cur.recordUndoInset();
4689 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4690 // content has been replaced,
4691 // so cursor might be invalid
4692 cur.pos() = cur.lastpos();
4693 cur.pit() = cur.lastpit();
4694 bvcur.setCursor(cur);
4701 if (cur.selIsMultiCell()) {
4702 if (copySelection(cur)) {
4703 cur.recordUndoInset();
4707 cell(cur.idx())->dispatch(cur, cmd);
4710 case LFUN_SELF_INSERT:
4711 if (cur.selIsMultiCell()) {
4712 cur.recordUndoInset();
4714 BufferView * bv = &cur.bv();
4715 docstring::const_iterator cit = cmd.argument().begin();
4716 docstring::const_iterator const end = cmd.argument().end();
4717 for (; cit != end; ++cit)
4718 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4721 bv->bookmarkEditPosition();
4723 cell(cur.idx())->dispatch(cur, cmd);
4726 case LFUN_CHAR_DELETE_BACKWARD:
4727 case LFUN_CHAR_DELETE_FORWARD:
4728 if (cur.selIsMultiCell()) {
4729 cur.recordUndoInset();
4732 cell(cur.idx())->dispatch(cur, cmd);
4736 if (!cur.selection())
4738 if (cur.selIsMultiCell()) {
4742 cell(cur.idx())->dispatch(cur, cmd);
4745 case LFUN_CLIPBOARD_PASTE:
4746 case LFUN_PRIMARY_SELECTION_PASTE: {
4747 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4748 theClipboard().getAsText(Clipboard::PlainTextType) :
4749 theSelection().get();
4752 // pass to InsertPlaintextString, but
4753 // only if we have multi-cell content
4754 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4755 cur.recordUndoInset();
4756 if (insertPlaintextString(cur.bv(), clip, false)) {
4757 // content has been replaced,
4758 // so cursor might be invalid
4759 cur.pos() = cur.lastpos();
4760 cur.pit() = cur.lastpit();
4761 bvcur.setCursor(cur);
4765 // Let the cell handle normal text
4766 cell(cur.idx())->dispatch(cur, cmd);
4771 if (!tabularStackDirty()) {
4772 if (!cur.selIsMultiCell())
4773 cell(cur.idx())->dispatch(cur, cmd);
4776 if (theClipboard().isInternal()) {
4777 cur.recordUndoInset();
4778 pasteClipboard(cur);
4782 case LFUN_FONT_EMPH:
4783 case LFUN_FONT_BOLD:
4784 case LFUN_FONT_BOLDSYMBOL:
4785 case LFUN_FONT_ROMAN:
4786 case LFUN_FONT_NOUN:
4787 case LFUN_FONT_ITAL:
4788 case LFUN_FONT_FRAK:
4789 case LFUN_FONT_TYPEWRITER:
4790 case LFUN_FONT_SANS:
4791 case LFUN_TEXTSTYLE_APPLY:
4792 case LFUN_TEXTSTYLE_UPDATE:
4793 case LFUN_FONT_SIZE:
4794 case LFUN_FONT_UNDERLINE:
4795 case LFUN_FONT_STRIKEOUT:
4796 case LFUN_FONT_CROSSOUT:
4797 case LFUN_FONT_UNDERUNDERLINE:
4798 case LFUN_FONT_UNDERWAVE:
4800 case LFUN_PARAGRAPH_PARAMS_APPLY:
4801 case LFUN_PARAGRAPH_PARAMS:
4802 case LFUN_WORD_CAPITALIZE:
4803 case LFUN_WORD_UPCASE:
4804 case LFUN_WORD_LOWCASE:
4805 case LFUN_CHARS_TRANSPOSE:
4806 if (cur.selIsMultiCell()) {
4809 getSelection(cur, rs, re, cs, ce);
4810 Cursor tmpcur = cur;
4811 for (row_type r = rs; r <= re; ++r) {
4812 for (col_type c = cs; c <= ce; ++c) {
4813 // cursor follows cell:
4814 tmpcur.idx() = tabular.cellIndex(r, c);
4815 // select this cell only:
4818 tmpcur.resetAnchor();
4819 tmpcur.pit() = tmpcur.lastpit();
4820 tmpcur.pos() = tmpcur.top().lastpos();
4821 tmpcur.setCursor(tmpcur);
4822 tmpcur.setSelection();
4823 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4828 cell(cur.idx())->dispatch(cur, cmd);
4832 case LFUN_INSET_SETTINGS:
4833 // relay this lfun to Inset, not to the cell.
4834 Inset::doDispatch(cur, cmd);
4838 // we try to handle this event in the insets dispatch function.
4839 cell(cur.idx())->dispatch(cur, cmd);
4845 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4846 string const & argument, FuncStatus & status) const
4849 int action = Tabular::LAST_ACTION;
4851 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4852 if (tabularFeature[i].feature == s) {
4853 action = tabularFeature[i].action;
4857 if (action == Tabular::LAST_ACTION) {
4859 status.setUnknown(true);
4863 row_type sel_row_start = 0;
4864 row_type sel_row_end = 0;
4865 col_type sel_col_start = 0;
4866 col_type sel_col_end = 0;
4867 Tabular::ltType dummyltt;
4870 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4873 case Tabular::SET_PWIDTH:
4874 case Tabular::SET_MPWIDTH:
4875 case Tabular::TOGGLE_VARWIDTH_COLUMN:
4876 case Tabular::SET_SPECIAL_COLUMN:
4877 case Tabular::SET_SPECIAL_MULTICOLUMN:
4878 case Tabular::APPEND_ROW:
4879 case Tabular::APPEND_COLUMN:
4880 case Tabular::DELETE_ROW:
4881 case Tabular::DELETE_COLUMN:
4882 case Tabular::COPY_ROW:
4883 case Tabular::COPY_COLUMN:
4884 case Tabular::SET_TOP_SPACE:
4885 case Tabular::SET_BOTTOM_SPACE:
4886 case Tabular::SET_INTERLINE_SPACE:
4890 case Tabular::SET_TABULAR_WIDTH:
4891 status.setEnabled(!tabular.rotate
4892 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4895 case Tabular::MOVE_COLUMN_RIGHT:
4896 case Tabular::MOVE_COLUMN_LEFT:
4897 case Tabular::MOVE_ROW_DOWN:
4898 case Tabular::MOVE_ROW_UP: {
4899 if (cur.selection()) {
4900 status.message(_("Selections not supported."));
4901 status.setEnabled(false);
4905 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4906 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4907 (action == Tabular::MOVE_COLUMN_LEFT &&
4908 tabular.cellColumn(cur.idx()) == 0) ||
4909 (action == Tabular::MOVE_ROW_DOWN &&
4910 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4911 (action == Tabular::MOVE_ROW_UP &&
4912 tabular.cellRow(cur.idx()) == 0)) {
4913 status.setEnabled(false);
4917 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4918 action == Tabular::MOVE_COLUMN_LEFT) {
4919 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4920 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4921 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4922 status.message(_("Multi-column in current or"
4923 " destination column."));
4924 status.setEnabled(false);
4929 if (action == Tabular::MOVE_ROW_DOWN ||
4930 action == Tabular::MOVE_ROW_UP) {
4931 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4932 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4933 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4934 status.message(_("Multi-row in current or"
4935 " destination row."));
4936 status.setEnabled(false);
4941 status.setEnabled(true);
4945 case Tabular::SET_DECIMAL_POINT:
4947 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4950 case Tabular::SET_MULTICOLUMN:
4951 case Tabular::UNSET_MULTICOLUMN:
4952 case Tabular::MULTICOLUMN:
4953 // If a row is set as longtable caption, it must not be allowed
4954 // to unset that this row is a multicolumn.
4955 status.setEnabled(sel_row_start == sel_row_end
4956 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4957 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4960 case Tabular::SET_MULTIROW:
4961 case Tabular::UNSET_MULTIROW:
4962 case Tabular::MULTIROW:
4963 // If a row is set as longtable caption, it must not be allowed
4964 // to unset that this row is a multirow.
4965 status.setEnabled(sel_col_start == sel_col_end
4966 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4967 status.setOnOff(tabular.isMultiRow(cur.idx()));
4970 case Tabular::SET_ALL_LINES:
4971 case Tabular::UNSET_ALL_LINES:
4972 case Tabular::SET_BORDER_LINES:
4973 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4976 case Tabular::SET_LINE_TOP:
4977 case Tabular::SET_LINE_BOTTOM:
4978 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4981 case Tabular::SET_LINE_LEFT:
4982 case Tabular::SET_LINE_RIGHT:
4983 status.setEnabled(!tabular.use_booktabs
4984 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4987 case Tabular::TOGGLE_LINE_TOP:
4988 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4989 status.setOnOff(tabular.topLine(cur.idx()));
4992 case Tabular::TOGGLE_LINE_BOTTOM:
4993 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4994 status.setOnOff(tabular.bottomLine(cur.idx()));
4997 case Tabular::TOGGLE_LINE_LEFT:
4998 status.setEnabled(!tabular.use_booktabs
4999 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5000 status.setOnOff(tabular.leftLine(cur.idx()));
5003 case Tabular::TOGGLE_LINE_RIGHT:
5004 status.setEnabled(!tabular.use_booktabs
5005 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5006 status.setOnOff(tabular.rightLine(cur.idx()));
5009 // multirow cells only inherit the alignment of the column if the column has
5010 // no width, otherwise they are left-aligned
5011 // therefore allow always left but right and center only if there is no width
5012 case Tabular::M_ALIGN_LEFT:
5015 case Tabular::ALIGN_LEFT:
5016 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5019 case Tabular::M_ALIGN_RIGHT:
5022 case Tabular::ALIGN_RIGHT:
5023 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5024 && !tabular.getPWidth(cur.idx()).zero()));
5025 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5028 case Tabular::M_ALIGN_CENTER:
5031 case Tabular::ALIGN_CENTER:
5032 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5033 && !tabular.getPWidth(cur.idx()).zero()));
5034 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5037 case Tabular::ALIGN_BLOCK:
5038 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5039 && !tabular.isMultiRow(cur.idx()));
5040 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5043 case Tabular::ALIGN_DECIMAL:
5044 status.setEnabled(!tabular.isMultiRow(cur.idx())
5045 && !tabular.isMultiColumn(cur.idx()));
5046 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5049 case Tabular::M_VALIGN_TOP:
5052 case Tabular::VALIGN_TOP:
5053 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5054 && !tabular.isMultiRow(cur.idx()));
5056 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5059 case Tabular::M_VALIGN_BOTTOM:
5062 case Tabular::VALIGN_BOTTOM:
5063 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5064 && !tabular.isMultiRow(cur.idx()));
5066 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5069 case Tabular::M_VALIGN_MIDDLE:
5072 case Tabular::VALIGN_MIDDLE:
5073 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5074 && !tabular.isMultiRow(cur.idx()));
5076 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5079 case Tabular::SET_LONGTABULAR:
5080 case Tabular::TOGGLE_LONGTABULAR:
5081 // setting as longtable is not allowed when table is inside a float
5082 if (cur.innerInsetOfType(FLOAT_CODE) != 0
5083 || cur.innerInsetOfType(WRAP_CODE) != 0)
5084 status.setEnabled(false);
5086 status.setEnabled(true);
5087 status.setOnOff(tabular.is_long_tabular);
5090 case Tabular::UNSET_LONGTABULAR:
5091 status.setOnOff(!tabular.is_long_tabular);
5094 case Tabular::TOGGLE_ROTATE_TABULAR:
5095 case Tabular::SET_ROTATE_TABULAR:
5096 status.setOnOff(tabular.rotate != 0);
5099 case Tabular::TABULAR_VALIGN_TOP:
5100 status.setEnabled(tabular.tabular_width.zero());
5101 status.setOnOff(tabular.tabular_valignment
5102 == Tabular::LYX_VALIGN_TOP);
5104 case Tabular::TABULAR_VALIGN_MIDDLE:
5105 status.setEnabled(tabular.tabular_width.zero());
5106 status.setOnOff(tabular.tabular_valignment
5107 == Tabular::LYX_VALIGN_MIDDLE);
5109 case Tabular::TABULAR_VALIGN_BOTTOM:
5110 status.setEnabled(tabular.tabular_width.zero());
5111 status.setOnOff(tabular.tabular_valignment
5112 == Tabular::LYX_VALIGN_BOTTOM);
5115 case Tabular::LONGTABULAR_ALIGN_LEFT:
5116 status.setOnOff(tabular.longtabular_alignment
5117 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5119 case Tabular::LONGTABULAR_ALIGN_CENTER:
5120 status.setOnOff(tabular.longtabular_alignment
5121 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5123 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5124 status.setOnOff(tabular.longtabular_alignment
5125 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5128 case Tabular::UNSET_ROTATE_TABULAR:
5129 status.setOnOff(tabular.rotate == 0);
5132 case Tabular::TOGGLE_ROTATE_CELL:
5133 case Tabular::SET_ROTATE_CELL:
5134 status.setOnOff(!oneCellHasRotationState(false,
5135 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5138 case Tabular::UNSET_ROTATE_CELL:
5139 status.setOnOff(!oneCellHasRotationState(true,
5140 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5143 case Tabular::SET_USEBOX:
5144 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5147 // every row can only be one thing:
5148 // either a footer or header
5149 case Tabular::SET_LTFIRSTHEAD:
5150 status.setEnabled(sel_row_start == sel_row_end);
5151 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5154 case Tabular::UNSET_LTFIRSTHEAD:
5155 status.setEnabled(sel_row_start == sel_row_end);
5156 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5159 case Tabular::SET_LTHEAD:
5160 status.setEnabled(sel_row_start == sel_row_end);
5161 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5164 case Tabular::UNSET_LTHEAD:
5165 status.setEnabled(sel_row_start == sel_row_end);
5166 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5169 case Tabular::SET_LTFOOT:
5170 status.setEnabled(sel_row_start == sel_row_end);
5171 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5174 case Tabular::UNSET_LTFOOT:
5175 status.setEnabled(sel_row_start == sel_row_end);
5176 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5179 case Tabular::SET_LTLASTFOOT:
5180 status.setEnabled(sel_row_start == sel_row_end);
5181 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5184 case Tabular::UNSET_LTLASTFOOT:
5185 status.setEnabled(sel_row_start == sel_row_end);
5186 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5189 case Tabular::SET_LTNEWPAGE:
5190 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5192 case Tabular::UNSET_LTNEWPAGE:
5193 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5196 // only one row in head/firsthead/foot/lasthead can be the caption
5197 // and a multirow cannot be set as caption
5198 case Tabular::SET_LTCAPTION:
5199 status.setEnabled(sel_row_start == sel_row_end
5200 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5201 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5202 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5203 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5204 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5205 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5206 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5207 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5208 && !tabular.isMultiRow(sel_row_start));
5209 status.setOnOff(tabular.ltCaption(sel_row_start));
5212 case Tabular::UNSET_LTCAPTION:
5213 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5216 case Tabular::TOGGLE_LTCAPTION:
5217 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5218 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5219 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5220 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5221 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5222 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5223 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5224 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5225 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5226 status.setOnOff(tabular.ltCaption(sel_row_start));
5229 case Tabular::TOGGLE_BOOKTABS:
5230 case Tabular::SET_BOOKTABS:
5231 status.setOnOff(tabular.use_booktabs);
5234 case Tabular::UNSET_BOOKTABS:
5235 status.setOnOff(!tabular.use_booktabs);
5240 status.setEnabled(false);
5247 // function sets an object as defined in FuncStatus.h:
5248 // states OK, Unknown, Disabled, On, Off.
5249 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5250 FuncStatus & status) const
5252 switch (cmd.action()) {
5253 case LFUN_INSET_MODIFY:
5254 if (cmd.getArg(0) != "tabular")
5256 if (cmd.getArg(1) == "for-dialog") {
5257 // The dialog is asking the status of a command
5258 if (&cur.inset() != this)
5260 string action = cmd.getArg(2);
5261 string arg = cmd.getLongArg(3);
5262 return getFeatureStatus(cur, action, arg, status);
5264 // We always enable the lfun if it is coming from the dialog
5265 // because the dialog makes sure all the settings are valid,
5266 // even though the first argument might not be valid now.
5267 status.setEnabled(true);
5271 case LFUN_TABULAR_FEATURE: {
5272 if (&cur.inset() != this)
5274 string action = cmd.getArg(0);
5275 string arg = cmd.getLongArg(1);
5276 return getFeatureStatus(cur, action, arg, status);
5279 case LFUN_CAPTION_INSERT: {
5280 // caption is only allowed in caption cell of longtable
5281 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5282 status.setEnabled(false);
5285 // only standard caption is allowed
5286 string arg = cmd.getArg(0);
5287 if (!arg.empty() && arg != "Standard") {
5288 status.setEnabled(false);
5291 // check if there is already a caption
5292 bool have_caption = false;
5293 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5294 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5295 ParagraphList::const_iterator pend = itc.paragraphs().end();
5296 for (; pit != pend; ++pit) {
5297 InsetList::const_iterator it = pit->insetList().begin();
5298 InsetList::const_iterator end = pit->insetList().end();
5299 for (; it != end; ++it) {
5300 if (it->inset->lyxCode() == CAPTION_CODE) {
5301 have_caption = true;
5306 status.setEnabled(!have_caption);
5310 // These are only enabled inside tabular
5311 case LFUN_CELL_BACKWARD:
5312 case LFUN_CELL_FORWARD:
5313 status.setEnabled(true);
5316 // disable these with multiple cells selected
5317 case LFUN_INSET_INSERT:
5318 case LFUN_TABULAR_INSERT:
5319 case LFUN_FLEX_INSERT:
5320 case LFUN_FLOAT_INSERT:
5321 case LFUN_FLOAT_WIDE_INSERT:
5322 case LFUN_FOOTNOTE_INSERT:
5323 case LFUN_MARGINALNOTE_INSERT:
5324 case LFUN_MATH_INSERT:
5325 case LFUN_MATH_MODE:
5326 case LFUN_MATH_MUTATE:
5327 case LFUN_MATH_DISPLAY:
5328 case LFUN_NOTE_INSERT:
5329 case LFUN_ARGUMENT_INSERT:
5330 case LFUN_BOX_INSERT:
5331 case LFUN_BRANCH_INSERT:
5332 case LFUN_PHANTOM_INSERT:
5333 case LFUN_WRAP_INSERT:
5334 case LFUN_PREVIEW_INSERT:
5335 case LFUN_ERT_INSERT: {
5336 if (cur.selIsMultiCell()) {
5337 status.setEnabled(false);
5340 return cell(cur.idx())->getStatus(cur, cmd, status);
5343 // disable in non-fixed-width cells
5344 case LFUN_PARAGRAPH_BREAK:
5345 // multirow does not allow paragraph breaks
5346 if (tabular.isMultiRow(cur.idx())) {
5347 status.setEnabled(false);
5351 case LFUN_NEWLINE_INSERT:
5352 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
5353 && tabular.getPWidth(cur.idx()).zero()) {
5354 status.setEnabled(false);
5357 return cell(cur.idx())->getStatus(cur, cmd, status);
5359 case LFUN_NEWPAGE_INSERT:
5360 status.setEnabled(false);
5364 if (tabularStackDirty() && theClipboard().isInternal()) {
5365 if (cur.selIsMultiCell()) {
5368 getSelection(cur, rs, re, cs, ce);
5369 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5370 && paste_tabular->nrows() == re - rs + 1)
5371 status.setEnabled(true);
5373 status.setEnabled(false);
5374 status.message(_("Selection size should match clipboard content."));
5377 status.setEnabled(true);
5380 return cell(cur.idx())->getStatus(cur, cmd, status);
5382 case LFUN_INSET_SETTINGS:
5383 // relay this lfun to Inset, not to the cell.
5384 return Inset::getStatus(cur, cmd, status);
5387 // we try to handle this event in the insets dispatch function.
5388 return cell(cur.idx())->getStatus(cur, cmd, status);
5394 Inset::DisplayType InsetTabular::display() const
5396 if (tabular.is_long_tabular) {
5397 switch (tabular.longtabular_alignment) {
5398 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5400 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5402 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5412 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5414 tabular.latex(os, runparams);
5418 int InsetTabular::plaintext(odocstringstream & os,
5419 OutputParams const & runparams, size_t max_length) const
5421 os << '\n'; // output table on a new line
5422 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5423 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5424 return PLAINTEXT_NEWLINE;
5428 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5433 // FIXME: Why not pass a proper DocIterator here?
5435 // if the table is inside a float it doesn't need the informaltable
5436 // wrapper. Search for it.
5437 for (master = owner(); master; master = master->owner())
5438 if (master->lyxCode() == FLOAT_CODE)
5443 os << "<informaltable>";
5446 ret += tabular.docbook(os, runparams);
5448 os << "</informaltable>";
5455 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5457 return tabular.xhtml(xs, rp);
5461 void InsetTabular::validate(LaTeXFeatures & features) const
5463 tabular.validate(features);
5464 features.useInsetLayout(getLayout());
5468 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5470 return tabular.cellInset(idx);
5474 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5476 return tabular.cellInset(idx);
5480 void InsetTabular::cursorPos(BufferView const & bv,
5481 CursorSlice const & sl, bool boundary, int & x, int & y) const
5483 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5485 // y offset correction
5486 y += cellYPos(sl.idx());
5487 y += tabular.textVOffset(sl.idx());
5488 y += tabular.offsetVAlignment();
5490 // x offset correction
5491 x += cellXPos(sl.idx());
5492 x += tabular.textHOffset(sl.idx());
5493 x += ADD_TO_TABULAR_WIDTH;
5497 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5501 Inset const & inset = *tabular.cellInset(cell);
5502 Point o = bv.coordCache().getInsets().xy(&inset);
5503 int const xbeg = o.x_ - tabular.textHOffset(cell);
5504 int const xend = xbeg + tabular.cellWidth(cell);
5505 row_type const row = tabular.cellRow(cell);
5506 int const ybeg = o.y_ - tabular.rowAscent(row)
5507 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5508 int const yend = ybeg + tabular.cellHeight(cell);
5520 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5521 // << " ybeg=" << ybeg << " yend=" << yend
5522 // << " xx=" << xx << " yy=" << yy
5523 // << " dist=" << xx + yy << endl;
5528 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5530 //lyxerr << "InsetTabular::editXY: " << this << endl;
5532 cur.idx() = getNearestCell(cur.bv(), x, y);
5533 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5537 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5539 cur.idx() = getNearestCell(cur.bv(), x, y);
5540 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5544 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5546 idx_type idx_min = 0;
5547 int dist_min = numeric_limits<int>::max();
5548 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5549 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5550 int const d = dist(bv, i, x, y);
5561 int InsetTabular::cellYPos(idx_type const cell) const
5563 row_type row = tabular.cellRow(cell);
5565 for (row_type r = 0; r < row; ++r)
5566 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5567 + tabular.interRowSpace(r + 1);
5572 int InsetTabular::cellXPos(idx_type const cell) const
5574 col_type col = tabular.cellColumn(cell);
5576 for (col_type c = 0; c < col; ++c)
5577 lx += tabular.column_info[c].width;
5582 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5584 row_type const row = tabular.cellRow(cur.idx());
5585 col_type const col = tabular.cellColumn(cur.idx());
5587 if (isRightToLeft(cur)) {
5588 if (tabular.cellColumn(cur.idx()) == 0) {
5589 if (row == tabular.nrows() - 1)
5591 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5596 cur.idx() = tabular.getLastCellInRow(row - 1);
5598 cur.idx() = tabular.cellIndex(row, col - 1);
5601 if (tabular.isLastCell(cur.idx()))
5603 if (cur.idx() == tabular.getLastCellInRow(row))
5604 cur.idx() = tabular.cellIndex(row + 1, 0);
5606 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5607 cur.idx() = tabular.cellIndex(row, colnextcell);
5611 cur.boundary(false);
5613 if (cur.selIsMultiCell()) {
5614 cur.pit() = cur.lastpit();
5615 cur.pos() = cur.lastpos();
5622 // in visual mode, place cursor at extreme left or right
5624 switch(entry_from) {
5626 case ENTRY_DIRECTION_RIGHT:
5627 cur.posVisToRowExtremity(false /* !left */);
5629 case ENTRY_DIRECTION_LEFT:
5630 cur.posVisToRowExtremity(true /* left */);
5632 case ENTRY_DIRECTION_IGNORE:
5633 // nothing to do in this case
5637 cur.setCurrentFont();
5641 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5643 row_type const row = tabular.cellRow(cur.idx());
5644 col_type const col = tabular.cellColumn(cur.idx());
5646 if (isRightToLeft(cur)) {
5647 if (cur.idx() == tabular.getLastCellInRow(row)) {
5650 cur.idx() = tabular.getFirstCellInRow(row);
5651 cur.idx() = tabular.cellAbove(cur.idx());
5653 if (tabular.isLastCell(cur.idx()))
5655 if (cur.idx() == tabular.getLastCellInRow(row))
5656 cur.idx() = tabular.cellIndex(row + 1, 0);
5658 cur.idx() = tabular.cellIndex(row, col + 1);
5661 if (cur.idx() == 0) // first cell
5664 cur.idx() = tabular.getLastCellInRow(row - 1);
5666 cur.idx() = tabular.cellIndex(row, col - 1);
5669 if (cur.selIsMultiCell()) {
5670 cur.pit() = cur.lastpit();
5671 cur.pos() = cur.lastpos();
5675 cur.pit() = cur.lastpit();
5676 cur.pos() = cur.lastpos();
5678 // in visual mode, place cursor at extreme left or right
5680 switch(entry_from) {
5682 case ENTRY_DIRECTION_RIGHT:
5683 cur.posVisToRowExtremity(false /* !left */);
5685 case ENTRY_DIRECTION_LEFT:
5686 cur.posVisToRowExtremity(true /* left */);
5688 case ENTRY_DIRECTION_IGNORE:
5689 // nothing to do in this case
5693 cur.setCurrentFont();
5697 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5699 cur.recordUndoInset(this);
5701 istringstream is(argument);
5702 // limit the size of strings we read to avoid memory problems
5706 size_t safe_guard = 0;
5711 if (safe_guard > 1000) {
5712 LYXERR0("parameter max count reached!");
5716 Tabular::Feature action = Tabular::LAST_ACTION;
5719 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5720 if (s != tabularFeature[i].feature)
5722 action = tabularFeature[i].action;
5725 if (action == Tabular::LAST_ACTION) {
5726 LYXERR0("Feature not found " << s);
5730 if (tabularFeature[i].need_value)
5732 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5733 tabularFeatures(cur, action, val);
5738 static void checkLongtableSpecial(Tabular::ltType & ltt,
5739 string const & special, bool & flag)
5741 if (special == "dl_above") {
5744 } else if (special == "dl_below") {
5745 ltt.bottomDL = flag;
5747 } else if (special == "empty") {
5757 bool InsetTabular::oneCellHasRotationState(bool rotated,
5758 row_type row_start, row_type row_end,
5759 col_type col_start, col_type col_end) const
5761 for (row_type r = row_start; r <= row_end; ++r)
5762 for (col_type c = col_start; c <= col_end; ++c)
5764 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5767 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5774 void InsetTabular::tabularFeatures(Cursor & cur,
5775 Tabular::Feature feature, string const & value)
5777 col_type sel_col_start;
5778 col_type sel_col_end;
5779 row_type sel_row_start;
5780 row_type sel_row_end;
5781 bool setLines = false;
5782 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5783 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5787 case Tabular::M_ALIGN_LEFT:
5788 case Tabular::ALIGN_LEFT:
5789 setAlign = LYX_ALIGN_LEFT;
5792 case Tabular::M_ALIGN_RIGHT:
5793 case Tabular::ALIGN_RIGHT:
5794 setAlign = LYX_ALIGN_RIGHT;
5797 case Tabular::M_ALIGN_CENTER:
5798 case Tabular::ALIGN_CENTER:
5799 setAlign = LYX_ALIGN_CENTER;
5802 case Tabular::ALIGN_BLOCK:
5803 setAlign = LYX_ALIGN_BLOCK;
5806 case Tabular::ALIGN_DECIMAL:
5807 setAlign = LYX_ALIGN_DECIMAL;
5810 case Tabular::M_VALIGN_TOP:
5811 case Tabular::VALIGN_TOP:
5812 setVAlign = Tabular::LYX_VALIGN_TOP;
5815 case Tabular::M_VALIGN_BOTTOM:
5816 case Tabular::VALIGN_BOTTOM:
5817 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5820 case Tabular::M_VALIGN_MIDDLE:
5821 case Tabular::VALIGN_MIDDLE:
5822 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5829 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5830 row_type const row = tabular.cellRow(cur.idx());
5831 col_type const column = tabular.cellColumn(cur.idx());
5833 Tabular::ltType ltt;
5837 case Tabular::SET_TABULAR_WIDTH:
5838 tabular.setTabularWidth(Length(value));
5841 case Tabular::SET_PWIDTH: {
5842 Length const len(value);
5843 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5844 tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
5846 && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
5847 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5852 case Tabular::SET_MPWIDTH:
5853 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5856 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
5857 bool const varwidth = value == "on";
5858 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5859 tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
5863 case Tabular::SET_MROFFSET:
5864 tabular.setMROffset(cur, cur.idx(), Length(value));
5867 case Tabular::SET_SPECIAL_COLUMN:
5868 case Tabular::SET_SPECIAL_MULTICOLUMN:
5869 if (value == "none")
5870 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5872 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5875 case Tabular::APPEND_ROW:
5876 // append the row into the tabular
5877 tabular.appendRow(row);
5880 case Tabular::APPEND_COLUMN:
5881 // append the column into the tabular
5882 tabular.appendColumn(column);
5883 cur.idx() = tabular.cellIndex(row, column);
5886 case Tabular::DELETE_ROW:
5887 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5888 for (col_type c = 0; c < tabular.ncols(); c++)
5889 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5890 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5893 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5894 tabular.deleteRow(sel_row_start);
5895 if (sel_row_start >= tabular.nrows())
5897 cur.idx() = tabular.cellIndex(sel_row_start, column);
5900 cur.selection(false);
5903 case Tabular::DELETE_COLUMN:
5904 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5905 for (row_type r = 0; r < tabular.nrows(); r++)
5906 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5907 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5910 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5911 for (row_type r = 0; r < tabular.nrows(); r++)
5912 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5913 tabular.leftLine(tabular.cellIndex(r, 0)));
5916 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5917 tabular.deleteColumn(sel_col_start);
5918 if (sel_col_start >= tabular.ncols())
5920 cur.idx() = tabular.cellIndex(row, sel_col_start);
5923 cur.selection(false);
5926 case Tabular::COPY_ROW:
5927 tabular.copyRow(row);
5930 case Tabular::COPY_COLUMN:
5931 tabular.copyColumn(column);
5932 cur.idx() = tabular.cellIndex(row, column);
5935 case Tabular::MOVE_COLUMN_RIGHT:
5936 tabular.moveColumn(column, Tabular::RIGHT);
5937 cur.idx() = tabular.cellIndex(row, column + 1);
5940 case Tabular::MOVE_COLUMN_LEFT:
5941 tabular.moveColumn(column, Tabular::LEFT);
5942 cur.idx() = tabular.cellIndex(row, column - 1);
5945 case Tabular::MOVE_ROW_DOWN:
5946 tabular.moveRow(row, Tabular::DOWN);
5947 cur.idx() = tabular.cellIndex(row + 1, column);
5950 case Tabular::MOVE_ROW_UP:
5951 tabular.moveRow(row, Tabular::UP);
5952 cur.idx() = tabular.cellIndex(row - 1, column);
5955 case Tabular::SET_LINE_TOP:
5956 case Tabular::TOGGLE_LINE_TOP: {
5957 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5958 ? (value == "true") : !tabular.topLine(cur.idx());
5959 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5960 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5961 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5965 case Tabular::SET_LINE_BOTTOM:
5966 case Tabular::TOGGLE_LINE_BOTTOM: {
5967 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5968 ? (value == "true") : !tabular.bottomLine(cur.idx());
5969 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5970 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5971 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5975 case Tabular::SET_LINE_LEFT:
5976 case Tabular::TOGGLE_LINE_LEFT: {
5977 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5978 ? (value == "true") : !tabular.leftLine(cur.idx());
5979 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5980 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5981 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5985 case Tabular::SET_LINE_RIGHT:
5986 case Tabular::TOGGLE_LINE_RIGHT: {
5987 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5988 ? (value == "true") : !tabular.rightLine(cur.idx());
5989 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5990 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5991 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5995 case Tabular::M_ALIGN_LEFT:
5996 case Tabular::M_ALIGN_RIGHT:
5997 case Tabular::M_ALIGN_CENTER:
5998 case Tabular::ALIGN_LEFT:
5999 case Tabular::ALIGN_RIGHT:
6000 case Tabular::ALIGN_CENTER:
6001 case Tabular::ALIGN_BLOCK:
6002 case Tabular::ALIGN_DECIMAL:
6003 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6004 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6005 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
6006 !tabular.getPWidth(c).zero());
6009 case Tabular::M_VALIGN_TOP:
6010 case Tabular::M_VALIGN_BOTTOM:
6011 case Tabular::M_VALIGN_MIDDLE:
6014 case Tabular::VALIGN_TOP:
6015 case Tabular::VALIGN_BOTTOM:
6016 case Tabular::VALIGN_MIDDLE:
6017 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6018 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6019 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6022 case Tabular::SET_MULTICOLUMN: {
6023 if (!cur.selection()) {
6024 // just multicol for one single cell
6025 // check whether we are completely in a multicol
6026 if (!tabular.isMultiColumn(cur.idx()))
6027 tabular.setMultiColumn(cur, cur.idx(), 1,
6028 tabular.rightLine(cur.idx()));
6031 // we have a selection so this means we just add all these
6032 // cells to form a multicolumn cell
6033 idx_type const s_start = cur.selBegin().idx();
6034 row_type const col_start = tabular.cellColumn(s_start);
6035 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6036 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6037 tabular.rightLine(cur.selEnd().idx()));
6040 cur.selection(false);
6044 case Tabular::UNSET_MULTICOLUMN: {
6045 if (!cur.selection()) {
6046 if (tabular.isMultiColumn(cur.idx()))
6047 tabular.unsetMultiColumn(cur.idx());
6052 case Tabular::MULTICOLUMN: {
6053 if (!cur.selection()) {
6054 if (tabular.isMultiColumn(cur.idx()))
6055 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6057 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6061 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6062 row_type const r = sel_row_start;
6063 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6064 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6067 // If the selection contains at least one singlecol cell
6068 // or multiple multicol cells,
6069 // we assume the user will merge is to a single multicol
6071 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6073 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6077 case Tabular::SET_MULTIROW: {
6078 if (!cur.selection()) {
6079 // just multirow for one single cell
6080 // check whether we are completely in a multirow
6081 if (!tabular.isMultiRow(cur.idx()))
6082 tabular.setMultiRow(cur, cur.idx(), 1,
6083 tabular.bottomLine(cur.idx()),
6084 tabular.getAlignment(cur.idx()));
6087 // we have a selection so this means we just add all this
6088 // cells to form a multirow cell
6089 idx_type const s_start = cur.selBegin().idx();
6090 row_type const row_start = tabular.cellRow(s_start);
6091 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6092 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6093 tabular.bottomLine(cur.selEnd().idx()),
6094 tabular.getAlignment(cur.selEnd().idx()));
6097 cur.selection(false);
6101 case Tabular::UNSET_MULTIROW: {
6102 if (!cur.selection()) {
6103 if (tabular.isMultiRow(cur.idx()))
6104 tabular.unsetMultiRow(cur.idx());
6109 case Tabular::MULTIROW: {
6110 if (!cur.selection()) {
6111 if (tabular.isMultiRow(cur.idx()))
6112 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6114 tabularFeatures(cur, Tabular::SET_MULTIROW);
6118 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6119 col_type const c = sel_col_start;
6120 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6121 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6124 // If the selection contains at least one singlerow cell
6125 // or multiple multirow cells,
6126 // we assume the user will merge is to a single multirow
6128 tabularFeatures(cur, Tabular::SET_MULTIROW);
6130 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6134 case Tabular::SET_ALL_LINES:
6137 case Tabular::UNSET_ALL_LINES:
6138 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6139 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6140 idx_type const cell = tabular.cellIndex(r, c);
6141 tabular.setTopLine(cell, setLines);
6142 tabular.setBottomLine(cell, setLines);
6143 tabular.setRightLine(cell, setLines);
6144 tabular.setLeftLine(cell, setLines);
6148 case Tabular::SET_BORDER_LINES:
6149 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6150 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6151 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6153 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6154 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6155 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6159 case Tabular::TOGGLE_LONGTABULAR:
6160 if (tabular.is_long_tabular)
6161 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6163 tabular.is_long_tabular = true;
6166 case Tabular::SET_LONGTABULAR:
6167 tabular.is_long_tabular = true;
6170 case Tabular::UNSET_LONGTABULAR:
6171 for (row_type r = 0; r < tabular.nrows(); ++r) {
6172 if (tabular.ltCaption(r)) {
6173 cur.idx() = tabular.cellIndex(r, 0);
6176 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6179 tabular.is_long_tabular = false;
6182 case Tabular::SET_ROTATE_TABULAR:
6183 tabular.rotate = convert<int>(value);
6186 case Tabular::UNSET_ROTATE_TABULAR:
6190 case Tabular::TOGGLE_ROTATE_TABULAR:
6191 // when pressing the rotate button we default to 90° rotation
6192 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6195 case Tabular::TABULAR_VALIGN_TOP:
6196 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6199 case Tabular::TABULAR_VALIGN_MIDDLE:
6200 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6203 case Tabular::TABULAR_VALIGN_BOTTOM:
6204 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6207 case Tabular::LONGTABULAR_ALIGN_LEFT:
6208 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6211 case Tabular::LONGTABULAR_ALIGN_CENTER:
6212 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6215 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6216 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6219 case Tabular::SET_ROTATE_CELL:
6220 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6221 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6222 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6225 case Tabular::UNSET_ROTATE_CELL:
6226 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6227 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6228 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6231 case Tabular::TOGGLE_ROTATE_CELL:
6233 bool oneNotRotated = oneCellHasRotationState(false,
6234 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6236 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6237 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6238 // when pressing the rotate cell button we default to 90° rotation
6240 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6242 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6247 case Tabular::SET_USEBOX: {
6248 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6249 if (val == tabular.getUsebox(cur.idx()))
6250 val = Tabular::BOX_NONE;
6251 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6252 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6253 tabular.setUsebox(tabular.cellIndex(r, c), val);
6257 case Tabular::UNSET_LTFIRSTHEAD:
6260 case Tabular::SET_LTFIRSTHEAD:
6261 tabular.getRowOfLTFirstHead(row, ltt);
6262 checkLongtableSpecial(ltt, value, flag);
6263 tabular.setLTHead(row, flag, ltt, true);
6266 case Tabular::UNSET_LTHEAD:
6269 case Tabular::SET_LTHEAD:
6270 tabular.getRowOfLTHead(row, ltt);
6271 checkLongtableSpecial(ltt, value, flag);
6272 tabular.setLTHead(row, flag, ltt, false);
6275 case Tabular::UNSET_LTFOOT:
6278 case Tabular::SET_LTFOOT:
6279 tabular.getRowOfLTFoot(row, ltt);
6280 checkLongtableSpecial(ltt, value, flag);
6281 tabular.setLTFoot(row, flag, ltt, false);
6284 case Tabular::UNSET_LTLASTFOOT:
6287 case Tabular::SET_LTLASTFOOT:
6288 tabular.getRowOfLTLastFoot(row, ltt);
6289 checkLongtableSpecial(ltt, value, flag);
6290 tabular.setLTFoot(row, flag, ltt, true);
6293 case Tabular::UNSET_LTNEWPAGE:
6296 case Tabular::SET_LTNEWPAGE:
6297 tabular.setLTNewPage(row, flag);
6300 case Tabular::SET_LTCAPTION: {
6301 if (tabular.ltCaption(row))
6303 cur.idx() = tabular.setLTCaption(cur, row, true);
6306 cur.selection(false);
6307 // If a row is set as caption, then also insert
6308 // a caption. Otherwise the LaTeX output is broken.
6309 // Select cell if it is non-empty
6310 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6311 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6312 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6316 case Tabular::UNSET_LTCAPTION: {
6317 if (!tabular.ltCaption(row))
6319 cur.idx() = tabular.setLTCaption(cur, row, false);
6322 cur.selection(false);
6323 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6324 if (lyx::getStatus(fr).enabled())
6329 case Tabular::TOGGLE_LTCAPTION: {
6330 if (tabular.ltCaption(row))
6331 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6333 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6337 case Tabular::TOGGLE_BOOKTABS:
6338 tabular.use_booktabs = !tabular.use_booktabs;
6341 case Tabular::SET_BOOKTABS:
6342 tabular.use_booktabs = true;
6345 case Tabular::UNSET_BOOKTABS:
6346 tabular.use_booktabs = false;
6349 case Tabular::SET_TOP_SPACE: {
6351 if (value == "default")
6352 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6353 tabular.row_info[r].top_space_default = true;
6354 else if (value == "none")
6355 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6356 tabular.row_info[r].top_space_default = false;
6357 tabular.row_info[r].top_space = len;
6359 else if (isValidLength(value, &len))
6360 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6361 tabular.row_info[r].top_space_default = false;
6362 tabular.row_info[r].top_space = len;
6367 case Tabular::SET_BOTTOM_SPACE: {
6369 if (value == "default")
6370 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6371 tabular.row_info[r].bottom_space_default = true;
6372 else if (value == "none")
6373 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6374 tabular.row_info[r].bottom_space_default = false;
6375 tabular.row_info[r].bottom_space = len;
6377 else if (isValidLength(value, &len))
6378 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6379 tabular.row_info[r].bottom_space_default = false;
6380 tabular.row_info[r].bottom_space = len;
6385 case Tabular::SET_INTERLINE_SPACE: {
6387 if (value == "default")
6388 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6389 tabular.row_info[r].interline_space_default = true;
6390 else if (value == "none")
6391 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6392 tabular.row_info[r].interline_space_default = false;
6393 tabular.row_info[r].interline_space = len;
6395 else if (isValidLength(value, &len))
6396 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6397 tabular.row_info[r].interline_space_default = false;
6398 tabular.row_info[r].interline_space = len;
6403 case Tabular::SET_DECIMAL_POINT:
6404 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6405 tabular.column_info[c].decimal_point = from_utf8(value);
6408 // dummy stuff just to avoid warnings
6409 case Tabular::LAST_ACTION:
6415 bool InsetTabular::copySelection(Cursor & cur)
6417 if (!cur.selection())
6422 getSelection(cur, rs, re, cs, ce);
6424 paste_tabular.reset(new Tabular(tabular));
6426 for (row_type r = 0; r < rs; ++r)
6427 paste_tabular->deleteRow(0);
6429 row_type const rows = re - rs + 1;
6430 while (paste_tabular->nrows() > rows)
6431 paste_tabular->deleteRow(rows);
6433 for (col_type c = 0; c < cs; ++c)
6434 paste_tabular->deleteColumn(0);
6436 col_type const columns = ce - cs + 1;
6437 while (paste_tabular->ncols() > columns)
6438 paste_tabular->deleteColumn(columns);
6440 paste_tabular->setBuffer(tabular.buffer());
6442 odocstringstream os;
6443 OutputParams const runparams(0);
6444 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6445 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6446 cap::copySelection(cur, os.str());
6448 // mark tabular stack dirty
6449 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6450 // when we (hopefully) have a one-for-all paste mechanism.
6451 // This must be called after cap::copySelection.
6452 dirtyTabularStack(true);
6458 bool InsetTabular::pasteClipboard(Cursor & cur)
6462 col_type actcol = tabular.cellColumn(cur.idx());
6463 row_type actrow = tabular.cellRow(cur.idx());
6465 if (cur.selIsMultiCell()) {
6468 getSelection(cur, actrow, re, actcol, ce);
6471 for (row_type r1 = 0, r2 = actrow;
6472 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6474 for (col_type c1 = 0, c2 = actcol;
6475 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6477 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6478 tabular.isPartOfMultiColumn(r2, c2))
6480 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6484 if (tabular.isPartOfMultiColumn(r2, c2)) {
6488 shared_ptr<InsetTableCell> inset(
6489 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6490 tabular.setCellInset(r2, c2, inset);
6491 // FIXME?: why do we need to do this explicitly? (EL)
6492 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6494 // FIXME: change tracking (MG)
6495 inset->setChange(Change(buffer().params().track_changes ?
6496 Change::INSERTED : Change::UNCHANGED));
6505 void InsetTabular::cutSelection(Cursor & cur)
6507 if (!cur.selection())
6512 getSelection(cur, rs, re, cs, ce);
6513 for (row_type r = rs; r <= re; ++r) {
6514 for (col_type c = cs; c <= ce; ++c) {
6515 shared_ptr<InsetTableCell> t
6516 = cell(tabular.cellIndex(r, c));
6517 if (buffer().params().track_changes)
6518 // FIXME: Change tracking (MG)
6519 t->setChange(Change(Change::DELETED));
6525 // cursor position might be invalid now
6526 if (cur.pit() > cur.lastpit())
6527 cur.pit() = cur.lastpit();
6528 if (cur.pos() > cur.lastpos())
6529 cur.pos() = cur.lastpos();
6530 cur.clearSelection();
6534 bool InsetTabular::isRightToLeft(Cursor & cur) const
6536 // LASSERT: It might be better to abandon this Buffer.
6537 LASSERT(cur.depth() > 1, return false);
6538 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6539 pos_type const parentpos = cur[cur.depth() - 2].pos();
6540 return parentpar.getFontSettings(buffer().params(),
6541 parentpos).language()->rightToLeft();
6545 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6548 LASSERT(stidx <= enidx, return docstring());
6550 col_type const col1 = tabular.cellColumn(stidx);
6551 col_type const col2 = tabular.cellColumn(enidx);
6552 row_type const row1 = tabular.cellRow(stidx);
6553 row_type const row2 = tabular.cellRow(enidx);
6555 for (col_type col = col1; col <= col2; col++)
6556 for (row_type row = row1; row <= row2; row++) {
6561 retval += tabular.cellInset(row, col)->asString(intoInsets);
6567 void InsetTabular::getSelection(Cursor & cur,
6568 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6570 CursorSlice const & beg = cur.selBegin();
6571 CursorSlice const & end = cur.selEnd();
6572 cs = tabular.cellColumn(beg.idx());
6573 ce = tabular.cellColumn(end.idx());
6577 rs = tabular.cellRow(beg.idx());
6578 re = tabular.cellRow(end.idx());
6584 Text * InsetTabular::getText(int idx) const
6586 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6590 void InsetTabular::setChange(Change const & change)
6592 for (idx_type idx = 0; idx < nargs(); ++idx)
6593 cell(idx)->setChange(change);
6597 void InsetTabular::acceptChanges()
6599 for (idx_type idx = 0; idx < nargs(); ++idx)
6600 cell(idx)->acceptChanges();
6604 void InsetTabular::rejectChanges()
6606 for (idx_type idx = 0; idx < nargs(); ++idx)
6607 cell(idx)->rejectChanges();
6611 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6613 return tabular.getPWidth(cell).zero();
6617 bool InsetTabular::forcePlainLayout(idx_type cell) const
6619 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
6623 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6626 if (buf.length() <= 0)
6631 col_type maxCols = 1;
6632 size_t const len = buf.length();
6636 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6644 maxCols = max(cols, maxCols);
6650 maxCols = max(cols, maxCols);
6656 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6657 loctab = paste_tabular.get();
6658 dirtyTabularStack(true);
6661 cell = bv.cursor().idx();
6662 ocol = tabular.cellColumn(cell);
6663 row = tabular.cellRow(cell);
6667 idx_type const cells = loctab->numberofcells;
6670 rows = loctab->nrows();
6671 col_type const columns = loctab->ncols();
6673 while (cell < cells && p < len && row < rows &&
6674 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6680 // we can only set this if we are not too far right
6681 if (cols < columns) {
6682 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6683 Font const font = bv.textMetrics(&inset->text()).
6685 inset->setText(buf.substr(op, p - op), font,
6686 buffer().params().track_changes);
6692 // we can only set this if we are not too far right
6693 if (cols < columns) {
6694 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6695 Font const font = bv.textMetrics(&inset->text()).
6697 inset->setText(buf.substr(op, p - op), font,
6698 buffer().params().track_changes);
6703 cell = loctab->cellIndex(row, cols);
6709 // check for the last cell if there is no trailing '\n'
6710 if (cell < cells && op < len) {
6711 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6712 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6713 inset->setText(buf.substr(op, len - op), font,
6714 buffer().params().track_changes);
6720 void InsetTabular::addPreview(DocIterator const & inset_pos,
6721 PreviewLoader & loader) const
6723 DocIterator cell_pos = inset_pos;
6725 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6726 for (row_type r = 0; r < tabular.nrows(); ++r) {
6727 for (col_type c = 0; c < tabular.ncols(); ++c) {
6728 cell_pos.top().idx() = tabular.cellIndex(r, c);
6729 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6735 bool InsetTabular::completionSupported(Cursor const & cur) const
6737 Cursor const & bvCur = cur.bv().cursor();
6738 if (&bvCur.inset() != this)
6740 return cur.text()->completionSupported(cur);
6744 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6746 return completionSupported(cur);
6750 bool InsetTabular::automaticInlineCompletion() const
6752 return lyxrc.completion_inline_text;
6756 bool InsetTabular::automaticPopupCompletion() const
6758 return lyxrc.completion_popup_text;
6762 bool InsetTabular::showCompletionCursor() const
6764 return lyxrc.completion_cursor_text;
6768 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6770 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6774 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6776 if (!completionSupported(cur))
6778 return cur.text()->completionPrefix(cur);
6782 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6784 if (!completionSupported(cur))
6787 return cur.text()->insertCompletion(cur, s, finished);
6791 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6792 Dimension & dim) const
6794 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6795 tm.completionPosAndDim(cur, x, y, dim);
6799 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6801 istringstream data(in);
6803 lex.setStream(data);
6810 if (!lex || token != "tabular") {
6811 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6815 // This is part of the inset proper that is usually swallowed
6816 // by Buffer::readInset
6818 if (!lex || token != "Tabular") {
6819 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6827 string InsetTabular::params2string(InsetTabular const & inset)
6830 data << "tabular" << ' ';
6832 data << "\\end_inset\n";
6837 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6839 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6840 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6841 if (!tabular.isPartOfMultiColumn(r,c) &&
6842 !tabular.isPartOfMultiRow(r,c))
6845 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6846 ParagraphList::iterator it = parlist.begin();
6847 ParagraphList::iterator const en = parlist.end();
6848 for (; it != en; ++it)
6849 it->setLayout(dc.plainLayout());