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 "InsetList.h"
38 #include "LaTeXFeatures.h"
42 #include "MetricsInfo.h"
43 #include "OutputParams.h"
44 #include "output_xhtml.h"
45 #include "Paragraph.h"
46 #include "ParagraphParameters.h"
47 #include "ParIterator.h"
49 #include "texstream.h"
50 #include "TextClass.h"
51 #include "TextMetrics.h"
53 #include "frontends/Application.h"
54 #include "frontends/alert.h"
55 #include "frontends/Clipboard.h"
56 #include "frontends/Painter.h"
57 #include "frontends/Selection.h"
59 #include "support/convert.h"
60 #include "support/debug.h"
61 #include "support/docstream.h"
62 #include "support/FileName.h"
63 #include "support/gettext.h"
64 #include "support/lassert.h"
65 #include "support/lstrings.h"
66 #include "support/unique_ptr.h"
75 using namespace lyx::support;
81 using cap::dirtyTabularStack;
82 using cap::tabularStackDirty;
84 using graphics::PreviewLoader;
86 using frontend::Painter;
87 using frontend::Clipboard;
89 namespace Alert = frontend::Alert;
94 int const ADD_TO_HEIGHT = 2; // in cell
95 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
96 int const default_line_space = 10; // ?
97 int const WIDTH_OF_LINE = 5; // space between double lines
101 unique_ptr<Tabular> paste_tabular;
104 struct TabularFeature {
105 Tabular::Feature action;
111 TabularFeature tabularFeature[] =
113 // the SET/UNSET actions are used by the table dialog,
114 // the TOGGLE actions by the table toolbar buttons
115 // FIXME: these values have been hardcoded in InsetMathGrid and other
117 { Tabular::APPEND_ROW, "append-row", false },
118 { Tabular::APPEND_COLUMN, "append-column", false },
119 { Tabular::DELETE_ROW, "delete-row", false },
120 { Tabular::DELETE_COLUMN, "delete-column", false },
121 { Tabular::COPY_ROW, "copy-row", false },
122 { Tabular::COPY_COLUMN, "copy-column", false },
123 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
124 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
125 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
126 { Tabular::MOVE_ROW_UP, "move-row-up", false },
127 { Tabular::SET_LINE_TOP, "set-line-top", true },
128 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
129 { Tabular::SET_LINE_LEFT, "set-line-left", true },
130 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
131 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
132 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
133 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
134 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
135 { Tabular::ALIGN_LEFT, "align-left", false },
136 { Tabular::ALIGN_RIGHT, "align-right", false },
137 { Tabular::ALIGN_CENTER, "align-center", false },
138 { Tabular::ALIGN_BLOCK, "align-block", false },
139 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
140 { Tabular::VALIGN_TOP, "valign-top", false },
141 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
142 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
143 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
144 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
145 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
146 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
147 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
148 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
149 { Tabular::MULTICOLUMN, "multicolumn", false },
150 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
151 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
152 { Tabular::MULTIROW, "multirow", false },
153 { Tabular::SET_MULTIROW, "set-multirow", false },
154 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
155 { Tabular::SET_MROFFSET, "set-mroffset", true },
156 { Tabular::SET_ALL_LINES, "set-all-lines", false },
157 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
158 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
159 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
160 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
161 { Tabular::SET_PWIDTH, "set-pwidth", true },
162 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
163 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
164 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
165 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
166 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
167 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
168 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
169 { Tabular::SET_USEBOX, "set-usebox", true },
170 { Tabular::SET_LTHEAD, "set-lthead", true },
171 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
172 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
173 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
174 { Tabular::SET_LTFOOT, "set-ltfoot", true },
175 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
176 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
177 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
178 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
179 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
180 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
181 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
182 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
183 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
184 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
185 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
186 { Tabular::SET_BOOKTABS, "set-booktabs", false },
187 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
188 { Tabular::SET_TOP_SPACE, "set-top-space", true },
189 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
190 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
191 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
192 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
193 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
194 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
195 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
196 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
197 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
198 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
199 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
200 { Tabular::LAST_ACTION, "", false }
204 string const tostr(LyXAlignment const & num)
209 case LYX_ALIGN_BLOCK:
213 case LYX_ALIGN_CENTER:
215 case LYX_ALIGN_RIGHT:
217 case LYX_ALIGN_LAYOUT:
219 case LYX_ALIGN_SPECIAL:
221 case LYX_ALIGN_DECIMAL:
228 string const tostr(Tabular::HAlignment const & num)
231 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
233 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
235 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
242 string const tostr(Tabular::VAlignment const & num)
245 case Tabular::LYX_VALIGN_TOP:
247 case Tabular::LYX_VALIGN_MIDDLE:
249 case Tabular::LYX_VALIGN_BOTTOM:
256 string const tostr(Tabular::BoxType const & num)
259 case Tabular::BOX_NONE:
261 case Tabular::BOX_PARBOX:
263 case Tabular::BOX_MINIPAGE:
270 // I would have liked a fromstr template a lot better. (Lgb)
271 bool string2type(string const & str, LyXAlignment & num)
274 num = LYX_ALIGN_NONE;
275 else if (str == "block")
276 num = LYX_ALIGN_BLOCK;
277 else if (str == "left")
278 num = LYX_ALIGN_LEFT;
279 else if (str == "center")
280 num = LYX_ALIGN_CENTER;
281 else if (str == "right")
282 num = LYX_ALIGN_RIGHT;
283 else if (str == "decimal")
284 num = LYX_ALIGN_DECIMAL;
291 bool string2type(string const & str, Tabular::HAlignment & num)
294 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
295 else if (str == "center" )
296 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
297 else if (str == "right")
298 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
305 bool string2type(string const & str, Tabular::VAlignment & num)
308 num = Tabular::LYX_VALIGN_TOP;
309 else if (str == "middle" )
310 num = Tabular::LYX_VALIGN_MIDDLE;
311 else if (str == "bottom")
312 num = Tabular::LYX_VALIGN_BOTTOM;
319 bool string2type(string const & str, Tabular::BoxType & num)
322 num = Tabular::BOX_NONE;
323 else if (str == "parbox")
324 num = Tabular::BOX_PARBOX;
325 else if (str == "minipage")
326 num = Tabular::BOX_MINIPAGE;
333 bool string2type(string const & str, bool & num)
337 else if (str == "false")
345 bool getTokenValue(string const & str, char const * token, string & ret)
348 size_t token_length = strlen(token);
349 size_t pos = str.find(token);
351 if (pos == string::npos || pos + token_length + 1 >= str.length()
352 || str[pos + token_length] != '=')
354 pos += token_length + 1;
356 if (ch != '"' && ch != '\'') { // only read till next space
360 while (pos < str.length() - 1 && str[++pos] != ch)
367 bool getTokenValue(string const & str, char const * token, docstring & ret)
370 bool const success = getTokenValue(str, token, tmp);
371 ret = from_utf8(tmp);
376 bool getTokenValue(string const & str, char const * token, int & num)
380 if (!getTokenValue(str, token, tmp))
382 num = convert<int>(tmp);
387 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
390 return getTokenValue(str, token, tmp) && string2type(tmp, num);
394 bool getTokenValue(string const & str, char const * token,
395 Tabular::HAlignment & num)
398 return getTokenValue(str, token, tmp) && string2type(tmp, num);
402 bool getTokenValue(string const & str, char const * token,
403 Tabular::VAlignment & num)
406 return getTokenValue(str, token, tmp) && string2type(tmp, num);
410 bool getTokenValue(string const & str, char const * token,
411 Tabular::BoxType & num)
414 return getTokenValue(str, token, tmp) && string2type(tmp, num);
418 bool getTokenValue(string const & str, char const * token, bool & flag)
420 // set the flag always to false as this should be the default for bools
421 // not in the file-format.
424 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
428 bool getTokenValue(string const & str, char const * token, Length & len)
430 // set the length to be zero() as default as this it should be if not
431 // in the file format.
434 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
438 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
443 if (!getTokenValue(str, token, tmp))
445 if (tmp == "default") {
449 return isValidLength(tmp, &len);
453 void l_getline(istream & is, string & str)
456 while (str.empty()) {
458 if (!str.empty() && str[str.length() - 1] == '\r')
459 str.erase(str.length() - 1);
464 string const write_attribute(string const & name, T const & t)
466 string const s = tostr(t);
467 return s.empty() ? s : " " + name + "=\"" + s + "\"";
471 string const write_attribute(string const & name, string const & t)
473 return t.empty() ? t : " " + name + "=\"" + t + "\"";
478 string const write_attribute(string const & name, docstring const & t)
480 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
485 string const write_attribute(string const & name, bool const & b)
487 // we write only true attribute values so we remove a bit of the
488 // file format bloat for tabulars.
489 return b ? write_attribute(name, convert<string>(b)) : string();
494 string const write_attribute(string const & name, int const & i)
496 // we write only true attribute values so we remove a bit of the
497 // file format bloat for tabulars.
498 return i ? write_attribute(name, convert<string>(i)) : string();
503 string const write_attribute(string const & name, Tabular::idx_type const & i)
505 // we write only true attribute values so we remove a bit of the
506 // file format bloat for tabulars.
507 return i ? write_attribute(name, convert<string>(i)) : string();
512 string const write_attribute(string const & name, Length const & value)
514 // we write only the value if we really have one same reson as above.
515 return value.zero() ? string() : write_attribute(name, value.asString());
521 string const featureAsString(Tabular::Feature action)
523 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
524 if (tabularFeature[i].action == action)
525 return tabularFeature[i].feature;
531 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
533 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
534 for (; dit; dit.forwardChar())
535 if (dit.inTexted() && dit.depth() == 1
536 && dit.paragraph().find(align_d, false, false, dit.pos()))
543 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
545 InsetTableCell tail = InsetTableCell(head);
546 DocIterator const dit = separatorPos(&head, align_d);
549 pit_type const psize = head.paragraphs().front().size();
550 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
551 tail.paragraphs().front().eraseChars(0,
552 dit.pos() < psize ? dit.pos() + 1 : psize, false);
559 /////////////////////////////////////////////////////////////////////
563 /////////////////////////////////////////////////////////////////////
566 Tabular::CellData::CellData(Buffer * buf)
569 multicolumn(Tabular::CELL_NORMAL),
570 multirow(Tabular::CELL_NORMAL),
571 alignment(LYX_ALIGN_CENTER),
572 valignment(LYX_VALIGN_TOP),
582 inset(new InsetTableCell(buf))
584 inset->setBuffer(*buf);
588 Tabular::CellData::CellData(CellData const & cs)
591 multicolumn(cs.multicolumn),
592 multirow(cs.multirow),
593 mroffset(cs.mroffset),
594 alignment(cs.alignment),
595 valignment(cs.valignment),
596 decimal_hoffset(cs.decimal_hoffset),
597 decimal_width(cs.decimal_width),
599 top_line(cs.top_line),
600 bottom_line(cs.bottom_line),
601 left_line(cs.left_line),
602 right_line(cs.right_line),
605 align_special(cs.align_special),
607 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
611 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
617 multicolumn = cs.multicolumn;
618 multirow = cs.multirow;
619 mroffset = cs.mroffset;
620 alignment = cs.alignment;
621 valignment = cs.valignment;
622 decimal_hoffset = cs.decimal_hoffset;
623 decimal_width = cs.decimal_width;
624 voffset = cs.voffset;
625 top_line = cs.top_line;
626 bottom_line = cs.bottom_line;
627 left_line = cs.left_line;
628 right_line = cs.right_line;
631 align_special = cs.align_special;
632 p_width = cs.p_width;
633 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
637 Tabular::RowData::RowData()
640 top_space_default(false),
641 bottom_space_default(false),
642 interline_space_default(false),
652 Tabular::ColumnData::ColumnData()
653 : alignment(LYX_ALIGN_CENTER),
654 valignment(LYX_VALIGN_TOP),
660 Tabular::ltType::ltType()
661 : set(false), topDL(false),
667 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
669 init(buffer, rows_arg, columns_arg);
673 void Tabular::setBuffer(Buffer & buffer)
676 for (row_type i = 0; i < nrows(); ++i)
677 for (col_type j = 0; j < ncols(); ++j)
678 cell_info[i][j].inset->setBuffer(*buffer_);
682 // activates all lines and sets all widths to 0
683 void Tabular::init(Buffer * buf, row_type rows_arg,
684 col_type columns_arg)
687 row_info = row_vector(rows_arg);
688 column_info = column_vector(columns_arg);
689 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
690 row_info.reserve(10);
691 column_info.reserve(10);
692 cell_info.reserve(100);
694 is_long_tabular = false;
695 tabular_valignment = LYX_VALIGN_MIDDLE;
696 tabular_width = Length();
697 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
699 use_booktabs = false;
700 // set silly default lines
701 for (row_type r = 0; r < nrows(); ++r)
702 for (col_type c = 0; c < ncols(); ++c) {
703 cell_info[r][c].inset->setBuffer(*buffer_);
704 cell_info[r][c].top_line = true;
705 cell_info[r][c].left_line = true;
706 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
707 cell_info[r][c].right_line = c == ncols() - 1;
712 void Tabular::deleteRow(row_type const row)
714 // Not allowed to delete last row
718 for (col_type c = 0; c < ncols(); ++c) {
719 // Care about multirow cells
720 if (row + 1 < nrows() &&
721 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
722 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
723 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
726 row_info.erase(row_info.begin() + row);
727 cell_info.erase(cell_info.begin() + row);
732 void Tabular::copyRow(row_type const row)
734 insertRow(row, true);
738 void Tabular::appendRow(row_type row)
740 insertRow(row, false);
744 void Tabular::insertRow(row_type const row, bool copy)
746 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
747 cell_info.insert(cell_info.begin() + row + 1,
748 cell_vector(0, CellData(buffer_)));
750 for (col_type c = 0; c < ncols(); ++c) {
751 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
752 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
753 if (buffer().params().track_changes)
754 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
755 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
756 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
760 for (col_type c = 0; c < ncols(); ++c) {
761 if (isPartOfMultiRow(row, c))
763 // inherit line settings
764 idx_type const i = cellIndex(row + 1, c);
765 idx_type const j = cellIndex(row, c);
766 setLeftLine(i, leftLine(j));
767 setRightLine(i, rightLine(j));
768 setTopLine(i, topLine(j));
769 if (topLine(j) && bottomLine(j)) {
770 setBottomLine(i, true);
771 setBottomLine(j, false);
773 // mark track changes
774 if (buffer().params().track_changes)
775 cellInfo(i).inset->setChange(Change(Change::INSERTED));
780 void Tabular::moveColumn(col_type col, ColDirection direction)
782 if (direction == Tabular::LEFT)
785 std::swap(column_info[col], column_info[col + 1]);
787 for (row_type r = 0; r < nrows(); ++r) {
788 std::swap(cell_info[r][col], cell_info[r][col + 1]);
789 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
790 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
792 // FIXME track changes is broken for tabular features (#8469)
793 idx_type const i = cellIndex(r, col);
794 idx_type const j = cellIndex(r, col + 1);
795 if (buffer().params().track_changes) {
796 cellInfo(i).inset->setChange(Change(Change::INSERTED));
797 cellInfo(j).inset->setChange(Change(Change::INSERTED));
804 void Tabular::moveRow(row_type row, RowDirection direction)
806 if (direction == Tabular::UP)
809 std::swap(row_info[row], row_info[row + 1]);
811 for (col_type c = 0; c < ncols(); ++c) {
812 std::swap(cell_info[row][c], cell_info[row + 1][c]);
813 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
814 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
816 // FIXME track changes is broken for tabular features (#8469)
817 idx_type const i = cellIndex(row, c);
818 idx_type const j = cellIndex(row + 1, c);
819 if (buffer().params().track_changes) {
820 cellInfo(i).inset->setChange(Change(Change::INSERTED));
821 cellInfo(j).inset->setChange(Change(Change::INSERTED));
828 void Tabular::deleteColumn(col_type const col)
830 // Not allowed to delete last column
834 for (row_type r = 0; r < nrows(); ++r) {
835 // Care about multicolumn cells
836 if (col + 1 < ncols() &&
837 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
838 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
839 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
841 cell_info[r].erase(cell_info[r].begin() + col);
843 column_info.erase(column_info.begin() + col);
848 void Tabular::copyColumn(col_type const col)
850 insertColumn(col, true);
854 void Tabular::appendColumn(col_type col)
856 insertColumn(col, false);
860 void Tabular::insertColumn(col_type const col, bool copy)
862 BufferParams const & bp = buffer().params();
863 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
865 for (row_type r = 0; r < nrows(); ++r) {
866 cell_info[r].insert(cell_info[r].begin() + col + 1,
867 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
868 if (bp.track_changes)
869 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
870 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
871 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
874 for (row_type r = 0; r < nrows(); ++r) {
875 // inherit line settings
876 idx_type const i = cellIndex(r, col + 1);
877 idx_type const j = cellIndex(r, col);
878 setBottomLine(i, bottomLine(j));
879 setTopLine(i, topLine(j));
880 setLeftLine(i, leftLine(j));
881 setRightLine(i, rightLine(j));
882 if (rightLine(i) && rightLine(j)) {
883 setRightLine(j, false);
885 if (buffer().params().track_changes)
886 cellInfo(i).inset->setChange(Change(Change::INSERTED));
891 void Tabular::updateIndexes()
896 for (row_type row = 0; row < nrows(); ++row)
897 for (col_type column = 0; column < ncols(); ++column) {
898 if (!isPartOfMultiColumn(row, column)
899 && !isPartOfMultiRow(row, column))
901 if (isPartOfMultiRow(row, column))
902 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
904 cell_info[row][column].cellno = numberofcells - 1;
907 rowofcell.resize(numberofcells);
908 columnofcell.resize(numberofcells);
910 // reset column and row of cells and update their width and alignment
911 for (row_type row = 0; row < nrows(); ++row)
912 for (col_type column = 0; column < ncols(); ++column) {
913 if (isPartOfMultiColumn(row, column))
915 // columnofcell needs to be called before setting width and aligment
916 // multirow cells inherit the width from the column width
917 if (!isPartOfMultiRow(row, column)) {
918 columnofcell[i] = column;
921 setFixedWidth(row, column);
922 if (isPartOfMultiRow(row, column))
924 cell_info[row][column].inset->setContentAlignment(
925 getAlignment(cellIndex(row, column)));
931 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
934 for (col_type c = 0; c < ncols(); ++c)
935 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
941 bool Tabular::topLine(idx_type const cell) const
943 return cellInfo(cell).top_line;
947 bool Tabular::bottomLine(idx_type const cell) const
949 return cellInfo(cell).bottom_line;
953 bool Tabular::leftLine(idx_type cell) const
957 return cellInfo(cell).left_line;
961 bool Tabular::rightLine(idx_type cell) const
965 return cellInfo(cell).right_line;
969 int Tabular::interRowSpace(row_type row) const
971 if (!row || row >= nrows())
974 int const interline_space = row_info[row - 1].interline_space_default ?
976 row_info[row - 1].interline_space.inPixels(width());
977 if (rowTopLine(row) && rowBottomLine(row - 1))
978 return interline_space + WIDTH_OF_LINE;
979 return interline_space;
983 int Tabular::interColumnSpace(idx_type cell) const
985 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
986 if (rightLine(cell) && nextcol < ncols()
987 && leftLine(cellIndex(cellRow(cell), nextcol)))
988 return WIDTH_OF_LINE;
993 int Tabular::cellWidth(idx_type cell) const
996 col_type const span = columnSpan(cell);
997 col_type const col = cellColumn(cell);
998 for(col_type c = col; c < col + span ; ++c)
999 w += column_info[c].width;
1004 int Tabular::cellHeight(idx_type cell) const
1006 row_type const span = rowSpan(cell);
1007 row_type const row = cellRow(cell);
1009 for(row_type r = row; r < row + span ; ++r) {
1010 h += rowAscent(r) + rowDescent(r);
1011 if (r != row + span - 1)
1012 h += interRowSpace(r + 1);
1019 bool Tabular::updateColumnWidths()
1021 vector<int> max_dwidth(ncols(), 0);
1022 for(col_type c = 0; c < ncols(); ++c)
1023 for(row_type r = 0; r < nrows(); ++r) {
1024 idx_type const i = cellIndex(r, c);
1025 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1026 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1029 bool update = false;
1030 // for each col get max of single col cells
1031 for(col_type c = 0; c < ncols(); ++c) {
1033 for(row_type r = 0; r < nrows(); ++r) {
1034 idx_type const i = cellIndex(r, c);
1035 if (columnSpan(i) == 1) {
1036 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1037 && cell_info[r][c].decimal_width != 0)
1038 new_width = max(new_width, cellInfo(i).width
1039 + max_dwidth[c] - cellInfo(i).decimal_width);
1041 new_width = max(new_width, cellInfo(i).width);
1045 if (column_info[c].width != new_width) {
1046 column_info[c].width = new_width;
1050 // update col widths to fit merged cells
1051 for(col_type c = 0; c < ncols(); ++c)
1052 for(row_type r = 0; r < nrows(); ++r) {
1053 idx_type const i = cellIndex(r, c);
1054 int const span = columnSpan(i);
1055 if (span == 1 || c > cellColumn(i))
1059 for(col_type j = c; j < c + span ; ++j)
1060 old_width += column_info[j].width;
1062 if (cellInfo(i).width > old_width) {
1063 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1072 int Tabular::width() const
1075 for (col_type c = 0; c < ncols(); ++c)
1076 width += column_info[c].width;
1081 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1084 col_type const col = cellColumn(cell);
1085 // set alignment for the whole row if we are not in a multicolumn cell,
1086 // exclude possible multicolumn cells in the row
1087 if (!isMultiColumn(cell)) {
1088 for (row_type r = 0; r < nrows(); ++r) {
1089 // only if the column has no width the multirow inherits the
1090 // alignment of the column, otherwise it is left aligned
1091 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1092 && !isMultiColumn(cellIndex(r, col))) {
1093 cell_info[r][col].alignment = align;
1094 cell_info[r][col].inset->setContentAlignment(align);
1096 if ((isMultiRow(cellIndex(r, col)) && has_width)
1097 && !isMultiColumn(cellIndex(r, col))) {
1098 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1099 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1102 column_info[col].alignment = align;
1103 docstring & dpoint = column_info[col].decimal_point;
1104 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1105 dpoint = from_utf8(lyxrc.default_decimal_point);
1107 cellInfo(cell).alignment = align;
1108 cellInset(cell)->setContentAlignment(align);
1113 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1116 if (!isMultiColumn(cell) || onlycolumn)
1117 column_info[cellColumn(cell)].valignment = align;
1119 cellInfo(cell).valignment = align;
1126 * Allow line and paragraph breaks for fixed width cells or disallow them,
1127 * merge cell paragraphs and reset layout to standard for variable width
1130 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1132 inset->toggleFixedWidth(fixedWidth);
1136 // merge all paragraphs to one
1137 BufferParams const & bp = cur.bv().buffer().params();
1138 while (inset->paragraphs().size() > 1)
1139 mergeParagraph(bp, inset->paragraphs(), 0);
1143 // undo information has already been recorded
1144 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1145 bp.documentClass().plainLayoutName());
1152 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1153 Length const & width)
1155 col_type const c = cellColumn(cell);
1157 column_info[c].p_width = width;
1158 // reset the vertical alignment to top if the fixed width
1159 // is removed or zero because only fixed width columns can
1160 // have a vertical alignment
1161 if (column_info[c].p_width.zero())
1162 column_info[c].valignment = LYX_VALIGN_TOP;
1163 for (row_type r = 0; r < nrows(); ++r) {
1164 idx_type const cell = cellIndex(r, c);
1165 // because of multicolumns
1166 toggleFixedWidth(cur, cellInset(cell).get(),
1167 !getPWidth(cell).zero());
1168 if (isMultiRow(cell))
1169 setAlignment(cell, LYX_ALIGN_LEFT, false);
1171 // cur paragraph can become invalid after paragraphs were merged
1172 if (cur.pit() > cur.lastpit())
1173 cur.pit() = cur.lastpit();
1174 // cur position can become invalid after newlines were removed
1175 if (cur.pos() > cur.lastpos())
1176 cur.pos() = cur.lastpos();
1180 bool Tabular::setFixedWidth(row_type r, col_type c)
1182 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1183 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1184 || (multicol && !cell_info[r][c].p_width.zero());
1185 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1190 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1191 Length const & width)
1193 if (!isMultiColumn(cell))
1196 cellInfo(cell).p_width = width;
1197 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1198 // cur paragraph can become invalid after paragraphs were merged
1199 if (cur.pit() > cur.lastpit())
1200 cur.pit() = cur.lastpit();
1201 // cur position can become invalid after newlines were removed
1202 if (cur.pos() > cur.lastpos())
1203 cur.pos() = cur.lastpos();
1208 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1210 cellInfo(cell).mroffset = mroffset;
1215 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1216 Tabular::Feature what)
1218 if (what == SET_SPECIAL_MULTICOLUMN)
1219 cellInfo(cell).align_special = special;
1221 column_info[cellColumn(cell)].align_special = special;
1225 void Tabular::setTopLine(idx_type i, bool line)
1227 cellInfo(i).top_line = line;
1231 void Tabular::setBottomLine(idx_type i, bool line)
1233 cellInfo(i).bottom_line = line;
1237 void Tabular::setLeftLine(idx_type cell, bool line)
1239 cellInfo(cell).left_line = line;
1243 void Tabular::setRightLine(idx_type cell, bool line)
1245 cellInfo(cell).right_line = line;
1248 bool Tabular::rowTopLine(row_type r) const
1250 bool all_rows_set = true;
1251 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1252 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1253 return all_rows_set;
1257 bool Tabular::rowBottomLine(row_type r) const
1259 bool all_rows_set = true;
1260 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1261 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1262 return all_rows_set;
1266 bool Tabular::columnLeftLine(col_type c) const
1273 for (row_type r = 0; r < nrows(); ++r) {
1274 idx_type const i = cellIndex(r, c);
1275 if (c == cellColumn(i)) {
1277 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1278 if (cellInfo(i).left_line || right)
1282 return 2 * nrows_left >= total;
1286 bool Tabular::columnRightLine(col_type c) const
1291 int nrows_right = 0;
1293 for (row_type r = 0; r < nrows(); ++r) {
1294 idx_type i = cellIndex(r, c);
1295 if (c == cellColumn(i) + columnSpan(i) - 1) {
1297 bool left = (c + 1 < ncols()
1298 && cellInfo(cellIndex(r, c + 1)).left_line)
1299 || c + 1 == ncols();
1300 if (cellInfo(i).right_line && left)
1304 return 2 * nrows_right >= total;
1308 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1310 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1311 return cellInfo(cell).alignment;
1313 return column_info[cellColumn(cell)].alignment;
1318 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1320 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1321 return cellInfo(cell).valignment;
1322 return column_info[cellColumn(cell)].valignment;
1326 int Tabular::offsetVAlignment() const
1328 // for top-alignment the first horizontal table line must be exactly at
1329 // the position of the base line of the surrounding text line
1330 // for bottom alignment, the same is for the last table line
1331 int offset_valign = 0;
1332 switch (tabular_valignment) {
1333 case Tabular::LYX_VALIGN_BOTTOM:
1334 offset_valign = rowAscent(0) - height();
1336 case Tabular::LYX_VALIGN_MIDDLE:
1337 offset_valign = (- height()) / 2 + rowAscent(0);
1339 case Tabular::LYX_VALIGN_TOP:
1340 offset_valign = rowAscent(0);
1343 return offset_valign;
1347 Length const Tabular::getPWidth(idx_type cell) const
1349 if (isMultiColumn(cell))
1350 return cellInfo(cell).p_width;
1351 return column_info[cellColumn(cell)].p_width;
1355 Length const Tabular::getMROffset(idx_type cell) const
1357 return cellInfo(cell).mroffset;
1361 int Tabular::textHOffset(idx_type cell) const
1363 // the LaTeX Way :-(
1364 int x = WIDTH_OF_LINE;
1366 int const w = cellWidth(cell) - cellInfo(cell).width;
1368 switch (getAlignment(cell)) {
1369 case LYX_ALIGN_CENTER:
1372 case LYX_ALIGN_RIGHT:
1375 case LYX_ALIGN_DECIMAL: {
1376 // we center when no decimal point
1377 if (cellInfo(cell).decimal_width == 0) {
1381 col_type const c = cellColumn(cell);
1382 int max_dhoffset = 0;
1383 for(row_type r = 0; r < row_info.size() ; ++r) {
1384 idx_type const i = cellIndex(r, c);
1385 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1386 && cellInfo(i).decimal_width != 0)
1387 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1389 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1392 // LYX_ALIGN_LEFT: nothing :-)
1400 int Tabular::textVOffset(idx_type cell) const
1402 int voffset = cellInfo(cell).voffset;
1403 if (isMultiRow(cell)) {
1404 row_type const row = cellRow(cell);
1405 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1411 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1414 idx_type const numcells = numberOfCellsInRow(row);
1415 // we check against numcells to make sure we do not crash if all the
1416 // cells are multirow (bug #7535), but in that case our return value
1417 // is really invalid, i.e., it is NOT the first cell in the row. but
1418 // i do not know what to do here. (rgh)
1419 while (c < numcells - 1
1420 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1422 return cell_info[row][c].cellno;
1426 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1428 col_type c = ncols() - 1;
1429 // of course we check against 0 so we don't crash. but we have the same
1430 // problem as in the previous routine: if all the cells are part of a
1431 // multirow or part of a multi column, then our return value is invalid.
1433 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1434 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1436 return cell_info[row][c].cellno;
1440 Tabular::row_type Tabular::cellRow(idx_type cell) const
1442 if (cell >= numberofcells)
1446 return rowofcell[cell];
1450 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1452 if (cell >= numberofcells)
1456 return columnofcell[cell];
1460 void Tabular::write(ostream & os) const
1464 << write_attribute("version", 3)
1465 << write_attribute("rows", nrows())
1466 << write_attribute("columns", ncols())
1468 // global longtable options
1470 << write_attribute("rotate", rotate)
1471 << write_attribute("booktabs", use_booktabs)
1472 << write_attribute("islongtable", is_long_tabular)
1473 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1474 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1475 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1476 << write_attribute("headTopDL", endhead.topDL)
1477 << write_attribute("headBottomDL", endhead.bottomDL)
1478 << write_attribute("footTopDL", endfoot.topDL)
1479 << write_attribute("footBottomDL", endfoot.bottomDL)
1480 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1481 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1482 << write_attribute("lastFootEmpty", endlastfoot.empty);
1483 // longtables cannot be aligned vertically
1484 if (!is_long_tabular) {
1485 os << write_attribute("tabularvalignment", tabular_valignment);
1486 os << write_attribute("tabularwidth", tabular_width);
1488 if (is_long_tabular)
1489 os << write_attribute("longtabularalignment", longtabular_alignment);
1491 for (col_type c = 0; c < ncols(); ++c) {
1493 << write_attribute("alignment", column_info[c].alignment);
1494 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1495 os << write_attribute("decimal_point", column_info[c].decimal_point);
1496 os << write_attribute("valignment", column_info[c].valignment)
1497 << write_attribute("width", column_info[c].p_width.asString())
1498 << write_attribute("special", column_info[c].align_special)
1501 for (row_type r = 0; r < nrows(); ++r) {
1502 static const string def("default");
1504 if (row_info[r].top_space_default)
1505 os << write_attribute("topspace", def);
1507 os << write_attribute("topspace", row_info[r].top_space);
1508 if (row_info[r].bottom_space_default)
1509 os << write_attribute("bottomspace", def);
1511 os << write_attribute("bottomspace", row_info[r].bottom_space);
1512 if (row_info[r].interline_space_default)
1513 os << write_attribute("interlinespace", def);
1515 os << write_attribute("interlinespace", row_info[r].interline_space);
1516 os << write_attribute("endhead", row_info[r].endhead)
1517 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1518 << write_attribute("endfoot", row_info[r].endfoot)
1519 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1520 << write_attribute("newpage", row_info[r].newpage)
1521 << write_attribute("caption", row_info[r].caption)
1523 for (col_type c = 0; c < ncols(); ++c) {
1525 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1526 << write_attribute("multirow", cell_info[r][c].multirow)
1527 << write_attribute("mroffset", cell_info[r][c].mroffset)
1528 << write_attribute("alignment", cell_info[r][c].alignment)
1529 << write_attribute("valignment", cell_info[r][c].valignment)
1530 << write_attribute("topline", cell_info[r][c].top_line)
1531 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1532 << write_attribute("leftline", cell_info[r][c].left_line)
1533 << write_attribute("rightline", cell_info[r][c].right_line)
1534 << write_attribute("rotate", cell_info[r][c].rotate)
1535 << write_attribute("usebox", cell_info[r][c].usebox)
1536 << write_attribute("width", cell_info[r][c].p_width)
1537 << write_attribute("special", cell_info[r][c].align_special)
1539 os << "\\begin_inset ";
1540 cell_info[r][c].inset->write(os);
1541 os << "\n\\end_inset\n"
1543 // FIXME This can be removed again once the mystery
1544 // crash has been resolved.
1549 os << "</lyxtabular>\n";
1553 void Tabular::read(Lexer & lex)
1556 istream & is = lex.getStream();
1558 l_getline(is, line);
1559 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1560 LASSERT(false, return);
1564 if (!getTokenValue(line, "version", version))
1566 LATTEST(version >= 2);
1569 if (!getTokenValue(line, "rows", rows_arg))
1572 if (!getTokenValue(line, "columns", columns_arg))
1574 init(buffer_, rows_arg, columns_arg);
1575 l_getline(is, line);
1576 if (!prefixIs(line, "<features")) {
1577 lyxerr << "Wrong tabular format (expected <features ...> got"
1578 << line << ')' << endl;
1581 getTokenValue(line, "rotate", rotate);
1582 getTokenValue(line, "booktabs", use_booktabs);
1583 getTokenValue(line, "islongtable", is_long_tabular);
1584 getTokenValue(line, "tabularvalignment", tabular_valignment);
1585 getTokenValue(line, "tabularwidth", tabular_width);
1586 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1587 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1588 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1589 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1590 getTokenValue(line, "headTopDL", endhead.topDL);
1591 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1592 getTokenValue(line, "footTopDL", endfoot.topDL);
1593 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1594 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1595 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1596 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1598 for (col_type c = 0; c < ncols(); ++c) {
1600 if (!prefixIs(line,"<column")) {
1601 lyxerr << "Wrong tabular format (expected <column ...> got"
1602 << line << ')' << endl;
1605 getTokenValue(line, "alignment", column_info[c].alignment);
1606 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1607 getTokenValue(line, "valignment", column_info[c].valignment);
1608 getTokenValue(line, "width", column_info[c].p_width);
1609 getTokenValue(line, "special", column_info[c].align_special);
1612 for (row_type i = 0; i < nrows(); ++i) {
1613 l_getline(is, line);
1614 if (!prefixIs(line, "<row")) {
1615 lyxerr << "Wrong tabular format (expected <row ...> got"
1616 << line << ')' << endl;
1619 getTokenValue(line, "topspace", row_info[i].top_space,
1620 row_info[i].top_space_default);
1621 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1622 row_info[i].bottom_space_default);
1623 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1624 row_info[i].interline_space_default);
1625 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1626 getTokenValue(line, "endhead", row_info[i].endhead);
1627 getTokenValue(line, "endfoot", row_info[i].endfoot);
1628 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1629 getTokenValue(line, "newpage", row_info[i].newpage);
1630 getTokenValue(line, "caption", row_info[i].caption);
1631 for (col_type j = 0; j < ncols(); ++j) {
1632 l_getline(is, line);
1633 if (!prefixIs(line, "<cell")) {
1634 lyxerr << "Wrong tabular format (expected <cell ...> got"
1635 << line << ')' << endl;
1638 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1639 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1640 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1641 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1642 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1643 getTokenValue(line, "topline", cell_info[i][j].top_line);
1644 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1645 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1646 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1647 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1648 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1649 getTokenValue(line, "width", cell_info[i][j].p_width);
1651 getTokenValue(line, "special", cell_info[i][j].align_special);
1652 l_getline(is, line);
1653 if (prefixIs(line, "\\begin_inset")) {
1654 cell_info[i][j].inset->setBuffer(*buffer_);
1655 cell_info[i][j].inset->read(lex);
1656 l_getline(is, line);
1658 if (!prefixIs(line, "</cell>")) {
1659 lyxerr << "Wrong tabular format (expected </cell> got"
1660 << line << ')' << endl;
1664 l_getline(is, line);
1665 if (!prefixIs(line, "</row>")) {
1666 lyxerr << "Wrong tabular format (expected </row> got"
1667 << line << ')' << endl;
1671 while (!prefixIs(line, "</lyxtabular>")) {
1672 l_getline(is, line);
1678 bool Tabular::isMultiColumn(idx_type cell) const
1680 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1681 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1685 bool Tabular::hasMultiColumn(col_type c) const
1687 for (row_type r = 0; r < nrows(); ++r) {
1688 if (isMultiColumn(cellIndex(r, c)))
1695 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1697 return cell_info[cellRow(cell)][cellColumn(cell)];
1701 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1703 return cell_info[cellRow(cell)][cellColumn(cell)];
1707 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1708 bool const right_border)
1710 idx_type const col = cellColumn(cell);
1711 idx_type const row = cellRow(cell);
1712 for (idx_type i = 0; i < number; ++i)
1713 unsetMultiRow(cellIndex(row, col + i));
1715 // unsetting of multirow may have invalidated cell index
1716 cell = cellIndex(row, col);
1717 CellData & cs = cellInfo(cell);
1718 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1719 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1720 cs.alignment = column_info[col].alignment;
1721 setRightLine(cell, right_border);
1723 idx_type lastcell = cellIndex(row, col + number - 1);
1724 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1725 CellData & cs1 = cellInfo(cell + i);
1726 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1727 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1735 bool Tabular::isMultiRow(idx_type cell) const
1737 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1738 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1741 bool Tabular::hasMultiRow(row_type r) const
1743 for (col_type c = 0; c < ncols(); ++c) {
1744 if (isMultiRow(cellIndex(r, c)))
1750 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1751 bool const bottom_border,
1752 LyXAlignment const halign)
1754 idx_type const col = cellColumn(cell);
1755 idx_type const row = cellRow(cell);
1756 for (idx_type i = 0; i < number; ++i)
1757 unsetMultiColumn(cellIndex(row + i, col));
1759 // unsetting of multirow may have invalidated cell index
1760 cell = cellIndex(row, col);
1761 CellData & cs = cellInfo(cell);
1762 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1763 cs.valignment = LYX_VALIGN_MIDDLE;
1764 // the horizontal alignment of multirow cells can only
1765 // be changed for the whole table row,
1766 // support changing this only for the multirow cell can be done via
1768 if (getPWidth(cell).zero())
1769 cs.alignment = halign;
1771 cs.alignment = LYX_ALIGN_LEFT;
1773 // set the bottom line of the last selected cell
1774 setBottomLine(cell, bottom_border);
1776 for (idx_type i = 1; i < number; ++i) {
1777 CellData & cs1 = cell_info[row + i][col];
1778 cs1.multirow = CELL_PART_OF_MULTIROW;
1779 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1787 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1789 row_type const row = cellRow(cell);
1790 col_type const col = cellColumn(cell);
1792 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1799 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1801 col_type const column = cellColumn(cell);
1802 col_type row = cellRow(cell) + 1;
1803 while (row < nrows() && isPartOfMultiRow(row, column))
1806 return row - cellRow(cell);
1810 void Tabular::unsetMultiColumn(idx_type cell)
1812 if (!isMultiColumn(cell))
1815 row_type const row = cellRow(cell);
1816 col_type const col = cellColumn(cell);
1817 row_type const span = columnSpan(cell);
1818 for (col_type c = 0; c < span; ++c) {
1819 // in the table dialog the lines are set in every case
1820 // when unsetting a multicolumn this leads to an additional right
1821 // line for every cell that was part of the former multicolumn cell,
1822 // except if the cell is in the last column
1823 // therefore remove this line
1824 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1825 && (col + c) < (col + span - 1))
1826 cell_info[row][col + c].right_line = false;
1827 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1833 void Tabular::unsetMultiRow(idx_type cell)
1835 if (!isMultiRow(cell))
1838 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1839 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1840 row_type const row = cellRow(cell);
1841 col_type const col = cellColumn(cell);
1842 row_type const span = rowSpan(cell);
1843 for (row_type r = 0; r < span; ++r)
1844 cell_info[row + r][col].multirow = CELL_NORMAL;
1849 void Tabular::setRotateCell(idx_type cell, int value)
1851 cellInfo(cell).rotate = value;
1855 int Tabular::getRotateCell(idx_type cell) const
1857 return cellInfo(cell).rotate;
1861 bool Tabular::needRotating() const
1865 for (row_type r = 0; r < nrows(); ++r)
1866 for (col_type c = 0; c < ncols(); ++c)
1867 if (cell_info[r][c].rotate != 0)
1873 bool Tabular::isLastCell(idx_type cell) const
1875 if (cell + 1 < numberofcells)
1881 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1883 if (cellRow(cell) == 0)
1886 col_type const col = cellColumn(cell);
1887 row_type r = cellRow(cell) - 1;
1888 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1891 return cell_info[r][col].cellno;
1895 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1897 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1898 if (nextrow < nrows())
1899 return cell_info[nextrow][cellColumn(cell)].cellno;
1904 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1906 LASSERT(column != npos && column < ncols(), column = 0);
1907 LASSERT(row != npos && row < nrows(), row = 0);
1908 return cell_info[row][column].cellno;
1912 void Tabular::setUsebox(idx_type cell, BoxType type)
1914 cellInfo(cell).usebox = type;
1918 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1919 // adds line breaks, see bug 4886.
1920 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1922 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1923 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1925 if (cellInfo(cell).usebox > 1)
1926 return cellInfo(cell).usebox;
1927 return useParbox(cell);
1932 // This are functions used for the longtable support
1934 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1940 row_info[row].endfirsthead = flag;
1944 row_info[row].endhead = flag;
1949 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1952 hd.set = haveLTHead();
1953 return row_info[row].endhead;
1957 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1960 hd.set = haveLTFirstHead();
1961 return row_info[row].endfirsthead;
1965 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1971 row_info[row].endlastfoot = flag;
1975 row_info[row].endfoot = flag;
1980 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1983 fd.set = haveLTFoot();
1984 return row_info[row].endfoot;
1988 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1991 fd.set = haveLTLastFoot();
1992 return row_info[row].endlastfoot;
1996 void Tabular::setLTNewPage(row_type row, bool what)
1998 row_info[row].newpage = what;
2002 bool Tabular::getLTNewPage(row_type row) const
2004 return row_info[row].newpage;
2008 bool Tabular::haveLTHead(bool withcaptions) const
2010 if (!is_long_tabular)
2012 for (row_type i = 0; i < nrows(); ++i)
2013 if (row_info[i].endhead &&
2014 (withcaptions || !row_info[i].caption))
2020 bool Tabular::haveLTFirstHead(bool withcaptions) const
2022 if (!is_long_tabular || endfirsthead.empty)
2024 for (row_type r = 0; r < nrows(); ++r)
2025 if (row_info[r].endfirsthead &&
2026 (withcaptions || !row_info[r].caption))
2032 bool Tabular::haveLTFoot(bool withcaptions) const
2034 if (!is_long_tabular)
2036 for (row_type r = 0; r < nrows(); ++r)
2037 if (row_info[r].endfoot &&
2038 (withcaptions || !row_info[r].caption))
2044 bool Tabular::haveLTLastFoot(bool withcaptions) const
2046 if (!is_long_tabular || endlastfoot.empty)
2048 for (row_type r = 0; r < nrows(); ++r)
2049 if (row_info[r].endlastfoot &&
2050 (withcaptions || !row_info[r].caption))
2056 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2058 idx_type i = getFirstCellInRow(row);
2060 setMultiColumn(i, numberOfCellsInRow(row), false);
2061 setTopLine(i, false);
2062 setBottomLine(i, false);
2063 setLeftLine(i, false);
2064 setRightLine(i, false);
2065 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2066 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2067 setLTHead(row, true, endfirsthead, true);
2068 row_info[row].endfirsthead = true;
2071 unsetMultiColumn(i);
2072 // When unsetting a caption row, also all existing
2073 // captions in this row must be dissolved.
2075 row_info[row].caption = what;
2080 bool Tabular::ltCaption(row_type row) const
2082 return row_info[row].caption;
2086 bool Tabular::haveLTCaption(CaptionType captiontype) const
2088 if (!is_long_tabular)
2090 for (row_type r = 0; r < nrows(); ++r) {
2091 if (row_info[r].caption) {
2092 switch (captiontype) {
2093 case CAPTION_FIRSTHEAD:
2094 if (row_info[r].endfirsthead)
2098 if (row_info[r].endhead)
2102 if (row_info[r].endfoot)
2105 case CAPTION_LASTFOOT:
2106 if (row_info[r].endlastfoot)
2118 // end longtable support functions
2120 void Tabular::setRowAscent(row_type row, int height)
2122 if (row >= nrows() || row_info[row].ascent == height)
2124 row_info[row].ascent = height;
2128 void Tabular::setRowDescent(row_type row, int height)
2130 if (row >= nrows() || row_info[row].descent == height)
2132 row_info[row].descent = height;
2136 int Tabular::rowAscent(row_type row) const
2138 LASSERT(row < nrows(), row = 0);
2139 return row_info[row].ascent;
2143 int Tabular::rowDescent(row_type row) const
2145 LASSERT(row < nrows(), row = 0);
2146 return row_info[row].descent;
2150 int Tabular::height() const
2153 for (row_type row = 0; row < nrows(); ++row)
2154 height += rowAscent(row) + rowDescent(row) +
2160 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2162 LASSERT(row < nrows(), return false);
2163 LASSERT(column < ncols(), return false);
2164 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2168 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2170 LASSERT(row < nrows(), return false);
2171 LASSERT(column < ncols(), return false);
2172 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2176 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang) const
2178 // we only output complete row lines and the 1st row here, the rest
2179 // is done in Tabular::TeXBottomHLine(...)
2181 // get for each column the topline (if any)
2182 vector<bool> topline;
2184 for (col_type c = 0; c < ncols(); ++c) {
2185 topline.push_back(topLine(cellIndex(row, c)));
2186 // If cell is part of a multirow and not the first cell of the
2187 // multirow, no line must be drawn.
2189 if (isMultiRow(cellIndex(row, c))
2190 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2196 // do nothing if empty first row, or incomplete row line after
2197 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2200 // only output complete row lines and the 1st row's clines
2201 if (nset == ncols()) {
2203 os << (row == 0 ? "\\toprule " : "\\midrule ");
2207 } else if (row == 0) {
2208 for (col_type c = 0; c < ncols(); ++c) {
2210 col_type offset = 0;
2211 for (col_type j = 0 ; j < c; ++j)
2212 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2215 //babel makes the "-" character an active one, so we have to suppress this here
2216 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2217 if (lang == "slovak" || lang == "czech")
2218 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2219 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2221 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2223 col_type cstart = c;
2224 for ( ; c < ncols() && topline[c]; ++c) {}
2226 for (col_type j = cstart ; j < c ; ++j)
2227 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2230 os << c + offset << "} ";
2238 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang) const
2240 // we output bottomlines of row r and the toplines of row r+1
2241 // if the latter do not span the whole tabular
2243 // get the bottomlines of row r, and toplines in next row
2244 bool lastrow = row == nrows() - 1;
2245 vector<bool> bottomline, topline;
2246 bool nextrowset = true;
2247 for (col_type c = 0; c < ncols(); ++c) {
2248 bottomline.push_back(bottomLine(cellIndex(row, c)));
2249 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2250 // If cell is part of a multirow and not the last cell of the
2251 // multirow, no line must be drawn.
2253 if (isMultiRow(cellIndex(row, c))
2254 && isMultiRow(cellIndex(row + 1, c))
2255 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2256 bottomline[c] = false;
2259 nextrowset &= topline[c];
2262 // combine this row's bottom lines and next row's toplines if necessary
2264 for (col_type c = 0; c < ncols(); ++c) {
2266 bottomline[c] = bottomline[c] || topline[c];
2271 // do nothing if empty, OR incomplete row line with a topline in next row
2272 if (nset == 0 || (nextrowset && nset != ncols()))
2275 if (nset == ncols()) {
2277 os << (lastrow ? "\\bottomrule" : "\\midrule");
2281 for (col_type c = 0; c < ncols(); ++c) {
2282 if (bottomline[c]) {
2283 col_type offset = 0;
2284 for (col_type j = 0 ; j < c; ++j)
2285 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2288 //babel makes the "-" character an active one, so we have to suppress this here
2289 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2290 if (lang == "slovak" || lang == "czech")
2291 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2292 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2294 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2296 col_type cstart = c;
2297 for ( ; c < ncols() && bottomline[c]; ++c) {}
2299 for (col_type j = cstart ; j < c ; ++j)
2300 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2303 os << c + offset << "} ";
2311 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2312 bool & ismulticol, bool & ismultirow) const
2314 row_type const r = cellRow(cell);
2315 if (is_long_tabular && row_info[r].caption)
2318 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2319 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2320 // figure out how to set the lines
2321 // we always set double lines to the right of the cell
2322 col_type const c = cellColumn(cell);
2323 col_type const nextcol = c + columnSpan(cell);
2324 bool colright = columnRightLine(c);
2325 bool colleft = columnLeftLine(c);
2326 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2327 bool nextcellleft = nextcol < ncols()
2328 && leftLine(cellIndex(r, nextcol));
2329 bool coldouble = colright && nextcolleft;
2330 bool celldouble = rightLine(cell) && nextcellleft;
2332 ismulticol = isMultiColumn(cell)
2333 || (c == 0 && colleft != leftLine(cell))
2334 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2335 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2336 || (coldouble != celldouble);
2338 // we center in multicol when no decimal point
2339 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2340 docstring const align_d = column_info[c].decimal_point;
2341 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2345 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2346 int latexcolspan = columnSpan(cell);
2347 for(col_type col = c; col < c + columnSpan(cell); ++col)
2348 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2352 os << "\\multicolumn{" << latexcolspan << "}{";
2353 if (c ==0 && leftLine(cell))
2355 if (!cellInfo(cell).align_special.empty()) {
2356 os << cellInfo(cell).align_special;
2358 if (!getPWidth(cell).zero()) {
2360 case LYX_ALIGN_LEFT:
2361 os << ">{\\raggedright}";
2363 case LYX_ALIGN_RIGHT:
2364 os << ">{\\raggedleft}";
2366 case LYX_ALIGN_CENTER:
2367 os << ">{\\centering}";
2373 case LYX_VALIGN_TOP:
2376 case LYX_VALIGN_MIDDLE:
2379 case LYX_VALIGN_BOTTOM:
2384 << from_ascii(getPWidth(cell).asLatexString())
2388 case LYX_ALIGN_LEFT:
2391 case LYX_ALIGN_RIGHT:
2398 } // end if else !getPWidth
2399 } // end if else !cellinfo_of_cell
2400 if (rightLine(cell) || nextcellleft)
2403 // add extra vertical line if we want a double one
2406 } // end if ismulticol
2408 // we only need code for the first multirow cell
2409 ismultirow = isMultiRow(cell);
2411 os << "\\multirow{" << rowSpan(cell) << "}{";
2412 if (!getPWidth(cell).zero())
2413 os << from_ascii(getPWidth(cell).asLatexString());
2415 // we need to set a default value
2418 if (!getMROffset(cell).zero())
2419 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2421 } // end if ismultirow
2423 if (getRotateCell(cell) != 0)
2424 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2426 if (getUsebox(cell) == BOX_PARBOX) {
2429 case LYX_VALIGN_TOP:
2432 case LYX_VALIGN_MIDDLE:
2435 case LYX_VALIGN_BOTTOM:
2439 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2441 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2442 os << "\\begin{minipage}[";
2444 case LYX_VALIGN_TOP:
2447 case LYX_VALIGN_MIDDLE:
2450 case LYX_VALIGN_BOTTOM:
2454 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2460 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2461 bool ismulticol, bool ismultirow) const
2463 row_type const r = cellRow(cell);
2464 if (is_long_tabular && row_info[r].caption)
2468 if (getUsebox(cell) == BOX_PARBOX)
2470 else if (getUsebox(cell) == BOX_MINIPAGE)
2471 os << breakln << "\\end{minipage}";
2472 if (getRotateCell(cell) != 0)
2473 os << breakln << "\\end{turn}";
2481 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2482 OutputParams const & runparams) const
2484 if (!is_long_tabular)
2488 // output caption which is in no header or footer
2489 if (haveLTCaption()) {
2490 for (row_type r = 0; r < nrows(); ++r) {
2491 if (row_info[r].caption &&
2492 !row_info[r].endfirsthead && !row_info[r].endhead &&
2493 !row_info[r].endfoot && !row_info[r].endlastfoot)
2494 TeXRow(os, r, runparams);
2497 // output first header info
2498 if (haveLTFirstHead()) {
2499 if (endfirsthead.topDL)
2501 for (row_type r = 0; r < nrows(); ++r) {
2502 if (row_info[r].endfirsthead)
2503 TeXRow(os, r, runparams);
2505 if (endfirsthead.bottomDL)
2507 os << "\\endfirsthead\n";
2509 // output header info
2511 if (endfirsthead.empty && !haveLTFirstHead())
2512 os << "\\endfirsthead\n";
2515 for (row_type r = 0; r < nrows(); ++r) {
2516 if (row_info[r].endhead)
2517 TeXRow(os, r, runparams);
2519 if (endhead.bottomDL)
2521 os << "\\endhead\n";
2523 // output footer info
2527 for (row_type r = 0; r < nrows(); ++r) {
2528 if (row_info[r].endfoot)
2529 TeXRow(os, r, runparams);
2531 if (endfoot.bottomDL)
2533 os << "\\endfoot\n";
2534 if (endlastfoot.empty && !haveLTLastFoot())
2535 os << "\\endlastfoot\n";
2537 // output lastfooter info
2538 if (haveLTLastFoot()) {
2539 if (endlastfoot.topDL)
2541 for (row_type r = 0; r < nrows(); ++r) {
2542 if (row_info[r].endlastfoot)
2543 TeXRow(os, r, runparams);
2545 if (endlastfoot.bottomDL)
2547 os << "\\endlastfoot\n";
2552 bool Tabular::isValidRow(row_type row) const
2554 if (!is_long_tabular)
2556 return !row_info[row].endhead && !row_info[row].endfirsthead
2557 && !row_info[row].endfoot && !row_info[row].endlastfoot
2558 && !row_info[row].caption;
2562 void Tabular::TeXRow(otexstream & os, row_type row,
2563 OutputParams const & runparams) const
2565 idx_type cell = cellIndex(row, 0);
2566 InsetTableCell const * inset = cellInset(cell);
2567 Paragraph const & par = inset->paragraphs().front();
2568 string const lang = par.getParLanguage(buffer().params())->lang();
2570 //output the top line
2571 TeXTopHLine(os, row, lang);
2573 if (row_info[row].top_space_default) {
2575 os << "\\addlinespace\n";
2577 os << "\\noalign{\\vskip\\doublerulesep}\n";
2578 } else if(!row_info[row].top_space.zero()) {
2580 os << "\\addlinespace["
2581 << from_ascii(row_info[row].top_space.asLatexString())
2584 os << "\\noalign{\\vskip"
2585 << from_ascii(row_info[row].top_space.asLatexString())
2589 bool ismulticol = false;
2590 bool ismultirow = false;
2591 for (col_type c = 0; c < ncols(); ++c) {
2592 if (isPartOfMultiColumn(row, c))
2595 cell = cellIndex(row, c);
2597 if (isPartOfMultiRow(row, c)
2598 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2599 if (cell != getLastCellInRow(row))
2604 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2605 InsetTableCell const * inset = cellInset(cell);
2607 Paragraph const & par = inset->paragraphs().front();
2609 os.texrow().forceStart(par.id(), 0);
2611 bool rtl = par.isRTL(buffer().params())
2613 && getPWidth(cell).zero()
2614 && !runparams.use_polyglossia;
2618 par.getParLanguage(buffer().params())->lang();
2619 if (lang == "farsi")
2621 else if (lang == "arabic_arabi")
2623 // currently, remaining RTL languages are
2624 // arabic_arabtex and hebrew
2628 // pass to the OutputParams that we are in a cell and
2629 // which alignment we have set.
2630 // InsetNewline needs this context information.
2631 OutputParams newrp(runparams);
2632 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2633 ? OutputParams::PLAIN
2634 : OutputParams::ALIGNED;
2636 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2637 // copy cell and split in 2
2638 InsetTableCell head = InsetTableCell(*cellInset(cell));
2639 head.setBuffer(const_cast<Buffer &>(buffer()));
2640 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2642 dit.push_back(CursorSlice(head));
2643 head.setMacrocontextPositionRecursive(dit);
2644 bool hassep = false;
2645 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2646 head.latex(os, newrp);
2649 tail.setBuffer(head.buffer());
2651 dit.push_back(CursorSlice(tail));
2652 tail.setMacrocontextPositionRecursive(dit);
2653 tail.latex(os, newrp);
2655 } else if (!isPartOfMultiRow(row, c)) {
2656 if (!runparams.nice)
2657 os.texrow().start(par.id(), 0);
2658 inset->latex(os, newrp);
2661 runparams.encoding = newrp.encoding;
2665 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2666 if (cell != getLastCellInRow(row)) { // not last cell in row
2673 os << "\\tabularnewline";
2674 if (row_info[row].bottom_space_default) {
2676 os << "\\addlinespace";
2678 os << "[\\doublerulesep]";
2679 } else if (!row_info[row].bottom_space.zero()) {
2681 os << "\\addlinespace";
2683 << from_ascii(row_info[row].bottom_space.asLatexString())
2688 //output the bottom line
2689 TeXBottomHLine(os, row, lang);
2691 if (row_info[row].interline_space_default) {
2693 os << "\\addlinespace\n";
2695 os << "\\noalign{\\vskip\\doublerulesep}\n";
2696 } else if (!row_info[row].interline_space.zero()) {
2698 os << "\\addlinespace["
2699 << from_ascii(row_info[row].interline_space.asLatexString())
2702 os << "\\noalign{\\vskip"
2703 << from_ascii(row_info[row].interline_space.asLatexString())
2709 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2711 bool const is_tabular_star = !tabular_width.zero();
2712 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
2714 //+---------------------------------------------------------------------
2715 //+ first the opening preamble +
2716 //+---------------------------------------------------------------------
2719 if (!TexRow::isNone(pos))
2720 os.texrow().start(pos);
2723 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2725 if (is_long_tabular) {
2726 os << "\\begin{longtable}";
2727 switch (longtabular_alignment) {
2728 case LYX_LONGTABULAR_ALIGN_LEFT:
2731 case LYX_LONGTABULAR_ALIGN_CENTER:
2733 case LYX_LONGTABULAR_ALIGN_RIGHT:
2738 if (is_tabular_star)
2739 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2741 os << "\\begin{tabular}";
2742 switch (tabular_valignment) {
2743 case LYX_VALIGN_TOP:
2746 case LYX_VALIGN_MIDDLE:
2748 case LYX_VALIGN_BOTTOM:
2756 if (is_tabular_star)
2757 os << "@{\\extracolsep{\\fill}}";
2759 for (col_type c = 0; c < ncols(); ++c) {
2760 if (columnLeftLine(c))
2762 if (!column_info[c].align_special.empty()) {
2763 os << column_info[c].align_special;
2765 if (!column_info[c].p_width.zero()) {
2766 bool decimal = false;
2767 switch (column_info[c].alignment) {
2768 case LYX_ALIGN_LEFT:
2769 os << ">{\\raggedright}";
2771 case LYX_ALIGN_RIGHT:
2772 os << ">{\\raggedleft}";
2774 case LYX_ALIGN_CENTER:
2775 os << ">{\\centering}";
2777 case LYX_ALIGN_NONE:
2778 case LYX_ALIGN_BLOCK:
2779 case LYX_ALIGN_LAYOUT:
2780 case LYX_ALIGN_SPECIAL:
2782 case LYX_ALIGN_DECIMAL:
2783 os << ">{\\raggedleft}";
2789 switch (column_info[c].valignment) {
2790 case LYX_VALIGN_TOP:
2791 // this is the default
2793 case LYX_VALIGN_MIDDLE:
2796 case LYX_VALIGN_BOTTOM:
2802 // Fixed-width cells with alignment at decimal separator
2803 // are output as two cells of half the width with the decimal
2804 // separator as column sep. This effectively puts the content
2805 // centered, which differs from the normal decimal sep alignment
2806 // and is not ideal, but we cannot do better ATM (see #9568).
2807 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
2809 docstring const halffixedwith =
2810 from_ascii(Length(column_info[c].p_width.value() / 2,
2811 column_info[c].p_width.unit()).asLatexString());
2815 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
2822 << from_ascii(column_info[c].p_width.asLatexString())
2825 switch (column_info[c].alignment) {
2826 case LYX_ALIGN_LEFT:
2829 case LYX_ALIGN_RIGHT:
2832 case LYX_ALIGN_DECIMAL:
2833 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2839 } // end if else !column_info[i].p_width
2840 } // end if else !column_info[i].align_special
2841 if (columnRightLine(c))
2846 TeXLongtableHeaderFooter(os, runparams);
2848 //+---------------------------------------------------------------------
2849 //+ the single row and columns (cells) +
2850 //+---------------------------------------------------------------------
2852 for (row_type r = 0; r < nrows(); ++r) {
2853 if (isValidRow(r)) {
2854 TeXRow(os, r, runparams);
2855 if (is_long_tabular && row_info[r].newpage)
2856 os << "\\newpage\n";
2860 //+---------------------------------------------------------------------
2861 //+ the closing of the tabular +
2862 //+---------------------------------------------------------------------
2864 if (is_long_tabular)
2865 os << "\\end{longtable}";
2867 if (is_tabular_star)
2868 os << "\\end{tabular*}";
2870 os << "\\end{tabular}";
2874 os << breakln << "\\end{turn}";
2876 if (!TexRow::isNone(pos))
2877 os.texrow().start(pos);
2881 int Tabular::docbookRow(odocstream & os, row_type row,
2882 OutputParams const & runparams) const
2885 idx_type cell = getFirstCellInRow(row);
2888 for (col_type c = 0; c < ncols(); ++c) {
2889 if (isPartOfMultiColumn(row, c))
2892 os << "<entry align=\"";
2893 switch (getAlignment(cell)) {
2894 case LYX_ALIGN_LEFT:
2897 case LYX_ALIGN_RIGHT:
2905 os << "\" valign=\"";
2906 switch (getVAlignment(cell)) {
2907 case LYX_VALIGN_TOP:
2910 case LYX_VALIGN_BOTTOM:
2913 case LYX_VALIGN_MIDDLE:
2918 if (isMultiColumn(cell)) {
2919 os << " namest=\"col" << c << "\" ";
2920 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2924 ret += cellInset(cell)->docbook(os, runparams);
2933 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2937 //+---------------------------------------------------------------------
2938 //+ first the opening preamble +
2939 //+---------------------------------------------------------------------
2941 os << "<tgroup cols=\"" << ncols()
2942 << "\" colsep=\"1\" rowsep=\"1\">\n";
2944 for (col_type c = 0; c < ncols(); ++c) {
2945 os << "<colspec colname=\"col" << c << "\" align=\"";
2946 switch (column_info[c].alignment) {
2947 case LYX_ALIGN_LEFT:
2950 case LYX_ALIGN_RIGHT:
2958 if (runparams.flavor == OutputParams::XML)
2964 //+---------------------------------------------------------------------
2965 //+ Long Tabular case +
2966 //+---------------------------------------------------------------------
2968 // output caption info
2969 // The caption flag wins over head/foot
2970 if (haveLTCaption()) {
2971 os << "<caption>\n";
2973 for (row_type r = 0; r < nrows(); ++r) {
2974 if (row_info[r].caption) {
2975 ret += docbookRow(os, r, runparams);
2978 os << "</caption>\n";
2981 // output header info
2982 if (haveLTHead(false) || haveLTFirstHead(false)) {
2985 for (row_type r = 0; r < nrows(); ++r) {
2986 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2987 !row_info[r].caption) {
2988 ret += docbookRow(os, r, runparams);
2994 // output footer info
2995 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2998 for (row_type r = 0; r < nrows(); ++r) {
2999 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
3000 !row_info[r].caption) {
3001 ret += docbookRow(os, r, runparams);
3008 //+---------------------------------------------------------------------
3009 //+ the single row and columns (cells) +
3010 //+---------------------------------------------------------------------
3014 for (row_type r = 0; r < nrows(); ++r) {
3015 if (isValidRow(r)) {
3016 ret += docbookRow(os, r, runparams);
3021 //+---------------------------------------------------------------------
3022 //+ the closing of the tabular +
3023 //+---------------------------------------------------------------------
3032 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3033 OutputParams const & runparams, bool header) const
3036 string const celltag = header ? "th" : "td";
3037 idx_type cell = getFirstCellInRow(row);
3039 xs << html::StartTag("tr");
3040 for (col_type c = 0; c < ncols(); ++c) {
3041 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3046 Length const cwidth = column_info[c].p_width;
3047 if (!cwidth.zero()) {
3048 string const hwidth = cwidth.asHTMLString();
3049 attr << "style =\"width: " << hwidth << ";\" ";
3053 switch (getAlignment(cell)) {
3054 case LYX_ALIGN_LEFT:
3057 case LYX_ALIGN_RIGHT:
3065 attr << " valign='";
3066 switch (getVAlignment(cell)) {
3067 case LYX_VALIGN_TOP:
3070 case LYX_VALIGN_BOTTOM:
3073 case LYX_VALIGN_MIDDLE:
3078 if (isMultiColumn(cell))
3079 attr << " colspan='" << columnSpan(cell) << "'";
3080 else if (isMultiRow(cell))
3081 attr << " rowspan='" << rowSpan(cell) << "'";
3083 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3084 ret += cellInset(cell)->xhtml(xs, runparams);
3085 xs << html::EndTag(celltag) << html::CR();
3088 xs << html::EndTag("tr");
3093 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3097 if (is_long_tabular) {
3098 // we'll wrap it in a div, so as to deal with alignment
3100 switch (longtabular_alignment) {
3101 case LYX_LONGTABULAR_ALIGN_LEFT:
3104 case LYX_LONGTABULAR_ALIGN_CENTER:
3107 case LYX_LONGTABULAR_ALIGN_RIGHT:
3111 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3113 // The caption flag wins over head/foot
3114 if (haveLTCaption()) {
3115 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3117 for (row_type r = 0; r < nrows(); ++r)
3118 if (row_info[r].caption)
3119 ret += xhtmlRow(xs, r, runparams);
3120 xs << html::EndTag("div") << html::CR();
3124 xs << html::StartTag("table") << html::CR();
3126 // output header info
3127 bool const havefirsthead = haveLTFirstHead(false);
3128 // if we have a first head, then we are going to ignore the
3129 // headers for the additional pages, since there aren't any
3130 // in XHTML. this test accomplishes that.
3131 bool const havehead = !havefirsthead && haveLTHead(false);
3132 if (havehead || havefirsthead) {
3133 xs << html::StartTag("thead") << html::CR();
3134 for (row_type r = 0; r < nrows(); ++r) {
3135 if (((havefirsthead && row_info[r].endfirsthead) ||
3136 (havehead && row_info[r].endhead)) &&
3137 !row_info[r].caption) {
3138 ret += xhtmlRow(xs, r, runparams, true);
3141 xs << html::EndTag("thead") << html::CR();
3143 // output footer info
3144 bool const havelastfoot = haveLTLastFoot(false);
3146 bool const havefoot = !havelastfoot && haveLTFoot(false);
3147 if (havefoot || havelastfoot) {
3148 xs << html::StartTag("tfoot") << html::CR();
3149 for (row_type r = 0; r < nrows(); ++r) {
3150 if (((havelastfoot && row_info[r].endlastfoot) ||
3151 (havefoot && row_info[r].endfoot)) &&
3152 !row_info[r].caption) {
3153 ret += xhtmlRow(xs, r, runparams);
3156 xs << html::EndTag("tfoot") << html::CR();
3159 xs << html::StartTag("tbody") << html::CR();
3160 for (row_type r = 0; r < nrows(); ++r) {
3161 if (isValidRow(r)) {
3162 ret += xhtmlRow(xs, r, runparams);
3165 xs << html::EndTag("tbody")
3167 << html::EndTag("table")
3169 if (is_long_tabular)
3170 xs << html::EndTag("div") << html::CR();
3175 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3176 vector<unsigned int> const & clen) const
3178 idx_type const fcell = getFirstCellInRow(row);
3179 idx_type const n = numberOfCellsInRow(row) + fcell;
3182 for (idx_type i = fcell; i < n; ++i) {
3192 for (idx_type i = fcell; i < n; ++i) {
3203 col_type column = cellColumn(i);
3204 int len = clen[column];
3205 while (column < ncols() - 1
3206 && isPartOfMultiColumn(row, ++column))
3207 len += clen[column] + 4;
3208 os << docstring(len, ch);
3223 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3224 vector<unsigned int> const & clen) const
3226 idx_type const fcell = getFirstCellInRow(row);
3227 idx_type const n = numberOfCellsInRow(row) + fcell;
3230 for (idx_type i = fcell; i < n; ++i) {
3231 if (bottomLine(i)) {
3240 for (idx_type i = fcell; i < n; ++i) {
3241 if (bottomLine(i)) {
3251 col_type column = cellColumn(i);
3252 int len = clen[column];
3253 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3254 len += clen[column] + 4;
3255 os << docstring(len, ch);
3256 if (bottomLine(i)) {
3270 void Tabular::plaintextPrintCell(odocstringstream & os,
3271 OutputParams const & runparams,
3272 idx_type cell, row_type row, col_type column,
3273 vector<unsigned int> const & clen,
3274 bool onlydata, size_t max_length) const
3276 odocstringstream sstr;
3277 cellInset(cell)->plaintext(sstr, runparams, max_length);
3289 unsigned int len1 = sstr.str().length();
3290 unsigned int len2 = clen[column];
3291 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3292 len2 += clen[column] + 4;
3295 switch (getAlignment(cell)) {
3297 case LYX_ALIGN_LEFT:
3300 case LYX_ALIGN_RIGHT:
3304 case LYX_ALIGN_CENTER:
3310 os << docstring(len1, ' ') << sstr.str()
3311 << docstring(len2, ' ');
3313 if (rightLine(cell))
3320 void Tabular::plaintext(odocstringstream & os,
3321 OutputParams const & runparams, int const depth,
3322 bool onlydata, char_type delim, size_t max_length) const
3324 // first calculate the width of the single columns
3325 vector<unsigned int> clen(ncols());
3328 // first all non multicolumn cells!
3329 for (col_type c = 0; c < ncols(); ++c) {
3331 for (row_type r = 0; r < nrows(); ++r) {
3332 idx_type cell = cellIndex(r, c);
3333 if (isMultiColumn(cell))
3335 odocstringstream sstr;
3336 cellInset(cell)->plaintext(sstr, runparams, max_length);
3337 if (clen[c] < sstr.str().length())
3338 clen[c] = sstr.str().length();
3341 // then all multicolumn cells!
3342 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 (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3347 odocstringstream sstr;
3348 cellInset(cell)->plaintext(sstr, runparams, max_length);
3349 int len = int(sstr.str().length());
3350 idx_type const n = columnSpan(cell);
3351 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3353 if (len > int(clen[c + n - 1]))
3354 clen[c + n - 1] = len;
3359 for (row_type r = 0; r < nrows(); ++r) {
3360 if (!onlydata && plaintextTopHLine(os, r, clen))
3361 os << docstring(depth * 2, ' ');
3362 for (col_type c = 0; c < ncols(); ++c) {
3363 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3365 if (onlydata && c > 0)
3366 // we don't use operator<< for single UCS4 character.
3367 // see explanation in docstream.h
3369 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3371 if (os.str().size() > max_length)
3376 os << docstring(depth * 2, ' ');
3377 if (plaintextBottomHLine(os, r, clen))
3378 os << docstring(depth * 2, ' ');
3380 if (os.str().size() > max_length)
3386 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3388 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3392 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3394 return cell_info[row][column].inset;
3398 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3400 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3404 void Tabular::setCellInset(row_type row, col_type column,
3405 shared_ptr<InsetTableCell> ins)
3407 CellData & cd = cell_info[row][column];
3412 void Tabular::validate(LaTeXFeatures & features) const
3414 features.require("NeedTabularnewline");
3416 features.require("booktabs");
3417 if (is_long_tabular)
3418 features.require("longtable");
3420 features.require("rotating");
3421 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3422 if (isMultiRow(cell))
3423 features.require("multirow");
3424 if (getVAlignment(cell) != LYX_VALIGN_TOP
3425 || !getPWidth(cell).zero())
3426 features.require("array");
3427 cellInset(cell)->validate(features);
3432 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3434 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3435 ParagraphList::const_iterator cit = parlist.begin();
3436 ParagraphList::const_iterator end = parlist.end();
3438 for (; cit != end; ++cit)
3439 for (int i = 0; i < cit->size(); ++i)
3440 if (cit->isNewline(i))
3447 /////////////////////////////////////////////////////////////////////
3451 /////////////////////////////////////////////////////////////////////
3453 InsetTableCell::InsetTableCell(Buffer * buf)
3454 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3455 contentAlign(LYX_ALIGN_CENTER)
3459 bool InsetTableCell::forcePlainLayout(idx_type) const
3461 return !isFixedWidth;
3465 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3467 return isFixedWidth;
3471 bool InsetTableCell::forceLocalFontSwitch() const
3473 return isFixedWidth;
3477 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3478 FuncStatus & status) const
3480 bool enabled = true;
3481 switch (cmd.action()) {
3482 case LFUN_MATH_DISPLAY:
3483 if (!hasFixedWidth()) {
3488 return InsetText::getStatus(cur, cmd, status);
3490 status.setEnabled(enabled);
3494 docstring InsetTableCell::asString(bool intoInsets)
3497 if (paragraphs().empty())
3499 ParagraphList::const_iterator it = paragraphs().begin();
3500 ParagraphList::const_iterator en = paragraphs().end();
3502 for (; it != en; ++it) {
3507 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3513 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3514 UpdateType utype, TocBackend & backend) const
3516 InsetText::iterateForToc(di, output_active, utype, backend);
3520 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3523 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3524 return InsetText::xhtml(xs, rp);
3529 /////////////////////////////////////////////////////////////////////
3533 /////////////////////////////////////////////////////////////////////
3535 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3537 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3538 rowselect_(false), colselect_(false)
3543 InsetTabular::InsetTabular(InsetTabular const & tab)
3544 : Inset(tab), tabular(tab.tabular),
3545 rowselect_(false), colselect_(false)
3550 InsetTabular::~InsetTabular()
3552 hideDialogs("tabular", this);
3556 void InsetTabular::setBuffer(Buffer & buf)
3558 tabular.setBuffer(buf);
3559 Inset::setBuffer(buf);
3563 bool InsetTabular::insetAllowed(InsetCode code) const
3568 case MATHMACRO_CODE:
3573 return tabular.is_long_tabular;
3581 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3583 return tabular.is_long_tabular &&
3584 (newtype == "Standard" || newtype == "Unnumbered");
3588 void InsetTabular::write(ostream & os) const
3590 os << "Tabular" << endl;
3595 string InsetTabular::contextMenu(BufferView const &, int, int) const
3597 // FIXME: depending on the selection state,
3598 // we could offer a different menu.
3599 return cell(0)->contextMenuName() + ";" + contextMenuName();
3603 string InsetTabular::contextMenuName() const
3605 return "context-tabular";
3609 void InsetTabular::read(Lexer & lex)
3611 //bool const old_format = (lex.getString() == "\\LyXTable");
3619 string token = lex.getString();
3620 while (lex && token != "\\end_inset") {
3622 token = lex.getString();
3625 lex.printError("Missing \\end_inset at this point. ");
3629 int InsetTabular::rowFromY(Cursor & cur, int y) const
3631 // top y coordinate of tabular
3632 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3634 for (; r < tabular.nrows() && y > h; ++r)
3635 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3636 + tabular.interRowSpace(r);
3642 int InsetTabular::columnFromX(Cursor & cur, int x) const
3644 // left x coordinate of tabular
3645 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3647 for (; c < tabular.ncols() && x > w; ++c)
3648 w += tabular.cellWidth(c);
3653 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3655 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3656 // mi.base.textwidth << "\n";
3657 LBUFERR(mi.base.bv);
3659 for (row_type r = 0; r < tabular.nrows(); ++r) {
3662 for (col_type c = 0; c < tabular.ncols(); ++c) {
3663 if (tabular.isPartOfMultiColumn(r, c)
3664 || tabular.isPartOfMultiRow(r, c))
3665 // multicolumn or multirow cell, but not first one
3667 idx_type const cell = tabular.cellIndex(r, c);
3670 Length const p_width = tabular.getPWidth(cell);
3671 if (!p_width.zero())
3672 m.base.textwidth = p_width.inPixels(mi.base);
3673 tabular.cellInset(cell)->metrics(m, dim);
3674 if (!p_width.zero())
3675 dim.wid = m.base.textwidth;
3676 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3677 + tabular.interColumnSpace(cell);
3679 // FIXME(?): do we need a second metrics call?
3680 TextMetrics const & tm =
3681 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3683 // determine horizontal offset because of decimal align (if necessary)
3684 int decimal_width = 0;
3685 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3686 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
3687 tail.setBuffer(tabular.buffer());
3688 // we need to set macrocontext position everywhere
3689 // otherwise we crash with nested insets (e.g. footnotes)
3690 // after decimal point
3691 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3693 dit.push_back(CursorSlice(tail));
3694 tail.setMacrocontextPositionRecursive(dit);
3696 // remove text leading decimal point
3697 docstring const align_d = tabular.column_info[c].decimal_point;
3698 dit = separatorPos(&tail, align_d);
3700 pit_type const psize = tail.paragraphs().front().size();
3702 tail.paragraphs().front().eraseChars(0,
3703 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3705 tail.metrics(m, dim1);
3706 decimal_width = dim1.width();
3709 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3710 tabular.cell_info[r][c].decimal_width = decimal_width;
3712 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3713 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3714 int const lastpardes = tm.last().second->descent()
3715 + TEXT_TO_INSET_OFFSET;
3717 switch (tabular.getVAlignment(cell)) {
3718 case Tabular::LYX_VALIGN_TOP:
3720 case Tabular::LYX_VALIGN_MIDDLE:
3721 offset = -(dim.des - lastpardes)/2;
3723 case Tabular::LYX_VALIGN_BOTTOM:
3724 offset = -(dim.des - lastpardes);
3727 tabular.cell_info[r][c].voffset = offset;
3728 maxasc = max(maxasc, dim.asc - offset);
3729 maxdes = max(maxdes, dim.des + offset);
3731 int const top_space = tabular.row_info[r].top_space_default ?
3732 default_line_space :
3733 tabular.row_info[r].top_space.inPixels(mi.base);
3734 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3735 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3736 default_line_space :
3737 tabular.row_info[r].bottom_space.inPixels(mi.base);
3738 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3741 tabular.updateColumnWidths();
3742 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
3743 dim.des = tabular.height() - dim.asc;
3744 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3748 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3751 if (&cur.inset() == this && cur.selection()) {
3752 if (cur.selIsMultiCell()) {
3755 getSelection(cur, rs, re, cs, ce);
3757 idx_type const cell = tabular.cellIndex(row, col);
3758 col_type const cspan = tabular.columnSpan(cell);
3759 row_type const rspan = tabular.rowSpan(cell);
3760 if (col + cspan - 1 >= cs && col <= ce
3761 && row + rspan - 1 >= rs && row <= re)
3764 if (col == tabular.cellColumn(cur.idx())
3765 && row == tabular.cellRow(cur.idx())) {
3766 CursorSlice const & beg = cur.selBegin();
3767 CursorSlice const & end = cur.selEnd();
3769 if ((end.lastpos() > 0 || end.lastpit() > 0)
3770 && end.pos() == end.lastpos() && beg.pos() == 0
3771 && end.pit() == end.lastpit() && beg.pit() == 0)
3779 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3781 x += ADD_TO_TABULAR_WIDTH;
3783 BufferView * bv = pi.base.bv;
3784 Cursor & cur = pi.base.bv->cursor();
3786 // FIXME: As the full background is painted in drawBackground(),
3787 // we have no choice but to do a full repaint for the Text cells.
3788 pi.full_repaint = true;
3790 bool const original_selection_state = pi.selected;
3794 int yy = y + tabular.offsetVAlignment();
3795 for (row_type r = 0; r < tabular.nrows(); ++r) {
3797 for (col_type c = 0; c < tabular.ncols(); ++c) {
3798 if (tabular.isPartOfMultiColumn(r, c))
3801 idx = tabular.cellIndex(r, c);
3803 if (tabular.isPartOfMultiRow(r, c)) {
3804 nx += tabular.cellWidth(idx);
3808 pi.selected |= isCellSelected(cur, r, c);
3809 int const cx = nx + tabular.textHOffset(idx);
3810 int const cy = yy + tabular.textVOffset(idx);
3811 // Cache the Inset position.
3812 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3813 cell(idx)->draw(pi, cx, cy);
3814 drawCellLines(pi, nx, yy, r, idx);
3815 nx += tabular.cellWidth(idx);
3816 pi.selected = original_selection_state;
3819 if (r + 1 < tabular.nrows())
3820 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3821 + tabular.interRowSpace(r + 1);
3826 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3828 x += ADD_TO_TABULAR_WIDTH;
3829 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
3830 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3831 pi.backgroundColor(this));
3835 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3837 Cursor & cur = pi.base.bv->cursor();
3839 x += ADD_TO_TABULAR_WIDTH;
3841 if (!cur.selection())
3843 if (&cur.inset() != this)
3848 bool const full_cell_selected = isCellSelected(cur,
3849 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3851 if (cur.selIsMultiCell() || full_cell_selected) {
3852 for (row_type r = 0; r < tabular.nrows(); ++r) {
3854 for (col_type c = 0; c < tabular.ncols(); ++c) {
3855 if (tabular.isPartOfMultiColumn(r, c))
3858 idx_type const cell = tabular.cellIndex(r, c);
3860 if (tabular.isPartOfMultiRow(r, c)) {
3861 xx += tabular.cellWidth(cell);
3864 int const w = tabular.cellWidth(cell);
3865 int const h = tabular.cellHeight(cell);
3866 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
3867 if (isCellSelected(cur, r, c))
3868 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3871 if (r + 1 < tabular.nrows())
3872 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3873 + tabular.interRowSpace(r + 1);
3877 // FIXME: This code has no effect because InsetTableCell does not handle
3878 // drawSelection other than the trivial implementation in Inset.
3880 // x += cellXPos(cur.idx());
3881 // x += tabular.textHOffset(cur.idx());
3882 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3887 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3888 row_type row, idx_type cell) const
3890 y -= tabular.rowAscent(row);
3891 int const w = tabular.cellWidth(cell);
3892 int const h = tabular.cellHeight(cell);
3893 Color const linecolor = pi.textColor(Color_tabularline);
3894 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3897 bool drawline = tabular.topLine(cell)
3898 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3899 pi.pain.line(x, y, x + w, y,
3900 drawline ? linecolor : gridcolor,
3901 drawline ? Painter::line_solid : Painter::line_onoffdash);
3904 drawline = tabular.bottomLine(cell);
3905 pi.pain.line(x, y + h, x + w, y + h,
3906 drawline ? linecolor : gridcolor,
3907 drawline ? Painter::line_solid : Painter::line_onoffdash);
3910 col_type const col = tabular.cellColumn(cell);
3911 drawline = tabular.leftLine(cell)
3912 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3913 pi.pain.line(x, y, x, y + h,
3914 drawline ? linecolor : gridcolor,
3915 drawline ? Painter::line_solid : Painter::line_onoffdash);
3918 x -= tabular.interColumnSpace(cell);
3919 col_type next_cell_col = col + 1;
3920 while (next_cell_col < tabular.ncols()
3921 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3923 drawline = tabular.rightLine(cell)
3924 || (next_cell_col < tabular.ncols()
3925 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3926 pi.pain.line(x + w, y, x + w, y + h,
3927 drawline ? linecolor : gridcolor,
3928 drawline ? Painter::line_solid : Painter::line_onoffdash);
3932 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3934 //lyxerr << "InsetTabular::edit: " << this << endl;
3938 if (isRightToLeft(cur))
3939 cur.idx() = tabular.getLastCellInRow(0);
3945 if (isRightToLeft(cur))
3946 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3948 cur.idx() = tabular.numberofcells - 1;
3950 cur.pos() = cur.lastpos(); // FIXME crude guess
3952 cur.setCurrentFont();
3953 // FIXME: this accesses the position cache before it is initialized
3954 //cur.bv().fitCursor();
3958 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3960 // In a longtable, tell captions what the current float is
3961 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3962 string const saveflt = cnts.current_float();
3963 if (tabular.is_long_tabular) {
3964 cnts.current_float("table");
3965 // in longtables, we only step the counter once
3966 cnts.step(from_ascii("table"), utype);
3967 cnts.isLongtable(true);
3970 ParIterator it2 = it;
3972 size_t const end = it2.nargs();
3973 for ( ; it2.idx() < end; it2.top().forwardIdx())
3974 buffer().updateBuffer(it2, utype);
3977 if (tabular.is_long_tabular) {
3978 cnts.current_float(saveflt);
3979 cnts.isLongtable(false);
3984 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
3985 UpdateType utype, TocBackend & backend) const
3987 DocIterator dit = cpit;
3989 size_t const end = dit.nargs();
3990 for ( ; dit.idx() < end; dit.top().forwardIdx())
3991 cell(dit.idx())->addToToc(dit, output_active, utype, backend);
3995 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
3997 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
3998 return x < x0 || x > x0 + tabular.width();
4002 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4004 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4005 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4006 // there is no margin added vertically to tabular insets.
4007 // However, it works for now.
4008 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4012 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4014 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4018 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4020 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4021 << "\n cur:" << cur);
4022 CursorSlice sl = cur.top();
4023 Cursor & bvcur = cur.bv().cursor();
4025 FuncCode const act = cmd.action();
4029 case LFUN_MOUSE_PRESS: {
4030 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4032 if (hitSelectRow(cur.bv(), cmd.x())) {
4033 row_type r = rowFromY(cur, cmd.y());
4034 cur.idx() = tabular.getFirstCellInRow(r);
4038 cur.idx() = tabular.getLastCellInRow(r);
4039 cur.pit() = cur.lastpit();
4040 cur.pos() = cur.lastpos();
4041 cur.selection(true);
4047 if (hitSelectColumn(cur.bv(), cmd.y())) {
4048 col_type c = columnFromX(cur, cmd.x());
4049 cur.idx() = tabular.cellIndex(0, c);
4053 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4054 cur.pit() = cur.lastpit();
4055 cur.pos() = cur.lastpos();
4056 cur.selection(true);
4061 // do not reset cursor/selection if we have selected
4062 // some cells (bug 2715).
4063 if (cmd.button() == mouse_button::button3
4064 && &bvcur.selBegin().inset() == this
4065 && bvcur.selIsMultiCell())
4068 // Let InsetTableCell do it
4069 cell(cur.idx())->dispatch(cur, cmd);
4072 case LFUN_MOUSE_MOTION:
4073 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4074 if (cmd.button() == mouse_button::button1) {
4075 // only accept motions to places not deeper nested than the real anchor
4076 if (!bvcur.realAnchor().hasPart(cur)) {
4080 // select (additional) row
4082 row_type r = rowFromY(cur, cmd.y());
4083 cur.idx() = tabular.getLastCellInRow(r);
4084 // we need to reset the cursor's pit and pos now, as the old ones
4085 // may no longer be valid.
4088 bvcur.setCursor(cur);
4089 bvcur.selection(true);
4092 // select (additional) column
4094 col_type c = columnFromX(cur, cmd.x());
4095 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4096 // we need to reset the cursor's pit and pos now, as the old ones
4097 // may no longer be valid.
4100 bvcur.setCursor(cur);
4101 bvcur.selection(true);
4104 // only update if selection changes
4105 if (bvcur.idx() == cur.idx() &&
4106 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4107 cur.noScreenUpdate();
4108 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4109 bvcur.setCursor(cur);
4110 bvcur.selection(true);
4111 // if this is a multicell selection, we just set the cursor to
4112 // the beginning of the cell's text.
4113 if (bvcur.selIsMultiCell()) {
4114 bvcur.pit() = bvcur.lastpit();
4115 bvcur.pos() = bvcur.lastpos();
4120 case LFUN_MOUSE_RELEASE:
4125 case LFUN_CELL_BACKWARD:
4127 cur.selection(false);
4130 case LFUN_CELL_FORWARD:
4132 cur.selection(false);
4135 case LFUN_CHAR_FORWARD_SELECT:
4136 case LFUN_CHAR_FORWARD:
4137 case LFUN_CHAR_BACKWARD_SELECT:
4138 case LFUN_CHAR_BACKWARD:
4139 case LFUN_CHAR_RIGHT_SELECT:
4140 case LFUN_CHAR_RIGHT:
4141 case LFUN_CHAR_LEFT_SELECT:
4142 case LFUN_CHAR_LEFT:
4143 case LFUN_WORD_FORWARD:
4144 case LFUN_WORD_FORWARD_SELECT:
4145 case LFUN_WORD_BACKWARD:
4146 case LFUN_WORD_BACKWARD_SELECT:
4147 case LFUN_WORD_RIGHT:
4148 case LFUN_WORD_RIGHT_SELECT:
4149 case LFUN_WORD_LEFT:
4150 case LFUN_WORD_LEFT_SELECT: {
4151 // determine whether we move to next or previous cell, where to enter
4152 // the new cell from, and which command to "finish" (i.e., exit the
4155 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4156 FuncCode finish_lfun;
4158 if (act == LFUN_CHAR_FORWARD
4159 || act == LFUN_CHAR_FORWARD_SELECT
4160 || act == LFUN_WORD_FORWARD
4161 || act == LFUN_WORD_FORWARD_SELECT) {
4163 finish_lfun = LFUN_FINISHED_FORWARD;
4165 else if (act == LFUN_CHAR_BACKWARD
4166 || act == LFUN_CHAR_BACKWARD_SELECT
4167 || act == LFUN_WORD_BACKWARD
4168 || act == LFUN_WORD_BACKWARD_SELECT) {
4170 finish_lfun = LFUN_FINISHED_BACKWARD;
4172 // LEFT or RIGHT commands --- the interpretation will depend on the
4173 // table's direction.
4175 bool const right = act == LFUN_CHAR_RIGHT
4176 || act == LFUN_CHAR_RIGHT_SELECT
4177 || act == LFUN_WORD_RIGHT
4178 || act == LFUN_WORD_RIGHT_SELECT;
4179 next_cell = isRightToLeft(cur) != right;
4181 if (lyxrc.visual_cursor)
4182 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4184 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4187 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4188 || act == LFUN_CHAR_BACKWARD_SELECT
4189 || act == LFUN_CHAR_RIGHT_SELECT
4190 || act == LFUN_CHAR_LEFT_SELECT
4191 || act == LFUN_WORD_FORWARD_SELECT
4192 || act == LFUN_WORD_RIGHT_SELECT
4193 || act == LFUN_WORD_BACKWARD_SELECT
4194 || act == LFUN_WORD_LEFT_SELECT;
4196 // If we have a multicell selection or we're
4197 // not doing some LFUN_*_SELECT thing anyway...
4198 if (!cur.selIsMultiCell() || !select) {
4199 col_type const c = tabular.cellColumn(cur.idx());
4200 row_type const r = tabular.cellRow(cur.idx());
4201 // Are we trying to select the whole cell and is the whole cell
4202 // not yet selected?
4203 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4204 ((next_cell && cur.pit() == cur.lastpit()
4205 && cur.pos() == cur.lastpos())
4206 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4208 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4210 // ...try to dispatch to the cell's inset.
4211 cell(cur.idx())->dispatch(cur, cmd);
4213 // When we already have a selection we want to select the whole cell
4214 // before going to the next cell.
4215 if (select_whole && !empty_cell){
4216 getText(cur.idx())->selectAll(cur);
4218 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4222 // FIXME: When we support the selection of an empty cell, remove
4223 // the !empty_cell from this condition. For now we jump to the next
4224 // cell if the current cell is empty.
4225 if (cur.result().dispatched() && !empty_cell)
4229 // move to next/prev cell, as appropriate
4230 // note that we will always do this if we're selecting and we have
4231 // a multicell selection
4232 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4233 << " cell from: " << int(entry_from));
4235 moveNextCell(cur, entry_from);
4237 movePrevCell(cur, entry_from);
4238 // if we're exiting the table, call the appropriate FINISHED lfun
4239 if (sl == cur.top()) {
4240 cmd = FuncRequest(finish_lfun);
4245 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4250 case LFUN_DOWN_SELECT:
4252 if (!(cur.selection() && cur.selIsMultiCell()))
4253 cell(cur.idx())->dispatch(cur, cmd);
4255 cur.dispatched(); // override the cell's decision
4256 if (sl == cur.top()) {
4257 // if our Text didn't do anything to the cursor
4258 // then we try to put the cursor into the cell below
4259 // setting also the right targetX.
4260 cur.selHandle(act == LFUN_DOWN_SELECT);
4261 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4262 int const xtarget = cur.targetX();
4263 // WARNING: Once cur.idx() has been reset, the cursor is in
4264 // an inconsistent state until pos() has been set. Be careful
4265 // what you do with it!
4266 cur.idx() = tabular.cellBelow(cur.idx());
4268 TextMetrics const & tm =
4269 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4270 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4271 cur.setCurrentFont();
4274 if (sl == cur.top()) {
4275 // we trick it to go to forward after leaving the
4277 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4280 if (cur.selIsMultiCell()) {
4281 cur.pit() = cur.lastpit();
4282 cur.pos() = cur.lastpos();
4283 cur.setCurrentFont();
4284 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4287 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4290 case LFUN_UP_SELECT:
4292 if (!(cur.selection() && cur.selIsMultiCell()))
4293 cell(cur.idx())->dispatch(cur, cmd);
4294 cur.dispatched(); // override the cell's decision
4295 if (sl == cur.top()) {
4296 // if our Text didn't do anything to the cursor
4297 // then we try to put the cursor into the cell above
4298 // setting also the right targetX.
4299 cur.selHandle(act == LFUN_UP_SELECT);
4300 if (tabular.cellRow(cur.idx()) != 0) {
4301 int const xtarget = cur.targetX();
4302 // WARNING: Once cur.idx() has been reset, the cursor is in
4303 // an inconsistent state until pos() has been set. Be careful
4304 // what you do with it!
4305 cur.idx() = tabular.cellAbove(cur.idx());
4306 cur.pit() = cur.lastpit();
4307 Text const * text = cell(cur.idx())->getText(0);
4308 TextMetrics const & tm = cur.bv().textMetrics(text);
4309 ParagraphMetrics const & pm =
4310 tm.parMetrics(cur.lastpit());
4311 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4312 cur.setCurrentFont();
4315 if (sl == cur.top()) {
4316 cmd = FuncRequest(LFUN_UP);
4319 if (cur.selIsMultiCell()) {
4321 cur.pos() = cur.lastpos();
4322 cur.setCurrentFont();
4323 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4326 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4329 case LFUN_LAYOUT_TABULAR:
4330 cur.bv().showDialog("tabular");
4333 case LFUN_INSET_MODIFY:
4334 // we come from the dialog
4335 if (cmd.getArg(0) == "tabular")
4336 tabularFeatures(cur, cmd.getLongArg(1));
4341 case LFUN_TABULAR_FEATURE:
4342 tabularFeatures(cur, to_utf8(cmd.argument()));
4345 // insert file functions
4346 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4347 case LFUN_FILE_INSERT_PLAINTEXT:
4349 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4350 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4351 FileName(to_utf8(cmd.argument())));
4354 cur.recordUndoInset();
4355 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4356 // content has been replaced,
4357 // so cursor might be invalid
4358 cur.pos() = cur.lastpos();
4359 cur.pit() = cur.lastpit();
4360 bvcur.setCursor(cur);
4367 if (cur.selIsMultiCell()) {
4368 if (copySelection(cur)) {
4369 cur.recordUndoInset();
4373 cell(cur.idx())->dispatch(cur, cmd);
4376 case LFUN_SELF_INSERT:
4377 if (cur.selIsMultiCell()) {
4378 cur.recordUndoInset();
4380 BufferView * bv = &cur.bv();
4381 docstring::const_iterator cit = cmd.argument().begin();
4382 docstring::const_iterator const end = cmd.argument().end();
4383 for (; cit != end; ++cit)
4384 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4387 bv->bookmarkEditPosition();
4389 cell(cur.idx())->dispatch(cur, cmd);
4392 case LFUN_CHAR_DELETE_BACKWARD:
4393 case LFUN_CHAR_DELETE_FORWARD:
4394 if (cur.selIsMultiCell()) {
4395 cur.recordUndoInset();
4398 cell(cur.idx())->dispatch(cur, cmd);
4402 if (!cur.selection())
4404 if (cur.selIsMultiCell()) {
4408 cell(cur.idx())->dispatch(cur, cmd);
4411 case LFUN_CLIPBOARD_PASTE:
4412 case LFUN_PRIMARY_SELECTION_PASTE: {
4413 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4414 theClipboard().getAsText(Clipboard::PlainTextType) :
4415 theSelection().get();
4418 // pass to InsertPlaintextString, but
4419 // only if we have multi-cell content
4420 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4421 cur.recordUndoInset();
4422 if (insertPlaintextString(cur.bv(), clip, false)) {
4423 // content has been replaced,
4424 // so cursor might be invalid
4425 cur.pos() = cur.lastpos();
4426 cur.pit() = cur.lastpit();
4427 bvcur.setCursor(cur);
4431 // Let the cell handle normal text
4432 cell(cur.idx())->dispatch(cur, cmd);
4437 if (!tabularStackDirty()) {
4438 if (!cur.selIsMultiCell())
4439 cell(cur.idx())->dispatch(cur, cmd);
4442 if (theClipboard().isInternal()) {
4443 cur.recordUndoInset();
4444 pasteClipboard(cur);
4448 case LFUN_FONT_EMPH:
4449 case LFUN_FONT_BOLD:
4450 case LFUN_FONT_BOLDSYMBOL:
4451 case LFUN_FONT_ROMAN:
4452 case LFUN_FONT_NOUN:
4453 case LFUN_FONT_ITAL:
4454 case LFUN_FONT_FRAK:
4455 case LFUN_FONT_TYPEWRITER:
4456 case LFUN_FONT_SANS:
4457 case LFUN_TEXTSTYLE_APPLY:
4458 case LFUN_TEXTSTYLE_UPDATE:
4459 case LFUN_FONT_SIZE:
4460 case LFUN_FONT_UNDERLINE:
4461 case LFUN_FONT_STRIKEOUT:
4462 case LFUN_FONT_CROSSOUT:
4463 case LFUN_FONT_UNDERUNDERLINE:
4464 case LFUN_FONT_UNDERWAVE:
4466 case LFUN_PARAGRAPH_PARAMS_APPLY:
4467 case LFUN_PARAGRAPH_PARAMS:
4468 case LFUN_WORD_CAPITALIZE:
4469 case LFUN_WORD_UPCASE:
4470 case LFUN_WORD_LOWCASE:
4471 case LFUN_CHARS_TRANSPOSE:
4472 if (cur.selIsMultiCell()) {
4475 getSelection(cur, rs, re, cs, ce);
4476 Cursor tmpcur = cur;
4477 for (row_type r = rs; r <= re; ++r) {
4478 for (col_type c = cs; c <= ce; ++c) {
4479 // cursor follows cell:
4480 tmpcur.idx() = tabular.cellIndex(r, c);
4481 // select this cell only:
4484 tmpcur.resetAnchor();
4485 tmpcur.pit() = tmpcur.lastpit();
4486 tmpcur.pos() = tmpcur.top().lastpos();
4487 tmpcur.setCursor(tmpcur);
4488 tmpcur.setSelection();
4489 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4494 cell(cur.idx())->dispatch(cur, cmd);
4498 case LFUN_INSET_SETTINGS:
4499 // relay this lfun to Inset, not to the cell.
4500 Inset::doDispatch(cur, cmd);
4504 // we try to handle this event in the insets dispatch function.
4505 cell(cur.idx())->dispatch(cur, cmd);
4511 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4512 string const & argument, FuncStatus & status) const
4515 int action = Tabular::LAST_ACTION;
4517 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4518 if (tabularFeature[i].feature == s) {
4519 action = tabularFeature[i].action;
4523 if (action == Tabular::LAST_ACTION) {
4525 status.setUnknown(true);
4529 row_type sel_row_start = 0;
4530 row_type sel_row_end = 0;
4531 col_type sel_col_start = 0;
4532 col_type sel_col_end = 0;
4533 Tabular::ltType dummyltt;
4536 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4539 case Tabular::SET_PWIDTH:
4540 case Tabular::SET_MPWIDTH:
4541 case Tabular::SET_SPECIAL_COLUMN:
4542 case Tabular::SET_SPECIAL_MULTICOLUMN:
4543 case Tabular::APPEND_ROW:
4544 case Tabular::APPEND_COLUMN:
4545 case Tabular::DELETE_ROW:
4546 case Tabular::DELETE_COLUMN:
4547 case Tabular::COPY_ROW:
4548 case Tabular::COPY_COLUMN:
4549 case Tabular::SET_TOP_SPACE:
4550 case Tabular::SET_BOTTOM_SPACE:
4551 case Tabular::SET_INTERLINE_SPACE:
4555 case Tabular::SET_TABULAR_WIDTH:
4556 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4557 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4560 case Tabular::MOVE_COLUMN_RIGHT:
4561 case Tabular::MOVE_COLUMN_LEFT:
4562 case Tabular::MOVE_ROW_DOWN:
4563 case Tabular::MOVE_ROW_UP: {
4564 if (cur.selection()) {
4565 status.message(_("Selections not supported."));
4566 status.setEnabled(false);
4570 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4571 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4572 (action == Tabular::MOVE_COLUMN_LEFT &&
4573 tabular.cellColumn(cur.idx()) == 0) ||
4574 (action == Tabular::MOVE_ROW_DOWN &&
4575 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4576 (action == Tabular::MOVE_ROW_UP &&
4577 tabular.cellRow(cur.idx()) == 0)) {
4578 status.setEnabled(false);
4582 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4583 action == Tabular::MOVE_COLUMN_LEFT) {
4584 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4585 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4586 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4587 status.message(_("Multi-column in current or"
4588 " destination column."));
4589 status.setEnabled(false);
4594 if (action == Tabular::MOVE_ROW_DOWN ||
4595 action == Tabular::MOVE_ROW_UP) {
4596 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4597 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4598 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4599 status.message(_("Multi-row in current or"
4600 " destination row."));
4601 status.setEnabled(false);
4606 status.setEnabled(true);
4610 case Tabular::SET_DECIMAL_POINT:
4612 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4615 case Tabular::SET_MULTICOLUMN:
4616 case Tabular::UNSET_MULTICOLUMN:
4617 case Tabular::MULTICOLUMN:
4618 // If a row is set as longtable caption, it must not be allowed
4619 // to unset that this row is a multicolumn.
4620 status.setEnabled(sel_row_start == sel_row_end
4621 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4622 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4625 case Tabular::SET_MULTIROW:
4626 case Tabular::UNSET_MULTIROW:
4627 case Tabular::MULTIROW:
4628 // If a row is set as longtable caption, it must not be allowed
4629 // to unset that this row is a multirow.
4630 status.setEnabled(sel_col_start == sel_col_end
4631 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4632 status.setOnOff(tabular.isMultiRow(cur.idx()));
4635 case Tabular::SET_ALL_LINES:
4636 case Tabular::UNSET_ALL_LINES:
4637 case Tabular::SET_BORDER_LINES:
4638 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4641 case Tabular::SET_LINE_TOP:
4642 case Tabular::SET_LINE_BOTTOM:
4643 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4646 case Tabular::SET_LINE_LEFT:
4647 case Tabular::SET_LINE_RIGHT:
4648 status.setEnabled(!tabular.use_booktabs
4649 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4652 case Tabular::TOGGLE_LINE_TOP:
4653 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4654 status.setOnOff(tabular.topLine(cur.idx()));
4657 case Tabular::TOGGLE_LINE_BOTTOM:
4658 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4659 status.setOnOff(tabular.bottomLine(cur.idx()));
4662 case Tabular::TOGGLE_LINE_LEFT:
4663 status.setEnabled(!tabular.use_booktabs
4664 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4665 status.setOnOff(tabular.leftLine(cur.idx()));
4668 case Tabular::TOGGLE_LINE_RIGHT:
4669 status.setEnabled(!tabular.use_booktabs
4670 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4671 status.setOnOff(tabular.rightLine(cur.idx()));
4674 // multirow cells only inherit the alignment of the column if the column has
4675 // no width, otherwise they are left-aligned
4676 // therefore allow always left but right and center only if there is no width
4677 case Tabular::M_ALIGN_LEFT:
4679 case Tabular::ALIGN_LEFT:
4680 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4683 case Tabular::M_ALIGN_RIGHT:
4685 case Tabular::ALIGN_RIGHT:
4686 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4687 && !tabular.getPWidth(cur.idx()).zero()));
4688 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4691 case Tabular::M_ALIGN_CENTER:
4693 case Tabular::ALIGN_CENTER:
4694 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4695 && !tabular.getPWidth(cur.idx()).zero()));
4696 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4699 case Tabular::ALIGN_BLOCK:
4700 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4701 && !tabular.isMultiRow(cur.idx()));
4702 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4705 case Tabular::ALIGN_DECIMAL:
4706 status.setEnabled(!tabular.isMultiRow(cur.idx())
4707 && !tabular.isMultiColumn(cur.idx()));
4708 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4711 case Tabular::M_VALIGN_TOP:
4713 case Tabular::VALIGN_TOP:
4714 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4715 && !tabular.isMultiRow(cur.idx()));
4717 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4720 case Tabular::M_VALIGN_BOTTOM:
4722 case Tabular::VALIGN_BOTTOM:
4723 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4724 && !tabular.isMultiRow(cur.idx()));
4726 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4729 case Tabular::M_VALIGN_MIDDLE:
4731 case Tabular::VALIGN_MIDDLE:
4732 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4733 && !tabular.isMultiRow(cur.idx()));
4735 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4738 case Tabular::SET_LONGTABULAR:
4739 case Tabular::TOGGLE_LONGTABULAR:
4740 // setting as longtable is not allowed when table is inside a float
4741 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4742 || cur.innerInsetOfType(WRAP_CODE) != 0)
4743 status.setEnabled(false);
4745 status.setEnabled(true);
4746 status.setOnOff(tabular.is_long_tabular);
4749 case Tabular::UNSET_LONGTABULAR:
4750 status.setOnOff(!tabular.is_long_tabular);
4753 case Tabular::TOGGLE_ROTATE_TABULAR:
4754 case Tabular::SET_ROTATE_TABULAR:
4755 status.setOnOff(tabular.rotate != 0);
4758 case Tabular::TABULAR_VALIGN_TOP:
4759 status.setEnabled(tabular.tabular_width.zero());
4760 status.setOnOff(tabular.tabular_valignment
4761 == Tabular::LYX_VALIGN_TOP);
4763 case Tabular::TABULAR_VALIGN_MIDDLE:
4764 status.setEnabled(tabular.tabular_width.zero());
4765 status.setOnOff(tabular.tabular_valignment
4766 == Tabular::LYX_VALIGN_MIDDLE);
4768 case Tabular::TABULAR_VALIGN_BOTTOM:
4769 status.setEnabled(tabular.tabular_width.zero());
4770 status.setOnOff(tabular.tabular_valignment
4771 == Tabular::LYX_VALIGN_BOTTOM);
4774 case Tabular::LONGTABULAR_ALIGN_LEFT:
4775 status.setOnOff(tabular.longtabular_alignment
4776 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4778 case Tabular::LONGTABULAR_ALIGN_CENTER:
4779 status.setOnOff(tabular.longtabular_alignment
4780 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4782 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4783 status.setOnOff(tabular.longtabular_alignment
4784 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4787 case Tabular::UNSET_ROTATE_TABULAR:
4788 status.setOnOff(tabular.rotate == 0);
4791 case Tabular::TOGGLE_ROTATE_CELL:
4792 case Tabular::SET_ROTATE_CELL:
4793 status.setOnOff(!oneCellHasRotationState(false,
4794 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4797 case Tabular::UNSET_ROTATE_CELL:
4798 status.setOnOff(!oneCellHasRotationState(true,
4799 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4802 case Tabular::SET_USEBOX:
4803 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4806 // every row can only be one thing:
4807 // either a footer or header
4808 case Tabular::SET_LTFIRSTHEAD:
4809 status.setEnabled(sel_row_start == sel_row_end);
4810 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4813 case Tabular::UNSET_LTFIRSTHEAD:
4814 status.setEnabled(sel_row_start == sel_row_end);
4815 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4818 case Tabular::SET_LTHEAD:
4819 status.setEnabled(sel_row_start == sel_row_end);
4820 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4823 case Tabular::UNSET_LTHEAD:
4824 status.setEnabled(sel_row_start == sel_row_end);
4825 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4828 case Tabular::SET_LTFOOT:
4829 status.setEnabled(sel_row_start == sel_row_end);
4830 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4833 case Tabular::UNSET_LTFOOT:
4834 status.setEnabled(sel_row_start == sel_row_end);
4835 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4838 case Tabular::SET_LTLASTFOOT:
4839 status.setEnabled(sel_row_start == sel_row_end);
4840 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4843 case Tabular::UNSET_LTLASTFOOT:
4844 status.setEnabled(sel_row_start == sel_row_end);
4845 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4848 case Tabular::SET_LTNEWPAGE:
4849 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4851 case Tabular::UNSET_LTNEWPAGE:
4852 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4855 // only one row in head/firsthead/foot/lasthead can be the caption
4856 // and a multirow cannot be set as caption
4857 case Tabular::SET_LTCAPTION:
4858 status.setEnabled(sel_row_start == sel_row_end
4859 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4860 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4861 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4862 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4863 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4864 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4865 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4866 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4867 && !tabular.isMultiRow(sel_row_start));
4868 status.setOnOff(tabular.ltCaption(sel_row_start));
4871 case Tabular::UNSET_LTCAPTION:
4872 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4875 case Tabular::TOGGLE_LTCAPTION:
4876 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4877 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4878 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4879 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4880 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4881 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4882 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4883 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4884 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4885 status.setOnOff(tabular.ltCaption(sel_row_start));
4888 case Tabular::TOGGLE_BOOKTABS:
4889 case Tabular::SET_BOOKTABS:
4890 status.setOnOff(tabular.use_booktabs);
4893 case Tabular::UNSET_BOOKTABS:
4894 status.setOnOff(!tabular.use_booktabs);
4899 status.setEnabled(false);
4906 // function sets an object as defined in FuncStatus.h:
4907 // states OK, Unknown, Disabled, On, Off.
4908 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4909 FuncStatus & status) const
4911 switch (cmd.action()) {
4912 case LFUN_INSET_MODIFY:
4913 if (cmd.getArg(0) != "tabular")
4915 if (cmd.getArg(1) == "for-dialog") {
4916 // The dialog is asking the status of a command
4917 if (&cur.inset() != this)
4919 string action = cmd.getArg(2);
4920 string arg = cmd.getLongArg(3);
4921 return getFeatureStatus(cur, action, arg, status);
4923 // We always enable the lfun if it is coming from the dialog
4924 // because the dialog makes sure all the settings are valid,
4925 // even though the first argument might not be valid now.
4926 status.setEnabled(true);
4930 case LFUN_TABULAR_FEATURE: {
4931 if (&cur.inset() != this)
4933 string action = cmd.getArg(0);
4934 string arg = cmd.getLongArg(1);
4935 return getFeatureStatus(cur, action, arg, status);
4938 case LFUN_CAPTION_INSERT: {
4939 // caption is only allowed in caption cell of longtable
4940 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4941 status.setEnabled(false);
4944 // only standard caption is allowed
4945 string arg = cmd.getArg(0);
4946 if (!arg.empty() && arg != "Standard") {
4947 status.setEnabled(false);
4950 // check if there is already a caption
4951 bool have_caption = false;
4952 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
4953 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4954 ParagraphList::const_iterator pend = itc.paragraphs().end();
4955 for (; pit != pend; ++pit) {
4956 InsetList::const_iterator it = pit->insetList().begin();
4957 InsetList::const_iterator end = pit->insetList().end();
4958 for (; it != end; ++it) {
4959 if (it->inset->lyxCode() == CAPTION_CODE) {
4960 have_caption = true;
4965 status.setEnabled(!have_caption);
4969 // These are only enabled inside tabular
4970 case LFUN_CELL_BACKWARD:
4971 case LFUN_CELL_FORWARD:
4972 status.setEnabled(true);
4975 // disable these with multiple cells selected
4976 case LFUN_INSET_INSERT:
4977 case LFUN_TABULAR_INSERT:
4978 case LFUN_FLEX_INSERT:
4979 case LFUN_FLOAT_INSERT:
4980 case LFUN_FLOAT_WIDE_INSERT:
4981 case LFUN_FOOTNOTE_INSERT:
4982 case LFUN_MARGINALNOTE_INSERT:
4983 case LFUN_MATH_INSERT:
4984 case LFUN_MATH_MODE:
4985 case LFUN_MATH_MUTATE:
4986 case LFUN_MATH_DISPLAY:
4987 case LFUN_NOTE_INSERT:
4988 case LFUN_ARGUMENT_INSERT:
4989 case LFUN_BOX_INSERT:
4990 case LFUN_BRANCH_INSERT:
4991 case LFUN_PHANTOM_INSERT:
4992 case LFUN_WRAP_INSERT:
4993 case LFUN_PREVIEW_INSERT:
4994 case LFUN_ERT_INSERT: {
4995 if (cur.selIsMultiCell()) {
4996 status.setEnabled(false);
4999 return cell(cur.idx())->getStatus(cur, cmd, status);
5002 // disable in non-fixed-width cells
5003 case LFUN_PARAGRAPH_BREAK:
5004 // multirow does not allow paragraph breaks
5005 if (tabular.isMultiRow(cur.idx())) {
5006 status.setEnabled(false);
5010 case LFUN_NEWLINE_INSERT: {
5011 if (tabular.getPWidth(cur.idx()).zero()) {
5012 status.setEnabled(false);
5015 return cell(cur.idx())->getStatus(cur, cmd, status);
5018 case LFUN_NEWPAGE_INSERT:
5019 status.setEnabled(false);
5023 if (tabularStackDirty() && theClipboard().isInternal()) {
5024 if (cur.selIsMultiCell()) {
5027 getSelection(cur, rs, re, cs, ce);
5028 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5029 && paste_tabular->nrows() == re - rs + 1)
5030 status.setEnabled(true);
5032 status.setEnabled(false);
5033 status.message(_("Selection size should match clipboard content."));
5036 status.setEnabled(true);
5039 return cell(cur.idx())->getStatus(cur, cmd, status);
5041 case LFUN_INSET_SETTINGS:
5042 // relay this lfun to Inset, not to the cell.
5043 return Inset::getStatus(cur, cmd, status);
5046 // we try to handle this event in the insets dispatch function.
5047 return cell(cur.idx())->getStatus(cur, cmd, status);
5053 Inset::DisplayType InsetTabular::display() const
5055 if (tabular.is_long_tabular) {
5056 switch (tabular.longtabular_alignment) {
5057 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5059 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5061 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5071 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5073 tabular.latex(os, runparams);
5077 int InsetTabular::plaintext(odocstringstream & os,
5078 OutputParams const & runparams, size_t max_length) const
5080 os << '\n'; // output table on a new line
5081 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5082 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5083 return PLAINTEXT_NEWLINE;
5087 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5092 // FIXME: Why not pass a proper DocIterator here?
5094 // if the table is inside a float it doesn't need the informaltable
5095 // wrapper. Search for it.
5096 for (master = owner(); master; master = master->owner())
5097 if (master->lyxCode() == FLOAT_CODE)
5102 os << "<informaltable>";
5105 ret += tabular.docbook(os, runparams);
5107 os << "</informaltable>";
5114 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5116 return tabular.xhtml(xs, rp);
5120 void InsetTabular::validate(LaTeXFeatures & features) const
5122 tabular.validate(features);
5123 features.useInsetLayout(getLayout());
5127 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5129 return tabular.cellInset(idx);
5133 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5135 return tabular.cellInset(idx);
5139 void InsetTabular::cursorPos(BufferView const & bv,
5140 CursorSlice const & sl, bool boundary, int & x, int & y) const
5142 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5144 // y offset correction
5145 y += cellYPos(sl.idx());
5146 y += tabular.textVOffset(sl.idx());
5147 y += tabular.offsetVAlignment();
5149 // x offset correction
5150 x += cellXPos(sl.idx());
5151 x += tabular.textHOffset(sl.idx());
5152 x += ADD_TO_TABULAR_WIDTH;
5156 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5160 Inset const & inset = *tabular.cellInset(cell);
5161 Point o = bv.coordCache().getInsets().xy(&inset);
5162 int const xbeg = o.x_ - tabular.textHOffset(cell);
5163 int const xend = xbeg + tabular.cellWidth(cell);
5164 row_type const row = tabular.cellRow(cell);
5165 int const ybeg = o.y_ - tabular.rowAscent(row)
5166 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5167 int const yend = ybeg + tabular.cellHeight(cell);
5179 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5180 // << " ybeg=" << ybeg << " yend=" << yend
5181 // << " xx=" << xx << " yy=" << yy
5182 // << " dist=" << xx + yy << endl;
5187 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5189 //lyxerr << "InsetTabular::editXY: " << this << endl;
5191 cur.idx() = getNearestCell(cur.bv(), x, y);
5192 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5196 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5198 cur.idx() = getNearestCell(cur.bv(), x, y);
5199 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5203 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5205 idx_type idx_min = 0;
5206 int dist_min = numeric_limits<int>::max();
5207 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5208 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5209 int const d = dist(bv, i, x, y);
5220 int InsetTabular::cellYPos(idx_type const cell) const
5222 row_type row = tabular.cellRow(cell);
5224 for (row_type r = 0; r < row; ++r)
5225 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5226 + tabular.interRowSpace(r + 1);
5231 int InsetTabular::cellXPos(idx_type const cell) const
5233 col_type col = tabular.cellColumn(cell);
5235 for (col_type c = 0; c < col; ++c)
5236 lx += tabular.column_info[c].width;
5241 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5243 row_type const row = tabular.cellRow(cur.idx());
5244 col_type const col = tabular.cellColumn(cur.idx());
5246 if (isRightToLeft(cur)) {
5247 if (tabular.cellColumn(cur.idx()) == 0) {
5248 if (row == tabular.nrows() - 1)
5250 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5255 cur.idx() = tabular.getLastCellInRow(row - 1);
5257 cur.idx() = tabular.cellIndex(row, col - 1);
5260 if (tabular.isLastCell(cur.idx()))
5262 if (cur.idx() == tabular.getLastCellInRow(row))
5263 cur.idx() = tabular.cellIndex(row + 1, 0);
5265 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5266 cur.idx() = tabular.cellIndex(row, colnextcell);
5270 cur.boundary(false);
5272 if (cur.selIsMultiCell()) {
5273 cur.pit() = cur.lastpit();
5274 cur.pos() = cur.lastpos();
5281 // in visual mode, place cursor at extreme left or right
5283 switch(entry_from) {
5285 case ENTRY_DIRECTION_RIGHT:
5286 cur.posVisToRowExtremity(false /* !left */);
5288 case ENTRY_DIRECTION_LEFT:
5289 cur.posVisToRowExtremity(true /* left */);
5291 case ENTRY_DIRECTION_IGNORE:
5292 // nothing to do in this case
5296 cur.setCurrentFont();
5300 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5302 row_type const row = tabular.cellRow(cur.idx());
5303 col_type const col = tabular.cellColumn(cur.idx());
5305 if (isRightToLeft(cur)) {
5306 if (cur.idx() == tabular.getLastCellInRow(row)) {
5309 cur.idx() = tabular.getFirstCellInRow(row);
5310 cur.idx() = tabular.cellAbove(cur.idx());
5312 if (tabular.isLastCell(cur.idx()))
5314 if (cur.idx() == tabular.getLastCellInRow(row))
5315 cur.idx() = tabular.cellIndex(row + 1, 0);
5317 cur.idx() = tabular.cellIndex(row, col + 1);
5320 if (cur.idx() == 0) // first cell
5323 cur.idx() = tabular.getLastCellInRow(row - 1);
5325 cur.idx() = tabular.cellIndex(row, col - 1);
5328 if (cur.selIsMultiCell()) {
5329 cur.pit() = cur.lastpit();
5330 cur.pos() = cur.lastpos();
5334 cur.pit() = cur.lastpit();
5335 cur.pos() = cur.lastpos();
5337 // in visual mode, place cursor at extreme left or right
5339 switch(entry_from) {
5341 case ENTRY_DIRECTION_RIGHT:
5342 cur.posVisToRowExtremity(false /* !left */);
5344 case ENTRY_DIRECTION_LEFT:
5345 cur.posVisToRowExtremity(true /* left */);
5347 case ENTRY_DIRECTION_IGNORE:
5348 // nothing to do in this case
5352 cur.setCurrentFont();
5356 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5358 cur.recordUndoInset(this);
5360 istringstream is(argument);
5361 // limit the size of strings we read to avoid memory problems
5365 size_t safe_guard = 0;
5370 if (safe_guard > 1000) {
5371 LYXERR0("parameter max count reached!");
5375 Tabular::Feature action = Tabular::LAST_ACTION;
5378 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5379 if (s != tabularFeature[i].feature)
5381 action = tabularFeature[i].action;
5384 if (action == Tabular::LAST_ACTION) {
5385 LYXERR0("Feature not found " << s);
5389 if (tabularFeature[i].need_value)
5391 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5392 tabularFeatures(cur, action, val);
5397 static void checkLongtableSpecial(Tabular::ltType & ltt,
5398 string const & special, bool & flag)
5400 if (special == "dl_above") {
5403 } else if (special == "dl_below") {
5404 ltt.bottomDL = flag;
5406 } else if (special == "empty") {
5416 bool InsetTabular::oneCellHasRotationState(bool rotated,
5417 row_type row_start, row_type row_end,
5418 col_type col_start, col_type col_end) const
5420 for (row_type r = row_start; r <= row_end; ++r)
5421 for (col_type c = col_start; c <= col_end; ++c)
5423 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5426 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5433 void InsetTabular::tabularFeatures(Cursor & cur,
5434 Tabular::Feature feature, string const & value)
5436 col_type sel_col_start;
5437 col_type sel_col_end;
5438 row_type sel_row_start;
5439 row_type sel_row_end;
5440 bool setLines = false;
5441 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5442 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5446 case Tabular::M_ALIGN_LEFT:
5447 case Tabular::ALIGN_LEFT:
5448 setAlign = LYX_ALIGN_LEFT;
5451 case Tabular::M_ALIGN_RIGHT:
5452 case Tabular::ALIGN_RIGHT:
5453 setAlign = LYX_ALIGN_RIGHT;
5456 case Tabular::M_ALIGN_CENTER:
5457 case Tabular::ALIGN_CENTER:
5458 setAlign = LYX_ALIGN_CENTER;
5461 case Tabular::ALIGN_BLOCK:
5462 setAlign = LYX_ALIGN_BLOCK;
5465 case Tabular::ALIGN_DECIMAL:
5466 setAlign = LYX_ALIGN_DECIMAL;
5469 case Tabular::M_VALIGN_TOP:
5470 case Tabular::VALIGN_TOP:
5471 setVAlign = Tabular::LYX_VALIGN_TOP;
5474 case Tabular::M_VALIGN_BOTTOM:
5475 case Tabular::VALIGN_BOTTOM:
5476 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5479 case Tabular::M_VALIGN_MIDDLE:
5480 case Tabular::VALIGN_MIDDLE:
5481 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5488 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5489 row_type const row = tabular.cellRow(cur.idx());
5490 col_type const column = tabular.cellColumn(cur.idx());
5492 Tabular::ltType ltt;
5496 case Tabular::SET_TABULAR_WIDTH:
5497 tabular.setTabularWidth(Length(value));
5500 case Tabular::SET_PWIDTH: {
5501 Length const len(value);
5502 tabular.setColumnPWidth(cur, cur.idx(), len);
5504 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5505 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5509 case Tabular::SET_MPWIDTH:
5510 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5513 case Tabular::SET_MROFFSET:
5514 tabular.setMROffset(cur, cur.idx(), Length(value));
5517 case Tabular::SET_SPECIAL_COLUMN:
5518 case Tabular::SET_SPECIAL_MULTICOLUMN:
5519 if (value == "none")
5520 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5522 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5525 case Tabular::APPEND_ROW:
5526 // append the row into the tabular
5527 tabular.appendRow(row);
5530 case Tabular::APPEND_COLUMN:
5531 // append the column into the tabular
5532 tabular.appendColumn(column);
5533 cur.idx() = tabular.cellIndex(row, column);
5536 case Tabular::DELETE_ROW:
5537 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5538 for (col_type c = 0; c < tabular.ncols(); c++)
5539 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5540 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5543 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5544 tabular.deleteRow(sel_row_start);
5545 if (sel_row_start >= tabular.nrows())
5547 cur.idx() = tabular.cellIndex(sel_row_start, column);
5550 cur.selection(false);
5553 case Tabular::DELETE_COLUMN:
5554 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5555 for (row_type r = 0; r < tabular.nrows(); r++)
5556 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5557 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5560 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5561 for (row_type r = 0; r < tabular.nrows(); r++)
5562 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5563 tabular.leftLine(tabular.cellIndex(r, 0)));
5566 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5567 tabular.deleteColumn(sel_col_start);
5568 if (sel_col_start >= tabular.ncols())
5570 cur.idx() = tabular.cellIndex(row, sel_col_start);
5573 cur.selection(false);
5576 case Tabular::COPY_ROW:
5577 tabular.copyRow(row);
5580 case Tabular::COPY_COLUMN:
5581 tabular.copyColumn(column);
5582 cur.idx() = tabular.cellIndex(row, column);
5585 case Tabular::MOVE_COLUMN_RIGHT:
5586 tabular.moveColumn(column, Tabular::RIGHT);
5587 cur.idx() = tabular.cellIndex(row, column + 1);
5590 case Tabular::MOVE_COLUMN_LEFT:
5591 tabular.moveColumn(column, Tabular::LEFT);
5592 cur.idx() = tabular.cellIndex(row, column - 1);
5595 case Tabular::MOVE_ROW_DOWN:
5596 tabular.moveRow(row, Tabular::DOWN);
5597 cur.idx() = tabular.cellIndex(row + 1, column);
5600 case Tabular::MOVE_ROW_UP:
5601 tabular.moveRow(row, Tabular::UP);
5602 cur.idx() = tabular.cellIndex(row - 1, column);
5605 case Tabular::SET_LINE_TOP:
5606 case Tabular::TOGGLE_LINE_TOP: {
5607 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5608 ? (value == "true") : !tabular.topLine(cur.idx());
5609 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5610 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5611 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5615 case Tabular::SET_LINE_BOTTOM:
5616 case Tabular::TOGGLE_LINE_BOTTOM: {
5617 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5618 ? (value == "true") : !tabular.bottomLine(cur.idx());
5619 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5620 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5621 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5625 case Tabular::SET_LINE_LEFT:
5626 case Tabular::TOGGLE_LINE_LEFT: {
5627 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5628 ? (value == "true") : !tabular.leftLine(cur.idx());
5629 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5630 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5631 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5635 case Tabular::SET_LINE_RIGHT:
5636 case Tabular::TOGGLE_LINE_RIGHT: {
5637 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5638 ? (value == "true") : !tabular.rightLine(cur.idx());
5639 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5640 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5641 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5645 case Tabular::M_ALIGN_LEFT:
5646 case Tabular::M_ALIGN_RIGHT:
5647 case Tabular::M_ALIGN_CENTER:
5648 case Tabular::ALIGN_LEFT:
5649 case Tabular::ALIGN_RIGHT:
5650 case Tabular::ALIGN_CENTER:
5651 case Tabular::ALIGN_BLOCK:
5652 case Tabular::ALIGN_DECIMAL:
5653 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5654 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5655 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5656 !tabular.getPWidth(c).zero());
5659 case Tabular::M_VALIGN_TOP:
5660 case Tabular::M_VALIGN_BOTTOM:
5661 case Tabular::M_VALIGN_MIDDLE:
5663 case Tabular::VALIGN_TOP:
5664 case Tabular::VALIGN_BOTTOM:
5665 case Tabular::VALIGN_MIDDLE:
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.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5671 case Tabular::SET_MULTICOLUMN: {
5672 if (!cur.selection()) {
5673 // just multicol for one single cell
5674 // check whether we are completely in a multicol
5675 if (!tabular.isMultiColumn(cur.idx()))
5676 tabular.setMultiColumn(cur.idx(), 1,
5677 tabular.rightLine(cur.idx()));
5680 // we have a selection so this means we just add all these
5681 // cells to form a multicolumn cell
5682 idx_type const s_start = cur.selBegin().idx();
5683 row_type const col_start = tabular.cellColumn(s_start);
5684 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5685 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5686 tabular.rightLine(cur.selEnd().idx()));
5689 cur.selection(false);
5693 case Tabular::UNSET_MULTICOLUMN: {
5694 if (!cur.selection()) {
5695 if (tabular.isMultiColumn(cur.idx()))
5696 tabular.unsetMultiColumn(cur.idx());
5701 case Tabular::MULTICOLUMN: {
5702 if (!cur.selection()) {
5703 if (tabular.isMultiColumn(cur.idx()))
5704 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5706 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5710 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5711 row_type const r = sel_row_start;
5712 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5713 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5716 // If the selection contains at least one singlecol cell
5717 // or multiple multicol cells,
5718 // we assume the user will merge is to a single multicol
5720 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5722 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5726 case Tabular::SET_MULTIROW: {
5727 if (!cur.selection()) {
5728 // just multirow for one single cell
5729 // check whether we are completely in a multirow
5730 if (!tabular.isMultiRow(cur.idx()))
5731 tabular.setMultiRow(cur.idx(), 1,
5732 tabular.bottomLine(cur.idx()),
5733 tabular.getAlignment(cur.idx()));
5736 // we have a selection so this means we just add all this
5737 // cells to form a multirow cell
5738 idx_type const s_start = cur.selBegin().idx();
5739 row_type const row_start = tabular.cellRow(s_start);
5740 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5741 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5742 tabular.bottomLine(cur.selEnd().idx()),
5743 tabular.getAlignment(cur.selEnd().idx()));
5746 cur.selection(false);
5750 case Tabular::UNSET_MULTIROW: {
5751 if (!cur.selection()) {
5752 if (tabular.isMultiRow(cur.idx()))
5753 tabular.unsetMultiRow(cur.idx());
5758 case Tabular::MULTIROW: {
5759 if (!cur.selection()) {
5760 if (tabular.isMultiRow(cur.idx()))
5761 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5763 tabularFeatures(cur, Tabular::SET_MULTIROW);
5767 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5768 col_type const c = sel_col_start;
5769 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5770 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5773 // If the selection contains at least one singlerow cell
5774 // or multiple multirow cells,
5775 // we assume the user will merge is to a single multirow
5777 tabularFeatures(cur, Tabular::SET_MULTIROW);
5779 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5783 case Tabular::SET_ALL_LINES:
5785 case Tabular::UNSET_ALL_LINES:
5786 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5787 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5788 idx_type const cell = tabular.cellIndex(r, c);
5789 tabular.setTopLine(cell, setLines);
5790 tabular.setBottomLine(cell, setLines);
5791 tabular.setRightLine(cell, setLines);
5792 tabular.setLeftLine(cell, setLines);
5796 case Tabular::SET_BORDER_LINES:
5797 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5798 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5799 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5801 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5802 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5803 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5807 case Tabular::TOGGLE_LONGTABULAR:
5808 if (tabular.is_long_tabular)
5809 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
5811 tabular.is_long_tabular = true;
5814 case Tabular::SET_LONGTABULAR:
5815 tabular.is_long_tabular = true;
5818 case Tabular::UNSET_LONGTABULAR:
5819 for (row_type r = 0; r < tabular.nrows(); ++r) {
5820 if (tabular.ltCaption(r)) {
5821 cur.idx() = tabular.cellIndex(r, 0);
5824 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5827 tabular.is_long_tabular = false;
5830 case Tabular::SET_ROTATE_TABULAR:
5831 tabular.rotate = convert<int>(value);
5834 case Tabular::UNSET_ROTATE_TABULAR:
5838 case Tabular::TOGGLE_ROTATE_TABULAR:
5839 // when pressing the rotate button we default to 90° rotation
5840 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5843 case Tabular::TABULAR_VALIGN_TOP:
5844 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5847 case Tabular::TABULAR_VALIGN_MIDDLE:
5848 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5851 case Tabular::TABULAR_VALIGN_BOTTOM:
5852 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5855 case Tabular::LONGTABULAR_ALIGN_LEFT:
5856 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5859 case Tabular::LONGTABULAR_ALIGN_CENTER:
5860 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5863 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5864 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5869 case Tabular::SET_ROTATE_CELL:
5870 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5871 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5872 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5875 case Tabular::UNSET_ROTATE_CELL:
5876 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5877 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5878 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5881 case Tabular::TOGGLE_ROTATE_CELL:
5883 bool oneNotRotated = oneCellHasRotationState(false,
5884 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5886 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5887 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5888 // when pressing the rotate cell button we default to 90° rotation
5890 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5892 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5897 case Tabular::SET_USEBOX: {
5898 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5899 if (val == tabular.getUsebox(cur.idx()))
5900 val = Tabular::BOX_NONE;
5901 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5902 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5903 tabular.setUsebox(tabular.cellIndex(r, c), val);
5907 case Tabular::UNSET_LTFIRSTHEAD:
5909 case Tabular::SET_LTFIRSTHEAD:
5910 tabular.getRowOfLTFirstHead(row, ltt);
5911 checkLongtableSpecial(ltt, value, flag);
5912 tabular.setLTHead(row, flag, ltt, true);
5915 case Tabular::UNSET_LTHEAD:
5917 case Tabular::SET_LTHEAD:
5918 tabular.getRowOfLTHead(row, ltt);
5919 checkLongtableSpecial(ltt, value, flag);
5920 tabular.setLTHead(row, flag, ltt, false);
5923 case Tabular::UNSET_LTFOOT:
5925 case Tabular::SET_LTFOOT:
5926 tabular.getRowOfLTFoot(row, ltt);
5927 checkLongtableSpecial(ltt, value, flag);
5928 tabular.setLTFoot(row, flag, ltt, false);
5931 case Tabular::UNSET_LTLASTFOOT:
5933 case Tabular::SET_LTLASTFOOT:
5934 tabular.getRowOfLTLastFoot(row, ltt);
5935 checkLongtableSpecial(ltt, value, flag);
5936 tabular.setLTFoot(row, flag, ltt, true);
5939 case Tabular::UNSET_LTNEWPAGE:
5941 case Tabular::SET_LTNEWPAGE:
5942 tabular.setLTNewPage(row, flag);
5945 case Tabular::SET_LTCAPTION: {
5946 if (tabular.ltCaption(row))
5948 cur.idx() = tabular.setLTCaption(row, true);
5951 cur.selection(false);
5952 // If a row is set as caption, then also insert
5953 // a caption. Otherwise the LaTeX output is broken.
5954 // Select cell if it is non-empty
5955 if (cur.lastpos() > 0 || cur.lastpit() > 0)
5956 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5957 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5961 case Tabular::UNSET_LTCAPTION: {
5962 if (!tabular.ltCaption(row))
5964 cur.idx() = tabular.setLTCaption(row, false);
5967 cur.selection(false);
5968 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5969 if (lyx::getStatus(fr).enabled())
5974 case Tabular::TOGGLE_LTCAPTION: {
5975 if (tabular.ltCaption(row))
5976 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5978 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5982 case Tabular::TOGGLE_BOOKTABS:
5983 tabular.use_booktabs = !tabular.use_booktabs;
5986 case Tabular::SET_BOOKTABS:
5987 tabular.use_booktabs = true;
5990 case Tabular::UNSET_BOOKTABS:
5991 tabular.use_booktabs = false;
5994 case Tabular::SET_TOP_SPACE: {
5996 if (value == "default")
5997 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5998 tabular.row_info[r].top_space_default = true;
5999 else if (value == "none")
6000 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6001 tabular.row_info[r].top_space_default = false;
6002 tabular.row_info[r].top_space = len;
6004 else if (isValidLength(value, &len))
6005 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6006 tabular.row_info[r].top_space_default = false;
6007 tabular.row_info[r].top_space = len;
6012 case Tabular::SET_BOTTOM_SPACE: {
6014 if (value == "default")
6015 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6016 tabular.row_info[r].bottom_space_default = true;
6017 else if (value == "none")
6018 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6019 tabular.row_info[r].bottom_space_default = false;
6020 tabular.row_info[r].bottom_space = len;
6022 else if (isValidLength(value, &len))
6023 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6024 tabular.row_info[r].bottom_space_default = false;
6025 tabular.row_info[r].bottom_space = len;
6030 case Tabular::SET_INTERLINE_SPACE: {
6032 if (value == "default")
6033 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6034 tabular.row_info[r].interline_space_default = true;
6035 else if (value == "none")
6036 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6037 tabular.row_info[r].interline_space_default = false;
6038 tabular.row_info[r].interline_space = len;
6040 else if (isValidLength(value, &len))
6041 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6042 tabular.row_info[r].interline_space_default = false;
6043 tabular.row_info[r].interline_space = len;
6048 case Tabular::SET_DECIMAL_POINT:
6049 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6050 tabular.column_info[c].decimal_point = from_utf8(value);
6053 // dummy stuff just to avoid warnings
6054 case Tabular::LAST_ACTION:
6060 bool InsetTabular::copySelection(Cursor & cur)
6062 if (!cur.selection())
6067 getSelection(cur, rs, re, cs, ce);
6069 paste_tabular.reset(new Tabular(tabular));
6071 for (row_type r = 0; r < rs; ++r)
6072 paste_tabular->deleteRow(0);
6074 row_type const rows = re - rs + 1;
6075 while (paste_tabular->nrows() > rows)
6076 paste_tabular->deleteRow(rows);
6078 for (col_type c = 0; c < cs; ++c)
6079 paste_tabular->deleteColumn(0);
6081 col_type const columns = ce - cs + 1;
6082 while (paste_tabular->ncols() > columns)
6083 paste_tabular->deleteColumn(columns);
6085 paste_tabular->setBuffer(tabular.buffer());
6087 odocstringstream os;
6088 OutputParams const runparams(0);
6089 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6090 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6091 cap::copySelection(cur, os.str());
6093 // mark tabular stack dirty
6094 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6095 // when we (hopefully) have a one-for-all paste mechanism.
6096 // This must be called after cap::copySelection.
6097 dirtyTabularStack(true);
6103 bool InsetTabular::pasteClipboard(Cursor & cur)
6107 col_type actcol = tabular.cellColumn(cur.idx());
6108 row_type actrow = tabular.cellRow(cur.idx());
6110 if (cur.selIsMultiCell()) {
6113 getSelection(cur, actrow, re, actcol, ce);
6116 for (row_type r1 = 0, r2 = actrow;
6117 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6119 for (col_type c1 = 0, c2 = actcol;
6120 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6122 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6123 tabular.isPartOfMultiColumn(r2, c2))
6125 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6129 if (tabular.isPartOfMultiColumn(r2, c2)) {
6133 shared_ptr<InsetTableCell> inset(
6134 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6135 tabular.setCellInset(r2, c2, inset);
6136 // FIXME?: why do we need to do this explicitly? (EL)
6137 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6139 // FIXME: change tracking (MG)
6140 inset->setChange(Change(buffer().params().track_changes ?
6141 Change::INSERTED : Change::UNCHANGED));
6149 void InsetTabular::cutSelection(Cursor & cur)
6151 if (!cur.selection())
6156 getSelection(cur, rs, re, cs, ce);
6157 for (row_type r = rs; r <= re; ++r) {
6158 for (col_type c = cs; c <= ce; ++c) {
6159 shared_ptr<InsetTableCell> t
6160 = cell(tabular.cellIndex(r, c));
6161 if (buffer().params().track_changes)
6162 // FIXME: Change tracking (MG)
6163 t->setChange(Change(Change::DELETED));
6169 // cursor position might be invalid now
6170 if (cur.pit() > cur.lastpit())
6171 cur.pit() = cur.lastpit();
6172 if (cur.pos() > cur.lastpos())
6173 cur.pos() = cur.lastpos();
6174 cur.clearSelection();
6178 bool InsetTabular::isRightToLeft(Cursor & cur) const
6180 // LASSERT: It might be better to abandon this Buffer.
6181 LASSERT(cur.depth() > 1, return false);
6182 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6183 pos_type const parentpos = cur[cur.depth() - 2].pos();
6184 return parentpar.getFontSettings(buffer().params(),
6185 parentpos).language()->rightToLeft();
6189 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6192 LASSERT(stidx <= enidx, return docstring());
6194 col_type const col1 = tabular.cellColumn(stidx);
6195 col_type const col2 = tabular.cellColumn(enidx);
6196 row_type const row1 = tabular.cellRow(stidx);
6197 row_type const row2 = tabular.cellRow(enidx);
6199 for (col_type col = col1; col <= col2; col++)
6200 for (row_type row = row1; row <= row2; row++) {
6205 retval += tabular.cellInset(row, col)->asString(intoInsets);
6211 void InsetTabular::getSelection(Cursor & cur,
6212 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6214 CursorSlice const & beg = cur.selBegin();
6215 CursorSlice const & end = cur.selEnd();
6216 cs = tabular.cellColumn(beg.idx());
6217 ce = tabular.cellColumn(end.idx());
6221 rs = tabular.cellRow(beg.idx());
6222 re = tabular.cellRow(end.idx());
6228 Text * InsetTabular::getText(int idx) const
6230 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6234 void InsetTabular::setChange(Change const & change)
6236 for (idx_type idx = 0; idx < nargs(); ++idx)
6237 cell(idx)->setChange(change);
6241 void InsetTabular::acceptChanges()
6243 for (idx_type idx = 0; idx < nargs(); ++idx)
6244 cell(idx)->acceptChanges();
6248 void InsetTabular::rejectChanges()
6250 for (idx_type idx = 0; idx < nargs(); ++idx)
6251 cell(idx)->rejectChanges();
6255 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6257 return tabular.getPWidth(cell).zero();
6261 bool InsetTabular::forcePlainLayout(idx_type cell) const
6263 return !tabular.getPWidth(cell).zero();
6267 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6270 if (buf.length() <= 0)
6275 col_type maxCols = 1;
6276 size_t const len = buf.length();
6280 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6288 maxCols = max(cols, maxCols);
6294 maxCols = max(cols, maxCols);
6300 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6301 loctab = paste_tabular.get();
6302 dirtyTabularStack(true);
6305 cell = bv.cursor().idx();
6306 ocol = tabular.cellColumn(cell);
6307 row = tabular.cellRow(cell);
6311 idx_type const cells = loctab->numberofcells;
6314 rows = loctab->nrows();
6315 col_type const columns = loctab->ncols();
6317 while (cell < cells && p < len && row < rows &&
6318 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6324 // we can only set this if we are not too far right
6325 if (cols < columns) {
6326 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6327 Font const font = bv.textMetrics(&inset->text()).
6329 inset->setText(buf.substr(op, p - op), font,
6330 buffer().params().track_changes);
6336 // we can only set this if we are not too far right
6337 if (cols < columns) {
6338 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6339 Font const font = bv.textMetrics(&inset->text()).
6341 inset->setText(buf.substr(op, p - op), font,
6342 buffer().params().track_changes);
6347 cell = loctab->cellIndex(row, cols);
6353 // check for the last cell if there is no trailing '\n'
6354 if (cell < cells && op < len) {
6355 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6356 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6357 inset->setText(buf.substr(op, len - op), font,
6358 buffer().params().track_changes);
6364 void InsetTabular::addPreview(DocIterator const & inset_pos,
6365 PreviewLoader & loader) const
6367 DocIterator cell_pos = inset_pos;
6369 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6370 for (row_type r = 0; r < tabular.nrows(); ++r) {
6371 for (col_type c = 0; c < tabular.ncols(); ++c) {
6372 cell_pos.top().idx() = tabular.cellIndex(r, c);
6373 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6379 bool InsetTabular::completionSupported(Cursor const & cur) const
6381 Cursor const & bvCur = cur.bv().cursor();
6382 if (&bvCur.inset() != this)
6384 return cur.text()->completionSupported(cur);
6388 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6390 return completionSupported(cur);
6394 bool InsetTabular::automaticInlineCompletion() const
6396 return lyxrc.completion_inline_text;
6400 bool InsetTabular::automaticPopupCompletion() const
6402 return lyxrc.completion_popup_text;
6406 bool InsetTabular::showCompletionCursor() const
6408 return lyxrc.completion_cursor_text;
6412 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6414 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6418 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6420 if (!completionSupported(cur))
6422 return cur.text()->completionPrefix(cur);
6426 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6428 if (!completionSupported(cur))
6431 return cur.text()->insertCompletion(cur, s, finished);
6435 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6436 Dimension & dim) const
6438 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6439 tm.completionPosAndDim(cur, x, y, dim);
6443 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6445 istringstream data(in);
6447 lex.setStream(data);
6454 if (!lex || token != "tabular") {
6455 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6459 // This is part of the inset proper that is usually swallowed
6460 // by Buffer::readInset
6462 if (!lex || token != "Tabular") {
6463 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6471 string InsetTabular::params2string(InsetTabular const & inset)
6474 data << "tabular" << ' ';
6476 data << "\\end_inset\n";
6481 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6483 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6484 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6485 if (!tabular.isPartOfMultiColumn(r,c) &&
6486 !tabular.isPartOfMultiRow(r,c))
6489 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6490 ParagraphList::iterator it = parlist.begin();
6491 ParagraphList::iterator const en = parlist.end();
6492 for (; it != en; ++it)
6493 it->setLayout(dc.plainLayout());