2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
16 * \author Scott Kostyshak
18 * Full author contact details are available in file CREDITS.
23 #include "InsetTabular.h"
25 #include "buffer_funcs.h"
27 #include "BufferParams.h"
28 #include "BufferView.h"
29 #include "CoordCache.h"
32 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
36 #include "InsetIterator.h"
37 #include "InsetList.h"
39 #include "LaTeXFeatures.h"
43 #include "MetricsInfo.h"
44 #include "OutputParams.h"
45 #include "output_xhtml.h"
46 #include "Paragraph.h"
47 #include "ParagraphParameters.h"
48 #include "ParIterator.h"
50 #include "texstream.h"
51 #include "TextClass.h"
52 #include "TextMetrics.h"
54 #include "frontends/Application.h"
55 #include "frontends/alert.h"
56 #include "frontends/Clipboard.h"
57 #include "frontends/Painter.h"
58 #include "frontends/Selection.h"
60 #include "support/convert.h"
61 #include "support/debug.h"
62 #include "support/docstream.h"
63 #include "support/FileName.h"
64 #include "support/gettext.h"
65 #include "support/lassert.h"
66 #include "support/lstrings.h"
67 #include "support/unique_ptr.h"
76 using namespace lyx::support;
82 using cap::dirtyTabularStack;
83 using cap::tabularStackDirty;
85 using graphics::PreviewLoader;
87 using frontend::Painter;
88 using frontend::Clipboard;
90 namespace Alert = frontend::Alert;
95 int const ADD_TO_HEIGHT = 2; // in cell
96 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
97 int const default_line_space = 10; // ?
98 int const WIDTH_OF_LINE = 5; // space between double lines
102 unique_ptr<Tabular> paste_tabular;
105 struct TabularFeature {
106 Tabular::Feature action;
112 TabularFeature tabularFeature[] =
114 // the SET/UNSET actions are used by the table dialog,
115 // the TOGGLE actions by the table toolbar buttons
116 // FIXME: these values have been hardcoded in InsetMathGrid and other
118 { Tabular::APPEND_ROW, "append-row", false },
119 { Tabular::APPEND_COLUMN, "append-column", false },
120 { Tabular::DELETE_ROW, "delete-row", false },
121 { Tabular::DELETE_COLUMN, "delete-column", false },
122 { Tabular::COPY_ROW, "copy-row", false },
123 { Tabular::COPY_COLUMN, "copy-column", false },
124 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
125 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
126 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
127 { Tabular::MOVE_ROW_UP, "move-row-up", false },
128 { Tabular::SET_LINE_TOP, "set-line-top", true },
129 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
130 { Tabular::SET_LINE_LEFT, "set-line-left", true },
131 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
132 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
133 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
134 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
135 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
136 { Tabular::ALIGN_LEFT, "align-left", false },
137 { Tabular::ALIGN_RIGHT, "align-right", false },
138 { Tabular::ALIGN_CENTER, "align-center", false },
139 { Tabular::ALIGN_BLOCK, "align-block", false },
140 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
141 { Tabular::VALIGN_TOP, "valign-top", false },
142 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
143 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
144 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
145 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
146 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
147 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
148 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
149 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
150 { Tabular::MULTICOLUMN, "multicolumn", false },
151 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
152 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
153 { Tabular::MULTIROW, "multirow", false },
154 { Tabular::SET_MULTIROW, "set-multirow", false },
155 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
156 { Tabular::SET_MROFFSET, "set-mroffset", true },
157 { Tabular::SET_ALL_LINES, "set-all-lines", false },
158 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
159 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
160 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
161 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
162 { Tabular::SET_PWIDTH, "set-pwidth", true },
163 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
164 { Tabular::TOGGLE_VARWIDTH_COLUMN, "toggle-varwidth-column", true },
165 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
166 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
167 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
168 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
169 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
170 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
171 { Tabular::SET_USEBOX, "set-usebox", true },
172 { Tabular::SET_LTHEAD, "set-lthead", true },
173 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
174 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
175 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
176 { Tabular::SET_LTFOOT, "set-ltfoot", true },
177 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
178 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
179 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
180 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
181 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
182 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
183 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
184 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
185 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
186 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
187 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
188 { Tabular::SET_BOOKTABS, "set-booktabs", false },
189 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
190 { Tabular::SET_TOP_SPACE, "set-top-space", true },
191 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
192 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
193 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
194 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
195 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
196 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
197 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
198 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
199 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
200 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
201 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
202 { Tabular::LAST_ACTION, "", false }
206 string const tostr(LyXAlignment const & num)
211 case LYX_ALIGN_BLOCK:
215 case LYX_ALIGN_CENTER:
217 case LYX_ALIGN_RIGHT:
219 case LYX_ALIGN_LAYOUT:
221 case LYX_ALIGN_SPECIAL:
223 case LYX_ALIGN_DECIMAL:
230 string const tostr(Tabular::HAlignment const & num)
233 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
235 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
237 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
244 string const tostr(Tabular::VAlignment const & num)
247 case Tabular::LYX_VALIGN_TOP:
249 case Tabular::LYX_VALIGN_MIDDLE:
251 case Tabular::LYX_VALIGN_BOTTOM:
258 string const tostr(Tabular::BoxType const & num)
261 case Tabular::BOX_NONE:
263 case Tabular::BOX_PARBOX:
265 case Tabular::BOX_MINIPAGE:
267 case Tabular::BOX_VARWIDTH:
274 // I would have liked a fromstr template a lot better. (Lgb)
275 bool string2type(string const & str, LyXAlignment & num)
278 num = LYX_ALIGN_NONE;
279 else if (str == "block")
280 num = LYX_ALIGN_BLOCK;
281 else if (str == "left")
282 num = LYX_ALIGN_LEFT;
283 else if (str == "center")
284 num = LYX_ALIGN_CENTER;
285 else if (str == "right")
286 num = LYX_ALIGN_RIGHT;
287 else if (str == "decimal")
288 num = LYX_ALIGN_DECIMAL;
295 bool string2type(string const & str, Tabular::HAlignment & num)
298 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
299 else if (str == "center" )
300 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
301 else if (str == "right")
302 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
309 bool string2type(string const & str, Tabular::VAlignment & num)
312 num = Tabular::LYX_VALIGN_TOP;
313 else if (str == "middle" )
314 num = Tabular::LYX_VALIGN_MIDDLE;
315 else if (str == "bottom")
316 num = Tabular::LYX_VALIGN_BOTTOM;
323 bool string2type(string const & str, Tabular::BoxType & num)
326 num = Tabular::BOX_NONE;
327 else if (str == "parbox")
328 num = Tabular::BOX_PARBOX;
329 else if (str == "minipage")
330 num = Tabular::BOX_MINIPAGE;
331 else if (str == "varwidth")
332 num = Tabular::BOX_VARWIDTH;
339 bool string2type(string const & str, bool & num)
343 else if (str == "false")
351 bool getTokenValue(string const & str, char const * token, string & ret)
354 size_t token_length = strlen(token);
355 size_t pos = str.find(token);
357 if (pos == string::npos || pos + token_length + 1 >= str.length()
358 || str[pos + token_length] != '=')
360 pos += token_length + 1;
362 if (ch != '"' && ch != '\'') { // only read till next space
366 while (pos < str.length() - 1 && str[++pos] != ch)
373 bool getTokenValue(string const & str, char const * token, docstring & ret)
376 bool const success = getTokenValue(str, token, tmp);
377 ret = from_utf8(tmp);
382 bool getTokenValue(string const & str, char const * token, int & num)
386 if (!getTokenValue(str, token, tmp))
388 num = convert<int>(tmp);
393 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
396 return getTokenValue(str, token, tmp) && string2type(tmp, num);
400 bool getTokenValue(string const & str, char const * token,
401 Tabular::HAlignment & num)
404 return getTokenValue(str, token, tmp) && string2type(tmp, num);
408 bool getTokenValue(string const & str, char const * token,
409 Tabular::VAlignment & num)
412 return getTokenValue(str, token, tmp) && string2type(tmp, num);
416 bool getTokenValue(string const & str, char const * token,
417 Tabular::BoxType & num)
420 return getTokenValue(str, token, tmp) && string2type(tmp, num);
424 bool getTokenValue(string const & str, char const * token, bool & flag)
426 // set the flag always to false as this should be the default for bools
427 // not in the file-format.
430 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
434 bool getTokenValue(string const & str, char const * token, Length & len)
436 // set the length to be zero() as default as this it should be if not
437 // in the file format.
440 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
444 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
449 if (!getTokenValue(str, token, tmp))
451 if (tmp == "default") {
455 return isValidLength(tmp, &len);
459 void l_getline(istream & is, string & str)
462 while (str.empty()) {
464 if (!str.empty() && str[str.length() - 1] == '\r')
465 str.erase(str.length() - 1);
470 string const write_attribute(string const & name, T const & t)
472 string const s = tostr(t);
473 return s.empty() ? s : " " + name + "=\"" + s + "\"";
477 string const write_attribute(string const & name, string const & t)
479 return t.empty() ? t : " " + name + "=\"" + t + "\"";
484 string const write_attribute(string const & name, docstring const & t)
486 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
491 string const write_attribute(string const & name, bool const & b)
493 // we write only true attribute values so we remove a bit of the
494 // file format bloat for tabulars.
495 return b ? write_attribute(name, convert<string>(b)) : string();
500 string const write_attribute(string const & name, int const & i)
502 // we write only true attribute values so we remove a bit of the
503 // file format bloat for tabulars.
504 return i ? write_attribute(name, convert<string>(i)) : string();
509 string const write_attribute(string const & name, Tabular::idx_type const & i)
511 // we write only true attribute values so we remove a bit of the
512 // file format bloat for tabulars.
513 return i ? write_attribute(name, convert<string>(i)) : string();
518 string const write_attribute(string const & name, Length const & value)
520 // we write only the value if we really have one same reson as above.
521 return value.zero() ? string() : write_attribute(name, value.asString());
527 string const featureAsString(Tabular::Feature action)
529 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
530 if (tabularFeature[i].action == action)
531 return tabularFeature[i].feature;
537 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
539 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
540 for (; dit; dit.forwardChar())
541 if (dit.inTexted() && dit.depth() == 1
542 && dit.paragraph().find(align_d, false, false, dit.pos()))
549 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
551 InsetTableCell tail = InsetTableCell(head);
552 DocIterator const dit = separatorPos(&head, align_d);
555 pit_type const psize = head.paragraphs().front().size();
556 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
557 tail.paragraphs().front().eraseChars(0,
558 dit.pos() < psize ? dit.pos() + 1 : psize, false);
565 /////////////////////////////////////////////////////////////////////
569 /////////////////////////////////////////////////////////////////////
572 Tabular::CellData::CellData(Buffer * buf)
575 multicolumn(Tabular::CELL_NORMAL),
576 multirow(Tabular::CELL_NORMAL),
577 alignment(LYX_ALIGN_CENTER),
578 valignment(LYX_VALIGN_TOP),
588 inset(new InsetTableCell(buf))
590 inset->setBuffer(*buf);
594 Tabular::CellData::CellData(CellData const & cs)
597 multicolumn(cs.multicolumn),
598 multirow(cs.multirow),
599 mroffset(cs.mroffset),
600 alignment(cs.alignment),
601 valignment(cs.valignment),
602 decimal_hoffset(cs.decimal_hoffset),
603 decimal_width(cs.decimal_width),
605 top_line(cs.top_line),
606 bottom_line(cs.bottom_line),
607 left_line(cs.left_line),
608 right_line(cs.right_line),
611 align_special(cs.align_special),
613 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
617 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
623 multicolumn = cs.multicolumn;
624 multirow = cs.multirow;
625 mroffset = cs.mroffset;
626 alignment = cs.alignment;
627 valignment = cs.valignment;
628 decimal_hoffset = cs.decimal_hoffset;
629 decimal_width = cs.decimal_width;
630 voffset = cs.voffset;
631 top_line = cs.top_line;
632 bottom_line = cs.bottom_line;
633 left_line = cs.left_line;
634 right_line = cs.right_line;
637 align_special = cs.align_special;
638 p_width = cs.p_width;
639 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
643 Tabular::RowData::RowData()
646 top_space_default(false),
647 bottom_space_default(false),
648 interline_space_default(false),
658 Tabular::ColumnData::ColumnData()
659 : alignment(LYX_ALIGN_CENTER),
660 valignment(LYX_VALIGN_TOP),
667 Tabular::ltType::ltType()
668 : set(false), topDL(false),
674 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
676 init(buffer, rows_arg, columns_arg);
680 void Tabular::setBuffer(Buffer & buffer)
683 for (row_type i = 0; i < nrows(); ++i)
684 for (col_type j = 0; j < ncols(); ++j)
685 cell_info[i][j].inset->setBuffer(*buffer_);
689 // activates all lines and sets all widths to 0
690 void Tabular::init(Buffer * buf, row_type rows_arg,
691 col_type columns_arg)
694 row_info = row_vector(rows_arg);
695 column_info = column_vector(columns_arg);
696 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
697 row_info.reserve(10);
698 column_info.reserve(10);
699 cell_info.reserve(100);
701 is_long_tabular = false;
702 tabular_valignment = LYX_VALIGN_MIDDLE;
703 tabular_width = Length();
704 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
706 use_booktabs = false;
707 // set silly default lines
708 for (row_type r = 0; r < nrows(); ++r)
709 for (col_type c = 0; c < ncols(); ++c) {
710 cell_info[r][c].inset->setBuffer(*buffer_);
711 cell_info[r][c].top_line = true;
712 cell_info[r][c].left_line = true;
713 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
714 cell_info[r][c].right_line = c == ncols() - 1;
719 void Tabular::deleteRow(row_type const row)
721 // Not allowed to delete last row
725 for (col_type c = 0; c < ncols(); ++c) {
726 // Care about multirow cells
727 if (row + 1 < nrows() &&
728 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
729 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
730 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
733 row_info.erase(row_info.begin() + row);
734 cell_info.erase(cell_info.begin() + row);
739 void Tabular::copyRow(row_type const row)
741 insertRow(row, true);
745 void Tabular::appendRow(row_type row)
747 insertRow(row, false);
751 void Tabular::insertRow(row_type const row, bool copy)
753 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
754 cell_info.insert(cell_info.begin() + row + 1,
755 cell_vector(0, CellData(buffer_)));
757 for (col_type c = 0; c < ncols(); ++c) {
758 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
759 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
760 if (buffer().params().track_changes)
761 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
762 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
763 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
767 for (col_type c = 0; c < ncols(); ++c) {
768 if (isPartOfMultiRow(row, c))
770 // inherit line settings
771 idx_type const i = cellIndex(row + 1, c);
772 idx_type const j = cellIndex(row, c);
773 setLeftLine(i, leftLine(j));
774 setRightLine(i, rightLine(j));
775 setTopLine(i, topLine(j));
776 if (topLine(j) && bottomLine(j)) {
777 setBottomLine(i, true);
778 setBottomLine(j, false);
780 // mark track changes
781 if (buffer().params().track_changes)
782 cellInfo(i).inset->setChange(Change(Change::INSERTED));
787 void Tabular::moveColumn(col_type col, ColDirection direction)
789 if (direction == Tabular::LEFT)
792 std::swap(column_info[col], column_info[col + 1]);
794 for (row_type r = 0; r < nrows(); ++r) {
795 std::swap(cell_info[r][col], cell_info[r][col + 1]);
796 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
797 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
799 // FIXME track changes is broken for tabular features (#8469)
800 idx_type const i = cellIndex(r, col);
801 idx_type const j = cellIndex(r, col + 1);
802 if (buffer().params().track_changes) {
803 cellInfo(i).inset->setChange(Change(Change::INSERTED));
804 cellInfo(j).inset->setChange(Change(Change::INSERTED));
811 void Tabular::moveRow(row_type row, RowDirection direction)
813 if (direction == Tabular::UP)
816 std::swap(row_info[row], row_info[row + 1]);
818 for (col_type c = 0; c < ncols(); ++c) {
819 std::swap(cell_info[row][c], cell_info[row + 1][c]);
820 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
821 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
823 // FIXME track changes is broken for tabular features (#8469)
824 idx_type const i = cellIndex(row, c);
825 idx_type const j = cellIndex(row + 1, c);
826 if (buffer().params().track_changes) {
827 cellInfo(i).inset->setChange(Change(Change::INSERTED));
828 cellInfo(j).inset->setChange(Change(Change::INSERTED));
835 void Tabular::deleteColumn(col_type const col)
837 // Not allowed to delete last column
841 for (row_type r = 0; r < nrows(); ++r) {
842 // Care about multicolumn cells
843 if (col + 1 < ncols() &&
844 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
845 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
846 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
848 cell_info[r].erase(cell_info[r].begin() + col);
850 column_info.erase(column_info.begin() + col);
855 void Tabular::copyColumn(col_type const col)
857 insertColumn(col, true);
861 void Tabular::appendColumn(col_type col)
863 insertColumn(col, false);
867 void Tabular::insertColumn(col_type const col, bool copy)
869 BufferParams const & bp = buffer().params();
870 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
872 for (row_type r = 0; r < nrows(); ++r) {
873 cell_info[r].insert(cell_info[r].begin() + col + 1,
874 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
875 if (bp.track_changes)
876 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
877 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
878 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
881 for (row_type r = 0; r < nrows(); ++r) {
882 // inherit line settings
883 idx_type const i = cellIndex(r, col + 1);
884 idx_type const j = cellIndex(r, col);
885 setBottomLine(i, bottomLine(j));
886 setTopLine(i, topLine(j));
887 setLeftLine(i, leftLine(j));
888 setRightLine(i, rightLine(j));
889 if (rightLine(i) && rightLine(j)) {
890 setRightLine(j, false);
892 if (buffer().params().track_changes)
893 cellInfo(i).inset->setChange(Change(Change::INSERTED));
898 void Tabular::updateIndexes()
903 for (row_type row = 0; row < nrows(); ++row)
904 for (col_type column = 0; column < ncols(); ++column) {
905 if (!isPartOfMultiColumn(row, column)
906 && !isPartOfMultiRow(row, column))
908 if (isPartOfMultiRow(row, column))
909 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
911 cell_info[row][column].cellno = numberofcells - 1;
914 rowofcell.resize(numberofcells);
915 columnofcell.resize(numberofcells);
917 // reset column and row of cells and update their width and alignment
918 for (row_type row = 0; row < nrows(); ++row)
919 for (col_type column = 0; column < ncols(); ++column) {
920 if (isPartOfMultiColumn(row, column)) {
921 cell_info[row][column].inset->toggleMultiCol(true);
924 cell_info[row][column].inset->toggleMultiCol(false);
925 // columnofcell needs to be called before setting width and aligment
926 // multirow cells inherit the width from the column width
927 if (!isPartOfMultiRow(row, column)) {
928 columnofcell[i] = column;
931 setFixedWidth(row, column);
932 if (isPartOfMultiRow(row, column)) {
933 cell_info[row][column].inset->toggleMultiRow(true);
936 cell_info[row][column].inset->toggleMultiRow(false);
937 cell_info[row][column].inset->setContentAlignment(
938 getAlignment(cellIndex(row, column)));
944 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
947 for (col_type c = 0; c < ncols(); ++c)
948 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
954 bool Tabular::topLine(idx_type const cell) const
956 return cellInfo(cell).top_line;
960 bool Tabular::bottomLine(idx_type const cell) const
962 return cellInfo(cell).bottom_line;
966 bool Tabular::leftLine(idx_type cell) const
970 return cellInfo(cell).left_line;
974 bool Tabular::rightLine(idx_type cell) const
978 return cellInfo(cell).right_line;
982 int Tabular::interRowSpace(row_type row) const
984 if (!row || row >= nrows())
987 int const interline_space = row_info[row - 1].interline_space_default ?
989 row_info[row - 1].interline_space.inPixels(width());
990 if (rowTopLine(row) && rowBottomLine(row - 1))
991 return interline_space + WIDTH_OF_LINE;
992 return interline_space;
996 int Tabular::interColumnSpace(idx_type cell) const
998 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
999 if (rightLine(cell) && nextcol < ncols()
1000 && leftLine(cellIndex(cellRow(cell), nextcol)))
1001 return WIDTH_OF_LINE;
1006 int Tabular::cellWidth(idx_type cell) const
1009 col_type const span = columnSpan(cell);
1010 col_type const col = cellColumn(cell);
1011 for(col_type c = col; c < col + span ; ++c)
1012 w += column_info[c].width;
1017 int Tabular::cellHeight(idx_type cell) const
1019 row_type const span = rowSpan(cell);
1020 row_type const row = cellRow(cell);
1022 for(row_type r = row; r < row + span ; ++r) {
1023 h += rowAscent(r) + rowDescent(r);
1024 if (r != row + span - 1)
1025 h += interRowSpace(r + 1);
1032 bool Tabular::updateColumnWidths(MetricsInfo & mi)
1034 vector<int> max_dwidth(ncols(), 0);
1035 // collect max. fixed width of column
1036 map<col_type, int> max_pwidth;
1037 // collect max. variable width of column
1038 map<col_type, int> max_width;
1039 // collect max. variable width of xcolumn
1040 map<col_type, int> max_xwidth;
1042 for(col_type c = 0; c < ncols(); ++c)
1043 for(row_type r = 0; r < nrows(); ++r) {
1044 idx_type const i = cellIndex(r, c);
1045 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1046 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1047 if (!getPWidth(i).zero())
1048 max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
1049 else if (column_info[c].varwidth)
1050 max_xwidth[c] = max(max_xwidth[c], cell_info[r][c].width);
1052 max_width[c] = max(max_width[c], cell_info[r][c].width);
1055 // If we have a fixed tabular width, we take this into account
1056 Length tab_width = tabular_width;
1057 bool const tabularx = hasVarwidthColumn();
1058 if (tabularx && tab_width.zero())
1059 // If no tabular width is specified with X columns,
1060 // we use 100% colwidth
1061 tab_width = Length(100, Length::PCW);
1063 if (!tab_width.zero()) {
1064 restwidth = mi.base.inPixels(tab_width);
1065 // Substract the fixed widths from the table width
1066 for (auto const w : max_pwidth)
1067 restwidth -= w.second;
1070 // If we have a fixed width, distribute the available table width
1071 // (minus the fixed widths) to the variable-width columns
1073 int restcols = ncols() - max_pwidth.size();
1075 vcolwidth = restwidth / restcols;
1077 // Now consider that some variable width columns exceed the vcolwidth
1078 // FIXME As opposed to tabular*, tabularx with X column do not exceed
1079 // the overall table width. This is not yet represented, as it
1080 // needs to include row breaking.
1081 if (vcolwidth > 0) {
1082 bool changed = false;
1083 for (auto const w : max_width) {
1084 if (tabularx || w.second > vcolwidth) {
1086 restwidth -= w.second;
1090 for (auto const w : max_xwidth) {
1091 if (w.second > vcolwidth) {
1093 restwidth -= w.second;
1097 if (changed && restwidth > 0)
1098 vcolwidth = restwidth / restcols;
1101 bool update = false;
1102 // for each col get max of single col cells
1103 for(col_type c = 0; c < ncols(); ++c) {
1105 for(row_type r = 0; r < nrows(); ++r) {
1106 idx_type const i = cellIndex(r, c);
1107 if (columnSpan(i) == 1) {
1108 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1109 && cell_info[r][c].decimal_width != 0)
1110 new_width = max(new_width, cellInfo(i).width
1111 + max_dwidth[c] - cellInfo(i).decimal_width);
1112 else if (getPWidth(i).zero() && vcolwidth > 0) {
1113 if (tabularx && !column_info[c].varwidth)
1114 new_width = max(new_width, cellInfo(i).width);
1116 new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
1118 new_width = max(new_width, cellInfo(i).width);
1122 if (column_info[c].width != new_width) {
1123 column_info[c].width = new_width;
1127 // update col widths to fit merged cells
1128 for(col_type c = 0; c < ncols(); ++c)
1129 for(row_type r = 0; r < nrows(); ++r) {
1130 idx_type const i = cellIndex(r, c);
1131 int const span = columnSpan(i);
1132 if (span == 1 || c > cellColumn(i))
1136 for(col_type j = c; j < c + span ; ++j)
1137 old_width += column_info[j].width;
1139 if (cellInfo(i).width > old_width) {
1140 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1149 int Tabular::width() const
1152 for (col_type c = 0; c < ncols(); ++c)
1153 width += column_info[c].width;
1158 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1161 col_type const col = cellColumn(cell);
1162 // set alignment for the whole row if we are not in a multicolumn cell,
1163 // exclude possible multicolumn cells in the row
1164 if (!isMultiColumn(cell)) {
1165 for (row_type r = 0; r < nrows(); ++r) {
1166 // only if the column has no width the multirow inherits the
1167 // alignment of the column, otherwise it is left aligned
1168 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1169 && !isMultiColumn(cellIndex(r, col))) {
1170 cell_info[r][col].alignment = align;
1171 cell_info[r][col].inset->setContentAlignment(align);
1173 if ((isMultiRow(cellIndex(r, col)) && has_width)
1174 && !isMultiColumn(cellIndex(r, col))) {
1175 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1176 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1179 column_info[col].alignment = align;
1180 docstring & dpoint = column_info[col].decimal_point;
1181 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1182 dpoint = from_utf8(lyxrc.default_decimal_point);
1184 cellInfo(cell).alignment = align;
1185 cellInset(cell)->setContentAlignment(align);
1190 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1193 if (!isMultiColumn(cell) || onlycolumn)
1194 column_info[cellColumn(cell)].valignment = align;
1196 cellInfo(cell).valignment = align;
1203 * Allow line and paragraph breaks for fixed width multicol/multirow cells
1204 * or disallow them, merge cell paragraphs and reset layout to standard
1205 * for variable width multicol cells.
1207 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
1208 bool const fixedWidth, bool const multicol,
1209 bool const multirow)
1211 inset->toggleFixedWidth(fixedWidth);
1212 if (!multirow && (fixedWidth || !multicol))
1215 // merge all paragraphs to one
1216 BufferParams const & bp = cur.bv().buffer().params();
1217 while (inset->paragraphs().size() > 1)
1218 mergeParagraph(bp, inset->paragraphs(), 0);
1220 // This is relevant for multirows
1225 ParagraphList::iterator pit = inset->paragraphs().begin();
1226 for (; pit != inset->paragraphs().end(); ++pit) {
1227 for (pos_type j = 0; j != pit->size(); ++j) {
1228 if (pit->isNewline(j))
1229 pit->eraseChar(j, bp.track_changes);
1235 // undo information has already been recorded
1236 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1237 bp.documentClass().plainLayoutName());
1244 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1245 Length const & width)
1247 col_type const c = cellColumn(cell);
1249 column_info[c].p_width = width;
1250 // reset the vertical alignment to top if the fixed width
1251 // is removed or zero because only fixed width columns can
1252 // have a vertical alignment
1253 if (column_info[c].p_width.zero())
1254 column_info[c].valignment = LYX_VALIGN_TOP;
1255 for (row_type r = 0; r < nrows(); ++r) {
1256 idx_type const cidx = cellIndex(r, c);
1257 // because of multicolumns
1258 toggleFixedWidth(cur, cellInset(cidx).get(),
1259 !getPWidth(cidx).zero(), isMultiColumn(cidx),
1261 if (isMultiRow(cidx))
1262 setAlignment(cidx, LYX_ALIGN_LEFT, false);
1264 // cur can become invalid after paragraphs were merged
1269 bool Tabular::setFixedWidth(row_type r, col_type c)
1271 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1272 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1273 || (multicol && !cell_info[r][c].p_width.zero());
1274 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1279 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1280 Length const & width)
1282 if (!isMultiColumn(cell))
1285 cellInfo(cell).p_width = width;
1286 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
1287 isMultiColumn(cell), isMultiRow(cell));
1288 // cur can become invalid after paragraphs were merged
1294 bool Tabular::toggleVarwidth(idx_type cell, bool const varwidth)
1296 column_info[cellColumn(cell)].varwidth = varwidth;
1301 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1303 cellInfo(cell).mroffset = mroffset;
1308 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1309 Tabular::Feature what)
1311 if (what == SET_SPECIAL_MULTICOLUMN)
1312 cellInfo(cell).align_special = special;
1314 column_info[cellColumn(cell)].align_special = special;
1318 void Tabular::setTopLine(idx_type i, bool line)
1320 cellInfo(i).top_line = line;
1324 void Tabular::setBottomLine(idx_type i, bool line)
1326 cellInfo(i).bottom_line = line;
1330 void Tabular::setLeftLine(idx_type cell, bool line)
1332 cellInfo(cell).left_line = line;
1336 void Tabular::setRightLine(idx_type cell, bool line)
1338 cellInfo(cell).right_line = line;
1341 bool Tabular::rowTopLine(row_type r) const
1343 bool all_rows_set = true;
1344 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1345 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1346 return all_rows_set;
1350 bool Tabular::rowBottomLine(row_type r) const
1352 bool all_rows_set = true;
1353 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1354 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1355 return all_rows_set;
1359 bool Tabular::columnLeftLine(col_type c) const
1366 for (row_type r = 0; r < nrows(); ++r) {
1367 idx_type const i = cellIndex(r, c);
1368 if (c == cellColumn(i)) {
1370 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1371 if (cellInfo(i).left_line || right)
1375 return 2 * nrows_left >= total;
1379 bool Tabular::columnRightLine(col_type c) const
1384 int nrows_right = 0;
1386 for (row_type r = 0; r < nrows(); ++r) {
1387 idx_type i = cellIndex(r, c);
1388 if (c == cellColumn(i) + columnSpan(i) - 1) {
1390 bool left = (c + 1 < ncols()
1391 && cellInfo(cellIndex(r, c + 1)).left_line)
1392 || c + 1 == ncols();
1393 if (cellInfo(i).right_line && left)
1397 return 2 * nrows_right >= total;
1401 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1403 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1404 return cellInfo(cell).alignment;
1406 return column_info[cellColumn(cell)].alignment;
1411 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1413 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1414 return cellInfo(cell).valignment;
1415 return column_info[cellColumn(cell)].valignment;
1419 int Tabular::offsetVAlignment() const
1421 // for top-alignment the first horizontal table line must be exactly at
1422 // the position of the base line of the surrounding text line
1423 // for bottom alignment, the same is for the last table line
1424 int offset_valign = 0;
1425 switch (tabular_valignment) {
1426 case Tabular::LYX_VALIGN_BOTTOM:
1427 offset_valign = rowAscent(0) - height();
1429 case Tabular::LYX_VALIGN_MIDDLE:
1430 offset_valign = (- height()) / 2 + rowAscent(0);
1432 case Tabular::LYX_VALIGN_TOP:
1433 offset_valign = rowAscent(0);
1436 return offset_valign;
1440 Length const Tabular::getPWidth(idx_type cell) const
1442 if (isMultiColumn(cell))
1443 return cellInfo(cell).p_width;
1444 return column_info[cellColumn(cell)].p_width;
1448 Length const Tabular::getMROffset(idx_type cell) const
1450 return cellInfo(cell).mroffset;
1454 int Tabular::textHOffset(idx_type cell) const
1456 // the LaTeX Way :-(
1457 int x = WIDTH_OF_LINE;
1459 int const w = cellWidth(cell) - cellInfo(cell).width;
1461 switch (getAlignment(cell)) {
1462 case LYX_ALIGN_CENTER:
1465 case LYX_ALIGN_RIGHT:
1468 case LYX_ALIGN_DECIMAL: {
1469 // we center when no decimal point
1470 if (cellInfo(cell).decimal_width == 0) {
1474 col_type const c = cellColumn(cell);
1475 int max_dhoffset = 0;
1476 for(row_type r = 0; r < row_info.size() ; ++r) {
1477 idx_type const i = cellIndex(r, c);
1478 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1479 && cellInfo(i).decimal_width != 0)
1480 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1482 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1485 // LYX_ALIGN_LEFT: nothing :-)
1493 int Tabular::textVOffset(idx_type cell) const
1495 int voffset = cellInfo(cell).voffset;
1496 if (isMultiRow(cell)) {
1497 row_type const row = cellRow(cell);
1498 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1504 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1507 idx_type const numcells = numberOfCellsInRow(row);
1508 // we check against numcells to make sure we do not crash if all the
1509 // cells are multirow (bug #7535), but in that case our return value
1510 // is really invalid, i.e., it is NOT the first cell in the row. but
1511 // i do not know what to do here. (rgh)
1512 while (c < numcells - 1
1513 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1515 return cell_info[row][c].cellno;
1519 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1521 col_type c = ncols() - 1;
1522 // of course we check against 0 so we don't crash. but we have the same
1523 // problem as in the previous routine: if all the cells are part of a
1524 // multirow or part of a multi column, then our return value is invalid.
1526 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1527 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1529 return cell_info[row][c].cellno;
1533 Tabular::row_type Tabular::cellRow(idx_type cell) const
1535 if (cell >= numberofcells)
1539 return rowofcell[cell];
1543 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1545 if (cell >= numberofcells)
1549 return columnofcell[cell];
1553 void Tabular::write(ostream & os) const
1557 << write_attribute("version", 3)
1558 << write_attribute("rows", nrows())
1559 << write_attribute("columns", ncols())
1561 // global longtable options
1563 << write_attribute("rotate", rotate)
1564 << write_attribute("booktabs", use_booktabs)
1565 << write_attribute("islongtable", is_long_tabular)
1566 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1567 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1568 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1569 << write_attribute("headTopDL", endhead.topDL)
1570 << write_attribute("headBottomDL", endhead.bottomDL)
1571 << write_attribute("footTopDL", endfoot.topDL)
1572 << write_attribute("footBottomDL", endfoot.bottomDL)
1573 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1574 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1575 << write_attribute("lastFootEmpty", endlastfoot.empty);
1576 // longtables cannot be aligned vertically
1577 if (!is_long_tabular)
1578 os << write_attribute("tabularvalignment", tabular_valignment);
1579 os << write_attribute("tabularwidth", tabular_width);
1580 if (is_long_tabular)
1581 os << write_attribute("longtabularalignment", longtabular_alignment);
1583 for (col_type c = 0; c < ncols(); ++c) {
1585 << write_attribute("alignment", column_info[c].alignment);
1586 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1587 os << write_attribute("decimal_point", column_info[c].decimal_point);
1588 os << write_attribute("valignment", column_info[c].valignment)
1589 << write_attribute("width", column_info[c].p_width.asString())
1590 << write_attribute("varwidth", column_info[c].varwidth)
1591 << write_attribute("special", column_info[c].align_special)
1594 for (row_type r = 0; r < nrows(); ++r) {
1595 static const string def("default");
1597 if (row_info[r].top_space_default)
1598 os << write_attribute("topspace", def);
1600 os << write_attribute("topspace", row_info[r].top_space);
1601 if (row_info[r].bottom_space_default)
1602 os << write_attribute("bottomspace", def);
1604 os << write_attribute("bottomspace", row_info[r].bottom_space);
1605 if (row_info[r].interline_space_default)
1606 os << write_attribute("interlinespace", def);
1608 os << write_attribute("interlinespace", row_info[r].interline_space);
1609 os << write_attribute("endhead", row_info[r].endhead)
1610 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1611 << write_attribute("endfoot", row_info[r].endfoot)
1612 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1613 << write_attribute("newpage", row_info[r].newpage)
1614 << write_attribute("caption", row_info[r].caption)
1616 for (col_type c = 0; c < ncols(); ++c) {
1618 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1619 << write_attribute("multirow", cell_info[r][c].multirow)
1620 << write_attribute("mroffset", cell_info[r][c].mroffset)
1621 << write_attribute("alignment", cell_info[r][c].alignment)
1622 << write_attribute("valignment", cell_info[r][c].valignment)
1623 << write_attribute("topline", cell_info[r][c].top_line)
1624 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1625 << write_attribute("leftline", cell_info[r][c].left_line)
1626 << write_attribute("rightline", cell_info[r][c].right_line)
1627 << write_attribute("rotate", cell_info[r][c].rotate)
1628 << write_attribute("usebox", cell_info[r][c].usebox)
1629 << write_attribute("width", cell_info[r][c].p_width)
1630 << write_attribute("special", cell_info[r][c].align_special)
1632 os << "\\begin_inset ";
1633 cell_info[r][c].inset->write(os);
1634 os << "\n\\end_inset\n"
1636 // FIXME This can be removed again once the mystery
1637 // crash has been resolved.
1642 os << "</lyxtabular>\n";
1646 void Tabular::read(Lexer & lex)
1649 istream & is = lex.getStream();
1651 l_getline(is, line);
1652 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1653 LASSERT(false, return);
1657 if (!getTokenValue(line, "version", version))
1659 LATTEST(version >= 2);
1662 if (!getTokenValue(line, "rows", rows_arg))
1665 if (!getTokenValue(line, "columns", columns_arg))
1667 init(buffer_, rows_arg, columns_arg);
1668 l_getline(is, line);
1669 if (!prefixIs(line, "<features")) {
1670 lyxerr << "Wrong tabular format (expected <features ...> got"
1671 << line << ')' << endl;
1674 getTokenValue(line, "rotate", rotate);
1675 getTokenValue(line, "booktabs", use_booktabs);
1676 getTokenValue(line, "islongtable", is_long_tabular);
1677 getTokenValue(line, "tabularvalignment", tabular_valignment);
1678 getTokenValue(line, "tabularwidth", tabular_width);
1679 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1680 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1681 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1682 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1683 getTokenValue(line, "headTopDL", endhead.topDL);
1684 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1685 getTokenValue(line, "footTopDL", endfoot.topDL);
1686 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1687 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1688 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1689 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1691 for (col_type c = 0; c < ncols(); ++c) {
1693 if (!prefixIs(line,"<column")) {
1694 lyxerr << "Wrong tabular format (expected <column ...> got"
1695 << line << ')' << endl;
1698 getTokenValue(line, "alignment", column_info[c].alignment);
1699 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1700 getTokenValue(line, "valignment", column_info[c].valignment);
1701 getTokenValue(line, "width", column_info[c].p_width);
1702 getTokenValue(line, "special", column_info[c].align_special);
1703 getTokenValue(line, "varwidth", column_info[c].varwidth);
1706 for (row_type i = 0; i < nrows(); ++i) {
1707 l_getline(is, line);
1708 if (!prefixIs(line, "<row")) {
1709 lyxerr << "Wrong tabular format (expected <row ...> got"
1710 << line << ')' << endl;
1713 getTokenValue(line, "topspace", row_info[i].top_space,
1714 row_info[i].top_space_default);
1715 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1716 row_info[i].bottom_space_default);
1717 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1718 row_info[i].interline_space_default);
1719 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1720 getTokenValue(line, "endhead", row_info[i].endhead);
1721 getTokenValue(line, "endfoot", row_info[i].endfoot);
1722 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1723 getTokenValue(line, "newpage", row_info[i].newpage);
1724 getTokenValue(line, "caption", row_info[i].caption);
1725 for (col_type j = 0; j < ncols(); ++j) {
1726 l_getline(is, line);
1727 if (!prefixIs(line, "<cell")) {
1728 lyxerr << "Wrong tabular format (expected <cell ...> got"
1729 << line << ')' << endl;
1732 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1733 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1734 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1735 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1736 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1737 getTokenValue(line, "topline", cell_info[i][j].top_line);
1738 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1739 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1740 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1741 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1742 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1743 getTokenValue(line, "width", cell_info[i][j].p_width);
1745 getTokenValue(line, "special", cell_info[i][j].align_special);
1746 l_getline(is, line);
1747 if (prefixIs(line, "\\begin_inset")) {
1748 cell_info[i][j].inset->setBuffer(*buffer_);
1749 cell_info[i][j].inset->read(lex);
1750 l_getline(is, line);
1752 if (!prefixIs(line, "</cell>")) {
1753 lyxerr << "Wrong tabular format (expected </cell> got"
1754 << line << ')' << endl;
1758 l_getline(is, line);
1759 if (!prefixIs(line, "</row>")) {
1760 lyxerr << "Wrong tabular format (expected </row> got"
1761 << line << ')' << endl;
1765 while (!prefixIs(line, "</lyxtabular>")) {
1766 l_getline(is, line);
1772 bool Tabular::isMultiColumn(idx_type cell) const
1774 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1775 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1779 bool Tabular::hasMultiColumn(col_type c) const
1781 for (row_type r = 0; r < nrows(); ++r) {
1782 if (isMultiColumn(cellIndex(r, c)))
1789 bool Tabular::hasVarwidthColumn() const
1791 for (col_type c = 0; c < ncols(); ++c) {
1792 if (column_info[c].varwidth)
1799 bool Tabular::isVTypeColumn(col_type c) const
1801 for (row_type r = 0; r < nrows(); ++r) {
1802 idx_type idx = cellIndex(r, c);
1803 if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
1810 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1812 return cell_info[cellRow(cell)][cellColumn(cell)];
1816 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1818 return cell_info[cellRow(cell)][cellColumn(cell)];
1822 Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
1823 bool const right_border)
1825 idx_type const col = cellColumn(cell);
1826 idx_type const row = cellRow(cell);
1827 for (idx_type i = 0; i < number; ++i)
1828 unsetMultiRow(cellIndex(row, col + i));
1830 // unsetting of multirow may have invalidated cell index
1831 cell = cellIndex(row, col);
1832 CellData & cs = cellInfo(cell);
1833 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1834 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1835 cs.alignment = column_info[col].alignment;
1836 setRightLine(cell, right_border);
1837 // non-fixed width multicolumns cannot have multiple paragraphs
1838 if (getPWidth(cell).zero()) {
1839 toggleFixedWidth(cur, cellInset(cell).get(),
1840 !getPWidth(cell).zero(), isMultiColumn(cell),
1842 // cur can become invalid after paragraphs were merged
1846 idx_type lastcell = cellIndex(row, col + number - 1);
1847 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1848 CellData & cs1 = cellInfo(cell + i);
1849 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1850 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1858 bool Tabular::isMultiRow(idx_type cell) const
1860 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1861 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1864 bool Tabular::hasMultiRow(row_type r) const
1866 for (col_type c = 0; c < ncols(); ++c) {
1867 if (isMultiRow(cellIndex(r, c)))
1873 Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
1874 bool const bottom_border,
1875 LyXAlignment const halign)
1877 idx_type const col = cellColumn(cell);
1878 idx_type const row = cellRow(cell);
1879 for (idx_type i = 0; i < number; ++i)
1880 unsetMultiColumn(cellIndex(row + i, col));
1882 // unsetting of multirow may have invalidated cell index
1883 cell = cellIndex(row, col);
1884 CellData & cs = cellInfo(cell);
1885 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1886 cs.valignment = LYX_VALIGN_MIDDLE;
1887 // the horizontal alignment of multirow cells can only
1888 // be changed for the whole table row,
1889 // support changing this only for the multirow cell can be done via
1891 if (getPWidth(cell).zero())
1892 cs.alignment = halign;
1894 cs.alignment = LYX_ALIGN_LEFT;
1896 // Multirows cannot have multiple paragraphs
1897 if (getPWidth(cell).zero()) {
1898 toggleFixedWidth(cur, cellInset(cell).get(),
1899 !getPWidth(cell).zero(),
1900 isMultiColumn(cell), isMultiRow(cell));
1901 // cur can become invalid after paragraphs were merged
1905 // set the bottom line of the last selected cell
1906 setBottomLine(cell, bottom_border);
1908 for (idx_type i = 1; i < number; ++i) {
1909 CellData & cs1 = cell_info[row + i][col];
1910 cs1.multirow = CELL_PART_OF_MULTIROW;
1911 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1919 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1921 row_type const row = cellRow(cell);
1922 col_type const col = cellColumn(cell);
1924 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1931 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1933 col_type const column = cellColumn(cell);
1934 col_type row = cellRow(cell) + 1;
1935 while (row < nrows() && isPartOfMultiRow(row, column))
1938 return row - cellRow(cell);
1942 void Tabular::unsetMultiColumn(idx_type cell)
1944 if (!isMultiColumn(cell))
1947 row_type const row = cellRow(cell);
1948 col_type const col = cellColumn(cell);
1949 row_type const span = columnSpan(cell);
1950 for (col_type c = 0; c < span; ++c) {
1951 // in the table dialog the lines are set in every case
1952 // when unsetting a multicolumn this leads to an additional right
1953 // line for every cell that was part of the former multicolumn cell,
1954 // except if the cell is in the last column
1955 // therefore remove this line
1956 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1957 && (col + c) < (col + span - 1))
1958 cell_info[row][col + c].right_line = false;
1959 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1965 void Tabular::unsetMultiRow(idx_type cell)
1967 if (!isMultiRow(cell))
1970 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1971 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1972 row_type const row = cellRow(cell);
1973 col_type const col = cellColumn(cell);
1974 row_type const span = rowSpan(cell);
1975 for (row_type r = 0; r < span; ++r)
1976 cell_info[row + r][col].multirow = CELL_NORMAL;
1981 void Tabular::setRotateCell(idx_type cell, int value)
1983 cellInfo(cell).rotate = value;
1987 int Tabular::getRotateCell(idx_type cell) const
1989 return cellInfo(cell).rotate;
1993 bool Tabular::needRotating() const
1995 if (rotate && !is_long_tabular)
1997 for (row_type r = 0; r < nrows(); ++r)
1998 for (col_type c = 0; c < ncols(); ++c)
1999 if (cell_info[r][c].rotate != 0)
2005 bool Tabular::isLastCell(idx_type cell) const
2007 if (cell + 1 < numberofcells)
2013 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
2015 if (cellRow(cell) == 0)
2018 col_type const col = cellColumn(cell);
2019 row_type r = cellRow(cell) - 1;
2020 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
2023 return cell_info[r][col].cellno;
2027 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
2029 row_type const nextrow = cellRow(cell) + rowSpan(cell);
2030 if (nextrow < nrows())
2031 return cell_info[nextrow][cellColumn(cell)].cellno;
2036 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
2038 LASSERT(column != npos && column < ncols(), column = 0);
2039 LASSERT(row != npos && row < nrows(), row = 0);
2040 return cell_info[row][column].cellno;
2044 void Tabular::setUsebox(idx_type cell, BoxType type)
2046 cellInfo(cell).usebox = type;
2050 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
2052 if (getRotateCell(cell) == 0
2053 && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
2054 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
2056 if (cellInfo(cell).usebox > 1)
2057 return cellInfo(cell).usebox;
2058 return useBox(cell);
2063 // This are functions used for the longtable support
2065 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
2071 row_info[row].endfirsthead = flag;
2075 row_info[row].endhead = flag;
2080 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
2083 hd.set = haveLTHead();
2084 return row_info[row].endhead;
2088 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
2091 hd.set = haveLTFirstHead();
2092 return row_info[row].endfirsthead;
2096 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
2102 row_info[row].endlastfoot = flag;
2106 row_info[row].endfoot = flag;
2111 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
2114 fd.set = haveLTFoot();
2115 return row_info[row].endfoot;
2119 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
2122 fd.set = haveLTLastFoot();
2123 return row_info[row].endlastfoot;
2127 void Tabular::setLTNewPage(row_type row, bool what)
2129 row_info[row].newpage = what;
2133 bool Tabular::getLTNewPage(row_type row) const
2135 return row_info[row].newpage;
2139 bool Tabular::haveLTHead(bool withcaptions) const
2141 if (!is_long_tabular)
2143 for (row_type i = 0; i < nrows(); ++i)
2144 if (row_info[i].endhead &&
2145 (withcaptions || !row_info[i].caption))
2151 bool Tabular::haveLTFirstHead(bool withcaptions) const
2153 if (!is_long_tabular || endfirsthead.empty)
2155 for (row_type r = 0; r < nrows(); ++r)
2156 if (row_info[r].endfirsthead &&
2157 (withcaptions || !row_info[r].caption))
2163 bool Tabular::haveLTFoot(bool withcaptions) const
2165 if (!is_long_tabular)
2167 for (row_type r = 0; r < nrows(); ++r)
2168 if (row_info[r].endfoot &&
2169 (withcaptions || !row_info[r].caption))
2175 bool Tabular::haveLTLastFoot(bool withcaptions) const
2177 if (!is_long_tabular || endlastfoot.empty)
2179 for (row_type r = 0; r < nrows(); ++r)
2180 if (row_info[r].endlastfoot &&
2181 (withcaptions || !row_info[r].caption))
2187 Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
2189 idx_type i = getFirstCellInRow(row);
2191 setMultiColumn(cur, i, numberOfCellsInRow(row), false);
2192 setTopLine(i, false);
2193 setBottomLine(i, false);
2194 setLeftLine(i, false);
2195 setRightLine(i, false);
2196 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2197 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2198 setLTHead(row, true, endfirsthead, true);
2199 row_info[row].endfirsthead = true;
2202 unsetMultiColumn(i);
2203 // When unsetting a caption row, also all existing
2204 // captions in this row must be dissolved.
2206 row_info[row].caption = what;
2211 bool Tabular::ltCaption(row_type row) const
2213 return row_info[row].caption;
2217 bool Tabular::haveLTCaption(CaptionType captiontype) const
2219 if (!is_long_tabular)
2221 for (row_type r = 0; r < nrows(); ++r) {
2222 if (row_info[r].caption) {
2223 switch (captiontype) {
2224 case CAPTION_FIRSTHEAD:
2225 if (row_info[r].endfirsthead)
2229 if (row_info[r].endhead)
2233 if (row_info[r].endfoot)
2236 case CAPTION_LASTFOOT:
2237 if (row_info[r].endlastfoot)
2249 // end longtable support functions
2251 void Tabular::setRowAscent(row_type row, int height)
2253 if (row >= nrows() || row_info[row].ascent == height)
2255 row_info[row].ascent = height;
2259 void Tabular::setRowDescent(row_type row, int height)
2261 if (row >= nrows() || row_info[row].descent == height)
2263 row_info[row].descent = height;
2267 int Tabular::rowAscent(row_type row) const
2269 LASSERT(row < nrows(), row = 0);
2270 return row_info[row].ascent;
2274 int Tabular::rowDescent(row_type row) const
2276 LASSERT(row < nrows(), row = 0);
2277 return row_info[row].descent;
2281 int Tabular::height() const
2284 for (row_type row = 0; row < nrows(); ++row)
2285 height += rowAscent(row) + rowDescent(row) +
2291 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2293 LASSERT(row < nrows(), return false);
2294 LASSERT(column < ncols(), return false);
2295 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2299 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2301 LASSERT(row < nrows(), return false);
2302 LASSERT(column < ncols(), return false);
2303 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2307 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang,
2308 list<col_type> columns) const
2310 // we only output complete row lines and the 1st row here, the rest
2311 // is done in Tabular::TeXBottomHLine(...)
2313 // get for each column the topline (if any)
2314 map<col_type, bool> topline;
2316 for (auto const & c : columns) {
2317 topline[c] = topLine(cellIndex(row, c));
2318 // If cell is part of a multirow and not the first cell of the
2319 // multirow, no line must be drawn.
2321 if (isMultiRow(cellIndex(row, c))
2322 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2324 if (topline.find(c) != topline.end() && topline.find(c)->second)
2328 // do nothing if empty first row, or incomplete row line after
2329 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2332 // only output complete row lines and the 1st row's clines
2333 if (nset == ncols()) {
2335 os << (row == 0 ? "\\toprule " : "\\midrule ");
2339 } else if (row == 0) {
2340 for (auto & c : columns) {
2341 if (topline.find(c)->second) {
2342 col_type offset = 0;
2343 for (col_type j = 0 ; j < c; ++j)
2344 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2347 //babel makes the "-" character an active one, so we have to suppress this here
2348 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2349 if (lang == "slovak" || lang == "czech")
2350 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2351 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2353 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2355 col_type cstart = c;
2356 for ( ; c < ncols() && topline.find(c)->second; ++c) {}
2358 for (col_type j = cstart ; j < c ; ++j)
2359 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2362 os << c + offset << "} ";
2370 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang,
2371 list<col_type> columns) const
2373 // we output bottomlines of row r and the toplines of row r+1
2374 // if the latter do not span the whole tabular
2376 // get the bottomlines of row r, and toplines in next row
2377 bool lastrow = row == nrows() - 1;
2378 map<col_type, bool> bottomline, topline;
2379 bool nextrowset = true;
2380 for (auto const & c : columns) {
2381 bottomline[c] = bottomLine(cellIndex(row, c));
2382 topline[c] = !lastrow && topLine(cellIndex(row + 1, c));
2383 // If cell is part of a multirow and not the last cell of the
2384 // multirow, no line must be drawn.
2386 if (isMultiRow(cellIndex(row, c))
2387 && isMultiRow(cellIndex(row + 1, c))
2388 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2389 bottomline[c] = false;
2392 nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
2395 // combine this row's bottom lines and next row's toplines if necessary
2397 for (auto const & c : columns) {
2399 bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
2400 if (bottomline.find(c)->second)
2404 // do nothing if empty, OR incomplete row line with a topline in next row
2405 if (nset == 0 || (nextrowset && nset != ncols()))
2408 if (nset == ncols()) {
2410 os << (lastrow ? "\\bottomrule" : "\\midrule");
2414 for (auto & c : columns) {
2415 if (bottomline.find(c)->second) {
2416 col_type offset = 0;
2417 for (col_type j = 0 ; j < c; ++j)
2418 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2421 //babel makes the "-" character an active one, so we have to suppress this here
2422 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2423 if (lang == "slovak" || lang == "czech")
2424 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2425 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2427 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2429 col_type cstart = c;
2430 for ( ; c < ncols() && bottomline.find(c)->second; ++c) {}
2432 for (col_type j = cstart ; j < c ; ++j)
2433 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2436 os << c + offset << "} ";
2444 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2445 bool & ismulticol, bool & ismultirow,
2446 bool const bidi) const
2448 row_type const r = cellRow(cell);
2449 if (is_long_tabular && row_info[r].caption)
2452 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2453 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2454 // figure out how to set the lines
2455 // we always set double lines to the right of the cell
2456 // or left in bidi RTL, respectively.
2457 col_type const c = cellColumn(cell);
2458 col_type const nextcol = c + columnSpan(cell);
2459 bool const decimal = column_info[c].alignment == LYX_ALIGN_DECIMAL;
2460 bool colright = columnRightLine(c);
2461 bool colleft = columnLeftLine(c);
2462 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2463 bool nextcellleft = nextcol < ncols()
2464 && leftLine(cellIndex(r, nextcol));
2465 bool coldouble = colright && nextcolleft;
2466 bool celldouble = rightLine(cell) && nextcellleft;
2468 ismulticol = (isMultiColumn(cell)
2469 || (c == 0 && colleft != leftLine(cell))
2470 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2471 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2472 || (coldouble != celldouble))
2475 // we center in multicol when no decimal point
2477 docstring const align_d = column_info[c].decimal_point;
2478 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2479 bool const nosep = !dit;
2480 ismulticol |= nosep;
2481 celldouble &= nosep;
2484 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2485 int latexcolspan = columnSpan(cell);
2486 for (col_type col = c; col < c + columnSpan(cell); ++col)
2487 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2491 os << "\\multicolumn{" << latexcolspan << "}{";
2492 if (((bidi && c == getLastCellInRow(cellRow(0)) && rightLine(cell))
2493 || (!bidi && c == 0 && leftLine(cell))))
2495 if (bidi && celldouble)
2496 // add extra vertical line if we want a double one
2498 if (!cellInfo(cell).align_special.empty()) {
2499 os << cellInfo(cell).align_special;
2501 if (!getPWidth(cell).zero()) {
2503 case LYX_ALIGN_LEFT:
2504 os << ">{\\raggedright}";
2506 case LYX_ALIGN_RIGHT:
2507 os << ">{\\raggedleft}";
2509 case LYX_ALIGN_CENTER:
2510 os << ">{\\centering}";
2516 case LYX_VALIGN_TOP:
2519 case LYX_VALIGN_MIDDLE:
2522 case LYX_VALIGN_BOTTOM:
2527 << from_ascii(getPWidth(cell).asLatexString())
2531 case LYX_ALIGN_LEFT:
2534 case LYX_ALIGN_RIGHT:
2541 } // end if else !getPWidth
2542 } // end if else !cellinfo_of_cell
2543 if ((bidi && leftLine(cell)) || (!bidi && rightLine(cell)) || nextcellleft)
2545 if (!bidi && celldouble)
2546 // add extra vertical line if we want a double one
2549 } // end if ismulticol
2551 // we only need code for the first multirow cell
2552 ismultirow = isMultiRow(cell);
2554 os << "\\multirow{" << rowSpan(cell) << "}{";
2555 if (!getPWidth(cell).zero())
2556 os << from_ascii(getPWidth(cell).asLatexString());
2558 // we need to set a default value
2561 if (!getMROffset(cell).zero())
2562 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2564 } // end if ismultirow
2566 if (getRotateCell(cell) != 0)
2567 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2569 if (getUsebox(cell) == BOX_PARBOX) {
2572 case LYX_VALIGN_TOP:
2575 case LYX_VALIGN_MIDDLE:
2578 case LYX_VALIGN_BOTTOM:
2582 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2584 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2585 os << "\\begin{minipage}[";
2587 case LYX_VALIGN_TOP:
2590 case LYX_VALIGN_MIDDLE:
2593 case LYX_VALIGN_BOTTOM:
2597 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2599 } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
2600 os << "\\begin{varwidth}[";
2602 case LYX_VALIGN_TOP:
2605 case LYX_VALIGN_MIDDLE:
2608 case LYX_VALIGN_BOTTOM:
2612 os << "]{\\linewidth}\n";
2617 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2618 bool ismulticol, bool ismultirow) const
2620 row_type const r = cellRow(cell);
2621 if (is_long_tabular && row_info[r].caption)
2625 if (getUsebox(cell) == BOX_PARBOX)
2627 else if (getUsebox(cell) == BOX_MINIPAGE)
2628 os << breakln << "\\end{minipage}";
2629 else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
2630 os << breakln << "\\end{varwidth}";
2631 if (getRotateCell(cell) != 0)
2632 os << breakln << "\\end{turn}";
2640 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2641 OutputParams const & runparams,
2642 list<col_type> columns) const
2644 if (!is_long_tabular)
2648 // output caption which is in no header or footer
2649 if (haveLTCaption()) {
2650 for (row_type r = 0; r < nrows(); ++r) {
2651 if (row_info[r].caption &&
2652 !row_info[r].endfirsthead && !row_info[r].endhead &&
2653 !row_info[r].endfoot && !row_info[r].endlastfoot)
2654 TeXRow(os, r, runparams, columns);
2657 // output first header info
2658 if (haveLTFirstHead()) {
2659 if (endfirsthead.topDL)
2661 for (row_type r = 0; r < nrows(); ++r) {
2662 if (row_info[r].endfirsthead)
2663 TeXRow(os, r, runparams, columns);
2665 if (endfirsthead.bottomDL)
2667 os << "\\endfirsthead\n";
2669 // output header info
2671 if (endfirsthead.empty && !haveLTFirstHead())
2672 os << "\\endfirsthead\n";
2675 for (row_type r = 0; r < nrows(); ++r) {
2676 if (row_info[r].endhead)
2677 TeXRow(os, r, runparams, columns);
2679 if (endhead.bottomDL)
2681 os << "\\endhead\n";
2683 // output footer info
2687 for (row_type r = 0; r < nrows(); ++r) {
2688 if (row_info[r].endfoot)
2689 TeXRow(os, r, runparams, columns);
2691 if (endfoot.bottomDL)
2693 os << "\\endfoot\n";
2694 if (endlastfoot.empty && !haveLTLastFoot())
2695 os << "\\endlastfoot\n";
2697 // output lastfooter info
2698 if (haveLTLastFoot()) {
2699 if (endlastfoot.topDL)
2701 for (row_type r = 0; r < nrows(); ++r) {
2702 if (row_info[r].endlastfoot)
2703 TeXRow(os, r, runparams, columns);
2705 if (endlastfoot.bottomDL)
2707 os << "\\endlastfoot\n";
2712 bool Tabular::isValidRow(row_type row) const
2714 if (!is_long_tabular)
2716 return !row_info[row].endhead && !row_info[row].endfirsthead
2717 && !row_info[row].endfoot && !row_info[row].endlastfoot
2718 && !row_info[row].caption;
2722 void Tabular::TeXRow(otexstream & os, row_type row,
2723 OutputParams const & runparams,
2724 list<col_type> columns) const
2726 idx_type cell = cellIndex(row, 0);
2727 InsetTableCell const * cinset = cellInset(cell);
2728 Paragraph const & cpar = cinset->paragraphs().front();
2729 string const clang = cpar.getParLanguage(buffer().params())->lang();
2731 //output the top line
2732 TeXTopHLine(os, row, clang, columns);
2734 if (row_info[row].top_space_default) {
2736 os << "\\addlinespace\n";
2738 os << "\\noalign{\\vskip\\doublerulesep}\n";
2739 } else if(!row_info[row].top_space.zero()) {
2741 os << "\\addlinespace["
2742 << from_ascii(row_info[row].top_space.asLatexString())
2745 os << "\\noalign{\\vskip"
2746 << from_ascii(row_info[row].top_space.asLatexString())
2750 bool ismulticol = false;
2751 bool ismultirow = false;
2753 // The bidi package (loaded by polyglossia) reverses RTL table columns
2754 bool const bidi_rtl =
2755 runparams.local_font->isRightToLeft()
2756 && runparams.use_polyglossia;
2758 bidi_rtl ? getFirstCellInRow(row) : getLastCellInRow(row);
2760 for (auto const & c : columns) {
2761 if (isPartOfMultiColumn(row, c))
2764 cell = cellIndex(row, c);
2766 if (isPartOfMultiRow(row, c)
2767 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2768 if (cell != lastcell)
2773 TeXCellPreamble(os, cell, ismulticol, ismultirow, bidi_rtl);
2774 InsetTableCell const * inset = cellInset(cell);
2776 Paragraph const & par = inset->paragraphs().front();
2778 os.texrow().forceStart(par.id(), 0);
2780 bool rtl = par.isRTL(buffer().params())
2782 && getPWidth(cell).zero()
2783 && !runparams.use_polyglossia;
2787 par.getParLanguage(buffer().params())->lang();
2788 if (lang == "farsi")
2790 else if (lang == "arabic_arabi")
2792 // currently, remaining RTL languages are
2793 // arabic_arabtex and hebrew
2797 // pass to the OutputParams that we are in a cell and
2798 // which alignment we have set.
2799 // InsetNewline needs this context information.
2800 OutputParams newrp(runparams);
2801 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2802 ? OutputParams::PLAIN
2803 : OutputParams::ALIGNED;
2805 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2806 // copy cell and split in 2
2807 InsetTableCell head = InsetTableCell(*cellInset(cell));
2808 head.setBuffer(const_cast<Buffer &>(buffer()));
2809 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2811 dit.push_back(CursorSlice(head));
2812 head.setMacrocontextPositionRecursive(dit);
2813 bool hassep = false;
2814 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2816 tail.setBuffer(head.buffer());
2818 dit.push_back(CursorSlice(tail));
2819 tail.setMacrocontextPositionRecursive(dit);
2823 tail.latex(os, newrp);
2826 head.latex(os, newrp);
2828 head.latex(os, newrp);
2831 tail.latex(os, newrp);
2834 } else if (ltCaption(row)) {
2835 // Inside longtable caption rows, we must only output the caption inset
2836 // with its content and omit anything outside of that (see #10791)
2837 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
2838 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
2839 for (; it != i_end; ++it) {
2840 if (it->lyxCode() != CAPTION_CODE)
2842 it->latex(os, runparams);
2845 } else if (!isPartOfMultiRow(row, c)) {
2846 if (!runparams.nice)
2847 os.texrow().start(par.id(), 0);
2848 inset->latex(os, newrp);
2851 runparams.encoding = newrp.encoding;
2855 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2856 if (cell != lastcell) { // not last cell in row
2863 os << "\\tabularnewline";
2864 if (row_info[row].bottom_space_default) {
2866 os << "\\addlinespace";
2868 os << "[\\doublerulesep]";
2869 } else if (!row_info[row].bottom_space.zero()) {
2871 os << "\\addlinespace";
2873 << from_ascii(row_info[row].bottom_space.asLatexString())
2878 //output the bottom line
2879 TeXBottomHLine(os, row, clang, columns);
2881 if (row_info[row].interline_space_default) {
2883 os << "\\addlinespace\n";
2885 os << "\\noalign{\\vskip\\doublerulesep}\n";
2886 } else if (!row_info[row].interline_space.zero()) {
2888 os << "\\addlinespace["
2889 << from_ascii(row_info[row].interline_space.asLatexString())
2892 os << "\\noalign{\\vskip"
2893 << from_ascii(row_info[row].interline_space.asLatexString())
2899 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2901 bool const is_tabular_star = !is_long_tabular && !tabular_width.zero()
2902 && !hasVarwidthColumn();
2903 bool const is_xltabular = is_long_tabular
2904 && (hasVarwidthColumn() || !tabular_width.zero());
2905 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
2907 //+---------------------------------------------------------------------
2908 //+ first the opening preamble +
2909 //+---------------------------------------------------------------------
2912 if (!TexRow::isNone(pos))
2913 os.texrow().start(pos);
2916 if (is_long_tabular)
2917 os << "\\begin{landscape}\n";
2919 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2922 if (is_long_tabular) {
2924 os << "\\begin{xltabular}";
2926 os << "\\begin{longtable}";
2927 switch (longtabular_alignment) {
2928 case LYX_LONGTABULAR_ALIGN_LEFT:
2931 case LYX_LONGTABULAR_ALIGN_CENTER:
2934 case LYX_LONGTABULAR_ALIGN_RIGHT:
2939 if (tabular_width.zero())
2940 os << "{" << from_ascii("\\columnwidth") << "}";
2942 os << "{" << from_ascii(tabular_width.asLatexString()) << "}";
2945 if (is_tabular_star)
2946 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2947 else if (hasVarwidthColumn()) {
2948 os << "\\begin{tabularx}{";
2949 if (tabular_width.zero())
2950 os << from_ascii("\\columnwidth") << "}";
2952 os << from_ascii(tabular_width.asLatexString()) << "}";
2954 os << "\\begin{tabular}";
2955 switch (tabular_valignment) {
2956 case LYX_VALIGN_TOP:
2959 case LYX_VALIGN_MIDDLE:
2961 case LYX_VALIGN_BOTTOM:
2969 if (is_tabular_star)
2970 os << "@{\\extracolsep{\\fill}}";
2972 // The bidi package (loaded by polyglossia) swaps the column
2973 // order for RTL (#9686). Thus we use this list.
2974 bool const bidi_rtl =
2975 runparams.local_font->isRightToLeft()
2976 && runparams.use_polyglossia;
2977 list<col_type> columns;
2978 for (col_type cl = 0; cl < ncols(); ++cl) {
2980 columns.push_front(cl);
2982 columns.push_back(cl);
2985 for (auto const & c : columns) {
2986 if ((bidi_rtl && columnRightLine(c)) || (!bidi_rtl && columnLeftLine(c)))
2988 if (!column_info[c].align_special.empty()) {
2989 os << column_info[c].align_special;
2991 if (!column_info[c].p_width.zero()) {
2992 bool decimal = false;
2993 switch (column_info[c].alignment) {
2994 case LYX_ALIGN_LEFT:
2995 os << ">{\\raggedright}";
2997 case LYX_ALIGN_RIGHT:
2998 os << ">{\\raggedleft}";
3000 case LYX_ALIGN_CENTER:
3001 os << ">{\\centering}";
3003 case LYX_ALIGN_NONE:
3004 case LYX_ALIGN_BLOCK:
3005 case LYX_ALIGN_LAYOUT:
3006 case LYX_ALIGN_SPECIAL:
3008 case LYX_ALIGN_DECIMAL: {
3010 os << ">{\\raggedright}";
3012 os << ">{\\raggedleft}";
3019 switch (column_info[c].valignment) {
3020 case LYX_VALIGN_TOP:
3021 // this is the default
3023 case LYX_VALIGN_MIDDLE:
3026 case LYX_VALIGN_BOTTOM:
3032 // Fixed-width cells with alignment at decimal separator
3033 // are output as two cells of half the width with the decimal
3034 // separator as column sep. This effectively puts the content
3035 // centered, which differs from the normal decimal sep alignment
3036 // and is not ideal, but we cannot do better ATM (see #9568).
3037 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
3039 docstring const halffixedwith =
3040 from_ascii(Length(column_info[c].p_width.value() / 2,
3041 column_info[c].p_width.unit()).asLatexString());
3045 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
3052 << from_ascii(column_info[c].p_width.asLatexString())
3054 } else if (column_info[c].varwidth) {
3055 switch (column_info[c].alignment) {
3056 case LYX_ALIGN_LEFT:
3057 os << ">{\\raggedright\\arraybackslash}";
3059 case LYX_ALIGN_RIGHT:
3060 os << ">{\\raggedleft\\arraybackslash}";
3062 case LYX_ALIGN_CENTER:
3063 os << ">{\\centering\\arraybackslash}";
3065 case LYX_ALIGN_NONE:
3066 case LYX_ALIGN_BLOCK:
3067 case LYX_ALIGN_LAYOUT:
3068 case LYX_ALIGN_SPECIAL:
3069 case LYX_ALIGN_DECIMAL:
3073 } else if (isVTypeColumn(c)) {
3074 switch (column_info[c].alignment) {
3075 case LYX_ALIGN_LEFT:
3076 os << ">{\\raggedright}";
3078 case LYX_ALIGN_RIGHT:
3079 os << ">{\\raggedleft}";
3081 case LYX_ALIGN_CENTER:
3082 os << ">{\\centering}";
3084 case LYX_ALIGN_NONE:
3085 case LYX_ALIGN_BLOCK:
3086 case LYX_ALIGN_LAYOUT:
3087 case LYX_ALIGN_SPECIAL:
3088 case LYX_ALIGN_DECIMAL:
3091 os << "V{\\linewidth}";
3093 switch (column_info[c].alignment) {
3094 case LYX_ALIGN_LEFT:
3097 case LYX_ALIGN_RIGHT:
3100 case LYX_ALIGN_DECIMAL:
3101 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
3107 } // end if else !column_info[i].p_width
3108 } // end if else !column_info[i].align_special
3109 if ((bidi_rtl && columnLeftLine(c)) || (!bidi_rtl && columnRightLine(c)))
3114 TeXLongtableHeaderFooter(os, runparams, columns);
3116 //+---------------------------------------------------------------------
3117 //+ the single row and columns (cells) +
3118 //+---------------------------------------------------------------------
3120 for (row_type r = 0; r < nrows(); ++r) {
3121 if (isValidRow(r)) {
3122 TeXRow(os, r, runparams, columns);
3123 if (is_long_tabular && row_info[r].newpage)
3124 os << "\\newpage\n";
3128 //+---------------------------------------------------------------------
3129 //+ the closing of the tabular +
3130 //+---------------------------------------------------------------------
3132 if (is_long_tabular) {
3134 os << "\\end{xltabular}";
3136 os << "\\end{longtable}";
3138 if (is_tabular_star)
3139 os << "\\end{tabular*}";
3140 else if (hasVarwidthColumn())
3141 os << "\\end{tabularx}";
3143 os << "\\end{tabular}";
3147 if (is_long_tabular)
3148 os << breakln << "\\end{landscape}";
3150 os << breakln << "\\end{turn}";
3153 if (!TexRow::isNone(pos))
3154 os.texrow().start(pos);
3158 int Tabular::docbookRow(odocstream & os, row_type row,
3159 OutputParams const & runparams) const
3162 idx_type cell = getFirstCellInRow(row);
3165 for (col_type c = 0; c < ncols(); ++c) {
3166 if (isPartOfMultiColumn(row, c))
3169 os << "<entry align=\"";
3170 switch (getAlignment(cell)) {
3171 case LYX_ALIGN_LEFT:
3174 case LYX_ALIGN_RIGHT:
3182 os << "\" valign=\"";
3183 switch (getVAlignment(cell)) {
3184 case LYX_VALIGN_TOP:
3187 case LYX_VALIGN_BOTTOM:
3190 case LYX_VALIGN_MIDDLE:
3195 if (isMultiColumn(cell)) {
3196 os << " namest=\"col" << c << "\" ";
3197 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
3201 ret += cellInset(cell)->docbook(os, runparams);
3210 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
3214 //+---------------------------------------------------------------------
3215 //+ first the opening preamble +
3216 //+---------------------------------------------------------------------
3218 os << "<tgroup cols=\"" << ncols()
3219 << "\" colsep=\"1\" rowsep=\"1\">\n";
3221 for (col_type c = 0; c < ncols(); ++c) {
3222 os << "<colspec colname=\"col" << c << "\" align=\"";
3223 switch (column_info[c].alignment) {
3224 case LYX_ALIGN_LEFT:
3227 case LYX_ALIGN_RIGHT:
3235 if (runparams.flavor == OutputParams::XML)
3241 //+---------------------------------------------------------------------
3242 //+ Long Tabular case +
3243 //+---------------------------------------------------------------------
3245 // output caption info
3246 // The caption flag wins over head/foot
3247 if (haveLTCaption()) {
3248 os << "<caption>\n";
3250 for (row_type r = 0; r < nrows(); ++r) {
3251 if (row_info[r].caption) {
3252 ret += docbookRow(os, r, runparams);
3255 os << "</caption>\n";
3258 // output header info
3259 if (haveLTHead(false) || haveLTFirstHead(false)) {
3262 for (row_type r = 0; r < nrows(); ++r) {
3263 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
3264 !row_info[r].caption) {
3265 ret += docbookRow(os, r, runparams);
3271 // output footer info
3272 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3275 for (row_type r = 0; r < nrows(); ++r) {
3276 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3277 !row_info[r].caption) {
3278 ret += docbookRow(os, r, runparams);
3285 //+---------------------------------------------------------------------
3286 //+ the single row and columns (cells) +
3287 //+---------------------------------------------------------------------
3291 for (row_type r = 0; r < nrows(); ++r) {
3292 if (isValidRow(r)) {
3293 ret += docbookRow(os, r, runparams);
3298 //+---------------------------------------------------------------------
3299 //+ the closing of the tabular +
3300 //+---------------------------------------------------------------------
3309 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3310 OutputParams const & runparams, bool header) const
3313 string const celltag = header ? "th" : "td";
3314 idx_type cell = getFirstCellInRow(row);
3316 xs << html::StartTag("tr");
3317 for (col_type c = 0; c < ncols(); ++c) {
3318 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3323 Length const cwidth = column_info[c].p_width;
3324 if (!cwidth.zero()) {
3325 string const hwidth = cwidth.asHTMLString();
3326 attr << "style =\"width: " << hwidth << ";\" ";
3330 switch (getAlignment(cell)) {
3331 case LYX_ALIGN_LEFT:
3334 case LYX_ALIGN_RIGHT:
3342 attr << " valign='";
3343 switch (getVAlignment(cell)) {
3344 case LYX_VALIGN_TOP:
3347 case LYX_VALIGN_BOTTOM:
3350 case LYX_VALIGN_MIDDLE:
3355 if (isMultiColumn(cell))
3356 attr << " colspan='" << columnSpan(cell) << "'";
3357 else if (isMultiRow(cell))
3358 attr << " rowspan='" << rowSpan(cell) << "'";
3360 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3361 ret += cellInset(cell)->xhtml(xs, runparams);
3362 xs << html::EndTag(celltag) << html::CR();
3365 xs << html::EndTag("tr");
3370 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3374 if (is_long_tabular) {
3375 // we'll wrap it in a div, so as to deal with alignment
3377 switch (longtabular_alignment) {
3378 case LYX_LONGTABULAR_ALIGN_LEFT:
3381 case LYX_LONGTABULAR_ALIGN_CENTER:
3384 case LYX_LONGTABULAR_ALIGN_RIGHT:
3388 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3390 // The caption flag wins over head/foot
3391 if (haveLTCaption()) {
3392 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3394 for (row_type r = 0; r < nrows(); ++r)
3395 if (row_info[r].caption)
3396 ret += xhtmlRow(xs, r, runparams);
3397 xs << html::EndTag("div") << html::CR();
3401 xs << html::StartTag("table") << html::CR();
3403 // output header info
3404 bool const havefirsthead = haveLTFirstHead(false);
3405 // if we have a first head, then we are going to ignore the
3406 // headers for the additional pages, since there aren't any
3407 // in XHTML. this test accomplishes that.
3408 bool const havehead = !havefirsthead && haveLTHead(false);
3409 if (havehead || havefirsthead) {
3410 xs << html::StartTag("thead") << html::CR();
3411 for (row_type r = 0; r < nrows(); ++r) {
3412 if (((havefirsthead && row_info[r].endfirsthead) ||
3413 (havehead && row_info[r].endhead)) &&
3414 !row_info[r].caption) {
3415 ret += xhtmlRow(xs, r, runparams, true);
3418 xs << html::EndTag("thead") << html::CR();
3420 // output footer info
3421 bool const havelastfoot = haveLTLastFoot(false);
3423 bool const havefoot = !havelastfoot && haveLTFoot(false);
3424 if (havefoot || havelastfoot) {
3425 xs << html::StartTag("tfoot") << html::CR();
3426 for (row_type r = 0; r < nrows(); ++r) {
3427 if (((havelastfoot && row_info[r].endlastfoot) ||
3428 (havefoot && row_info[r].endfoot)) &&
3429 !row_info[r].caption) {
3430 ret += xhtmlRow(xs, r, runparams);
3433 xs << html::EndTag("tfoot") << html::CR();
3436 xs << html::StartTag("tbody") << html::CR();
3437 for (row_type r = 0; r < nrows(); ++r) {
3438 if (isValidRow(r)) {
3439 ret += xhtmlRow(xs, r, runparams);
3442 xs << html::EndTag("tbody")
3444 << html::EndTag("table")
3446 if (is_long_tabular)
3447 xs << html::EndTag("div") << html::CR();
3452 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3453 vector<unsigned int> const & clen) const
3455 idx_type const fcell = getFirstCellInRow(row);
3456 idx_type const n = numberOfCellsInRow(row) + fcell;
3459 for (idx_type i = fcell; i < n; ++i) {
3469 for (idx_type i = fcell; i < n; ++i) {
3480 col_type column = cellColumn(i);
3481 int len = clen[column];
3482 while (column < ncols() - 1
3483 && isPartOfMultiColumn(row, ++column))
3484 len += clen[column] + 4;
3485 os << docstring(len, ch);
3500 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3501 vector<unsigned int> const & clen) const
3503 idx_type const fcell = getFirstCellInRow(row);
3504 idx_type const n = numberOfCellsInRow(row) + fcell;
3507 for (idx_type i = fcell; i < n; ++i) {
3508 if (bottomLine(i)) {
3517 for (idx_type i = fcell; i < n; ++i) {
3518 if (bottomLine(i)) {
3528 col_type column = cellColumn(i);
3529 int len = clen[column];
3530 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3531 len += clen[column] + 4;
3532 os << docstring(len, ch);
3533 if (bottomLine(i)) {
3547 void Tabular::plaintextPrintCell(odocstringstream & os,
3548 OutputParams const & runparams,
3549 idx_type cell, row_type row, col_type column,
3550 vector<unsigned int> const & clen,
3551 bool onlydata, size_t max_length) const
3553 odocstringstream sstr;
3554 cellInset(cell)->plaintext(sstr, runparams, max_length);
3566 unsigned int len1 = sstr.str().length();
3567 unsigned int len2 = clen[column];
3568 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3569 len2 += clen[column] + 4;
3572 switch (getAlignment(cell)) {
3574 case LYX_ALIGN_LEFT:
3577 case LYX_ALIGN_RIGHT:
3581 case LYX_ALIGN_CENTER:
3587 os << docstring(len1, ' ') << sstr.str()
3588 << docstring(len2, ' ');
3590 if (rightLine(cell))
3597 void Tabular::plaintext(odocstringstream & os,
3598 OutputParams const & runparams, int const depth,
3599 bool onlydata, char_type delim, size_t max_length) const
3601 // first calculate the width of the single columns
3602 vector<unsigned int> clen(ncols());
3605 // first all non multicolumn cells!
3606 for (col_type c = 0; c < ncols(); ++c) {
3608 for (row_type r = 0; r < nrows(); ++r) {
3609 idx_type cell = cellIndex(r, c);
3610 if (isMultiColumn(cell))
3612 odocstringstream sstr;
3613 cellInset(cell)->plaintext(sstr, runparams, max_length);
3614 if (clen[c] < sstr.str().length())
3615 clen[c] = sstr.str().length();
3618 // then all multicolumn cells!
3619 for (col_type c = 0; c < ncols(); ++c) {
3620 for (row_type r = 0; r < nrows(); ++r) {
3621 idx_type cell = cellIndex(r, c);
3622 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3624 odocstringstream sstr;
3625 cellInset(cell)->plaintext(sstr, runparams, max_length);
3626 int len = int(sstr.str().length());
3627 idx_type const n = columnSpan(cell);
3628 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3630 if (len > int(clen[c + n - 1]))
3631 clen[c + n - 1] = len;
3636 for (row_type r = 0; r < nrows(); ++r) {
3637 if (!onlydata && plaintextTopHLine(os, r, clen))
3638 os << docstring(depth * 2, ' ');
3639 for (col_type c = 0; c < ncols(); ++c) {
3640 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3642 if (onlydata && c > 0)
3643 // we don't use operator<< for single UCS4 character.
3644 // see explanation in docstream.h
3646 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3648 if (os.str().size() > max_length)
3653 os << docstring(depth * 2, ' ');
3654 if (plaintextBottomHLine(os, r, clen))
3655 os << docstring(depth * 2, ' ');
3657 if (os.str().size() > max_length)
3663 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3665 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3669 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3671 return cell_info[row][column].inset;
3675 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3677 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3681 void Tabular::setCellInset(row_type row, col_type column,
3682 shared_ptr<InsetTableCell> ins)
3684 CellData & cd = cell_info[row][column];
3689 void Tabular::validate(LaTeXFeatures & features) const
3691 features.require("NeedTabularnewline");
3693 features.require("booktabs");
3694 if (is_long_tabular && !hasVarwidthColumn()) {
3695 if (tabular_width.zero())
3696 features.require("longtable");
3698 features.require("xltabular");
3700 if (rotate && is_long_tabular)
3701 features.require("lscape");
3703 features.require("rotating");
3704 if (hasVarwidthColumn()) {
3705 if (is_long_tabular)
3706 features.require("xltabular");
3708 features.require("tabularx");
3710 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3711 if (isMultiRow(cell))
3712 features.require("multirow");
3713 if (getUsebox(cell) == BOX_VARWIDTH)
3714 features.require("varwidth");
3715 if (getVAlignment(cell) != LYX_VALIGN_TOP
3716 || !getPWidth(cell).zero()
3717 || isVTypeColumn(cellColumn(cell)))
3718 features.require("array");
3719 // Tell footnote that we need a savenote
3720 // environment in non-long tables or
3721 // longtable headers/footers
3722 else if (!is_long_tabular && !features.inFloat())
3723 features.saveNoteEnv("tabular");
3724 else if (!isValidRow(cellRow(cell)))
3725 features.saveNoteEnv("longtable");
3727 cellInset(cell)->validate(features);
3728 features.saveNoteEnv(string());
3733 Tabular::BoxType Tabular::useBox(idx_type cell) const
3735 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3736 if (parlist.size() > 1)
3737 return BOX_VARWIDTH;
3739 ParagraphList::const_iterator cit = parlist.begin();
3740 ParagraphList::const_iterator end = parlist.end();
3742 for (; cit != end; ++cit)
3743 for (int i = 0; i < cit->size(); ++i)
3744 if (cit->isNewline(i) || cit->layout().isEnvironment())
3745 return BOX_VARWIDTH;
3751 /////////////////////////////////////////////////////////////////////
3755 /////////////////////////////////////////////////////////////////////
3757 InsetTableCell::InsetTableCell(Buffer * buf)
3758 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3759 isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
3763 bool InsetTableCell::forcePlainLayout(idx_type) const
3765 return isMultiRow || (isMultiColumn && !isFixedWidth);
3769 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3771 return isFixedWidth;
3775 bool InsetTableCell::forceLocalFontSwitch() const
3777 return isFixedWidth;
3781 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3782 FuncStatus & status) const
3784 bool enabled = true;
3785 switch (cmd.action()) {
3786 case LFUN_MATH_DISPLAY:
3787 if (!hasFixedWidth()) {
3792 return InsetText::getStatus(cur, cmd, status);
3794 status.setEnabled(enabled);
3798 docstring InsetTableCell::asString(bool intoInsets)
3801 if (paragraphs().empty())
3803 ParagraphList::const_iterator it = paragraphs().begin();
3804 ParagraphList::const_iterator en = paragraphs().end();
3806 for (; it != en; ++it) {
3811 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3817 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3818 UpdateType utype, TocBackend & backend) const
3820 InsetText::iterateForToc(di, output_active, utype, backend);
3824 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3827 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3828 return InsetText::xhtml(xs, rp);
3832 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
3834 TextMetrics & tm = mi.base.bv->textMetrics(&text());
3836 // Hand font through to contained lyxtext:
3837 tm.font_.fontInfo() = mi.base.font;
3838 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
3840 // This can happen when a layout has a left and right margin,
3841 // and the view is made very narrow. We can't do better than
3842 // to draw it partly out of view (bug 5890).
3843 if (mi.base.textwidth < 1)
3844 mi.base.textwidth = 1;
3846 // We tell metrics here not to expand on multiple pars
3847 // This is the difference to InsetText::Metrics
3848 if (hasFixedWidth())
3849 tm.metrics(mi, dim, mi.base.textwidth, false);
3851 tm.metrics(mi, dim, 0, false);
3852 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
3853 dim.asc += TEXT_TO_INSET_OFFSET;
3854 dim.des += TEXT_TO_INSET_OFFSET;
3855 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
3860 /////////////////////////////////////////////////////////////////////
3864 /////////////////////////////////////////////////////////////////////
3866 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3868 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3869 rowselect_(false), colselect_(false)
3874 InsetTabular::InsetTabular(InsetTabular const & tab)
3875 : Inset(tab), tabular(tab.tabular),
3876 rowselect_(false), colselect_(false)
3881 InsetTabular::~InsetTabular()
3883 hideDialogs("tabular", this);
3887 void InsetTabular::setBuffer(Buffer & buf)
3889 tabular.setBuffer(buf);
3890 Inset::setBuffer(buf);
3894 bool InsetTabular::insetAllowed(InsetCode code) const
3899 case MATHMACRO_CODE:
3904 return tabular.is_long_tabular;
3912 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3914 return tabular.is_long_tabular &&
3915 (newtype == "Standard" || newtype == "Unnumbered");
3919 void InsetTabular::write(ostream & os) const
3921 os << "Tabular" << endl;
3926 string InsetTabular::contextMenu(BufferView const &, int, int) const
3928 // FIXME: depending on the selection state,
3929 // we could offer a different menu.
3930 return cell(0)->contextMenuName() + ";" + contextMenuName();
3934 string InsetTabular::contextMenuName() const
3936 return "context-tabular";
3940 void InsetTabular::read(Lexer & lex)
3942 //bool const old_format = (lex.getString() == "\\LyXTable");
3950 string token = lex.getString();
3951 while (lex && token != "\\end_inset") {
3953 token = lex.getString();
3956 lex.printError("Missing \\end_inset at this point. ");
3960 int InsetTabular::rowFromY(Cursor & cur, int y) const
3962 // top y coordinate of tabular
3963 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3965 for (; r < tabular.nrows() && y > h; ++r)
3966 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3967 + tabular.interRowSpace(r);
3973 int InsetTabular::columnFromX(Cursor & cur, int x) const
3975 // left x coordinate of tabular
3976 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3978 for (; c < tabular.ncols() && x > w; ++c)
3979 w += tabular.cellWidth(c);
3984 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3986 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3987 // mi.base.textwidth << "\n";
3988 LBUFERR(mi.base.bv);
3990 for (row_type r = 0; r < tabular.nrows(); ++r) {
3993 for (col_type c = 0; c < tabular.ncols(); ++c) {
3994 if (tabular.isPartOfMultiColumn(r, c)
3995 || tabular.isPartOfMultiRow(r, c))
3996 // multicolumn or multirow cell, but not first one
3998 idx_type const cell = tabular.cellIndex(r, c);
4001 Length const p_width = tabular.getPWidth(cell);
4002 if (!p_width.zero())
4003 m.base.textwidth = mi.base.inPixels(p_width);
4004 tabular.cellInset(cell)->metrics(m, dim0);
4005 if (!p_width.zero())
4006 dim0.wid = m.base.textwidth;
4007 tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
4008 + tabular.interColumnSpace(cell);
4010 // FIXME(?): do we need a second metrics call?
4011 TextMetrics const & tm =
4012 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
4014 // determine horizontal offset because of decimal align (if necessary)
4015 int decimal_width = 0;
4016 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
4017 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
4018 tail.setBuffer(tabular.buffer());
4019 // we need to set macrocontext position everywhere
4020 // otherwise we crash with nested insets (e.g. footnotes)
4021 // after decimal point
4022 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
4024 dit.push_back(CursorSlice(tail));
4025 tail.setMacrocontextPositionRecursive(dit);
4027 // remove text leading decimal point
4028 docstring const align_d = tabular.column_info[c].decimal_point;
4029 dit = separatorPos(&tail, align_d);
4031 pit_type const psize = tail.paragraphs().front().size();
4033 tail.paragraphs().front().eraseChars(0,
4034 dit.pos() < psize ? dit.pos() + 1 : psize, false);
4036 tail.metrics(m, dim1);
4037 decimal_width = dim1.width();
4040 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
4041 tabular.cell_info[r][c].decimal_width = decimal_width;
4043 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
4044 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
4045 int const lastpardes = tm.last().second->descent()
4046 + TEXT_TO_INSET_OFFSET;
4048 switch (tabular.getVAlignment(cell)) {
4049 case Tabular::LYX_VALIGN_TOP:
4051 case Tabular::LYX_VALIGN_MIDDLE:
4052 offset = -(dim0.des - lastpardes)/2;
4054 case Tabular::LYX_VALIGN_BOTTOM:
4055 offset = -(dim0.des - lastpardes);
4058 tabular.cell_info[r][c].voffset = offset;
4059 maxasc = max(maxasc, dim0.asc - offset);
4060 maxdes = max(maxdes, dim0.des + offset);
4062 int const top_space = tabular.row_info[r].top_space_default ?
4063 default_line_space :
4064 mi.base.inPixels(tabular.row_info[r].top_space);
4065 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
4066 int const bottom_space = tabular.row_info[r].bottom_space_default ?
4067 default_line_space :
4068 mi.base.inPixels(tabular.row_info[r].bottom_space);
4069 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
4072 tabular.updateColumnWidths(mi);
4073 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
4074 dim.des = tabular.height() - dim.asc;
4075 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
4079 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
4081 if (&cur.inset() == this && cur.selection()) {
4082 if (cur.selIsMultiCell()) {
4085 getSelection(cur, rs, re, cs, ce);
4087 idx_type const cell = tabular.cellIndex(row, col);
4088 col_type const cspan = tabular.columnSpan(cell);
4089 row_type const rspan = tabular.rowSpan(cell);
4090 if (col + cspan - 1 >= cs && col <= ce
4091 && row + rspan - 1 >= rs && row <= re)
4094 if (col == tabular.cellColumn(cur.idx())
4095 && row == tabular.cellRow(cur.idx())) {
4096 CursorSlice const & beg = cur.selBegin();
4097 CursorSlice const & end = cur.selEnd();
4099 if ((end.lastpos() > 0 || end.lastpit() > 0)
4100 && end.pos() == end.lastpos() && beg.pos() == 0
4101 && end.pit() == end.lastpit() && beg.pit() == 0)
4109 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
4111 x += ADD_TO_TABULAR_WIDTH;
4113 BufferView * bv = pi.base.bv;
4114 Cursor & cur = pi.base.bv->cursor();
4116 // FIXME: As the full background is painted in drawBackground(),
4117 // we have no choice but to do a full repaint for the Text cells.
4118 pi.full_repaint = true;
4120 bool const original_selection_state = pi.selected;
4124 int yy = y + tabular.offsetVAlignment();
4125 for (row_type r = 0; r < tabular.nrows(); ++r) {
4127 for (col_type c = 0; c < tabular.ncols(); ++c) {
4128 if (tabular.isPartOfMultiColumn(r, c))
4131 idx = tabular.cellIndex(r, c);
4133 if (tabular.isPartOfMultiRow(r, c)) {
4134 nx += tabular.cellWidth(idx);
4138 pi.selected |= isCellSelected(cur, r, c);
4139 int const cx = nx + tabular.textHOffset(idx);
4140 int const cy = yy + tabular.textVOffset(idx);
4141 // Cache the Inset position.
4142 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
4143 cell(idx)->draw(pi, cx, cy);
4144 drawCellLines(pi, nx, yy, r, idx);
4145 nx += tabular.cellWidth(idx);
4146 pi.selected = original_selection_state;
4149 if (r + 1 < tabular.nrows())
4150 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4151 + tabular.interRowSpace(r + 1);
4156 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
4158 x += ADD_TO_TABULAR_WIDTH;
4159 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
4160 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
4161 pi.backgroundColor(this));
4165 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
4167 Cursor & cur = pi.base.bv->cursor();
4169 x += ADD_TO_TABULAR_WIDTH;
4171 if (!cur.selection())
4173 if (&cur.inset() != this)
4178 bool const full_cell_selected = isCellSelected(cur,
4179 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
4181 if (cur.selIsMultiCell() || full_cell_selected) {
4182 for (row_type r = 0; r < tabular.nrows(); ++r) {
4184 for (col_type c = 0; c < tabular.ncols(); ++c) {
4185 if (tabular.isPartOfMultiColumn(r, c))
4188 idx_type const cell = tabular.cellIndex(r, c);
4190 if (tabular.isPartOfMultiRow(r, c)) {
4191 xx += tabular.cellWidth(cell);
4194 int const w = tabular.cellWidth(cell);
4195 int const h = tabular.cellHeight(cell);
4196 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
4197 if (isCellSelected(cur, r, c))
4198 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
4201 if (r + 1 < tabular.nrows())
4202 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4203 + tabular.interRowSpace(r + 1);
4207 // FIXME: This code has no effect because InsetTableCell does not handle
4208 // drawSelection other than the trivial implementation in Inset.
4210 // x += cellXPos(cur.idx());
4211 // x += tabular.textHOffset(cur.idx());
4212 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
4217 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
4218 row_type row, idx_type cell) const
4220 y -= tabular.rowAscent(row);
4221 int const w = tabular.cellWidth(cell);
4222 int const h = tabular.cellHeight(cell);
4223 Color const linecolor = pi.textColor(Color_tabularline);
4224 Color const gridcolor = pi.textColor(Color_tabularonoffline);
4227 bool drawline = tabular.topLine(cell)
4228 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
4229 pi.pain.line(x, y, x + w, y,
4230 drawline ? linecolor : gridcolor,
4231 drawline ? Painter::line_solid : Painter::line_onoffdash);
4234 drawline = tabular.bottomLine(cell);
4235 pi.pain.line(x, y + h, x + w, y + h,
4236 drawline ? linecolor : gridcolor,
4237 drawline ? Painter::line_solid : Painter::line_onoffdash);
4240 col_type const col = tabular.cellColumn(cell);
4241 drawline = tabular.leftLine(cell)
4242 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
4243 pi.pain.line(x, y, x, y + h,
4244 drawline ? linecolor : gridcolor,
4245 drawline ? Painter::line_solid : Painter::line_onoffdash);
4248 x -= tabular.interColumnSpace(cell);
4249 col_type next_cell_col = col + 1;
4250 while (next_cell_col < tabular.ncols()
4251 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
4253 drawline = tabular.rightLine(cell)
4254 || (next_cell_col < tabular.ncols()
4255 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
4256 pi.pain.line(x + w, y, x + w, y + h,
4257 drawline ? linecolor : gridcolor,
4258 drawline ? Painter::line_solid : Painter::line_onoffdash);
4262 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
4264 //lyxerr << "InsetTabular::edit: " << this << endl;
4268 if (isRightToLeft(cur))
4269 cur.idx() = tabular.getLastCellInRow(0);
4275 if (isRightToLeft(cur))
4276 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
4278 cur.idx() = tabular.numberofcells - 1;
4280 cur.pos() = cur.lastpos(); // FIXME crude guess
4282 cur.setCurrentFont();
4283 // FIXME: this accesses the position cache before it is initialized
4284 //cur.bv().fitCursor();
4288 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
4290 // In a longtable, tell captions what the current float is
4291 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
4292 string const saveflt = cnts.current_float();
4293 if (tabular.is_long_tabular) {
4294 cnts.current_float("table");
4295 // in longtables, we only step the counter once
4296 cnts.step(from_ascii("table"), utype);
4297 cnts.isLongtable(true);
4300 ParIterator it2 = it;
4302 size_t const end = it2.nargs();
4303 for ( ; it2.idx() < end; it2.top().forwardIdx())
4304 buffer().updateBuffer(it2, utype);
4307 if (tabular.is_long_tabular) {
4308 cnts.current_float(saveflt);
4309 cnts.isLongtable(false);
4314 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4315 UpdateType utype, TocBackend & backend) const
4317 DocIterator dit = cpit;
4319 size_t const end = dit.nargs();
4320 for ( ; dit.idx() < end; dit.top().forwardIdx())
4321 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4325 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4327 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4328 return x < x0 || x > x0 + tabular.width();
4332 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4334 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4335 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4336 // there is no margin added vertically to tabular insets.
4337 // However, it works for now.
4338 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4342 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4344 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4348 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4350 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4351 << "\n cur:" << cur);
4352 CursorSlice sl = cur.top();
4353 Cursor & bvcur = cur.bv().cursor();
4355 FuncCode const act = cmd.action();
4359 case LFUN_MOUSE_PRESS: {
4360 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4362 if (hitSelectRow(cur.bv(), cmd.x())) {
4363 row_type r = rowFromY(cur, cmd.y());
4364 cur.idx() = tabular.getFirstCellInRow(r);
4368 cur.idx() = tabular.getLastCellInRow(r);
4369 cur.pit() = cur.lastpit();
4370 cur.pos() = cur.lastpos();
4371 cur.selection(true);
4377 if (hitSelectColumn(cur.bv(), cmd.y())) {
4378 col_type c = columnFromX(cur, cmd.x());
4379 cur.idx() = tabular.cellIndex(0, c);
4383 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4384 cur.pit() = cur.lastpit();
4385 cur.pos() = cur.lastpos();
4386 cur.selection(true);
4391 // do not reset cursor/selection if we have selected
4392 // some cells (bug 2715).
4393 if (cmd.button() == mouse_button::button3
4394 && &bvcur.selBegin().inset() == this
4395 && bvcur.selIsMultiCell())
4398 // Let InsetTableCell do it
4399 cell(cur.idx())->dispatch(cur, cmd);
4402 case LFUN_MOUSE_MOTION:
4403 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4404 if (cmd.button() == mouse_button::button1) {
4405 // only accept motions to places not deeper nested than the real anchor
4406 if (!bvcur.realAnchor().hasPart(cur)) {
4410 // select (additional) row
4412 row_type r = rowFromY(cur, cmd.y());
4413 cur.idx() = tabular.getLastCellInRow(r);
4414 // we need to reset the cursor's pit and pos now, as the old ones
4415 // may no longer be valid.
4418 bvcur.setCursor(cur);
4419 bvcur.selection(true);
4422 // select (additional) column
4424 col_type c = columnFromX(cur, cmd.x());
4425 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4426 // we need to reset the cursor's pit and pos now, as the old ones
4427 // may no longer be valid.
4430 bvcur.setCursor(cur);
4431 bvcur.selection(true);
4434 // only update if selection changes
4435 if (bvcur.idx() == cur.idx() &&
4436 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4437 cur.noScreenUpdate();
4438 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4439 bvcur.setCursor(cur);
4440 bvcur.selection(true);
4441 // if this is a multicell selection, we just set the cursor to
4442 // the beginning of the cell's text.
4443 if (bvcur.selIsMultiCell()) {
4444 bvcur.pit() = bvcur.lastpit();
4445 bvcur.pos() = bvcur.lastpos();
4450 case LFUN_MOUSE_RELEASE:
4455 case LFUN_CELL_BACKWARD:
4457 cur.selection(false);
4460 case LFUN_CELL_FORWARD:
4462 cur.selection(false);
4465 case LFUN_CHAR_FORWARD_SELECT:
4466 case LFUN_CHAR_FORWARD:
4467 case LFUN_CHAR_BACKWARD_SELECT:
4468 case LFUN_CHAR_BACKWARD:
4469 case LFUN_CHAR_RIGHT_SELECT:
4470 case LFUN_CHAR_RIGHT:
4471 case LFUN_CHAR_LEFT_SELECT:
4472 case LFUN_CHAR_LEFT:
4473 case LFUN_WORD_FORWARD:
4474 case LFUN_WORD_FORWARD_SELECT:
4475 case LFUN_WORD_BACKWARD:
4476 case LFUN_WORD_BACKWARD_SELECT:
4477 case LFUN_WORD_RIGHT:
4478 case LFUN_WORD_RIGHT_SELECT:
4479 case LFUN_WORD_LEFT:
4480 case LFUN_WORD_LEFT_SELECT: {
4481 // determine whether we move to next or previous cell, where to enter
4482 // the new cell from, and which command to "finish" (i.e., exit the
4485 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4486 FuncCode finish_lfun;
4488 if (act == LFUN_CHAR_FORWARD
4489 || act == LFUN_CHAR_FORWARD_SELECT
4490 || act == LFUN_WORD_FORWARD
4491 || act == LFUN_WORD_FORWARD_SELECT) {
4493 finish_lfun = LFUN_FINISHED_FORWARD;
4495 else if (act == LFUN_CHAR_BACKWARD
4496 || act == LFUN_CHAR_BACKWARD_SELECT
4497 || act == LFUN_WORD_BACKWARD
4498 || act == LFUN_WORD_BACKWARD_SELECT) {
4500 finish_lfun = LFUN_FINISHED_BACKWARD;
4502 // LEFT or RIGHT commands --- the interpretation will depend on the
4503 // table's direction.
4505 bool const right = act == LFUN_CHAR_RIGHT
4506 || act == LFUN_CHAR_RIGHT_SELECT
4507 || act == LFUN_WORD_RIGHT
4508 || act == LFUN_WORD_RIGHT_SELECT;
4509 next_cell = isRightToLeft(cur) != right;
4511 if (lyxrc.visual_cursor)
4512 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4514 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4517 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4518 || act == LFUN_CHAR_BACKWARD_SELECT
4519 || act == LFUN_CHAR_RIGHT_SELECT
4520 || act == LFUN_CHAR_LEFT_SELECT
4521 || act == LFUN_WORD_FORWARD_SELECT
4522 || act == LFUN_WORD_RIGHT_SELECT
4523 || act == LFUN_WORD_BACKWARD_SELECT
4524 || act == LFUN_WORD_LEFT_SELECT;
4526 // If we have a multicell selection or we're
4527 // not doing some LFUN_*_SELECT thing anyway...
4528 if (!cur.selIsMultiCell() || !select) {
4529 col_type const c = tabular.cellColumn(cur.idx());
4530 row_type const r = tabular.cellRow(cur.idx());
4531 // Are we trying to select the whole cell and is the whole cell
4532 // not yet selected?
4533 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4534 ((next_cell && cur.pit() == cur.lastpit()
4535 && cur.pos() == cur.lastpos())
4536 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4538 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4540 // ...try to dispatch to the cell's inset.
4541 cell(cur.idx())->dispatch(cur, cmd);
4543 // When we already have a selection we want to select the whole cell
4544 // before going to the next cell.
4545 if (select_whole && !empty_cell){
4546 getText(cur.idx())->selectAll(cur);
4548 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4552 // FIXME: When we support the selection of an empty cell, remove
4553 // the !empty_cell from this condition. For now we jump to the next
4554 // cell if the current cell is empty.
4555 if (cur.result().dispatched() && !empty_cell)
4559 // move to next/prev cell, as appropriate
4560 // note that we will always do this if we're selecting and we have
4561 // a multicell selection
4562 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4563 << " cell from: " << int(entry_from));
4565 moveNextCell(cur, entry_from);
4567 movePrevCell(cur, entry_from);
4568 // if we're exiting the table, call the appropriate FINISHED lfun
4569 if (sl == cur.top()) {
4570 cmd = FuncRequest(finish_lfun);
4575 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4580 case LFUN_DOWN_SELECT:
4582 if (!(cur.selection() && cur.selIsMultiCell()))
4583 cell(cur.idx())->dispatch(cur, cmd);
4585 cur.dispatched(); // override the cell's decision
4586 if (sl == cur.top()) {
4587 // if our Text didn't do anything to the cursor
4588 // then we try to put the cursor into the cell below
4589 // setting also the right targetX.
4590 cur.selHandle(act == LFUN_DOWN_SELECT);
4591 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4592 int const xtarget = cur.targetX();
4593 // WARNING: Once cur.idx() has been reset, the cursor is in
4594 // an inconsistent state until pos() has been set. Be careful
4595 // what you do with it!
4596 cur.idx() = tabular.cellBelow(cur.idx());
4598 TextMetrics const & tm =
4599 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4600 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4601 cur.setCurrentFont();
4604 if (sl == cur.top()) {
4605 // we trick it to go to forward after leaving the
4607 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4610 if (cur.selIsMultiCell()) {
4611 cur.pit() = cur.lastpit();
4612 cur.pos() = cur.lastpos();
4613 cur.setCurrentFont();
4614 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4617 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4620 case LFUN_UP_SELECT:
4622 if (!(cur.selection() && cur.selIsMultiCell()))
4623 cell(cur.idx())->dispatch(cur, cmd);
4624 cur.dispatched(); // override the cell's decision
4625 if (sl == cur.top()) {
4626 // if our Text didn't do anything to the cursor
4627 // then we try to put the cursor into the cell above
4628 // setting also the right targetX.
4629 cur.selHandle(act == LFUN_UP_SELECT);
4630 if (tabular.cellRow(cur.idx()) != 0) {
4631 int const xtarget = cur.targetX();
4632 // WARNING: Once cur.idx() has been reset, the cursor is in
4633 // an inconsistent state until pos() has been set. Be careful
4634 // what you do with it!
4635 cur.idx() = tabular.cellAbove(cur.idx());
4636 cur.pit() = cur.lastpit();
4637 Text const * text = cell(cur.idx())->getText(0);
4638 TextMetrics const & tm = cur.bv().textMetrics(text);
4639 ParagraphMetrics const & pm =
4640 tm.parMetrics(cur.lastpit());
4641 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4642 cur.setCurrentFont();
4645 if (sl == cur.top()) {
4646 cmd = FuncRequest(LFUN_UP);
4649 if (cur.selIsMultiCell()) {
4651 cur.pos() = cur.lastpos();
4652 cur.setCurrentFont();
4653 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4656 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4659 case LFUN_LAYOUT_TABULAR:
4660 cur.bv().showDialog("tabular");
4663 case LFUN_INSET_MODIFY:
4664 // we come from the dialog
4665 if (cmd.getArg(0) == "tabular")
4666 tabularFeatures(cur, cmd.getLongArg(1));
4671 case LFUN_TABULAR_FEATURE:
4672 tabularFeatures(cur, to_utf8(cmd.argument()));
4675 // insert file functions
4676 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4677 case LFUN_FILE_INSERT_PLAINTEXT:
4679 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4680 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4681 FileName(to_utf8(cmd.argument())));
4684 cur.recordUndoInset();
4685 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4686 // content has been replaced,
4687 // so cursor might be invalid
4688 cur.pos() = cur.lastpos();
4689 cur.pit() = cur.lastpit();
4690 bvcur.setCursor(cur);
4697 if (cur.selIsMultiCell()) {
4698 if (copySelection(cur)) {
4699 cur.recordUndoInset();
4703 cell(cur.idx())->dispatch(cur, cmd);
4706 case LFUN_SELF_INSERT:
4707 if (cur.selIsMultiCell()) {
4708 cur.recordUndoInset();
4710 BufferView * bv = &cur.bv();
4711 docstring::const_iterator cit = cmd.argument().begin();
4712 docstring::const_iterator const end = cmd.argument().end();
4713 for (; cit != end; ++cit)
4714 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4717 bv->bookmarkEditPosition();
4719 cell(cur.idx())->dispatch(cur, cmd);
4722 case LFUN_CHAR_DELETE_BACKWARD:
4723 case LFUN_CHAR_DELETE_FORWARD:
4724 if (cur.selIsMultiCell()) {
4725 cur.recordUndoInset();
4728 cell(cur.idx())->dispatch(cur, cmd);
4732 if (!cur.selection())
4734 if (cur.selIsMultiCell()) {
4738 cell(cur.idx())->dispatch(cur, cmd);
4741 case LFUN_CLIPBOARD_PASTE:
4742 case LFUN_PRIMARY_SELECTION_PASTE: {
4743 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4744 theClipboard().getAsText(Clipboard::PlainTextType) :
4745 theSelection().get();
4748 // pass to InsertPlaintextString, but
4749 // only if we have multi-cell content
4750 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4751 cur.recordUndoInset();
4752 if (insertPlaintextString(cur.bv(), clip, false)) {
4753 // content has been replaced,
4754 // so cursor might be invalid
4755 cur.pos() = cur.lastpos();
4756 cur.pit() = cur.lastpit();
4757 bvcur.setCursor(cur);
4761 // Let the cell handle normal text
4762 cell(cur.idx())->dispatch(cur, cmd);
4767 if (!tabularStackDirty()) {
4768 if (!cur.selIsMultiCell())
4769 cell(cur.idx())->dispatch(cur, cmd);
4772 if (theClipboard().isInternal()) {
4773 cur.recordUndoInset();
4774 pasteClipboard(cur);
4778 case LFUN_FONT_EMPH:
4779 case LFUN_FONT_BOLD:
4780 case LFUN_FONT_BOLDSYMBOL:
4781 case LFUN_FONT_ROMAN:
4782 case LFUN_FONT_NOUN:
4783 case LFUN_FONT_ITAL:
4784 case LFUN_FONT_FRAK:
4785 case LFUN_FONT_TYPEWRITER:
4786 case LFUN_FONT_SANS:
4787 case LFUN_TEXTSTYLE_APPLY:
4788 case LFUN_TEXTSTYLE_UPDATE:
4789 case LFUN_FONT_SIZE:
4790 case LFUN_FONT_UNDERLINE:
4791 case LFUN_FONT_STRIKEOUT:
4792 case LFUN_FONT_CROSSOUT:
4793 case LFUN_FONT_UNDERUNDERLINE:
4794 case LFUN_FONT_UNDERWAVE:
4796 case LFUN_PARAGRAPH_PARAMS_APPLY:
4797 case LFUN_PARAGRAPH_PARAMS:
4798 case LFUN_WORD_CAPITALIZE:
4799 case LFUN_WORD_UPCASE:
4800 case LFUN_WORD_LOWCASE:
4801 case LFUN_CHARS_TRANSPOSE:
4802 if (cur.selIsMultiCell()) {
4805 getSelection(cur, rs, re, cs, ce);
4806 Cursor tmpcur = cur;
4807 for (row_type r = rs; r <= re; ++r) {
4808 for (col_type c = cs; c <= ce; ++c) {
4809 // cursor follows cell:
4810 tmpcur.idx() = tabular.cellIndex(r, c);
4811 // select this cell only:
4814 tmpcur.resetAnchor();
4815 tmpcur.pit() = tmpcur.lastpit();
4816 tmpcur.pos() = tmpcur.top().lastpos();
4817 tmpcur.setCursor(tmpcur);
4818 tmpcur.setSelection();
4819 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4824 cell(cur.idx())->dispatch(cur, cmd);
4828 case LFUN_INSET_SETTINGS:
4829 // relay this lfun to Inset, not to the cell.
4830 Inset::doDispatch(cur, cmd);
4834 // we try to handle this event in the insets dispatch function.
4835 cell(cur.idx())->dispatch(cur, cmd);
4841 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4842 string const & argument, FuncStatus & status) const
4845 int action = Tabular::LAST_ACTION;
4847 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4848 if (tabularFeature[i].feature == s) {
4849 action = tabularFeature[i].action;
4853 if (action == Tabular::LAST_ACTION) {
4855 status.setUnknown(true);
4859 row_type sel_row_start = 0;
4860 row_type sel_row_end = 0;
4861 col_type sel_col_start = 0;
4862 col_type sel_col_end = 0;
4863 Tabular::ltType dummyltt;
4866 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4869 case Tabular::SET_PWIDTH:
4870 case Tabular::SET_MPWIDTH:
4871 case Tabular::TOGGLE_VARWIDTH_COLUMN:
4872 case Tabular::SET_SPECIAL_COLUMN:
4873 case Tabular::SET_SPECIAL_MULTICOLUMN:
4874 case Tabular::APPEND_ROW:
4875 case Tabular::APPEND_COLUMN:
4876 case Tabular::DELETE_ROW:
4877 case Tabular::DELETE_COLUMN:
4878 case Tabular::COPY_ROW:
4879 case Tabular::COPY_COLUMN:
4880 case Tabular::SET_TOP_SPACE:
4881 case Tabular::SET_BOTTOM_SPACE:
4882 case Tabular::SET_INTERLINE_SPACE:
4886 case Tabular::SET_TABULAR_WIDTH:
4887 status.setEnabled(!tabular.rotate
4888 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4891 case Tabular::MOVE_COLUMN_RIGHT:
4892 case Tabular::MOVE_COLUMN_LEFT:
4893 case Tabular::MOVE_ROW_DOWN:
4894 case Tabular::MOVE_ROW_UP: {
4895 if (cur.selection()) {
4896 status.message(_("Selections not supported."));
4897 status.setEnabled(false);
4901 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4902 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4903 (action == Tabular::MOVE_COLUMN_LEFT &&
4904 tabular.cellColumn(cur.idx()) == 0) ||
4905 (action == Tabular::MOVE_ROW_DOWN &&
4906 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4907 (action == Tabular::MOVE_ROW_UP &&
4908 tabular.cellRow(cur.idx()) == 0)) {
4909 status.setEnabled(false);
4913 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4914 action == Tabular::MOVE_COLUMN_LEFT) {
4915 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4916 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4917 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4918 status.message(_("Multi-column in current or"
4919 " destination column."));
4920 status.setEnabled(false);
4925 if (action == Tabular::MOVE_ROW_DOWN ||
4926 action == Tabular::MOVE_ROW_UP) {
4927 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4928 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4929 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4930 status.message(_("Multi-row in current or"
4931 " destination row."));
4932 status.setEnabled(false);
4937 status.setEnabled(true);
4941 case Tabular::SET_DECIMAL_POINT:
4943 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4946 case Tabular::SET_MULTICOLUMN:
4947 case Tabular::UNSET_MULTICOLUMN:
4948 case Tabular::MULTICOLUMN:
4949 // If a row is set as longtable caption, it must not be allowed
4950 // to unset that this row is a multicolumn.
4951 status.setEnabled(sel_row_start == sel_row_end
4952 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4953 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4956 case Tabular::SET_MULTIROW:
4957 case Tabular::UNSET_MULTIROW:
4958 case Tabular::MULTIROW:
4959 // If a row is set as longtable caption, it must not be allowed
4960 // to unset that this row is a multirow.
4961 status.setEnabled(sel_col_start == sel_col_end
4962 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4963 status.setOnOff(tabular.isMultiRow(cur.idx()));
4966 case Tabular::SET_ALL_LINES:
4967 case Tabular::UNSET_ALL_LINES:
4968 case Tabular::SET_BORDER_LINES:
4969 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4972 case Tabular::SET_LINE_TOP:
4973 case Tabular::SET_LINE_BOTTOM:
4974 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4977 case Tabular::SET_LINE_LEFT:
4978 case Tabular::SET_LINE_RIGHT:
4979 status.setEnabled(!tabular.use_booktabs
4980 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4983 case Tabular::TOGGLE_LINE_TOP:
4984 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4985 status.setOnOff(tabular.topLine(cur.idx()));
4988 case Tabular::TOGGLE_LINE_BOTTOM:
4989 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4990 status.setOnOff(tabular.bottomLine(cur.idx()));
4993 case Tabular::TOGGLE_LINE_LEFT:
4994 status.setEnabled(!tabular.use_booktabs
4995 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4996 status.setOnOff(tabular.leftLine(cur.idx()));
4999 case Tabular::TOGGLE_LINE_RIGHT:
5000 status.setEnabled(!tabular.use_booktabs
5001 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
5002 status.setOnOff(tabular.rightLine(cur.idx()));
5005 // multirow cells only inherit the alignment of the column if the column has
5006 // no width, otherwise they are left-aligned
5007 // therefore allow always left but right and center only if there is no width
5008 case Tabular::M_ALIGN_LEFT:
5011 case Tabular::ALIGN_LEFT:
5012 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
5015 case Tabular::M_ALIGN_RIGHT:
5018 case Tabular::ALIGN_RIGHT:
5019 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5020 && !tabular.getPWidth(cur.idx()).zero()));
5021 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
5024 case Tabular::M_ALIGN_CENTER:
5027 case Tabular::ALIGN_CENTER:
5028 status.setEnabled(!(tabular.isMultiRow(cur.idx())
5029 && !tabular.getPWidth(cur.idx()).zero()));
5030 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
5033 case Tabular::ALIGN_BLOCK:
5034 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5035 && !tabular.isMultiRow(cur.idx()));
5036 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
5039 case Tabular::ALIGN_DECIMAL:
5040 status.setEnabled(!tabular.isMultiRow(cur.idx())
5041 && !tabular.isMultiColumn(cur.idx()));
5042 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
5045 case Tabular::M_VALIGN_TOP:
5048 case Tabular::VALIGN_TOP:
5049 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5050 && !tabular.isMultiRow(cur.idx()));
5052 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
5055 case Tabular::M_VALIGN_BOTTOM:
5058 case Tabular::VALIGN_BOTTOM:
5059 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5060 && !tabular.isMultiRow(cur.idx()));
5062 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
5065 case Tabular::M_VALIGN_MIDDLE:
5068 case Tabular::VALIGN_MIDDLE:
5069 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
5070 && !tabular.isMultiRow(cur.idx()));
5072 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
5075 case Tabular::SET_LONGTABULAR:
5076 case Tabular::TOGGLE_LONGTABULAR:
5077 // setting as longtable is not allowed when table is inside a float
5078 if (cur.innerInsetOfType(FLOAT_CODE) != 0
5079 || cur.innerInsetOfType(WRAP_CODE) != 0)
5080 status.setEnabled(false);
5082 status.setEnabled(true);
5083 status.setOnOff(tabular.is_long_tabular);
5086 case Tabular::UNSET_LONGTABULAR:
5087 status.setOnOff(!tabular.is_long_tabular);
5090 case Tabular::TOGGLE_ROTATE_TABULAR:
5091 case Tabular::SET_ROTATE_TABULAR:
5092 status.setOnOff(tabular.rotate != 0);
5095 case Tabular::TABULAR_VALIGN_TOP:
5096 status.setEnabled(tabular.tabular_width.zero());
5097 status.setOnOff(tabular.tabular_valignment
5098 == Tabular::LYX_VALIGN_TOP);
5100 case Tabular::TABULAR_VALIGN_MIDDLE:
5101 status.setEnabled(tabular.tabular_width.zero());
5102 status.setOnOff(tabular.tabular_valignment
5103 == Tabular::LYX_VALIGN_MIDDLE);
5105 case Tabular::TABULAR_VALIGN_BOTTOM:
5106 status.setEnabled(tabular.tabular_width.zero());
5107 status.setOnOff(tabular.tabular_valignment
5108 == Tabular::LYX_VALIGN_BOTTOM);
5111 case Tabular::LONGTABULAR_ALIGN_LEFT:
5112 status.setOnOff(tabular.longtabular_alignment
5113 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
5115 case Tabular::LONGTABULAR_ALIGN_CENTER:
5116 status.setOnOff(tabular.longtabular_alignment
5117 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
5119 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5120 status.setOnOff(tabular.longtabular_alignment
5121 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
5124 case Tabular::UNSET_ROTATE_TABULAR:
5125 status.setOnOff(tabular.rotate == 0);
5128 case Tabular::TOGGLE_ROTATE_CELL:
5129 case Tabular::SET_ROTATE_CELL:
5130 status.setOnOff(!oneCellHasRotationState(false,
5131 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5134 case Tabular::UNSET_ROTATE_CELL:
5135 status.setOnOff(!oneCellHasRotationState(true,
5136 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
5139 case Tabular::SET_USEBOX:
5140 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
5143 // every row can only be one thing:
5144 // either a footer or header
5145 case Tabular::SET_LTFIRSTHEAD:
5146 status.setEnabled(sel_row_start == sel_row_end);
5147 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5150 case Tabular::UNSET_LTFIRSTHEAD:
5151 status.setEnabled(sel_row_start == sel_row_end);
5152 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
5155 case Tabular::SET_LTHEAD:
5156 status.setEnabled(sel_row_start == sel_row_end);
5157 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
5160 case Tabular::UNSET_LTHEAD:
5161 status.setEnabled(sel_row_start == sel_row_end);
5162 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
5165 case Tabular::SET_LTFOOT:
5166 status.setEnabled(sel_row_start == sel_row_end);
5167 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5170 case Tabular::UNSET_LTFOOT:
5171 status.setEnabled(sel_row_start == sel_row_end);
5172 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
5175 case Tabular::SET_LTLASTFOOT:
5176 status.setEnabled(sel_row_start == sel_row_end);
5177 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5180 case Tabular::UNSET_LTLASTFOOT:
5181 status.setEnabled(sel_row_start == sel_row_end);
5182 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
5185 case Tabular::SET_LTNEWPAGE:
5186 status.setOnOff(tabular.getLTNewPage(sel_row_start));
5188 case Tabular::UNSET_LTNEWPAGE:
5189 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
5192 // only one row in head/firsthead/foot/lasthead can be the caption
5193 // and a multirow cannot be set as caption
5194 case Tabular::SET_LTCAPTION:
5195 status.setEnabled(sel_row_start == sel_row_end
5196 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5197 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5198 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5199 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5200 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5201 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5202 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5203 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
5204 && !tabular.isMultiRow(sel_row_start));
5205 status.setOnOff(tabular.ltCaption(sel_row_start));
5208 case Tabular::UNSET_LTCAPTION:
5209 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
5212 case Tabular::TOGGLE_LTCAPTION:
5213 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
5214 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
5215 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
5216 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
5217 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
5218 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
5219 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
5220 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
5221 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
5222 status.setOnOff(tabular.ltCaption(sel_row_start));
5225 case Tabular::TOGGLE_BOOKTABS:
5226 case Tabular::SET_BOOKTABS:
5227 status.setOnOff(tabular.use_booktabs);
5230 case Tabular::UNSET_BOOKTABS:
5231 status.setOnOff(!tabular.use_booktabs);
5236 status.setEnabled(false);
5243 // function sets an object as defined in FuncStatus.h:
5244 // states OK, Unknown, Disabled, On, Off.
5245 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
5246 FuncStatus & status) const
5248 switch (cmd.action()) {
5249 case LFUN_INSET_MODIFY:
5250 if (cmd.getArg(0) != "tabular")
5252 if (cmd.getArg(1) == "for-dialog") {
5253 // The dialog is asking the status of a command
5254 if (&cur.inset() != this)
5256 string action = cmd.getArg(2);
5257 string arg = cmd.getLongArg(3);
5258 return getFeatureStatus(cur, action, arg, status);
5260 // We always enable the lfun if it is coming from the dialog
5261 // because the dialog makes sure all the settings are valid,
5262 // even though the first argument might not be valid now.
5263 status.setEnabled(true);
5267 case LFUN_TABULAR_FEATURE: {
5268 if (&cur.inset() != this)
5270 string action = cmd.getArg(0);
5271 string arg = cmd.getLongArg(1);
5272 return getFeatureStatus(cur, action, arg, status);
5275 case LFUN_CAPTION_INSERT: {
5276 // caption is only allowed in caption cell of longtable
5277 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
5278 status.setEnabled(false);
5281 // only standard caption is allowed
5282 string arg = cmd.getArg(0);
5283 if (!arg.empty() && arg != "Standard") {
5284 status.setEnabled(false);
5287 // check if there is already a caption
5288 bool have_caption = false;
5289 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
5290 ParagraphList::const_iterator pit = itc.paragraphs().begin();
5291 ParagraphList::const_iterator pend = itc.paragraphs().end();
5292 for (; pit != pend; ++pit) {
5293 InsetList::const_iterator it = pit->insetList().begin();
5294 InsetList::const_iterator end = pit->insetList().end();
5295 for (; it != end; ++it) {
5296 if (it->inset->lyxCode() == CAPTION_CODE) {
5297 have_caption = true;
5302 status.setEnabled(!have_caption);
5306 // These are only enabled inside tabular
5307 case LFUN_CELL_BACKWARD:
5308 case LFUN_CELL_FORWARD:
5309 status.setEnabled(true);
5312 // disable these with multiple cells selected
5313 case LFUN_INSET_INSERT:
5314 case LFUN_TABULAR_INSERT:
5315 case LFUN_FLEX_INSERT:
5316 case LFUN_FLOAT_INSERT:
5317 case LFUN_FLOAT_WIDE_INSERT:
5318 case LFUN_FOOTNOTE_INSERT:
5319 case LFUN_MARGINALNOTE_INSERT:
5320 case LFUN_MATH_INSERT:
5321 case LFUN_MATH_MODE:
5322 case LFUN_MATH_MUTATE:
5323 case LFUN_MATH_DISPLAY:
5324 case LFUN_NOTE_INSERT:
5325 case LFUN_ARGUMENT_INSERT:
5326 case LFUN_BOX_INSERT:
5327 case LFUN_BRANCH_INSERT:
5328 case LFUN_PHANTOM_INSERT:
5329 case LFUN_WRAP_INSERT:
5330 case LFUN_PREVIEW_INSERT:
5331 case LFUN_ERT_INSERT: {
5332 if (cur.selIsMultiCell()) {
5333 status.setEnabled(false);
5336 return cell(cur.idx())->getStatus(cur, cmd, status);
5339 // disable in non-fixed-width cells
5340 case LFUN_PARAGRAPH_BREAK:
5341 // multirow does not allow paragraph breaks
5342 if (tabular.isMultiRow(cur.idx())) {
5343 status.setEnabled(false);
5347 case LFUN_NEWLINE_INSERT:
5348 if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
5349 && tabular.getPWidth(cur.idx()).zero()) {
5350 status.setEnabled(false);
5353 return cell(cur.idx())->getStatus(cur, cmd, status);
5355 case LFUN_NEWPAGE_INSERT:
5356 status.setEnabled(false);
5360 if (tabularStackDirty() && theClipboard().isInternal()) {
5361 if (cur.selIsMultiCell()) {
5364 getSelection(cur, rs, re, cs, ce);
5365 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5366 && paste_tabular->nrows() == re - rs + 1)
5367 status.setEnabled(true);
5369 status.setEnabled(false);
5370 status.message(_("Selection size should match clipboard content."));
5373 status.setEnabled(true);
5376 return cell(cur.idx())->getStatus(cur, cmd, status);
5378 case LFUN_INSET_SETTINGS:
5379 // relay this lfun to Inset, not to the cell.
5380 return Inset::getStatus(cur, cmd, status);
5383 // we try to handle this event in the insets dispatch function.
5384 return cell(cur.idx())->getStatus(cur, cmd, status);
5390 Inset::DisplayType InsetTabular::display() const
5392 if (tabular.is_long_tabular) {
5393 switch (tabular.longtabular_alignment) {
5394 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5396 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5398 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5408 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5410 tabular.latex(os, runparams);
5414 int InsetTabular::plaintext(odocstringstream & os,
5415 OutputParams const & runparams, size_t max_length) const
5417 os << '\n'; // output table on a new line
5418 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5419 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5420 return PLAINTEXT_NEWLINE;
5424 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5429 // FIXME: Why not pass a proper DocIterator here?
5431 // if the table is inside a float it doesn't need the informaltable
5432 // wrapper. Search for it.
5433 for (master = owner(); master; master = master->owner())
5434 if (master->lyxCode() == FLOAT_CODE)
5439 os << "<informaltable>";
5442 ret += tabular.docbook(os, runparams);
5444 os << "</informaltable>";
5451 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5453 return tabular.xhtml(xs, rp);
5457 void InsetTabular::validate(LaTeXFeatures & features) const
5459 tabular.validate(features);
5460 features.useInsetLayout(getLayout());
5464 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5466 return tabular.cellInset(idx);
5470 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5472 return tabular.cellInset(idx);
5476 void InsetTabular::cursorPos(BufferView const & bv,
5477 CursorSlice const & sl, bool boundary, int & x, int & y) const
5479 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5481 // y offset correction
5482 y += cellYPos(sl.idx());
5483 y += tabular.textVOffset(sl.idx());
5484 y += tabular.offsetVAlignment();
5486 // x offset correction
5487 x += cellXPos(sl.idx());
5488 x += tabular.textHOffset(sl.idx());
5489 x += ADD_TO_TABULAR_WIDTH;
5493 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5497 Inset const & inset = *tabular.cellInset(cell);
5498 Point o = bv.coordCache().getInsets().xy(&inset);
5499 int const xbeg = o.x_ - tabular.textHOffset(cell);
5500 int const xend = xbeg + tabular.cellWidth(cell);
5501 row_type const row = tabular.cellRow(cell);
5502 int const ybeg = o.y_ - tabular.rowAscent(row)
5503 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5504 int const yend = ybeg + tabular.cellHeight(cell);
5516 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5517 // << " ybeg=" << ybeg << " yend=" << yend
5518 // << " xx=" << xx << " yy=" << yy
5519 // << " dist=" << xx + yy << endl;
5524 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5526 //lyxerr << "InsetTabular::editXY: " << this << endl;
5528 cur.idx() = getNearestCell(cur.bv(), x, y);
5529 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5533 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5535 cur.idx() = getNearestCell(cur.bv(), x, y);
5536 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5540 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5542 idx_type idx_min = 0;
5543 int dist_min = numeric_limits<int>::max();
5544 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5545 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5546 int const d = dist(bv, i, x, y);
5557 int InsetTabular::cellYPos(idx_type const cell) const
5559 row_type row = tabular.cellRow(cell);
5561 for (row_type r = 0; r < row; ++r)
5562 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5563 + tabular.interRowSpace(r + 1);
5568 int InsetTabular::cellXPos(idx_type const cell) const
5570 col_type col = tabular.cellColumn(cell);
5572 for (col_type c = 0; c < col; ++c)
5573 lx += tabular.column_info[c].width;
5578 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5580 row_type const row = tabular.cellRow(cur.idx());
5581 col_type const col = tabular.cellColumn(cur.idx());
5583 if (isRightToLeft(cur)) {
5584 if (tabular.cellColumn(cur.idx()) == 0) {
5585 if (row == tabular.nrows() - 1)
5587 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5592 cur.idx() = tabular.getLastCellInRow(row - 1);
5594 cur.idx() = tabular.cellIndex(row, col - 1);
5597 if (tabular.isLastCell(cur.idx()))
5599 if (cur.idx() == tabular.getLastCellInRow(row))
5600 cur.idx() = tabular.cellIndex(row + 1, 0);
5602 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5603 cur.idx() = tabular.cellIndex(row, colnextcell);
5607 cur.boundary(false);
5609 if (cur.selIsMultiCell()) {
5610 cur.pit() = cur.lastpit();
5611 cur.pos() = cur.lastpos();
5618 // in visual mode, place cursor at extreme left or right
5620 switch(entry_from) {
5622 case ENTRY_DIRECTION_RIGHT:
5623 cur.posVisToRowExtremity(false /* !left */);
5625 case ENTRY_DIRECTION_LEFT:
5626 cur.posVisToRowExtremity(true /* left */);
5628 case ENTRY_DIRECTION_IGNORE:
5629 // nothing to do in this case
5633 cur.setCurrentFont();
5637 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5639 row_type const row = tabular.cellRow(cur.idx());
5640 col_type const col = tabular.cellColumn(cur.idx());
5642 if (isRightToLeft(cur)) {
5643 if (cur.idx() == tabular.getLastCellInRow(row)) {
5646 cur.idx() = tabular.getFirstCellInRow(row);
5647 cur.idx() = tabular.cellAbove(cur.idx());
5649 if (tabular.isLastCell(cur.idx()))
5651 if (cur.idx() == tabular.getLastCellInRow(row))
5652 cur.idx() = tabular.cellIndex(row + 1, 0);
5654 cur.idx() = tabular.cellIndex(row, col + 1);
5657 if (cur.idx() == 0) // first cell
5660 cur.idx() = tabular.getLastCellInRow(row - 1);
5662 cur.idx() = tabular.cellIndex(row, col - 1);
5665 if (cur.selIsMultiCell()) {
5666 cur.pit() = cur.lastpit();
5667 cur.pos() = cur.lastpos();
5671 cur.pit() = cur.lastpit();
5672 cur.pos() = cur.lastpos();
5674 // in visual mode, place cursor at extreme left or right
5676 switch(entry_from) {
5678 case ENTRY_DIRECTION_RIGHT:
5679 cur.posVisToRowExtremity(false /* !left */);
5681 case ENTRY_DIRECTION_LEFT:
5682 cur.posVisToRowExtremity(true /* left */);
5684 case ENTRY_DIRECTION_IGNORE:
5685 // nothing to do in this case
5689 cur.setCurrentFont();
5693 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5695 cur.recordUndoInset(this);
5697 istringstream is(argument);
5698 // limit the size of strings we read to avoid memory problems
5702 size_t safe_guard = 0;
5707 if (safe_guard > 1000) {
5708 LYXERR0("parameter max count reached!");
5712 Tabular::Feature action = Tabular::LAST_ACTION;
5715 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5716 if (s != tabularFeature[i].feature)
5718 action = tabularFeature[i].action;
5721 if (action == Tabular::LAST_ACTION) {
5722 LYXERR0("Feature not found " << s);
5726 if (tabularFeature[i].need_value)
5728 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5729 tabularFeatures(cur, action, val);
5734 static void checkLongtableSpecial(Tabular::ltType & ltt,
5735 string const & special, bool & flag)
5737 if (special == "dl_above") {
5740 } else if (special == "dl_below") {
5741 ltt.bottomDL = flag;
5743 } else if (special == "empty") {
5753 bool InsetTabular::oneCellHasRotationState(bool rotated,
5754 row_type row_start, row_type row_end,
5755 col_type col_start, col_type col_end) const
5757 for (row_type r = row_start; r <= row_end; ++r)
5758 for (col_type c = col_start; c <= col_end; ++c)
5760 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5763 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5770 void InsetTabular::tabularFeatures(Cursor & cur,
5771 Tabular::Feature feature, string const & value)
5773 col_type sel_col_start;
5774 col_type sel_col_end;
5775 row_type sel_row_start;
5776 row_type sel_row_end;
5777 bool setLines = false;
5778 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5779 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5783 case Tabular::M_ALIGN_LEFT:
5784 case Tabular::ALIGN_LEFT:
5785 setAlign = LYX_ALIGN_LEFT;
5788 case Tabular::M_ALIGN_RIGHT:
5789 case Tabular::ALIGN_RIGHT:
5790 setAlign = LYX_ALIGN_RIGHT;
5793 case Tabular::M_ALIGN_CENTER:
5794 case Tabular::ALIGN_CENTER:
5795 setAlign = LYX_ALIGN_CENTER;
5798 case Tabular::ALIGN_BLOCK:
5799 setAlign = LYX_ALIGN_BLOCK;
5802 case Tabular::ALIGN_DECIMAL:
5803 setAlign = LYX_ALIGN_DECIMAL;
5806 case Tabular::M_VALIGN_TOP:
5807 case Tabular::VALIGN_TOP:
5808 setVAlign = Tabular::LYX_VALIGN_TOP;
5811 case Tabular::M_VALIGN_BOTTOM:
5812 case Tabular::VALIGN_BOTTOM:
5813 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5816 case Tabular::M_VALIGN_MIDDLE:
5817 case Tabular::VALIGN_MIDDLE:
5818 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5825 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5826 row_type const row = tabular.cellRow(cur.idx());
5827 col_type const column = tabular.cellColumn(cur.idx());
5829 Tabular::ltType ltt;
5833 case Tabular::SET_TABULAR_WIDTH:
5834 tabular.setTabularWidth(Length(value));
5837 case Tabular::SET_PWIDTH: {
5838 Length const len(value);
5839 tabular.setColumnPWidth(cur, cur.idx(), len);
5841 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5842 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5846 case Tabular::SET_MPWIDTH:
5847 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5850 case Tabular::TOGGLE_VARWIDTH_COLUMN: {
5851 bool const varwidth = value == "on";
5852 tabular.toggleVarwidth(cur.idx(), varwidth);
5856 case Tabular::SET_MROFFSET:
5857 tabular.setMROffset(cur, cur.idx(), Length(value));
5860 case Tabular::SET_SPECIAL_COLUMN:
5861 case Tabular::SET_SPECIAL_MULTICOLUMN:
5862 if (value == "none")
5863 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5865 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5868 case Tabular::APPEND_ROW:
5869 // append the row into the tabular
5870 tabular.appendRow(row);
5873 case Tabular::APPEND_COLUMN:
5874 // append the column into the tabular
5875 tabular.appendColumn(column);
5876 cur.idx() = tabular.cellIndex(row, column);
5879 case Tabular::DELETE_ROW:
5880 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5881 for (col_type c = 0; c < tabular.ncols(); c++)
5882 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5883 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5886 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5887 tabular.deleteRow(sel_row_start);
5888 if (sel_row_start >= tabular.nrows())
5890 cur.idx() = tabular.cellIndex(sel_row_start, column);
5893 cur.selection(false);
5896 case Tabular::DELETE_COLUMN:
5897 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5898 for (row_type r = 0; r < tabular.nrows(); r++)
5899 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5900 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5903 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5904 for (row_type r = 0; r < tabular.nrows(); r++)
5905 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5906 tabular.leftLine(tabular.cellIndex(r, 0)));
5909 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5910 tabular.deleteColumn(sel_col_start);
5911 if (sel_col_start >= tabular.ncols())
5913 cur.idx() = tabular.cellIndex(row, sel_col_start);
5916 cur.selection(false);
5919 case Tabular::COPY_ROW:
5920 tabular.copyRow(row);
5923 case Tabular::COPY_COLUMN:
5924 tabular.copyColumn(column);
5925 cur.idx() = tabular.cellIndex(row, column);
5928 case Tabular::MOVE_COLUMN_RIGHT:
5929 tabular.moveColumn(column, Tabular::RIGHT);
5930 cur.idx() = tabular.cellIndex(row, column + 1);
5933 case Tabular::MOVE_COLUMN_LEFT:
5934 tabular.moveColumn(column, Tabular::LEFT);
5935 cur.idx() = tabular.cellIndex(row, column - 1);
5938 case Tabular::MOVE_ROW_DOWN:
5939 tabular.moveRow(row, Tabular::DOWN);
5940 cur.idx() = tabular.cellIndex(row + 1, column);
5943 case Tabular::MOVE_ROW_UP:
5944 tabular.moveRow(row, Tabular::UP);
5945 cur.idx() = tabular.cellIndex(row - 1, column);
5948 case Tabular::SET_LINE_TOP:
5949 case Tabular::TOGGLE_LINE_TOP: {
5950 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5951 ? (value == "true") : !tabular.topLine(cur.idx());
5952 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5953 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5954 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5958 case Tabular::SET_LINE_BOTTOM:
5959 case Tabular::TOGGLE_LINE_BOTTOM: {
5960 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5961 ? (value == "true") : !tabular.bottomLine(cur.idx());
5962 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5963 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5964 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5968 case Tabular::SET_LINE_LEFT:
5969 case Tabular::TOGGLE_LINE_LEFT: {
5970 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5971 ? (value == "true") : !tabular.leftLine(cur.idx());
5972 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5973 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5974 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5978 case Tabular::SET_LINE_RIGHT:
5979 case Tabular::TOGGLE_LINE_RIGHT: {
5980 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5981 ? (value == "true") : !tabular.rightLine(cur.idx());
5982 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5983 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5984 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5988 case Tabular::M_ALIGN_LEFT:
5989 case Tabular::M_ALIGN_RIGHT:
5990 case Tabular::M_ALIGN_CENTER:
5991 case Tabular::ALIGN_LEFT:
5992 case Tabular::ALIGN_RIGHT:
5993 case Tabular::ALIGN_CENTER:
5994 case Tabular::ALIGN_BLOCK:
5995 case Tabular::ALIGN_DECIMAL:
5996 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5997 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5998 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5999 !tabular.getPWidth(c).zero());
6002 case Tabular::M_VALIGN_TOP:
6003 case Tabular::M_VALIGN_BOTTOM:
6004 case Tabular::M_VALIGN_MIDDLE:
6007 case Tabular::VALIGN_TOP:
6008 case Tabular::VALIGN_BOTTOM:
6009 case Tabular::VALIGN_MIDDLE:
6010 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6011 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6012 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
6015 case Tabular::SET_MULTICOLUMN: {
6016 if (!cur.selection()) {
6017 // just multicol for one single cell
6018 // check whether we are completely in a multicol
6019 if (!tabular.isMultiColumn(cur.idx()))
6020 tabular.setMultiColumn(cur, cur.idx(), 1,
6021 tabular.rightLine(cur.idx()));
6024 // we have a selection so this means we just add all these
6025 // cells to form a multicolumn cell
6026 idx_type const s_start = cur.selBegin().idx();
6027 row_type const col_start = tabular.cellColumn(s_start);
6028 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
6029 cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
6030 tabular.rightLine(cur.selEnd().idx()));
6033 cur.selection(false);
6037 case Tabular::UNSET_MULTICOLUMN: {
6038 if (!cur.selection()) {
6039 if (tabular.isMultiColumn(cur.idx()))
6040 tabular.unsetMultiColumn(cur.idx());
6045 case Tabular::MULTICOLUMN: {
6046 if (!cur.selection()) {
6047 if (tabular.isMultiColumn(cur.idx()))
6048 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6050 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6054 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6055 row_type const r = sel_row_start;
6056 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
6057 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
6060 // If the selection contains at least one singlecol cell
6061 // or multiple multicol cells,
6062 // we assume the user will merge is to a single multicol
6064 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
6066 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
6070 case Tabular::SET_MULTIROW: {
6071 if (!cur.selection()) {
6072 // just multirow for one single cell
6073 // check whether we are completely in a multirow
6074 if (!tabular.isMultiRow(cur.idx()))
6075 tabular.setMultiRow(cur, cur.idx(), 1,
6076 tabular.bottomLine(cur.idx()),
6077 tabular.getAlignment(cur.idx()));
6080 // we have a selection so this means we just add all this
6081 // cells to form a multirow cell
6082 idx_type const s_start = cur.selBegin().idx();
6083 row_type const row_start = tabular.cellRow(s_start);
6084 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
6085 cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
6086 tabular.bottomLine(cur.selEnd().idx()),
6087 tabular.getAlignment(cur.selEnd().idx()));
6090 cur.selection(false);
6094 case Tabular::UNSET_MULTIROW: {
6095 if (!cur.selection()) {
6096 if (tabular.isMultiRow(cur.idx()))
6097 tabular.unsetMultiRow(cur.idx());
6102 case Tabular::MULTIROW: {
6103 if (!cur.selection()) {
6104 if (tabular.isMultiRow(cur.idx()))
6105 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6107 tabularFeatures(cur, Tabular::SET_MULTIROW);
6111 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6112 col_type const c = sel_col_start;
6113 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
6114 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
6117 // If the selection contains at least one singlerow cell
6118 // or multiple multirow cells,
6119 // we assume the user will merge is to a single multirow
6121 tabularFeatures(cur, Tabular::SET_MULTIROW);
6123 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
6127 case Tabular::SET_ALL_LINES:
6130 case Tabular::UNSET_ALL_LINES:
6131 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6132 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6133 idx_type const cell = tabular.cellIndex(r, c);
6134 tabular.setTopLine(cell, setLines);
6135 tabular.setBottomLine(cell, setLines);
6136 tabular.setRightLine(cell, setLines);
6137 tabular.setLeftLine(cell, setLines);
6141 case Tabular::SET_BORDER_LINES:
6142 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6143 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
6144 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
6146 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6147 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
6148 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
6152 case Tabular::TOGGLE_LONGTABULAR:
6153 if (tabular.is_long_tabular)
6154 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
6156 tabular.is_long_tabular = true;
6159 case Tabular::SET_LONGTABULAR:
6160 tabular.is_long_tabular = true;
6163 case Tabular::UNSET_LONGTABULAR:
6164 for (row_type r = 0; r < tabular.nrows(); ++r) {
6165 if (tabular.ltCaption(r)) {
6166 cur.idx() = tabular.cellIndex(r, 0);
6169 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
6172 tabular.is_long_tabular = false;
6175 case Tabular::SET_ROTATE_TABULAR:
6176 tabular.rotate = convert<int>(value);
6179 case Tabular::UNSET_ROTATE_TABULAR:
6183 case Tabular::TOGGLE_ROTATE_TABULAR:
6184 // when pressing the rotate button we default to 90° rotation
6185 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
6188 case Tabular::TABULAR_VALIGN_TOP:
6189 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
6192 case Tabular::TABULAR_VALIGN_MIDDLE:
6193 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
6196 case Tabular::TABULAR_VALIGN_BOTTOM:
6197 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
6200 case Tabular::LONGTABULAR_ALIGN_LEFT:
6201 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
6204 case Tabular::LONGTABULAR_ALIGN_CENTER:
6205 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
6208 case Tabular::LONGTABULAR_ALIGN_RIGHT:
6209 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
6214 case Tabular::SET_ROTATE_CELL:
6215 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6216 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6217 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
6220 case Tabular::UNSET_ROTATE_CELL:
6221 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6222 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6223 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6226 case Tabular::TOGGLE_ROTATE_CELL:
6228 bool oneNotRotated = oneCellHasRotationState(false,
6229 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
6231 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6232 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
6233 // when pressing the rotate cell button we default to 90° rotation
6235 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
6237 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
6242 case Tabular::SET_USEBOX: {
6243 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
6244 if (val == tabular.getUsebox(cur.idx()))
6245 val = Tabular::BOX_NONE;
6246 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6247 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6248 tabular.setUsebox(tabular.cellIndex(r, c), val);
6252 case Tabular::UNSET_LTFIRSTHEAD:
6255 case Tabular::SET_LTFIRSTHEAD:
6256 tabular.getRowOfLTFirstHead(row, ltt);
6257 checkLongtableSpecial(ltt, value, flag);
6258 tabular.setLTHead(row, flag, ltt, true);
6261 case Tabular::UNSET_LTHEAD:
6264 case Tabular::SET_LTHEAD:
6265 tabular.getRowOfLTHead(row, ltt);
6266 checkLongtableSpecial(ltt, value, flag);
6267 tabular.setLTHead(row, flag, ltt, false);
6270 case Tabular::UNSET_LTFOOT:
6273 case Tabular::SET_LTFOOT:
6274 tabular.getRowOfLTFoot(row, ltt);
6275 checkLongtableSpecial(ltt, value, flag);
6276 tabular.setLTFoot(row, flag, ltt, false);
6279 case Tabular::UNSET_LTLASTFOOT:
6282 case Tabular::SET_LTLASTFOOT:
6283 tabular.getRowOfLTLastFoot(row, ltt);
6284 checkLongtableSpecial(ltt, value, flag);
6285 tabular.setLTFoot(row, flag, ltt, true);
6288 case Tabular::UNSET_LTNEWPAGE:
6291 case Tabular::SET_LTNEWPAGE:
6292 tabular.setLTNewPage(row, flag);
6295 case Tabular::SET_LTCAPTION: {
6296 if (tabular.ltCaption(row))
6298 cur.idx() = tabular.setLTCaption(cur, row, true);
6301 cur.selection(false);
6302 // If a row is set as caption, then also insert
6303 // a caption. Otherwise the LaTeX output is broken.
6304 // Select cell if it is non-empty
6305 if (cur.lastpos() > 0 || cur.lastpit() > 0)
6306 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
6307 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
6311 case Tabular::UNSET_LTCAPTION: {
6312 if (!tabular.ltCaption(row))
6314 cur.idx() = tabular.setLTCaption(cur, row, false);
6317 cur.selection(false);
6318 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6319 if (lyx::getStatus(fr).enabled())
6324 case Tabular::TOGGLE_LTCAPTION: {
6325 if (tabular.ltCaption(row))
6326 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6328 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6332 case Tabular::TOGGLE_BOOKTABS:
6333 tabular.use_booktabs = !tabular.use_booktabs;
6336 case Tabular::SET_BOOKTABS:
6337 tabular.use_booktabs = true;
6340 case Tabular::UNSET_BOOKTABS:
6341 tabular.use_booktabs = false;
6344 case Tabular::SET_TOP_SPACE: {
6346 if (value == "default")
6347 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6348 tabular.row_info[r].top_space_default = true;
6349 else if (value == "none")
6350 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6351 tabular.row_info[r].top_space_default = false;
6352 tabular.row_info[r].top_space = len;
6354 else if (isValidLength(value, &len))
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;
6362 case Tabular::SET_BOTTOM_SPACE: {
6364 if (value == "default")
6365 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6366 tabular.row_info[r].bottom_space_default = true;
6367 else if (value == "none")
6368 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6369 tabular.row_info[r].bottom_space_default = false;
6370 tabular.row_info[r].bottom_space = len;
6372 else if (isValidLength(value, &len))
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;
6380 case Tabular::SET_INTERLINE_SPACE: {
6382 if (value == "default")
6383 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6384 tabular.row_info[r].interline_space_default = true;
6385 else if (value == "none")
6386 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6387 tabular.row_info[r].interline_space_default = false;
6388 tabular.row_info[r].interline_space = len;
6390 else if (isValidLength(value, &len))
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;
6398 case Tabular::SET_DECIMAL_POINT:
6399 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6400 tabular.column_info[c].decimal_point = from_utf8(value);
6403 // dummy stuff just to avoid warnings
6404 case Tabular::LAST_ACTION:
6410 bool InsetTabular::copySelection(Cursor & cur)
6412 if (!cur.selection())
6417 getSelection(cur, rs, re, cs, ce);
6419 paste_tabular.reset(new Tabular(tabular));
6421 for (row_type r = 0; r < rs; ++r)
6422 paste_tabular->deleteRow(0);
6424 row_type const rows = re - rs + 1;
6425 while (paste_tabular->nrows() > rows)
6426 paste_tabular->deleteRow(rows);
6428 for (col_type c = 0; c < cs; ++c)
6429 paste_tabular->deleteColumn(0);
6431 col_type const columns = ce - cs + 1;
6432 while (paste_tabular->ncols() > columns)
6433 paste_tabular->deleteColumn(columns);
6435 paste_tabular->setBuffer(tabular.buffer());
6437 odocstringstream os;
6438 OutputParams const runparams(0);
6439 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6440 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6441 cap::copySelection(cur, os.str());
6443 // mark tabular stack dirty
6444 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6445 // when we (hopefully) have a one-for-all paste mechanism.
6446 // This must be called after cap::copySelection.
6447 dirtyTabularStack(true);
6453 bool InsetTabular::pasteClipboard(Cursor & cur)
6457 col_type actcol = tabular.cellColumn(cur.idx());
6458 row_type actrow = tabular.cellRow(cur.idx());
6460 if (cur.selIsMultiCell()) {
6463 getSelection(cur, actrow, re, actcol, ce);
6466 for (row_type r1 = 0, r2 = actrow;
6467 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6469 for (col_type c1 = 0, c2 = actcol;
6470 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6472 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6473 tabular.isPartOfMultiColumn(r2, c2))
6475 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6479 if (tabular.isPartOfMultiColumn(r2, c2)) {
6483 shared_ptr<InsetTableCell> inset(
6484 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6485 tabular.setCellInset(r2, c2, inset);
6486 // FIXME?: why do we need to do this explicitly? (EL)
6487 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6489 // FIXME: change tracking (MG)
6490 inset->setChange(Change(buffer().params().track_changes ?
6491 Change::INSERTED : Change::UNCHANGED));
6500 void InsetTabular::cutSelection(Cursor & cur)
6502 if (!cur.selection())
6507 getSelection(cur, rs, re, cs, ce);
6508 for (row_type r = rs; r <= re; ++r) {
6509 for (col_type c = cs; c <= ce; ++c) {
6510 shared_ptr<InsetTableCell> t
6511 = cell(tabular.cellIndex(r, c));
6512 if (buffer().params().track_changes)
6513 // FIXME: Change tracking (MG)
6514 t->setChange(Change(Change::DELETED));
6520 // cursor position might be invalid now
6521 if (cur.pit() > cur.lastpit())
6522 cur.pit() = cur.lastpit();
6523 if (cur.pos() > cur.lastpos())
6524 cur.pos() = cur.lastpos();
6525 cur.clearSelection();
6529 bool InsetTabular::isRightToLeft(Cursor & cur) const
6531 // LASSERT: It might be better to abandon this Buffer.
6532 LASSERT(cur.depth() > 1, return false);
6533 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6534 pos_type const parentpos = cur[cur.depth() - 2].pos();
6535 return parentpar.getFontSettings(buffer().params(),
6536 parentpos).language()->rightToLeft();
6540 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6543 LASSERT(stidx <= enidx, return docstring());
6545 col_type const col1 = tabular.cellColumn(stidx);
6546 col_type const col2 = tabular.cellColumn(enidx);
6547 row_type const row1 = tabular.cellRow(stidx);
6548 row_type const row2 = tabular.cellRow(enidx);
6550 for (col_type col = col1; col <= col2; col++)
6551 for (row_type row = row1; row <= row2; row++) {
6556 retval += tabular.cellInset(row, col)->asString(intoInsets);
6562 void InsetTabular::getSelection(Cursor & cur,
6563 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6565 CursorSlice const & beg = cur.selBegin();
6566 CursorSlice const & end = cur.selEnd();
6567 cs = tabular.cellColumn(beg.idx());
6568 ce = tabular.cellColumn(end.idx());
6572 rs = tabular.cellRow(beg.idx());
6573 re = tabular.cellRow(end.idx());
6579 Text * InsetTabular::getText(int idx) const
6581 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6585 void InsetTabular::setChange(Change const & change)
6587 for (idx_type idx = 0; idx < nargs(); ++idx)
6588 cell(idx)->setChange(change);
6592 void InsetTabular::acceptChanges()
6594 for (idx_type idx = 0; idx < nargs(); ++idx)
6595 cell(idx)->acceptChanges();
6599 void InsetTabular::rejectChanges()
6601 for (idx_type idx = 0; idx < nargs(); ++idx)
6602 cell(idx)->rejectChanges();
6606 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6608 return tabular.getPWidth(cell).zero();
6612 bool InsetTabular::forcePlainLayout(idx_type cell) const
6614 return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
6618 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6621 if (buf.length() <= 0)
6626 col_type maxCols = 1;
6627 size_t const len = buf.length();
6631 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6639 maxCols = max(cols, maxCols);
6645 maxCols = max(cols, maxCols);
6651 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6652 loctab = paste_tabular.get();
6653 dirtyTabularStack(true);
6656 cell = bv.cursor().idx();
6657 ocol = tabular.cellColumn(cell);
6658 row = tabular.cellRow(cell);
6662 idx_type const cells = loctab->numberofcells;
6665 rows = loctab->nrows();
6666 col_type const columns = loctab->ncols();
6668 while (cell < cells && p < len && row < rows &&
6669 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6675 // we can only set this if we are not too far right
6676 if (cols < columns) {
6677 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6678 Font const font = bv.textMetrics(&inset->text()).
6680 inset->setText(buf.substr(op, p - op), font,
6681 buffer().params().track_changes);
6687 // we can only set this if we are not too far right
6688 if (cols < columns) {
6689 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6690 Font const font = bv.textMetrics(&inset->text()).
6692 inset->setText(buf.substr(op, p - op), font,
6693 buffer().params().track_changes);
6698 cell = loctab->cellIndex(row, cols);
6704 // check for the last cell if there is no trailing '\n'
6705 if (cell < cells && op < len) {
6706 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6707 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6708 inset->setText(buf.substr(op, len - op), font,
6709 buffer().params().track_changes);
6715 void InsetTabular::addPreview(DocIterator const & inset_pos,
6716 PreviewLoader & loader) const
6718 DocIterator cell_pos = inset_pos;
6720 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6721 for (row_type r = 0; r < tabular.nrows(); ++r) {
6722 for (col_type c = 0; c < tabular.ncols(); ++c) {
6723 cell_pos.top().idx() = tabular.cellIndex(r, c);
6724 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6730 bool InsetTabular::completionSupported(Cursor const & cur) const
6732 Cursor const & bvCur = cur.bv().cursor();
6733 if (&bvCur.inset() != this)
6735 return cur.text()->completionSupported(cur);
6739 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6741 return completionSupported(cur);
6745 bool InsetTabular::automaticInlineCompletion() const
6747 return lyxrc.completion_inline_text;
6751 bool InsetTabular::automaticPopupCompletion() const
6753 return lyxrc.completion_popup_text;
6757 bool InsetTabular::showCompletionCursor() const
6759 return lyxrc.completion_cursor_text;
6763 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6765 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6769 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6771 if (!completionSupported(cur))
6773 return cur.text()->completionPrefix(cur);
6777 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6779 if (!completionSupported(cur))
6782 return cur.text()->insertCompletion(cur, s, finished);
6786 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6787 Dimension & dim) const
6789 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6790 tm.completionPosAndDim(cur, x, y, dim);
6794 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6796 istringstream data(in);
6798 lex.setStream(data);
6805 if (!lex || token != "tabular") {
6806 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6810 // This is part of the inset proper that is usually swallowed
6811 // by Buffer::readInset
6813 if (!lex || token != "Tabular") {
6814 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6822 string InsetTabular::params2string(InsetTabular const & inset)
6825 data << "tabular" << ' ';
6827 data << "\\end_inset\n";
6832 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6834 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6835 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6836 if (!tabular.isPartOfMultiColumn(r,c) &&
6837 !tabular.isPartOfMultiRow(r,c))
6840 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6841 ParagraphList::iterator it = parlist.begin();
6842 ParagraphList::iterator const en = parlist.end();
6843 for (; it != en; ++it)
6844 it->setLayout(dc.plainLayout());