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::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
165 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
166 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
167 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
168 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
169 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
170 { Tabular::SET_USEBOX, "set-usebox", true },
171 { Tabular::SET_LTHEAD, "set-lthead", true },
172 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
173 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
174 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
175 { Tabular::SET_LTFOOT, "set-ltfoot", true },
176 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
177 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
178 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
179 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
180 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
181 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
182 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
183 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
184 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
185 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
186 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
187 { Tabular::SET_BOOKTABS, "set-booktabs", false },
188 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
189 { Tabular::SET_TOP_SPACE, "set-top-space", true },
190 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
191 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
192 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
193 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
194 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
195 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
196 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
197 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
198 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
199 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
200 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
201 { Tabular::LAST_ACTION, "", false }
205 string const tostr(LyXAlignment const & num)
210 case LYX_ALIGN_BLOCK:
214 case LYX_ALIGN_CENTER:
216 case LYX_ALIGN_RIGHT:
218 case LYX_ALIGN_LAYOUT:
220 case LYX_ALIGN_SPECIAL:
222 case LYX_ALIGN_DECIMAL:
229 string const tostr(Tabular::HAlignment const & num)
232 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
234 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
236 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
243 string const tostr(Tabular::VAlignment const & num)
246 case Tabular::LYX_VALIGN_TOP:
248 case Tabular::LYX_VALIGN_MIDDLE:
250 case Tabular::LYX_VALIGN_BOTTOM:
257 string const tostr(Tabular::BoxType const & num)
260 case Tabular::BOX_NONE:
262 case Tabular::BOX_PARBOX:
264 case Tabular::BOX_MINIPAGE:
271 // I would have liked a fromstr template a lot better. (Lgb)
272 bool string2type(string const & str, LyXAlignment & num)
275 num = LYX_ALIGN_NONE;
276 else if (str == "block")
277 num = LYX_ALIGN_BLOCK;
278 else if (str == "left")
279 num = LYX_ALIGN_LEFT;
280 else if (str == "center")
281 num = LYX_ALIGN_CENTER;
282 else if (str == "right")
283 num = LYX_ALIGN_RIGHT;
284 else if (str == "decimal")
285 num = LYX_ALIGN_DECIMAL;
292 bool string2type(string const & str, Tabular::HAlignment & num)
295 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
296 else if (str == "center" )
297 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
298 else if (str == "right")
299 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
306 bool string2type(string const & str, Tabular::VAlignment & num)
309 num = Tabular::LYX_VALIGN_TOP;
310 else if (str == "middle" )
311 num = Tabular::LYX_VALIGN_MIDDLE;
312 else if (str == "bottom")
313 num = Tabular::LYX_VALIGN_BOTTOM;
320 bool string2type(string const & str, Tabular::BoxType & num)
323 num = Tabular::BOX_NONE;
324 else if (str == "parbox")
325 num = Tabular::BOX_PARBOX;
326 else if (str == "minipage")
327 num = Tabular::BOX_MINIPAGE;
334 bool string2type(string const & str, bool & num)
338 else if (str == "false")
346 bool getTokenValue(string const & str, char const * token, string & ret)
349 size_t token_length = strlen(token);
350 size_t pos = str.find(token);
352 if (pos == string::npos || pos + token_length + 1 >= str.length()
353 || str[pos + token_length] != '=')
355 pos += token_length + 1;
357 if (ch != '"' && ch != '\'') { // only read till next space
361 while (pos < str.length() - 1 && str[++pos] != ch)
368 bool getTokenValue(string const & str, char const * token, docstring & ret)
371 bool const success = getTokenValue(str, token, tmp);
372 ret = from_utf8(tmp);
377 bool getTokenValue(string const & str, char const * token, int & num)
381 if (!getTokenValue(str, token, tmp))
383 num = convert<int>(tmp);
388 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
391 return getTokenValue(str, token, tmp) && string2type(tmp, num);
395 bool getTokenValue(string const & str, char const * token,
396 Tabular::HAlignment & num)
399 return getTokenValue(str, token, tmp) && string2type(tmp, num);
403 bool getTokenValue(string const & str, char const * token,
404 Tabular::VAlignment & num)
407 return getTokenValue(str, token, tmp) && string2type(tmp, num);
411 bool getTokenValue(string const & str, char const * token,
412 Tabular::BoxType & num)
415 return getTokenValue(str, token, tmp) && string2type(tmp, num);
419 bool getTokenValue(string const & str, char const * token, bool & flag)
421 // set the flag always to false as this should be the default for bools
422 // not in the file-format.
425 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
429 bool getTokenValue(string const & str, char const * token, Length & len)
431 // set the length to be zero() as default as this it should be if not
432 // in the file format.
435 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
439 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
444 if (!getTokenValue(str, token, tmp))
446 if (tmp == "default") {
450 return isValidLength(tmp, &len);
454 void l_getline(istream & is, string & str)
457 while (str.empty()) {
459 if (!str.empty() && str[str.length() - 1] == '\r')
460 str.erase(str.length() - 1);
465 string const write_attribute(string const & name, T const & t)
467 string const s = tostr(t);
468 return s.empty() ? s : " " + name + "=\"" + s + "\"";
472 string const write_attribute(string const & name, string const & t)
474 return t.empty() ? t : " " + name + "=\"" + t + "\"";
479 string const write_attribute(string const & name, docstring const & t)
481 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
486 string const write_attribute(string const & name, bool const & b)
488 // we write only true attribute values so we remove a bit of the
489 // file format bloat for tabulars.
490 return b ? write_attribute(name, convert<string>(b)) : string();
495 string const write_attribute(string const & name, int const & i)
497 // we write only true attribute values so we remove a bit of the
498 // file format bloat for tabulars.
499 return i ? write_attribute(name, convert<string>(i)) : string();
504 string const write_attribute(string const & name, Tabular::idx_type const & i)
506 // we write only true attribute values so we remove a bit of the
507 // file format bloat for tabulars.
508 return i ? write_attribute(name, convert<string>(i)) : string();
513 string const write_attribute(string const & name, Length const & value)
515 // we write only the value if we really have one same reson as above.
516 return value.zero() ? string() : write_attribute(name, value.asString());
522 string const featureAsString(Tabular::Feature action)
524 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
525 if (tabularFeature[i].action == action)
526 return tabularFeature[i].feature;
532 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
534 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
535 for (; dit; dit.forwardChar())
536 if (dit.inTexted() && dit.depth() == 1
537 && dit.paragraph().find(align_d, false, false, dit.pos()))
544 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
546 InsetTableCell tail = InsetTableCell(head);
547 DocIterator const dit = separatorPos(&head, align_d);
550 pit_type const psize = head.paragraphs().front().size();
551 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
552 tail.paragraphs().front().eraseChars(0,
553 dit.pos() < psize ? dit.pos() + 1 : psize, false);
560 /////////////////////////////////////////////////////////////////////
564 /////////////////////////////////////////////////////////////////////
567 Tabular::CellData::CellData(Buffer * buf)
570 multicolumn(Tabular::CELL_NORMAL),
571 multirow(Tabular::CELL_NORMAL),
572 alignment(LYX_ALIGN_CENTER),
573 valignment(LYX_VALIGN_TOP),
583 inset(new InsetTableCell(buf))
585 inset->setBuffer(*buf);
589 Tabular::CellData::CellData(CellData const & cs)
592 multicolumn(cs.multicolumn),
593 multirow(cs.multirow),
594 mroffset(cs.mroffset),
595 alignment(cs.alignment),
596 valignment(cs.valignment),
597 decimal_hoffset(cs.decimal_hoffset),
598 decimal_width(cs.decimal_width),
600 top_line(cs.top_line),
601 bottom_line(cs.bottom_line),
602 left_line(cs.left_line),
603 right_line(cs.right_line),
606 align_special(cs.align_special),
608 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
612 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
618 multicolumn = cs.multicolumn;
619 multirow = cs.multirow;
620 mroffset = cs.mroffset;
621 alignment = cs.alignment;
622 valignment = cs.valignment;
623 decimal_hoffset = cs.decimal_hoffset;
624 decimal_width = cs.decimal_width;
625 voffset = cs.voffset;
626 top_line = cs.top_line;
627 bottom_line = cs.bottom_line;
628 left_line = cs.left_line;
629 right_line = cs.right_line;
632 align_special = cs.align_special;
633 p_width = cs.p_width;
634 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
638 Tabular::RowData::RowData()
641 top_space_default(false),
642 bottom_space_default(false),
643 interline_space_default(false),
653 Tabular::ColumnData::ColumnData()
654 : alignment(LYX_ALIGN_CENTER),
655 valignment(LYX_VALIGN_TOP),
661 Tabular::ltType::ltType()
662 : set(false), topDL(false),
668 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
670 init(buffer, rows_arg, columns_arg);
674 void Tabular::setBuffer(Buffer & buffer)
677 for (row_type i = 0; i < nrows(); ++i)
678 for (col_type j = 0; j < ncols(); ++j)
679 cell_info[i][j].inset->setBuffer(*buffer_);
683 // activates all lines and sets all widths to 0
684 void Tabular::init(Buffer * buf, row_type rows_arg,
685 col_type columns_arg)
688 row_info = row_vector(rows_arg);
689 column_info = column_vector(columns_arg);
690 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
691 row_info.reserve(10);
692 column_info.reserve(10);
693 cell_info.reserve(100);
695 is_long_tabular = false;
696 tabular_valignment = LYX_VALIGN_MIDDLE;
697 tabular_width = Length();
698 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
700 use_booktabs = false;
701 // set silly default lines
702 for (row_type r = 0; r < nrows(); ++r)
703 for (col_type c = 0; c < ncols(); ++c) {
704 cell_info[r][c].inset->setBuffer(*buffer_);
705 cell_info[r][c].top_line = true;
706 cell_info[r][c].left_line = true;
707 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
708 cell_info[r][c].right_line = c == ncols() - 1;
713 void Tabular::deleteRow(row_type const row)
715 // Not allowed to delete last row
719 for (col_type c = 0; c < ncols(); ++c) {
720 // Care about multirow cells
721 if (row + 1 < nrows() &&
722 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
723 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
724 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
727 row_info.erase(row_info.begin() + row);
728 cell_info.erase(cell_info.begin() + row);
733 void Tabular::copyRow(row_type const row)
735 insertRow(row, true);
739 void Tabular::appendRow(row_type row)
741 insertRow(row, false);
745 void Tabular::insertRow(row_type const row, bool copy)
747 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
748 cell_info.insert(cell_info.begin() + row + 1,
749 cell_vector(0, CellData(buffer_)));
751 for (col_type c = 0; c < ncols(); ++c) {
752 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
753 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
754 if (buffer().params().track_changes)
755 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
756 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
757 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
761 for (col_type c = 0; c < ncols(); ++c) {
762 if (isPartOfMultiRow(row, c))
764 // inherit line settings
765 idx_type const i = cellIndex(row + 1, c);
766 idx_type const j = cellIndex(row, c);
767 setLeftLine(i, leftLine(j));
768 setRightLine(i, rightLine(j));
769 setTopLine(i, topLine(j));
770 if (topLine(j) && bottomLine(j)) {
771 setBottomLine(i, true);
772 setBottomLine(j, false);
774 // mark track changes
775 if (buffer().params().track_changes)
776 cellInfo(i).inset->setChange(Change(Change::INSERTED));
781 void Tabular::moveColumn(col_type col, ColDirection direction)
783 if (direction == Tabular::LEFT)
786 std::swap(column_info[col], column_info[col + 1]);
788 for (row_type r = 0; r < nrows(); ++r) {
789 std::swap(cell_info[r][col], cell_info[r][col + 1]);
790 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
791 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
793 // FIXME track changes is broken for tabular features (#8469)
794 idx_type const i = cellIndex(r, col);
795 idx_type const j = cellIndex(r, col + 1);
796 if (buffer().params().track_changes) {
797 cellInfo(i).inset->setChange(Change(Change::INSERTED));
798 cellInfo(j).inset->setChange(Change(Change::INSERTED));
805 void Tabular::moveRow(row_type row, RowDirection direction)
807 if (direction == Tabular::UP)
810 std::swap(row_info[row], row_info[row + 1]);
812 for (col_type c = 0; c < ncols(); ++c) {
813 std::swap(cell_info[row][c], cell_info[row + 1][c]);
814 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
815 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
817 // FIXME track changes is broken for tabular features (#8469)
818 idx_type const i = cellIndex(row, c);
819 idx_type const j = cellIndex(row + 1, c);
820 if (buffer().params().track_changes) {
821 cellInfo(i).inset->setChange(Change(Change::INSERTED));
822 cellInfo(j).inset->setChange(Change(Change::INSERTED));
829 void Tabular::deleteColumn(col_type const col)
831 // Not allowed to delete last column
835 for (row_type r = 0; r < nrows(); ++r) {
836 // Care about multicolumn cells
837 if (col + 1 < ncols() &&
838 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
839 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
840 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
842 cell_info[r].erase(cell_info[r].begin() + col);
844 column_info.erase(column_info.begin() + col);
849 void Tabular::copyColumn(col_type const col)
851 insertColumn(col, true);
855 void Tabular::appendColumn(col_type col)
857 insertColumn(col, false);
861 void Tabular::insertColumn(col_type const col, bool copy)
863 BufferParams const & bp = buffer().params();
864 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
866 for (row_type r = 0; r < nrows(); ++r) {
867 cell_info[r].insert(cell_info[r].begin() + col + 1,
868 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
869 if (bp.track_changes)
870 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
871 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
872 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
875 for (row_type r = 0; r < nrows(); ++r) {
876 // inherit line settings
877 idx_type const i = cellIndex(r, col + 1);
878 idx_type const j = cellIndex(r, col);
879 setBottomLine(i, bottomLine(j));
880 setTopLine(i, topLine(j));
881 setLeftLine(i, leftLine(j));
882 setRightLine(i, rightLine(j));
883 if (rightLine(i) && rightLine(j)) {
884 setRightLine(j, false);
886 if (buffer().params().track_changes)
887 cellInfo(i).inset->setChange(Change(Change::INSERTED));
892 void Tabular::updateIndexes()
897 for (row_type row = 0; row < nrows(); ++row)
898 for (col_type column = 0; column < ncols(); ++column) {
899 if (!isPartOfMultiColumn(row, column)
900 && !isPartOfMultiRow(row, column))
902 if (isPartOfMultiRow(row, column))
903 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
905 cell_info[row][column].cellno = numberofcells - 1;
908 rowofcell.resize(numberofcells);
909 columnofcell.resize(numberofcells);
911 // reset column and row of cells and update their width and alignment
912 for (row_type row = 0; row < nrows(); ++row)
913 for (col_type column = 0; column < ncols(); ++column) {
914 if (isPartOfMultiColumn(row, column))
916 // columnofcell needs to be called before setting width and aligment
917 // multirow cells inherit the width from the column width
918 if (!isPartOfMultiRow(row, column)) {
919 columnofcell[i] = column;
922 setFixedWidth(row, column);
923 if (isPartOfMultiRow(row, column))
925 cell_info[row][column].inset->setContentAlignment(
926 getAlignment(cellIndex(row, column)));
932 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
935 for (col_type c = 0; c < ncols(); ++c)
936 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
942 bool Tabular::topLine(idx_type const cell) const
944 return cellInfo(cell).top_line;
948 bool Tabular::bottomLine(idx_type const cell) const
950 return cellInfo(cell).bottom_line;
954 bool Tabular::leftLine(idx_type cell) const
958 return cellInfo(cell).left_line;
962 bool Tabular::rightLine(idx_type cell) const
966 return cellInfo(cell).right_line;
970 int Tabular::interRowSpace(row_type row) const
972 if (!row || row >= nrows())
975 int const interline_space = row_info[row - 1].interline_space_default ?
977 row_info[row - 1].interline_space.inPixels(width());
978 if (rowTopLine(row) && rowBottomLine(row - 1))
979 return interline_space + WIDTH_OF_LINE;
980 return interline_space;
984 int Tabular::interColumnSpace(idx_type cell) const
986 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
987 if (rightLine(cell) && nextcol < ncols()
988 && leftLine(cellIndex(cellRow(cell), nextcol)))
989 return WIDTH_OF_LINE;
994 int Tabular::cellWidth(idx_type cell) const
997 col_type const span = columnSpan(cell);
998 col_type const col = cellColumn(cell);
999 for(col_type c = col; c < col + span ; ++c)
1000 w += column_info[c].width;
1005 int Tabular::cellHeight(idx_type cell) const
1007 row_type const span = rowSpan(cell);
1008 row_type const row = cellRow(cell);
1010 for(row_type r = row; r < row + span ; ++r) {
1011 h += rowAscent(r) + rowDescent(r);
1012 if (r != row + span - 1)
1013 h += interRowSpace(r + 1);
1020 bool Tabular::updateColumnWidths()
1022 vector<int> max_dwidth(ncols(), 0);
1023 for(col_type c = 0; c < ncols(); ++c)
1024 for(row_type r = 0; r < nrows(); ++r) {
1025 idx_type const i = cellIndex(r, c);
1026 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1027 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1030 bool update = false;
1031 // for each col get max of single col cells
1032 for(col_type c = 0; c < ncols(); ++c) {
1034 for(row_type r = 0; r < nrows(); ++r) {
1035 idx_type const i = cellIndex(r, c);
1036 if (columnSpan(i) == 1) {
1037 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1038 && cell_info[r][c].decimal_width != 0)
1039 new_width = max(new_width, cellInfo(i).width
1040 + max_dwidth[c] - cellInfo(i).decimal_width);
1042 new_width = max(new_width, cellInfo(i).width);
1046 if (column_info[c].width != new_width) {
1047 column_info[c].width = new_width;
1051 // update col widths to fit merged cells
1052 for(col_type c = 0; c < ncols(); ++c)
1053 for(row_type r = 0; r < nrows(); ++r) {
1054 idx_type const i = cellIndex(r, c);
1055 int const span = columnSpan(i);
1056 if (span == 1 || c > cellColumn(i))
1060 for(col_type j = c; j < c + span ; ++j)
1061 old_width += column_info[j].width;
1063 if (cellInfo(i).width > old_width) {
1064 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1073 int Tabular::width() const
1076 for (col_type c = 0; c < ncols(); ++c)
1077 width += column_info[c].width;
1082 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1085 col_type const col = cellColumn(cell);
1086 // set alignment for the whole row if we are not in a multicolumn cell,
1087 // exclude possible multicolumn cells in the row
1088 if (!isMultiColumn(cell)) {
1089 for (row_type r = 0; r < nrows(); ++r) {
1090 // only if the column has no width the multirow inherits the
1091 // alignment of the column, otherwise it is left aligned
1092 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1093 && !isMultiColumn(cellIndex(r, col))) {
1094 cell_info[r][col].alignment = align;
1095 cell_info[r][col].inset->setContentAlignment(align);
1097 if ((isMultiRow(cellIndex(r, col)) && has_width)
1098 && !isMultiColumn(cellIndex(r, col))) {
1099 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1100 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1103 column_info[col].alignment = align;
1104 docstring & dpoint = column_info[col].decimal_point;
1105 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1106 dpoint = from_utf8(lyxrc.default_decimal_point);
1108 cellInfo(cell).alignment = align;
1109 cellInset(cell)->setContentAlignment(align);
1114 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1117 if (!isMultiColumn(cell) || onlycolumn)
1118 column_info[cellColumn(cell)].valignment = align;
1120 cellInfo(cell).valignment = align;
1127 * Allow line and paragraph breaks for fixed width cells or disallow them,
1128 * merge cell paragraphs and reset layout to standard for variable width
1131 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1133 inset->toggleFixedWidth(fixedWidth);
1137 // merge all paragraphs to one
1138 BufferParams const & bp = cur.bv().buffer().params();
1139 while (inset->paragraphs().size() > 1)
1140 mergeParagraph(bp, inset->paragraphs(), 0);
1144 // undo information has already been recorded
1145 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1146 bp.documentClass().plainLayoutName());
1153 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1154 Length const & width)
1156 col_type const c = cellColumn(cell);
1158 column_info[c].p_width = width;
1159 // reset the vertical alignment to top if the fixed width
1160 // is removed or zero because only fixed width columns can
1161 // have a vertical alignment
1162 if (column_info[c].p_width.zero())
1163 column_info[c].valignment = LYX_VALIGN_TOP;
1164 for (row_type r = 0; r < nrows(); ++r) {
1165 idx_type const cell = cellIndex(r, c);
1166 // because of multicolumns
1167 toggleFixedWidth(cur, cellInset(cell).get(),
1168 !getPWidth(cell).zero());
1169 if (isMultiRow(cell))
1170 setAlignment(cell, LYX_ALIGN_LEFT, false);
1172 // cur paragraph can become invalid after paragraphs were merged
1173 if (cur.pit() > cur.lastpit())
1174 cur.pit() = cur.lastpit();
1175 // cur position can become invalid after newlines were removed
1176 if (cur.pos() > cur.lastpos())
1177 cur.pos() = cur.lastpos();
1181 bool Tabular::setFixedWidth(row_type r, col_type c)
1183 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1184 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1185 || (multicol && !cell_info[r][c].p_width.zero());
1186 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1191 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1192 Length const & width)
1194 if (!isMultiColumn(cell))
1197 cellInfo(cell).p_width = width;
1198 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1199 // cur paragraph can become invalid after paragraphs were merged
1200 if (cur.pit() > cur.lastpit())
1201 cur.pit() = cur.lastpit();
1202 // cur position can become invalid after newlines were removed
1203 if (cur.pos() > cur.lastpos())
1204 cur.pos() = cur.lastpos();
1209 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1211 cellInfo(cell).mroffset = mroffset;
1216 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1217 Tabular::Feature what)
1219 if (what == SET_SPECIAL_MULTICOLUMN)
1220 cellInfo(cell).align_special = special;
1222 column_info[cellColumn(cell)].align_special = special;
1226 void Tabular::setTopLine(idx_type i, bool line)
1228 cellInfo(i).top_line = line;
1232 void Tabular::setBottomLine(idx_type i, bool line)
1234 cellInfo(i).bottom_line = line;
1238 void Tabular::setLeftLine(idx_type cell, bool line)
1240 cellInfo(cell).left_line = line;
1244 void Tabular::setRightLine(idx_type cell, bool line)
1246 cellInfo(cell).right_line = line;
1249 bool Tabular::rowTopLine(row_type r) const
1251 bool all_rows_set = true;
1252 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1253 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1254 return all_rows_set;
1258 bool Tabular::rowBottomLine(row_type r) const
1260 bool all_rows_set = true;
1261 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1262 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1263 return all_rows_set;
1267 bool Tabular::columnLeftLine(col_type c) const
1274 for (row_type r = 0; r < nrows(); ++r) {
1275 idx_type const i = cellIndex(r, c);
1276 if (c == cellColumn(i)) {
1278 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1279 if (cellInfo(i).left_line || right)
1283 return 2 * nrows_left >= total;
1287 bool Tabular::columnRightLine(col_type c) const
1292 int nrows_right = 0;
1294 for (row_type r = 0; r < nrows(); ++r) {
1295 idx_type i = cellIndex(r, c);
1296 if (c == cellColumn(i) + columnSpan(i) - 1) {
1298 bool left = (c + 1 < ncols()
1299 && cellInfo(cellIndex(r, c + 1)).left_line)
1300 || c + 1 == ncols();
1301 if (cellInfo(i).right_line && left)
1305 return 2 * nrows_right >= total;
1309 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1311 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1312 return cellInfo(cell).alignment;
1314 return column_info[cellColumn(cell)].alignment;
1319 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1321 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1322 return cellInfo(cell).valignment;
1323 return column_info[cellColumn(cell)].valignment;
1327 int Tabular::offsetVAlignment() const
1329 // for top-alignment the first horizontal table line must be exactly at
1330 // the position of the base line of the surrounding text line
1331 // for bottom alignment, the same is for the last table line
1332 int offset_valign = 0;
1333 switch (tabular_valignment) {
1334 case Tabular::LYX_VALIGN_BOTTOM:
1335 offset_valign = rowAscent(0) - height();
1337 case Tabular::LYX_VALIGN_MIDDLE:
1338 offset_valign = (- height()) / 2 + rowAscent(0);
1340 case Tabular::LYX_VALIGN_TOP:
1341 offset_valign = rowAscent(0);
1344 return offset_valign;
1348 Length const Tabular::getPWidth(idx_type cell) const
1350 if (isMultiColumn(cell))
1351 return cellInfo(cell).p_width;
1352 return column_info[cellColumn(cell)].p_width;
1356 Length const Tabular::getMROffset(idx_type cell) const
1358 return cellInfo(cell).mroffset;
1362 int Tabular::textHOffset(idx_type cell) const
1364 // the LaTeX Way :-(
1365 int x = WIDTH_OF_LINE;
1367 int const w = cellWidth(cell) - cellInfo(cell).width;
1369 switch (getAlignment(cell)) {
1370 case LYX_ALIGN_CENTER:
1373 case LYX_ALIGN_RIGHT:
1376 case LYX_ALIGN_DECIMAL: {
1377 // we center when no decimal point
1378 if (cellInfo(cell).decimal_width == 0) {
1382 col_type const c = cellColumn(cell);
1383 int max_dhoffset = 0;
1384 for(row_type r = 0; r < row_info.size() ; ++r) {
1385 idx_type const i = cellIndex(r, c);
1386 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1387 && cellInfo(i).decimal_width != 0)
1388 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1390 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1393 // LYX_ALIGN_LEFT: nothing :-)
1401 int Tabular::textVOffset(idx_type cell) const
1403 int voffset = cellInfo(cell).voffset;
1404 if (isMultiRow(cell)) {
1405 row_type const row = cellRow(cell);
1406 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1412 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1415 idx_type const numcells = numberOfCellsInRow(row);
1416 // we check against numcells to make sure we do not crash if all the
1417 // cells are multirow (bug #7535), but in that case our return value
1418 // is really invalid, i.e., it is NOT the first cell in the row. but
1419 // i do not know what to do here. (rgh)
1420 while (c < numcells - 1
1421 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1423 return cell_info[row][c].cellno;
1427 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1429 col_type c = ncols() - 1;
1430 // of course we check against 0 so we don't crash. but we have the same
1431 // problem as in the previous routine: if all the cells are part of a
1432 // multirow or part of a multi column, then our return value is invalid.
1434 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1435 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1437 return cell_info[row][c].cellno;
1441 Tabular::row_type Tabular::cellRow(idx_type cell) const
1443 if (cell >= numberofcells)
1447 return rowofcell[cell];
1451 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1453 if (cell >= numberofcells)
1457 return columnofcell[cell];
1461 void Tabular::write(ostream & os) const
1465 << write_attribute("version", 3)
1466 << write_attribute("rows", nrows())
1467 << write_attribute("columns", ncols())
1469 // global longtable options
1471 << write_attribute("rotate", rotate)
1472 << write_attribute("booktabs", use_booktabs)
1473 << write_attribute("islongtable", is_long_tabular)
1474 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1475 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1476 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1477 << write_attribute("headTopDL", endhead.topDL)
1478 << write_attribute("headBottomDL", endhead.bottomDL)
1479 << write_attribute("footTopDL", endfoot.topDL)
1480 << write_attribute("footBottomDL", endfoot.bottomDL)
1481 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1482 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1483 << write_attribute("lastFootEmpty", endlastfoot.empty);
1484 // longtables cannot be aligned vertically
1485 if (!is_long_tabular) {
1486 os << write_attribute("tabularvalignment", tabular_valignment);
1487 os << write_attribute("tabularwidth", tabular_width);
1489 if (is_long_tabular)
1490 os << write_attribute("longtabularalignment", longtabular_alignment);
1492 for (col_type c = 0; c < ncols(); ++c) {
1494 << write_attribute("alignment", column_info[c].alignment);
1495 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1496 os << write_attribute("decimal_point", column_info[c].decimal_point);
1497 os << write_attribute("valignment", column_info[c].valignment)
1498 << write_attribute("width", column_info[c].p_width.asString())
1499 << write_attribute("special", column_info[c].align_special)
1502 for (row_type r = 0; r < nrows(); ++r) {
1503 static const string def("default");
1505 if (row_info[r].top_space_default)
1506 os << write_attribute("topspace", def);
1508 os << write_attribute("topspace", row_info[r].top_space);
1509 if (row_info[r].bottom_space_default)
1510 os << write_attribute("bottomspace", def);
1512 os << write_attribute("bottomspace", row_info[r].bottom_space);
1513 if (row_info[r].interline_space_default)
1514 os << write_attribute("interlinespace", def);
1516 os << write_attribute("interlinespace", row_info[r].interline_space);
1517 os << write_attribute("endhead", row_info[r].endhead)
1518 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1519 << write_attribute("endfoot", row_info[r].endfoot)
1520 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1521 << write_attribute("newpage", row_info[r].newpage)
1522 << write_attribute("caption", row_info[r].caption)
1524 for (col_type c = 0; c < ncols(); ++c) {
1526 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1527 << write_attribute("multirow", cell_info[r][c].multirow)
1528 << write_attribute("mroffset", cell_info[r][c].mroffset)
1529 << write_attribute("alignment", cell_info[r][c].alignment)
1530 << write_attribute("valignment", cell_info[r][c].valignment)
1531 << write_attribute("topline", cell_info[r][c].top_line)
1532 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1533 << write_attribute("leftline", cell_info[r][c].left_line)
1534 << write_attribute("rightline", cell_info[r][c].right_line)
1535 << write_attribute("rotate", cell_info[r][c].rotate)
1536 << write_attribute("usebox", cell_info[r][c].usebox)
1537 << write_attribute("width", cell_info[r][c].p_width)
1538 << write_attribute("special", cell_info[r][c].align_special)
1540 os << "\\begin_inset ";
1541 cell_info[r][c].inset->write(os);
1542 os << "\n\\end_inset\n"
1544 // FIXME This can be removed again once the mystery
1545 // crash has been resolved.
1550 os << "</lyxtabular>\n";
1554 void Tabular::read(Lexer & lex)
1557 istream & is = lex.getStream();
1559 l_getline(is, line);
1560 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1561 LASSERT(false, return);
1565 if (!getTokenValue(line, "version", version))
1567 LATTEST(version >= 2);
1570 if (!getTokenValue(line, "rows", rows_arg))
1573 if (!getTokenValue(line, "columns", columns_arg))
1575 init(buffer_, rows_arg, columns_arg);
1576 l_getline(is, line);
1577 if (!prefixIs(line, "<features")) {
1578 lyxerr << "Wrong tabular format (expected <features ...> got"
1579 << line << ')' << endl;
1582 getTokenValue(line, "rotate", rotate);
1583 getTokenValue(line, "booktabs", use_booktabs);
1584 getTokenValue(line, "islongtable", is_long_tabular);
1585 getTokenValue(line, "tabularvalignment", tabular_valignment);
1586 getTokenValue(line, "tabularwidth", tabular_width);
1587 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1588 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1589 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1590 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1591 getTokenValue(line, "headTopDL", endhead.topDL);
1592 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1593 getTokenValue(line, "footTopDL", endfoot.topDL);
1594 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1595 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1596 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1597 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1599 for (col_type c = 0; c < ncols(); ++c) {
1601 if (!prefixIs(line,"<column")) {
1602 lyxerr << "Wrong tabular format (expected <column ...> got"
1603 << line << ')' << endl;
1606 getTokenValue(line, "alignment", column_info[c].alignment);
1607 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1608 getTokenValue(line, "valignment", column_info[c].valignment);
1609 getTokenValue(line, "width", column_info[c].p_width);
1610 getTokenValue(line, "special", column_info[c].align_special);
1613 for (row_type i = 0; i < nrows(); ++i) {
1614 l_getline(is, line);
1615 if (!prefixIs(line, "<row")) {
1616 lyxerr << "Wrong tabular format (expected <row ...> got"
1617 << line << ')' << endl;
1620 getTokenValue(line, "topspace", row_info[i].top_space,
1621 row_info[i].top_space_default);
1622 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1623 row_info[i].bottom_space_default);
1624 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1625 row_info[i].interline_space_default);
1626 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1627 getTokenValue(line, "endhead", row_info[i].endhead);
1628 getTokenValue(line, "endfoot", row_info[i].endfoot);
1629 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1630 getTokenValue(line, "newpage", row_info[i].newpage);
1631 getTokenValue(line, "caption", row_info[i].caption);
1632 for (col_type j = 0; j < ncols(); ++j) {
1633 l_getline(is, line);
1634 if (!prefixIs(line, "<cell")) {
1635 lyxerr << "Wrong tabular format (expected <cell ...> got"
1636 << line << ')' << endl;
1639 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1640 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1641 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1642 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1643 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1644 getTokenValue(line, "topline", cell_info[i][j].top_line);
1645 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1646 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1647 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1648 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1649 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1650 getTokenValue(line, "width", cell_info[i][j].p_width);
1652 getTokenValue(line, "special", cell_info[i][j].align_special);
1653 l_getline(is, line);
1654 if (prefixIs(line, "\\begin_inset")) {
1655 cell_info[i][j].inset->setBuffer(*buffer_);
1656 cell_info[i][j].inset->read(lex);
1657 l_getline(is, line);
1659 if (!prefixIs(line, "</cell>")) {
1660 lyxerr << "Wrong tabular format (expected </cell> got"
1661 << line << ')' << endl;
1665 l_getline(is, line);
1666 if (!prefixIs(line, "</row>")) {
1667 lyxerr << "Wrong tabular format (expected </row> got"
1668 << line << ')' << endl;
1672 while (!prefixIs(line, "</lyxtabular>")) {
1673 l_getline(is, line);
1679 bool Tabular::isMultiColumn(idx_type cell) const
1681 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1682 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1686 bool Tabular::hasMultiColumn(col_type c) const
1688 for (row_type r = 0; r < nrows(); ++r) {
1689 if (isMultiColumn(cellIndex(r, c)))
1696 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1698 return cell_info[cellRow(cell)][cellColumn(cell)];
1702 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1704 return cell_info[cellRow(cell)][cellColumn(cell)];
1708 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1709 bool const right_border)
1711 idx_type const col = cellColumn(cell);
1712 idx_type const row = cellRow(cell);
1713 for (idx_type i = 0; i < number; ++i)
1714 unsetMultiRow(cellIndex(row, col + i));
1716 // unsetting of multirow may have invalidated cell index
1717 cell = cellIndex(row, col);
1718 CellData & cs = cellInfo(cell);
1719 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1720 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1721 cs.alignment = column_info[col].alignment;
1722 setRightLine(cell, right_border);
1724 idx_type lastcell = cellIndex(row, col + number - 1);
1725 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1726 CellData & cs1 = cellInfo(cell + i);
1727 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1728 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1736 bool Tabular::isMultiRow(idx_type cell) const
1738 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1739 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1742 bool Tabular::hasMultiRow(row_type r) const
1744 for (col_type c = 0; c < ncols(); ++c) {
1745 if (isMultiRow(cellIndex(r, c)))
1751 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1752 bool const bottom_border,
1753 LyXAlignment const halign)
1755 idx_type const col = cellColumn(cell);
1756 idx_type const row = cellRow(cell);
1757 for (idx_type i = 0; i < number; ++i)
1758 unsetMultiColumn(cellIndex(row + i, col));
1760 // unsetting of multirow may have invalidated cell index
1761 cell = cellIndex(row, col);
1762 CellData & cs = cellInfo(cell);
1763 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1764 cs.valignment = LYX_VALIGN_MIDDLE;
1765 // the horizontal alignment of multirow cells can only
1766 // be changed for the whole table row,
1767 // support changing this only for the multirow cell can be done via
1769 if (getPWidth(cell).zero())
1770 cs.alignment = halign;
1772 cs.alignment = LYX_ALIGN_LEFT;
1774 // set the bottom line of the last selected cell
1775 setBottomLine(cell, bottom_border);
1777 for (idx_type i = 1; i < number; ++i) {
1778 CellData & cs1 = cell_info[row + i][col];
1779 cs1.multirow = CELL_PART_OF_MULTIROW;
1780 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1788 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1790 row_type const row = cellRow(cell);
1791 col_type const col = cellColumn(cell);
1793 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1800 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1802 col_type const column = cellColumn(cell);
1803 col_type row = cellRow(cell) + 1;
1804 while (row < nrows() && isPartOfMultiRow(row, column))
1807 return row - cellRow(cell);
1811 void Tabular::unsetMultiColumn(idx_type cell)
1813 if (!isMultiColumn(cell))
1816 row_type const row = cellRow(cell);
1817 col_type const col = cellColumn(cell);
1818 row_type const span = columnSpan(cell);
1819 for (col_type c = 0; c < span; ++c) {
1820 // in the table dialog the lines are set in every case
1821 // when unsetting a multicolumn this leads to an additional right
1822 // line for every cell that was part of the former multicolumn cell,
1823 // except if the cell is in the last column
1824 // therefore remove this line
1825 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1826 && (col + c) < (col + span - 1))
1827 cell_info[row][col + c].right_line = false;
1828 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1834 void Tabular::unsetMultiRow(idx_type cell)
1836 if (!isMultiRow(cell))
1839 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1840 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1841 row_type const row = cellRow(cell);
1842 col_type const col = cellColumn(cell);
1843 row_type const span = rowSpan(cell);
1844 for (row_type r = 0; r < span; ++r)
1845 cell_info[row + r][col].multirow = CELL_NORMAL;
1850 void Tabular::setRotateCell(idx_type cell, int value)
1852 cellInfo(cell).rotate = value;
1856 int Tabular::getRotateCell(idx_type cell) const
1858 return cellInfo(cell).rotate;
1862 bool Tabular::needRotating() const
1864 if (rotate && !is_long_tabular)
1866 for (row_type r = 0; r < nrows(); ++r)
1867 for (col_type c = 0; c < ncols(); ++c)
1868 if (cell_info[r][c].rotate != 0)
1874 bool Tabular::isLastCell(idx_type cell) const
1876 if (cell + 1 < numberofcells)
1882 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1884 if (cellRow(cell) == 0)
1887 col_type const col = cellColumn(cell);
1888 row_type r = cellRow(cell) - 1;
1889 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1892 return cell_info[r][col].cellno;
1896 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1898 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1899 if (nextrow < nrows())
1900 return cell_info[nextrow][cellColumn(cell)].cellno;
1905 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1907 LASSERT(column != npos && column < ncols(), column = 0);
1908 LASSERT(row != npos && row < nrows(), row = 0);
1909 return cell_info[row][column].cellno;
1913 void Tabular::setUsebox(idx_type cell, BoxType type)
1915 cellInfo(cell).usebox = type;
1919 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1920 // adds line breaks, see bug 4886.
1921 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1923 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1924 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1926 if (cellInfo(cell).usebox > 1)
1927 return cellInfo(cell).usebox;
1928 return useParbox(cell);
1933 // This are functions used for the longtable support
1935 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1941 row_info[row].endfirsthead = flag;
1945 row_info[row].endhead = flag;
1950 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1953 hd.set = haveLTHead();
1954 return row_info[row].endhead;
1958 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1961 hd.set = haveLTFirstHead();
1962 return row_info[row].endfirsthead;
1966 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1972 row_info[row].endlastfoot = flag;
1976 row_info[row].endfoot = flag;
1981 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1984 fd.set = haveLTFoot();
1985 return row_info[row].endfoot;
1989 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1992 fd.set = haveLTLastFoot();
1993 return row_info[row].endlastfoot;
1997 void Tabular::setLTNewPage(row_type row, bool what)
1999 row_info[row].newpage = what;
2003 bool Tabular::getLTNewPage(row_type row) const
2005 return row_info[row].newpage;
2009 bool Tabular::haveLTHead(bool withcaptions) const
2011 if (!is_long_tabular)
2013 for (row_type i = 0; i < nrows(); ++i)
2014 if (row_info[i].endhead &&
2015 (withcaptions || !row_info[i].caption))
2021 bool Tabular::haveLTFirstHead(bool withcaptions) const
2023 if (!is_long_tabular || endfirsthead.empty)
2025 for (row_type r = 0; r < nrows(); ++r)
2026 if (row_info[r].endfirsthead &&
2027 (withcaptions || !row_info[r].caption))
2033 bool Tabular::haveLTFoot(bool withcaptions) const
2035 if (!is_long_tabular)
2037 for (row_type r = 0; r < nrows(); ++r)
2038 if (row_info[r].endfoot &&
2039 (withcaptions || !row_info[r].caption))
2045 bool Tabular::haveLTLastFoot(bool withcaptions) const
2047 if (!is_long_tabular || endlastfoot.empty)
2049 for (row_type r = 0; r < nrows(); ++r)
2050 if (row_info[r].endlastfoot &&
2051 (withcaptions || !row_info[r].caption))
2057 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2059 idx_type i = getFirstCellInRow(row);
2061 setMultiColumn(i, numberOfCellsInRow(row), false);
2062 setTopLine(i, false);
2063 setBottomLine(i, false);
2064 setLeftLine(i, false);
2065 setRightLine(i, false);
2066 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2067 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2068 setLTHead(row, true, endfirsthead, true);
2069 row_info[row].endfirsthead = true;
2072 unsetMultiColumn(i);
2073 // When unsetting a caption row, also all existing
2074 // captions in this row must be dissolved.
2076 row_info[row].caption = what;
2081 bool Tabular::ltCaption(row_type row) const
2083 return row_info[row].caption;
2087 bool Tabular::haveLTCaption(CaptionType captiontype) const
2089 if (!is_long_tabular)
2091 for (row_type r = 0; r < nrows(); ++r) {
2092 if (row_info[r].caption) {
2093 switch (captiontype) {
2094 case CAPTION_FIRSTHEAD:
2095 if (row_info[r].endfirsthead)
2099 if (row_info[r].endhead)
2103 if (row_info[r].endfoot)
2106 case CAPTION_LASTFOOT:
2107 if (row_info[r].endlastfoot)
2119 // end longtable support functions
2121 void Tabular::setRowAscent(row_type row, int height)
2123 if (row >= nrows() || row_info[row].ascent == height)
2125 row_info[row].ascent = height;
2129 void Tabular::setRowDescent(row_type row, int height)
2131 if (row >= nrows() || row_info[row].descent == height)
2133 row_info[row].descent = height;
2137 int Tabular::rowAscent(row_type row) const
2139 LASSERT(row < nrows(), row = 0);
2140 return row_info[row].ascent;
2144 int Tabular::rowDescent(row_type row) const
2146 LASSERT(row < nrows(), row = 0);
2147 return row_info[row].descent;
2151 int Tabular::height() const
2154 for (row_type row = 0; row < nrows(); ++row)
2155 height += rowAscent(row) + rowDescent(row) +
2161 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2163 LASSERT(row < nrows(), return false);
2164 LASSERT(column < ncols(), return false);
2165 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2169 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2171 LASSERT(row < nrows(), return false);
2172 LASSERT(column < ncols(), return false);
2173 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2177 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang) const
2179 // we only output complete row lines and the 1st row here, the rest
2180 // is done in Tabular::TeXBottomHLine(...)
2182 // get for each column the topline (if any)
2183 vector<bool> topline;
2185 for (col_type c = 0; c < ncols(); ++c) {
2186 topline.push_back(topLine(cellIndex(row, c)));
2187 // If cell is part of a multirow and not the first cell of the
2188 // multirow, no line must be drawn.
2190 if (isMultiRow(cellIndex(row, c))
2191 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2197 // do nothing if empty first row, or incomplete row line after
2198 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2201 // only output complete row lines and the 1st row's clines
2202 if (nset == ncols()) {
2204 os << (row == 0 ? "\\toprule " : "\\midrule ");
2208 } else if (row == 0) {
2209 for (col_type c = 0; c < ncols(); ++c) {
2211 col_type offset = 0;
2212 for (col_type j = 0 ; j < c; ++j)
2213 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2216 //babel makes the "-" character an active one, so we have to suppress this here
2217 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2218 if (lang == "slovak" || lang == "czech")
2219 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2220 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2222 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2224 col_type cstart = c;
2225 for ( ; c < ncols() && topline[c]; ++c) {}
2227 for (col_type j = cstart ; j < c ; ++j)
2228 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2231 os << c + offset << "} ";
2239 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang) const
2241 // we output bottomlines of row r and the toplines of row r+1
2242 // if the latter do not span the whole tabular
2244 // get the bottomlines of row r, and toplines in next row
2245 bool lastrow = row == nrows() - 1;
2246 vector<bool> bottomline, topline;
2247 bool nextrowset = true;
2248 for (col_type c = 0; c < ncols(); ++c) {
2249 bottomline.push_back(bottomLine(cellIndex(row, c)));
2250 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2251 // If cell is part of a multirow and not the last cell of the
2252 // multirow, no line must be drawn.
2254 if (isMultiRow(cellIndex(row, c))
2255 && isMultiRow(cellIndex(row + 1, c))
2256 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2257 bottomline[c] = false;
2260 nextrowset &= topline[c];
2263 // combine this row's bottom lines and next row's toplines if necessary
2265 for (col_type c = 0; c < ncols(); ++c) {
2267 bottomline[c] = bottomline[c] || topline[c];
2272 // do nothing if empty, OR incomplete row line with a topline in next row
2273 if (nset == 0 || (nextrowset && nset != ncols()))
2276 if (nset == ncols()) {
2278 os << (lastrow ? "\\bottomrule" : "\\midrule");
2282 for (col_type c = 0; c < ncols(); ++c) {
2283 if (bottomline[c]) {
2284 col_type offset = 0;
2285 for (col_type j = 0 ; j < c; ++j)
2286 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2289 //babel makes the "-" character an active one, so we have to suppress this here
2290 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2291 if (lang == "slovak" || lang == "czech")
2292 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2293 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2295 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2297 col_type cstart = c;
2298 for ( ; c < ncols() && bottomline[c]; ++c) {}
2300 for (col_type j = cstart ; j < c ; ++j)
2301 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2304 os << c + offset << "} ";
2312 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2313 bool & ismulticol, bool & ismultirow) const
2315 row_type const r = cellRow(cell);
2316 if (is_long_tabular && row_info[r].caption)
2319 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2320 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2321 // figure out how to set the lines
2322 // we always set double lines to the right of the cell
2323 col_type const c = cellColumn(cell);
2324 col_type const nextcol = c + columnSpan(cell);
2325 bool colright = columnRightLine(c);
2326 bool colleft = columnLeftLine(c);
2327 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2328 bool nextcellleft = nextcol < ncols()
2329 && leftLine(cellIndex(r, nextcol));
2330 bool coldouble = colright && nextcolleft;
2331 bool celldouble = rightLine(cell) && nextcellleft;
2333 ismulticol = isMultiColumn(cell)
2334 || (c == 0 && colleft != leftLine(cell))
2335 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2336 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2337 || (coldouble != celldouble);
2339 // we center in multicol when no decimal point
2340 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2341 docstring const align_d = column_info[c].decimal_point;
2342 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2346 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2347 int latexcolspan = columnSpan(cell);
2348 for(col_type col = c; col < c + columnSpan(cell); ++col)
2349 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2353 os << "\\multicolumn{" << latexcolspan << "}{";
2354 if (c ==0 && leftLine(cell))
2356 if (!cellInfo(cell).align_special.empty()) {
2357 os << cellInfo(cell).align_special;
2359 if (!getPWidth(cell).zero()) {
2361 case LYX_ALIGN_LEFT:
2362 os << ">{\\raggedright}";
2364 case LYX_ALIGN_RIGHT:
2365 os << ">{\\raggedleft}";
2367 case LYX_ALIGN_CENTER:
2368 os << ">{\\centering}";
2374 case LYX_VALIGN_TOP:
2377 case LYX_VALIGN_MIDDLE:
2380 case LYX_VALIGN_BOTTOM:
2385 << from_ascii(getPWidth(cell).asLatexString())
2389 case LYX_ALIGN_LEFT:
2392 case LYX_ALIGN_RIGHT:
2399 } // end if else !getPWidth
2400 } // end if else !cellinfo_of_cell
2401 if (rightLine(cell) || nextcellleft)
2404 // add extra vertical line if we want a double one
2407 } // end if ismulticol
2409 // we only need code for the first multirow cell
2410 ismultirow = isMultiRow(cell);
2412 os << "\\multirow{" << rowSpan(cell) << "}{";
2413 if (!getPWidth(cell).zero())
2414 os << from_ascii(getPWidth(cell).asLatexString());
2416 // we need to set a default value
2419 if (!getMROffset(cell).zero())
2420 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2422 } // end if ismultirow
2424 if (getRotateCell(cell) != 0)
2425 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2427 if (getUsebox(cell) == BOX_PARBOX) {
2430 case LYX_VALIGN_TOP:
2433 case LYX_VALIGN_MIDDLE:
2436 case LYX_VALIGN_BOTTOM:
2440 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2442 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2443 os << "\\begin{minipage}[";
2445 case LYX_VALIGN_TOP:
2448 case LYX_VALIGN_MIDDLE:
2451 case LYX_VALIGN_BOTTOM:
2455 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2461 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2462 bool ismulticol, bool ismultirow) const
2464 row_type const r = cellRow(cell);
2465 if (is_long_tabular && row_info[r].caption)
2469 if (getUsebox(cell) == BOX_PARBOX)
2471 else if (getUsebox(cell) == BOX_MINIPAGE)
2472 os << breakln << "\\end{minipage}";
2473 if (getRotateCell(cell) != 0)
2474 os << breakln << "\\end{turn}";
2482 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2483 OutputParams const & runparams) const
2485 if (!is_long_tabular)
2489 // output caption which is in no header or footer
2490 if (haveLTCaption()) {
2491 for (row_type r = 0; r < nrows(); ++r) {
2492 if (row_info[r].caption &&
2493 !row_info[r].endfirsthead && !row_info[r].endhead &&
2494 !row_info[r].endfoot && !row_info[r].endlastfoot)
2495 TeXRow(os, r, runparams);
2498 // output first header info
2499 if (haveLTFirstHead()) {
2500 if (endfirsthead.topDL)
2502 for (row_type r = 0; r < nrows(); ++r) {
2503 if (row_info[r].endfirsthead)
2504 TeXRow(os, r, runparams);
2506 if (endfirsthead.bottomDL)
2508 os << "\\endfirsthead\n";
2510 // output header info
2512 if (endfirsthead.empty && !haveLTFirstHead())
2513 os << "\\endfirsthead\n";
2516 for (row_type r = 0; r < nrows(); ++r) {
2517 if (row_info[r].endhead)
2518 TeXRow(os, r, runparams);
2520 if (endhead.bottomDL)
2522 os << "\\endhead\n";
2524 // output footer info
2528 for (row_type r = 0; r < nrows(); ++r) {
2529 if (row_info[r].endfoot)
2530 TeXRow(os, r, runparams);
2532 if (endfoot.bottomDL)
2534 os << "\\endfoot\n";
2535 if (endlastfoot.empty && !haveLTLastFoot())
2536 os << "\\endlastfoot\n";
2538 // output lastfooter info
2539 if (haveLTLastFoot()) {
2540 if (endlastfoot.topDL)
2542 for (row_type r = 0; r < nrows(); ++r) {
2543 if (row_info[r].endlastfoot)
2544 TeXRow(os, r, runparams);
2546 if (endlastfoot.bottomDL)
2548 os << "\\endlastfoot\n";
2553 bool Tabular::isValidRow(row_type row) const
2555 if (!is_long_tabular)
2557 return !row_info[row].endhead && !row_info[row].endfirsthead
2558 && !row_info[row].endfoot && !row_info[row].endlastfoot
2559 && !row_info[row].caption;
2563 void Tabular::TeXRow(otexstream & os, row_type row,
2564 OutputParams const & runparams) const
2566 idx_type cell = cellIndex(row, 0);
2567 InsetTableCell const * inset = cellInset(cell);
2568 Paragraph const & par = inset->paragraphs().front();
2569 string const lang = par.getParLanguage(buffer().params())->lang();
2571 //output the top line
2572 TeXTopHLine(os, row, lang);
2574 if (row_info[row].top_space_default) {
2576 os << "\\addlinespace\n";
2578 os << "\\noalign{\\vskip\\doublerulesep}\n";
2579 } else if(!row_info[row].top_space.zero()) {
2581 os << "\\addlinespace["
2582 << from_ascii(row_info[row].top_space.asLatexString())
2585 os << "\\noalign{\\vskip"
2586 << from_ascii(row_info[row].top_space.asLatexString())
2590 bool ismulticol = false;
2591 bool ismultirow = false;
2592 for (col_type c = 0; c < ncols(); ++c) {
2593 if (isPartOfMultiColumn(row, c))
2596 cell = cellIndex(row, c);
2598 if (isPartOfMultiRow(row, c)
2599 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2600 if (cell != getLastCellInRow(row))
2605 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2606 InsetTableCell const * inset = cellInset(cell);
2608 Paragraph const & par = inset->paragraphs().front();
2610 os.texrow().forceStart(par.id(), 0);
2612 bool rtl = par.isRTL(buffer().params())
2614 && getPWidth(cell).zero()
2615 && !runparams.use_polyglossia;
2619 par.getParLanguage(buffer().params())->lang();
2620 if (lang == "farsi")
2622 else if (lang == "arabic_arabi")
2624 // currently, remaining RTL languages are
2625 // arabic_arabtex and hebrew
2629 // pass to the OutputParams that we are in a cell and
2630 // which alignment we have set.
2631 // InsetNewline needs this context information.
2632 OutputParams newrp(runparams);
2633 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2634 ? OutputParams::PLAIN
2635 : OutputParams::ALIGNED;
2637 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2638 // copy cell and split in 2
2639 InsetTableCell head = InsetTableCell(*cellInset(cell));
2640 head.setBuffer(const_cast<Buffer &>(buffer()));
2641 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2643 dit.push_back(CursorSlice(head));
2644 head.setMacrocontextPositionRecursive(dit);
2645 bool hassep = false;
2646 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2647 head.latex(os, newrp);
2650 tail.setBuffer(head.buffer());
2652 dit.push_back(CursorSlice(tail));
2653 tail.setMacrocontextPositionRecursive(dit);
2654 tail.latex(os, newrp);
2656 } else if (ltCaption(row)) {
2657 // Inside longtable caption rows, we must only output the caption inset
2658 // with its content and omit anything outside of that (see #10791)
2659 InsetIterator it = inset_iterator_begin(*const_cast<InsetTableCell *>(inset));
2660 InsetIterator i_end = inset_iterator_end(*const_cast<InsetTableCell *>(inset));
2661 for (; it != i_end; ++it) {
2662 if (it->lyxCode() != CAPTION_CODE)
2664 it->latex(os, runparams);
2667 } else if (!isPartOfMultiRow(row, c)) {
2668 if (!runparams.nice)
2669 os.texrow().start(par.id(), 0);
2670 inset->latex(os, newrp);
2673 runparams.encoding = newrp.encoding;
2677 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2678 if (cell != getLastCellInRow(row)) { // not last cell in row
2685 os << "\\tabularnewline";
2686 if (row_info[row].bottom_space_default) {
2688 os << "\\addlinespace";
2690 os << "[\\doublerulesep]";
2691 } else if (!row_info[row].bottom_space.zero()) {
2693 os << "\\addlinespace";
2695 << from_ascii(row_info[row].bottom_space.asLatexString())
2700 //output the bottom line
2701 TeXBottomHLine(os, row, lang);
2703 if (row_info[row].interline_space_default) {
2705 os << "\\addlinespace\n";
2707 os << "\\noalign{\\vskip\\doublerulesep}\n";
2708 } else if (!row_info[row].interline_space.zero()) {
2710 os << "\\addlinespace["
2711 << from_ascii(row_info[row].interline_space.asLatexString())
2714 os << "\\noalign{\\vskip"
2715 << from_ascii(row_info[row].interline_space.asLatexString())
2721 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2723 bool const is_tabular_star = !tabular_width.zero();
2724 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
2726 //+---------------------------------------------------------------------
2727 //+ first the opening preamble +
2728 //+---------------------------------------------------------------------
2731 if (!TexRow::isNone(pos))
2732 os.texrow().start(pos);
2734 if (rotate != 0 && !is_long_tabular)
2735 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2737 if (is_long_tabular) {
2738 os << "\\begin{longtable}";
2739 switch (longtabular_alignment) {
2740 case LYX_LONGTABULAR_ALIGN_LEFT:
2743 case LYX_LONGTABULAR_ALIGN_CENTER:
2745 case LYX_LONGTABULAR_ALIGN_RIGHT:
2750 if (is_tabular_star)
2751 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2753 os << "\\begin{tabular}";
2754 switch (tabular_valignment) {
2755 case LYX_VALIGN_TOP:
2758 case LYX_VALIGN_MIDDLE:
2760 case LYX_VALIGN_BOTTOM:
2768 if (is_tabular_star)
2769 os << "@{\\extracolsep{\\fill}}";
2771 for (col_type c = 0; c < ncols(); ++c) {
2772 if (columnLeftLine(c))
2774 if (!column_info[c].align_special.empty()) {
2775 os << column_info[c].align_special;
2777 if (!column_info[c].p_width.zero()) {
2778 bool decimal = false;
2779 switch (column_info[c].alignment) {
2780 case LYX_ALIGN_LEFT:
2781 os << ">{\\raggedright}";
2783 case LYX_ALIGN_RIGHT:
2784 os << ">{\\raggedleft}";
2786 case LYX_ALIGN_CENTER:
2787 os << ">{\\centering}";
2789 case LYX_ALIGN_NONE:
2790 case LYX_ALIGN_BLOCK:
2791 case LYX_ALIGN_LAYOUT:
2792 case LYX_ALIGN_SPECIAL:
2794 case LYX_ALIGN_DECIMAL:
2795 os << ">{\\raggedleft}";
2801 switch (column_info[c].valignment) {
2802 case LYX_VALIGN_TOP:
2803 // this is the default
2805 case LYX_VALIGN_MIDDLE:
2808 case LYX_VALIGN_BOTTOM:
2814 // Fixed-width cells with alignment at decimal separator
2815 // are output as two cells of half the width with the decimal
2816 // separator as column sep. This effectively puts the content
2817 // centered, which differs from the normal decimal sep alignment
2818 // and is not ideal, but we cannot do better ATM (see #9568).
2819 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
2821 docstring const halffixedwith =
2822 from_ascii(Length(column_info[c].p_width.value() / 2,
2823 column_info[c].p_width.unit()).asLatexString());
2827 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
2834 << from_ascii(column_info[c].p_width.asLatexString())
2837 switch (column_info[c].alignment) {
2838 case LYX_ALIGN_LEFT:
2841 case LYX_ALIGN_RIGHT:
2844 case LYX_ALIGN_DECIMAL:
2845 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2851 } // end if else !column_info[i].p_width
2852 } // end if else !column_info[i].align_special
2853 if (columnRightLine(c))
2858 TeXLongtableHeaderFooter(os, runparams);
2860 //+---------------------------------------------------------------------
2861 //+ the single row and columns (cells) +
2862 //+---------------------------------------------------------------------
2864 for (row_type r = 0; r < nrows(); ++r) {
2865 if (isValidRow(r)) {
2866 TeXRow(os, r, runparams);
2867 if (is_long_tabular && row_info[r].newpage)
2868 os << "\\newpage\n";
2872 //+---------------------------------------------------------------------
2873 //+ the closing of the tabular +
2874 //+---------------------------------------------------------------------
2876 if (is_long_tabular)
2877 os << "\\end{longtable}";
2879 if (is_tabular_star)
2880 os << "\\end{tabular*}";
2882 os << "\\end{tabular}";
2885 if (rotate != 0 && !is_long_tabular)
2886 os << breakln << "\\end{turn}";
2888 if (!TexRow::isNone(pos))
2889 os.texrow().start(pos);
2893 int Tabular::docbookRow(odocstream & os, row_type row,
2894 OutputParams const & runparams) const
2897 idx_type cell = getFirstCellInRow(row);
2900 for (col_type c = 0; c < ncols(); ++c) {
2901 if (isPartOfMultiColumn(row, c))
2904 os << "<entry align=\"";
2905 switch (getAlignment(cell)) {
2906 case LYX_ALIGN_LEFT:
2909 case LYX_ALIGN_RIGHT:
2917 os << "\" valign=\"";
2918 switch (getVAlignment(cell)) {
2919 case LYX_VALIGN_TOP:
2922 case LYX_VALIGN_BOTTOM:
2925 case LYX_VALIGN_MIDDLE:
2930 if (isMultiColumn(cell)) {
2931 os << " namest=\"col" << c << "\" ";
2932 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2936 ret += cellInset(cell)->docbook(os, runparams);
2945 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2949 //+---------------------------------------------------------------------
2950 //+ first the opening preamble +
2951 //+---------------------------------------------------------------------
2953 os << "<tgroup cols=\"" << ncols()
2954 << "\" colsep=\"1\" rowsep=\"1\">\n";
2956 for (col_type c = 0; c < ncols(); ++c) {
2957 os << "<colspec colname=\"col" << c << "\" align=\"";
2958 switch (column_info[c].alignment) {
2959 case LYX_ALIGN_LEFT:
2962 case LYX_ALIGN_RIGHT:
2970 if (runparams.flavor == OutputParams::XML)
2976 //+---------------------------------------------------------------------
2977 //+ Long Tabular case +
2978 //+---------------------------------------------------------------------
2980 // output caption info
2981 // The caption flag wins over head/foot
2982 if (haveLTCaption()) {
2983 os << "<caption>\n";
2985 for (row_type r = 0; r < nrows(); ++r) {
2986 if (row_info[r].caption) {
2987 ret += docbookRow(os, r, runparams);
2990 os << "</caption>\n";
2993 // output header info
2994 if (haveLTHead(false) || haveLTFirstHead(false)) {
2997 for (row_type r = 0; r < nrows(); ++r) {
2998 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2999 !row_info[r].caption) {
3000 ret += docbookRow(os, r, runparams);
3006 // output footer info
3007 if (haveLTFoot(false) || haveLTLastFoot(false)) {
3010 for (row_type r = 0; r < nrows(); ++r) {
3011 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3012 !row_info[r].caption) {
3013 ret += docbookRow(os, r, runparams);
3020 //+---------------------------------------------------------------------
3021 //+ the single row and columns (cells) +
3022 //+---------------------------------------------------------------------
3026 for (row_type r = 0; r < nrows(); ++r) {
3027 if (isValidRow(r)) {
3028 ret += docbookRow(os, r, runparams);
3033 //+---------------------------------------------------------------------
3034 //+ the closing of the tabular +
3035 //+---------------------------------------------------------------------
3044 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3045 OutputParams const & runparams, bool header) const
3048 string const celltag = header ? "th" : "td";
3049 idx_type cell = getFirstCellInRow(row);
3051 xs << html::StartTag("tr");
3052 for (col_type c = 0; c < ncols(); ++c) {
3053 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3058 Length const cwidth = column_info[c].p_width;
3059 if (!cwidth.zero()) {
3060 string const hwidth = cwidth.asHTMLString();
3061 attr << "style =\"width: " << hwidth << ";\" ";
3065 switch (getAlignment(cell)) {
3066 case LYX_ALIGN_LEFT:
3069 case LYX_ALIGN_RIGHT:
3077 attr << " valign='";
3078 switch (getVAlignment(cell)) {
3079 case LYX_VALIGN_TOP:
3082 case LYX_VALIGN_BOTTOM:
3085 case LYX_VALIGN_MIDDLE:
3090 if (isMultiColumn(cell))
3091 attr << " colspan='" << columnSpan(cell) << "'";
3092 else if (isMultiRow(cell))
3093 attr << " rowspan='" << rowSpan(cell) << "'";
3095 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3096 ret += cellInset(cell)->xhtml(xs, runparams);
3097 xs << html::EndTag(celltag) << html::CR();
3100 xs << html::EndTag("tr");
3105 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3109 if (is_long_tabular) {
3110 // we'll wrap it in a div, so as to deal with alignment
3112 switch (longtabular_alignment) {
3113 case LYX_LONGTABULAR_ALIGN_LEFT:
3116 case LYX_LONGTABULAR_ALIGN_CENTER:
3119 case LYX_LONGTABULAR_ALIGN_RIGHT:
3123 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3125 // The caption flag wins over head/foot
3126 if (haveLTCaption()) {
3127 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3129 for (row_type r = 0; r < nrows(); ++r)
3130 if (row_info[r].caption)
3131 ret += xhtmlRow(xs, r, runparams);
3132 xs << html::EndTag("div") << html::CR();
3136 xs << html::StartTag("table") << html::CR();
3138 // output header info
3139 bool const havefirsthead = haveLTFirstHead(false);
3140 // if we have a first head, then we are going to ignore the
3141 // headers for the additional pages, since there aren't any
3142 // in XHTML. this test accomplishes that.
3143 bool const havehead = !havefirsthead && haveLTHead(false);
3144 if (havehead || havefirsthead) {
3145 xs << html::StartTag("thead") << html::CR();
3146 for (row_type r = 0; r < nrows(); ++r) {
3147 if (((havefirsthead && row_info[r].endfirsthead) ||
3148 (havehead && row_info[r].endhead)) &&
3149 !row_info[r].caption) {
3150 ret += xhtmlRow(xs, r, runparams, true);
3153 xs << html::EndTag("thead") << html::CR();
3155 // output footer info
3156 bool const havelastfoot = haveLTLastFoot(false);
3158 bool const havefoot = !havelastfoot && haveLTFoot(false);
3159 if (havefoot || havelastfoot) {
3160 xs << html::StartTag("tfoot") << html::CR();
3161 for (row_type r = 0; r < nrows(); ++r) {
3162 if (((havelastfoot && row_info[r].endlastfoot) ||
3163 (havefoot && row_info[r].endfoot)) &&
3164 !row_info[r].caption) {
3165 ret += xhtmlRow(xs, r, runparams);
3168 xs << html::EndTag("tfoot") << html::CR();
3171 xs << html::StartTag("tbody") << html::CR();
3172 for (row_type r = 0; r < nrows(); ++r) {
3173 if (isValidRow(r)) {
3174 ret += xhtmlRow(xs, r, runparams);
3177 xs << html::EndTag("tbody")
3179 << html::EndTag("table")
3181 if (is_long_tabular)
3182 xs << html::EndTag("div") << html::CR();
3187 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3188 vector<unsigned int> const & clen) const
3190 idx_type const fcell = getFirstCellInRow(row);
3191 idx_type const n = numberOfCellsInRow(row) + fcell;
3194 for (idx_type i = fcell; i < n; ++i) {
3204 for (idx_type i = fcell; i < n; ++i) {
3215 col_type column = cellColumn(i);
3216 int len = clen[column];
3217 while (column < ncols() - 1
3218 && isPartOfMultiColumn(row, ++column))
3219 len += clen[column] + 4;
3220 os << docstring(len, ch);
3235 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3236 vector<unsigned int> const & clen) const
3238 idx_type const fcell = getFirstCellInRow(row);
3239 idx_type const n = numberOfCellsInRow(row) + fcell;
3242 for (idx_type i = fcell; i < n; ++i) {
3243 if (bottomLine(i)) {
3252 for (idx_type i = fcell; i < n; ++i) {
3253 if (bottomLine(i)) {
3263 col_type column = cellColumn(i);
3264 int len = clen[column];
3265 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3266 len += clen[column] + 4;
3267 os << docstring(len, ch);
3268 if (bottomLine(i)) {
3282 void Tabular::plaintextPrintCell(odocstringstream & os,
3283 OutputParams const & runparams,
3284 idx_type cell, row_type row, col_type column,
3285 vector<unsigned int> const & clen,
3286 bool onlydata, size_t max_length) const
3288 odocstringstream sstr;
3289 cellInset(cell)->plaintext(sstr, runparams, max_length);
3301 unsigned int len1 = sstr.str().length();
3302 unsigned int len2 = clen[column];
3303 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3304 len2 += clen[column] + 4;
3307 switch (getAlignment(cell)) {
3309 case LYX_ALIGN_LEFT:
3312 case LYX_ALIGN_RIGHT:
3316 case LYX_ALIGN_CENTER:
3322 os << docstring(len1, ' ') << sstr.str()
3323 << docstring(len2, ' ');
3325 if (rightLine(cell))
3332 void Tabular::plaintext(odocstringstream & os,
3333 OutputParams const & runparams, int const depth,
3334 bool onlydata, char_type delim, size_t max_length) const
3336 // first calculate the width of the single columns
3337 vector<unsigned int> clen(ncols());
3340 // first all non multicolumn cells!
3341 for (col_type c = 0; c < ncols(); ++c) {
3343 for (row_type r = 0; r < nrows(); ++r) {
3344 idx_type cell = cellIndex(r, c);
3345 if (isMultiColumn(cell))
3347 odocstringstream sstr;
3348 cellInset(cell)->plaintext(sstr, runparams, max_length);
3349 if (clen[c] < sstr.str().length())
3350 clen[c] = sstr.str().length();
3353 // then all multicolumn cells!
3354 for (col_type c = 0; c < ncols(); ++c) {
3355 for (row_type r = 0; r < nrows(); ++r) {
3356 idx_type cell = cellIndex(r, c);
3357 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3359 odocstringstream sstr;
3360 cellInset(cell)->plaintext(sstr, runparams, max_length);
3361 int len = int(sstr.str().length());
3362 idx_type const n = columnSpan(cell);
3363 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3365 if (len > int(clen[c + n - 1]))
3366 clen[c + n - 1] = len;
3371 for (row_type r = 0; r < nrows(); ++r) {
3372 if (!onlydata && plaintextTopHLine(os, r, clen))
3373 os << docstring(depth * 2, ' ');
3374 for (col_type c = 0; c < ncols(); ++c) {
3375 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3377 if (onlydata && c > 0)
3378 // we don't use operator<< for single UCS4 character.
3379 // see explanation in docstream.h
3381 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3383 if (os.str().size() > max_length)
3388 os << docstring(depth * 2, ' ');
3389 if (plaintextBottomHLine(os, r, clen))
3390 os << docstring(depth * 2, ' ');
3392 if (os.str().size() > max_length)
3398 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3400 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3404 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3406 return cell_info[row][column].inset;
3410 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3412 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3416 void Tabular::setCellInset(row_type row, col_type column,
3417 shared_ptr<InsetTableCell> ins)
3419 CellData & cd = cell_info[row][column];
3424 void Tabular::validate(LaTeXFeatures & features) const
3426 features.require("NeedTabularnewline");
3428 features.require("booktabs");
3429 if (is_long_tabular)
3430 features.require("longtable");
3432 features.require("rotating");
3433 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3434 if (isMultiRow(cell))
3435 features.require("multirow");
3436 if (getVAlignment(cell) != LYX_VALIGN_TOP
3437 || !getPWidth(cell).zero())
3438 features.require("array");
3439 // Tell footnote that we need a savenote
3440 // environment in non-long tables or
3441 // longtable headers/footers
3442 else if (!is_long_tabular && !features.inFloat())
3443 features.saveNoteEnv("tabular");
3444 else if (!isValidRow(cellRow(cell)))
3445 features.saveNoteEnv("longtable");
3447 cellInset(cell)->validate(features);
3448 features.saveNoteEnv(string());
3453 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3455 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3456 ParagraphList::const_iterator cit = parlist.begin();
3457 ParagraphList::const_iterator end = parlist.end();
3459 for (; cit != end; ++cit)
3460 for (int i = 0; i < cit->size(); ++i)
3461 if (cit->isNewline(i))
3468 /////////////////////////////////////////////////////////////////////
3472 /////////////////////////////////////////////////////////////////////
3474 InsetTableCell::InsetTableCell(Buffer * buf)
3475 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3476 contentAlign(LYX_ALIGN_CENTER)
3480 bool InsetTableCell::forcePlainLayout(idx_type) const
3482 return !isFixedWidth;
3486 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3488 return isFixedWidth;
3492 bool InsetTableCell::forceLocalFontSwitch() const
3494 return isFixedWidth;
3498 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3499 FuncStatus & status) const
3501 bool enabled = true;
3502 switch (cmd.action()) {
3503 case LFUN_MATH_DISPLAY:
3504 if (!hasFixedWidth()) {
3509 return InsetText::getStatus(cur, cmd, status);
3511 status.setEnabled(enabled);
3515 docstring InsetTableCell::asString(bool intoInsets)
3518 if (paragraphs().empty())
3520 ParagraphList::const_iterator it = paragraphs().begin();
3521 ParagraphList::const_iterator en = paragraphs().end();
3523 for (; it != en; ++it) {
3528 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3534 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3535 UpdateType utype, TocBackend & backend) const
3537 InsetText::iterateForToc(di, output_active, utype, backend);
3541 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3544 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3545 return InsetText::xhtml(xs, rp);
3550 /////////////////////////////////////////////////////////////////////
3554 /////////////////////////////////////////////////////////////////////
3556 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3558 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3559 rowselect_(false), colselect_(false)
3564 InsetTabular::InsetTabular(InsetTabular const & tab)
3565 : Inset(tab), tabular(tab.tabular),
3566 rowselect_(false), colselect_(false)
3571 InsetTabular::~InsetTabular()
3573 hideDialogs("tabular", this);
3577 void InsetTabular::setBuffer(Buffer & buf)
3579 tabular.setBuffer(buf);
3580 Inset::setBuffer(buf);
3584 bool InsetTabular::insetAllowed(InsetCode code) const
3589 case MATHMACRO_CODE:
3594 return tabular.is_long_tabular;
3602 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3604 return tabular.is_long_tabular &&
3605 (newtype == "Standard" || newtype == "Unnumbered");
3609 void InsetTabular::write(ostream & os) const
3611 os << "Tabular" << endl;
3616 string InsetTabular::contextMenu(BufferView const &, int, int) const
3618 // FIXME: depending on the selection state,
3619 // we could offer a different menu.
3620 return cell(0)->contextMenuName() + ";" + contextMenuName();
3624 string InsetTabular::contextMenuName() const
3626 return "context-tabular";
3630 void InsetTabular::read(Lexer & lex)
3632 //bool const old_format = (lex.getString() == "\\LyXTable");
3640 string token = lex.getString();
3641 while (lex && token != "\\end_inset") {
3643 token = lex.getString();
3646 lex.printError("Missing \\end_inset at this point. ");
3650 int InsetTabular::rowFromY(Cursor & cur, int y) const
3652 // top y coordinate of tabular
3653 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3655 for (; r < tabular.nrows() && y > h; ++r)
3656 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3657 + tabular.interRowSpace(r);
3663 int InsetTabular::columnFromX(Cursor & cur, int x) const
3665 // left x coordinate of tabular
3666 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3668 for (; c < tabular.ncols() && x > w; ++c)
3669 w += tabular.cellWidth(c);
3674 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3676 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3677 // mi.base.textwidth << "\n";
3678 LBUFERR(mi.base.bv);
3680 for (row_type r = 0; r < tabular.nrows(); ++r) {
3683 for (col_type c = 0; c < tabular.ncols(); ++c) {
3684 if (tabular.isPartOfMultiColumn(r, c)
3685 || tabular.isPartOfMultiRow(r, c))
3686 // multicolumn or multirow cell, but not first one
3688 idx_type const cell = tabular.cellIndex(r, c);
3691 Length const p_width = tabular.getPWidth(cell);
3692 if (!p_width.zero())
3693 m.base.textwidth = mi.base.inPixels(p_width);
3694 tabular.cellInset(cell)->metrics(m, dim);
3695 if (!p_width.zero())
3696 dim.wid = m.base.textwidth;
3697 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3698 + tabular.interColumnSpace(cell);
3700 // FIXME(?): do we need a second metrics call?
3701 TextMetrics const & tm =
3702 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3704 // determine horizontal offset because of decimal align (if necessary)
3705 int decimal_width = 0;
3706 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3707 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
3708 tail.setBuffer(tabular.buffer());
3709 // we need to set macrocontext position everywhere
3710 // otherwise we crash with nested insets (e.g. footnotes)
3711 // after decimal point
3712 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3714 dit.push_back(CursorSlice(tail));
3715 tail.setMacrocontextPositionRecursive(dit);
3717 // remove text leading decimal point
3718 docstring const align_d = tabular.column_info[c].decimal_point;
3719 dit = separatorPos(&tail, align_d);
3721 pit_type const psize = tail.paragraphs().front().size();
3723 tail.paragraphs().front().eraseChars(0,
3724 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3726 tail.metrics(m, dim1);
3727 decimal_width = dim1.width();
3730 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3731 tabular.cell_info[r][c].decimal_width = decimal_width;
3733 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3734 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3735 int const lastpardes = tm.last().second->descent()
3736 + TEXT_TO_INSET_OFFSET;
3738 switch (tabular.getVAlignment(cell)) {
3739 case Tabular::LYX_VALIGN_TOP:
3741 case Tabular::LYX_VALIGN_MIDDLE:
3742 offset = -(dim.des - lastpardes)/2;
3744 case Tabular::LYX_VALIGN_BOTTOM:
3745 offset = -(dim.des - lastpardes);
3748 tabular.cell_info[r][c].voffset = offset;
3749 maxasc = max(maxasc, dim.asc - offset);
3750 maxdes = max(maxdes, dim.des + offset);
3752 int const top_space = tabular.row_info[r].top_space_default ?
3753 default_line_space :
3754 mi.base.inPixels(tabular.row_info[r].top_space);
3755 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3756 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3757 default_line_space :
3758 mi.base.inPixels(tabular.row_info[r].bottom_space);
3759 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3762 tabular.updateColumnWidths();
3763 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
3764 dim.des = tabular.height() - dim.asc;
3765 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3769 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3772 if (&cur.inset() == this && cur.selection()) {
3773 if (cur.selIsMultiCell()) {
3776 getSelection(cur, rs, re, cs, ce);
3778 idx_type const cell = tabular.cellIndex(row, col);
3779 col_type const cspan = tabular.columnSpan(cell);
3780 row_type const rspan = tabular.rowSpan(cell);
3781 if (col + cspan - 1 >= cs && col <= ce
3782 && row + rspan - 1 >= rs && row <= re)
3785 if (col == tabular.cellColumn(cur.idx())
3786 && row == tabular.cellRow(cur.idx())) {
3787 CursorSlice const & beg = cur.selBegin();
3788 CursorSlice const & end = cur.selEnd();
3790 if ((end.lastpos() > 0 || end.lastpit() > 0)
3791 && end.pos() == end.lastpos() && beg.pos() == 0
3792 && end.pit() == end.lastpit() && beg.pit() == 0)
3800 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3802 x += ADD_TO_TABULAR_WIDTH;
3804 BufferView * bv = pi.base.bv;
3805 Cursor & cur = pi.base.bv->cursor();
3807 // FIXME: As the full background is painted in drawBackground(),
3808 // we have no choice but to do a full repaint for the Text cells.
3809 pi.full_repaint = true;
3811 bool const original_selection_state = pi.selected;
3815 int yy = y + tabular.offsetVAlignment();
3816 for (row_type r = 0; r < tabular.nrows(); ++r) {
3818 for (col_type c = 0; c < tabular.ncols(); ++c) {
3819 if (tabular.isPartOfMultiColumn(r, c))
3822 idx = tabular.cellIndex(r, c);
3824 if (tabular.isPartOfMultiRow(r, c)) {
3825 nx += tabular.cellWidth(idx);
3829 pi.selected |= isCellSelected(cur, r, c);
3830 int const cx = nx + tabular.textHOffset(idx);
3831 int const cy = yy + tabular.textVOffset(idx);
3832 // Cache the Inset position.
3833 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3834 cell(idx)->draw(pi, cx, cy);
3835 drawCellLines(pi, nx, yy, r, idx);
3836 nx += tabular.cellWidth(idx);
3837 pi.selected = original_selection_state;
3840 if (r + 1 < tabular.nrows())
3841 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3842 + tabular.interRowSpace(r + 1);
3847 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3849 x += ADD_TO_TABULAR_WIDTH;
3850 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
3851 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3852 pi.backgroundColor(this));
3856 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3858 Cursor & cur = pi.base.bv->cursor();
3860 x += ADD_TO_TABULAR_WIDTH;
3862 if (!cur.selection())
3864 if (&cur.inset() != this)
3869 bool const full_cell_selected = isCellSelected(cur,
3870 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3872 if (cur.selIsMultiCell() || full_cell_selected) {
3873 for (row_type r = 0; r < tabular.nrows(); ++r) {
3875 for (col_type c = 0; c < tabular.ncols(); ++c) {
3876 if (tabular.isPartOfMultiColumn(r, c))
3879 idx_type const cell = tabular.cellIndex(r, c);
3881 if (tabular.isPartOfMultiRow(r, c)) {
3882 xx += tabular.cellWidth(cell);
3885 int const w = tabular.cellWidth(cell);
3886 int const h = tabular.cellHeight(cell);
3887 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
3888 if (isCellSelected(cur, r, c))
3889 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3892 if (r + 1 < tabular.nrows())
3893 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3894 + tabular.interRowSpace(r + 1);
3898 // FIXME: This code has no effect because InsetTableCell does not handle
3899 // drawSelection other than the trivial implementation in Inset.
3901 // x += cellXPos(cur.idx());
3902 // x += tabular.textHOffset(cur.idx());
3903 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3908 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3909 row_type row, idx_type cell) const
3911 y -= tabular.rowAscent(row);
3912 int const w = tabular.cellWidth(cell);
3913 int const h = tabular.cellHeight(cell);
3914 Color const linecolor = pi.textColor(Color_tabularline);
3915 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3918 bool drawline = tabular.topLine(cell)
3919 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3920 pi.pain.line(x, y, x + w, y,
3921 drawline ? linecolor : gridcolor,
3922 drawline ? Painter::line_solid : Painter::line_onoffdash);
3925 drawline = tabular.bottomLine(cell);
3926 pi.pain.line(x, y + h, x + w, y + h,
3927 drawline ? linecolor : gridcolor,
3928 drawline ? Painter::line_solid : Painter::line_onoffdash);
3931 col_type const col = tabular.cellColumn(cell);
3932 drawline = tabular.leftLine(cell)
3933 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3934 pi.pain.line(x, y, x, y + h,
3935 drawline ? linecolor : gridcolor,
3936 drawline ? Painter::line_solid : Painter::line_onoffdash);
3939 x -= tabular.interColumnSpace(cell);
3940 col_type next_cell_col = col + 1;
3941 while (next_cell_col < tabular.ncols()
3942 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3944 drawline = tabular.rightLine(cell)
3945 || (next_cell_col < tabular.ncols()
3946 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3947 pi.pain.line(x + w, y, x + w, y + h,
3948 drawline ? linecolor : gridcolor,
3949 drawline ? Painter::line_solid : Painter::line_onoffdash);
3953 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3955 //lyxerr << "InsetTabular::edit: " << this << endl;
3959 if (isRightToLeft(cur))
3960 cur.idx() = tabular.getLastCellInRow(0);
3966 if (isRightToLeft(cur))
3967 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3969 cur.idx() = tabular.numberofcells - 1;
3971 cur.pos() = cur.lastpos(); // FIXME crude guess
3973 cur.setCurrentFont();
3974 // FIXME: this accesses the position cache before it is initialized
3975 //cur.bv().fitCursor();
3979 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3981 // In a longtable, tell captions what the current float is
3982 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3983 string const saveflt = cnts.current_float();
3984 if (tabular.is_long_tabular) {
3985 cnts.current_float("table");
3986 // in longtables, we only step the counter once
3987 cnts.step(from_ascii("table"), utype);
3988 cnts.isLongtable(true);
3991 ParIterator it2 = it;
3993 size_t const end = it2.nargs();
3994 for ( ; it2.idx() < end; it2.top().forwardIdx())
3995 buffer().updateBuffer(it2, utype);
3998 if (tabular.is_long_tabular) {
3999 cnts.current_float(saveflt);
4000 cnts.isLongtable(false);
4005 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
4006 UpdateType utype, TocBackend & backend) const
4008 DocIterator dit = cpit;
4010 size_t const end = dit.nargs();
4011 for ( ; dit.idx() < end; dit.top().forwardIdx())
4012 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
4016 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
4018 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
4019 return x < x0 || x > x0 + tabular.width();
4023 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4025 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4026 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4027 // there is no margin added vertically to tabular insets.
4028 // However, it works for now.
4029 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4033 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4035 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4039 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4041 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4042 << "\n cur:" << cur);
4043 CursorSlice sl = cur.top();
4044 Cursor & bvcur = cur.bv().cursor();
4046 FuncCode const act = cmd.action();
4050 case LFUN_MOUSE_PRESS: {
4051 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4053 if (hitSelectRow(cur.bv(), cmd.x())) {
4054 row_type r = rowFromY(cur, cmd.y());
4055 cur.idx() = tabular.getFirstCellInRow(r);
4059 cur.idx() = tabular.getLastCellInRow(r);
4060 cur.pit() = cur.lastpit();
4061 cur.pos() = cur.lastpos();
4062 cur.selection(true);
4068 if (hitSelectColumn(cur.bv(), cmd.y())) {
4069 col_type c = columnFromX(cur, cmd.x());
4070 cur.idx() = tabular.cellIndex(0, c);
4074 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4075 cur.pit() = cur.lastpit();
4076 cur.pos() = cur.lastpos();
4077 cur.selection(true);
4082 // do not reset cursor/selection if we have selected
4083 // some cells (bug 2715).
4084 if (cmd.button() == mouse_button::button3
4085 && &bvcur.selBegin().inset() == this
4086 && bvcur.selIsMultiCell())
4089 // Let InsetTableCell do it
4090 cell(cur.idx())->dispatch(cur, cmd);
4093 case LFUN_MOUSE_MOTION:
4094 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4095 if (cmd.button() == mouse_button::button1) {
4096 // only accept motions to places not deeper nested than the real anchor
4097 if (!bvcur.realAnchor().hasPart(cur)) {
4101 // select (additional) row
4103 row_type r = rowFromY(cur, cmd.y());
4104 cur.idx() = tabular.getLastCellInRow(r);
4105 // we need to reset the cursor's pit and pos now, as the old ones
4106 // may no longer be valid.
4109 bvcur.setCursor(cur);
4110 bvcur.selection(true);
4113 // select (additional) column
4115 col_type c = columnFromX(cur, cmd.x());
4116 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4117 // we need to reset the cursor's pit and pos now, as the old ones
4118 // may no longer be valid.
4121 bvcur.setCursor(cur);
4122 bvcur.selection(true);
4125 // only update if selection changes
4126 if (bvcur.idx() == cur.idx() &&
4127 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4128 cur.noScreenUpdate();
4129 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4130 bvcur.setCursor(cur);
4131 bvcur.selection(true);
4132 // if this is a multicell selection, we just set the cursor to
4133 // the beginning of the cell's text.
4134 if (bvcur.selIsMultiCell()) {
4135 bvcur.pit() = bvcur.lastpit();
4136 bvcur.pos() = bvcur.lastpos();
4141 case LFUN_MOUSE_RELEASE:
4146 case LFUN_CELL_BACKWARD:
4148 cur.selection(false);
4151 case LFUN_CELL_FORWARD:
4153 cur.selection(false);
4156 case LFUN_CHAR_FORWARD_SELECT:
4157 case LFUN_CHAR_FORWARD:
4158 case LFUN_CHAR_BACKWARD_SELECT:
4159 case LFUN_CHAR_BACKWARD:
4160 case LFUN_CHAR_RIGHT_SELECT:
4161 case LFUN_CHAR_RIGHT:
4162 case LFUN_CHAR_LEFT_SELECT:
4163 case LFUN_CHAR_LEFT:
4164 case LFUN_WORD_FORWARD:
4165 case LFUN_WORD_FORWARD_SELECT:
4166 case LFUN_WORD_BACKWARD:
4167 case LFUN_WORD_BACKWARD_SELECT:
4168 case LFUN_WORD_RIGHT:
4169 case LFUN_WORD_RIGHT_SELECT:
4170 case LFUN_WORD_LEFT:
4171 case LFUN_WORD_LEFT_SELECT: {
4172 // determine whether we move to next or previous cell, where to enter
4173 // the new cell from, and which command to "finish" (i.e., exit the
4176 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4177 FuncCode finish_lfun;
4179 if (act == LFUN_CHAR_FORWARD
4180 || act == LFUN_CHAR_FORWARD_SELECT
4181 || act == LFUN_WORD_FORWARD
4182 || act == LFUN_WORD_FORWARD_SELECT) {
4184 finish_lfun = LFUN_FINISHED_FORWARD;
4186 else if (act == LFUN_CHAR_BACKWARD
4187 || act == LFUN_CHAR_BACKWARD_SELECT
4188 || act == LFUN_WORD_BACKWARD
4189 || act == LFUN_WORD_BACKWARD_SELECT) {
4191 finish_lfun = LFUN_FINISHED_BACKWARD;
4193 // LEFT or RIGHT commands --- the interpretation will depend on the
4194 // table's direction.
4196 bool const right = act == LFUN_CHAR_RIGHT
4197 || act == LFUN_CHAR_RIGHT_SELECT
4198 || act == LFUN_WORD_RIGHT
4199 || act == LFUN_WORD_RIGHT_SELECT;
4200 next_cell = isRightToLeft(cur) != right;
4202 if (lyxrc.visual_cursor)
4203 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4205 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4208 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4209 || act == LFUN_CHAR_BACKWARD_SELECT
4210 || act == LFUN_CHAR_RIGHT_SELECT
4211 || act == LFUN_CHAR_LEFT_SELECT
4212 || act == LFUN_WORD_FORWARD_SELECT
4213 || act == LFUN_WORD_RIGHT_SELECT
4214 || act == LFUN_WORD_BACKWARD_SELECT
4215 || act == LFUN_WORD_LEFT_SELECT;
4217 // If we have a multicell selection or we're
4218 // not doing some LFUN_*_SELECT thing anyway...
4219 if (!cur.selIsMultiCell() || !select) {
4220 col_type const c = tabular.cellColumn(cur.idx());
4221 row_type const r = tabular.cellRow(cur.idx());
4222 // Are we trying to select the whole cell and is the whole cell
4223 // not yet selected?
4224 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4225 ((next_cell && cur.pit() == cur.lastpit()
4226 && cur.pos() == cur.lastpos())
4227 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4229 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4231 // ...try to dispatch to the cell's inset.
4232 cell(cur.idx())->dispatch(cur, cmd);
4234 // When we already have a selection we want to select the whole cell
4235 // before going to the next cell.
4236 if (select_whole && !empty_cell){
4237 getText(cur.idx())->selectAll(cur);
4239 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4243 // FIXME: When we support the selection of an empty cell, remove
4244 // the !empty_cell from this condition. For now we jump to the next
4245 // cell if the current cell is empty.
4246 if (cur.result().dispatched() && !empty_cell)
4250 // move to next/prev cell, as appropriate
4251 // note that we will always do this if we're selecting and we have
4252 // a multicell selection
4253 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4254 << " cell from: " << int(entry_from));
4256 moveNextCell(cur, entry_from);
4258 movePrevCell(cur, entry_from);
4259 // if we're exiting the table, call the appropriate FINISHED lfun
4260 if (sl == cur.top()) {
4261 cmd = FuncRequest(finish_lfun);
4266 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4271 case LFUN_DOWN_SELECT:
4273 if (!(cur.selection() && cur.selIsMultiCell()))
4274 cell(cur.idx())->dispatch(cur, cmd);
4276 cur.dispatched(); // override the cell's decision
4277 if (sl == cur.top()) {
4278 // if our Text didn't do anything to the cursor
4279 // then we try to put the cursor into the cell below
4280 // setting also the right targetX.
4281 cur.selHandle(act == LFUN_DOWN_SELECT);
4282 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4283 int const xtarget = cur.targetX();
4284 // WARNING: Once cur.idx() has been reset, the cursor is in
4285 // an inconsistent state until pos() has been set. Be careful
4286 // what you do with it!
4287 cur.idx() = tabular.cellBelow(cur.idx());
4289 TextMetrics const & tm =
4290 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4291 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4292 cur.setCurrentFont();
4295 if (sl == cur.top()) {
4296 // we trick it to go to forward after leaving the
4298 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4301 if (cur.selIsMultiCell()) {
4302 cur.pit() = cur.lastpit();
4303 cur.pos() = cur.lastpos();
4304 cur.setCurrentFont();
4305 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4308 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4311 case LFUN_UP_SELECT:
4313 if (!(cur.selection() && cur.selIsMultiCell()))
4314 cell(cur.idx())->dispatch(cur, cmd);
4315 cur.dispatched(); // override the cell's decision
4316 if (sl == cur.top()) {
4317 // if our Text didn't do anything to the cursor
4318 // then we try to put the cursor into the cell above
4319 // setting also the right targetX.
4320 cur.selHandle(act == LFUN_UP_SELECT);
4321 if (tabular.cellRow(cur.idx()) != 0) {
4322 int const xtarget = cur.targetX();
4323 // WARNING: Once cur.idx() has been reset, the cursor is in
4324 // an inconsistent state until pos() has been set. Be careful
4325 // what you do with it!
4326 cur.idx() = tabular.cellAbove(cur.idx());
4327 cur.pit() = cur.lastpit();
4328 Text const * text = cell(cur.idx())->getText(0);
4329 TextMetrics const & tm = cur.bv().textMetrics(text);
4330 ParagraphMetrics const & pm =
4331 tm.parMetrics(cur.lastpit());
4332 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4333 cur.setCurrentFont();
4336 if (sl == cur.top()) {
4337 cmd = FuncRequest(LFUN_UP);
4340 if (cur.selIsMultiCell()) {
4342 cur.pos() = cur.lastpos();
4343 cur.setCurrentFont();
4344 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4347 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4350 case LFUN_LAYOUT_TABULAR:
4351 cur.bv().showDialog("tabular");
4354 case LFUN_INSET_MODIFY:
4355 // we come from the dialog
4356 if (cmd.getArg(0) == "tabular")
4357 tabularFeatures(cur, cmd.getLongArg(1));
4362 case LFUN_TABULAR_FEATURE:
4363 tabularFeatures(cur, to_utf8(cmd.argument()));
4366 // insert file functions
4367 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4368 case LFUN_FILE_INSERT_PLAINTEXT:
4370 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4371 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4372 FileName(to_utf8(cmd.argument())));
4375 cur.recordUndoInset();
4376 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4377 // content has been replaced,
4378 // so cursor might be invalid
4379 cur.pos() = cur.lastpos();
4380 cur.pit() = cur.lastpit();
4381 bvcur.setCursor(cur);
4388 if (cur.selIsMultiCell()) {
4389 if (copySelection(cur)) {
4390 cur.recordUndoInset();
4394 cell(cur.idx())->dispatch(cur, cmd);
4397 case LFUN_SELF_INSERT:
4398 if (cur.selIsMultiCell()) {
4399 cur.recordUndoInset();
4401 BufferView * bv = &cur.bv();
4402 docstring::const_iterator cit = cmd.argument().begin();
4403 docstring::const_iterator const end = cmd.argument().end();
4404 for (; cit != end; ++cit)
4405 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4408 bv->bookmarkEditPosition();
4410 cell(cur.idx())->dispatch(cur, cmd);
4413 case LFUN_CHAR_DELETE_BACKWARD:
4414 case LFUN_CHAR_DELETE_FORWARD:
4415 if (cur.selIsMultiCell()) {
4416 cur.recordUndoInset();
4419 cell(cur.idx())->dispatch(cur, cmd);
4423 if (!cur.selection())
4425 if (cur.selIsMultiCell()) {
4429 cell(cur.idx())->dispatch(cur, cmd);
4432 case LFUN_CLIPBOARD_PASTE:
4433 case LFUN_PRIMARY_SELECTION_PASTE: {
4434 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4435 theClipboard().getAsText(Clipboard::PlainTextType) :
4436 theSelection().get();
4439 // pass to InsertPlaintextString, but
4440 // only if we have multi-cell content
4441 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4442 cur.recordUndoInset();
4443 if (insertPlaintextString(cur.bv(), clip, false)) {
4444 // content has been replaced,
4445 // so cursor might be invalid
4446 cur.pos() = cur.lastpos();
4447 cur.pit() = cur.lastpit();
4448 bvcur.setCursor(cur);
4452 // Let the cell handle normal text
4453 cell(cur.idx())->dispatch(cur, cmd);
4458 if (!tabularStackDirty()) {
4459 if (!cur.selIsMultiCell())
4460 cell(cur.idx())->dispatch(cur, cmd);
4463 if (theClipboard().isInternal()) {
4464 cur.recordUndoInset();
4465 pasteClipboard(cur);
4469 case LFUN_FONT_EMPH:
4470 case LFUN_FONT_BOLD:
4471 case LFUN_FONT_BOLDSYMBOL:
4472 case LFUN_FONT_ROMAN:
4473 case LFUN_FONT_NOUN:
4474 case LFUN_FONT_ITAL:
4475 case LFUN_FONT_FRAK:
4476 case LFUN_FONT_TYPEWRITER:
4477 case LFUN_FONT_SANS:
4478 case LFUN_TEXTSTYLE_APPLY:
4479 case LFUN_TEXTSTYLE_UPDATE:
4480 case LFUN_FONT_SIZE:
4481 case LFUN_FONT_UNDERLINE:
4482 case LFUN_FONT_STRIKEOUT:
4483 case LFUN_FONT_CROSSOUT:
4484 case LFUN_FONT_UNDERUNDERLINE:
4485 case LFUN_FONT_UNDERWAVE:
4487 case LFUN_PARAGRAPH_PARAMS_APPLY:
4488 case LFUN_PARAGRAPH_PARAMS:
4489 case LFUN_WORD_CAPITALIZE:
4490 case LFUN_WORD_UPCASE:
4491 case LFUN_WORD_LOWCASE:
4492 case LFUN_CHARS_TRANSPOSE:
4493 if (cur.selIsMultiCell()) {
4496 getSelection(cur, rs, re, cs, ce);
4497 Cursor tmpcur = cur;
4498 for (row_type r = rs; r <= re; ++r) {
4499 for (col_type c = cs; c <= ce; ++c) {
4500 // cursor follows cell:
4501 tmpcur.idx() = tabular.cellIndex(r, c);
4502 // select this cell only:
4505 tmpcur.resetAnchor();
4506 tmpcur.pit() = tmpcur.lastpit();
4507 tmpcur.pos() = tmpcur.top().lastpos();
4508 tmpcur.setCursor(tmpcur);
4509 tmpcur.setSelection();
4510 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4515 cell(cur.idx())->dispatch(cur, cmd);
4519 case LFUN_INSET_SETTINGS:
4520 // relay this lfun to Inset, not to the cell.
4521 Inset::doDispatch(cur, cmd);
4525 // we try to handle this event in the insets dispatch function.
4526 cell(cur.idx())->dispatch(cur, cmd);
4532 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4533 string const & argument, FuncStatus & status) const
4536 int action = Tabular::LAST_ACTION;
4538 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4539 if (tabularFeature[i].feature == s) {
4540 action = tabularFeature[i].action;
4544 if (action == Tabular::LAST_ACTION) {
4546 status.setUnknown(true);
4550 row_type sel_row_start = 0;
4551 row_type sel_row_end = 0;
4552 col_type sel_col_start = 0;
4553 col_type sel_col_end = 0;
4554 Tabular::ltType dummyltt;
4557 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4560 case Tabular::SET_PWIDTH:
4561 case Tabular::SET_MPWIDTH:
4562 case Tabular::SET_SPECIAL_COLUMN:
4563 case Tabular::SET_SPECIAL_MULTICOLUMN:
4564 case Tabular::APPEND_ROW:
4565 case Tabular::APPEND_COLUMN:
4566 case Tabular::DELETE_ROW:
4567 case Tabular::DELETE_COLUMN:
4568 case Tabular::COPY_ROW:
4569 case Tabular::COPY_COLUMN:
4570 case Tabular::SET_TOP_SPACE:
4571 case Tabular::SET_BOTTOM_SPACE:
4572 case Tabular::SET_INTERLINE_SPACE:
4576 case Tabular::SET_TABULAR_WIDTH:
4577 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4578 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4581 case Tabular::MOVE_COLUMN_RIGHT:
4582 case Tabular::MOVE_COLUMN_LEFT:
4583 case Tabular::MOVE_ROW_DOWN:
4584 case Tabular::MOVE_ROW_UP: {
4585 if (cur.selection()) {
4586 status.message(_("Selections not supported."));
4587 status.setEnabled(false);
4591 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4592 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4593 (action == Tabular::MOVE_COLUMN_LEFT &&
4594 tabular.cellColumn(cur.idx()) == 0) ||
4595 (action == Tabular::MOVE_ROW_DOWN &&
4596 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4597 (action == Tabular::MOVE_ROW_UP &&
4598 tabular.cellRow(cur.idx()) == 0)) {
4599 status.setEnabled(false);
4603 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4604 action == Tabular::MOVE_COLUMN_LEFT) {
4605 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4606 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4607 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4608 status.message(_("Multi-column in current or"
4609 " destination column."));
4610 status.setEnabled(false);
4615 if (action == Tabular::MOVE_ROW_DOWN ||
4616 action == Tabular::MOVE_ROW_UP) {
4617 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4618 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4619 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4620 status.message(_("Multi-row in current or"
4621 " destination row."));
4622 status.setEnabled(false);
4627 status.setEnabled(true);
4631 case Tabular::SET_DECIMAL_POINT:
4633 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4636 case Tabular::SET_MULTICOLUMN:
4637 case Tabular::UNSET_MULTICOLUMN:
4638 case Tabular::MULTICOLUMN:
4639 // If a row is set as longtable caption, it must not be allowed
4640 // to unset that this row is a multicolumn.
4641 status.setEnabled(sel_row_start == sel_row_end
4642 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4643 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4646 case Tabular::SET_MULTIROW:
4647 case Tabular::UNSET_MULTIROW:
4648 case Tabular::MULTIROW:
4649 // If a row is set as longtable caption, it must not be allowed
4650 // to unset that this row is a multirow.
4651 status.setEnabled(sel_col_start == sel_col_end
4652 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4653 status.setOnOff(tabular.isMultiRow(cur.idx()));
4656 case Tabular::SET_ALL_LINES:
4657 case Tabular::UNSET_ALL_LINES:
4658 case Tabular::SET_BORDER_LINES:
4659 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4662 case Tabular::SET_LINE_TOP:
4663 case Tabular::SET_LINE_BOTTOM:
4664 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4667 case Tabular::SET_LINE_LEFT:
4668 case Tabular::SET_LINE_RIGHT:
4669 status.setEnabled(!tabular.use_booktabs
4670 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4673 case Tabular::TOGGLE_LINE_TOP:
4674 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4675 status.setOnOff(tabular.topLine(cur.idx()));
4678 case Tabular::TOGGLE_LINE_BOTTOM:
4679 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4680 status.setOnOff(tabular.bottomLine(cur.idx()));
4683 case Tabular::TOGGLE_LINE_LEFT:
4684 status.setEnabled(!tabular.use_booktabs
4685 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4686 status.setOnOff(tabular.leftLine(cur.idx()));
4689 case Tabular::TOGGLE_LINE_RIGHT:
4690 status.setEnabled(!tabular.use_booktabs
4691 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4692 status.setOnOff(tabular.rightLine(cur.idx()));
4695 // multirow cells only inherit the alignment of the column if the column has
4696 // no width, otherwise they are left-aligned
4697 // therefore allow always left but right and center only if there is no width
4698 case Tabular::M_ALIGN_LEFT:
4701 case Tabular::ALIGN_LEFT:
4702 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4705 case Tabular::M_ALIGN_RIGHT:
4708 case Tabular::ALIGN_RIGHT:
4709 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4710 && !tabular.getPWidth(cur.idx()).zero()));
4711 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4714 case Tabular::M_ALIGN_CENTER:
4717 case Tabular::ALIGN_CENTER:
4718 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4719 && !tabular.getPWidth(cur.idx()).zero()));
4720 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4723 case Tabular::ALIGN_BLOCK:
4724 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4725 && !tabular.isMultiRow(cur.idx()));
4726 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4729 case Tabular::ALIGN_DECIMAL:
4730 status.setEnabled(!tabular.isMultiRow(cur.idx())
4731 && !tabular.isMultiColumn(cur.idx()));
4732 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4735 case Tabular::M_VALIGN_TOP:
4738 case Tabular::VALIGN_TOP:
4739 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4740 && !tabular.isMultiRow(cur.idx()));
4742 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4745 case Tabular::M_VALIGN_BOTTOM:
4748 case Tabular::VALIGN_BOTTOM:
4749 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4750 && !tabular.isMultiRow(cur.idx()));
4752 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4755 case Tabular::M_VALIGN_MIDDLE:
4758 case Tabular::VALIGN_MIDDLE:
4759 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4760 && !tabular.isMultiRow(cur.idx()));
4762 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4765 case Tabular::SET_LONGTABULAR:
4766 case Tabular::TOGGLE_LONGTABULAR:
4767 // setting as longtable is not allowed when table is inside a float
4768 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4769 || cur.innerInsetOfType(WRAP_CODE) != 0)
4770 status.setEnabled(false);
4772 status.setEnabled(true);
4773 status.setOnOff(tabular.is_long_tabular);
4776 case Tabular::UNSET_LONGTABULAR:
4777 status.setOnOff(!tabular.is_long_tabular);
4780 case Tabular::TOGGLE_ROTATE_TABULAR:
4781 case Tabular::SET_ROTATE_TABULAR:
4782 status.setOnOff(tabular.rotate != 0);
4785 case Tabular::TABULAR_VALIGN_TOP:
4786 status.setEnabled(tabular.tabular_width.zero());
4787 status.setOnOff(tabular.tabular_valignment
4788 == Tabular::LYX_VALIGN_TOP);
4790 case Tabular::TABULAR_VALIGN_MIDDLE:
4791 status.setEnabled(tabular.tabular_width.zero());
4792 status.setOnOff(tabular.tabular_valignment
4793 == Tabular::LYX_VALIGN_MIDDLE);
4795 case Tabular::TABULAR_VALIGN_BOTTOM:
4796 status.setEnabled(tabular.tabular_width.zero());
4797 status.setOnOff(tabular.tabular_valignment
4798 == Tabular::LYX_VALIGN_BOTTOM);
4801 case Tabular::LONGTABULAR_ALIGN_LEFT:
4802 status.setOnOff(tabular.longtabular_alignment
4803 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4805 case Tabular::LONGTABULAR_ALIGN_CENTER:
4806 status.setOnOff(tabular.longtabular_alignment
4807 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4809 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4810 status.setOnOff(tabular.longtabular_alignment
4811 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4814 case Tabular::UNSET_ROTATE_TABULAR:
4815 status.setOnOff(tabular.rotate == 0);
4818 case Tabular::TOGGLE_ROTATE_CELL:
4819 case Tabular::SET_ROTATE_CELL:
4820 status.setOnOff(!oneCellHasRotationState(false,
4821 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4824 case Tabular::UNSET_ROTATE_CELL:
4825 status.setOnOff(!oneCellHasRotationState(true,
4826 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4829 case Tabular::SET_USEBOX:
4830 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4833 // every row can only be one thing:
4834 // either a footer or header
4835 case Tabular::SET_LTFIRSTHEAD:
4836 status.setEnabled(sel_row_start == sel_row_end);
4837 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4840 case Tabular::UNSET_LTFIRSTHEAD:
4841 status.setEnabled(sel_row_start == sel_row_end);
4842 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4845 case Tabular::SET_LTHEAD:
4846 status.setEnabled(sel_row_start == sel_row_end);
4847 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4850 case Tabular::UNSET_LTHEAD:
4851 status.setEnabled(sel_row_start == sel_row_end);
4852 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4855 case Tabular::SET_LTFOOT:
4856 status.setEnabled(sel_row_start == sel_row_end);
4857 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4860 case Tabular::UNSET_LTFOOT:
4861 status.setEnabled(sel_row_start == sel_row_end);
4862 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4865 case Tabular::SET_LTLASTFOOT:
4866 status.setEnabled(sel_row_start == sel_row_end);
4867 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4870 case Tabular::UNSET_LTLASTFOOT:
4871 status.setEnabled(sel_row_start == sel_row_end);
4872 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4875 case Tabular::SET_LTNEWPAGE:
4876 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4878 case Tabular::UNSET_LTNEWPAGE:
4879 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4882 // only one row in head/firsthead/foot/lasthead can be the caption
4883 // and a multirow cannot be set as caption
4884 case Tabular::SET_LTCAPTION:
4885 status.setEnabled(sel_row_start == sel_row_end
4886 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4887 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4888 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4889 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4890 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4891 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4892 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4893 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4894 && !tabular.isMultiRow(sel_row_start));
4895 status.setOnOff(tabular.ltCaption(sel_row_start));
4898 case Tabular::UNSET_LTCAPTION:
4899 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4902 case Tabular::TOGGLE_LTCAPTION:
4903 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4904 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4905 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4906 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4907 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4908 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4909 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4910 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4911 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4912 status.setOnOff(tabular.ltCaption(sel_row_start));
4915 case Tabular::TOGGLE_BOOKTABS:
4916 case Tabular::SET_BOOKTABS:
4917 status.setOnOff(tabular.use_booktabs);
4920 case Tabular::UNSET_BOOKTABS:
4921 status.setOnOff(!tabular.use_booktabs);
4926 status.setEnabled(false);
4933 // function sets an object as defined in FuncStatus.h:
4934 // states OK, Unknown, Disabled, On, Off.
4935 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4936 FuncStatus & status) const
4938 switch (cmd.action()) {
4939 case LFUN_INSET_MODIFY:
4940 if (cmd.getArg(0) != "tabular")
4942 if (cmd.getArg(1) == "for-dialog") {
4943 // The dialog is asking the status of a command
4944 if (&cur.inset() != this)
4946 string action = cmd.getArg(2);
4947 string arg = cmd.getLongArg(3);
4948 return getFeatureStatus(cur, action, arg, status);
4950 // We always enable the lfun if it is coming from the dialog
4951 // because the dialog makes sure all the settings are valid,
4952 // even though the first argument might not be valid now.
4953 status.setEnabled(true);
4957 case LFUN_TABULAR_FEATURE: {
4958 if (&cur.inset() != this)
4960 string action = cmd.getArg(0);
4961 string arg = cmd.getLongArg(1);
4962 return getFeatureStatus(cur, action, arg, status);
4965 case LFUN_CAPTION_INSERT: {
4966 // caption is only allowed in caption cell of longtable
4967 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4968 status.setEnabled(false);
4971 // only standard caption is allowed
4972 string arg = cmd.getArg(0);
4973 if (!arg.empty() && arg != "Standard") {
4974 status.setEnabled(false);
4977 // check if there is already a caption
4978 bool have_caption = false;
4979 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
4980 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4981 ParagraphList::const_iterator pend = itc.paragraphs().end();
4982 for (; pit != pend; ++pit) {
4983 InsetList::const_iterator it = pit->insetList().begin();
4984 InsetList::const_iterator end = pit->insetList().end();
4985 for (; it != end; ++it) {
4986 if (it->inset->lyxCode() == CAPTION_CODE) {
4987 have_caption = true;
4992 status.setEnabled(!have_caption);
4996 // These are only enabled inside tabular
4997 case LFUN_CELL_BACKWARD:
4998 case LFUN_CELL_FORWARD:
4999 status.setEnabled(true);
5002 // disable these with multiple cells selected
5003 case LFUN_INSET_INSERT:
5004 case LFUN_TABULAR_INSERT:
5005 case LFUN_FLEX_INSERT:
5006 case LFUN_FLOAT_INSERT:
5007 case LFUN_FLOAT_WIDE_INSERT:
5008 case LFUN_FOOTNOTE_INSERT:
5009 case LFUN_MARGINALNOTE_INSERT:
5010 case LFUN_MATH_INSERT:
5011 case LFUN_MATH_MODE:
5012 case LFUN_MATH_MUTATE:
5013 case LFUN_MATH_DISPLAY:
5014 case LFUN_NOTE_INSERT:
5015 case LFUN_ARGUMENT_INSERT:
5016 case LFUN_BOX_INSERT:
5017 case LFUN_BRANCH_INSERT:
5018 case LFUN_PHANTOM_INSERT:
5019 case LFUN_WRAP_INSERT:
5020 case LFUN_PREVIEW_INSERT:
5021 case LFUN_ERT_INSERT: {
5022 if (cur.selIsMultiCell()) {
5023 status.setEnabled(false);
5026 return cell(cur.idx())->getStatus(cur, cmd, status);
5029 // disable in non-fixed-width cells
5030 case LFUN_PARAGRAPH_BREAK:
5031 // multirow does not allow paragraph breaks
5032 if (tabular.isMultiRow(cur.idx())) {
5033 status.setEnabled(false);
5037 case LFUN_NEWLINE_INSERT: {
5038 if (tabular.getPWidth(cur.idx()).zero()) {
5039 status.setEnabled(false);
5042 return cell(cur.idx())->getStatus(cur, cmd, status);
5045 case LFUN_NEWPAGE_INSERT:
5046 status.setEnabled(false);
5050 if (tabularStackDirty() && theClipboard().isInternal()) {
5051 if (cur.selIsMultiCell()) {
5054 getSelection(cur, rs, re, cs, ce);
5055 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5056 && paste_tabular->nrows() == re - rs + 1)
5057 status.setEnabled(true);
5059 status.setEnabled(false);
5060 status.message(_("Selection size should match clipboard content."));
5063 status.setEnabled(true);
5066 return cell(cur.idx())->getStatus(cur, cmd, status);
5068 case LFUN_INSET_SETTINGS:
5069 // relay this lfun to Inset, not to the cell.
5070 return Inset::getStatus(cur, cmd, status);
5073 // we try to handle this event in the insets dispatch function.
5074 return cell(cur.idx())->getStatus(cur, cmd, status);
5080 Inset::DisplayType InsetTabular::display() const
5082 if (tabular.is_long_tabular) {
5083 switch (tabular.longtabular_alignment) {
5084 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5086 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5088 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5098 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5100 tabular.latex(os, runparams);
5104 int InsetTabular::plaintext(odocstringstream & os,
5105 OutputParams const & runparams, size_t max_length) const
5107 os << '\n'; // output table on a new line
5108 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5109 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5110 return PLAINTEXT_NEWLINE;
5114 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5119 // FIXME: Why not pass a proper DocIterator here?
5121 // if the table is inside a float it doesn't need the informaltable
5122 // wrapper. Search for it.
5123 for (master = owner(); master; master = master->owner())
5124 if (master->lyxCode() == FLOAT_CODE)
5129 os << "<informaltable>";
5132 ret += tabular.docbook(os, runparams);
5134 os << "</informaltable>";
5141 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5143 return tabular.xhtml(xs, rp);
5147 void InsetTabular::validate(LaTeXFeatures & features) const
5149 tabular.validate(features);
5150 features.useInsetLayout(getLayout());
5154 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5156 return tabular.cellInset(idx);
5160 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5162 return tabular.cellInset(idx);
5166 void InsetTabular::cursorPos(BufferView const & bv,
5167 CursorSlice const & sl, bool boundary, int & x, int & y) const
5169 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5171 // y offset correction
5172 y += cellYPos(sl.idx());
5173 y += tabular.textVOffset(sl.idx());
5174 y += tabular.offsetVAlignment();
5176 // x offset correction
5177 x += cellXPos(sl.idx());
5178 x += tabular.textHOffset(sl.idx());
5179 x += ADD_TO_TABULAR_WIDTH;
5183 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5187 Inset const & inset = *tabular.cellInset(cell);
5188 Point o = bv.coordCache().getInsets().xy(&inset);
5189 int const xbeg = o.x_ - tabular.textHOffset(cell);
5190 int const xend = xbeg + tabular.cellWidth(cell);
5191 row_type const row = tabular.cellRow(cell);
5192 int const ybeg = o.y_ - tabular.rowAscent(row)
5193 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5194 int const yend = ybeg + tabular.cellHeight(cell);
5206 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5207 // << " ybeg=" << ybeg << " yend=" << yend
5208 // << " xx=" << xx << " yy=" << yy
5209 // << " dist=" << xx + yy << endl;
5214 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5216 //lyxerr << "InsetTabular::editXY: " << this << endl;
5218 cur.idx() = getNearestCell(cur.bv(), x, y);
5219 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5223 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5225 cur.idx() = getNearestCell(cur.bv(), x, y);
5226 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5230 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5232 idx_type idx_min = 0;
5233 int dist_min = numeric_limits<int>::max();
5234 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5235 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5236 int const d = dist(bv, i, x, y);
5247 int InsetTabular::cellYPos(idx_type const cell) const
5249 row_type row = tabular.cellRow(cell);
5251 for (row_type r = 0; r < row; ++r)
5252 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5253 + tabular.interRowSpace(r + 1);
5258 int InsetTabular::cellXPos(idx_type const cell) const
5260 col_type col = tabular.cellColumn(cell);
5262 for (col_type c = 0; c < col; ++c)
5263 lx += tabular.column_info[c].width;
5268 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5270 row_type const row = tabular.cellRow(cur.idx());
5271 col_type const col = tabular.cellColumn(cur.idx());
5273 if (isRightToLeft(cur)) {
5274 if (tabular.cellColumn(cur.idx()) == 0) {
5275 if (row == tabular.nrows() - 1)
5277 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5282 cur.idx() = tabular.getLastCellInRow(row - 1);
5284 cur.idx() = tabular.cellIndex(row, col - 1);
5287 if (tabular.isLastCell(cur.idx()))
5289 if (cur.idx() == tabular.getLastCellInRow(row))
5290 cur.idx() = tabular.cellIndex(row + 1, 0);
5292 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5293 cur.idx() = tabular.cellIndex(row, colnextcell);
5297 cur.boundary(false);
5299 if (cur.selIsMultiCell()) {
5300 cur.pit() = cur.lastpit();
5301 cur.pos() = cur.lastpos();
5308 // in visual mode, place cursor at extreme left or right
5310 switch(entry_from) {
5312 case ENTRY_DIRECTION_RIGHT:
5313 cur.posVisToRowExtremity(false /* !left */);
5315 case ENTRY_DIRECTION_LEFT:
5316 cur.posVisToRowExtremity(true /* left */);
5318 case ENTRY_DIRECTION_IGNORE:
5319 // nothing to do in this case
5323 cur.setCurrentFont();
5327 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5329 row_type const row = tabular.cellRow(cur.idx());
5330 col_type const col = tabular.cellColumn(cur.idx());
5332 if (isRightToLeft(cur)) {
5333 if (cur.idx() == tabular.getLastCellInRow(row)) {
5336 cur.idx() = tabular.getFirstCellInRow(row);
5337 cur.idx() = tabular.cellAbove(cur.idx());
5339 if (tabular.isLastCell(cur.idx()))
5341 if (cur.idx() == tabular.getLastCellInRow(row))
5342 cur.idx() = tabular.cellIndex(row + 1, 0);
5344 cur.idx() = tabular.cellIndex(row, col + 1);
5347 if (cur.idx() == 0) // first cell
5350 cur.idx() = tabular.getLastCellInRow(row - 1);
5352 cur.idx() = tabular.cellIndex(row, col - 1);
5355 if (cur.selIsMultiCell()) {
5356 cur.pit() = cur.lastpit();
5357 cur.pos() = cur.lastpos();
5361 cur.pit() = cur.lastpit();
5362 cur.pos() = cur.lastpos();
5364 // in visual mode, place cursor at extreme left or right
5366 switch(entry_from) {
5368 case ENTRY_DIRECTION_RIGHT:
5369 cur.posVisToRowExtremity(false /* !left */);
5371 case ENTRY_DIRECTION_LEFT:
5372 cur.posVisToRowExtremity(true /* left */);
5374 case ENTRY_DIRECTION_IGNORE:
5375 // nothing to do in this case
5379 cur.setCurrentFont();
5383 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5385 cur.recordUndoInset(this);
5387 istringstream is(argument);
5388 // limit the size of strings we read to avoid memory problems
5392 size_t safe_guard = 0;
5397 if (safe_guard > 1000) {
5398 LYXERR0("parameter max count reached!");
5402 Tabular::Feature action = Tabular::LAST_ACTION;
5405 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5406 if (s != tabularFeature[i].feature)
5408 action = tabularFeature[i].action;
5411 if (action == Tabular::LAST_ACTION) {
5412 LYXERR0("Feature not found " << s);
5416 if (tabularFeature[i].need_value)
5418 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5419 tabularFeatures(cur, action, val);
5424 static void checkLongtableSpecial(Tabular::ltType & ltt,
5425 string const & special, bool & flag)
5427 if (special == "dl_above") {
5430 } else if (special == "dl_below") {
5431 ltt.bottomDL = flag;
5433 } else if (special == "empty") {
5443 bool InsetTabular::oneCellHasRotationState(bool rotated,
5444 row_type row_start, row_type row_end,
5445 col_type col_start, col_type col_end) const
5447 for (row_type r = row_start; r <= row_end; ++r)
5448 for (col_type c = col_start; c <= col_end; ++c)
5450 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5453 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5460 void InsetTabular::tabularFeatures(Cursor & cur,
5461 Tabular::Feature feature, string const & value)
5463 col_type sel_col_start;
5464 col_type sel_col_end;
5465 row_type sel_row_start;
5466 row_type sel_row_end;
5467 bool setLines = false;
5468 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5469 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5473 case Tabular::M_ALIGN_LEFT:
5474 case Tabular::ALIGN_LEFT:
5475 setAlign = LYX_ALIGN_LEFT;
5478 case Tabular::M_ALIGN_RIGHT:
5479 case Tabular::ALIGN_RIGHT:
5480 setAlign = LYX_ALIGN_RIGHT;
5483 case Tabular::M_ALIGN_CENTER:
5484 case Tabular::ALIGN_CENTER:
5485 setAlign = LYX_ALIGN_CENTER;
5488 case Tabular::ALIGN_BLOCK:
5489 setAlign = LYX_ALIGN_BLOCK;
5492 case Tabular::ALIGN_DECIMAL:
5493 setAlign = LYX_ALIGN_DECIMAL;
5496 case Tabular::M_VALIGN_TOP:
5497 case Tabular::VALIGN_TOP:
5498 setVAlign = Tabular::LYX_VALIGN_TOP;
5501 case Tabular::M_VALIGN_BOTTOM:
5502 case Tabular::VALIGN_BOTTOM:
5503 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5506 case Tabular::M_VALIGN_MIDDLE:
5507 case Tabular::VALIGN_MIDDLE:
5508 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5515 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5516 row_type const row = tabular.cellRow(cur.idx());
5517 col_type const column = tabular.cellColumn(cur.idx());
5519 Tabular::ltType ltt;
5523 case Tabular::SET_TABULAR_WIDTH:
5524 tabular.setTabularWidth(Length(value));
5527 case Tabular::SET_PWIDTH: {
5528 Length const len(value);
5529 tabular.setColumnPWidth(cur, cur.idx(), len);
5531 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5532 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5536 case Tabular::SET_MPWIDTH:
5537 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5540 case Tabular::SET_MROFFSET:
5541 tabular.setMROffset(cur, cur.idx(), Length(value));
5544 case Tabular::SET_SPECIAL_COLUMN:
5545 case Tabular::SET_SPECIAL_MULTICOLUMN:
5546 if (value == "none")
5547 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5549 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5552 case Tabular::APPEND_ROW:
5553 // append the row into the tabular
5554 tabular.appendRow(row);
5557 case Tabular::APPEND_COLUMN:
5558 // append the column into the tabular
5559 tabular.appendColumn(column);
5560 cur.idx() = tabular.cellIndex(row, column);
5563 case Tabular::DELETE_ROW:
5564 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5565 for (col_type c = 0; c < tabular.ncols(); c++)
5566 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5567 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5570 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5571 tabular.deleteRow(sel_row_start);
5572 if (sel_row_start >= tabular.nrows())
5574 cur.idx() = tabular.cellIndex(sel_row_start, column);
5577 cur.selection(false);
5580 case Tabular::DELETE_COLUMN:
5581 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5582 for (row_type r = 0; r < tabular.nrows(); r++)
5583 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5584 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5587 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5588 for (row_type r = 0; r < tabular.nrows(); r++)
5589 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5590 tabular.leftLine(tabular.cellIndex(r, 0)));
5593 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5594 tabular.deleteColumn(sel_col_start);
5595 if (sel_col_start >= tabular.ncols())
5597 cur.idx() = tabular.cellIndex(row, sel_col_start);
5600 cur.selection(false);
5603 case Tabular::COPY_ROW:
5604 tabular.copyRow(row);
5607 case Tabular::COPY_COLUMN:
5608 tabular.copyColumn(column);
5609 cur.idx() = tabular.cellIndex(row, column);
5612 case Tabular::MOVE_COLUMN_RIGHT:
5613 tabular.moveColumn(column, Tabular::RIGHT);
5614 cur.idx() = tabular.cellIndex(row, column + 1);
5617 case Tabular::MOVE_COLUMN_LEFT:
5618 tabular.moveColumn(column, Tabular::LEFT);
5619 cur.idx() = tabular.cellIndex(row, column - 1);
5622 case Tabular::MOVE_ROW_DOWN:
5623 tabular.moveRow(row, Tabular::DOWN);
5624 cur.idx() = tabular.cellIndex(row + 1, column);
5627 case Tabular::MOVE_ROW_UP:
5628 tabular.moveRow(row, Tabular::UP);
5629 cur.idx() = tabular.cellIndex(row - 1, column);
5632 case Tabular::SET_LINE_TOP:
5633 case Tabular::TOGGLE_LINE_TOP: {
5634 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5635 ? (value == "true") : !tabular.topLine(cur.idx());
5636 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5637 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5638 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5642 case Tabular::SET_LINE_BOTTOM:
5643 case Tabular::TOGGLE_LINE_BOTTOM: {
5644 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5645 ? (value == "true") : !tabular.bottomLine(cur.idx());
5646 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5647 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5648 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5652 case Tabular::SET_LINE_LEFT:
5653 case Tabular::TOGGLE_LINE_LEFT: {
5654 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5655 ? (value == "true") : !tabular.leftLine(cur.idx());
5656 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5657 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5658 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5662 case Tabular::SET_LINE_RIGHT:
5663 case Tabular::TOGGLE_LINE_RIGHT: {
5664 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5665 ? (value == "true") : !tabular.rightLine(cur.idx());
5666 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5667 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5668 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5672 case Tabular::M_ALIGN_LEFT:
5673 case Tabular::M_ALIGN_RIGHT:
5674 case Tabular::M_ALIGN_CENTER:
5675 case Tabular::ALIGN_LEFT:
5676 case Tabular::ALIGN_RIGHT:
5677 case Tabular::ALIGN_CENTER:
5678 case Tabular::ALIGN_BLOCK:
5679 case Tabular::ALIGN_DECIMAL:
5680 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5681 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5682 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5683 !tabular.getPWidth(c).zero());
5686 case Tabular::M_VALIGN_TOP:
5687 case Tabular::M_VALIGN_BOTTOM:
5688 case Tabular::M_VALIGN_MIDDLE:
5691 case Tabular::VALIGN_TOP:
5692 case Tabular::VALIGN_BOTTOM:
5693 case Tabular::VALIGN_MIDDLE:
5694 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5695 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5696 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5699 case Tabular::SET_MULTICOLUMN: {
5700 if (!cur.selection()) {
5701 // just multicol for one single cell
5702 // check whether we are completely in a multicol
5703 if (!tabular.isMultiColumn(cur.idx()))
5704 tabular.setMultiColumn(cur.idx(), 1,
5705 tabular.rightLine(cur.idx()));
5708 // we have a selection so this means we just add all these
5709 // cells to form a multicolumn cell
5710 idx_type const s_start = cur.selBegin().idx();
5711 row_type const col_start = tabular.cellColumn(s_start);
5712 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5713 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5714 tabular.rightLine(cur.selEnd().idx()));
5717 cur.selection(false);
5721 case Tabular::UNSET_MULTICOLUMN: {
5722 if (!cur.selection()) {
5723 if (tabular.isMultiColumn(cur.idx()))
5724 tabular.unsetMultiColumn(cur.idx());
5729 case Tabular::MULTICOLUMN: {
5730 if (!cur.selection()) {
5731 if (tabular.isMultiColumn(cur.idx()))
5732 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5734 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5738 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5739 row_type const r = sel_row_start;
5740 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5741 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5744 // If the selection contains at least one singlecol cell
5745 // or multiple multicol cells,
5746 // we assume the user will merge is to a single multicol
5748 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5750 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5754 case Tabular::SET_MULTIROW: {
5755 if (!cur.selection()) {
5756 // just multirow for one single cell
5757 // check whether we are completely in a multirow
5758 if (!tabular.isMultiRow(cur.idx()))
5759 tabular.setMultiRow(cur.idx(), 1,
5760 tabular.bottomLine(cur.idx()),
5761 tabular.getAlignment(cur.idx()));
5764 // we have a selection so this means we just add all this
5765 // cells to form a multirow cell
5766 idx_type const s_start = cur.selBegin().idx();
5767 row_type const row_start = tabular.cellRow(s_start);
5768 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5769 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5770 tabular.bottomLine(cur.selEnd().idx()),
5771 tabular.getAlignment(cur.selEnd().idx()));
5774 cur.selection(false);
5778 case Tabular::UNSET_MULTIROW: {
5779 if (!cur.selection()) {
5780 if (tabular.isMultiRow(cur.idx()))
5781 tabular.unsetMultiRow(cur.idx());
5786 case Tabular::MULTIROW: {
5787 if (!cur.selection()) {
5788 if (tabular.isMultiRow(cur.idx()))
5789 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5791 tabularFeatures(cur, Tabular::SET_MULTIROW);
5795 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5796 col_type const c = sel_col_start;
5797 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5798 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5801 // If the selection contains at least one singlerow cell
5802 // or multiple multirow cells,
5803 // we assume the user will merge is to a single multirow
5805 tabularFeatures(cur, Tabular::SET_MULTIROW);
5807 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5811 case Tabular::SET_ALL_LINES:
5814 case Tabular::UNSET_ALL_LINES:
5815 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5816 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5817 idx_type const cell = tabular.cellIndex(r, c);
5818 tabular.setTopLine(cell, setLines);
5819 tabular.setBottomLine(cell, setLines);
5820 tabular.setRightLine(cell, setLines);
5821 tabular.setLeftLine(cell, setLines);
5825 case Tabular::SET_BORDER_LINES:
5826 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5827 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5828 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5830 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5831 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5832 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5836 case Tabular::TOGGLE_LONGTABULAR:
5837 if (tabular.is_long_tabular)
5838 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
5840 tabular.is_long_tabular = true;
5843 case Tabular::SET_LONGTABULAR:
5844 tabular.is_long_tabular = true;
5847 case Tabular::UNSET_LONGTABULAR:
5848 for (row_type r = 0; r < tabular.nrows(); ++r) {
5849 if (tabular.ltCaption(r)) {
5850 cur.idx() = tabular.cellIndex(r, 0);
5853 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5856 tabular.is_long_tabular = false;
5859 case Tabular::SET_ROTATE_TABULAR:
5860 tabular.rotate = convert<int>(value);
5863 case Tabular::UNSET_ROTATE_TABULAR:
5867 case Tabular::TOGGLE_ROTATE_TABULAR:
5868 // when pressing the rotate button we default to 90° rotation
5869 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5872 case Tabular::TABULAR_VALIGN_TOP:
5873 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5876 case Tabular::TABULAR_VALIGN_MIDDLE:
5877 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5880 case Tabular::TABULAR_VALIGN_BOTTOM:
5881 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5884 case Tabular::LONGTABULAR_ALIGN_LEFT:
5885 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5888 case Tabular::LONGTABULAR_ALIGN_CENTER:
5889 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5892 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5893 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5898 case Tabular::SET_ROTATE_CELL:
5899 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5900 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5901 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5904 case Tabular::UNSET_ROTATE_CELL:
5905 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5906 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5907 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5910 case Tabular::TOGGLE_ROTATE_CELL:
5912 bool oneNotRotated = oneCellHasRotationState(false,
5913 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5915 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5916 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5917 // when pressing the rotate cell button we default to 90° rotation
5919 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5921 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5926 case Tabular::SET_USEBOX: {
5927 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5928 if (val == tabular.getUsebox(cur.idx()))
5929 val = Tabular::BOX_NONE;
5930 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5931 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5932 tabular.setUsebox(tabular.cellIndex(r, c), val);
5936 case Tabular::UNSET_LTFIRSTHEAD:
5939 case Tabular::SET_LTFIRSTHEAD:
5940 tabular.getRowOfLTFirstHead(row, ltt);
5941 checkLongtableSpecial(ltt, value, flag);
5942 tabular.setLTHead(row, flag, ltt, true);
5945 case Tabular::UNSET_LTHEAD:
5948 case Tabular::SET_LTHEAD:
5949 tabular.getRowOfLTHead(row, ltt);
5950 checkLongtableSpecial(ltt, value, flag);
5951 tabular.setLTHead(row, flag, ltt, false);
5954 case Tabular::UNSET_LTFOOT:
5957 case Tabular::SET_LTFOOT:
5958 tabular.getRowOfLTFoot(row, ltt);
5959 checkLongtableSpecial(ltt, value, flag);
5960 tabular.setLTFoot(row, flag, ltt, false);
5963 case Tabular::UNSET_LTLASTFOOT:
5966 case Tabular::SET_LTLASTFOOT:
5967 tabular.getRowOfLTLastFoot(row, ltt);
5968 checkLongtableSpecial(ltt, value, flag);
5969 tabular.setLTFoot(row, flag, ltt, true);
5972 case Tabular::UNSET_LTNEWPAGE:
5975 case Tabular::SET_LTNEWPAGE:
5976 tabular.setLTNewPage(row, flag);
5979 case Tabular::SET_LTCAPTION: {
5980 if (tabular.ltCaption(row))
5982 cur.idx() = tabular.setLTCaption(row, true);
5985 cur.selection(false);
5986 // If a row is set as caption, then also insert
5987 // a caption. Otherwise the LaTeX output is broken.
5988 // Select cell if it is non-empty
5989 if (cur.lastpos() > 0 || cur.lastpit() > 0)
5990 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5991 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5995 case Tabular::UNSET_LTCAPTION: {
5996 if (!tabular.ltCaption(row))
5998 cur.idx() = tabular.setLTCaption(row, false);
6001 cur.selection(false);
6002 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
6003 if (lyx::getStatus(fr).enabled())
6008 case Tabular::TOGGLE_LTCAPTION: {
6009 if (tabular.ltCaption(row))
6010 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
6012 tabularFeatures(cur, Tabular::SET_LTCAPTION);
6016 case Tabular::TOGGLE_BOOKTABS:
6017 tabular.use_booktabs = !tabular.use_booktabs;
6020 case Tabular::SET_BOOKTABS:
6021 tabular.use_booktabs = true;
6024 case Tabular::UNSET_BOOKTABS:
6025 tabular.use_booktabs = false;
6028 case Tabular::SET_TOP_SPACE: {
6030 if (value == "default")
6031 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6032 tabular.row_info[r].top_space_default = true;
6033 else if (value == "none")
6034 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6035 tabular.row_info[r].top_space_default = false;
6036 tabular.row_info[r].top_space = len;
6038 else if (isValidLength(value, &len))
6039 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6040 tabular.row_info[r].top_space_default = false;
6041 tabular.row_info[r].top_space = len;
6046 case Tabular::SET_BOTTOM_SPACE: {
6048 if (value == "default")
6049 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6050 tabular.row_info[r].bottom_space_default = true;
6051 else if (value == "none")
6052 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6053 tabular.row_info[r].bottom_space_default = false;
6054 tabular.row_info[r].bottom_space = len;
6056 else if (isValidLength(value, &len))
6057 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6058 tabular.row_info[r].bottom_space_default = false;
6059 tabular.row_info[r].bottom_space = len;
6064 case Tabular::SET_INTERLINE_SPACE: {
6066 if (value == "default")
6067 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6068 tabular.row_info[r].interline_space_default = true;
6069 else if (value == "none")
6070 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6071 tabular.row_info[r].interline_space_default = false;
6072 tabular.row_info[r].interline_space = len;
6074 else if (isValidLength(value, &len))
6075 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6076 tabular.row_info[r].interline_space_default = false;
6077 tabular.row_info[r].interline_space = len;
6082 case Tabular::SET_DECIMAL_POINT:
6083 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6084 tabular.column_info[c].decimal_point = from_utf8(value);
6087 // dummy stuff just to avoid warnings
6088 case Tabular::LAST_ACTION:
6094 bool InsetTabular::copySelection(Cursor & cur)
6096 if (!cur.selection())
6101 getSelection(cur, rs, re, cs, ce);
6103 paste_tabular.reset(new Tabular(tabular));
6105 for (row_type r = 0; r < rs; ++r)
6106 paste_tabular->deleteRow(0);
6108 row_type const rows = re - rs + 1;
6109 while (paste_tabular->nrows() > rows)
6110 paste_tabular->deleteRow(rows);
6112 for (col_type c = 0; c < cs; ++c)
6113 paste_tabular->deleteColumn(0);
6115 col_type const columns = ce - cs + 1;
6116 while (paste_tabular->ncols() > columns)
6117 paste_tabular->deleteColumn(columns);
6119 paste_tabular->setBuffer(tabular.buffer());
6121 odocstringstream os;
6122 OutputParams const runparams(0);
6123 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6124 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6125 cap::copySelection(cur, os.str());
6127 // mark tabular stack dirty
6128 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6129 // when we (hopefully) have a one-for-all paste mechanism.
6130 // This must be called after cap::copySelection.
6131 dirtyTabularStack(true);
6137 bool InsetTabular::pasteClipboard(Cursor & cur)
6141 col_type actcol = tabular.cellColumn(cur.idx());
6142 row_type actrow = tabular.cellRow(cur.idx());
6144 if (cur.selIsMultiCell()) {
6147 getSelection(cur, actrow, re, actcol, ce);
6150 for (row_type r1 = 0, r2 = actrow;
6151 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6153 for (col_type c1 = 0, c2 = actcol;
6154 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6156 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6157 tabular.isPartOfMultiColumn(r2, c2))
6159 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6163 if (tabular.isPartOfMultiColumn(r2, c2)) {
6167 shared_ptr<InsetTableCell> inset(
6168 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6169 tabular.setCellInset(r2, c2, inset);
6170 // FIXME?: why do we need to do this explicitly? (EL)
6171 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6173 // FIXME: change tracking (MG)
6174 inset->setChange(Change(buffer().params().track_changes ?
6175 Change::INSERTED : Change::UNCHANGED));
6183 void InsetTabular::cutSelection(Cursor & cur)
6185 if (!cur.selection())
6190 getSelection(cur, rs, re, cs, ce);
6191 for (row_type r = rs; r <= re; ++r) {
6192 for (col_type c = cs; c <= ce; ++c) {
6193 shared_ptr<InsetTableCell> t
6194 = cell(tabular.cellIndex(r, c));
6195 if (buffer().params().track_changes)
6196 // FIXME: Change tracking (MG)
6197 t->setChange(Change(Change::DELETED));
6203 // cursor position might be invalid now
6204 if (cur.pit() > cur.lastpit())
6205 cur.pit() = cur.lastpit();
6206 if (cur.pos() > cur.lastpos())
6207 cur.pos() = cur.lastpos();
6208 cur.clearSelection();
6212 bool InsetTabular::isRightToLeft(Cursor & cur) const
6214 // LASSERT: It might be better to abandon this Buffer.
6215 LASSERT(cur.depth() > 1, return false);
6216 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6217 pos_type const parentpos = cur[cur.depth() - 2].pos();
6218 return parentpar.getFontSettings(buffer().params(),
6219 parentpos).language()->rightToLeft();
6223 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6226 LASSERT(stidx <= enidx, return docstring());
6228 col_type const col1 = tabular.cellColumn(stidx);
6229 col_type const col2 = tabular.cellColumn(enidx);
6230 row_type const row1 = tabular.cellRow(stidx);
6231 row_type const row2 = tabular.cellRow(enidx);
6233 for (col_type col = col1; col <= col2; col++)
6234 for (row_type row = row1; row <= row2; row++) {
6239 retval += tabular.cellInset(row, col)->asString(intoInsets);
6245 void InsetTabular::getSelection(Cursor & cur,
6246 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6248 CursorSlice const & beg = cur.selBegin();
6249 CursorSlice const & end = cur.selEnd();
6250 cs = tabular.cellColumn(beg.idx());
6251 ce = tabular.cellColumn(end.idx());
6255 rs = tabular.cellRow(beg.idx());
6256 re = tabular.cellRow(end.idx());
6262 Text * InsetTabular::getText(int idx) const
6264 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6268 void InsetTabular::setChange(Change const & change)
6270 for (idx_type idx = 0; idx < nargs(); ++idx)
6271 cell(idx)->setChange(change);
6275 void InsetTabular::acceptChanges()
6277 for (idx_type idx = 0; idx < nargs(); ++idx)
6278 cell(idx)->acceptChanges();
6282 void InsetTabular::rejectChanges()
6284 for (idx_type idx = 0; idx < nargs(); ++idx)
6285 cell(idx)->rejectChanges();
6289 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6291 return tabular.getPWidth(cell).zero();
6295 bool InsetTabular::forcePlainLayout(idx_type cell) const
6297 return !tabular.getPWidth(cell).zero();
6301 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6304 if (buf.length() <= 0)
6309 col_type maxCols = 1;
6310 size_t const len = buf.length();
6314 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6322 maxCols = max(cols, maxCols);
6328 maxCols = max(cols, maxCols);
6334 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6335 loctab = paste_tabular.get();
6336 dirtyTabularStack(true);
6339 cell = bv.cursor().idx();
6340 ocol = tabular.cellColumn(cell);
6341 row = tabular.cellRow(cell);
6345 idx_type const cells = loctab->numberofcells;
6348 rows = loctab->nrows();
6349 col_type const columns = loctab->ncols();
6351 while (cell < cells && p < len && row < rows &&
6352 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6358 // we can only set this if we are not too far right
6359 if (cols < columns) {
6360 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6361 Font const font = bv.textMetrics(&inset->text()).
6363 inset->setText(buf.substr(op, p - op), font,
6364 buffer().params().track_changes);
6370 // we can only set this if we are not too far right
6371 if (cols < columns) {
6372 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6373 Font const font = bv.textMetrics(&inset->text()).
6375 inset->setText(buf.substr(op, p - op), font,
6376 buffer().params().track_changes);
6381 cell = loctab->cellIndex(row, cols);
6387 // check for the last cell if there is no trailing '\n'
6388 if (cell < cells && op < len) {
6389 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6390 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6391 inset->setText(buf.substr(op, len - op), font,
6392 buffer().params().track_changes);
6398 void InsetTabular::addPreview(DocIterator const & inset_pos,
6399 PreviewLoader & loader) const
6401 DocIterator cell_pos = inset_pos;
6403 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6404 for (row_type r = 0; r < tabular.nrows(); ++r) {
6405 for (col_type c = 0; c < tabular.ncols(); ++c) {
6406 cell_pos.top().idx() = tabular.cellIndex(r, c);
6407 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6413 bool InsetTabular::completionSupported(Cursor const & cur) const
6415 Cursor const & bvCur = cur.bv().cursor();
6416 if (&bvCur.inset() != this)
6418 return cur.text()->completionSupported(cur);
6422 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6424 return completionSupported(cur);
6428 bool InsetTabular::automaticInlineCompletion() const
6430 return lyxrc.completion_inline_text;
6434 bool InsetTabular::automaticPopupCompletion() const
6436 return lyxrc.completion_popup_text;
6440 bool InsetTabular::showCompletionCursor() const
6442 return lyxrc.completion_cursor_text;
6446 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6448 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6452 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6454 if (!completionSupported(cur))
6456 return cur.text()->completionPrefix(cur);
6460 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6462 if (!completionSupported(cur))
6465 return cur.text()->insertCompletion(cur, s, finished);
6469 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6470 Dimension & dim) const
6472 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6473 tm.completionPosAndDim(cur, x, y, dim);
6477 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6479 istringstream data(in);
6481 lex.setStream(data);
6488 if (!lex || token != "tabular") {
6489 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6493 // This is part of the inset proper that is usually swallowed
6494 // by Buffer::readInset
6496 if (!lex || token != "Tabular") {
6497 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6505 string InsetTabular::params2string(InsetTabular const & inset)
6508 data << "tabular" << ' ';
6510 data << "\\end_inset\n";
6515 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6517 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6518 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6519 if (!tabular.isPartOfMultiColumn(r,c) &&
6520 !tabular.isPartOfMultiRow(r,c))
6523 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6524 ParagraphList::iterator it = parlist.begin();
6525 ParagraphList::iterator const en = parlist.end();
6526 for (; it != en; ++it)
6527 it->setLayout(dc.plainLayout());